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.11 2000/10/18 20:12:10 zarq 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 *pidfilename; /* pid file location */
68 static pid_t ppid; /* pid of non-detached part */
69 char **g_argv; /* a copy of the cmdline arguments */
71 void cleanup_and_exit(int);
74 void make_names(void);
75 RETSIGTYPE parent_exit(int a);
76 void setup_signals(void);
77 int write_pidfile(void);
79 static struct option const long_options[] =
81 { "kill", no_argument, NULL, 'k' },
82 { "net", required_argument, NULL, 'n' },
83 { "timeout", required_argument, NULL, 'p' },
84 { "help", no_argument, &show_help, 1 },
85 { "version", no_argument, &show_version, 1 },
86 { "no-detach", no_argument, &do_detach, 0 },
94 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
97 printf(_("Usage: %s [option]...\n\n"), program_name);
98 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
99 " -D, --no-detach Don't fork and detach.\n"
100 " -d Increase debug level.\n"
101 " -k, --kill Attempt to kill a running tincd and exit.\n"
102 " -n, --net=NETNAME Connect to net NETNAME.\n"
103 " -t, --timeout=TIMEOUT Seconds to wait before giving a timeout.\n"));
104 printf(_(" --help Display this help and exit.\n"
105 " --version Output version information and exit.\n\n"));
106 printf(_("Report bugs to tinc@nl.linux.org.\n"));
112 parse_options(int argc, char **argv, char **envp)
115 int option_index = 0;
118 while((r = getopt_long(argc, argv, "c:Ddkn:t:", long_options, &option_index)) != EOF)
122 case 0: /* long option */
124 case 'c': /* config file */
125 confbase = xmalloc(strlen(optarg)+1);
126 strcpy(confbase, optarg);
128 case 'D': /* no detach */
131 case 'd': /* inc debug level */
134 case 'k': /* kill old tincds */
137 case 'n': /* net name given */
138 netname = xmalloc(strlen(optarg)+1);
139 strcpy(netname, optarg);
141 case 't': /* timeout */
142 if(!(p = add_config_val(&config, TYPE_INT, optarg)))
144 printf(_("Invalid timeout value `%s'.\n"), optarg);
156 void memory_full(int size)
158 syslog(LOG_ERR, _("Memory exhausted (last is %s:%d) (couldn't allocate %d bytes), exiting."), cp_file, cp_line, size);
163 Detach from current terminal, write pidfile, kill parent
174 if((pid = fork()) < 0)
179 if(pid) /* parent process */
181 signal(SIGTERM, parent_exit);
182 // sleep(600); /* wait 10 minutes */
192 if((fd = open("/dev/tty", O_RDWR)) >= 0)
194 if(ioctl(fd, TIOCNOTTY, NULL))
208 chdir("/"); /* avoid keeping a mointpoint busy */
210 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
213 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
214 VERSION, __DATE__, __TIME__, debug_lvl);
216 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION, debug_lvl);
218 xalloc_fail_func = memory_full;
224 Close network connections, and terminate neatly
226 void cleanup_and_exit(int c)
228 close_network_connections();
231 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
232 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
240 check for an existing tinc for this net, and write pid to pidfile
242 int write_pidfile(void)
246 if((pid = check_pid(pidfilename)))
249 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
252 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
256 /* if it's locked, write-protected, or whatever */
257 if(!write_pid(pidfilename))
264 kill older tincd for this net
270 if(!(pid = read_pid(pidfilename)))
273 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
275 fprintf(stderr, _("No other tincd is running.\n"));
279 errno = 0; /* No error, sometimes errno is only changed on error */
280 /* ESRCH is returned when no process with that pid is found */
281 if(kill(pid, SIGTERM) && errno == ESRCH)
282 fprintf(stderr, _("Removing stale lock file.\n"));
283 remove_pid(pidfilename);
289 Set all files and paths according to netname
291 void make_names(void)
296 asprintf(&pidfilename, "/var/run/tinc.%s.pid", netname);
298 asprintf(&confbase, "%s/tinc/%s", CONFDIR, netname);
300 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 /* Do some intl stuff right now */
325 unknown = _("unknown");
327 parse_options(argc, argv, envp);
331 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
332 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans, Guus Sliepen and others.\n"
333 "See the AUTHORS file for a complete list.\n\n"
334 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
335 "and you are welcome to redistribute it under certain conditions;\n"
336 "see the file COPYING for details.\n"));
346 fprintf(stderr, _("You must be root to run this program. Sorry.\n"));
357 if(read_server_config())
365 /* FIXME: wt* is this suppose to do?
371 if(!setup_network_connections())
377 syslog(LOG_ERR, _("Unrecoverable error"));
382 syslog(LOG_NOTICE, _("Restarting in %d seconds!"), MAXTIMEOUT);
387 syslog(LOG_ERR, _("Aieee! Not restarting."));
394 sigterm_handler(int a)
397 syslog(LOG_NOTICE, _("Got TERM signal"));
402 sigquit_handler(int a)
405 syslog(LOG_NOTICE, _("Got QUIT signal"));
410 sigsegv_square(int a)
412 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
417 sigsegv_handler(int a)
419 syslog(LOG_ERR, _("Got SEGV signal"));
424 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
425 signal(SIGSEGV, sigsegv_square);
426 close_network_connections();
428 remove_pid(pidfilename);
429 execvp(g_argv[0], g_argv);
433 syslog(LOG_NOTICE, _("Aieee! Not restarting."));
439 sighup_handler(int a)
442 syslog(LOG_NOTICE, _("Got HUP signal, rereading configuration and restarting"));
447 sigint_handler(int a)
450 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
455 sigusr1_handler(int a)
461 sigusr2_handler(int a)
464 syslog(LOG_NOTICE, _("Got USR2 signal, forcing new key generation"));
465 /* FIXME: reprogram this.
473 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
485 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
486 signal(SIGTERM, sigterm_handler);
487 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
488 signal(SIGQUIT, sigquit_handler);
489 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
490 signal(SIGSEGV, sigsegv_handler);
491 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
492 signal(SIGHUP, sighup_handler);
493 signal(SIGPIPE, SIG_IGN);
494 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
495 signal(SIGINT, sigint_handler);
496 signal(SIGUSR1, sigusr1_handler);
497 signal(SIGUSR2, sigusr2_handler);
498 // signal(SIGCHLD, parent_exit);
501 RETSIGTYPE parent_exit(int a)