2 process.c -- process management functions
3 Copyright (C) 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: process.c,v 1.1.2.12 2000/11/22 22:18:03 guus Exp $
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
46 #include "connection.h"
50 /* A list containing all our children */
51 list_t *child_pids = NULL;
53 /* If zero, don't detach from the terminal. */
58 extern char *identname;
59 extern char *pidfilename;
62 void init_processes(void)
65 child_pids = list_new();
69 void memory_full(int size)
71 syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exiting."), size);
76 /* Some functions the less gifted operating systems might lack... */
78 #ifndef HAVE_FCLOSEALL
91 int daemon(int nochdir, int noclose)
98 if((pid = fork()) < 0)
103 if(pid) /* parent process */
105 signal(SIGTERM, parent_exit);
106 sleep(600); /* wait 10 minutes */
110 if((fd = open("/dev/tty", O_RDWR)) >= 0)
112 if(ioctl(fd, TIOCNOTTY, NULL))
134 Close network connections, and terminate neatly
136 void cleanup_and_exit(int c)
139 close_network_connections();
141 if(debug_lvl > DEBUG_NOTHING)
142 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
143 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
151 check for an existing tinc for this net, and write pid to pidfile
153 int write_pidfile(void)
157 if((pid = check_pid(pidfilename)))
160 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
163 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
167 /* if it's locked, write-protected, or whatever */
168 if(!write_pid(pidfilename))
175 kill older tincd for this net
181 if(!(pid = read_pid(pidfilename)))
184 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
186 fprintf(stderr, _("No other tincd is running.\n"));
190 errno = 0; /* No error, sometimes errno is only changed on error */
191 /* ESRCH is returned when no process with that pid is found */
192 if(kill(pid, SIGTERM) && errno == ESRCH)
193 fprintf(stderr, _("Removing stale lock file.\n"));
194 remove_pid(pidfilename);
200 Detach from current terminal, write pidfile, kill parent
213 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
215 if(debug_lvl > DEBUG_NOTHING)
216 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
217 VERSION, __DATE__, __TIME__, debug_lvl);
219 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
221 xalloc_fail_func = memory_full;
227 Execute the program name, with sane environment. All output will be
228 redirected to syslog.
230 void _execute_script(const char *name) __attribute__ ((noreturn));
231 void _execute_script(const char *name)
241 asprintf(&s, "NETNAME=%s", netname);
242 putenv(s); /* Don't free s! see man 3 putenv */
251 if(chdir(confbase) < 0)
252 /* This cannot fail since we already read config files from this
254 /* Yes this can fail, somebody could have removed this directory
255 when we didn't pay attention. - Ivo */
258 /* Now if THIS fails, something wicked is going on. - Ivo */
259 syslog(LOG_ERR, _("Couldn't chdir to `/': %m"));
261 /* Continue anyway. */
264 asprintf(&scriptname, "%s/%s", confbase, name);
266 /* Close all file descriptors */
270 /* Open standard input */
271 if((fd = open("/dev/null", O_RDONLY)) < 0)
273 syslog(LOG_ERR, _("Opening `/dev/null' failed: %m"));
278 syslog(LOG_ERR, _("Couldn't assign /dev/null to standard input: %m"));
284 close(1); /* fd #1 should be the first available filedescriptor now. */
285 /* Standard output directly goes to syslog */
286 openlog(name, LOG_CONS | LOG_PID, LOG_DAEMON);
287 /* Standard error as well */
290 syslog(LOG_ERR, _("System call `%s' failed: %m"),
296 if(error && debug_lvl > 1)
297 syslog(LOG_INFO, _("This means that any output the script generates will not be shown in syslog."));
299 execl(scriptname, NULL);
300 /* No return on success */
302 if(errno != ENOENT) /* Ignore if the file does not exist */
303 syslog(LOG_WARNING, _("Error executing `%s': %m"), scriptname);
305 /* No need to free things */
310 Fork and execute the program pointed to by name.
312 int execute_script(const char *name)
316 if((pid = fork()) < 0)
318 syslog(LOG_ERR, _("System call `%s' failed: %m"),
325 list_append(child_pids, &pid);
330 _execute_script(name);
334 Check a child (the pointer data is actually an integer, the PID of
335 that child. A non-zero return value means that the child has exited
336 and can be removed from our list.
338 int check_child(void *data)
344 pid = waitpid(pid, &status, WNOHANG);
345 if(WIFEXITED(status))
347 if(WIFSIGNALED(status)) /* Child was killed by a signal */
349 syslog(LOG_ERR, _("Child with PID %d was killed by signal %d (%s)"),
350 pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
353 if(WEXITSTATUS(status) != 0)
355 syslog(LOG_INFO, _("Child with PID %d exited with code %d"),
356 WEXITSTATUS(status));
361 /* Child is still running */
366 Check the status of all our children.
368 void check_children(void)
370 list_forall_nodes(child_pids, check_child);
379 sigterm_handler(int a)
381 if(debug_lvl > DEBUG_NOTHING)
382 syslog(LOG_NOTICE, _("Got TERM signal"));
388 sigquit_handler(int a)
390 if(debug_lvl > DEBUG_NOTHING)
391 syslog(LOG_NOTICE, _("Got QUIT signal"));
396 sigsegv_square(int a)
398 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
403 sigsegv_handler(int a)
405 syslog(LOG_ERR, _("Got SEGV signal"));
410 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
411 signal(SIGSEGV, sigsegv_square);
412 close_network_connections();
414 remove_pid(pidfilename);
415 execvp(g_argv[0], g_argv);
419 syslog(LOG_NOTICE, _("Not restarting."));
425 sighup_handler(int a)
427 if(debug_lvl > DEBUG_NOTHING)
428 syslog(LOG_NOTICE, _("Got HUP signal"));
433 sigint_handler(int a)
435 if(debug_lvl > DEBUG_NOTHING)
436 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
441 sigusr1_handler(int a)
443 dump_connection_list();
447 sigusr2_handler(int a)
455 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
467 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
468 signal(SIGTERM, sigterm_handler);
469 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
470 signal(SIGQUIT, sigquit_handler);
471 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
472 signal(SIGSEGV, sigsegv_handler);
473 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
474 signal(SIGHUP, sighup_handler);
475 signal(SIGPIPE, SIG_IGN);
476 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
477 signal(SIGINT, sigint_handler);
478 signal(SIGUSR1, sigusr1_handler);
479 signal(SIGUSR2, sigusr2_handler);
480 signal(SIGCHLD, SIG_IGN);
483 RETSIGTYPE parent_exit(int a)