blob: 6d0b512c8cb10adc8d2192e612b0dfa03d9c2e09 [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnant75123022009-05-22 13:27:56 +02003 * Copyright © 2009 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 Remnant75123022009-05-22 13:27:56 +02006 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3, as
8 * 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 *
15 * You should have received a copy of the GNU General Public License
Scott James Remnant75123022009-05-22 13:27:56 +020016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Scott James Remnant50748842006-05-16 21:02:31 +010017 */
Scott James Remnant75123022009-05-22 13:27:56 +020018 *
Scott James Remnant50748842006-05-16 21:02:31 +010019
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 Remnantf3ef5112008-06-05 01:26:10 +010069#endif /* DEBUG */
Scott James Remnant3401ab72006-09-01 02:14:47 +010070
Scott James Remnant1db88042006-09-01 03:14:19 +010071
Scott James Remnant3401ab72006-09-01 02:14:47 +010072/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000073 * argv0:
74 *
75 * Path to program executed, used for re-executing the init binary from the
76 * same location we were executed from.
77 **/
78static const char *argv0 = NULL;
79
80/**
Scott James Remnant1db88042006-09-01 03:14:19 +010081 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010082 *
Scott James Remnant8b227402006-10-11 17:55:27 +010083 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010084 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010085 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010086static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010087
88
89/**
90 * options:
91 *
92 * Command-line options we accept.
93 **/
94static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010095 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010096
97 /* Ignore invalid options */
98 { '-', "--", NULL, NULL, NULL, NULL, NULL },
99
100 NIH_OPTION_LAST
101};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100102
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100103
Scott James Remnant50748842006-05-16 21:02:31 +0100104int
105main (int argc,
106 char *argv[])
107{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100108 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000109 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100110
Scott James Remnant06abbec2007-03-09 13:02:38 +0000111 argv0 = argv[0];
112 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200113
Scott James Remnant930e25a2006-10-13 12:28:05 +0100114 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100115 nih_option_set_help (
116 _("This daemon is normally executed by the kernel and given "
117 "process id 1 to denote its special status. When executed "
118 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100119
Scott James Remnant3401ab72006-09-01 02:14:47 +0100120 args = nih_option_parser (NULL, argc, argv, options, FALSE);
121 if (! args)
122 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200123
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000124#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100125 /* Check we're root */
126 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000127 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100128 exit (1);
129 }
130
131 /* Check we're process #1 */
132 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100133 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100134 /* Ignore failure, probably just that telinit doesn't exist */
135
Scott James Remnant31421b72007-03-08 23:53:05 +0000136 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100137 exit (1);
138 }
139
Scott James Remnant78626fb2007-01-10 16:48:10 +0000140 /* Clear our arguments from the command-line, so that we show up in
141 * ps or top output as /sbin/init, with no extra flags.
142 *
143 * This is a very Linux-specific trick; by deleting the NULL
144 * terminator at the end of the last argument, we fool the kernel
145 * into believing we used a setproctitle()-a-like to extend the
146 * argument space into the environment space, and thus make it use
147 * strlen() instead of its own assumed length. In fact, we've done
148 * the exact opposite, and shrunk the command line length to just that
149 * of whatever is in argv[0].
150 *
151 * If we don't do this, and just write \0 over the rest of argv, for
152 * example; the command-line length still includes those \0s, and ps
153 * will show whitespace in their place.
154 */
155 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000156 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000157
Scott James Remnant505f9282007-01-10 18:44:38 +0000158 arg_end = argv[argc-1] + strlen (argv[argc-1]);
159 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000160 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000161
Scott James Remnantfa733382008-01-15 15:33:27 +0000162
163 /* Become the leader of a new session and process group, shedding
164 * any controlling tty (which we shouldn't have had anyway - but
165 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000166 */
167 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100168
Scott James Remnantfa733382008-01-15 15:33:27 +0000169 /* Set the standard file descriptors to the ordinary console device,
170 * resetting it to sane defaults unless we're inheriting from another
171 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200172 */
Scott James Remnantf2f69d02008-04-29 23:38:23 +0100173 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0)
Scott James Remnant5d702952007-01-05 17:21:34 +0000174 nih_free (nih_error_get ());
Scott James Remnant77e8db32006-08-21 08:47:50 +0200175
Scott James Remnant2e204b72007-02-03 23:15:28 +0000176 /* Set the PATH environment variable */
177 setenv ("PATH", PATH, TRUE);
178
Scott James Remnantfa733382008-01-15 15:33:27 +0000179 /* Switch to the root directory in case we were started from some
180 * strange place, or worse, some directory in the initramfs that's
181 * going to go away soon.
182 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100183 if (chdir ("/"))
184 nih_warn ("%s: %s", _("Unable to set root directory"),
185 strerror (errno));
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000186#else /* DEBUG */
187 nih_log_set_priority (NIH_LOG_DEBUG);
188#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000189
Scott James Remnant2e204b72007-02-03 23:15:28 +0000190
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100191 /* Reset the signal state and install the signal handler for those
192 * signals we actually want to catch; this also sets those that
193 * can be sent to us, because we're special
194 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100195 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000196 nih_signal_reset ();
197
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100198#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000199 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000200 * iteration through the main loop.
201 */
202 nih_signal_set_handler (SIGSEGV, crash_handler);
203 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000204#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000205
Scott James Remnant5c408432007-11-07 21:42:25 -0500206 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
207 * directly; it's enough that they interrupt the main loop and
208 * get dealt with during it.
209 */
210 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
211 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100212
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000213#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100214 /* Ask the kernel to send us SIGINT when control-alt-delete is
215 * pressed; generate an event with the same name.
216 */
217 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500218 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000219 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100220
221 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100222 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100223 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500224 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
225 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000226 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
227 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500228 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100229
Scott James Remnant2c950692007-02-25 09:13:38 +0000230 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500231 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000232 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
233
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100234 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500235 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100236 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100237#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100238
Scott James Remnantfa733382008-01-15 15:33:27 +0000239
Scott James Remnant0eade492007-11-15 05:48:07 +0000240 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000241 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100242 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100243
Scott James Remnant5c408432007-11-07 21:42:25 -0500244 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000245 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000246 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100247
Scott James Remnant94d00982006-08-25 15:38:22 +0200248
Scott James Remnant54b2a952007-06-10 22:15:24 +0100249 /* Read configuration */
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400250 NIH_MUST (conf_source_new (NULL, CONFDIR "/init.conf", CONF_FILE));
251 NIH_MUST (conf_source_new (NULL, CONFDIR "/conf.d", CONF_DIR));
252 NIH_MUST (conf_source_new (NULL, CONFDIR "/jobs.d", CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100253
254 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000255
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100256 /* Create a listening server for private connections. */
257 while (control_server_open () < 0) {
258 NihError *err;
259
260 err = nih_error_get ();
261 if (err->number != ENOMEM) {
262 nih_warn ("%s: %s", _("Unable to listen for private connections"),
263 err->message);
264 nih_free (err);
265 break;
266 }
267 nih_free (err);
268 }
269
Scott James Remnantf8491442008-04-18 13:19:24 +0100270 /* Open connection to the system bus; we normally expect this to
271 * fail and will try again later - don't let ENOMEM stop us though.
272 */
273 while (control_bus_open () < 0) {
274 NihError *err;
275 int number;
276
277 err = nih_error_get ();
278 number = err->number;
279 nih_free (err);
280
281 if (number != ENOMEM)
282 break;
283 }
284
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100285#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000286 /* Now that the startup is complete, send all further logging output
287 * to syslog instead of to the console.
288 */
289 openlog (program_name, LOG_CONS, LOG_DAEMON);
290 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000291#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000292
293
Scott James Remnant3401ab72006-09-01 02:14:47 +0100294 /* Generate and run the startup event or read the state from the
295 * init daemon that exec'd us
296 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100297 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100298 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100299 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100300 sigset_t mask;
301
Scott James Remnant1db88042006-09-01 03:14:19 +0100302 /* We're ok to receive signals again */
303 sigemptyset (&mask);
304 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100305 }
306
Scott James Remnantfa733382008-01-15 15:33:27 +0000307 /* Run through the loop at least once to deal with signals that were
308 * delivered to the previous process while the mask was set or to
309 * process the startup event we emitted.
310 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000311 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200312 ret = nih_main_loop ();
313
314 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100315}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100316
317
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000318#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100319/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000320 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100321 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100322 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000323 * Handle receiving the SEGV or ABRT signal, usually caused by one of
324 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100325 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000326 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100327 * Sadly there's no real alternative to the ensuing kernel panic. Our
328 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
329 * re-exec since the system will be suddenly lobotomised. We definitely
330 * don't want to start a root shell or anything like that. Best thing is
331 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100332 **/
333static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000334crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100335{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100336 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000337
338 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100339
340 pid = fork ();
341 if (pid == 0) {
342 struct sigaction act;
343 struct rlimit limit;
344 sigset_t mask;
345
346 /* Mask out all signals */
347 sigfillset (&mask);
348 sigprocmask (SIG_SETMASK, &mask, NULL);
349
Scott James Remnant16a286f2007-01-10 15:38:33 +0000350 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100351 act.sa_handler = SIG_DFL;
352 act.sa_flags = 0;
353 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000354 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100355
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100356 /* Don't limit the core dump size */
357 limit.rlim_cur = RLIM_INFINITY;
358 limit.rlim_max = RLIM_INFINITY;
359 setrlimit (RLIMIT_CORE, &limit);
360
Scott James Remnantfa733382008-01-15 15:33:27 +0000361 /* Dump in the root directory */
Scott James Remnant5635e072008-05-06 23:22:58 +0100362 nih_warn ("%s: %s", _("Unable to set root directory"),
363 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000364
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
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100388 /* Goodbye, cruel world. */
389 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100390}
391
392/**
393 * cad_handler:
394 * @data: unused,
395 * @signal: signal that called this handler.
396 *
397 * Handle having recieved the SIGINT signal, sent to us when somebody
398 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100399 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100400 **/
401static void
402cad_handler (void *data,
403 NihSignal *signal)
404{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100405 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100406}
407
408/**
409 * kbd_handler:
410 * @data: unused,
411 * @signal: signal that called this handler.
412 *
413 * Handle having recieved the SIGWINCH signal, sent to us when somebody
414 * presses Alt-UpArrow on the console. We just generate a
415 * kbdrequest event.
416 **/
417static void
418kbd_handler (void *data,
419 NihSignal *signal)
420{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100421 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100422}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100423
424/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000425 * pwr_handler:
426 * @data: unused,
427 * @signal: signal that called this handler.
428 *
429 * Handle having recieved the SIGPWR signal, sent to us when powstatd
430 * changes the /etc/powerstatus file. We just generate a
431 * power-status-changed event and jobs read the file.
432 **/
433static void
434pwr_handler (void *data,
435 NihSignal *signal)
436{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100437 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000438}
439
440/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100441 * hup_handler:
442 * @data: unused,
443 * @signal: signal that called this handler.
444 *
445 * Handle having recieved the SIGHUP signal, which we use to instruct us to
446 * reload our configuration.
447 **/
448static void
449hup_handler (void *data,
450 NihSignal *signal)
451{
452 nih_info (_("Reloading configuration"));
453 conf_reload ();
454}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100455#endif /* DEBUG */