blob: 5cedf47b403964cc681f19eebdeb2a8f90be9f48 [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnante7138052010-02-04 00:41:25 -08003 * Copyright © 2010 Canonical Ltd.
Scott James Remnant7d5b2ea2009-05-22 15:20:12 +02004 * Author: Scott James Remnant <scott@netsplit.com>.
Scott James Remnant50748842006-05-16 21:02:31 +01005 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +01006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
Scott James Remnant75123022009-05-22 13:27:56 +02008 * published by the Free Software Foundation.
Scott James Remnant50748842006-05-16 21:02:31 +01009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +010015 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Scott James Remnant50748842006-05-16 21:02:31 +010018 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif /* HAVE_CONFIG_H */
23
24
Scott James Remnantf43bdf32006-08-27 18:20:29 +010025#include <sys/types.h>
Scott James Remnantea806b72006-10-18 15:01:00 +010026#include <sys/time.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010027#include <sys/wait.h>
28#include <sys/ioctl.h>
29#include <sys/reboot.h>
30#include <sys/resource.h>
31
Scott James Remnant3401ab72006-09-01 02:14:47 +010032#include <errno.h>
33#include <stdio.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010034#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020035#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010036#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020037#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020038#include <unistd.h>
39
Scott James Remnant097b2a92006-09-01 19:30:37 +010040#include <linux/kd.h>
41
Scott James Remnant77e8db32006-08-21 08:47:50 +020042#include <nih/macros.h>
43#include <nih/alloc.h>
44#include <nih/list.h>
45#include <nih/timer.h>
46#include <nih/signal.h>
47#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010048#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020049#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010050#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020051#include <nih/logging.h>
52
Scott James Remnantf2f69d02008-04-29 23:38:23 +010053#include "paths.h"
Scott James Remnant5d82d902008-04-30 00:03:29 +010054#include "events.h"
Scott James Remnantf2f69d02008-04-29 23:38:23 +010055#include "system.h"
Scott James Remnant63fd5c72008-04-30 21:34:31 +010056#include "job_process.h"
Scott James Remnant77e8db32006-08-21 08:47:50 +020057#include "event.h"
Scott James Remnant54b2a952007-06-10 22:15:24 +010058#include "conf.h"
Scott James Remnantf8491442008-04-18 13:19:24 +010059#include "control.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 Remnant9f62a8e2008-01-15 19:22:36 +000063#ifndef DEBUG
Scott James Remnant02977f82011-02-17 15:33:04 -080064static int logger_kmsg (NihLogLevel priority, const char *message);
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 Remnant7ba2cf62007-06-10 22:20:38 +010069static void hup_handler (void *data, NihSignal *signal);
Scott James Remnante7138052010-02-04 00:41:25 -080070static void usr1_handler (void *data, NihSignal *signal);
Scott James Remnantf3ef5112008-06-05 01:26:10 +010071#endif /* DEBUG */
Scott James Remnant3401ab72006-09-01 02:14:47 +010072
Scott James Remnant1db88042006-09-01 03:14:19 +010073
Scott James Remnant3401ab72006-09-01 02:14:47 +010074/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000075 * argv0:
76 *
77 * Path to program executed, used for re-executing the init binary from the
78 * same location we were executed from.
79 **/
80static const char *argv0 = NULL;
81
82/**
Scott James Remnant1db88042006-09-01 03:14:19 +010083 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010084 *
Scott James Remnant8b227402006-10-11 17:55:27 +010085 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010086 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010087 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010088static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010089
90
91/**
92 * options:
93 *
94 * Command-line options we accept.
95 **/
96static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010097 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010098
99 /* Ignore invalid options */
100 { '-', "--", NULL, NULL, NULL, NULL, NULL },
101
102 NIH_OPTION_LAST
103};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100104
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100105
Scott James Remnant50748842006-05-16 21:02:31 +0100106int
107main (int argc,
108 char *argv[])
109{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100110 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000111 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100112
Scott James Remnant06abbec2007-03-09 13:02:38 +0000113 argv0 = argv[0];
114 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200115
Scott James Remnant930e25a2006-10-13 12:28:05 +0100116 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100117 nih_option_set_help (
118 _("This daemon is normally executed by the kernel and given "
119 "process id 1 to denote its special status. When executed "
120 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100121
Scott James Remnant3401ab72006-09-01 02:14:47 +0100122 args = nih_option_parser (NULL, argc, argv, options, FALSE);
123 if (! args)
124 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200125
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000126#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100127 /* Check we're root */
128 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000129 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100130 exit (1);
131 }
132
133 /* Check we're process #1 */
134 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100135 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100136 /* Ignore failure, probably just that telinit doesn't exist */
137
Scott James Remnant31421b72007-03-08 23:53:05 +0000138 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100139 exit (1);
140 }
141
Scott James Remnant78626fb2007-01-10 16:48:10 +0000142 /* Clear our arguments from the command-line, so that we show up in
143 * ps or top output as /sbin/init, with no extra flags.
144 *
145 * This is a very Linux-specific trick; by deleting the NULL
146 * terminator at the end of the last argument, we fool the kernel
147 * into believing we used a setproctitle()-a-like to extend the
148 * argument space into the environment space, and thus make it use
149 * strlen() instead of its own assumed length. In fact, we've done
150 * the exact opposite, and shrunk the command line length to just that
151 * of whatever is in argv[0].
152 *
153 * If we don't do this, and just write \0 over the rest of argv, for
154 * example; the command-line length still includes those \0s, and ps
155 * will show whitespace in their place.
156 */
157 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000158 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000159
Scott James Remnant505f9282007-01-10 18:44:38 +0000160 arg_end = argv[argc-1] + strlen (argv[argc-1]);
161 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000162 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000163
Scott James Remnantfa733382008-01-15 15:33:27 +0000164
165 /* Become the leader of a new session and process group, shedding
166 * any controlling tty (which we shouldn't have had anyway - but
167 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000168 */
169 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100170
Scott James Remnantfa733382008-01-15 15:33:27 +0000171 /* Set the standard file descriptors to the ordinary console device,
172 * resetting it to sane defaults unless we're inheriting from another
173 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200174 */
Scott James Remnantf2f69d02008-04-29 23:38:23 +0100175 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0)
Scott James Remnant5d702952007-01-05 17:21:34 +0000176 nih_free (nih_error_get ());
Scott James Remnantf99ea0c2010-02-04 11:14:30 -0800177 if (system_setup_console (CONSOLE_NONE, FALSE) < 0)
178 nih_free (nih_error_get ());
Scott James Remnant77e8db32006-08-21 08:47:50 +0200179
Scott James Remnant2e204b72007-02-03 23:15:28 +0000180 /* Set the PATH environment variable */
181 setenv ("PATH", PATH, TRUE);
182
Scott James Remnantfa733382008-01-15 15:33:27 +0000183 /* Switch to the root directory in case we were started from some
184 * strange place, or worse, some directory in the initramfs that's
185 * going to go away soon.
186 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100187 if (chdir ("/"))
188 nih_warn ("%s: %s", _("Unable to set root directory"),
189 strerror (errno));
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000190
191 /* Mount the /proc and /sys filesystems, which are pretty much
192 * essential for any Linux system; not to mention used by
193 * ourselves.
194 */
195 if (system_mount ("proc", "/proc") < 0) {
196 NihError *err;
197
198 err = nih_error_get ();
199 nih_warn ("%s: %s", _("Unable to mount /proc filesystem"),
200 err->message);
201 nih_free (err);
202 }
203
204 if (system_mount ("sysfs", "/sys") < 0) {
205 NihError *err;
206
207 err = nih_error_get ();
208 nih_warn ("%s: %s", _("Unable to mount /sys filesystem"),
209 err->message);
210 nih_free (err);
211 }
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000212#else /* DEBUG */
213 nih_log_set_priority (NIH_LOG_DEBUG);
James Hunt39f1c4f2010-12-13 18:15:24 +0000214 nih_debug ("Running as PID %d (PPID %d)",
215 (int)getpid (), (int)getppid ());
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000216#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000217
Scott James Remnant2e204b72007-02-03 23:15:28 +0000218
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100219 /* Reset the signal state and install the signal handler for those
220 * signals we actually want to catch; this also sets those that
221 * can be sent to us, because we're special
222 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100223 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000224 nih_signal_reset ();
225
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100226#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000227 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000228 * iteration through the main loop.
229 */
230 nih_signal_set_handler (SIGSEGV, crash_handler);
231 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000232#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000233
Scott James Remnant5c408432007-11-07 21:42:25 -0500234 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
235 * directly; it's enough that they interrupt the main loop and
236 * get dealt with during it.
237 */
238 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
239 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100240
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000241#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100242 /* Ask the kernel to send us SIGINT when control-alt-delete is
243 * pressed; generate an event with the same name.
244 */
245 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500246 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000247 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100248
249 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100250 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100251 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500252 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
253 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000254 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
255 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500256 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100257
Scott James Remnant2c950692007-02-25 09:13:38 +0000258 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500259 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000260 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
261
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100262 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500263 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100264 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnante7138052010-02-04 00:41:25 -0800265
266 /* SIGUSR1 instructs us to reconnect to D-Bus */
267 nih_signal_set_handler (SIGUSR1, nih_signal_handler);
268 NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100269#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100270
Scott James Remnantfa733382008-01-15 15:33:27 +0000271
Scott James Remnant0eade492007-11-15 05:48:07 +0000272 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000273 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100274 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100275
Scott James Remnant5c408432007-11-07 21:42:25 -0500276 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000277 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000278 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100279
Scott James Remnant94d00982006-08-25 15:38:22 +0200280
Scott James Remnant54b2a952007-06-10 22:15:24 +0100281 /* Read configuration */
Scott James Remnant854fda22009-06-23 01:21:55 +0100282 NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
283 NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100284
285 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000286
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100287 /* Create a listening server for private connections. */
288 while (control_server_open () < 0) {
289 NihError *err;
290
291 err = nih_error_get ();
292 if (err->number != ENOMEM) {
293 nih_warn ("%s: %s", _("Unable to listen for private connections"),
294 err->message);
295 nih_free (err);
296 break;
297 }
298 nih_free (err);
299 }
300
Scott James Remnantf8491442008-04-18 13:19:24 +0100301 /* Open connection to the system bus; we normally expect this to
302 * fail and will try again later - don't let ENOMEM stop us though.
303 */
304 while (control_bus_open () < 0) {
305 NihError *err;
306 int number;
307
308 err = nih_error_get ();
309 number = err->number;
310 nih_free (err);
311
312 if (number != ENOMEM)
313 break;
314 }
315
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100316#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000317 /* Now that the startup is complete, send all further logging output
Scott James Remnant02977f82011-02-17 15:33:04 -0800318 * to kmsg instead of to the console.
Scott James Remnantfa733382008-01-15 15:33:27 +0000319 */
Scott James Remnant02977f82011-02-17 15:33:04 -0800320 nih_log_set_logger (logger_kmsg);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000321#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000322
323
Scott James Remnant3401ab72006-09-01 02:14:47 +0100324 /* Generate and run the startup event or read the state from the
325 * init daemon that exec'd us
326 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100327 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100328 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100329 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100330 sigset_t mask;
331
Scott James Remnant1db88042006-09-01 03:14:19 +0100332 /* We're ok to receive signals again */
333 sigemptyset (&mask);
334 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100335 }
336
Scott James Remnantfa733382008-01-15 15:33:27 +0000337 /* Run through the loop at least once to deal with signals that were
338 * delivered to the previous process while the mask was set or to
339 * process the startup event we emitted.
340 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000341 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200342 ret = nih_main_loop ();
343
344 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100345}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100346
347
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000348#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100349/**
Scott James Remnant02977f82011-02-17 15:33:04 -0800350 * logger_kmsg:
351 * @priority: priority of message being logged,
352 * @message: message to log.
353 *
354 * Outputs the @message to the kernel log message socket prefixed with an
355 * appropriate tag based on @priority, the program name and terminated with
356 * a new line.
357 *
358 * Returns: zero on success, negative value on error.
359 **/
360static int
361logger_kmsg (NihLogLevel priority,
362 const char *message)
363{
364 int tag;
365 FILE *kmsg;
366
367 nih_assert (message != NULL);
368
369 switch (priority) {
370 case NIH_LOG_DEBUG:
371 tag = '7';
372 break;
373 case NIH_LOG_INFO:
374 tag = '6';
375 break;
376 case NIH_LOG_MESSAGE:
377 tag = '5';
378 break;
379 case NIH_LOG_WARN:
380 tag = '4';
381 break;
382 case NIH_LOG_ERROR:
383 tag = '3';
384 break;
385 case NIH_LOG_FATAL:
386 tag = '2';
387 break;
388 default:
389 tag = 'd';
390 }
391
392 kmsg = fopen ("/dev/kmsg", "w");
393 if (! kmsg)
394 return -1;
395
396 if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
397 int saved_errno = errno;
398 fclose (kmsg);
399 errno = saved_errno;
400 return -1;
401 }
402
403 if (fclose (kmsg) < 0)
404 return -1;
405
406 return 0;
407}
408
409
410/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000411 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100412 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100413 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000414 * Handle receiving the SEGV or ABRT signal, usually caused by one of
415 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100416 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000417 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100418 * Sadly there's no real alternative to the ensuing kernel panic. Our
419 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
420 * re-exec since the system will be suddenly lobotomised. We definitely
421 * don't want to start a root shell or anything like that. Best thing is
422 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100423 **/
424static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000425crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100426{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100427 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000428
429 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100430
431 pid = fork ();
432 if (pid == 0) {
433 struct sigaction act;
434 struct rlimit limit;
435 sigset_t mask;
436
437 /* Mask out all signals */
438 sigfillset (&mask);
439 sigprocmask (SIG_SETMASK, &mask, NULL);
440
Scott James Remnant16a286f2007-01-10 15:38:33 +0000441 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100442 act.sa_handler = SIG_DFL;
443 act.sa_flags = 0;
444 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000445 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100446
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100447 /* Don't limit the core dump size */
448 limit.rlim_cur = RLIM_INFINITY;
449 limit.rlim_max = RLIM_INFINITY;
450 setrlimit (RLIMIT_CORE, &limit);
451
Scott James Remnantfa733382008-01-15 15:33:27 +0000452 /* Dump in the root directory */
Scott James Remnant9d736bb2009-07-21 18:15:20 +0100453 if (chdir ("/"))
454 nih_warn ("%s: %s", _("Unable to set root directory"),
455 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000456
Scott James Remnant16a286f2007-01-10 15:38:33 +0000457 /* Raise the signal again */
458 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100459
460 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000461 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100462 sigprocmask (SIG_SETMASK, &mask, NULL);
463
464 /* Wait for death */
465 pause ();
466 exit (0);
467 } else if (pid > 0) {
468 /* Wait for the core to be generated */
469 waitpid (pid, NULL, 0);
470
Scott James Remnant31421b72007-03-08 23:53:05 +0000471 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000472 (signum == SIGSEGV
473 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100474 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000475 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000476 (signum == SIGSEGV
477 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100478 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000479
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100480 /* Goodbye, cruel world. */
481 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100482}
483
484/**
485 * cad_handler:
486 * @data: unused,
487 * @signal: signal that called this handler.
488 *
489 * Handle having recieved the SIGINT signal, sent to us when somebody
490 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100491 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100492 **/
493static void
494cad_handler (void *data,
495 NihSignal *signal)
496{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100497 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100498}
499
500/**
501 * kbd_handler:
502 * @data: unused,
503 * @signal: signal that called this handler.
504 *
505 * Handle having recieved the SIGWINCH signal, sent to us when somebody
506 * presses Alt-UpArrow on the console. We just generate a
507 * kbdrequest event.
508 **/
509static void
510kbd_handler (void *data,
511 NihSignal *signal)
512{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100513 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100514}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100515
516/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000517 * pwr_handler:
518 * @data: unused,
519 * @signal: signal that called this handler.
520 *
521 * Handle having recieved the SIGPWR signal, sent to us when powstatd
522 * changes the /etc/powerstatus file. We just generate a
523 * power-status-changed event and jobs read the file.
524 **/
525static void
526pwr_handler (void *data,
527 NihSignal *signal)
528{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100529 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000530}
531
532/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100533 * hup_handler:
534 * @data: unused,
535 * @signal: signal that called this handler.
536 *
537 * Handle having recieved the SIGHUP signal, which we use to instruct us to
538 * reload our configuration.
539 **/
540static void
541hup_handler (void *data,
542 NihSignal *signal)
543{
544 nih_info (_("Reloading configuration"));
545 conf_reload ();
Scott James Remnante7138052010-02-04 00:41:25 -0800546}
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100547
Scott James Remnante7138052010-02-04 00:41:25 -0800548/**
549 * usr1_handler:
550 * @data: unused,
551 * @signal: signal that called this handler.
552 *
553 * Handle having recieved the SIGUSR signal, which we use to instruct us to
554 * reconnect to D-Bus.
555 **/
556static void
557usr1_handler (void *data,
558 NihSignal *signal)
559{
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100560 if (! control_bus) {
561 nih_info (_("Reconnecting to system bus"));
562
563 if (control_bus_open () < 0) {
564 NihError *err;
565
566 err = nih_error_get ();
567 nih_warn ("%s: %s", _("Unable to connect to the system bus"),
568 err->message);
569 nih_free (err);
570 }
571 }
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100572}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100573#endif /* DEBUG */