2 process.c -- process management functions
3 Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 Guus Sliepen <guus@sliepen.eu.org>
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.45 2002/09/09 19:39:59 guus Exp $
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
47 #include "connection.h"
52 /* If zero, don't detach from the terminal. */
55 extern char *identname;
56 extern char *pidfilename;
61 static int saved_debug_lvl = 0;
67 void memory_full(int size)
69 syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size);
74 /* Some functions the less gifted operating systems might lack... */
76 #ifndef HAVE_FCLOSEALL
90 Close network connections, and terminate neatly
92 void cleanup_and_exit(int c)
95 close_network_connections();
97 if(debug_lvl > DEBUG_NOTHING)
100 syslog(LOG_NOTICE, _("Terminating"));
107 check for an existing tinc for this net, and write pid to pidfile
109 int write_pidfile(void)
113 pid = check_pid(pidfilename);
118 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
121 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
125 /* if it's locked, write-protected, or whatever */
126 if(!write_pid(pidfilename))
133 kill older tincd for this net
135 int kill_other(int signal)
139 pid = read_pid(pidfilename);
144 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
146 fprintf(stderr, _("No other tincd is running.\n"));
150 errno = 0; /* No error, sometimes errno is only changed on error */
151 /* ESRCH is returned when no process with that pid is found */
152 if(kill(pid, signal) && errno == ESRCH)
155 fprintf(stderr, _("The tincd for net `%s' is no longer running. "), netname);
157 fprintf(stderr, _("The tincd is no longer running. "));
159 fprintf(stderr, _("Removing stale lock file.\n"));
160 remove_pid(pidfilename);
167 Detach from current terminal, write pidfile, kill parent
174 /* First check if we can open a fresh new pidfile */
179 /* If we succeeded in doing that, detach */
187 fprintf(stderr, _("Couldn't detach from terminal: %s"), strerror(errno));
191 /* Now UPDATE the pid in the pidfile, because we changed it... */
193 if(!write_pid(pidfilename))
197 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
199 if(debug_lvl > DEBUG_NOTHING)
200 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
201 VERSION, __DATE__, __TIME__, debug_lvl);
203 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
205 xalloc_fail_func = memory_full;
211 Execute the program name, with sane environment. All output will be
212 redirected to syslog.
214 void _execute_script(const char *scriptname, char **envp) __attribute__ ((noreturn));
215 void _execute_script(const char *scriptname, char **envp)
224 /* Close all file descriptors */
225 closelog(); /* <- this means we cannot use syslog() here anymore! */
228 execl(scriptname, NULL);
229 /* No return on success */
231 openlog("tinc", LOG_CONS | LOG_PID, LOG_DAEMON);
232 syslog(LOG_ERR, _("Could not execute `%s': %s"), scriptname, strerror(errno));
237 Fork and execute the program pointed to by name.
239 int execute_script(const char *name, char **envp)
246 asprintf(&scriptname, "%s/%s", confbase, name);
248 /* First check if there is a script */
250 if(stat(scriptname, &s))
257 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fork", strerror(errno));
263 if(debug_lvl >= DEBUG_STATUS)
264 syslog(LOG_INFO, _("Executing script %s"), name);
268 if(waitpid(pid, &status, 0) == pid)
270 if(WIFEXITED(status)) /* Child exited by itself */
272 if(WEXITSTATUS(status))
274 syslog(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status));
280 else if(WIFSIGNALED(status)) /* Child was killed by a signal */
282 syslog(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"),
283 pid, name, WTERMSIG(status), strsignal(WTERMSIG(status)));
286 else /* Something strange happened */
288 syslog(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, name);
294 syslog(LOG_ERR, _("System call `%s' failed: %s"), "waitpid", strerror(errno));
301 _execute_script(scriptname, envp);
310 sigterm_handler(int a)
312 if(debug_lvl > DEBUG_NOTHING)
313 syslog(LOG_NOTICE, _("Got TERM signal"));
319 sigquit_handler(int a)
321 if(debug_lvl > DEBUG_NOTHING)
322 syslog(LOG_NOTICE, _("Got QUIT signal"));
327 fatal_signal_square(int a)
329 syslog(LOG_ERR, _("Got another fatal signal %d (%s): not restarting."), a, strsignal(a));
335 fatal_signal_handler(int a)
337 struct sigaction act;
338 syslog(LOG_ERR, _("Got fatal signal %d (%s)"), a, strsignal(a));
343 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
345 act.sa_handler = fatal_signal_square;
346 act.sa_mask = emptysigset;
348 sigaction(SIGSEGV, &act, NULL);
350 close_network_connections();
352 remove_pid(pidfilename);
353 execvp(g_argv[0], g_argv);
357 syslog(LOG_NOTICE, _("Not restarting."));
363 sighup_handler(int a)
365 if(debug_lvl > DEBUG_NOTHING)
366 syslog(LOG_NOTICE, _("Got HUP signal"));
371 sigint_handler(int a)
375 syslog(LOG_NOTICE, _("Reverting to old debug level (%d)"),
377 debug_lvl = saved_debug_lvl;
382 syslog(LOG_NOTICE, _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
384 saved_debug_lvl = debug_lvl;
390 sigalrm_handler(int a)
392 if(debug_lvl > DEBUG_NOTHING)
393 syslog(LOG_NOTICE, _("Got ALRM signal"));
398 sigusr1_handler(int a)
404 sigusr2_handler(int a)
413 sigwinch_handler(int a)
420 unexpected_signal_handler(int a)
422 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
427 ignore_signal_handler(int a)
429 if(debug_lvl >= DEBUG_SCARY_THINGS)
431 syslog(LOG_DEBUG, _("Ignored signal %d (%s)"), a, strsignal(a));
438 void (*handler)(int);
440 { SIGHUP, sighup_handler },
441 { SIGTERM, sigterm_handler },
442 { SIGQUIT, sigquit_handler },
443 { SIGSEGV, fatal_signal_handler },
444 { SIGBUS, fatal_signal_handler },
445 { SIGILL, fatal_signal_handler },
446 { SIGPIPE, ignore_signal_handler },
447 { SIGINT, sigint_handler },
448 { SIGUSR1, sigusr1_handler },
449 { SIGUSR2, sigusr2_handler },
450 { SIGCHLD, ignore_signal_handler },
451 { SIGALRM, sigalrm_handler },
452 { SIGWINCH, sigwinch_handler },
460 struct sigaction act;
462 sigemptyset(&emptysigset);
463 act.sa_handler = NULL;
464 act.sa_mask = emptysigset;
467 /* Set a default signal handler for every signal, errors will be
469 for(i = 0; i < NSIG; i++)
472 act.sa_handler = SIG_DFL;
474 act.sa_handler = unexpected_signal_handler;
475 sigaction(i, &act, NULL);
478 /* If we didn't detach, allow coredumps */
480 sighandlers[3].handler = SIG_DFL;
482 /* Then, for each known signal that we want to catch, assign a
483 handler to the signal, with error checking this time. */
484 for(i = 0; sighandlers[i].signal; i++)
486 act.sa_handler = sighandlers[i].handler;
487 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
488 fprintf(stderr, _("Installing signal handler for signal %d (%s) failed: %s\n"),
489 sighandlers[i].signal, strsignal(sighandlers[i].signal), strerror(errno));