blob: dbd9876db8b013323e71c9056acb3a66e650ff57 [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 Remnantf43bdf32006-08-27 18:20:29 +010063/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010064static void reset_console (void);
Scott James Remnant16a286f2007-01-10 15:38:33 +000065static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010066static void cad_handler (void *data, NihSignal *signal);
67static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000068static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010069static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant1db88042006-09-01 03:14:19 +010070static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010071
Scott James Remnant1db88042006-09-01 03:14:19 +010072
Scott James Remnant3401ab72006-09-01 02:14:47 +010073/**
Scott James Remnant1db88042006-09-01 03:14:19 +010074 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010075 *
Scott James Remnant8b227402006-10-11 17:55:27 +010076 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010077 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010078 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010079static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010080
81
82/**
83 * options:
84 *
85 * Command-line options we accept.
86 **/
87static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010088 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010089
90 /* Ignore invalid options */
91 { '-', "--", NULL, NULL, NULL, NULL, NULL },
92
93 NIH_OPTION_LAST
94};
Scott James Remnantff0d26a2006-08-31 20:49:43 +010095
Scott James Remnantf43bdf32006-08-27 18:20:29 +010096
Scott James Remnant50748842006-05-16 21:02:31 +010097int
98main (int argc,
99 char *argv[])
100{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100101 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000102 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100103
Scott James Remnant77e8db32006-08-21 08:47:50 +0200104 nih_main_init (argv[0]);
105
Scott James Remnant930e25a2006-10-13 12:28:05 +0100106 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100107 nih_option_set_help (
108 _("This daemon is normally executed by the kernel and given "
109 "process id 1 to denote its special status. When executed "
110 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100111
Scott James Remnant3401ab72006-09-01 02:14:47 +0100112 args = nih_option_parser (NULL, argc, argv, options, FALSE);
113 if (! args)
114 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200115
Scott James Remnanta17917d2006-09-07 00:18:28 +0100116 /* Check we're root */
117 if (getuid ()) {
118 nih_error (_("Need to be root"));
119 exit (1);
120 }
121
122 /* Check we're process #1 */
123 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100124 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100125 /* Ignore failure, probably just that telinit doesn't exist */
126
127 nih_error (_("Not being executed as init"));
128 exit (1);
129 }
130
Scott James Remnant2e204b72007-02-03 23:15:28 +0000131 /* Open control socket */
132 if (! control_open ()) {
133 NihError *err;
134
135 err = nih_error_get ();
136 nih_error ("%s: %s", _("Unable to open control socket"),
137 err->message);
138 nih_free (err);
139
140 exit (1);
141 }
142
143 /* Read configuration */
144 if (cfg_watch_dir (CFG_DIR) < 0) {
145 NihError *err;
146
147 err = nih_error_get ();
148 nih_error ("%s: %s", _("Error parsing configuration"),
149 err->message);
150 nih_free (err);
151
152 exit (1);
153 }
154
Scott James Remnant78626fb2007-01-10 16:48:10 +0000155
156 /* Clear our arguments from the command-line, so that we show up in
157 * ps or top output as /sbin/init, with no extra flags.
158 *
159 * This is a very Linux-specific trick; by deleting the NULL
160 * terminator at the end of the last argument, we fool the kernel
161 * into believing we used a setproctitle()-a-like to extend the
162 * argument space into the environment space, and thus make it use
163 * strlen() instead of its own assumed length. In fact, we've done
164 * the exact opposite, and shrunk the command line length to just that
165 * of whatever is in argv[0].
166 *
167 * If we don't do this, and just write \0 over the rest of argv, for
168 * example; the command-line length still includes those \0s, and ps
169 * will show whitespace in their place.
170 */
171 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000172 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000173
Scott James Remnant505f9282007-01-10 18:44:38 +0000174 arg_end = argv[argc-1] + strlen (argv[argc-1]);
175 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000176 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000177
Scott James Remnant2e204b72007-02-03 23:15:28 +0000178 /* Become session and process group leader (should be already,
179 * but you never know what initramfs did
180 */
181 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100182
Scott James Remnant3401ab72006-09-01 02:14:47 +0100183 /* Send all logging output to syslog */
184 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200185 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200186
Scott James Remnant77e8db32006-08-21 08:47:50 +0200187 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100188 * device instead. Normally we reset the console, unless we're
189 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200190 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000191 for (int i = 0; i < 3; i++)
Scott James Remnant77e8db32006-08-21 08:47:50 +0200192 close (i);
193
Scott James Remnant5d702952007-01-05 17:21:34 +0000194 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
195 nih_free (nih_error_get ());
Scott James Remnant1db88042006-09-01 03:14:19 +0100196 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100197 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200198
Scott James Remnant2e204b72007-02-03 23:15:28 +0000199 /* Set the PATH environment variable */
200 setenv ("PATH", PATH, TRUE);
201
202
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100203 /* Reset the signal state and install the signal handler for those
204 * signals we actually want to catch; this also sets those that
205 * can be sent to us, because we're special
206 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000207 if (! restart)
208 nih_signal_reset ();
209
210 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100211 nih_signal_set_handler (SIGALRM, nih_signal_handler);
212 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100213 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
214 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100215 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100216 nih_signal_set_handler (SIGINT, nih_signal_handler);
217 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000218 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000219 nih_signal_set_handler (SIGSEGV, crash_handler);
220 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100221
Scott James Remnanteabb7802006-08-31 15:39:04 +0100222 /* Ensure that we don't process events while paused */
Scott James Remnant5d702952007-01-05 17:21:34 +0000223 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
224 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100225
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100226 /* Ask the kernel to send us SIGINT when control-alt-delete is
227 * pressed; generate an event with the same name.
228 */
229 reboot (RB_DISABLE_CAD);
Scott James Remnant5d702952007-01-05 17:21:34 +0000230 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100231
232 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
233 * generate a kbdrequest event.
234 */
Scott James Remnant5d702952007-01-05 17:21:34 +0000235 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0)
236 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
237 kbd_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100238
Scott James Remnant2c950692007-02-25 09:13:38 +0000239 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
240 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
241
Scott James Remnant1db88042006-09-01 03:14:19 +0100242 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5d702952007-01-05 17:21:34 +0000243 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM,
244 (NihSignalHandler)term_handler,
245 argv[0]));
Scott James Remnant1db88042006-09-01 03:14:19 +0100246
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100247 /* Reap all children that die */
Scott James Remnant5d702952007-01-05 17:21:34 +0000248 NIH_MUST (nih_child_add_watch (NULL, -1, job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100249
Scott James Remnant65906602007-02-08 02:51:39 +0000250 /* Process the event queue and check the jobs to make sure we
251 * haven't stalled, every time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000252 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000253 NULL));
Scott James Remnantc6740472007-02-09 21:16:28 +0000254 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_stalled,
255 NULL));
256 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_free_deleted,
Scott James Remnant5d702952007-01-05 17:21:34 +0000257 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100258
Scott James Remnant94d00982006-08-25 15:38:22 +0200259
Scott James Remnant3401ab72006-09-01 02:14:47 +0100260 /* Generate and run the startup event or read the state from the
261 * init daemon that exec'd us
262 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100263 if (! restart) {
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000264 event_emit (STARTUP_EVENT, NULL, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100265 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100266 sigset_t mask;
267
Scott James Remnant1db88042006-09-01 03:14:19 +0100268 /* We're ok to receive signals again */
269 sigemptyset (&mask);
270 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100271 }
272
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000273 /* Run through the loop at least once */
274 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200275 ret = nih_main_loop ();
276
277 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100278}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100279
280
281/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100282 * reset_console:
283 *
284 * Set up the console flags to something sensible. Cribbed from sysvinit,
285 * initng, etc.
286 **/
287static void
288reset_console (void)
289{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100290 struct termios tty;
291
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100292 tcgetattr (0, &tty);
293
294 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
295 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
296
297 /* Set up usual keys */
298 tty.c_cc[VINTR] = 3; /* ^C */
299 tty.c_cc[VQUIT] = 28; /* ^\ */
300 tty.c_cc[VERASE] = 127;
301 tty.c_cc[VKILL] = 24; /* ^X */
302 tty.c_cc[VEOF] = 4; /* ^D */
303 tty.c_cc[VTIME] = 0;
304 tty.c_cc[VMIN] = 1;
305 tty.c_cc[VSTART] = 17; /* ^Q */
306 tty.c_cc[VSTOP] = 19; /* ^S */
307 tty.c_cc[VSUSP] = 26; /* ^Z */
308
309 /* Pre and post processing */
310 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
311 tty.c_oflag = (OPOST | ONLCR);
312 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
313
314 /* Set the terminal line and flush it */
315 tcsetattr (0, TCSANOW, &tty);
316 tcflush (0, TCIOFLUSH);
317}
318
319
320/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000321 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100322 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100323 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000324 * Handle receiving the SEGV or ABRT signal, usually caused by one of
325 * our own mistakes. We deal with it by dumping core in a child process
326 * and just carrying on in the parent.
327 *
328 * This may or may not work, but the only alternative would be sigjmp()ing
329 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
330 * lists pointers) or exec'ing another process (which we couldn't transfer
331 * our state to anyway). This just hopes that the kernel resumes on the
332 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100333 **/
334static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000335crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100336{
337 pid_t pid;
338
339 pid = fork ();
340 if (pid == 0) {
341 struct sigaction act;
342 struct rlimit limit;
343 sigset_t mask;
344
345 /* Mask out all signals */
346 sigfillset (&mask);
347 sigprocmask (SIG_SETMASK, &mask, NULL);
348
Scott James Remnant16a286f2007-01-10 15:38:33 +0000349 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100350 act.sa_handler = SIG_DFL;
351 act.sa_flags = 0;
352 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000353 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100354
355 /* Dump in the root directory */
356 chdir ("/");
357
358 /* Don't limit the core dump size */
359 limit.rlim_cur = RLIM_INFINITY;
360 limit.rlim_max = RLIM_INFINITY;
361 setrlimit (RLIMIT_CORE, &limit);
362
Scott James Remnant16a286f2007-01-10 15:38:33 +0000363 /* Raise the signal again */
364 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100365
366 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000367 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100368 sigprocmask (SIG_SETMASK, &mask, NULL);
369
370 /* Wait for death */
371 pause ();
372 exit (0);
373 } else if (pid > 0) {
374 /* Wait for the core to be generated */
375 waitpid (pid, NULL, 0);
376
Scott James Remnant16a286f2007-01-10 15:38:33 +0000377 nih_error (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000378 (signum == SIGSEGV
379 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100380 } else {
Scott James Remnant16a286f2007-01-10 15:38:33 +0000381 nih_error (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000382 (signum == SIGSEGV
383 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100384 }
385}
386
387/**
388 * cad_handler:
389 * @data: unused,
390 * @signal: signal that called this handler.
391 *
392 * Handle having recieved the SIGINT signal, sent to us when somebody
393 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100394 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100395 **/
396static void
397cad_handler (void *data,
398 NihSignal *signal)
399{
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000400 event_emit (CTRLALTDEL_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100401}
402
403/**
404 * kbd_handler:
405 * @data: unused,
406 * @signal: signal that called this handler.
407 *
408 * Handle having recieved the SIGWINCH signal, sent to us when somebody
409 * presses Alt-UpArrow on the console. We just generate a
410 * kbdrequest event.
411 **/
412static void
413kbd_handler (void *data,
414 NihSignal *signal)
415{
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000416 event_emit (KBDREQUEST_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100417}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100418
419/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000420 * pwr_handler:
421 * @data: unused,
422 * @signal: signal that called this handler.
423 *
424 * Handle having recieved the SIGPWR signal, sent to us when powstatd
425 * changes the /etc/powerstatus file. We just generate a
426 * power-status-changed event and jobs read the file.
427 **/
428static void
429pwr_handler (void *data,
430 NihSignal *signal)
431{
432 event_emit (PWRSTATUS_EVENT, NULL, NULL);
433}
434
435/**
Scott James Remnanteabb7802006-08-31 15:39:04 +0100436 * stop_handler:
437 * @data: unused,
438 * @signal: signal caught.
439 *
440 * This is called when we receive the STOP, TSTP or CONT signals; we
441 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000442 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100443 **/
444static void
445stop_handler (void *data,
446 NihSignal *signal)
447{
448 nih_assert (signal != NULL);
449
450 if (signal->signum == SIGCONT) {
451 nih_info (_("Event queue resumed"));
452 paused = FALSE;
453 } else {
454 nih_info (_("Event queue paused"));
455 paused = TRUE;
456 }
457}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100458
459
460/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100461 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100462 * @argv0: program to run,
463 * @signal: signal caught.
464 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100465 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100466 * to reexec ourselves.
467 **/
468static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100469term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100470 NihSignal *signal)
471{
Scott James Remnant6f464962006-09-01 04:10:20 +0100472 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100473 sigset_t mask, oldmask;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100474
475 nih_assert (argv0 != NULL);
476 nih_assert (signal != NULL);
477
478 nih_warn (_("Re-executing %s"), argv0);
479
Scott James Remnant1db88042006-09-01 03:14:19 +0100480 /* Block signals while we work. We're the last signal handler
481 * installed so this should mean that they're all handled now.
482 *
483 * The child must make sure that it unblocks these again when
484 * it's ready.
485 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100486 sigfillset (&mask);
487 sigprocmask (SIG_BLOCK, &mask, &oldmask);
488
Scott James Remnant3401ab72006-09-01 02:14:47 +0100489 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100490 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100491 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100492
Scott James Remnant6f464962006-09-01 04:10:20 +0100493 err = nih_error_get ();
494 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100495 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100496
Scott James Remnant3401ab72006-09-01 02:14:47 +0100497 sigprocmask (SIG_SETMASK, &oldmask, NULL);
498}