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.52 2003/07/06 23:16:28 guus Exp $
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
46 #include "connection.h"
52 /* If zero, don't detach from the terminal. */
55 extern char *identname;
56 extern char *pidfilename;
58 extern int use_logfile;
62 static int saved_debug_level = -1;
68 static void memory_full(int size)
70 logger(DEBUG_ALWAYS, LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size);
75 /* Some functions the less gifted operating systems might lack... */
77 #ifndef HAVE_FCLOSEALL
78 static int fcloseall(void)
91 Close network connections, and terminate neatly
93 void cleanup_and_exit(int c)
97 close_network_connections();
99 if(debug_level > DEBUG_NOTHING)
102 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Terminating"));
109 check for an existing tinc for this net, and write pid to pidfile
111 static int write_pidfile(void)
117 pid = check_pid(pidfilename);
121 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
124 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
128 /* if it's locked, write-protected, or whatever */
129 if(!write_pid(pidfilename))
136 kill older tincd for this net
138 int kill_other(int signal)
144 pid = read_pid(pidfilename);
148 fprintf(stderr, _("No other tincd is running for net `%s'.\n"),
151 fprintf(stderr, _("No other tincd is running.\n"));
155 errno = 0; /* No error, sometimes errno is only changed on error */
157 /* ESRCH is returned when no process with that pid is found */
158 if(kill(pid, signal) && errno == ESRCH) {
160 fprintf(stderr, _("The tincd for net `%s' is no longer running. "),
163 fprintf(stderr, _("The tincd is no longer running. "));
165 fprintf(stderr, _("Removing stale lock file.\n"));
166 remove_pid(pidfilename);
173 Detach from current terminal, write pidfile, kill parent
181 /* First check if we can open a fresh new pidfile */
186 /* If we succeeded in doing that, detach */
191 if(daemon(0, 0) < 0) {
192 fprintf(stderr, _("Couldn't detach from terminal: %s"),
197 /* Now UPDATE the pid in the pidfile, because we changed it... */
199 if(!write_pid(pidfilename))
203 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
205 logger(DEBUG_ALWAYS, LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
206 VERSION, __DATE__, __TIME__, debug_level);
208 xalloc_fail_func = memory_full;
214 Execute the program name, with sane environment.
216 static void _execute_script(const char *scriptname, char **envp)
217 __attribute__ ((noreturn));
218 static void _execute_script(const char *scriptname, char **envp)
229 /* Close all file descriptors */
232 execl(scriptname, NULL);
233 /* No return on success */
235 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
236 logger(DEBUG_ALWAYS, LOG_ERR, _("Could not execute `%s': %s"), scriptname,
242 Fork and execute the program pointed to by name.
244 int execute_script(const char *name, char **envp)
253 asprintf(&scriptname, "%s/%s", confbase, name);
255 /* First check if there is a script */
257 if(stat(scriptname, &s))
263 logger(DEBUG_ALWAYS, LOG_ERR, _("System call `%s' failed: %s"), "fork",
269 logger(DEBUG_STATUS, LOG_INFO, _("Executing script %s"), name);
273 if(waitpid(pid, &status, 0) == pid) {
274 if(WIFEXITED(status)) { /* Child exited by itself */
275 if(WEXITSTATUS(status)) {
276 logger(DEBUG_ALWAYS, LOG_ERR, _("Process %d (%s) exited with non-zero status %d"),
277 pid, name, WEXITSTATUS(status));
281 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
282 logger(DEBUG_ALWAYS, LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid,
283 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
285 } else { /* Something strange happened */
286 logger(DEBUG_ALWAYS, LOG_ERR, _("Process %d (%s) terminated abnormally"), pid,
291 logger(DEBUG_ALWAYS, LOG_ERR, _("System call `%s' failed: %s"), "waitpid",
299 _execute_script(scriptname, envp);
307 static RETSIGTYPE sigterm_handler(int a)
309 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Got TERM signal"));
314 static RETSIGTYPE sigquit_handler(int a)
316 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Got QUIT signal"));
320 static RETSIGTYPE fatal_signal_square(int a)
322 logger(DEBUG_ALWAYS, LOG_ERR, _("Got another fatal signal %d (%s): not restarting."), a,
328 static RETSIGTYPE fatal_signal_handler(int a)
330 struct sigaction act;
331 logger(DEBUG_ALWAYS, LOG_ERR, _("Got fatal signal %d (%s)"), a, strsignal(a));
335 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
337 act.sa_handler = fatal_signal_square;
338 act.sa_mask = emptysigset;
340 sigaction(SIGSEGV, &act, NULL);
342 close_network_connections();
344 remove_pid(pidfilename);
345 execvp(g_argv[0], g_argv);
347 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Not restarting."));
352 static RETSIGTYPE sighup_handler(int a)
354 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Got HUP signal"));
358 static RETSIGTYPE sigint_handler(int a)
360 if(saved_debug_level != -1) {
361 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Reverting to old debug level (%d)"),
363 debug_level = saved_debug_level;
364 saved_debug_level = -1;
366 logger(DEBUG_ALWAYS, LOG_NOTICE,
367 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
369 saved_debug_level = debug_level;
374 static RETSIGTYPE sigalrm_handler(int a)
376 logger(DEBUG_ALWAYS, LOG_NOTICE, _("Got ALRM signal"));
380 static RETSIGTYPE sigusr1_handler(int a)
385 static RETSIGTYPE sigusr2_handler(int a)
393 static RETSIGTYPE sigwinch_handler(int a)
399 static RETSIGTYPE unexpected_signal_handler(int a)
401 logger(DEBUG_ALWAYS, LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
405 static RETSIGTYPE ignore_signal_handler(int a)
407 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, _("Ignored signal %d (%s)"), a, strsignal(a));
412 void (*handler)(int);
415 SIGHUP, sighup_handler}, {
416 SIGTERM, sigterm_handler}, {
417 SIGQUIT, sigquit_handler}, {
418 SIGSEGV, fatal_signal_handler}, {
419 SIGBUS, fatal_signal_handler}, {
420 SIGILL, fatal_signal_handler}, {
421 SIGPIPE, ignore_signal_handler}, {
422 SIGINT, sigint_handler}, {
423 SIGUSR1, sigusr1_handler}, {
424 SIGUSR2, sigusr2_handler}, {
425 SIGCHLD, ignore_signal_handler}, {
426 SIGALRM, sigalrm_handler}, {
427 SIGWINCH, sigwinch_handler}, {
431 void setup_signals(void)
434 struct sigaction act;
436 sigemptyset(&emptysigset);
437 act.sa_handler = NULL;
438 act.sa_mask = emptysigset;
441 /* Set a default signal handler for every signal, errors will be
443 for(i = 0; i < NSIG; i++) {
445 act.sa_handler = SIG_DFL;
447 act.sa_handler = unexpected_signal_handler;
448 sigaction(i, &act, NULL);
451 /* If we didn't detach, allow coredumps */
453 sighandlers[3].handler = SIG_DFL;
455 /* Then, for each known signal that we want to catch, assign a
456 handler to the signal, with error checking this time. */
457 for(i = 0; sighandlers[i].signal; i++) {
458 act.sa_handler = sighandlers[i].handler;
459 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
460 fprintf(stderr, _("Installing signal handler for signal %d (%s) failed: %s\n"),
461 sighandlers[i].signal, strsignal(sighandlers[i].signal),