blob: fdc4d016f7978216ebf959549e0c4b7eb46aeb39 [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnant540dcfd2011-03-16 15:54:56 -07003 * Copyright © 2011 Google Inc.
Scott James Remnante7138052010-02-04 00:41:25 -08004 * Copyright © 2010 Canonical Ltd.
Scott James Remnant7d5b2ea2009-05-22 15:20:12 +02005 * Author: Scott James Remnant <scott@netsplit.com>.
Scott James Remnant50748842006-05-16 21:02:31 +01006 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2, as
Scott James Remnant75123022009-05-22 13:27:56 +02009 * published by the Free Software Foundation.
Scott James Remnant50748842006-05-16 21:02:31 +010010 *
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 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +010016 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Scott James Remnant50748842006-05-16 21:02:31 +010019 */
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
Scott James Remnantf2f69d02008-04-29 23:38:23 +010054#include "paths.h"
Scott James Remnant5d82d902008-04-30 00:03:29 +010055#include "events.h"
Scott James Remnantf2f69d02008-04-29 23:38:23 +010056#include "system.h"
Scott James Remnant63fd5c72008-04-30 21:34:31 +010057#include "job_process.h"
Scott James Remnant77e8db32006-08-21 08:47:50 +020058#include "event.h"
Scott James Remnant54b2a952007-06-10 22:15:24 +010059#include "conf.h"
Scott James Remnantf8491442008-04-18 13:19:24 +010060#include "control.h"
Scott James Remnant50748842006-05-16 21:02:31 +010061
62
Scott James Remnantf43bdf32006-08-27 18:20:29 +010063/* Prototypes for static functions */
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000064#ifndef DEBUG
Scott James Remnant02977f82011-02-17 15:33:04 -080065static int logger_kmsg (NihLogLevel priority, const char *message);
Scott James Remnant16a286f2007-01-10 15:38:33 +000066static void crash_handler (int signum);
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 Remnant7ba2cf62007-06-10 22:20:38 +010070static void hup_handler (void *data, NihSignal *signal);
Scott James Remnante7138052010-02-04 00:41:25 -080071static void usr1_handler (void *data, NihSignal *signal);
Scott James Remnantf3ef5112008-06-05 01:26:10 +010072#endif /* DEBUG */
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
91
92/**
93 * options:
94 *
95 * Command-line options we accept.
96 **/
97static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010098 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010099
100 /* Ignore invalid options */
101 { '-', "--", NULL, NULL, NULL, NULL, NULL },
102
103 NIH_OPTION_LAST
104};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100105
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100106
Scott James Remnant50748842006-05-16 21:02:31 +0100107int
108main (int argc,
109 char *argv[])
110{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100111 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000112 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100113
Scott James Remnant06abbec2007-03-09 13:02:38 +0000114 argv0 = argv[0];
115 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200116
Scott James Remnant930e25a2006-10-13 12:28:05 +0100117 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100118 nih_option_set_help (
119 _("This daemon is normally executed by the kernel and given "
120 "process id 1 to denote its special status. When executed "
121 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100122
Scott James Remnant3401ab72006-09-01 02:14:47 +0100123 args = nih_option_parser (NULL, argc, argv, options, FALSE);
124 if (! args)
125 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200126
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000127#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100128 /* Check we're root */
129 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000130 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100131 exit (1);
132 }
133
134 /* Check we're process #1 */
135 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100136 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100137 /* Ignore failure, probably just that telinit doesn't exist */
138
Scott James Remnant31421b72007-03-08 23:53:05 +0000139 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100140 exit (1);
141 }
142
Scott James Remnant78626fb2007-01-10 16:48:10 +0000143 /* Clear our arguments from the command-line, so that we show up in
144 * ps or top output as /sbin/init, with no extra flags.
145 *
146 * This is a very Linux-specific trick; by deleting the NULL
147 * terminator at the end of the last argument, we fool the kernel
148 * into believing we used a setproctitle()-a-like to extend the
149 * argument space into the environment space, and thus make it use
150 * strlen() instead of its own assumed length. In fact, we've done
151 * the exact opposite, and shrunk the command line length to just that
152 * of whatever is in argv[0].
153 *
154 * If we don't do this, and just write \0 over the rest of argv, for
155 * example; the command-line length still includes those \0s, and ps
156 * will show whitespace in their place.
157 */
158 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000159 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000160
Scott James Remnant505f9282007-01-10 18:44:38 +0000161 arg_end = argv[argc-1] + strlen (argv[argc-1]);
162 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000163 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000164
Scott James Remnantfa733382008-01-15 15:33:27 +0000165
166 /* Become the leader of a new session and process group, shedding
167 * any controlling tty (which we shouldn't have had anyway - but
168 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000169 */
170 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100171
Scott James Remnantfa733382008-01-15 15:33:27 +0000172 /* Set the standard file descriptors to the ordinary console device,
173 * resetting it to sane defaults unless we're inheriting from another
174 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200175 */
Scott James Remnantf2f69d02008-04-29 23:38:23 +0100176 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0)
Scott James Remnant5d702952007-01-05 17:21:34 +0000177 nih_free (nih_error_get ());
Scott James Remnant77e8db32006-08-21 08:47:50 +0200178
Scott James Remnant2e204b72007-02-03 23:15:28 +0000179 /* Set the PATH environment variable */
180 setenv ("PATH", PATH, TRUE);
181
Scott James Remnantfa733382008-01-15 15:33:27 +0000182 /* Switch to the root directory in case we were started from some
183 * strange place, or worse, some directory in the initramfs that's
184 * going to go away soon.
185 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100186 if (chdir ("/"))
187 nih_warn ("%s: %s", _("Unable to set root directory"),
188 strerror (errno));
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000189
190 /* Mount the /proc and /sys filesystems, which are pretty much
191 * essential for any Linux system; not to mention used by
192 * ourselves.
193 */
194 if (system_mount ("proc", "/proc") < 0) {
195 NihError *err;
196
197 err = nih_error_get ();
198 nih_warn ("%s: %s", _("Unable to mount /proc filesystem"),
199 err->message);
200 nih_free (err);
201 }
202
203 if (system_mount ("sysfs", "/sys") < 0) {
204 NihError *err;
205
206 err = nih_error_get ();
207 nih_warn ("%s: %s", _("Unable to mount /sys filesystem"),
208 err->message);
209 nih_free (err);
210 }
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000211#else /* DEBUG */
212 nih_log_set_priority (NIH_LOG_DEBUG);
James Hunt39f1c4f2010-12-13 18:15:24 +0000213 nih_debug ("Running as PID %d (PPID %d)",
214 (int)getpid (), (int)getppid ());
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000215#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000216
Scott James Remnant2e204b72007-02-03 23:15:28 +0000217
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100218 /* Reset the signal state and install the signal handler for those
219 * signals we actually want to catch; this also sets those that
220 * can be sent to us, because we're special
221 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100222 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000223 nih_signal_reset ();
224
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100225#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000226 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000227 * iteration through the main loop.
228 */
229 nih_signal_set_handler (SIGSEGV, crash_handler);
230 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000231#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000232
Scott James Remnant5c408432007-11-07 21:42:25 -0500233 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
234 * directly; it's enough that they interrupt the main loop and
235 * get dealt with during it.
236 */
237 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
238 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100239
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000240#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100241 /* Ask the kernel to send us SIGINT when control-alt-delete is
242 * pressed; generate an event with the same name.
243 */
244 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500245 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000246 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100247
248 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100249 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100250 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500251 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
252 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000253 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
254 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500255 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100256
Scott James Remnant2c950692007-02-25 09:13:38 +0000257 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500258 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000259 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
260
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100261 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500262 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100263 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnante7138052010-02-04 00:41:25 -0800264
265 /* SIGUSR1 instructs us to reconnect to D-Bus */
266 nih_signal_set_handler (SIGUSR1, nih_signal_handler);
267 NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100268#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100269
Scott James Remnantfa733382008-01-15 15:33:27 +0000270
Scott James Remnant0eade492007-11-15 05:48:07 +0000271 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000272 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100273 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100274
Scott James Remnant5c408432007-11-07 21:42:25 -0500275 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000276 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000277 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100278
Scott James Remnant94d00982006-08-25 15:38:22 +0200279
Scott James Remnant54b2a952007-06-10 22:15:24 +0100280 /* Read configuration */
Scott James Remnant854fda22009-06-23 01:21:55 +0100281 NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
282 NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100283
284 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000285
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100286 /* Create a listening server for private connections. */
287 while (control_server_open () < 0) {
288 NihError *err;
289
290 err = nih_error_get ();
291 if (err->number != ENOMEM) {
292 nih_warn ("%s: %s", _("Unable to listen for private connections"),
293 err->message);
294 nih_free (err);
295 break;
296 }
297 nih_free (err);
298 }
299
Scott James Remnantf8491442008-04-18 13:19:24 +0100300 /* Open connection to the system bus; we normally expect this to
301 * fail and will try again later - don't let ENOMEM stop us though.
302 */
303 while (control_bus_open () < 0) {
304 NihError *err;
305 int number;
306
307 err = nih_error_get ();
308 number = err->number;
309 nih_free (err);
310
311 if (number != ENOMEM)
312 break;
313 }
314
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100315#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000316 /* Now that the startup is complete, send all further logging output
Scott James Remnant02977f82011-02-17 15:33:04 -0800317 * to kmsg instead of to the console.
Scott James Remnantfa733382008-01-15 15:33:27 +0000318 */
Scott James Remnant540dcfd2011-03-16 15:54:56 -0700319 if (system_setup_console (CONSOLE_NONE, FALSE) < 0)
320 nih_free (nih_error_get ());
321
Scott James Remnant02977f82011-02-17 15:33:04 -0800322 nih_log_set_logger (logger_kmsg);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000323#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000324
325
Scott James Remnant3401ab72006-09-01 02:14:47 +0100326 /* Generate and run the startup event or read the state from the
327 * init daemon that exec'd us
328 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100329 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100330 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100331 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100332 sigset_t mask;
333
Scott James Remnant1db88042006-09-01 03:14:19 +0100334 /* We're ok to receive signals again */
335 sigemptyset (&mask);
336 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100337 }
338
Scott James Remnantfa733382008-01-15 15:33:27 +0000339 /* Run through the loop at least once to deal with signals that were
340 * delivered to the previous process while the mask was set or to
341 * process the startup event we emitted.
342 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000343 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200344 ret = nih_main_loop ();
345
346 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100347}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100348
349
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000350#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100351/**
Scott James Remnant02977f82011-02-17 15:33:04 -0800352 * logger_kmsg:
353 * @priority: priority of message being logged,
354 * @message: message to log.
355 *
356 * Outputs the @message to the kernel log message socket prefixed with an
357 * appropriate tag based on @priority, the program name and terminated with
358 * a new line.
359 *
360 * Returns: zero on success, negative value on error.
361 **/
362static int
363logger_kmsg (NihLogLevel priority,
364 const char *message)
365{
366 int tag;
367 FILE *kmsg;
368
369 nih_assert (message != NULL);
370
371 switch (priority) {
372 case NIH_LOG_DEBUG:
373 tag = '7';
374 break;
375 case NIH_LOG_INFO:
376 tag = '6';
377 break;
378 case NIH_LOG_MESSAGE:
379 tag = '5';
380 break;
381 case NIH_LOG_WARN:
382 tag = '4';
383 break;
384 case NIH_LOG_ERROR:
385 tag = '3';
386 break;
387 case NIH_LOG_FATAL:
388 tag = '2';
389 break;
390 default:
391 tag = 'd';
392 }
393
394 kmsg = fopen ("/dev/kmsg", "w");
395 if (! kmsg)
396 return -1;
397
398 if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
399 int saved_errno = errno;
400 fclose (kmsg);
401 errno = saved_errno;
402 return -1;
403 }
404
405 if (fclose (kmsg) < 0)
406 return -1;
407
408 return 0;
409}
410
411
412/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000413 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100414 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100415 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000416 * Handle receiving the SEGV or ABRT signal, usually caused by one of
417 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100418 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000419 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100420 * Sadly there's no real alternative to the ensuing kernel panic. Our
421 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
422 * re-exec since the system will be suddenly lobotomised. We definitely
423 * don't want to start a root shell or anything like that. Best thing is
424 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100425 **/
426static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000427crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100428{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100429 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000430
431 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100432
433 pid = fork ();
434 if (pid == 0) {
435 struct sigaction act;
436 struct rlimit limit;
437 sigset_t mask;
438
439 /* Mask out all signals */
440 sigfillset (&mask);
441 sigprocmask (SIG_SETMASK, &mask, NULL);
442
Scott James Remnant16a286f2007-01-10 15:38:33 +0000443 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100444 act.sa_handler = SIG_DFL;
445 act.sa_flags = 0;
446 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000447 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100448
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100449 /* Don't limit the core dump size */
450 limit.rlim_cur = RLIM_INFINITY;
451 limit.rlim_max = RLIM_INFINITY;
452 setrlimit (RLIMIT_CORE, &limit);
453
Scott James Remnantfa733382008-01-15 15:33:27 +0000454 /* Dump in the root directory */
Scott James Remnant9d736bb2009-07-21 18:15:20 +0100455 if (chdir ("/"))
456 nih_warn ("%s: %s", _("Unable to set root directory"),
457 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000458
Scott James Remnant16a286f2007-01-10 15:38:33 +0000459 /* Raise the signal again */
460 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100461
462 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000463 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100464 sigprocmask (SIG_SETMASK, &mask, NULL);
465
466 /* Wait for death */
467 pause ();
468 exit (0);
469 } else if (pid > 0) {
470 /* Wait for the core to be generated */
471 waitpid (pid, NULL, 0);
472
Scott James Remnant31421b72007-03-08 23:53:05 +0000473 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000474 (signum == SIGSEGV
475 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100476 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000477 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000478 (signum == SIGSEGV
479 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100480 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000481
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100482 /* Goodbye, cruel world. */
483 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100484}
485
486/**
487 * cad_handler:
488 * @data: unused,
489 * @signal: signal that called this handler.
490 *
491 * Handle having recieved the SIGINT signal, sent to us when somebody
492 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100493 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100494 **/
495static void
496cad_handler (void *data,
497 NihSignal *signal)
498{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100499 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100500}
501
502/**
503 * kbd_handler:
504 * @data: unused,
505 * @signal: signal that called this handler.
506 *
507 * Handle having recieved the SIGWINCH signal, sent to us when somebody
508 * presses Alt-UpArrow on the console. We just generate a
509 * kbdrequest event.
510 **/
511static void
512kbd_handler (void *data,
513 NihSignal *signal)
514{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100515 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100516}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100517
518/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000519 * pwr_handler:
520 * @data: unused,
521 * @signal: signal that called this handler.
522 *
523 * Handle having recieved the SIGPWR signal, sent to us when powstatd
524 * changes the /etc/powerstatus file. We just generate a
525 * power-status-changed event and jobs read the file.
526 **/
527static void
528pwr_handler (void *data,
529 NihSignal *signal)
530{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100531 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000532}
533
534/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100535 * hup_handler:
536 * @data: unused,
537 * @signal: signal that called this handler.
538 *
539 * Handle having recieved the SIGHUP signal, which we use to instruct us to
540 * reload our configuration.
541 **/
542static void
543hup_handler (void *data,
544 NihSignal *signal)
545{
546 nih_info (_("Reloading configuration"));
547 conf_reload ();
Scott James Remnante7138052010-02-04 00:41:25 -0800548}
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100549
Scott James Remnante7138052010-02-04 00:41:25 -0800550/**
551 * usr1_handler:
552 * @data: unused,
553 * @signal: signal that called this handler.
554 *
555 * Handle having recieved the SIGUSR signal, which we use to instruct us to
556 * reconnect to D-Bus.
557 **/
558static void
559usr1_handler (void *data,
560 NihSignal *signal)
561{
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100562 if (! control_bus) {
563 nih_info (_("Reconnecting to system bus"));
564
565 if (control_bus_open () < 0) {
566 NihError *err;
567
568 err = nih_error_get ();
569 nih_warn ("%s: %s", _("Unable to connect to the system bus"),
570 err->message);
571 nih_free (err);
572 }
573 }
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100574}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100575#endif /* DEBUG */