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.
27 #include <sys/types.h>
31 #ifdef HAVE_SYS_IOCTL_H
32 # include <sys/ioctl.h>
44 /* The name this program was run with. */
47 /* If nonzero, display usage information and exit. */
50 /* If nonzero, print the version on standard output and exit. */
51 static int show_version;
53 /* If nonzero, it will attempt to kill a running tincd and exit. */
54 static int kill_tincd = 0;
56 char *confbase = NULL; /* directory in which all config files are */
57 char *configfilename = NULL; /* configuration file name */
58 char *identname; /* program name for syslog */
59 char *netname = NULL; /* name of the vpn network */
60 char *pidfilename; /* pid file location */
61 static pid_t ppid; /* pid of non-detached part */
62 char **g_argv; /* a copy of the cmdline arguments */
64 void cleanup_and_exit(int);
67 void make_names(void);
68 RETSIGTYPE parent_exit(int a);
69 void setup_signals(void);
70 int write_pidfile(void);
72 static struct option const long_options[] =
74 { "kill", no_argument, NULL, 'k' },
75 { "net", required_argument, NULL, 'n' },
76 { "timeout", required_argument, NULL, 'p' },
77 { "help", no_argument, &show_help, 1 },
78 { "version", no_argument, &show_version, 1 },
86 fprintf(stderr, "Try `%s --help\' for more information.\n", program_name);
89 printf("Usage: %s [option]...\n\n", program_name);
90 printf(" -c, --config=FILE Read configuration options from FILE.\n"
91 " -d Increase debug level.\n"
92 " -k, --kill Attempt to kill a running tincd and exit.\n"
93 " -n, --net=NETNAME Connect to net NETNAME.\n"
94 " -t, --timeout=TIMEOUT Seconds to wait before giving a timeout.\n");
95 printf(" --help Display this help and exit.\n"
96 " --version Output version information and exit.\n\n");
97 printf("Report bugs to zarq@iname.com.\n");
103 parse_options(int argc, char **argv, char **envp)
106 int option_index = 0;
109 while((r = getopt_long(argc, argv, "c:dkn:t:", long_options, &option_index)) != EOF)
113 case 0: /* long option */
115 case 'c': /* config file */
116 configfilename = xmalloc(strlen(optarg)+1);
117 strcpy(configfilename, optarg);
119 case 'd': /* inc debug level */
122 case 'k': /* kill old tincds */
125 case 'n': /* net name given */
126 netname = xmalloc(strlen(optarg)+1);
127 strcpy(netname, optarg);
129 case 't': /* timeout */
130 if(!(p = add_config_val(&config, TYPE_INT, optarg)))
132 printf("Invalid timeout value `%s'.\n", optarg);
144 void memory_full(void)
146 syslog(LOG_ERR, "Memory exhausted; exiting.");
151 Detach from current terminal, write pidfile, kill parent
159 if((pid = fork()) < 0)
164 if(pid) /* parent process */
166 signal(SIGTERM, parent_exit);
167 sleep(600); /* wait 10 minutes */
174 if((fd = open("/dev/tty", O_RDWR)) >= 0)
176 if(ioctl(fd, TIOCNOTTY, NULL))
188 chdir("/"); /* avoid keeping a mointpoint busy */
190 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
193 syslog(LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d.",
194 VERSION, __DATE__, __TIME__, debug_lvl);
196 syslog(LOG_NOTICE, "tincd %s starting, debug level %d.", VERSION, debug_lvl);
198 xalloc_fail_func = memory_full;
204 Close network connections, and terminate neatly
206 void cleanup_and_exit(int c)
208 close_network_connections();
211 syslog(LOG_INFO, "Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d.",
212 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
220 check for an existing tinc for this net, and write pid to pidfile
222 int write_pidfile(void)
226 if((pid = check_pid(pidfilename)))
229 fprintf(stderr, "A tincd is already running for net `%s' with pid %d.\n",
232 fprintf(stderr, "A tincd is already running with pid %d.\n", pid);
236 /* if it's locked, write-protected, or whatever */
237 if(!write_pid(pidfilename))
244 kill older tincd for this net
250 if(!(pid = read_pid(pidfilename)))
253 fprintf(stderr, "No other tincd is running for net `%s'.\n", netname);
255 fprintf(stderr, "No other tincd is running.\n");
259 errno = 0; /* No error, sometimes errno is only changed on error */
260 /* ESRCH is returned when no process with that pid is found */
261 if(kill(pid, SIGTERM) && errno == ESRCH)
262 fprintf(stderr, "Removing stale lock file.\n");
263 remove_pid(pidfilename);
269 Set all files and paths according to netname
271 void make_names(void)
277 configfilename = xmalloc(strlen(netname)+18+strlen(CONFDIR));
278 sprintf(configfilename, "%s/tinc/%s/tincd.conf", CONFDIR, netname);
282 configfilename = xmalloc(17+strlen(CONFDIR));
283 sprintf(configfilename, "%s/tinc/tincd.conf", CONFDIR);
289 pidfilename = xmalloc(strlen(netname)+20);
290 sprintf(pidfilename, "/var/run/tincd.%s.pid", netname);
291 confbase = xmalloc(strlen(netname)+8+strlen(CONFDIR));
292 sprintf(confbase, "%s/tinc/%s/", CONFDIR, netname);
293 identname = xmalloc(strlen(netname)+7);
294 sprintf(identname, "tincd.%s", netname);
298 pidfilename = "/var/run/tincd.pid";
299 confbase = xmalloc(7+strlen(CONFDIR));
300 sprintf(confbase, "%s/tinc/", CONFDIR);
306 main(int argc, char **argv, char **envp)
308 program_name = argv[0];
310 parse_options(argc, argv, envp);
314 printf("%s version %s\nCopyright (C) 1998,99 Ivo Timmermans and others,\n"
315 "see the AUTHORS file for a complete list.\n\n"
316 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
317 "and you are welcome to redistribute it under certain conditions;\n"
318 "see the file COPYING for details.\n\n", PACKAGE, VERSION);
319 printf("This product includes software developed by Eric Young (eay@mincom.oz.au)\n");
329 fprintf(stderr, "You must be root to run this program. sorry.\n");
340 if(read_config_file(configfilename))
351 if(setup_network_connections())
361 sigterm_handler(int a)
364 syslog(LOG_NOTICE, "Got TERM signal");
369 sigquit_handler(int a)
372 syslog(LOG_NOTICE, "Got QUIT signal");
377 sigsegv_square(int a)
379 syslog(LOG_NOTICE, "Got another SEGV signal: not restarting");
384 sigsegv_handler(int a)
387 syslog(LOG_NOTICE, "Got SEGV signal after %s line %d. Trying to re-execute.",
390 syslog(LOG_NOTICE, "Got SEGV signal; trying to re-execute.");
392 signal(SIGSEGV, sigsegv_square);
394 close_network_connections();
395 remove_pid(pidfilename);
396 execvp(g_argv[0], g_argv);
400 sighup_handler(int a)
403 syslog(LOG_NOTICE, "Got HUP signal");
404 close_network_connections();
405 setup_network_connections();
406 /* FIXME: read config-file and re-establish network connections */
410 sigint_handler(int a)
413 syslog(LOG_NOTICE, "Got INT signal");
418 sigusr1_handler(int a)
424 sigusr2_handler(int a)
427 syslog(LOG_NOTICE, "Forcing new keys");
435 syslog(LOG_NOTICE, "Got unexpected signal after %s line %d.",
438 syslog(LOG_NOTICE, "Got unexpected signal.");
449 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
450 signal(SIGTERM, sigterm_handler);
451 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
452 signal(SIGQUIT, sigquit_handler);
453 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
454 signal(SIGSEGV, sigsegv_handler);
455 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
456 signal(SIGHUP, sighup_handler);
457 signal(SIGPIPE, SIG_IGN);
458 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
459 signal(SIGINT, sigint_handler);
460 signal(SIGUSR1, sigusr1_handler);
461 signal(SIGUSR2, sigusr2_handler);
464 RETSIGTYPE parent_exit(int a)