2 process.c -- process management functions
3 Copyright (C) 1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000 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.17 2000/11/28 08:59:27 zarq Exp $
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
46 #include "connection.h"
50 /* If zero, don't detach from the terminal. */
53 extern char *identname;
54 extern char *pidfilename;
59 void memory_full(int size)
61 syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exiting."), size);
66 /* Some functions the less gifted operating systems might lack... */
68 #ifndef HAVE_FCLOSEALL
81 Close network connections, and terminate neatly
83 void cleanup_and_exit(int c)
86 close_network_connections();
88 if(debug_lvl > DEBUG_NOTHING)
89 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
90 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
92 syslog(LOG_NOTICE, _("Terminating"));
99 check for an existing tinc for this net, and write pid to pidfile
101 int write_pidfile(void)
105 if((pid = check_pid(pidfilename)))
108 fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
111 fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid);
115 /* if it's locked, write-protected, or whatever */
116 if(!write_pid(pidfilename))
123 kill older tincd for this net
129 if(!(pid = read_pid(pidfilename)))
132 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
134 fprintf(stderr, _("No other tincd is running.\n"));
138 errno = 0; /* No error, sometimes errno is only changed on error */
139 /* ESRCH is returned when no process with that pid is found */
140 if(kill(pid, SIGTERM) && errno == ESRCH)
141 fprintf(stderr, _("Removing stale lock file.\n"));
142 remove_pid(pidfilename);
148 Detach from current terminal, write pidfile, kill parent
155 /* First check if we can open a fresh new pidfile */
160 /* If we succeeded in doing that, detach */
166 fprintf(stderr, _("Couldn't detach from terminal: %m"));
170 /* Now UPDATE the pid in the pidfile, because we changed it... */
172 if(!write_pid(pidfilename))
176 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
178 if(debug_lvl > DEBUG_NOTHING)
179 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
180 VERSION, __DATE__, __TIME__, debug_lvl);
182 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
184 xalloc_fail_func = memory_full;
190 Execute the program name, with sane environment. All output will be
191 redirected to syslog.
193 void _execute_script(const char *name) __attribute__ ((noreturn));
194 void _execute_script(const char *name)
201 asprintf(&s, "NETNAME=%s", netname);
202 putenv(s); /* Don't free s! see man 3 putenv */
213 asprintf(&scriptname, "%s/%s", confbase, name);
215 /* Close all file descriptors */
216 closelog(); /* <- this means we cannot use syslog() here anymore! */
219 execl(scriptname, NULL);
220 /* No return on success */
222 if(errno != ENOENT) /* Ignore if the file does not exist */
223 exit(-1); /* Some error while trying execl(). */
229 Fork and execute the program pointed to by name.
231 int execute_script(const char *name)
236 if((pid = fork()) < 0)
238 syslog(LOG_ERR, _("System call `%s' failed: %m"),
245 if(debug_lvl >= DEBUG_STATUS)
246 syslog(LOG_INFO, _("Executing script %s"), name);
248 if(waitpid(pid, &status, 0) == pid)
250 if(WIFEXITED(status)) /* Child exited by itself */
252 if(WEXITSTATUS(status))
254 syslog(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status));
260 else if(WIFSIGNALED(status)) /* Child was killed by a signal */
262 syslog(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"),
263 pid, name, WTERMSIG(status), strsignal(WTERMSIG(status)));
266 else /* Something strange happened */
268 syslog(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, name);
274 syslog(LOG_ERR, _("System call `%s' failed: %m"), "waitpid");
281 _execute_script(name);
290 sigterm_handler(int a, siginfo_t *info, void *b)
292 if(debug_lvl > DEBUG_NOTHING)
293 syslog(LOG_NOTICE, _("Got TERM signal"));
299 sigquit_handler(int a, siginfo_t *info, void *b)
301 if(debug_lvl > DEBUG_NOTHING)
302 syslog(LOG_NOTICE, _("Got QUIT signal"));
307 sigsegv_square(int a, siginfo_t *info, void *b)
309 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
315 sigsegv_handler(int a, siginfo_t *info, void *b)
317 struct sigaction act;
318 syslog(LOG_ERR, _("Got SEGV signal"));
323 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
325 act.sa_handler = NULL;
326 act.sa_mask = emptysigset;
327 act.sa_flags = SA_SIGINFO;
328 act.sa_sigaction = sigsegv_square;
330 close_network_connections();
332 remove_pid(pidfilename);
333 execvp(g_argv[0], g_argv);
337 syslog(LOG_NOTICE, _("Not restarting."));
343 sighup_handler(int a, siginfo_t *info, void *b)
345 if(debug_lvl > DEBUG_NOTHING)
346 syslog(LOG_NOTICE, _("Got HUP signal"));
351 sigint_handler(int a, siginfo_t *info, void *b)
353 if(debug_lvl > DEBUG_NOTHING)
354 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
359 sigusr1_handler(int a, siginfo_t *info, void *b)
361 dump_connection_list();
365 sigusr2_handler(int a, siginfo_t *info, void *b)
371 unexpected_signal_handler(int a, siginfo_t *info, void *b)
373 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
379 void (*handler)(int, siginfo_t *, void *);
381 { SIGHUP, sighup_handler },
382 { SIGTERM, sigterm_handler },
383 { SIGQUIT, sigquit_handler },
384 { SIGSEGV, sigsegv_handler },
386 { SIGINT, sigint_handler },
387 { SIGUSR1, sigusr1_handler },
388 { SIGUSR2, sigusr2_handler },
397 struct sigaction act;
399 sigemptyset(&emptysigset);
400 act.sa_handler = NULL;
401 act.sa_mask = emptysigset;
402 act.sa_flags = SA_SIGINFO;
404 /* Set a default signal handler for every signal, errors will be
406 for(i = 0; i < NSIG; i++)
408 act.sa_sigaction = unexpected_signal_handler;
409 sigaction(sighandlers[i].signal, &act, NULL);
412 /* Then, for each known signal that we want to catch, assign a
413 handler to the signal, with error checking this time. */
414 for(i = 0; sighandlers[i].signal; i++)
416 act.sa_sigaction = sighandlers[i].handler;
417 if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
418 fprintf(stderr, _("Installing signal handler for signal %d (%s) failed: %m\n"),
419 sighandlers[i].signal, strsignal(sighandlers[i].signal));