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.15 2000/11/25 13:33:33 guus 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;
57 void memory_full(int size)
59 syslog(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exiting."), size);
64 /* Some functions the less gifted operating systems might lack... */
66 #ifndef HAVE_FCLOSEALL
79 Close network connections, and terminate neatly
81 void cleanup_and_exit(int c)
84 close_network_connections();
86 if(debug_lvl > DEBUG_NOTHING)
87 syslog(LOG_INFO, _("Total bytes written: tap %d, socket %d; bytes read: tap %d, socket %d"),
88 total_tap_out, total_socket_out, total_tap_in, total_socket_in);
90 syslog(LOG_NOTICE, _("Terminating"));
97 check for an existing tinc for this net, and write pid to pidfile
99 int write_pidfile(void)
103 if((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
127 if(!(pid = read_pid(pidfilename)))
130 fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
132 fprintf(stderr, _("No other tincd is running.\n"));
136 errno = 0; /* No error, sometimes errno is only changed on error */
137 /* ESRCH is returned when no process with that pid is found */
138 if(kill(pid, SIGTERM) && errno == ESRCH)
139 fprintf(stderr, _("Removing stale lock file.\n"));
140 remove_pid(pidfilename);
146 Detach from current terminal, write pidfile, kill parent
153 /* First check if we can open a fresh new pidfile */
158 /* If we succeeded in doing that, detach */
165 /* Now UPDATE the pid in the pidfile, because we changed it... */
167 if(!write_pid(pidfilename))
171 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
173 if(debug_lvl > DEBUG_NOTHING)
174 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
175 VERSION, __DATE__, __TIME__, debug_lvl);
177 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
179 xalloc_fail_func = memory_full;
185 Execute the program name, with sane environment. All output will be
186 redirected to syslog.
188 void _execute_script(const char *name) __attribute__ ((noreturn));
189 void _execute_script(const char *name)
196 asprintf(&s, "NETNAME=%s", netname);
197 putenv(s); /* Don't free s! see man 3 putenv */
208 asprintf(&scriptname, "%s/%s", confbase, name);
210 /* Close all file descriptors */
211 closelog(); /* <- this means we cannot use syslog() here anymore! */
214 execl(scriptname, NULL);
215 /* No return on success */
217 if(errno != ENOENT) /* Ignore if the file does not exist */
218 exit(-1); /* Some error while trying execl(). */
224 Fork and execute the program pointed to by name.
226 int execute_script(const char *name)
231 if((pid = fork()) < 0)
233 syslog(LOG_ERR, _("System call `%s' failed: %m"),
240 if(debug_lvl >= DEBUG_STATUS)
241 syslog(LOG_INFO, _("Executing script %s"), name);
243 if(waitpid(pid, &status, 0) == pid)
245 if(WIFEXITED(status)) /* Child exited by itself */
247 if(WEXITSTATUS(status))
249 syslog(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status));
255 else if(WIFSIGNALED(status)) /* Child was killed by a signal */
257 syslog(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"),
258 pid, name, WTERMSIG(status), strsignal(WTERMSIG(status)));
261 else /* Something strange happened */
263 syslog(LOG_ERR, _("Process %d (%s) terminated abnormaly"), pid, name);
269 syslog(LOG_ERR, _("System call `%s' failed: %m"), "waitpid");
276 _execute_script(name);
284 sigterm_handler(int a)
286 if(debug_lvl > DEBUG_NOTHING)
287 syslog(LOG_NOTICE, _("Got TERM signal"));
293 sigquit_handler(int a)
295 if(debug_lvl > DEBUG_NOTHING)
296 syslog(LOG_NOTICE, _("Got QUIT signal"));
301 sigsegv_square(int a)
303 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
309 sigsegv_handler(int a)
311 syslog(LOG_ERR, _("Got SEGV signal"));
316 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
317 signal(SIGSEGV, sigsegv_square);
318 close_network_connections();
320 remove_pid(pidfilename);
321 execvp(g_argv[0], g_argv);
325 syslog(LOG_NOTICE, _("Not restarting."));
331 sighup_handler(int a)
333 if(debug_lvl > DEBUG_NOTHING)
334 syslog(LOG_NOTICE, _("Got HUP signal"));
339 sigint_handler(int a)
341 if(debug_lvl > DEBUG_NOTHING)
342 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
347 sigusr1_handler(int a)
349 dump_connection_list();
353 sigusr2_handler(int a)
361 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
373 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
374 signal(SIGTERM, sigterm_handler);
375 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
376 signal(SIGQUIT, sigquit_handler);
377 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
378 signal(SIGSEGV, sigsegv_handler);
379 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
380 signal(SIGHUP, sighup_handler);
381 signal(SIGPIPE, SIG_IGN);
382 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
383 signal(SIGINT, sigint_handler);
384 signal(SIGUSR1, sigusr1_handler);
385 signal(SIGUSR2, sigusr2_handler);
386 signal(SIGCHLD, SIG_IGN);