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.4 2000/06/29 17:09:08 guus Exp $
30 #include <sys/types.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 # include <sys/ioctl.h>
49 /* The name this program was run with. */
52 /* If nonzero, display usage information and exit. */
55 /* If nonzero, print the version on standard output and exit. */
56 static int show_version;
58 /* If nonzero, it will attempt to kill a running tincd and exit. */
59 static int kill_tincd = 0;
61 /* If zero, don't detach from the terminal. */
62 static int do_detach = 1;
64 char *confbase = NULL; /* directory in which all config files are */
65 char *configfilename = NULL; /* configuration file name */
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=FILE Read configuration options from FILE.\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 configfilename = xmalloc(strlen(optarg)+1);
127 strcpy(configfilename, 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)
298 configfilename = xmalloc(strlen(netname)+18+strlen(CONFDIR));
299 sprintf(configfilename, "%s/tinc/%s/tinc.conf", CONFDIR, netname);
303 configfilename = xmalloc(17+strlen(CONFDIR));
304 sprintf(configfilename, "%s/tinc/tinc.conf", CONFDIR);
310 pidfilename = xmalloc(strlen(netname)+20);
311 sprintf(pidfilename, "/var/run/tinc.%s.pid", netname);
312 confbase = xmalloc(strlen(netname)+8+strlen(CONFDIR));
313 sprintf(confbase, "%s/tinc/%s/", CONFDIR, netname);
314 identname = xmalloc(strlen(netname)+7);
315 sprintf(identname, "tinc.%s", netname);
319 pidfilename = "/var/run/tinc.pid";
320 confbase = xmalloc(7+strlen(CONFDIR));
321 sprintf(confbase, "%s/tinc/", CONFDIR);
327 main(int argc, char **argv, char **envp)
329 program_name = argv[0];
331 setlocale (LC_ALL, "");
332 bindtextdomain (PACKAGE, LOCALEDIR);
333 textdomain (PACKAGE);
335 parse_options(argc, argv, envp);
339 printf(_("%s version %s\n"), PACKAGE, VERSION);
340 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans and others,\n"
341 "see the AUTHORS file for a complete list.\n\n"
342 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
343 "and you are welcome to redistribute it under certain conditions;\n"
344 "see the file COPYING for details.\n\n"));
345 printf(_("This product includes software developed by Eric Young (eay@mincom.oz.au)\n"));
355 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
366 if(read_config_file(configfilename))
377 if(setup_network_connections())
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);
420 close_network_connections();
421 remove_pid(pidfilename);
422 execvp(g_argv[0], g_argv);
426 sighup_handler(int a)
429 syslog(LOG_NOTICE, _("Got HUP signal"));
430 close_network_connections();
431 setup_network_connections();
432 /* FIXME: read config-file and re-establish network connections */
436 sigint_handler(int a)
439 syslog(LOG_NOTICE, _("Got INT signal"));
444 sigusr1_handler(int a)
450 sigusr2_handler(int a)
453 syslog(LOG_NOTICE, _("Forcing new key generation"));
461 syslog(LOG_NOTICE, _("Got unexpected signal %d after %s line %d"),
462 a, cp_file, cp_line);
464 syslog(LOG_NOTICE, _("Got unexpected signal %d"), a);
475 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
476 signal(SIGTERM, sigterm_handler);
477 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
478 signal(SIGQUIT, sigquit_handler);
479 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
480 signal(SIGSEGV, sigsegv_handler);
481 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
482 signal(SIGHUP, sighup_handler);
483 signal(SIGPIPE, SIG_IGN);
484 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
485 signal(SIGINT, sigint_handler);
486 signal(SIGUSR1, sigusr1_handler);
487 signal(SIGUSR2, sigusr2_handler);
488 // signal(SIGCHLD, parent_exit);
491 RETSIGTYPE parent_exit(int a)