blob: c6d64c052712d0ea3610239c46f322eb980b3a98 [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>
40
Scott James Remnant097b2a92006-09-01 19:30:37 +010041#include <linux/kd.h>
42
Scott James Remnant77e8db32006-08-21 08:47:50 +020043#include <nih/macros.h>
44#include <nih/alloc.h>
45#include <nih/list.h>
46#include <nih/timer.h>
47#include <nih/signal.h>
48#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010049#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020050#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010051#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020052#include <nih/logging.h>
53
54#include "process.h"
55#include "job.h"
56#include "event.h"
Scott James Remnant54b2a952007-06-10 22:15:24 +010057#include "conf.h"
Scott James Remnante0d0dd12006-09-14 10:51:05 +010058#include "paths.h"
Scott James Remnant50748842006-05-16 21:02:31 +010059
60
Scott James Remnantf43bdf32006-08-27 18:20:29 +010061/* Prototypes for static functions */
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000062#ifndef DEBUG
Scott James Remnant16a286f2007-01-10 15:38:33 +000063static void crash_handler (int signum);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000064#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +000065static void term_handler (void *data, NihSignal *signal);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000066#ifndef DEBUG
Scott James Remnant3401ab72006-09-01 02:14:47 +010067static void cad_handler (void *data, NihSignal *signal);
68static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000069static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000070#endif /* DEBUG */
Scott James Remnant7ba2cf62007-06-10 22:20:38 +010071static void hup_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010072static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010073
Scott James Remnant1db88042006-09-01 03:14:19 +010074
Scott James Remnant3401ab72006-09-01 02:14:47 +010075/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000076 * argv0:
77 *
78 * Path to program executed, used for re-executing the init binary from the
79 * same location we were executed from.
80 **/
81static const char *argv0 = NULL;
82
83/**
Scott James Remnant1db88042006-09-01 03:14:19 +010084 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010085 *
Scott James Remnant8b227402006-10-11 17:55:27 +010086 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010087 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010088 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010089static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010090
Scott James Remnant502ea702007-03-05 20:47:18 +000091/**
92 * rescue:
93 *
94 * This is set to TRUE if we're being re-exec'd by an existing init process
95 * that has crashed.
96 **/
97static int rescue = FALSE;
98
Scott James Remnant3401ab72006-09-01 02:14:47 +010099
100/**
101 * options:
102 *
103 * Command-line options we accept.
104 **/
105static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100106 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant502ea702007-03-05 20:47:18 +0000107 { 0, "rescue", NULL, NULL, NULL, &rescue, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100108
109 /* Ignore invalid options */
110 { '-', "--", NULL, NULL, NULL, NULL, NULL },
111
112 NIH_OPTION_LAST
113};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100114
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100115
Scott James Remnant50748842006-05-16 21:02:31 +0100116int
117main (int argc,
118 char *argv[])
119{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100120 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000121 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100122
Scott James Remnant06abbec2007-03-09 13:02:38 +0000123 argv0 = argv[0];
124 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200125
Scott James Remnant930e25a2006-10-13 12:28:05 +0100126 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100127 nih_option_set_help (
128 _("This daemon is normally executed by the kernel and given "
129 "process id 1 to denote its special status. When executed "
130 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100131
Scott James Remnant3401ab72006-09-01 02:14:47 +0100132 args = nih_option_parser (NULL, argc, argv, options, FALSE);
133 if (! args)
134 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200135
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000136#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100137 /* Check we're root */
138 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000139 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100140 exit (1);
141 }
142
143 /* Check we're process #1 */
144 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100145 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100146 /* Ignore failure, probably just that telinit doesn't exist */
147
Scott James Remnant31421b72007-03-08 23:53:05 +0000148 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100149 exit (1);
150 }
151
Scott James Remnant78626fb2007-01-10 16:48:10 +0000152 /* Clear our arguments from the command-line, so that we show up in
153 * ps or top output as /sbin/init, with no extra flags.
154 *
155 * This is a very Linux-specific trick; by deleting the NULL
156 * terminator at the end of the last argument, we fool the kernel
157 * into believing we used a setproctitle()-a-like to extend the
158 * argument space into the environment space, and thus make it use
159 * strlen() instead of its own assumed length. In fact, we've done
160 * the exact opposite, and shrunk the command line length to just that
161 * of whatever is in argv[0].
162 *
163 * If we don't do this, and just write \0 over the rest of argv, for
164 * example; the command-line length still includes those \0s, and ps
165 * will show whitespace in their place.
166 */
167 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000168 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000169
Scott James Remnant505f9282007-01-10 18:44:38 +0000170 arg_end = argv[argc-1] + strlen (argv[argc-1]);
171 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000172 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000173
Scott James Remnantfa733382008-01-15 15:33:27 +0000174
175 /* Become the leader of a new session and process group, shedding
176 * any controlling tty (which we shouldn't have had anyway - but
177 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000178 */
179 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100180
Scott James Remnantfa733382008-01-15 15:33:27 +0000181 /* Set the standard file descriptors to the ordinary console device,
182 * resetting it to sane defaults unless we're inheriting from another
183 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200184 */
Scott James Remnantfa733382008-01-15 15:33:27 +0000185 if (process_setup_console (CONSOLE_OUTPUT, ! (restart || rescue)) < 0)
Scott James Remnant5d702952007-01-05 17:21:34 +0000186 nih_free (nih_error_get ());
Scott James Remnant77e8db32006-08-21 08:47:50 +0200187
Scott James Remnant2e204b72007-02-03 23:15:28 +0000188 /* Set the PATH environment variable */
189 setenv ("PATH", PATH, TRUE);
190
Scott James Remnantfa733382008-01-15 15:33:27 +0000191 /* Switch to the root directory in case we were started from some
192 * strange place, or worse, some directory in the initramfs that's
193 * going to go away soon.
194 */
195 chdir ("/");
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000196#else /* DEBUG */
197 nih_log_set_priority (NIH_LOG_DEBUG);
198#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000199
Scott James Remnant2e204b72007-02-03 23:15:28 +0000200
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100201 /* Reset the signal state and install the signal handler for those
202 * signals we actually want to catch; this also sets those that
203 * can be sent to us, because we're special
204 */
Scott James Remnant502ea702007-03-05 20:47:18 +0000205 if (! (restart || rescue))
Scott James Remnant2e204b72007-02-03 23:15:28 +0000206 nih_signal_reset ();
207
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000208#ifndef DEBUG \
209 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000210 * iteration through the main loop.
211 */
212 nih_signal_set_handler (SIGSEGV, crash_handler);
213 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000214#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000215
Scott James Remnant5c408432007-11-07 21:42:25 -0500216 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
217 * directly; it's enough that they interrupt the main loop and
218 * get dealt with during it.
219 */
220 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
221 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100222
Scott James Remnant5c408432007-11-07 21:42:25 -0500223 /* Allow SIGTSTP and SIGCONT to pause and unpause event processing */
224 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000225 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500226
227 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000228 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100229
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000230#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100231 /* Ask the kernel to send us SIGINT when control-alt-delete is
232 * pressed; generate an event with the same name.
233 */
234 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500235 nih_signal_set_handler (SIGINT, nih_signal_handler);
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 Remnant5c408432007-11-07 21:42:25 -0500241 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
242 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000243 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
244 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500245 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100246
Scott James Remnant2c950692007-02-25 09:13:38 +0000247 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500248 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000249 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000250#endif /* DEBUG */
Scott James Remnant2c950692007-02-25 09:13:38 +0000251
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100252 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500253 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100254 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
255
Scott James Remnantfa733382008-01-15 15:33:27 +0000256 /* SIGTERM instructs us to re-exec ourselves; this should be the
257 * last in the list to ensure that all other signals are handled
258 * before a SIGTERM.
259 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500260 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100261 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, term_handler, NULL));
Scott James Remnant1db88042006-09-01 03:14:19 +0100262
Scott James Remnantfa733382008-01-15 15:33:27 +0000263
Scott James Remnant0eade492007-11-15 05:48:07 +0000264 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000265 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
266 job_child_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100267
Scott James Remnant5c408432007-11-07 21:42:25 -0500268 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000269 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000270 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100271
Scott James Remnant94d00982006-08-25 15:38:22 +0200272
Scott James Remnant54b2a952007-06-10 22:15:24 +0100273 /* Read configuration */
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400274 NIH_MUST (conf_source_new (NULL, CONFDIR "/init.conf", CONF_FILE));
275 NIH_MUST (conf_source_new (NULL, CONFDIR "/conf.d", CONF_DIR));
276 NIH_MUST (conf_source_new (NULL, CONFDIR "/jobs.d", CONF_JOB_DIR));
Scott James Remnantdd9249d2007-06-12 15:30:19 +0100277#ifdef LEGACY_CONFDIR
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400278 NIH_MUST (conf_source_new (NULL, LEGACY_CONFDIR, CONF_JOB_DIR));
Scott James Remnantdd9249d2007-06-12 15:30:19 +0100279#endif /* LEGACY_CONFDIR */
Scott James Remnant54b2a952007-06-10 22:15:24 +0100280
281 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000282
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000283#ifndef DEBUG \
Scott James Remnantfa733382008-01-15 15:33:27 +0000284 /* Now that the startup is complete, send all further logging output
285 * to syslog instead of to the console.
286 */
287 openlog (program_name, LOG_CONS, LOG_DAEMON);
288 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000289#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000290
291
Scott James Remnant3401ab72006-09-01 02:14:47 +0100292 /* Generate and run the startup event or read the state from the
293 * init daemon that exec'd us
294 */
Scott James Remnant502ea702007-03-05 20:47:18 +0000295 if (! (restart || rescue)) {
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100296 event_new (NULL, STARTUP_EVENT, NULL, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100297 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100298 sigset_t mask;
299
Scott James Remnant1db88042006-09-01 03:14:19 +0100300 /* We're ok to receive signals again */
301 sigemptyset (&mask);
302 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100303 }
304
Scott James Remnantfa733382008-01-15 15:33:27 +0000305 /* Run through the loop at least once to deal with signals that were
306 * delivered to the previous process while the mask was set or to
307 * process the startup event we emitted.
308 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000309 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200310 ret = nih_main_loop ();
311
312 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100313}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100314
315
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000316#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100317/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000318 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100319 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100320 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000321 * Handle receiving the SEGV or ABRT signal, usually caused by one of
322 * our own mistakes. We deal with it by dumping core in a child process
323 * and just carrying on in the parent.
324 *
325 * This may or may not work, but the only alternative would be sigjmp()ing
326 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
327 * lists pointers) or exec'ing another process (which we couldn't transfer
328 * our state to anyway). This just hopes that the kernel resumes on the
329 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100330 **/
331static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000332crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100333{
Scott James Remnant06abbec2007-03-09 13:02:38 +0000334 NihError *err;
335 const char *loglevel = NULL;
336 sigset_t mask, oldmask;
337 pid_t pid;
338
339 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100340
341 pid = fork ();
342 if (pid == 0) {
343 struct sigaction act;
344 struct rlimit limit;
345 sigset_t mask;
346
347 /* Mask out all signals */
348 sigfillset (&mask);
349 sigprocmask (SIG_SETMASK, &mask, NULL);
350
Scott James Remnant16a286f2007-01-10 15:38:33 +0000351 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100352 act.sa_handler = SIG_DFL;
353 act.sa_flags = 0;
354 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000355 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100356
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100357 /* Don't limit the core dump size */
358 limit.rlim_cur = RLIM_INFINITY;
359 limit.rlim_max = RLIM_INFINITY;
360 setrlimit (RLIMIT_CORE, &limit);
361
Scott James Remnantfa733382008-01-15 15:33:27 +0000362 /* Dump in the root directory */
363 chdir ("/");
364
Scott James Remnant16a286f2007-01-10 15:38:33 +0000365 /* Raise the signal again */
366 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100367
368 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000369 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100370 sigprocmask (SIG_SETMASK, &mask, NULL);
371
372 /* Wait for death */
373 pause ();
374 exit (0);
375 } else if (pid > 0) {
376 /* Wait for the core to be generated */
377 waitpid (pid, NULL, 0);
378
Scott James Remnant31421b72007-03-08 23:53:05 +0000379 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000380 (signum == SIGSEGV
381 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100382 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000383 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000384 (signum == SIGSEGV
385 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100386 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000387
388 /* There's no point carrying on from here; our state is almost
389 * likely in tatters, so we'd just end up core-dumping again and
390 * writing over the one that contains the real bug. We can't
391 * even re-exec properly, since we'd probably core dump while
392 * trying to transfer the state.
393 *
394 * So we just do the only thing we can, block out all signals
395 * and try and start again from scratch.
396 */
397 sigfillset (&mask);
398 sigprocmask (SIG_BLOCK, &mask, &oldmask);
399
400 /* Argument list */
Scott James Remnant06abbec2007-03-09 13:02:38 +0000401 if (nih_log_priority <= NIH_LOG_DEBUG) {
402 loglevel = "--debug";
403 } else if (nih_log_priority <= NIH_LOG_INFO) {
404 loglevel = "--verbose";
405 } else if (nih_log_priority >= NIH_LOG_ERROR) {
406 loglevel = "--error";
407 } else {
408 loglevel = NULL;
409 }
410 execl (argv0, argv0, "--rescue", loglevel, NULL);
Scott James Remnant502ea702007-03-05 20:47:18 +0000411 nih_error_raise_system ();
412
413 err = nih_error_get ();
Scott James Remnant31421b72007-03-08 23:53:05 +0000414 nih_fatal (_("Failed to re-execute %s: %s"),
Scott James Remnant502ea702007-03-05 20:47:18 +0000415 "/sbin/init", err->message);
416 nih_free (err);
417
418 sigprocmask (SIG_SETMASK, &oldmask, NULL);
419
420 /* Oh Bugger */
421 exit (1);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100422}
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000423#endif
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100424
425/**
Scott James Remnantfa733382008-01-15 15:33:27 +0000426 * term_handler:
427 * @data: unused,
428 * @signal: signal caught.
429 *
430 * This is called when we receive the TERM signal, which instructs us
431 * to reexec ourselves.
432 **/
433static void
434term_handler (void *data,
435 NihSignal *signal)
436{
437 NihError *err;
438 const char *loglevel;
439 sigset_t mask, oldmask;
440
441 nih_assert (argv0 != NULL);
442 nih_assert (signal != NULL);
443
444 nih_warn (_("Re-executing %s"), argv0);
445
446 /* Block signals while we work. We're the last signal handler
447 * installed so this should mean that they're all handled now.
448 *
449 * The child must make sure that it unblocks these again when
450 * it's ready.
451 */
452 sigfillset (&mask);
453 sigprocmask (SIG_BLOCK, &mask, &oldmask);
454
455 /* Argument list */
456 if (nih_log_priority <= NIH_LOG_DEBUG) {
457 loglevel = "--debug";
458 } else if (nih_log_priority <= NIH_LOG_INFO) {
459 loglevel = "--verbose";
460 } else if (nih_log_priority >= NIH_LOG_ERROR) {
461 loglevel = "--error";
462 } else {
463 loglevel = NULL;
464 }
465 execl (argv0, argv0, "--restart", loglevel, NULL);
466 nih_error_raise_system ();
467
468 err = nih_error_get ();
469 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
470 nih_free (err);
471
472 sigprocmask (SIG_SETMASK, &oldmask, NULL);
473}
474
475
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000476#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000477/**
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100478 * cad_handler:
479 * @data: unused,
480 * @signal: signal that called this handler.
481 *
482 * Handle having recieved the SIGINT signal, sent to us when somebody
483 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100484 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100485 **/
486static void
487cad_handler (void *data,
488 NihSignal *signal)
489{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100490 event_new (NULL, CTRLALTDEL_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100491}
492
493/**
494 * kbd_handler:
495 * @data: unused,
496 * @signal: signal that called this handler.
497 *
498 * Handle having recieved the SIGWINCH signal, sent to us when somebody
499 * presses Alt-UpArrow on the console. We just generate a
500 * kbdrequest event.
501 **/
502static void
503kbd_handler (void *data,
504 NihSignal *signal)
505{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100506 event_new (NULL, KBDREQUEST_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100507}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100508
509/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000510 * pwr_handler:
511 * @data: unused,
512 * @signal: signal that called this handler.
513 *
514 * Handle having recieved the SIGPWR signal, sent to us when powstatd
515 * changes the /etc/powerstatus file. We just generate a
516 * power-status-changed event and jobs read the file.
517 **/
518static void
519pwr_handler (void *data,
520 NihSignal *signal)
521{
Scott James Remnant186ecdc2007-05-18 20:18:17 +0100522 event_new (NULL, PWRSTATUS_EVENT, NULL, NULL);
Scott James Remnant2c950692007-02-25 09:13:38 +0000523}
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000524#endif
Scott James Remnant2c950692007-02-25 09:13:38 +0000525
526/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100527 * hup_handler:
528 * @data: unused,
529 * @signal: signal that called this handler.
530 *
531 * Handle having recieved the SIGHUP signal, which we use to instruct us to
532 * reload our configuration.
533 **/
534static void
535hup_handler (void *data,
536 NihSignal *signal)
537{
538 nih_info (_("Reloading configuration"));
539 conf_reload ();
540}
541
542/**
Scott James Remnanteabb7802006-08-31 15:39:04 +0100543 * stop_handler:
544 * @data: unused,
545 * @signal: signal caught.
546 *
547 * This is called when we receive the STOP, TSTP or CONT signals; we
548 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000549 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100550 **/
551static void
552stop_handler (void *data,
553 NihSignal *signal)
554{
555 nih_assert (signal != NULL);
556
557 if (signal->signum == SIGCONT) {
558 nih_info (_("Event queue resumed"));
559 paused = FALSE;
560 } else {
561 nih_info (_("Event queue paused"));
562 paused = TRUE;
563 }
564}