2 tincd.c -- the main file for tincd
3 Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Revision 1.5 2000/04/17 16:23:29 zarq
23 * Pass the requested size from xmalloc() and xrealloc() on to xalloc_fail_func()
25 * Revision 1.4 2000/04/06 18:28:29 zarq
26 * New option -D, don't detach.
37 #include <sys/types.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
54 /* The name this program was run with. */
57 /* If nonzero, display usage information and exit. */
60 /* If nonzero, print the version on standard output and exit. */
61 static int show_version;
63 /* If nonzero, it will attempt to kill a running tincd and exit. */
64 static int kill_tincd = 0;
66 /* If zero, don't detach from the terminal. */
67 static int do_detach = 1;
69 char *confbase = NULL; /* directory in which all config files are */
70 char *configfilename = NULL; /* configuration file name */
71 char *identname; /* program name for syslog */
72 char *netname = NULL; /* name of the vpn network */
73 char *pidfilename; /* pid file location */
74 static pid_t ppid; /* pid of non-detached part */
75 char **g_argv; /* a copy of the cmdline arguments */
77 void cleanup_and_exit(int);
80 void make_names(void);
81 RETSIGTYPE parent_exit(int a);
82 void setup_signals(void);
83 int write_pidfile(void);
85 static struct option const long_options[] =
87 { "kill", no_argument, NULL, 'k' },
88 { "net", required_argument, NULL, 'n' },
89 { "timeout", required_argument, NULL, 'p' },
90 { "help", no_argument, &show_help, 1 },
91 { "version", no_argument, &show_version, 1 },
92 { "no-detach", no_argument, &do_detach, 0 },
100 fprintf(stderr, "Try `%s --help\' for more information.\n", program_name);
103 printf("Usage: %s [option]...\n\n", program_name);
104 printf(" -c, --config=FILE Read configuration options from FILE.\n"
105 " -D, --no-detach Don't fork and detach.\n"
106 " -d Increase debug level.\n"
107 " -k, --kill Attempt to kill a running tincd and exit.\n"
108 " -n, --net=NETNAME Connect to net NETNAME.\n"
109 " -t, --timeout=TIMEOUT Seconds to wait before giving a timeout.\n");
110 printf(" --help Display this help and exit.\n"
111 " --version Output version information and exit.\n\n");
112 printf("Report bugs to zarq@iname.com.\n");
118 parse_options(int argc, char **argv, char **envp)
121 int option_index = 0;
124 while((r = getopt_long(argc, argv, "c:Ddkn:t:", long_options, &option_index)) != EOF)
128 case 0: /* long option */
130 case 'c': /* config file */
131 configfilename = xmalloc(strlen(optarg)+1);
132 strcpy(configfilename, optarg);
134 case 'D': /* no detach */
137 case 'd': /* inc debug level */
140 case 'k': /* kill old tincds */
143 case 'n': /* net name given */
144 netname = xmalloc(strlen(optarg)+1);
145 strcpy(netname, optarg);
147 case 't': /* timeout */
148 if(!(p = add_config_val(&config, TYPE_INT, optarg)))
150 printf("Invalid timeout value `%s'.\n", optarg);
162 void memory_full(int size)
164 syslog(LOG_ERR, "Memory exhausted (last is %s:%d) (couldn't allocate %d bytes); exiting.", cp_file, cp_line, size);
169 Detach from current terminal, write pidfile, kill parent
180 if((pid = fork()) < 0)
185 if(pid) /* parent process */
187 signal(SIGTERM, parent_exit);
188 sleep(600); /* wait 10 minutes */
198 if((fd = open("/dev/tty", O_RDWR)) >= 0)
200 if(ioctl(fd, TIOCNOTTY, NULL))
214 chdir("/"); /* avoid keeping a mointpoint busy */
216 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
219 syslog(LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d.",
220 VERSION, __DATE__, __TIME__, debug_lvl);
222 syslog(LOG_NOTICE, "tincd %s starting, debug level %d.", VERSION, debug_lvl);
224 xalloc_fail_func = memory_full;
230 Close network connections, and terminate neatly
232 void cleanup_and_exit(int c)
234 close_network_connections();
237 syslog(LOG_INFO, "Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d.",
238 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
246 check for an existing tinc for this net, and write pid to pidfile
248 int write_pidfile(void)
252 if((pid = check_pid(pidfilename)))
255 fprintf(stderr, "A tincd is already running for net `%s' with pid %d.\n",
258 fprintf(stderr, "A tincd is already running with pid %d.\n", pid);
262 /* if it's locked, write-protected, or whatever */
263 if(!write_pid(pidfilename))
270 kill older tincd for this net
276 if(!(pid = read_pid(pidfilename)))
279 fprintf(stderr, "No other tincd is running for net `%s'.\n", netname);
281 fprintf(stderr, "No other tincd is running.\n");
285 errno = 0; /* No error, sometimes errno is only changed on error */
286 /* ESRCH is returned when no process with that pid is found */
287 if(kill(pid, SIGTERM) && errno == ESRCH)
288 fprintf(stderr, "Removing stale lock file.\n");
289 remove_pid(pidfilename);
295 Set all files and paths according to netname
297 void make_names(void)
303 configfilename = xmalloc(strlen(netname)+18+strlen(CONFDIR));
304 sprintf(configfilename, "%s/tinc/%s/tincd.conf", CONFDIR, netname);
308 configfilename = xmalloc(17+strlen(CONFDIR));
309 sprintf(configfilename, "%s/tinc/tincd.conf", CONFDIR);
315 pidfilename = xmalloc(strlen(netname)+20);
316 sprintf(pidfilename, "/var/run/tincd.%s.pid", netname);
317 confbase = xmalloc(strlen(netname)+8+strlen(CONFDIR));
318 sprintf(confbase, "%s/tinc/%s/", CONFDIR, netname);
319 identname = xmalloc(strlen(netname)+7);
320 sprintf(identname, "tincd.%s", netname);
324 pidfilename = "/var/run/tincd.pid";
325 confbase = xmalloc(7+strlen(CONFDIR));
326 sprintf(confbase, "%s/tinc/", CONFDIR);
332 main(int argc, char **argv, char **envp)
334 program_name = argv[0];
336 parse_options(argc, argv, envp);
340 printf("%s version %s\nCopyright (C) 1998,99 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", PACKAGE, VERSION);
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 keys");
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)