blob: d086929aa29a982a0620a8aa5000e020f1b4b375 [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"
Scott James Remnant54b2a952007-06-10 22:15:24 +010058#include "conf.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 Remnant7ba2cf62007-06-10 22:20:38 +010068static void hup_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010069static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant06abbec2007-03-09 13:02:38 +000070static void term_handler (void *data, 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 Remnant06abbec2007-03-09 13:02:38 +000074 * argv0:
75 *
76 * Path to program executed, used for re-executing the init binary from the
77 * same location we were executed from.
78 **/
79static const char *argv0 = NULL;
80
81/**
Scott James Remnant1db88042006-09-01 03:14:19 +010082 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010083 *
Scott James Remnant8b227402006-10-11 17:55:27 +010084 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010085 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010086 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010087static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010088
Scott James Remnant502ea702007-03-05 20:47:18 +000089/**
90 * rescue:
91 *
92 * This is set to TRUE if we're being re-exec'd by an existing init process
93 * that has crashed.
94 **/
95static int rescue = FALSE;
96
Scott James Remnant3401ab72006-09-01 02:14:47 +010097
98/**
99 * options:
100 *
101 * Command-line options we accept.
102 **/
103static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100104 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant502ea702007-03-05 20:47:18 +0000105 { 0, "rescue", NULL, NULL, NULL, &rescue, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100106
107 /* Ignore invalid options */
108 { '-', "--", NULL, NULL, NULL, NULL, NULL },
109
110 NIH_OPTION_LAST
111};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100112
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100113
Scott James Remnant50748842006-05-16 21:02:31 +0100114int
115main (int argc,
116 char *argv[])
117{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100118 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000119 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100120
Scott James Remnant06abbec2007-03-09 13:02:38 +0000121 argv0 = argv[0];
122 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200123
Scott James Remnant930e25a2006-10-13 12:28:05 +0100124 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100125 nih_option_set_help (
126 _("This daemon is normally executed by the kernel and given "
127 "process id 1 to denote its special status. When executed "
128 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100129
Scott James Remnant3401ab72006-09-01 02:14:47 +0100130 args = nih_option_parser (NULL, argc, argv, options, FALSE);
131 if (! args)
132 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200133
Scott James Remnanta17917d2006-09-07 00:18:28 +0100134 /* Check we're root */
135 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000136 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100137 exit (1);
138 }
139
140 /* Check we're process #1 */
141 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100142 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100143 /* Ignore failure, probably just that telinit doesn't exist */
144
Scott James Remnant31421b72007-03-08 23:53:05 +0000145 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100146 exit (1);
147 }
148
Scott James Remnant78626fb2007-01-10 16:48:10 +0000149
150 /* Clear our arguments from the command-line, so that we show up in
151 * ps or top output as /sbin/init, with no extra flags.
152 *
153 * This is a very Linux-specific trick; by deleting the NULL
154 * terminator at the end of the last argument, we fool the kernel
155 * into believing we used a setproctitle()-a-like to extend the
156 * argument space into the environment space, and thus make it use
157 * strlen() instead of its own assumed length. In fact, we've done
158 * the exact opposite, and shrunk the command line length to just that
159 * of whatever is in argv[0].
160 *
161 * If we don't do this, and just write \0 over the rest of argv, for
162 * example; the command-line length still includes those \0s, and ps
163 * will show whitespace in their place.
164 */
165 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000166 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000167
Scott James Remnant505f9282007-01-10 18:44:38 +0000168 arg_end = argv[argc-1] + strlen (argv[argc-1]);
169 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000170 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000171
Scott James Remnant2e204b72007-02-03 23:15:28 +0000172 /* Become session and process group leader (should be already,
Scott James Remnant5c408432007-11-07 21:42:25 -0500173 * but you never know what initramfs did)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000174 */
175 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100176
Scott James Remnant3401ab72006-09-01 02:14:47 +0100177 /* Send all logging output to syslog */
178 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200179 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200180
Scott James Remnant77e8db32006-08-21 08:47:50 +0200181 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100182 * device instead. Normally we reset the console, unless we're
183 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200184 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000185 for (int i = 0; i < 3; i++)
Scott James Remnant77e8db32006-08-21 08:47:50 +0200186 close (i);
187
Scott James Remnant5d702952007-01-05 17:21:34 +0000188 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
189 nih_free (nih_error_get ());
Scott James Remnant502ea702007-03-05 20:47:18 +0000190 if (! (restart || rescue))
Scott James Remnant3401ab72006-09-01 02:14:47 +0100191 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200192
Scott James Remnant2e204b72007-02-03 23:15:28 +0000193 /* Set the PATH environment variable */
194 setenv ("PATH", PATH, TRUE);
195
196
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100197 /* Reset the signal state and install the signal handler for those
198 * signals we actually want to catch; this also sets those that
199 * can be sent to us, because we're special
200 */
Scott James Remnant502ea702007-03-05 20:47:18 +0000201 if (! (restart || rescue))
Scott James Remnant2e204b72007-02-03 23:15:28 +0000202 nih_signal_reset ();
203
Scott James Remnant5c408432007-11-07 21:42:25 -0500204 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
205 * directly; it's enough that they interrupt the main loop and
206 * get dealt with during it.
207 */
208 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
209 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100210
Scott James Remnant5c408432007-11-07 21:42:25 -0500211 /* Catch fatal errors immediately rather than waiting for a new
212 * iteration through the main loop.
213 */
214 nih_signal_set_handler (SIGSEGV, crash_handler);
215 nih_signal_set_handler (SIGABRT, crash_handler);
216
217 /* Allow SIGTSTP and SIGCONT to pause and unpause event processing */
218 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000219 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500220
221 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000222 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100223
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100224 /* Ask the kernel to send us SIGINT when control-alt-delete is
225 * pressed; generate an event with the same name.
226 */
227 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500228 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000229 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100230
231 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
232 * generate a kbdrequest event.
233 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500234 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
235 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000236 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
237 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500238 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100239
Scott James Remnant2c950692007-02-25 09:13:38 +0000240 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500241 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000242 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
243
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100244 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500245 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100246 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
247
Scott James Remnant1db88042006-09-01 03:14:19 +0100248 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5c408432007-11-07 21:42:25 -0500249 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100250 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, term_handler, NULL));
Scott James Remnant1db88042006-09-01 03:14:19 +0100251
Scott James Remnant0eade492007-11-15 05:48:07 +0000252 /* Watch children for events */
253 NIH_MUST (nih_child_add_watch (NULL, -1,
254 (NIH_CHILD_EXITED | NIH_CHILD_KILLED
255 | NIH_CHILD_DUMPED),
256 job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100257
Scott James Remnant5c408432007-11-07 21:42:25 -0500258 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000259 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000260 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100261
Scott James Remnant94d00982006-08-25 15:38:22 +0200262
Scott James Remnant54b2a952007-06-10 22:15:24 +0100263 /* Read configuration */
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400264 NIH_MUST (conf_source_new (NULL, CONFDIR "/init.conf", CONF_FILE));
265 NIH_MUST (conf_source_new (NULL, CONFDIR "/conf.d", CONF_DIR));
266 NIH_MUST (conf_source_new (NULL, CONFDIR "/jobs.d", CONF_JOB_DIR));
Scott James Remnantdd9249d2007-06-12 15:30:19 +0100267#ifdef LEGACY_CONFDIR
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400268 NIH_MUST (conf_source_new (NULL, LEGACY_CONFDIR, CONF_JOB_DIR));
Scott James Remnantdd9249d2007-06-12 15:30:19 +0100269#endif /* LEGACY_CONFDIR */
Scott James Remnant54b2a952007-06-10 22:15:24 +0100270
271 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000272
Scott James Remnant3401ab72006-09-01 02:14:47 +0100273 /* Generate and run the startup event or read the state from the
274 * init daemon that exec'd us
275 */
Scott James Remnant502ea702007-03-05 20:47:18 +0000276 if (! (restart || rescue)) {
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100277 event_new (NULL, STARTUP_EVENT, NULL, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100278 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100279 sigset_t mask;
280
Scott James Remnant1db88042006-09-01 03:14:19 +0100281 /* We're ok to receive signals again */
282 sigemptyset (&mask);
283 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100284 }
285
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000286 /* Run through the loop at least once */
287 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200288 ret = nih_main_loop ();
289
290 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100291}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100292
293
294/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100295 * reset_console:
296 *
297 * Set up the console flags to something sensible. Cribbed from sysvinit,
298 * initng, etc.
299 **/
300static void
301reset_console (void)
302{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100303 struct termios tty;
304
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100305 tcgetattr (0, &tty);
306
307 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
308 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
309
310 /* Set up usual keys */
311 tty.c_cc[VINTR] = 3; /* ^C */
312 tty.c_cc[VQUIT] = 28; /* ^\ */
313 tty.c_cc[VERASE] = 127;
314 tty.c_cc[VKILL] = 24; /* ^X */
315 tty.c_cc[VEOF] = 4; /* ^D */
316 tty.c_cc[VTIME] = 0;
317 tty.c_cc[VMIN] = 1;
318 tty.c_cc[VSTART] = 17; /* ^Q */
319 tty.c_cc[VSTOP] = 19; /* ^S */
320 tty.c_cc[VSUSP] = 26; /* ^Z */
321
322 /* Pre and post processing */
323 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
324 tty.c_oflag = (OPOST | ONLCR);
325 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
326
327 /* Set the terminal line and flush it */
328 tcsetattr (0, TCSANOW, &tty);
329 tcflush (0, TCIOFLUSH);
330}
331
332
333/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000334 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100335 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100336 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000337 * Handle receiving the SEGV or ABRT signal, usually caused by one of
338 * our own mistakes. We deal with it by dumping core in a child process
339 * and just carrying on in the parent.
340 *
341 * This may or may not work, but the only alternative would be sigjmp()ing
342 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
343 * lists pointers) or exec'ing another process (which we couldn't transfer
344 * our state to anyway). This just hopes that the kernel resumes on the
345 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100346 **/
347static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000348crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100349{
Scott James Remnant06abbec2007-03-09 13:02:38 +0000350 NihError *err;
351 const char *loglevel = NULL;
352 sigset_t mask, oldmask;
353 pid_t pid;
354
355 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100356
357 pid = fork ();
358 if (pid == 0) {
359 struct sigaction act;
360 struct rlimit limit;
361 sigset_t mask;
362
363 /* Mask out all signals */
364 sigfillset (&mask);
365 sigprocmask (SIG_SETMASK, &mask, NULL);
366
Scott James Remnant16a286f2007-01-10 15:38:33 +0000367 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100368 act.sa_handler = SIG_DFL;
369 act.sa_flags = 0;
370 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000371 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100372
373 /* Dump in the root directory */
374 chdir ("/");
375
376 /* Don't limit the core dump size */
377 limit.rlim_cur = RLIM_INFINITY;
378 limit.rlim_max = RLIM_INFINITY;
379 setrlimit (RLIMIT_CORE, &limit);
380
Scott James Remnant16a286f2007-01-10 15:38:33 +0000381 /* Raise the signal again */
382 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100383
384 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000385 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100386 sigprocmask (SIG_SETMASK, &mask, NULL);
387
388 /* Wait for death */
389 pause ();
390 exit (0);
391 } else if (pid > 0) {
392 /* Wait for the core to be generated */
393 waitpid (pid, NULL, 0);
394
Scott James Remnant31421b72007-03-08 23:53:05 +0000395 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000396 (signum == SIGSEGV
397 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100398 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000399 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000400 (signum == SIGSEGV
401 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100402 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000403
404 /* There's no point carrying on from here; our state is almost
405 * likely in tatters, so we'd just end up core-dumping again and
406 * writing over the one that contains the real bug. We can't
407 * even re-exec properly, since we'd probably core dump while
408 * trying to transfer the state.
409 *
410 * So we just do the only thing we can, block out all signals
411 * and try and start again from scratch.
412 */
413 sigfillset (&mask);
414 sigprocmask (SIG_BLOCK, &mask, &oldmask);
415
416 /* Argument list */
Scott James Remnant06abbec2007-03-09 13:02:38 +0000417 if (nih_log_priority <= NIH_LOG_DEBUG) {
418 loglevel = "--debug";
419 } else if (nih_log_priority <= NIH_LOG_INFO) {
420 loglevel = "--verbose";
421 } else if (nih_log_priority >= NIH_LOG_ERROR) {
422 loglevel = "--error";
423 } else {
424 loglevel = NULL;
425 }
426 execl (argv0, argv0, "--rescue", loglevel, NULL);
Scott James Remnant502ea702007-03-05 20:47:18 +0000427 nih_error_raise_system ();
428
429 err = nih_error_get ();
Scott James Remnant31421b72007-03-08 23:53:05 +0000430 nih_fatal (_("Failed to re-execute %s: %s"),
Scott James Remnant502ea702007-03-05 20:47:18 +0000431 "/sbin/init", err->message);
432 nih_free (err);
433
434 sigprocmask (SIG_SETMASK, &oldmask, NULL);
435
436 /* Oh Bugger */
437 exit (1);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100438}
439
440/**
441 * cad_handler:
442 * @data: unused,
443 * @signal: signal that called this handler.
444 *
445 * Handle having recieved the SIGINT signal, sent to us when somebody
446 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100447 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100448 **/
449static void
450cad_handler (void *data,
451 NihSignal *signal)
452{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100453 event_new (NULL, CTRLALTDEL_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100454}
455
456/**
457 * kbd_handler:
458 * @data: unused,
459 * @signal: signal that called this handler.
460 *
461 * Handle having recieved the SIGWINCH signal, sent to us when somebody
462 * presses Alt-UpArrow on the console. We just generate a
463 * kbdrequest event.
464 **/
465static void
466kbd_handler (void *data,
467 NihSignal *signal)
468{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100469 event_new (NULL, KBDREQUEST_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100470}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100471
472/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000473 * pwr_handler:
474 * @data: unused,
475 * @signal: signal that called this handler.
476 *
477 * Handle having recieved the SIGPWR signal, sent to us when powstatd
478 * changes the /etc/powerstatus file. We just generate a
479 * power-status-changed event and jobs read the file.
480 **/
481static void
482pwr_handler (void *data,
483 NihSignal *signal)
484{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100485 event_new (NULL, PWRSTATUS_EVENT, NULL, NULL);
Scott James Remnant2c950692007-02-25 09:13:38 +0000486}
487
488/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100489 * hup_handler:
490 * @data: unused,
491 * @signal: signal that called this handler.
492 *
493 * Handle having recieved the SIGHUP signal, which we use to instruct us to
494 * reload our configuration.
495 **/
496static void
497hup_handler (void *data,
498 NihSignal *signal)
499{
500 nih_info (_("Reloading configuration"));
501 conf_reload ();
502}
503
504/**
Scott James Remnanteabb7802006-08-31 15:39:04 +0100505 * stop_handler:
506 * @data: unused,
507 * @signal: signal caught.
508 *
509 * This is called when we receive the STOP, TSTP or CONT signals; we
510 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000511 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100512 **/
513static void
514stop_handler (void *data,
515 NihSignal *signal)
516{
517 nih_assert (signal != NULL);
518
519 if (signal->signum == SIGCONT) {
520 nih_info (_("Event queue resumed"));
521 paused = FALSE;
522 } else {
523 nih_info (_("Event queue paused"));
524 paused = TRUE;
525 }
526}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100527
528
529/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100530 * term_handler:
Scott James Remnant06abbec2007-03-09 13:02:38 +0000531 * @data: unused,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100532 * @signal: signal caught.
533 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100534 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100535 * to reexec ourselves.
536 **/
537static void
Scott James Remnant06abbec2007-03-09 13:02:38 +0000538term_handler (void *data,
539 NihSignal *signal)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100540{
Scott James Remnant06abbec2007-03-09 13:02:38 +0000541 NihError *err;
542 const char *loglevel;
543 sigset_t mask, oldmask;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100544
545 nih_assert (argv0 != NULL);
546 nih_assert (signal != NULL);
547
548 nih_warn (_("Re-executing %s"), argv0);
549
Scott James Remnant1db88042006-09-01 03:14:19 +0100550 /* Block signals while we work. We're the last signal handler
551 * installed so this should mean that they're all handled now.
552 *
553 * The child must make sure that it unblocks these again when
554 * it's ready.
555 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100556 sigfillset (&mask);
557 sigprocmask (SIG_BLOCK, &mask, &oldmask);
558
Scott James Remnant3401ab72006-09-01 02:14:47 +0100559 /* Argument list */
Scott James Remnant06abbec2007-03-09 13:02:38 +0000560 if (nih_log_priority <= NIH_LOG_DEBUG) {
561 loglevel = "--debug";
562 } else if (nih_log_priority <= NIH_LOG_INFO) {
563 loglevel = "--verbose";
564 } else if (nih_log_priority >= NIH_LOG_ERROR) {
565 loglevel = "--error";
566 } else {
567 loglevel = NULL;
568 }
569 execl (argv0, argv0, "--restart", loglevel, NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100570 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100571
Scott James Remnant6f464962006-09-01 04:10:20 +0100572 err = nih_error_get ();
573 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100574 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100575
Scott James Remnant3401ab72006-09-01 02:14:47 +0100576 sigprocmask (SIG_SETMASK, &oldmask, NULL);
577}