blob: 5173c44e2754411389362235f66cae374fbbd9e2 [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 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 Remnante7138052010-02-04 00:41:25 -080069static void usr1_handler (void *data, NihSignal *signal);
Scott James Remnantf3ef5112008-06-05 01:26:10 +010070#endif /* DEBUG */
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
89
90/**
91 * options:
92 *
93 * Command-line options we accept.
94 **/
95static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010096 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010097
98 /* Ignore invalid options */
99 { '-', "--", NULL, NULL, NULL, NULL, NULL },
100
101 NIH_OPTION_LAST
102};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100103
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100104
Scott James Remnant50748842006-05-16 21:02:31 +0100105int
106main (int argc,
107 char *argv[])
108{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100109 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000110 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100111
Scott James Remnant06abbec2007-03-09 13:02:38 +0000112 argv0 = argv[0];
113 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200114
Scott James Remnant930e25a2006-10-13 12:28:05 +0100115 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100116 nih_option_set_help (
117 _("This daemon is normally executed by the kernel and given "
118 "process id 1 to denote its special status. When executed "
119 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100120
Scott James Remnant3401ab72006-09-01 02:14:47 +0100121 args = nih_option_parser (NULL, argc, argv, options, FALSE);
122 if (! args)
123 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200124
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000125#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100126 /* Check we're root */
127 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000128 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100129 exit (1);
130 }
131
132 /* Check we're process #1 */
133 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100134 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100135 /* Ignore failure, probably just that telinit doesn't exist */
136
Scott James Remnant31421b72007-03-08 23:53:05 +0000137 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100138 exit (1);
139 }
140
Scott James Remnant78626fb2007-01-10 16:48:10 +0000141 /* Clear our arguments from the command-line, so that we show up in
142 * ps or top output as /sbin/init, with no extra flags.
143 *
144 * This is a very Linux-specific trick; by deleting the NULL
145 * terminator at the end of the last argument, we fool the kernel
146 * into believing we used a setproctitle()-a-like to extend the
147 * argument space into the environment space, and thus make it use
148 * strlen() instead of its own assumed length. In fact, we've done
149 * the exact opposite, and shrunk the command line length to just that
150 * of whatever is in argv[0].
151 *
152 * If we don't do this, and just write \0 over the rest of argv, for
153 * example; the command-line length still includes those \0s, and ps
154 * will show whitespace in their place.
155 */
156 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000157 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000158
Scott James Remnant505f9282007-01-10 18:44:38 +0000159 arg_end = argv[argc-1] + strlen (argv[argc-1]);
160 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000161 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000162
Scott James Remnantfa733382008-01-15 15:33:27 +0000163
164 /* Become the leader of a new session and process group, shedding
165 * any controlling tty (which we shouldn't have had anyway - but
166 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000167 */
168 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100169
Scott James Remnantfa733382008-01-15 15:33:27 +0000170 /* Set the standard file descriptors to the ordinary console device,
171 * resetting it to sane defaults unless we're inheriting from another
172 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200173 */
Scott James Remnantf2f69d02008-04-29 23:38:23 +0100174 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0)
Scott James Remnant5d702952007-01-05 17:21:34 +0000175 nih_free (nih_error_get ());
Scott James Remnantf99ea0c2010-02-04 11:14:30 -0800176 if (system_setup_console (CONSOLE_NONE, FALSE) < 0)
177 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 Remnant9f62a8e2008-01-15 19:22:36 +0000189#else /* DEBUG */
190 nih_log_set_priority (NIH_LOG_DEBUG);
191#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000192
Scott James Remnant2e204b72007-02-03 23:15:28 +0000193
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100194 /* Reset the signal state and install the signal handler for those
195 * signals we actually want to catch; this also sets those that
196 * can be sent to us, because we're special
197 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100198 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000199 nih_signal_reset ();
200
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100201#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000202 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000203 * iteration through the main loop.
204 */
205 nih_signal_set_handler (SIGSEGV, crash_handler);
206 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000207#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000208
Scott James Remnant5c408432007-11-07 21:42:25 -0500209 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
210 * directly; it's enough that they interrupt the main loop and
211 * get dealt with during it.
212 */
213 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
214 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100215
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000216#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100217 /* Ask the kernel to send us SIGINT when control-alt-delete is
218 * pressed; generate an event with the same name.
219 */
220 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500221 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000222 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100223
224 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100225 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100226 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500227 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
228 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000229 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
230 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500231 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100232
Scott James Remnant2c950692007-02-25 09:13:38 +0000233 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500234 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000235 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
236
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100237 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500238 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100239 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnante7138052010-02-04 00:41:25 -0800240
241 /* SIGUSR1 instructs us to reconnect to D-Bus */
242 nih_signal_set_handler (SIGUSR1, nih_signal_handler);
243 NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100244#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100245
Scott James Remnantfa733382008-01-15 15:33:27 +0000246
Scott James Remnant0eade492007-11-15 05:48:07 +0000247 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000248 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100249 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100250
Scott James Remnant5c408432007-11-07 21:42:25 -0500251 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000252 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000253 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100254
Scott James Remnant94d00982006-08-25 15:38:22 +0200255
Scott James Remnant54b2a952007-06-10 22:15:24 +0100256 /* Read configuration */
Scott James Remnant854fda22009-06-23 01:21:55 +0100257 NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
258 NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100259
260 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000261
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100262 /* Create a listening server for private connections. */
263 while (control_server_open () < 0) {
264 NihError *err;
265
266 err = nih_error_get ();
267 if (err->number != ENOMEM) {
268 nih_warn ("%s: %s", _("Unable to listen for private connections"),
269 err->message);
270 nih_free (err);
271 break;
272 }
273 nih_free (err);
274 }
275
Scott James Remnantf8491442008-04-18 13:19:24 +0100276 /* Open connection to the system bus; we normally expect this to
277 * fail and will try again later - don't let ENOMEM stop us though.
278 */
279 while (control_bus_open () < 0) {
280 NihError *err;
281 int number;
282
283 err = nih_error_get ();
284 number = err->number;
285 nih_free (err);
286
287 if (number != ENOMEM)
288 break;
289 }
290
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100291#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000292 /* Now that the startup is complete, send all further logging output
293 * to syslog instead of to the console.
294 */
295 openlog (program_name, LOG_CONS, LOG_DAEMON);
296 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000297#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000298
299
Scott James Remnant3401ab72006-09-01 02:14:47 +0100300 /* Generate and run the startup event or read the state from the
301 * init daemon that exec'd us
302 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100303 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100304 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100305 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100306 sigset_t mask;
307
Scott James Remnant1db88042006-09-01 03:14:19 +0100308 /* We're ok to receive signals again */
309 sigemptyset (&mask);
310 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100311 }
312
Scott James Remnantfa733382008-01-15 15:33:27 +0000313 /* Run through the loop at least once to deal with signals that were
314 * delivered to the previous process while the mask was set or to
315 * process the startup event we emitted.
316 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000317 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200318 ret = nih_main_loop ();
319
320 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100321}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100322
323
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000324#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100325/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000326 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100327 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100328 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000329 * Handle receiving the SEGV or ABRT signal, usually caused by one of
330 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100331 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000332 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100333 * Sadly there's no real alternative to the ensuing kernel panic. Our
334 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
335 * re-exec since the system will be suddenly lobotomised. We definitely
336 * don't want to start a root shell or anything like that. Best thing is
337 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100338 **/
339static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000340crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100341{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100342 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000343
344 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100345
346 pid = fork ();
347 if (pid == 0) {
348 struct sigaction act;
349 struct rlimit limit;
350 sigset_t mask;
351
352 /* Mask out all signals */
353 sigfillset (&mask);
354 sigprocmask (SIG_SETMASK, &mask, NULL);
355
Scott James Remnant16a286f2007-01-10 15:38:33 +0000356 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100357 act.sa_handler = SIG_DFL;
358 act.sa_flags = 0;
359 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000360 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100361
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100362 /* Don't limit the core dump size */
363 limit.rlim_cur = RLIM_INFINITY;
364 limit.rlim_max = RLIM_INFINITY;
365 setrlimit (RLIMIT_CORE, &limit);
366
Scott James Remnantfa733382008-01-15 15:33:27 +0000367 /* Dump in the root directory */
Scott James Remnant9d736bb2009-07-21 18:15:20 +0100368 if (chdir ("/"))
369 nih_warn ("%s: %s", _("Unable to set root directory"),
370 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000371
Scott James Remnant16a286f2007-01-10 15:38:33 +0000372 /* Raise the signal again */
373 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100374
375 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000376 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100377 sigprocmask (SIG_SETMASK, &mask, NULL);
378
379 /* Wait for death */
380 pause ();
381 exit (0);
382 } else if (pid > 0) {
383 /* Wait for the core to be generated */
384 waitpid (pid, NULL, 0);
385
Scott James Remnant31421b72007-03-08 23:53:05 +0000386 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000387 (signum == SIGSEGV
388 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100389 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000390 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000391 (signum == SIGSEGV
392 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100393 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000394
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100395 /* Goodbye, cruel world. */
396 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100397}
398
399/**
400 * cad_handler:
401 * @data: unused,
402 * @signal: signal that called this handler.
403 *
404 * Handle having recieved the SIGINT signal, sent to us when somebody
405 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100406 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100407 **/
408static void
409cad_handler (void *data,
410 NihSignal *signal)
411{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100412 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100413}
414
415/**
416 * kbd_handler:
417 * @data: unused,
418 * @signal: signal that called this handler.
419 *
420 * Handle having recieved the SIGWINCH signal, sent to us when somebody
421 * presses Alt-UpArrow on the console. We just generate a
422 * kbdrequest event.
423 **/
424static void
425kbd_handler (void *data,
426 NihSignal *signal)
427{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100428 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100429}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100430
431/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000432 * pwr_handler:
433 * @data: unused,
434 * @signal: signal that called this handler.
435 *
436 * Handle having recieved the SIGPWR signal, sent to us when powstatd
437 * changes the /etc/powerstatus file. We just generate a
438 * power-status-changed event and jobs read the file.
439 **/
440static void
441pwr_handler (void *data,
442 NihSignal *signal)
443{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100444 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000445}
446
447/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100448 * hup_handler:
449 * @data: unused,
450 * @signal: signal that called this handler.
451 *
452 * Handle having recieved the SIGHUP signal, which we use to instruct us to
453 * reload our configuration.
454 **/
455static void
456hup_handler (void *data,
457 NihSignal *signal)
458{
459 nih_info (_("Reloading configuration"));
460 conf_reload ();
Scott James Remnante7138052010-02-04 00:41:25 -0800461}
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100462
Scott James Remnante7138052010-02-04 00:41:25 -0800463/**
464 * usr1_handler:
465 * @data: unused,
466 * @signal: signal that called this handler.
467 *
468 * Handle having recieved the SIGUSR signal, which we use to instruct us to
469 * reconnect to D-Bus.
470 **/
471static void
472usr1_handler (void *data,
473 NihSignal *signal)
474{
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100475 if (! control_bus) {
476 nih_info (_("Reconnecting to system bus"));
477
478 if (control_bus_open () < 0) {
479 NihError *err;
480
481 err = nih_error_get ();
482 nih_warn ("%s: %s", _("Unable to connect to the system bus"),
483 err->message);
484 nih_free (err);
485 }
486 }
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100487}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100488#endif /* DEBUG */