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.55 2003/07/17 15:06:26 guus Exp $
26 #include "connection.h"
37 /* If zero, don't detach from the terminal. */
40 extern char *identname;
41 extern char *pidfilename;
43 extern int use_logfile;
47 static int saved_debug_level = -1;
53 static void memory_full(int size)
55 logger(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size);
60 /* Some functions the less gifted operating systems might lack... */
62 #ifndef HAVE_FCLOSEALL
63 static int fcloseall(void)
76 Close network connections, and terminate neatly
78 void cleanup_and_exit(int c)
82 close_network_connections();
87 logger(LOG_NOTICE, _("Terminating"));
94 check for an existing tinc for this net, and write pid to pidfile
96 static int write_pidfile(void)
102 pid = check_pid(pidfilename);
106 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
109 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
113 /* if it's locked, write-protected, or whatever */
114 if(!write_pid(pidfilename))
121 kill older tincd for this net
123 int kill_other(int signal)
129 pid = read_pid(pidfilename);
133 fprintf(stderr, _("No other tincd is running for net `%s'.\n"),
136 fprintf(stderr, _("No other tincd is running.\n"));
140 errno = 0; /* No error, sometimes errno is only changed on error */
142 /* ESRCH is returned when no process with that pid is found */
143 if(kill(pid, signal) && errno == ESRCH) {
145 fprintf(stderr, _("The tincd for net `%s' is no longer running. "),
148 fprintf(stderr, _("The tincd is no longer running. "));
150 fprintf(stderr, _("Removing stale lock file.\n"));
151 remove_pid(pidfilename);
158 Detach from current terminal, write pidfile, kill parent
166 /* First check if we can open a fresh new pidfile */
171 /* If we succeeded in doing that, detach */
176 if(daemon(0, 0) < 0) {
177 fprintf(stderr, _("Couldn't detach from terminal: %s"),
182 /* Now UPDATE the pid in the pidfile, because we changed it... */
184 if(!write_pid(pidfilename))
188 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
190 logger(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
191 VERSION, __DATE__, __TIME__, debug_level);
193 xalloc_fail_func = memory_full;
199 Execute the program name, with sane environment.
201 static void _execute_script(const char *scriptname, char **envp)
202 __attribute__ ((noreturn));
203 static void _execute_script(const char *scriptname, char **envp)
216 /* Close all file descriptors */
219 execl(scriptname, NULL);
220 /* No return on success */
224 openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
225 logger(LOG_ERR, _("Could not execute `%s': %s"), scriptname,
226 strerror(save_errno));
231 Fork and execute the program pointed to by name.
233 int execute_script(const char *name, char **envp)
242 asprintf(&scriptname, "%s/%s", confbase, name);
244 /* First check if there is a script */
246 if(stat(scriptname, &s))
252 logger(LOG_ERR, _("System call `%s' failed: %s"), "fork",
258 ifdebug(STATUS) logger(LOG_INFO, _("Executing script %s"), name);
262 if(waitpid(pid, &status, 0) == pid) {
263 if(WIFEXITED(status)) { /* Child exited by itself */
264 if(WEXITSTATUS(status)) {
265 logger(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"),
266 pid, name, WEXITSTATUS(status));
270 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
271 logger(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid,
272 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
274 } else { /* Something strange happened */
275 logger(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid,
279 } else if (errno != EINTR) {
280 logger(LOG_ERR, _("System call `%s' failed: %s"), "waitpid",
285 /* Why do we get EINTR? */
291 _execute_script(scriptname, envp);
299 static RETSIGTYPE sigterm_handler(int a)
301 logger(LOG_NOTICE, _("Got TERM signal"));
306 static RETSIGTYPE sigquit_handler(int a)
308 logger(LOG_NOTICE, _("Got QUIT signal"));
312 static RETSIGTYPE fatal_signal_square(int a)
314 logger(LOG_ERR, _("Got another fatal signal %d (%s): not restarting."), a,
320 static RETSIGTYPE fatal_signal_handler(int a)
322 struct sigaction act;
323 logger(LOG_ERR, _("Got fatal signal %d (%s)"), a, strsignal(a));
327 logger(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
329 act.sa_handler = fatal_signal_square;
330 act.sa_mask = emptysigset;
332 sigaction(SIGSEGV, &act, NULL);
334 close_network_connections();
336 remove_pid(pidfilename);
337 execvp(g_argv[0], g_argv);
339 logger(LOG_NOTICE, _("Not restarting."));
344 static RETSIGTYPE sighup_handler(int a)
346 logger(LOG_NOTICE, _("Got HUP signal"));
350 static RETSIGTYPE sigint_handler(int a)
352 if(saved_debug_level != -1) {
353 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
355 debug_level = saved_debug_level;
356 saved_debug_level = -1;
359 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
361 saved_debug_level = debug_level;
366 static RETSIGTYPE sigalrm_handler(int a)
368 logger(LOG_NOTICE, _("Got ALRM signal"));
372 static RETSIGTYPE sigusr1_handler(int a)
377 static RETSIGTYPE sigusr2_handler(int a)
385 static RETSIGTYPE sigwinch_handler(int a)
391 static RETSIGTYPE unexpected_signal_handler(int a)
393 logger(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
397 static RETSIGTYPE ignore_signal_handler(int a)
399 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Ignored signal %d (%s)"), a, strsignal(a));
404 void (*handler)(int);
407 SIGHUP, sighup_handler}, {
408 SIGTERM, sigterm_handler}, {
409 SIGQUIT, sigquit_handler}, {
410 SIGSEGV, fatal_signal_handler}, {
411 SIGBUS, fatal_signal_handler}, {
412 SIGILL, fatal_signal_handler}, {
413 SIGPIPE, ignore_signal_handler}, {
414 SIGINT, sigint_handler}, {
415 SIGUSR1, sigusr1_handler}, {
416 SIGUSR2, sigusr2_handler}, {
417 SIGCHLD, ignore_signal_handler}, {
418 SIGALRM, sigalrm_handler}, {
419 SIGWINCH, sigwinch_handler}, {
423 void setup_signals(void)
426 struct sigaction act;
428 sigemptyset(&emptysigset);
429 act.sa_handler = NULL;
430 act.sa_mask = emptysigset;
433 /* Set a default signal handler for every signal, errors will be
435 for(i = 0; i < NSIG; i++) {
437 act.sa_handler = SIG_DFL;
439 act.sa_handler = unexpected_signal_handler;
440 sigaction(i, &act, NULL);
443 /* If we didn't detach, allow coredumps */
445 sighandlers[3].handler = SIG_DFL;
447 /* Then, for each known signal that we want to catch, assign a
448 handler to the signal, with error checking this time. */
449 for(i = 0; sighandlers[i].signal; i++) {
450 act.sa_handler = sighandlers[i].handler;
451 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
452 fprintf(stderr, _("Installing signal handler for signal %d (%s) failed: %s\n"),
453 sighandlers[i].signal, strsignal(sighandlers[i].signal),