blob: 743481efca5a1489aeb296f18657d6cddf485371 [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnant3d6bb792007-01-05 22:33:43 +00003 * Copyright © 2007 Canonical Ltd.
Scott James Remnantb6270dd2006-07-13 02:16:38 +01004 * Author: Scott James Remnant <scott@ubuntu.com>.
Scott James Remnant50748842006-05-16 21:02:31 +01005 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif /* HAVE_CONFIG_H */
24
25
Scott James Remnantf43bdf32006-08-27 18:20:29 +010026#include <sys/types.h>
Scott James Remnantea806b72006-10-18 15:01:00 +010027#include <sys/time.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010028#include <sys/wait.h>
29#include <sys/ioctl.h>
30#include <sys/reboot.h>
31#include <sys/resource.h>
32
Scott James Remnant3401ab72006-09-01 02:14:47 +010033#include <errno.h>
34#include <stdio.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010035#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020036#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010037#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020038#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020039#include <unistd.h>
Scott James Remnantff0d26a2006-08-31 20:49:43 +010040#include <termios.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020041
Scott James Remnant097b2a92006-09-01 19:30:37 +010042#include <linux/kd.h>
43
Scott James Remnant77e8db32006-08-21 08:47:50 +020044#include <nih/macros.h>
45#include <nih/alloc.h>
46#include <nih/list.h>
47#include <nih/timer.h>
48#include <nih/signal.h>
49#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010050#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020051#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010052#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020053#include <nih/logging.h>
54
55#include "process.h"
56#include "job.h"
57#include "event.h"
58#include "control.h"
59#include "cfgfile.h"
Scott James Remnante0d0dd12006-09-14 10:51:05 +010060#include "paths.h"
Scott James Remnant50748842006-05-16 21:02:31 +010061
62
Scott James Remnant1db88042006-09-01 03:14:19 +010063/**
64 * STATE_FD:
65 *
66 * File descriptor we read our state from.
67 **/
68#define STATE_FD 101
69
70
Scott James Remnantf43bdf32006-08-27 18:20:29 +010071/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010072static void reset_console (void);
Scott James Remnant16a286f2007-01-10 15:38:33 +000073static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010074static void cad_handler (void *data, NihSignal *signal);
75static void kbd_handler (void *data, NihSignal *signal);
76static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant1db88042006-09-01 03:14:19 +010077static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010078
Scott James Remnant1db88042006-09-01 03:14:19 +010079
Scott James Remnant3401ab72006-09-01 02:14:47 +010080/**
Scott James Remnant1db88042006-09-01 03:14:19 +010081 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010082 *
Scott James Remnant8b227402006-10-11 17:55:27 +010083 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010084 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010085 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010086static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010087
88
89/**
90 * options:
91 *
92 * Command-line options we accept.
93 **/
94static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010095 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010096
97 /* Ignore invalid options */
98 { '-', "--", NULL, NULL, NULL, NULL, NULL },
99
100 NIH_OPTION_LAST
101};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100102
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100103
Scott James Remnant50748842006-05-16 21:02:31 +0100104int
105main (int argc,
106 char *argv[])
107{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100108 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000109 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100110
Scott James Remnant77e8db32006-08-21 08:47:50 +0200111 nih_main_init (argv[0]);
112
Scott James Remnant930e25a2006-10-13 12:28:05 +0100113 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100114 nih_option_set_help (
115 _("This daemon is normally executed by the kernel and given "
116 "process id 1 to denote its special status. When executed "
117 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100118
Scott James Remnant3401ab72006-09-01 02:14:47 +0100119 args = nih_option_parser (NULL, argc, argv, options, FALSE);
120 if (! args)
121 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200122
Scott James Remnanta17917d2006-09-07 00:18:28 +0100123 /* Check we're root */
124 if (getuid ()) {
125 nih_error (_("Need to be root"));
126 exit (1);
127 }
128
129 /* Check we're process #1 */
130 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100131 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100132 /* Ignore failure, probably just that telinit doesn't exist */
133
134 nih_error (_("Not being executed as init"));
135 exit (1);
136 }
137
Scott James Remnant2e204b72007-02-03 23:15:28 +0000138 /* Open control socket */
139 if (! control_open ()) {
140 NihError *err;
141
142 err = nih_error_get ();
143 nih_error ("%s: %s", _("Unable to open control socket"),
144 err->message);
145 nih_free (err);
146
147 exit (1);
148 }
149
150 /* Read configuration */
151 if (cfg_watch_dir (CFG_DIR) < 0) {
152 NihError *err;
153
154 err = nih_error_get ();
155 nih_error ("%s: %s", _("Error parsing configuration"),
156 err->message);
157 nih_free (err);
158
159 exit (1);
160 }
161
Scott James Remnant78626fb2007-01-10 16:48:10 +0000162
163 /* Clear our arguments from the command-line, so that we show up in
164 * ps or top output as /sbin/init, with no extra flags.
165 *
166 * This is a very Linux-specific trick; by deleting the NULL
167 * terminator at the end of the last argument, we fool the kernel
168 * into believing we used a setproctitle()-a-like to extend the
169 * argument space into the environment space, and thus make it use
170 * strlen() instead of its own assumed length. In fact, we've done
171 * the exact opposite, and shrunk the command line length to just that
172 * of whatever is in argv[0].
173 *
174 * If we don't do this, and just write \0 over the rest of argv, for
175 * example; the command-line length still includes those \0s, and ps
176 * will show whitespace in their place.
177 */
178 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000179 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000180
Scott James Remnant505f9282007-01-10 18:44:38 +0000181 arg_end = argv[argc-1] + strlen (argv[argc-1]);
182 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000183 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000184
Scott James Remnant2e204b72007-02-03 23:15:28 +0000185 /* Become session and process group leader (should be already,
186 * but you never know what initramfs did
187 */
188 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100189
Scott James Remnant3401ab72006-09-01 02:14:47 +0100190 /* Send all logging output to syslog */
191 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200192 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200193
Scott James Remnant77e8db32006-08-21 08:47:50 +0200194 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100195 * device instead. Normally we reset the console, unless we're
196 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200197 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000198 for (int i = 0; i < 3; i++)
Scott James Remnant77e8db32006-08-21 08:47:50 +0200199 close (i);
200
Scott James Remnant5d702952007-01-05 17:21:34 +0000201 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
202 nih_free (nih_error_get ());
Scott James Remnant1db88042006-09-01 03:14:19 +0100203 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100204 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200205
Scott James Remnant2e204b72007-02-03 23:15:28 +0000206 /* Set the PATH environment variable */
207 setenv ("PATH", PATH, TRUE);
208
209
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100210 /* Reset the signal state and install the signal handler for those
211 * signals we actually want to catch; this also sets those that
212 * can be sent to us, because we're special
213 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000214 if (! restart)
215 nih_signal_reset ();
216
217 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100218 nih_signal_set_handler (SIGALRM, nih_signal_handler);
219 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100220 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
221 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100222 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100223 nih_signal_set_handler (SIGINT, nih_signal_handler);
224 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000225 nih_signal_set_handler (SIGSEGV, crash_handler);
226 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100227
Scott James Remnanteabb7802006-08-31 15:39:04 +0100228 /* Ensure that we don't process events while paused */
Scott James Remnant5d702952007-01-05 17:21:34 +0000229 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
230 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100231
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100232 /* Ask the kernel to send us SIGINT when control-alt-delete is
233 * pressed; generate an event with the same name.
234 */
235 reboot (RB_DISABLE_CAD);
Scott James Remnant5d702952007-01-05 17:21:34 +0000236 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100237
238 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
239 * generate a kbdrequest event.
240 */
Scott James Remnant5d702952007-01-05 17:21:34 +0000241 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0)
242 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
243 kbd_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100244
Scott James Remnant1db88042006-09-01 03:14:19 +0100245 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5d702952007-01-05 17:21:34 +0000246 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM,
247 (NihSignalHandler)term_handler,
248 argv[0]));
Scott James Remnant1db88042006-09-01 03:14:19 +0100249
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100250 /* Reap all children that die */
Scott James Remnant5d702952007-01-05 17:21:34 +0000251 NIH_MUST (nih_child_add_watch (NULL, -1, job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100252
Scott James Remnant65906602007-02-08 02:51:39 +0000253 /* Process the event queue and check the jobs to make sure we
254 * haven't stalled, every time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000255 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000256 NULL));
Scott James Remnant65906602007-02-08 02:51:39 +0000257 NIH_MUST (nih_main_loop_add_func (NULL,
258 (NihMainLoopCb)job_detect_stalled,
Scott James Remnant5d702952007-01-05 17:21:34 +0000259 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100260
Scott James Remnant94d00982006-08-25 15:38:22 +0200261
Scott James Remnant3401ab72006-09-01 02:14:47 +0100262 /* Generate and run the startup event or read the state from the
263 * init daemon that exec'd us
264 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100265 if (! restart) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100266 Job *logd;
267
268 /* FIXME this is a bit of a hack, should have a list of
269 * essential services or something
270 */
271 logd = job_find_by_name ("logd");
Scott James Remnant515b2b92006-09-09 03:41:27 +0100272 if (logd) {
Scott James Remnant80eabbc2007-02-07 19:54:27 +0000273 job_change_goal (logd, JOB_START, NULL);
Scott James Remnant515b2b92006-09-09 03:41:27 +0100274 if (logd->state == JOB_RUNNING) {
275 /* Hang around until logd signals that it's
276 * listening ... but not too long
277 */
278 alarm (5);
279 waitpid (logd->pid, NULL, WUNTRACED);
280 kill (logd->pid, SIGCONT);
281 alarm (0);
282 }
283 }
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100284
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000285 event_emit (STARTUP_EVENT, NULL, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100286 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100287 sigset_t mask;
288
Scott James Remnant1db88042006-09-01 03:14:19 +0100289 /* We're ok to receive signals again */
290 sigemptyset (&mask);
291 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100292 }
293
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000294 /* Run through the loop at least once */
295 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200296 ret = nih_main_loop ();
297
298 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100299}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100300
301
302/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100303 * reset_console:
304 *
305 * Set up the console flags to something sensible. Cribbed from sysvinit,
306 * initng, etc.
307 **/
308static void
309reset_console (void)
310{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100311 struct termios tty;
312
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100313 tcgetattr (0, &tty);
314
315 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
316 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
317
318 /* Set up usual keys */
319 tty.c_cc[VINTR] = 3; /* ^C */
320 tty.c_cc[VQUIT] = 28; /* ^\ */
321 tty.c_cc[VERASE] = 127;
322 tty.c_cc[VKILL] = 24; /* ^X */
323 tty.c_cc[VEOF] = 4; /* ^D */
324 tty.c_cc[VTIME] = 0;
325 tty.c_cc[VMIN] = 1;
326 tty.c_cc[VSTART] = 17; /* ^Q */
327 tty.c_cc[VSTOP] = 19; /* ^S */
328 tty.c_cc[VSUSP] = 26; /* ^Z */
329
330 /* Pre and post processing */
331 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
332 tty.c_oflag = (OPOST | ONLCR);
333 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
334
335 /* Set the terminal line and flush it */
336 tcsetattr (0, TCSANOW, &tty);
337 tcflush (0, TCIOFLUSH);
338}
339
340
341/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000342 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100343 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100344 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000345 * Handle receiving the SEGV or ABRT signal, usually caused by one of
346 * our own mistakes. We deal with it by dumping core in a child process
347 * and just carrying on in the parent.
348 *
349 * This may or may not work, but the only alternative would be sigjmp()ing
350 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
351 * lists pointers) or exec'ing another process (which we couldn't transfer
352 * our state to anyway). This just hopes that the kernel resumes on the
353 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100354 **/
355static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000356crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100357{
358 pid_t pid;
359
360 pid = fork ();
361 if (pid == 0) {
362 struct sigaction act;
363 struct rlimit limit;
364 sigset_t mask;
365
366 /* Mask out all signals */
367 sigfillset (&mask);
368 sigprocmask (SIG_SETMASK, &mask, NULL);
369
Scott James Remnant16a286f2007-01-10 15:38:33 +0000370 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100371 act.sa_handler = SIG_DFL;
372 act.sa_flags = 0;
373 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000374 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100375
376 /* Dump in the root directory */
377 chdir ("/");
378
379 /* Don't limit the core dump size */
380 limit.rlim_cur = RLIM_INFINITY;
381 limit.rlim_max = RLIM_INFINITY;
382 setrlimit (RLIMIT_CORE, &limit);
383
Scott James Remnant16a286f2007-01-10 15:38:33 +0000384 /* Raise the signal again */
385 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100386
387 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000388 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100389 sigprocmask (SIG_SETMASK, &mask, NULL);
390
391 /* Wait for death */
392 pause ();
393 exit (0);
394 } else if (pid > 0) {
395 /* Wait for the core to be generated */
396 waitpid (pid, NULL, 0);
397
Scott James Remnant16a286f2007-01-10 15:38:33 +0000398 nih_error (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000399 (signum == SIGSEGV
400 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100401 } else {
Scott James Remnant16a286f2007-01-10 15:38:33 +0000402 nih_error (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000403 (signum == SIGSEGV
404 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100405 }
406}
407
408/**
409 * cad_handler:
410 * @data: unused,
411 * @signal: signal that called this handler.
412 *
413 * Handle having recieved the SIGINT signal, sent to us when somebody
414 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100415 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100416 **/
417static void
418cad_handler (void *data,
419 NihSignal *signal)
420{
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000421 event_emit (CTRLALTDEL_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100422}
423
424/**
425 * kbd_handler:
426 * @data: unused,
427 * @signal: signal that called this handler.
428 *
429 * Handle having recieved the SIGWINCH signal, sent to us when somebody
430 * presses Alt-UpArrow on the console. We just generate a
431 * kbdrequest event.
432 **/
433static void
434kbd_handler (void *data,
435 NihSignal *signal)
436{
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000437 event_emit (KBDREQUEST_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100438}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100439
440/**
441 * stop_handler:
442 * @data: unused,
443 * @signal: signal caught.
444 *
445 * This is called when we receive the STOP, TSTP or CONT signals; we
446 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000447 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100448 **/
449static void
450stop_handler (void *data,
451 NihSignal *signal)
452{
453 nih_assert (signal != NULL);
454
455 if (signal->signum == SIGCONT) {
456 nih_info (_("Event queue resumed"));
457 paused = FALSE;
458 } else {
459 nih_info (_("Event queue paused"));
460 paused = TRUE;
461 }
462}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100463
464
465/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100466 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100467 * @argv0: program to run,
468 * @signal: signal caught.
469 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100470 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100471 * to reexec ourselves.
472 **/
473static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100474term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100475 NihSignal *signal)
476{
Scott James Remnant6f464962006-09-01 04:10:20 +0100477 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100478 sigset_t mask, oldmask;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100479
480 nih_assert (argv0 != NULL);
481 nih_assert (signal != NULL);
482
483 nih_warn (_("Re-executing %s"), argv0);
484
Scott James Remnant1db88042006-09-01 03:14:19 +0100485 /* Block signals while we work. We're the last signal handler
486 * installed so this should mean that they're all handled now.
487 *
488 * The child must make sure that it unblocks these again when
489 * it's ready.
490 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100491 sigfillset (&mask);
492 sigprocmask (SIG_BLOCK, &mask, &oldmask);
493
Scott James Remnant3401ab72006-09-01 02:14:47 +0100494 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100495 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100496 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100497
Scott James Remnant6f464962006-09-01 04:10:20 +0100498 err = nih_error_get ();
499 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100500 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100501
Scott James Remnant3401ab72006-09-01 02:14:47 +0100502 sigprocmask (SIG_SETMASK, &oldmask, NULL);
503}