2 process.c -- process management functions
3 Copyright (C) 1999-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 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.57 2003/07/22 20:55:20 guus Exp $
26 #include "connection.h"
37 /* If zero, don't detach from the terminal. */
38 bool do_detach = true;
42 extern char *identname;
43 extern char *pidfilename;
45 extern bool use_logfile;
49 static int saved_debug_level = -1;
51 static void memory_full(int size)
53 logger(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size);
58 /* Some functions the less gifted operating systems might lack... */
60 #ifndef HAVE_FCLOSEALL
61 static int fcloseall(void)
74 Close network connections, and terminate neatly
76 void cleanup_and_exit(int c)
80 close_network_connections();
85 logger(LOG_NOTICE, _("Terminating"));
92 check for an existing tinc for this net, and write pid to pidfile
94 static bool write_pidfile(void)
100 pid = check_pid(pidfilename);
104 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
107 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
111 /* if it's locked, write-protected, or whatever */
112 if(!write_pid(pidfilename))
119 kill older tincd for this net
121 bool kill_other(int signal)
127 pid = read_pid(pidfilename);
131 fprintf(stderr, _("No other tincd is running for net `%s'.\n"),
134 fprintf(stderr, _("No other tincd is running.\n"));
138 errno = 0; /* No error, sometimes errno is only changed on error */
140 /* ESRCH is returned when no process with that pid is found */
141 if(kill(pid, signal) && errno == ESRCH) {
143 fprintf(stderr, _("The tincd for net `%s' is no longer running. "),
146 fprintf(stderr, _("The tincd is no longer running. "));
148 fprintf(stderr, _("Removing stale lock file.\n"));
149 remove_pid(pidfilename);
156 Detach from current terminal, write pidfile, kill parent
164 /* First check if we can open a fresh new pidfile */
169 /* If we succeeded in doing that, detach */
175 fprintf(stderr, _("Couldn't detach from terminal: %s"),
180 /* Now UPDATE the pid in the pidfile, because we changed it... */
182 if(!write_pid(pidfilename))
186 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
188 logger(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
189 VERSION, __DATE__, __TIME__, debug_level);
191 xalloc_fail_func = memory_full;
197 Execute the program name, with sane environment.
199 static void _execute_script(const char *scriptname, char **envp)
200 __attribute__ ((noreturn));
201 static void _execute_script(const char *scriptname, char **envp)
214 /* Close all file descriptors */
217 execl(scriptname, NULL);
218 /* No return on success */
222 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
223 logger(LOG_ERR, _("Could not execute `%s': %s"), scriptname,
224 strerror(save_errno));
229 Fork and execute the program pointed to by name.
231 bool execute_script(const char *name, char **envp)
240 asprintf(&scriptname, "%s/%s", confbase, name);
242 /* First check if there is a script */
244 if(stat(scriptname, &s))
250 logger(LOG_ERR, _("System call `%s' failed: %s"), "fork",
256 ifdebug(STATUS) logger(LOG_INFO, _("Executing script %s"), name);
260 if(waitpid(pid, &status, 0) == pid) {
261 if(WIFEXITED(status)) { /* Child exited by itself */
262 if(WEXITSTATUS(status)) {
263 logger(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"),
264 pid, name, WEXITSTATUS(status));
268 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
269 logger(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid,
270 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
272 } else { /* Something strange happened */
273 logger(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid,
277 } else if (errno != EINTR) {
278 logger(LOG_ERR, _("System call `%s' failed: %s"), "waitpid",
283 /* Why do we get EINTR? */
289 _execute_script(scriptname, envp);
297 static RETSIGTYPE sigterm_handler(int a)
299 logger(LOG_NOTICE, _("Got TERM signal"));
304 static RETSIGTYPE sigquit_handler(int a)
306 logger(LOG_NOTICE, _("Got QUIT signal"));
310 static RETSIGTYPE fatal_signal_square(int a)
312 logger(LOG_ERR, _("Got another fatal signal %d (%s): not restarting."), a,
318 static RETSIGTYPE fatal_signal_handler(int a)
320 struct sigaction act;
321 logger(LOG_ERR, _("Got fatal signal %d (%s)"), a, strsignal(a));
325 logger(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
327 act.sa_handler = fatal_signal_square;
328 act.sa_mask = emptysigset;
330 sigaction(SIGSEGV, &act, NULL);
332 close_network_connections();
334 remove_pid(pidfilename);
335 execvp(g_argv[0], g_argv);
337 logger(LOG_NOTICE, _("Not restarting."));
342 static RETSIGTYPE sighup_handler(int a)
344 logger(LOG_NOTICE, _("Got HUP signal"));
348 static RETSIGTYPE sigint_handler(int a)
350 if(saved_debug_level != -1) {
351 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
353 debug_level = saved_debug_level;
354 saved_debug_level = -1;
357 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
359 saved_debug_level = debug_level;
364 static RETSIGTYPE sigalrm_handler(int a)
366 logger(LOG_NOTICE, _("Got ALRM signal"));
370 static RETSIGTYPE sigusr1_handler(int a)
375 static RETSIGTYPE sigusr2_handler(int a)
383 static RETSIGTYPE sigwinch_handler(int a)
388 static RETSIGTYPE unexpected_signal_handler(int a)
390 logger(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
394 static RETSIGTYPE ignore_signal_handler(int a)
396 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Ignored signal %d (%s)"), a, strsignal(a));
401 void (*handler)(int);
403 {SIGHUP, sighup_handler},
404 {SIGTERM, sigterm_handler},
405 {SIGQUIT, sigquit_handler},
406 {SIGSEGV, fatal_signal_handler},
407 {SIGBUS, fatal_signal_handler},
408 {SIGILL, fatal_signal_handler},
409 {SIGPIPE, ignore_signal_handler},
410 {SIGINT, sigint_handler},
411 {SIGUSR1, sigusr1_handler},
412 {SIGUSR2, sigusr2_handler},
413 {SIGCHLD, ignore_signal_handler},
414 {SIGALRM, sigalrm_handler},
415 {SIGWINCH, sigwinch_handler},
419 void setup_signals(void)
422 struct sigaction act;
424 sigemptyset(&emptysigset);
425 act.sa_handler = NULL;
426 act.sa_mask = emptysigset;
429 /* Set a default signal handler for every signal, errors will be
431 for(i = 0; i < NSIG; i++) {
433 act.sa_handler = SIG_DFL;
435 act.sa_handler = unexpected_signal_handler;
436 sigaction(i, &act, NULL);
439 /* If we didn't detach, allow coredumps */
441 sighandlers[3].handler = SIG_DFL;
443 /* Then, for each known signal that we want to catch, assign a
444 handler to the signal, with error checking this time. */
445 for(i = 0; sighandlers[i].signal; i++) {
446 act.sa_handler = sighandlers[i].handler;
447 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
448 fprintf(stderr, _("Installing signal handler for signal %d (%s) failed: %s\n"),
449 sighandlers[i].signal, strsignal(sighandlers[i].signal),