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.1 2000/11/16 17:54:28 zarq Exp $
27 /* A list containing all our children */
30 /* If zero, don't detach from the terminal. */
34 Detach from current terminal, write pidfile, kill parent
45 if((pid = fork()) < 0)
50 if(pid) /* parent process */
52 signal(SIGTERM, parent_exit);
53 sleep(600); /* wait 10 minutes */
63 if((fd = open("/dev/tty", O_RDWR)) >= 0)
65 if(ioctl(fd, TIOCNOTTY, NULL))
79 chdir("/"); /* avoid keeping a mointpoint busy */
81 openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
83 if(debug_lvl > DEBUG_NOTHING)
84 syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
85 VERSION, __DATE__, __TIME__, debug_lvl);
87 syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
89 xalloc_fail_func = memory_full;
95 Execute the program name, with sane environment. All output will be
98 void _execute_script(const char *name)
107 asprintf(&s, "NETNAME=%s", netname);
108 putenv(s); /* Don't free s! see man 3 putenv */
117 if(chdir(confbase) < 0)
118 /* This cannot fail since we already read config files from this
120 /* Yes this can fail, somebody could have removed this directory
121 when we didn't pay attention. - Ivo */
124 /* Now if THIS fails, something wicked is going on. - Ivo */
125 syslog(LOG_ERR, _("Couldn't chdir to `/': %m"));
127 /* Continue anyway. */
130 asprintf(&scriptname, "%s/%s", confbase, name);
132 /* Close all file descriptors */
136 /* Open standard input */
137 if(open("/dev/null", O_RDONLY) < 0)
139 syslog(LOG_ERR, _("Opening `/dev/null' failed: %m"));
145 /* Standard output directly goes to syslog */
146 openlog(name, LOG_CONS | LOG_PID, LOG_DAEMON);
147 /* Standard error as well */
150 syslog(LOG_ERR, _("System call `%s' failed: %m"),
156 if(error && debug_lvl > 1)
157 syslog(LOG_INFO, _("This means that any output the script generates will not be shown in syslog."));
159 execl(scriptname, NULL);
160 /* No return on success */
162 if(errno != ENOENT) /* Ignore if the file does not exist */
163 syslog(LOG_WARNING, _("Error executing `%s': %m"), scriptname);
165 /* No need to free things */
170 Fork and execute the program pointed to by name.
172 int execute_script(const char *name)
176 if((pid = fork()) < 0)
178 syslog(LOG_ERR, _("System call `%s' failed: %m"),
185 list_append(child_pids, pid);
191 _execute_script(name);
195 Check a child (the pointer data is actually an integer, the PID of
196 that child. A non-zero return value means that the child has exited
197 and can be removed from our list.
199 int check_child(void *data)
205 pid = waitpid(pid, &status, WNOHANG);
206 if(WIFEXITED(status))
208 if(WIFSIGNALED(status)) /* Child was killed by a signal */
210 syslog(LOG_ERR, _("Child with PID %d was killed by signal %d (%s)"),
211 pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
214 if(WEXITSTATUS(status) != 0)
216 syslog(LOG_INFO, _("Child with PID %d exited with code %d"),
217 WEXITSTATUS(status));
222 /* Child is still running */
227 Check the status of all our children.
229 void check_children(void)
231 list_forall_nodes(child_pids, check_child);
241 sigterm_handler(int a)
243 if(debug_lvl > DEBUG_NOTHING)
244 syslog(LOG_NOTICE, _("Got TERM signal"));
250 sigquit_handler(int a)
252 if(debug_lvl > DEBUG_NOTHING)
253 syslog(LOG_NOTICE, _("Got QUIT signal"));
258 sigsegv_square(int a)
260 syslog(LOG_ERR, _("Got another SEGV signal: not restarting"));
265 sigsegv_handler(int a)
267 syslog(LOG_ERR, _("Got SEGV signal"));
272 syslog(LOG_NOTICE, _("Trying to re-execute in 5 seconds..."));
273 signal(SIGSEGV, sigsegv_square);
274 close_network_connections();
276 remove_pid(pidfilename);
277 execvp(g_argv[0], g_argv);
281 syslog(LOG_NOTICE, _("Not restarting."));
287 sighup_handler(int a)
289 if(debug_lvl > DEBUG_NOTHING)
290 syslog(LOG_NOTICE, _("Got HUP signal"));
295 sigint_handler(int a)
297 if(debug_lvl > DEBUG_NOTHING)
298 syslog(LOG_NOTICE, _("Got INT signal, exiting"));
303 sigusr1_handler(int a)
309 sigusr2_handler(int a)
317 syslog(LOG_WARNING, _("Got unexpected signal %d (%s)"), a, strsignal(a));
329 if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
330 signal(SIGTERM, sigterm_handler);
331 if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
332 signal(SIGQUIT, sigquit_handler);
333 if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
334 signal(SIGSEGV, sigsegv_handler);
335 if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
336 signal(SIGHUP, sighup_handler);
337 signal(SIGPIPE, SIG_IGN);
338 if(signal(SIGINT, SIG_IGN) != SIG_ERR)
339 signal(SIGINT, sigint_handler);
340 signal(SIGUSR1, sigusr1_handler);
341 signal(SIGUSR2, sigusr2_handler);
342 signal(SIGCHLD, SIG_IGN);
345 RETSIGTYPE parent_exit(int a)