blob: 938d5f347f599449df1841cd3bb4ae47641906ea [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnant404f8eb2008-01-16 01:47:25 +00003 * Copyright © 2008 Canonical Ltd.
Scott James Remnant6b1c7ee2008-02-17 18:00:33 +00004 * Author: Scott James Remnant <scott@netsplit.com>.
Scott James Remnant50748842006-05-16 21:02:31 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif /* HAVE_CONFIG_H */
24
25
Scott James Remnantf43bdf32006-08-27 18:20:29 +010026#include <sys/types.h>
Scott James Remnantea806b72006-10-18 15:01:00 +010027#include <sys/time.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010028#include <sys/wait.h>
29#include <sys/ioctl.h>
30#include <sys/reboot.h>
31#include <sys/resource.h>
32
Scott James Remnant3401ab72006-09-01 02:14:47 +010033#include <errno.h>
34#include <stdio.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010035#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020036#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010037#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020038#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020039#include <unistd.h>
40
Scott James Remnant097b2a92006-09-01 19:30:37 +010041#include <linux/kd.h>
42
Scott James Remnant77e8db32006-08-21 08:47:50 +020043#include <nih/macros.h>
44#include <nih/alloc.h>
45#include <nih/list.h>
46#include <nih/timer.h>
47#include <nih/signal.h>
48#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010049#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020050#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010051#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020052#include <nih/logging.h>
53
54#include "process.h"
55#include "job.h"
56#include "event.h"
Scott James Remnant54b2a952007-06-10 22:15:24 +010057#include "conf.h"
Scott James Remnante0d0dd12006-09-14 10:51:05 +010058#include "paths.h"
Scott James Remnant50748842006-05-16 21:02:31 +010059
60
Scott James Remnantf43bdf32006-08-27 18:20:29 +010061/* Prototypes for static functions */
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000062#ifndef DEBUG
Scott James Remnant16a286f2007-01-10 15:38:33 +000063static void crash_handler (int signum);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000064#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +000065static void term_handler (void *data, NihSignal *signal);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000066#ifndef DEBUG
Scott James Remnant3401ab72006-09-01 02:14:47 +010067static void cad_handler (void *data, NihSignal *signal);
68static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000069static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000070#endif /* DEBUG */
Scott James Remnant7ba2cf62007-06-10 22:20:38 +010071static void hup_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010072static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010073
Scott James Remnant1db88042006-09-01 03:14:19 +010074
Scott James Remnant3401ab72006-09-01 02:14:47 +010075/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000076 * argv0:
77 *
78 * Path to program executed, used for re-executing the init binary from the
79 * same location we were executed from.
80 **/
81static const char *argv0 = NULL;
82
83/**
Scott James Remnant1db88042006-09-01 03:14:19 +010084 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010085 *
Scott James Remnant8b227402006-10-11 17:55:27 +010086 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010087 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010088 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010089static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010090
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 Remnant4b56d6f2008-04-12 12:23:15 +0100176 if (process_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 */
186 chdir ("/");
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000187#else /* DEBUG */
188 nih_log_set_priority (NIH_LOG_DEBUG);
189#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000190
Scott James Remnant2e204b72007-02-03 23:15:28 +0000191
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100192 /* Reset the signal state and install the signal handler for those
193 * signals we actually want to catch; this also sets those that
194 * can be sent to us, because we're special
195 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100196 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000197 nih_signal_reset ();
198
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100199#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000200 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000201 * iteration through the main loop.
202 */
203 nih_signal_set_handler (SIGSEGV, crash_handler);
204 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000205#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000206
Scott James Remnant5c408432007-11-07 21:42:25 -0500207 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
208 * directly; it's enough that they interrupt the main loop and
209 * get dealt with during it.
210 */
211 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
212 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100213
Scott James Remnant5c408432007-11-07 21:42:25 -0500214 /* Allow SIGTSTP and SIGCONT to pause and unpause event processing */
215 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000216 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500217
218 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000219 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100220
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000221#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100222 /* Ask the kernel to send us SIGINT when control-alt-delete is
223 * pressed; generate an event with the same name.
224 */
225 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500226 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000227 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100228
229 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100230 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100231 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500232 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
233 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000234 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
235 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500236 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100237
Scott James Remnant2c950692007-02-25 09:13:38 +0000238 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500239 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000240 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000241#endif /* DEBUG */
Scott James Remnant2c950692007-02-25 09:13:38 +0000242
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100243 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500244 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100245 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
246
Scott James Remnantfa733382008-01-15 15:33:27 +0000247 /* SIGTERM instructs us to re-exec ourselves; this should be the
248 * last in the list to ensure that all other signals are handled
249 * before a SIGTERM.
250 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500251 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100252 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, term_handler, NULL));
Scott James Remnant1db88042006-09-01 03:14:19 +0100253
Scott James Remnantfa733382008-01-15 15:33:27 +0000254
Scott James Remnant0eade492007-11-15 05:48:07 +0000255 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000256 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
257 job_child_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100258
Scott James Remnant5c408432007-11-07 21:42:25 -0500259 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000260 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000261 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100262
Scott James Remnant94d00982006-08-25 15:38:22 +0200263
Scott James Remnant54b2a952007-06-10 22:15:24 +0100264 /* Read configuration */
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400265 NIH_MUST (conf_source_new (NULL, CONFDIR "/init.conf", CONF_FILE));
266 NIH_MUST (conf_source_new (NULL, CONFDIR "/conf.d", CONF_DIR));
267 NIH_MUST (conf_source_new (NULL, CONFDIR "/jobs.d", CONF_JOB_DIR));
Scott James Remnantdd9249d2007-06-12 15:30:19 +0100268#ifdef LEGACY_CONFDIR
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400269 NIH_MUST (conf_source_new (NULL, LEGACY_CONFDIR, CONF_JOB_DIR));
Scott James Remnantdd9249d2007-06-12 15:30:19 +0100270#endif /* LEGACY_CONFDIR */
Scott James Remnant54b2a952007-06-10 22:15:24 +0100271
272 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000273
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100274#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000275 /* Now that the startup is complete, send all further logging output
276 * to syslog instead of to the console.
277 */
278 openlog (program_name, LOG_CONS, LOG_DAEMON);
279 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000280#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000281
282
Scott James Remnant3401ab72006-09-01 02:14:47 +0100283 /* Generate and run the startup event or read the state from the
284 * init daemon that exec'd us
285 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100286 if (! restart) {
Scott James Remnant36eea462008-02-22 15:11:05 +0000287 event_new (NULL, STARTUP_EVENT, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100288 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100289 sigset_t mask;
290
Scott James Remnant1db88042006-09-01 03:14:19 +0100291 /* We're ok to receive signals again */
292 sigemptyset (&mask);
293 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100294 }
295
Scott James Remnantfa733382008-01-15 15:33:27 +0000296 /* Run through the loop at least once to deal with signals that were
297 * delivered to the previous process while the mask was set or to
298 * process the startup event we emitted.
299 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000300 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200301 ret = nih_main_loop ();
302
303 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100304}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100305
306
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000307#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100308/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000309 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100310 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100311 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000312 * Handle receiving the SEGV or ABRT signal, usually caused by one of
313 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100314 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000315 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100316 * Sadly there's no real alternative to the ensuing kernel panic. Our
317 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
318 * re-exec since the system will be suddenly lobotomised. We definitely
319 * don't want to start a root shell or anything like that. Best thing is
320 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100321 **/
322static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000323crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100324{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100325 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000326
327 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100328
329 pid = fork ();
330 if (pid == 0) {
331 struct sigaction act;
332 struct rlimit limit;
333 sigset_t mask;
334
335 /* Mask out all signals */
336 sigfillset (&mask);
337 sigprocmask (SIG_SETMASK, &mask, NULL);
338
Scott James Remnant16a286f2007-01-10 15:38:33 +0000339 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100340 act.sa_handler = SIG_DFL;
341 act.sa_flags = 0;
342 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000343 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100344
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100345 /* Don't limit the core dump size */
346 limit.rlim_cur = RLIM_INFINITY;
347 limit.rlim_max = RLIM_INFINITY;
348 setrlimit (RLIMIT_CORE, &limit);
349
Scott James Remnantfa733382008-01-15 15:33:27 +0000350 /* Dump in the root directory */
351 chdir ("/");
352
Scott James Remnant16a286f2007-01-10 15:38:33 +0000353 /* Raise the signal again */
354 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100355
356 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000357 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100358 sigprocmask (SIG_SETMASK, &mask, NULL);
359
360 /* Wait for death */
361 pause ();
362 exit (0);
363 } else if (pid > 0) {
364 /* Wait for the core to be generated */
365 waitpid (pid, NULL, 0);
366
Scott James Remnant31421b72007-03-08 23:53:05 +0000367 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000368 (signum == SIGSEGV
369 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100370 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000371 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000372 (signum == SIGSEGV
373 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100374 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000375
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100376 /* Goodbye, cruel world. */
377 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100378}
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000379#endif
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100380
381/**
Scott James Remnantfa733382008-01-15 15:33:27 +0000382 * term_handler:
383 * @data: unused,
384 * @signal: signal caught.
385 *
386 * This is called when we receive the TERM signal, which instructs us
387 * to reexec ourselves.
388 **/
389static void
390term_handler (void *data,
391 NihSignal *signal)
392{
393 NihError *err;
394 const char *loglevel;
395 sigset_t mask, oldmask;
396
397 nih_assert (argv0 != NULL);
398 nih_assert (signal != NULL);
399
400 nih_warn (_("Re-executing %s"), argv0);
401
402 /* Block signals while we work. We're the last signal handler
403 * installed so this should mean that they're all handled now.
404 *
405 * The child must make sure that it unblocks these again when
406 * it's ready.
407 */
408 sigfillset (&mask);
409 sigprocmask (SIG_BLOCK, &mask, &oldmask);
410
411 /* Argument list */
412 if (nih_log_priority <= NIH_LOG_DEBUG) {
413 loglevel = "--debug";
414 } else if (nih_log_priority <= NIH_LOG_INFO) {
415 loglevel = "--verbose";
416 } else if (nih_log_priority >= NIH_LOG_ERROR) {
417 loglevel = "--error";
418 } else {
419 loglevel = NULL;
420 }
421 execl (argv0, argv0, "--restart", loglevel, NULL);
422 nih_error_raise_system ();
423
424 err = nih_error_get ();
425 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
426 nih_free (err);
427
428 sigprocmask (SIG_SETMASK, &oldmask, NULL);
429}
430
431
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000432#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000433/**
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100434 * cad_handler:
435 * @data: unused,
436 * @signal: signal that called this handler.
437 *
438 * Handle having recieved the SIGINT signal, sent to us when somebody
439 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100440 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100441 **/
442static void
443cad_handler (void *data,
444 NihSignal *signal)
445{
Scott James Remnant36eea462008-02-22 15:11:05 +0000446 event_new (NULL, CTRLALTDEL_EVENT, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100447}
448
449/**
450 * kbd_handler:
451 * @data: unused,
452 * @signal: signal that called this handler.
453 *
454 * Handle having recieved the SIGWINCH signal, sent to us when somebody
455 * presses Alt-UpArrow on the console. We just generate a
456 * kbdrequest event.
457 **/
458static void
459kbd_handler (void *data,
460 NihSignal *signal)
461{
Scott James Remnant36eea462008-02-22 15:11:05 +0000462 event_new (NULL, KBDREQUEST_EVENT, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100463}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100464
465/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000466 * pwr_handler:
467 * @data: unused,
468 * @signal: signal that called this handler.
469 *
470 * Handle having recieved the SIGPWR signal, sent to us when powstatd
471 * changes the /etc/powerstatus file. We just generate a
472 * power-status-changed event and jobs read the file.
473 **/
474static void
475pwr_handler (void *data,
476 NihSignal *signal)
477{
Scott James Remnant36eea462008-02-22 15:11:05 +0000478 event_new (NULL, PWRSTATUS_EVENT, NULL);
Scott James Remnant2c950692007-02-25 09:13:38 +0000479}
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000480#endif
Scott James Remnant2c950692007-02-25 09:13:38 +0000481
482/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100483 * hup_handler:
484 * @data: unused,
485 * @signal: signal that called this handler.
486 *
487 * Handle having recieved the SIGHUP signal, which we use to instruct us to
488 * reload our configuration.
489 **/
490static void
491hup_handler (void *data,
492 NihSignal *signal)
493{
494 nih_info (_("Reloading configuration"));
495 conf_reload ();
496}
497
498/**
Scott James Remnanteabb7802006-08-31 15:39:04 +0100499 * stop_handler:
500 * @data: unused,
501 * @signal: signal caught.
502 *
503 * This is called when we receive the STOP, TSTP or CONT signals; we
504 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000505 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100506 **/
507static void
508stop_handler (void *data,
509 NihSignal *signal)
510{
511 nih_assert (signal != NULL);
512
513 if (signal->signum == SIGCONT) {
514 nih_info (_("Event queue resumed"));
515 paused = FALSE;
516 } else {
517 nih_info (_("Event queue paused"));
518 paused = TRUE;
519 }
520}