2 process.c -- process management functions
3 Copyright (C) 1999-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000,2001 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.31 2001/10/31 20:37:54 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;
62 void memory_full(int size)
64 syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exiting."), size);
69 /* Some functions the less gifted operating systems might lack... */
71 #ifndef HAVE_FCLOSEALL
84 Close network connections, and terminate neatly
86 void cleanup_and_exit(int c)
89 close_network_connections();
91 if(debug_lvl > DEBUG_NOTHING)
94 syslog(LOG_NOTICE, _("Terminating"));
101 check for an existing tinc for this net, and write pid to pidfile
103 int write_pidfile(void)
107 if((pid = check_pid(pidfilename)))
110 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
113 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
117 /* if it's locked, write-protected, or whatever */
118 if(!write_pid(pidfilename))
125 kill older tincd for this net
127 int kill_other(int signal)
131 if(!(pid = read_pid(pidfilename)))
134 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
136 fprintf(stderr, _("No other tincd is running.\n"));
140 errno = 0; /* No error, sometimes errno is only changed on error */
141 /* ESRCH is returned when no process with that pid is found */
142 if(kill(pid, signal) && errno == ESRCH)
143 fprintf(stderr, _("Removing stale lock file.\n"));
144 remove_pid(pidfilename);
150 Detach from current terminal, write pidfile, kill parent
157 /* First check if we can open a fresh new pidfile */
162 /* If we succeeded in doing that, detach */
170 fprintf(stderr, _("Couldn't detach from terminal: %s"), strerror(errno));
174 /* Now UPDATE the pid in the pidfile, because we changed it... */
176 if(!write_pid(pidfilename))
180 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
182 if(debug_lvl > DEBUG_NOTHING)
183 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
184 VERSION, __DATE__, __TIME__, debug_lvl);
186 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
188 xalloc_fail_func = memory_full;
194 Execute the program name, with sane environment. All output will be
195 redirected to syslog.
197 void _execute_script(const char *name) __attribute__ ((noreturn));
198 void _execute_script(const char *name)
206 unsetenv("INTERFACE");
211 asprintf(&s, "NETNAME=%s", netname);
212 putenv(s); /* Don't free s! see man 3 putenv */
217 asprintf(&s, "DEVICE=%s", device);
218 putenv(s); /* Don't free s! see man 3 putenv */
223 asprintf(&s, "INTERFACE=%s", interface);
224 putenv(s); /* Don't free s! see man 3 putenv */
229 asprintf(&scriptname, "%s/%s", confbase, name);
231 /* Close all file descriptors */
232 closelog(); /* <- this means we cannot use syslog() here anymore! */
235 execl(scriptname, NULL);
236 /* No return on success */
238 if(errno != ENOENT) /* Ignore if the file does not exist */
239 exit(1); /* Some error while trying execl(). */
245 Fork and execute the program pointed to by name.
247 int execute_script(const char *name)
252 if((pid = fork()) < 0)
254 syslog(LOG_ERR, _("System call `%s' failed: %m"),
261 if(debug_lvl >= DEBUG_STATUS)
262 syslog(LOG_INFO, _("Executing script %s"), name);
264 if(waitpid(pid, &status, 0) == pid)
266 if(WIFEXITED(status)) /* Child exited by itself */
268 if(WEXITSTATUS(status))
270 syslog(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status));
276 else if(WIFSIGNALED(status)) /* Child was killed by a signal */
278 syslog(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"),
279 pid, name, WTERMSIG(status), strsignal(WTERMSIG(status)));
282 else /* Something strange happened */
284 syslog(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, name);
290 syslog(LOG_ERR, _("System call `%s' failed: %m"), "waitpid");
297 _execute_script(name);
306 sigterm_handler(int a, siginfo_t *info, void *b)
308 if(debug_lvl > DEBUG_NOTHING)
309 syslog(LOG_NOTICE, _("Got TERM signal"));
315 sigquit_handler(int a, siginfo_t *info, void *b)
317 if(debug_lvl > DEBUG_NOTHING)
318 syslog(LOG_NOTICE, _("Got QUIT signal"));
323 sigsegv_square(int a, siginfo_t *info, void *b)
325 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
331 sigsegv_handler(int a, siginfo_t *info, void *b)
333 struct sigaction act;
334 syslog(LOG_ERR, _("Got SEGV signal"));
339 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
341 act.sa_handler = NULL;
342 act.sa_mask = emptysigset;
343 act.sa_flags = SA_SIGINFO;
344 act.sa_sigaction = sigsegv_square;
346 close_network_connections();
348 remove_pid(pidfilename);
349 execvp(g_argv[0], g_argv);
353 syslog(LOG_NOTICE, _("Not restarting."));
359 sighup_handler(int a, siginfo_t *info, void *b)
361 if(debug_lvl > DEBUG_NOTHING)
362 syslog(LOG_NOTICE, _("Got HUP signal"));
367 sigint_handler(int a, siginfo_t *info, void *b)
371 syslog(LOG_NOTICE, _("Reverting to old debug level (%d)"),
373 debug_lvl = saved_debug_lvl;
378 syslog(LOG_NOTICE, _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
380 saved_debug_lvl = debug_lvl;
386 sigusr1_handler(int a, siginfo_t *info, void *b)
392 sigusr2_handler(int a, siginfo_t *info, void *b)
401 unexpected_signal_handler(int a, siginfo_t *info, void *b)
403 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
408 ignore_signal_handler(int a, siginfo_t *info, void *b)
410 if(debug_lvl >= DEBUG_SCARY_THINGS)
412 syslog(LOG_DEBUG, _("Ignored signal %d (%s)"), a, strsignal(a));
419 void (*handler)(int, siginfo_t *, void *);
421 { SIGHUP, sighup_handler },
422 { SIGTERM, sigterm_handler },
423 { SIGQUIT, sigquit_handler },
424 { SIGSEGV, sigsegv_handler },
425 { SIGPIPE, ignore_signal_handler },
426 { SIGINT, sigint_handler },
427 { SIGUSR1, sigusr1_handler },
428 { SIGUSR2, sigusr2_handler },
429 { SIGCHLD, ignore_signal_handler },
430 { SIGALRM, ignore_signal_handler },
438 struct sigaction act;
440 sigemptyset(&emptysigset);
441 act.sa_handler = NULL;
442 act.sa_mask = emptysigset;
443 act.sa_flags = SA_SIGINFO;
445 /* Set a default signal handler for every signal, errors will be
447 for(i = 0; i < NSIG; i++)
449 act.sa_sigaction = unexpected_signal_handler;
450 sigaction(i, &act, NULL);
453 /* If we didn't detach, allow coredumps */
455 sighandlers[3].handler = SIG_DFL;
457 /* Then, for each known signal that we want to catch, assign a
458 handler to the signal, with error checking this time. */
459 for(i = 0; sighandlers[i].signal; i++)
461 act.sa_sigaction = sighandlers[i].handler;
462 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
463 fprintf(stderr, _("Installing signal handler for signal %d (%s) failed: %s\n"),
464 sighandlers[i].signal, strsignal(sighandlers[i].signal), strerror(errno));