2 process.c -- process management functions
3 Copyright (C) 1999-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 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.37 2002/03/19 00:07:09 guus Exp $
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
46 #include "connection.h"
51 /* If zero, don't detach from the terminal. */
54 extern char *identname;
55 extern char *pidfilename;
60 static int saved_debug_lvl = 0;
66 void memory_full(int size)
68 syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size);
73 /* Some functions the less gifted operating systems might lack... */
75 #ifndef HAVE_FCLOSEALL
89 Close network connections, and terminate neatly
91 void cleanup_and_exit(int c)
94 close_network_connections();
96 if(debug_lvl > DEBUG_NOTHING)
99 syslog(LOG_NOTICE, _("Terminating"));
106 check for an existing tinc for this net, and write pid to pidfile
108 int write_pidfile(void)
112 if((pid = check_pid(pidfilename)))
115 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
118 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
122 /* if it's locked, write-protected, or whatever */
123 if(!write_pid(pidfilename))
130 kill older tincd for this net
132 int kill_other(int signal)
136 if(!(pid = read_pid(pidfilename)))
139 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
141 fprintf(stderr, _("No other tincd is running.\n"));
145 errno = 0; /* No error, sometimes errno is only changed on error */
146 /* ESRCH is returned when no process with that pid is found */
147 if(kill(pid, signal) && errno == ESRCH)
150 fprintf(stderr, _("The tincd for net `%s' is no longer running. "), netname);
152 fprintf(stderr, _("The tincd is no longer running. "));
154 fprintf(stderr, _("Removing stale lock file.\n"));
155 remove_pid(pidfilename);
162 Detach from current terminal, write pidfile, kill parent
169 /* First check if we can open a fresh new pidfile */
174 /* If we succeeded in doing that, detach */
182 fprintf(stderr, _("Couldn't detach from terminal: %s"), strerror(errno));
186 /* Now UPDATE the pid in the pidfile, because we changed it... */
188 if(!write_pid(pidfilename))
192 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
194 if(debug_lvl > DEBUG_NOTHING)
195 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
196 VERSION, __DATE__, __TIME__, debug_lvl);
198 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
200 xalloc_fail_func = memory_full;
206 Execute the program name, with sane environment. All output will be
207 redirected to syslog.
209 void _execute_script(const char *name) __attribute__ ((noreturn));
210 void _execute_script(const char *name)
218 unsetenv("INTERFACE");
223 asprintf(&s, "NETNAME=%s", netname);
224 putenv(s); /* Don't free s! see man 3 putenv */
229 asprintf(&s, "DEVICE=%s", device);
230 putenv(s); /* Don't free s! see man 3 putenv */
235 asprintf(&s, "INTERFACE=%s", interface);
236 putenv(s); /* Don't free s! see man 3 putenv */
241 asprintf(&scriptname, "%s/%s", confbase, name);
243 /* Close all file descriptors */
244 closelog(); /* <- this means we cannot use syslog() here anymore! */
247 execl(scriptname, NULL);
248 /* No return on success */
250 if(errno != ENOENT) /* Ignore if the file does not exist */
251 exit(1); /* Some error while trying execl(). */
257 Fork and execute the program pointed to by name.
259 int execute_script(const char *name)
264 if((pid = fork()) < 0)
266 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fork", strerror(errno));
272 if(debug_lvl >= DEBUG_STATUS)
273 syslog(LOG_INFO, _("Executing script %s"), name);
275 if(waitpid(pid, &status, 0) == pid)
277 if(WIFEXITED(status)) /* Child exited by itself */
279 if(WEXITSTATUS(status))
281 syslog(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status));
287 else if(WIFSIGNALED(status)) /* Child was killed by a signal */
289 syslog(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"),
290 pid, name, WTERMSIG(status), strsignal(WTERMSIG(status)));
293 else /* Something strange happened */
295 syslog(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, name);
301 syslog(LOG_ERR, _("System call `%s' failed: %s"), "waitpid", strerror(errno));
308 _execute_script(name);
317 sigterm_handler(int a)
319 if(debug_lvl > DEBUG_NOTHING)
320 syslog(LOG_NOTICE, _("Got TERM signal"));
326 sigquit_handler(int a)
328 if(debug_lvl > DEBUG_NOTHING)
329 syslog(LOG_NOTICE, _("Got QUIT signal"));
334 fatal_signal_square(int a)
336 syslog(LOG_ERR, _("Got another fatal signal %d (%s): not restarting."), a, strsignal(a));
342 fatal_signal_handler(int a)
344 struct sigaction act;
345 syslog(LOG_ERR, _("Got fatal signal %d (%s)"), a, strsignal(a));
350 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
352 act.sa_handler = fatal_signal_square;
353 act.sa_mask = emptysigset;
355 sigaction(SIGSEGV, &act, NULL);
357 close_network_connections();
359 remove_pid(pidfilename);
360 execvp(g_argv[0], g_argv);
364 syslog(LOG_NOTICE, _("Not restarting."));
370 sighup_handler(int a)
372 if(debug_lvl > DEBUG_NOTHING)
373 syslog(LOG_NOTICE, _("Got HUP signal"));
378 sigint_handler(int a)
382 syslog(LOG_NOTICE, _("Reverting to old debug level (%d)"),
384 debug_lvl = saved_debug_lvl;
389 syslog(LOG_NOTICE, _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
391 saved_debug_lvl = debug_lvl;
397 sigalrm_handler(int a)
399 if(debug_lvl > DEBUG_NOTHING)
400 syslog(LOG_NOTICE, _("Got ALRM signal"));
405 sigusr1_handler(int a)
411 sigusr2_handler(int a)
420 sigwinch_handler(int a)
427 unexpected_signal_handler(int a)
429 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
434 ignore_signal_handler(int a)
436 if(debug_lvl >= DEBUG_SCARY_THINGS)
438 syslog(LOG_DEBUG, _("Ignored signal %d (%s)"), a, strsignal(a));
445 void (*handler)(int);
447 { SIGHUP, sighup_handler },
448 { SIGTERM, sigterm_handler },
449 { SIGQUIT, sigquit_handler },
450 { SIGSEGV, fatal_signal_handler },
451 { SIGBUS, fatal_signal_handler },
452 { SIGILL, fatal_signal_handler },
453 { SIGPIPE, ignore_signal_handler },
454 { SIGINT, sigint_handler },
455 { SIGUSR1, sigusr1_handler },
456 { SIGUSR2, sigusr2_handler },
457 { SIGCHLD, ignore_signal_handler },
458 { SIGALRM, sigalrm_handler },
459 { SIGWINCH, sigwinch_handler },
467 struct sigaction act;
469 sigemptyset(&emptysigset);
470 act.sa_handler = NULL;
471 act.sa_mask = emptysigset;
474 /* Set a default signal handler for every signal, errors will be
476 for(i = 0; i < NSIG; i++)
479 act.sa_handler = SIG_DFL;
481 act.sa_handler = unexpected_signal_handler;
482 sigaction(i, &act, NULL);
485 /* If we didn't detach, allow coredumps */
487 sighandlers[3].handler = SIG_DFL;
489 /* Then, for each known signal that we want to catch, assign a
490 handler to the signal, with error checking this time. */
491 for(i = 0; sighandlers[i].signal; i++)
493 act.sa_handler = sighandlers[i].handler;
494 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
495 fprintf(stderr, _("Installing signal handler for signal %d (%s) failed: %s\n"),
496 sighandlers[i].signal, strsignal(sighandlers[i].signal), strerror(errno));