blob: a94ddc6830f57ba9c33178f452e15b27b67a04f3 [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"
Scott James Remnante0d0dd12006-09-14 10:51:05 +010059#include "paths.h"
Scott James Remnant50748842006-05-16 21:02:31 +010060
61
Scott James Remnantf43bdf32006-08-27 18:20:29 +010062/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010063static void reset_console (void);
Scott James Remnant16a286f2007-01-10 15:38:33 +000064static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010065static void cad_handler (void *data, NihSignal *signal);
66static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000067static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010068static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant06abbec2007-03-09 13:02:38 +000069static void term_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010070
Scott James Remnant1db88042006-09-01 03:14:19 +010071
Scott James Remnant3401ab72006-09-01 02:14:47 +010072/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000073 * argv0:
74 *
75 * Path to program executed, used for re-executing the init binary from the
76 * same location we were executed from.
77 **/
78static const char *argv0 = NULL;
79
80/**
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
Scott James Remnant502ea702007-03-05 20:47:18 +000088/**
89 * rescue:
90 *
91 * This is set to TRUE if we're being re-exec'd by an existing init process
92 * that has crashed.
93 **/
94static int rescue = FALSE;
95
Scott James Remnant3401ab72006-09-01 02:14:47 +010096
97/**
98 * options:
99 *
100 * Command-line options we accept.
101 **/
102static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100103 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant502ea702007-03-05 20:47:18 +0000104 { 0, "rescue", NULL, NULL, NULL, &rescue, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100105
106 /* Ignore invalid options */
107 { '-', "--", NULL, NULL, NULL, NULL, NULL },
108
109 NIH_OPTION_LAST
110};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100111
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100112
Scott James Remnant50748842006-05-16 21:02:31 +0100113int
114main (int argc,
115 char *argv[])
116{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100117 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000118 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100119
Scott James Remnant06abbec2007-03-09 13:02:38 +0000120 argv0 = argv[0];
121 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200122
Scott James Remnant930e25a2006-10-13 12:28:05 +0100123 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100124 nih_option_set_help (
125 _("This daemon is normally executed by the kernel and given "
126 "process id 1 to denote its special status. When executed "
127 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100128
Scott James Remnant3401ab72006-09-01 02:14:47 +0100129 args = nih_option_parser (NULL, argc, argv, options, FALSE);
130 if (! args)
131 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200132
Scott James Remnanta17917d2006-09-07 00:18:28 +0100133 /* Check we're root */
134 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000135 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100136 exit (1);
137 }
138
139 /* Check we're process #1 */
140 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100141 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100142 /* Ignore failure, probably just that telinit doesn't exist */
143
Scott James Remnant31421b72007-03-08 23:53:05 +0000144 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100145 exit (1);
146 }
147
Scott James Remnant78626fb2007-01-10 16:48:10 +0000148
149 /* Clear our arguments from the command-line, so that we show up in
150 * ps or top output as /sbin/init, with no extra flags.
151 *
152 * This is a very Linux-specific trick; by deleting the NULL
153 * terminator at the end of the last argument, we fool the kernel
154 * into believing we used a setproctitle()-a-like to extend the
155 * argument space into the environment space, and thus make it use
156 * strlen() instead of its own assumed length. In fact, we've done
157 * the exact opposite, and shrunk the command line length to just that
158 * of whatever is in argv[0].
159 *
160 * If we don't do this, and just write \0 over the rest of argv, for
161 * example; the command-line length still includes those \0s, and ps
162 * will show whitespace in their place.
163 */
164 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000165 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000166
Scott James Remnant505f9282007-01-10 18:44:38 +0000167 arg_end = argv[argc-1] + strlen (argv[argc-1]);
168 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000169 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000170
Scott James Remnant2e204b72007-02-03 23:15:28 +0000171 /* Become session and process group leader (should be already,
172 * but you never know what initramfs did
173 */
174 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100175
Scott James Remnant3401ab72006-09-01 02:14:47 +0100176 /* Send all logging output to syslog */
177 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200178 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200179
Scott James Remnant77e8db32006-08-21 08:47:50 +0200180 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100181 * device instead. Normally we reset the console, unless we're
182 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200183 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000184 for (int i = 0; i < 3; i++)
Scott James Remnant77e8db32006-08-21 08:47:50 +0200185 close (i);
186
Scott James Remnant5d702952007-01-05 17:21:34 +0000187 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
188 nih_free (nih_error_get ());
Scott James Remnant502ea702007-03-05 20:47:18 +0000189 if (! (restart || rescue))
Scott James Remnant3401ab72006-09-01 02:14:47 +0100190 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200191
Scott James Remnant2e204b72007-02-03 23:15:28 +0000192 /* Set the PATH environment variable */
193 setenv ("PATH", PATH, TRUE);
194
195
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100196 /* Reset the signal state and install the signal handler for those
197 * signals we actually want to catch; this also sets those that
198 * can be sent to us, because we're special
199 */
Scott James Remnant502ea702007-03-05 20:47:18 +0000200 if (! (restart || rescue))
Scott James Remnant2e204b72007-02-03 23:15:28 +0000201 nih_signal_reset ();
202
203 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100204 nih_signal_set_handler (SIGALRM, nih_signal_handler);
205 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100206 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
207 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100208 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100209 nih_signal_set_handler (SIGINT, nih_signal_handler);
210 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000211 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000212 nih_signal_set_handler (SIGSEGV, crash_handler);
213 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100214
Scott James Remnanteabb7802006-08-31 15:39:04 +0100215 /* Ensure that we don't process events while paused */
Scott James Remnant5d702952007-01-05 17:21:34 +0000216 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
217 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100218
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100219 /* Ask the kernel to send us SIGINT when control-alt-delete is
220 * pressed; generate an event with the same name.
221 */
222 reboot (RB_DISABLE_CAD);
Scott James Remnant5d702952007-01-05 17:21:34 +0000223 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100224
225 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
226 * generate a kbdrequest event.
227 */
Scott James Remnant5d702952007-01-05 17:21:34 +0000228 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0)
229 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
230 kbd_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100231
Scott James Remnant2c950692007-02-25 09:13:38 +0000232 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
233 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
234
Scott James Remnant1db88042006-09-01 03:14:19 +0100235 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5d702952007-01-05 17:21:34 +0000236 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM,
237 (NihSignalHandler)term_handler,
Scott James Remnant06abbec2007-03-09 13:02:38 +0000238 NULL));
Scott James Remnant1db88042006-09-01 03:14:19 +0100239
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100240 /* Reap all children that die */
Scott James Remnant5d702952007-01-05 17:21:34 +0000241 NIH_MUST (nih_child_add_watch (NULL, -1, job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100242
Scott James Remnant65906602007-02-08 02:51:39 +0000243 /* Process the event queue and check the jobs to make sure we
244 * haven't stalled, every time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000245 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000246 NULL));
Scott James Remnantc6740472007-02-09 21:16:28 +0000247 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_stalled,
248 NULL));
249 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_free_deleted,
Scott James Remnant5d702952007-01-05 17:21:34 +0000250 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100251
Scott James Remnant94d00982006-08-25 15:38:22 +0200252
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000253 /* Open control socket */
254 if (! control_open ()) {
255 NihError *err;
256
257 err = nih_error_get ();
258 nih_fatal ("%s: %s", _("Unable to open control socket"),
259 err->message);
260 nih_free (err);
261
262 exit (1);
263 }
264
Scott James Remnante8b584e2007-06-03 19:08:38 +0100265 /* FIXME: Read configuration */
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000266
Scott James Remnant3401ab72006-09-01 02:14:47 +0100267 /* Generate and run the startup event or read the state from the
268 * init daemon that exec'd us
269 */
Scott James Remnant502ea702007-03-05 20:47:18 +0000270 if (! (restart || rescue)) {
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100271 event_new (NULL, STARTUP_EVENT, NULL, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100272 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100273 sigset_t mask;
274
Scott James Remnant1db88042006-09-01 03:14:19 +0100275 /* We're ok to receive signals again */
276 sigemptyset (&mask);
277 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100278 }
279
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000280 /* Run through the loop at least once */
281 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200282 ret = nih_main_loop ();
283
284 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100285}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100286
287
288/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100289 * reset_console:
290 *
291 * Set up the console flags to something sensible. Cribbed from sysvinit,
292 * initng, etc.
293 **/
294static void
295reset_console (void)
296{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100297 struct termios tty;
298
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100299 tcgetattr (0, &tty);
300
301 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
302 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
303
304 /* Set up usual keys */
305 tty.c_cc[VINTR] = 3; /* ^C */
306 tty.c_cc[VQUIT] = 28; /* ^\ */
307 tty.c_cc[VERASE] = 127;
308 tty.c_cc[VKILL] = 24; /* ^X */
309 tty.c_cc[VEOF] = 4; /* ^D */
310 tty.c_cc[VTIME] = 0;
311 tty.c_cc[VMIN] = 1;
312 tty.c_cc[VSTART] = 17; /* ^Q */
313 tty.c_cc[VSTOP] = 19; /* ^S */
314 tty.c_cc[VSUSP] = 26; /* ^Z */
315
316 /* Pre and post processing */
317 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
318 tty.c_oflag = (OPOST | ONLCR);
319 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
320
321 /* Set the terminal line and flush it */
322 tcsetattr (0, TCSANOW, &tty);
323 tcflush (0, TCIOFLUSH);
324}
325
326
327/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000328 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100329 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100330 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000331 * Handle receiving the SEGV or ABRT signal, usually caused by one of
332 * our own mistakes. We deal with it by dumping core in a child process
333 * and just carrying on in the parent.
334 *
335 * This may or may not work, but the only alternative would be sigjmp()ing
336 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
337 * lists pointers) or exec'ing another process (which we couldn't transfer
338 * our state to anyway). This just hopes that the kernel resumes on the
339 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100340 **/
341static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000342crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100343{
Scott James Remnant06abbec2007-03-09 13:02:38 +0000344 NihError *err;
345 const char *loglevel = NULL;
346 sigset_t mask, oldmask;
347 pid_t pid;
348
349 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100350
351 pid = fork ();
352 if (pid == 0) {
353 struct sigaction act;
354 struct rlimit limit;
355 sigset_t mask;
356
357 /* Mask out all signals */
358 sigfillset (&mask);
359 sigprocmask (SIG_SETMASK, &mask, NULL);
360
Scott James Remnant16a286f2007-01-10 15:38:33 +0000361 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100362 act.sa_handler = SIG_DFL;
363 act.sa_flags = 0;
364 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000365 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100366
367 /* Dump in the root directory */
368 chdir ("/");
369
370 /* Don't limit the core dump size */
371 limit.rlim_cur = RLIM_INFINITY;
372 limit.rlim_max = RLIM_INFINITY;
373 setrlimit (RLIMIT_CORE, &limit);
374
Scott James Remnant16a286f2007-01-10 15:38:33 +0000375 /* Raise the signal again */
376 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100377
378 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000379 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100380 sigprocmask (SIG_SETMASK, &mask, NULL);
381
382 /* Wait for death */
383 pause ();
384 exit (0);
385 } else if (pid > 0) {
386 /* Wait for the core to be generated */
387 waitpid (pid, NULL, 0);
388
Scott James Remnant31421b72007-03-08 23:53:05 +0000389 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000390 (signum == SIGSEGV
391 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100392 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000393 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000394 (signum == SIGSEGV
395 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100396 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000397
398 /* There's no point carrying on from here; our state is almost
399 * likely in tatters, so we'd just end up core-dumping again and
400 * writing over the one that contains the real bug. We can't
401 * even re-exec properly, since we'd probably core dump while
402 * trying to transfer the state.
403 *
404 * So we just do the only thing we can, block out all signals
405 * and try and start again from scratch.
406 */
407 sigfillset (&mask);
408 sigprocmask (SIG_BLOCK, &mask, &oldmask);
409
410 /* Argument list */
Scott James Remnant06abbec2007-03-09 13:02:38 +0000411 if (nih_log_priority <= NIH_LOG_DEBUG) {
412 loglevel = "--debug";
413 } else if (nih_log_priority <= NIH_LOG_INFO) {
414 loglevel = "--verbose";
415 } else if (nih_log_priority >= NIH_LOG_ERROR) {
416 loglevel = "--error";
417 } else {
418 loglevel = NULL;
419 }
420 execl (argv0, argv0, "--rescue", loglevel, NULL);
Scott James Remnant502ea702007-03-05 20:47:18 +0000421 nih_error_raise_system ();
422
423 err = nih_error_get ();
Scott James Remnant31421b72007-03-08 23:53:05 +0000424 nih_fatal (_("Failed to re-execute %s: %s"),
Scott James Remnant502ea702007-03-05 20:47:18 +0000425 "/sbin/init", err->message);
426 nih_free (err);
427
428 sigprocmask (SIG_SETMASK, &oldmask, NULL);
429
430 /* Oh Bugger */
431 exit (1);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100432}
433
434/**
435 * cad_handler:
436 * @data: unused,
437 * @signal: signal that called this handler.
438 *
439 * Handle having recieved the SIGINT signal, sent to us when somebody
440 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100441 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100442 **/
443static void
444cad_handler (void *data,
445 NihSignal *signal)
446{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100447 event_new (NULL, CTRLALTDEL_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100448}
449
450/**
451 * kbd_handler:
452 * @data: unused,
453 * @signal: signal that called this handler.
454 *
455 * Handle having recieved the SIGWINCH signal, sent to us when somebody
456 * presses Alt-UpArrow on the console. We just generate a
457 * kbdrequest event.
458 **/
459static void
460kbd_handler (void *data,
461 NihSignal *signal)
462{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100463 event_new (NULL, KBDREQUEST_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100464}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100465
466/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000467 * pwr_handler:
468 * @data: unused,
469 * @signal: signal that called this handler.
470 *
471 * Handle having recieved the SIGPWR signal, sent to us when powstatd
472 * changes the /etc/powerstatus file. We just generate a
473 * power-status-changed event and jobs read the file.
474 **/
475static void
476pwr_handler (void *data,
477 NihSignal *signal)
478{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100479 event_new (NULL, PWRSTATUS_EVENT, NULL, NULL);
Scott James Remnant2c950692007-02-25 09:13:38 +0000480}
481
482/**
Scott James Remnanteabb7802006-08-31 15:39:04 +0100483 * stop_handler:
484 * @data: unused,
485 * @signal: signal caught.
486 *
487 * This is called when we receive the STOP, TSTP or CONT signals; we
488 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000489 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100490 **/
491static void
492stop_handler (void *data,
493 NihSignal *signal)
494{
495 nih_assert (signal != NULL);
496
497 if (signal->signum == SIGCONT) {
498 nih_info (_("Event queue resumed"));
499 paused = FALSE;
500 } else {
501 nih_info (_("Event queue paused"));
502 paused = TRUE;
503 }
504}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100505
506
507/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100508 * term_handler:
Scott James Remnant06abbec2007-03-09 13:02:38 +0000509 * @data: unused,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100510 * @signal: signal caught.
511 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100512 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100513 * to reexec ourselves.
514 **/
515static void
Scott James Remnant06abbec2007-03-09 13:02:38 +0000516term_handler (void *data,
517 NihSignal *signal)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100518{
Scott James Remnant06abbec2007-03-09 13:02:38 +0000519 NihError *err;
520 const char *loglevel;
521 sigset_t mask, oldmask;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100522
523 nih_assert (argv0 != NULL);
524 nih_assert (signal != NULL);
525
526 nih_warn (_("Re-executing %s"), argv0);
527
Scott James Remnant1db88042006-09-01 03:14:19 +0100528 /* Block signals while we work. We're the last signal handler
529 * installed so this should mean that they're all handled now.
530 *
531 * The child must make sure that it unblocks these again when
532 * it's ready.
533 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100534 sigfillset (&mask);
535 sigprocmask (SIG_BLOCK, &mask, &oldmask);
536
Scott James Remnant3401ab72006-09-01 02:14:47 +0100537 /* Argument list */
Scott James Remnant06abbec2007-03-09 13:02:38 +0000538 if (nih_log_priority <= NIH_LOG_DEBUG) {
539 loglevel = "--debug";
540 } else if (nih_log_priority <= NIH_LOG_INFO) {
541 loglevel = "--verbose";
542 } else if (nih_log_priority >= NIH_LOG_ERROR) {
543 loglevel = "--error";
544 } else {
545 loglevel = NULL;
546 }
547 execl (argv0, argv0, "--restart", loglevel, NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100548 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100549
Scott James Remnant6f464962006-09-01 04:10:20 +0100550 err = nih_error_get ();
551 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100552 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100553
Scott James Remnant3401ab72006-09-01 02:14:47 +0100554 sigprocmask (SIG_SETMASK, &oldmask, NULL);
555}