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.9 2000/09/06 11:49:05 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 *confbase = NULL; /* directory in which all config files are */
67 /* char *configfilename = NULL; /* configuration file name, moved to config.c */
68 char *identname; /* program name for syslog */
69 char *netname = NULL; /* name of the vpn network */
70 char *pidfilename; /* pid file location */
71 static pid_t ppid; /* pid of non-detached part */
72 char **g_argv; /* a copy of the cmdline arguments */
74 void cleanup_and_exit(int);
77 void make_names(void);
78 RETSIGTYPE parent_exit(int a);
79 void setup_signals(void);
80 int write_pidfile(void);
82 static struct option const long_options[] =
84 { "kill", no_argument, NULL, 'k' },
85 { "net", required_argument, NULL, 'n' },
86 { "timeout", required_argument, NULL, 'p' },
87 { "help", no_argument, &show_help, 1 },
88 { "version", no_argument, &show_version, 1 },
89 { "no-detach", no_argument, &do_detach, 0 },
97 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
100 printf(_("Usage: %s [option]...\n\n"), program_name);
101 printf(_(" -c, --config=FILE Read configuration options from FILE.\n"
102 " -D, --no-detach Don't fork and detach.\n"
103 " -d Increase debug level.\n"
104 " -k, --kill Attempt to kill a running tincd and exit.\n"
105 " -n, --net=NETNAME Connect to net NETNAME.\n"
106 " -t, --timeout=TIMEOUT Seconds to wait before giving a timeout.\n"));
107 printf(_(" --help Display this help and exit.\n"
108 " --version Output version information and exit.\n\n"));
109 printf(_("Report bugs to tinc@nl.linux.org.\n"));
115 parse_options(int argc, char **argv, char **envp)
118 int option_index = 0;
121 while((r = getopt_long(argc, argv, "c:Ddkn:t:", long_options, &option_index)) != EOF)
125 case 0: /* long option */
127 case 'c': /* config file */
128 configfilename = xmalloc(strlen(optarg)+1);
129 strcpy(configfilename, optarg);
131 case 'D': /* no detach */
134 case 'd': /* inc debug level */
137 case 'k': /* kill old tincds */
140 case 'n': /* net name given */
141 netname = xmalloc(strlen(optarg)+1);
142 strcpy(netname, optarg);
144 case 't': /* timeout */
145 if(!(p = add_config_val(&config, TYPE_INT, optarg)))
147 printf(_("Invalid timeout value `%s'.\n"), optarg);
159 void memory_full(int size)
161 syslog(LOG_ERR, _("Memory exhausted (last is %s:%d) (couldn't allocate %d bytes), exiting."), cp_file, cp_line, size);
166 Detach from current terminal, write pidfile, kill parent
177 if((pid = fork()) < 0)
182 if(pid) /* parent process */
184 signal(SIGTERM, parent_exit);
185 sleep(600); /* wait 10 minutes */
195 if((fd = open("/dev/tty", O_RDWR)) >= 0)
197 if(ioctl(fd, TIOCNOTTY, NULL))
211 chdir("/"); /* avoid keeping a mointpoint busy */
213 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
216 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
217 VERSION, __DATE__, __TIME__, debug_lvl);
219 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION, debug_lvl);
221 xalloc_fail_func = memory_full;
227 Close network connections, and terminate neatly
229 void cleanup_and_exit(int c)
231 close_network_connections();
234 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
235 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
243 check for an existing tinc for this net, and write pid to pidfile
245 int write_pidfile(void)
249 if((pid = check_pid(pidfilename)))
252 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
255 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
259 /* if it's locked, write-protected, or whatever */
260 if(!write_pid(pidfilename))
267 kill older tincd for this net
273 if(!(pid = read_pid(pidfilename)))
276 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
278 fprintf(stderr, _("No other tincd is running.\n"));
282 errno = 0; /* No error, sometimes errno is only changed on error */
283 /* ESRCH is returned when no process with that pid is found */
284 if(kill(pid, SIGTERM) && errno == ESRCH)
285 fprintf(stderr, _("Removing stale lock file.\n"));
286 remove_pid(pidfilename);
292 Set all files and paths according to netname
294 void make_names(void)
300 asprintf(&configfilename, "%s/tinc/%s/tinc.conf", CONFDIR, netname);
304 asprintf(&configfilename, "%s/tinc/tinc.conf", CONFDIR);
310 asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname);
311 asprintf(&confbase, "%s/tinc/%s/", CONFDIR, netname);
312 asprintf(&identname, "tinc.%s", netname);
316 pidfilename = "/var/run/tinc.pid";
317 asprintf(&confbase, "%s/tinc/", CONFDIR);
323 main(int argc, char **argv, char **envp)
325 program_name = argv[0];
327 setlocale (LC_ALL, "");
328 bindtextdomain (PACKAGE, LOCALEDIR);
329 textdomain (PACKAGE);
331 parse_options(argc, argv, envp);
335 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
336 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans and others,\n"
337 "see the AUTHORS file for a complete list.\n\n"
338 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
339 "and you are welcome to redistribute it under certain conditions;\n"
340 "see the file COPYING for details.\n\n"));
341 printf(_("This product includes software developed by Eric Young (eay@mincom.oz.au)\n"));
351 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
362 if(read_config_file(configfilename))
375 setup_network_connections();
381 syslog(LOG_ERR, _("Unrecoverable error, restarting in %d seconds!"), MAXTIMEOUT);
387 sigterm_handler(int a)
390 syslog(LOG_NOTICE, _("Got TERM signal"));
395 sigquit_handler(int a)
398 syslog(LOG_NOTICE, _("Got QUIT signal"));
403 sigsegv_square(int a)
405 syslog(LOG_NOTICE, _("Got another SEGV signal: not restarting"));
410 sigsegv_handler(int a)
413 syslog(LOG_NOTICE, _("Got SEGV signal after %s line %d, trying to re-execute"),
416 syslog(LOG_NOTICE, _("Got SEGV signal, trying to re-execute"));
418 signal(SIGSEGV, sigsegv_square);
419 close_network_connections();
420 remove_pid(pidfilename);
421 execvp(g_argv[0], g_argv);
425 sighup_handler(int a)
428 syslog(LOG_NOTICE, _("Got HUP signal, rereading configuration and restarting"));
433 sigint_handler(int a)
436 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
441 sigusr1_handler(int a)
447 sigusr2_handler(int a)
450 syslog(LOG_NOTICE, _("Got USR2 signal, forcing new key generation"));
458 syslog(LOG_NOTICE, _("Got unexpected %s after %s line %d"),
459 strsignal(a), cp_file, cp_line);
461 syslog(LOG_NOTICE, _("Got unexpected %s"), strsignal(a));
472 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
473 signal(SIGTERM, sigterm_handler);
474 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
475 signal(SIGQUIT, sigquit_handler);
476 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
477 signal(SIGSEGV, sigsegv_handler);
478 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
479 signal(SIGHUP, sighup_handler);
480 signal(SIGPIPE, SIG_IGN);
481 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
482 signal(SIGINT, sigint_handler);
483 signal(SIGUSR1, sigusr1_handler);
484 signal(SIGUSR2, sigusr2_handler);
485 // signal(SIGCHLD, parent_exit);
488 RETSIGTYPE parent_exit(int a)