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.8 2000/08/17 16:51:08 guus Exp $
30 #include <sys/types.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 # include <sys/ioctl.h>
50 /* The name this program was run with. */
53 /* If nonzero, display usage information and exit. */
56 /* If nonzero, print the version on standard output and exit. */
57 static int show_version;
59 /* If nonzero, it will attempt to kill a running tincd and exit. */
60 static int kill_tincd = 0;
62 /* If zero, don't detach from the terminal. */
63 static int do_detach = 1;
65 char *confbase = NULL; /* directory in which all config files are */
66 /* char *configfilename = NULL; /* configuration file name, moved to config.c */
67 char *identname; /* program name for syslog */
68 char *netname = NULL; /* name of the vpn network */
69 char *pidfilename; /* pid file location */
70 static pid_t ppid; /* pid of non-detached part */
71 char **g_argv; /* a copy of the cmdline arguments */
73 void cleanup_and_exit(int);
76 void make_names(void);
77 RETSIGTYPE parent_exit(int a);
78 void setup_signals(void);
79 int write_pidfile(void);
81 static struct option const long_options[] =
83 { "kill", no_argument, NULL, 'k' },
84 { "net", required_argument, NULL, 'n' },
85 { "timeout", required_argument, NULL, 'p' },
86 { "help", no_argument, &show_help, 1 },
87 { "version", no_argument, &show_version, 1 },
88 { "no-detach", no_argument, &do_detach, 0 },
96 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
99 printf(_("Usage: %s [option]...\n\n"), program_name);
100 printf(_(" -c, --config=FILE Read configuration options from FILE.\n"
101 " -D, --no-detach Don't fork and detach.\n"
102 " -d Increase debug level.\n"
103 " -k, --kill Attempt to kill a running tincd and exit.\n"
104 " -n, --net=NETNAME Connect to net NETNAME.\n"
105 " -t, --timeout=TIMEOUT Seconds to wait before giving a timeout.\n"));
106 printf(_(" --help Display this help and exit.\n"
107 " --version Output version information and exit.\n\n"));
108 printf(_("Report bugs to tinc@nl.linux.org.\n"));
114 parse_options(int argc, char **argv, char **envp)
117 int option_index = 0;
120 while((r = getopt_long(argc, argv, "c:Ddkn:t:", long_options, &option_index)) != EOF)
124 case 0: /* long option */
126 case 'c': /* config file */
127 configfilename = xmalloc(strlen(optarg)+1);
128 strcpy(configfilename, optarg);
130 case 'D': /* no detach */
133 case 'd': /* inc debug level */
136 case 'k': /* kill old tincds */
139 case 'n': /* net name given */
140 netname = xmalloc(strlen(optarg)+1);
141 strcpy(netname, optarg);
143 case 't': /* timeout */
144 if(!(p = add_config_val(&config, TYPE_INT, optarg)))
146 printf(_("Invalid timeout value `%s'.\n"), optarg);
158 void memory_full(int size)
160 syslog(LOG_ERR, _("Memory exhausted (last is %s:%d) (couldn't allocate %d bytes), exiting."), cp_file, cp_line, size);
165 Detach from current terminal, write pidfile, kill parent
176 if((pid = fork()) < 0)
181 if(pid) /* parent process */
183 signal(SIGTERM, parent_exit);
184 sleep(600); /* wait 10 minutes */
194 if((fd = open("/dev/tty", O_RDWR)) >= 0)
196 if(ioctl(fd, TIOCNOTTY, NULL))
210 chdir("/"); /* avoid keeping a mointpoint busy */
212 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
215 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
216 VERSION, __DATE__, __TIME__, debug_lvl);
218 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION, debug_lvl);
220 xalloc_fail_func = memory_full;
226 Close network connections, and terminate neatly
228 void cleanup_and_exit(int c)
230 close_network_connections();
233 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
234 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
242 check for an existing tinc for this net, and write pid to pidfile
244 int write_pidfile(void)
248 if((pid = check_pid(pidfilename)))
251 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
254 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
258 /* if it's locked, write-protected, or whatever */
259 if(!write_pid(pidfilename))
266 kill older tincd for this net
272 if(!(pid = read_pid(pidfilename)))
275 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
277 fprintf(stderr, _("No other tincd is running.\n"));
281 errno = 0; /* No error, sometimes errno is only changed on error */
282 /* ESRCH is returned when no process with that pid is found */
283 if(kill(pid, SIGTERM) && errno == ESRCH)
284 fprintf(stderr, _("Removing stale lock file.\n"));
285 remove_pid(pidfilename);
291 Set all files and paths according to netname
293 void make_names(void)
299 asprintf(&configfilename, "%s/tinc/%s/tinc.conf", CONFDIR, netname);
303 asprintf(&configfilename, "%s/tinc/tinc.conf", CONFDIR);
309 asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname);
310 asprintf(&confbase, "%s/tinc/%s/", CONFDIR, netname);
311 asprintf(&identname, "tinc.%s", netname);
315 pidfilename = "/var/run/tinc.pid";
316 asprintf(&confbase, "%s/tinc/", CONFDIR);
322 main(int argc, char **argv, char **envp)
324 program_name = argv[0];
326 setlocale (LC_ALL, "");
327 bindtextdomain (PACKAGE, LOCALEDIR);
328 textdomain (PACKAGE);
330 parse_options(argc, argv, envp);
334 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
335 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans and others,\n"
336 "see the AUTHORS file for a complete list.\n\n"
337 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
338 "and you are welcome to redistribute it under certain conditions;\n"
339 "see the file COPYING for details.\n\n"));
340 printf(_("This product includes software developed by Eric Young (eay@mincom.oz.au)\n"));
350 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
361 if(read_config_file(configfilename))
374 setup_network_connections();
380 syslog(LOG_ERR, _("Unrecoverable error, restarting in %d seconds!"), MAXTIMEOUT);
386 sigterm_handler(int a)
389 syslog(LOG_NOTICE, _("Got TERM signal"));
394 sigquit_handler(int a)
397 syslog(LOG_NOTICE, _("Got QUIT signal"));
402 sigsegv_square(int a)
404 syslog(LOG_NOTICE, _("Got another SEGV signal: not restarting"));
409 sigsegv_handler(int a)
412 syslog(LOG_NOTICE, _("Got SEGV signal after %s line %d, trying to re-execute"),
415 syslog(LOG_NOTICE, _("Got SEGV signal, trying to re-execute"));
417 signal(SIGSEGV, sigsegv_square);
418 close_network_connections();
419 remove_pid(pidfilename);
420 execvp(g_argv[0], g_argv);
424 sighup_handler(int a)
427 syslog(LOG_NOTICE, _("Got HUP signal, rereading configuration and restarting"));
432 sigint_handler(int a)
435 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
440 sigusr1_handler(int a)
446 sigusr2_handler(int a)
449 syslog(LOG_NOTICE, _("Got USR2 signal, forcing new key generation"));
457 syslog(LOG_NOTICE, _("Got unexpected signal %d after %s line %d"),
458 a, cp_file, cp_line);
460 syslog(LOG_NOTICE, _("Got unexpected signal %d"), a);
471 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
472 signal(SIGTERM, sigterm_handler);
473 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
474 signal(SIGQUIT, sigquit_handler);
475 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
476 signal(SIGSEGV, sigsegv_handler);
477 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
478 signal(SIGHUP, sighup_handler);
479 signal(SIGPIPE, SIG_IGN);
480 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
481 signal(SIGINT, sigint_handler);
482 signal(SIGUSR1, sigusr1_handler);
483 signal(SIGUSR2, sigusr2_handler);
484 // signal(SIGCHLD, parent_exit);
487 RETSIGTYPE parent_exit(int a)