2 tincd.c -- the main file for tincd
3 Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: tincd.c,v 1.10.4.11 2000/10/14 17:04:16 guus Exp $
30 #include <sys/types.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
51 /* The name this program was run with. */
54 /* If nonzero, display usage information and exit. */
57 /* If nonzero, print the version on standard output and exit. */
58 static int show_version;
60 /* If nonzero, it will attempt to kill a running tincd and exit. */
61 static int kill_tincd = 0;
63 /* If zero, don't detach from the terminal. */
64 static int do_detach = 1;
66 char *identname; /* program name for syslog */
67 char *netname = NULL; /* name of the vpn network */
68 char *pidfilename; /* pid file location */
69 static pid_t ppid; /* pid of non-detached part */
70 char **g_argv; /* a copy of the cmdline arguments */
72 void cleanup_and_exit(int);
75 void make_names(void);
76 RETSIGTYPE parent_exit(int a);
77 void setup_signals(void);
78 int write_pidfile(void);
80 static struct option const long_options[] =
82 { "kill", no_argument, NULL, 'k' },
83 { "net", required_argument, NULL, 'n' },
84 { "timeout", required_argument, NULL, 'p' },
85 { "help", no_argument, &show_help, 1 },
86 { "version", no_argument, &show_version, 1 },
87 { "no-detach", no_argument, &do_detach, 0 },
95 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
98 printf(_("Usage: %s [option]...\n\n"), program_name);
99 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
100 " -D, --no-detach Don't fork and detach.\n"
101 " -d Increase debug level.\n"
102 " -k, --kill Attempt to kill a running tincd and exit.\n"
103 " -n, --net=NETNAME Connect to net NETNAME.\n"
104 " -t, --timeout=TIMEOUT Seconds to wait before giving a timeout.\n"));
105 printf(_(" --help Display this help and exit.\n"
106 " --version Output version information and exit.\n\n"));
107 printf(_("Report bugs to tinc@nl.linux.org.\n"));
113 parse_options(int argc, char **argv, char **envp)
116 int option_index = 0;
119 while((r = getopt_long(argc, argv, "c:Ddkn:t:", long_options, &option_index)) != EOF)
123 case 0: /* long option */
125 case 'c': /* config file */
126 confbase = xmalloc(strlen(optarg)+1);
127 strcpy(confbase, optarg);
129 case 'D': /* no detach */
132 case 'd': /* inc debug level */
135 case 'k': /* kill old tincds */
138 case 'n': /* net name given */
139 netname = xmalloc(strlen(optarg)+1);
140 strcpy(netname, optarg);
142 case 't': /* timeout */
143 if(!(p = add_config_val(&config, TYPE_INT, optarg)))
145 printf(_("Invalid timeout value `%s'.\n"), optarg);
157 void memory_full(int size)
159 syslog(LOG_ERR, _("Memory exhausted (last is %s:%d) (couldn't allocate %d bytes), exiting."), cp_file, cp_line, size);
164 Detach from current terminal, write pidfile, kill parent
175 if((pid = fork()) < 0)
180 if(pid) /* parent process */
182 signal(SIGTERM, parent_exit);
183 sleep(600); /* wait 10 minutes */
193 if((fd = open("/dev/tty", O_RDWR)) >= 0)
195 if(ioctl(fd, TIOCNOTTY, NULL))
209 chdir("/"); /* avoid keeping a mointpoint busy */
211 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
214 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
215 VERSION, __DATE__, __TIME__, debug_lvl);
217 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION, debug_lvl);
219 xalloc_fail_func = memory_full;
225 Close network connections, and terminate neatly
227 void cleanup_and_exit(int c)
229 close_network_connections();
232 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
233 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
241 check for an existing tinc for this net, and write pid to pidfile
243 int write_pidfile(void)
247 if((pid = check_pid(pidfilename)))
250 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
253 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
257 /* if it's locked, write-protected, or whatever */
258 if(!write_pid(pidfilename))
265 kill older tincd for this net
271 if(!(pid = read_pid(pidfilename)))
274 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
276 fprintf(stderr, _("No other tincd is running.\n"));
280 errno = 0; /* No error, sometimes errno is only changed on error */
281 /* ESRCH is returned when no process with that pid is found */
282 if(kill(pid, SIGTERM) && errno == ESRCH)
283 fprintf(stderr, _("Removing stale lock file.\n"));
284 remove_pid(pidfilename);
290 Set all files and paths according to netname
292 void make_names(void)
297 asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname);
299 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
301 asprintf(&identname, "tinc.%s", netname);
306 pidfilename = "/var/run/tinc.pid";
308 asprintf(&confbase, "%s/tinc", CONFDIR);
315 main(int argc, char **argv, char **envp)
317 program_name = argv[0];
319 setlocale (LC_ALL, "");
320 bindtextdomain (PACKAGE, LOCALEDIR);
321 textdomain (PACKAGE);
323 parse_options(argc, argv, envp);
327 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
328 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans and others,\n"
329 "see the AUTHORS file for a complete list.\n\n"
330 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
331 "and you are welcome to redistribute it under certain conditions;\n"
332 "see the file COPYING for details.\n\n"));
333 printf(_("This product includes software developed by Eric Young (eay@mincom.oz.au)\n"));
343 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
354 if(read_server_config())
362 /* FIXME: wt* is this suppose to do?
368 setup_network_connections();
374 syslog(LOG_ERR, _("Unrecoverable error, restarting in %d seconds!"), MAXTIMEOUT);
380 sigterm_handler(int a)
383 syslog(LOG_NOTICE, _("Got TERM signal"));
388 sigquit_handler(int a)
391 syslog(LOG_NOTICE, _("Got QUIT signal"));
396 sigsegv_square(int a)
398 syslog(LOG_NOTICE, _("Got another SEGV signal: not restarting"));
403 sigsegv_handler(int a)
406 syslog(LOG_NOTICE, _("Got SEGV signal after %s line %d, trying to re-execute"),
409 syslog(LOG_NOTICE, _("Got SEGV signal, trying to re-execute"));
411 signal(SIGSEGV, sigsegv_square);
412 close_network_connections();
413 remove_pid(pidfilename);
414 execvp(g_argv[0], g_argv);
418 sighup_handler(int a)
421 syslog(LOG_NOTICE, _("Got HUP signal, rereading configuration and restarting"));
426 sigint_handler(int a)
429 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
434 sigusr1_handler(int a)
440 sigusr2_handler(int a)
443 syslog(LOG_NOTICE, _("Got USR2 signal, forcing new key generation"));
444 /* FIXME: reprogram this.
453 syslog(LOG_NOTICE, _("Got unexpected %s after %s line %d"),
454 strsignal(a), cp_file, cp_line);
456 syslog(LOG_NOTICE, _("Got unexpected %s"), strsignal(a));
467 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
468 signal(SIGTERM, sigterm_handler);
469 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
470 signal(SIGQUIT, sigquit_handler);
471 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
472 signal(SIGSEGV, sigsegv_handler);
473 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
474 signal(SIGHUP, sighup_handler);
475 signal(SIGPIPE, SIG_IGN);
476 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
477 signal(SIGINT, sigint_handler);
478 signal(SIGUSR1, sigusr1_handler);
479 signal(SIGUSR2, sigusr2_handler);
480 // signal(SIGCHLD, parent_exit);
483 RETSIGTYPE parent_exit(int a)