2 tincd.c -- the main file for tincd
3 Copyright (C) 1998,1999,2000 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.9 2000/05/29 21:01:26 zarq
23 * Internationalization of tinc.
25 * Revision 1.8 2000/05/14 12:22:42 guus
28 * Revision 1.7 2000/04/27 13:47:51 zarq
29 * Default config file name is tinc.conf, and pidfile is tinc.pid.
31 * Revision 1.6 2000/04/18 16:04:10 zarq
32 * Address for bugreports changed to tinc@nl.linux.org.
34 * Revision 1.5 2000/04/17 16:23:29 zarq
35 * Pass the requested size from xmalloc() and xrealloc() on to xalloc_fail_func()
37 * Revision 1.4 2000/04/06 18:28:29 zarq
38 * New option -D, don't detach.
49 #include <sys/types.h>
53 #ifdef HAVE_SYS_IOCTL_H
54 # include <sys/ioctl.h>
68 /* The name this program was run with. */
71 /* If nonzero, display usage information and exit. */
74 /* If nonzero, print the version on standard output and exit. */
75 static int show_version;
77 /* If nonzero, it will attempt to kill a running tincd and exit. */
78 static int kill_tincd = 0;
80 /* If zero, don't detach from the terminal. */
81 static int do_detach = 1;
83 char *confbase = NULL; /* directory in which all config files are */
84 char *configfilename = NULL; /* configuration file name */
85 char *identname; /* program name for syslog */
86 char *netname = NULL; /* name of the vpn network */
87 char *pidfilename; /* pid file location */
88 static pid_t ppid; /* pid of non-detached part */
89 char **g_argv; /* a copy of the cmdline arguments */
91 void cleanup_and_exit(int);
94 void make_names(void);
95 RETSIGTYPE parent_exit(int a);
96 void setup_signals(void);
97 int write_pidfile(void);
99 static struct option const long_options[] =
101 { "kill", no_argument, NULL, 'k' },
102 { "net", required_argument, NULL, 'n' },
103 { "timeout", required_argument, NULL, 'p' },
104 { "help", no_argument, &show_help, 1 },
105 { "version", no_argument, &show_version, 1 },
106 { "no-detach", no_argument, &do_detach, 0 },
114 fprintf(stderr, _("Try `%s --help\' for more information.\n"), program_name);
117 printf(_("Usage: %s [option]...\n\n"), program_name);
118 printf(_(" -c, --config=FILE Read configuration options from FILE.\n"
119 " -D, --no-detach Don't fork and detach.\n"
120 " -d Increase debug level.\n"
121 " -k, --kill Attempt to kill a running tincd and exit.\n"
122 " -n, --net=NETNAME Connect to net NETNAME.\n"
123 " -t, --timeout=TIMEOUT Seconds to wait before giving a timeout.\n"));
124 printf(_(" --help Display this help and exit.\n"
125 " --version Output version information and exit.\n\n"));
126 printf(_("Report bugs to tinc@nl.linux.org.\n"));
132 parse_options(int argc, char **argv, char **envp)
135 int option_index = 0;
138 while((r = getopt_long(argc, argv, "c:Ddkn:t:", long_options, &option_index)) != EOF)
142 case 0: /* long option */
144 case 'c': /* config file */
145 configfilename = xmalloc(strlen(optarg)+1);
146 strcpy(configfilename, optarg);
148 case 'D': /* no detach */
151 case 'd': /* inc debug level */
154 case 'k': /* kill old tincds */
157 case 'n': /* net name given */
158 netname = xmalloc(strlen(optarg)+1);
159 strcpy(netname, optarg);
161 case 't': /* timeout */
162 if(!(p = add_config_val(&config, TYPE_INT, optarg)))
164 printf(_("Invalid timeout value `%s'.\n"), optarg);
176 void memory_full(int size)
178 syslog(LOG_ERR, _("Memory exhausted (last is %s:%d) (couldn't allocate %d bytes); exiting."), cp_file, cp_line, size);
183 Detach from current terminal, write pidfile, kill parent
194 if((pid = fork()) < 0)
199 if(pid) /* parent process */
201 signal(SIGTERM, parent_exit);
202 sleep(600); /* wait 10 minutes */
212 if((fd = open("/dev/tty", O_RDWR)) >= 0)
214 if(ioctl(fd, TIOCNOTTY, NULL))
228 chdir("/"); /* avoid keeping a mointpoint busy */
230 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
233 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d."),
234 VERSION, __DATE__, __TIME__, debug_lvl);
236 syslog(LOG_NOTICE, _("tincd %s starting, debug level %d."), VERSION, debug_lvl);
238 xalloc_fail_func = memory_full;
244 Close network connections, and terminate neatly
246 void cleanup_and_exit(int c)
248 close_network_connections();
251 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d."),
252 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
260 check for an existing tinc for this net, and write pid to pidfile
262 int write_pidfile(void)
266 if((pid = check_pid(pidfilename)))
269 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
272 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
276 /* if it's locked, write-protected, or whatever */
277 if(!write_pid(pidfilename))
284 kill older tincd for this net
290 if(!(pid = read_pid(pidfilename)))
293 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
295 fprintf(stderr, _("No other tincd is running.\n"));
299 errno = 0; /* No error, sometimes errno is only changed on error */
300 /* ESRCH is returned when no process with that pid is found */
301 if(kill(pid, SIGTERM) && errno == ESRCH)
302 fprintf(stderr, _("Removing stale lock file.\n"));
303 remove_pid(pidfilename);
309 Set all files and paths according to netname
311 void make_names(void)
317 configfilename = xmalloc(strlen(netname)+18+strlen(CONFDIR));
318 sprintf(configfilename, "%s/tinc/%s/tinc.conf", CONFDIR, netname);
322 configfilename = xmalloc(17+strlen(CONFDIR));
323 sprintf(configfilename, "%s/tinc/tinc.conf", CONFDIR);
329 pidfilename = xmalloc(strlen(netname)+20);
330 sprintf(pidfilename, "/var/run/tinc.%s.pid", netname);
331 confbase = xmalloc(strlen(netname)+8+strlen(CONFDIR));
332 sprintf(confbase, "%s/tinc/%s/", CONFDIR, netname);
333 identname = xmalloc(strlen(netname)+7);
334 sprintf(identname, "tinc.%s", netname);
338 pidfilename = "/var/run/tinc.pid";
339 confbase = xmalloc(7+strlen(CONFDIR));
340 sprintf(confbase, "%s/tinc/", CONFDIR);
346 main(int argc, char **argv, char **envp)
348 program_name = argv[0];
350 setlocale (LC_ALL, "");
351 bindtextdomain (PACKAGE, LOCALEDIR);
352 textdomain (PACKAGE);
354 parse_options(argc, argv, envp);
358 printf(_("%s version %s\n"), PACKAGE, VERSION);
359 printf(_("Copyright (C) 1998,1999,2000 Ivo Timmermans and others,\n"
360 "see the AUTHORS file for a complete list.\n\n"
361 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
362 "and you are welcome to redistribute it under certain conditions;\n"
363 "see the file COPYING for details.\n\n"));
364 printf(_("This product includes software developed by Eric Young (eay@mincom.oz.au)\n"));
374 fprintf(stderr, _("You must be root to run this program. sorry.\n"));
385 if(read_config_file(configfilename))
396 if(setup_network_connections())
406 sigterm_handler(int a)
409 syslog(LOG_NOTICE, _("Got TERM signal"));
414 sigquit_handler(int a)
417 syslog(LOG_NOTICE, _("Got QUIT signal"));
422 sigsegv_square(int a)
424 syslog(LOG_NOTICE, _("Got another SEGV signal: not restarting"));
429 sigsegv_handler(int a)
432 syslog(LOG_NOTICE, _("Got SEGV signal after %s line %d. Trying to re-execute."),
435 syslog(LOG_NOTICE, _("Got SEGV signal; trying to re-execute."));
437 signal(SIGSEGV, sigsegv_square);
439 close_network_connections();
440 remove_pid(pidfilename);
441 execvp(g_argv[0], g_argv);
445 sighup_handler(int a)
448 syslog(LOG_NOTICE, _("Got HUP signal"));
449 close_network_connections();
450 setup_network_connections();
451 /* FIXME: read config-file and re-establish network connections */
455 sigint_handler(int a)
458 syslog(LOG_NOTICE, _("Got INT signal"));
463 sigusr1_handler(int a)
469 sigusr2_handler(int a)
472 syslog(LOG_NOTICE, _("Forcing new key generation"));
480 syslog(LOG_NOTICE, _("Got unexpected signal (%d) after %s line %d."),
481 a, cp_file, cp_line);
483 syslog(LOG_NOTICE, _("Got unexpected signal (%d)."), a);
494 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
495 signal(SIGTERM, sigterm_handler);
496 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
497 signal(SIGQUIT, sigquit_handler);
498 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
499 signal(SIGSEGV, sigsegv_handler);
500 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
501 signal(SIGHUP, sighup_handler);
502 signal(SIGPIPE, SIG_IGN);
503 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
504 signal(SIGINT, sigint_handler);
505 signal(SIGUSR1, sigusr1_handler);
506 signal(SIGUSR2, sigusr2_handler);
507 signal(SIGCHLD, parent_exit);
510 RETSIGTYPE parent_exit(int a)