blob: df036c4dd1cc9fc9c9d11ae606792997933a6e00 [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
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 Remnant16a286f2007-01-10 15:38:33 +000065static void crash_handler (int signum);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000066#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +000067static void term_handler (void *data, NihSignal *signal);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000068#ifndef DEBUG
Scott James Remnant3401ab72006-09-01 02:14:47 +010069static void cad_handler (void *data, NihSignal *signal);
70static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000071static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000072#endif /* DEBUG */
Scott James Remnant7ba2cf62007-06-10 22:20:38 +010073static void hup_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010074static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010075
Scott James Remnant1db88042006-09-01 03:14:19 +010076
Scott James Remnant3401ab72006-09-01 02:14:47 +010077/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000078 * argv0:
79 *
80 * Path to program executed, used for re-executing the init binary from the
81 * same location we were executed from.
82 **/
83static const char *argv0 = NULL;
84
85/**
Scott James Remnant1db88042006-09-01 03:14:19 +010086 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010087 *
Scott James Remnant8b227402006-10-11 17:55:27 +010088 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010089 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010090 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010091static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010092
93
94/**
95 * options:
96 *
97 * Command-line options we accept.
98 **/
99static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100100 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100101
102 /* Ignore invalid options */
103 { '-', "--", NULL, NULL, NULL, NULL, NULL },
104
105 NIH_OPTION_LAST
106};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100107
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100108
Scott James Remnant50748842006-05-16 21:02:31 +0100109int
110main (int argc,
111 char *argv[])
112{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100113 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000114 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100115
Scott James Remnant06abbec2007-03-09 13:02:38 +0000116 argv0 = argv[0];
117 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200118
Scott James Remnant930e25a2006-10-13 12:28:05 +0100119 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100120 nih_option_set_help (
121 _("This daemon is normally executed by the kernel and given "
122 "process id 1 to denote its special status. When executed "
123 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100124
Scott James Remnant3401ab72006-09-01 02:14:47 +0100125 args = nih_option_parser (NULL, argc, argv, options, FALSE);
126 if (! args)
127 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200128
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000129#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100130 /* Check we're root */
131 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000132 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100133 exit (1);
134 }
135
136 /* Check we're process #1 */
137 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100138 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100139 /* Ignore failure, probably just that telinit doesn't exist */
140
Scott James Remnant31421b72007-03-08 23:53:05 +0000141 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100142 exit (1);
143 }
144
Scott James Remnant78626fb2007-01-10 16:48:10 +0000145 /* Clear our arguments from the command-line, so that we show up in
146 * ps or top output as /sbin/init, with no extra flags.
147 *
148 * This is a very Linux-specific trick; by deleting the NULL
149 * terminator at the end of the last argument, we fool the kernel
150 * into believing we used a setproctitle()-a-like to extend the
151 * argument space into the environment space, and thus make it use
152 * strlen() instead of its own assumed length. In fact, we've done
153 * the exact opposite, and shrunk the command line length to just that
154 * of whatever is in argv[0].
155 *
156 * If we don't do this, and just write \0 over the rest of argv, for
157 * example; the command-line length still includes those \0s, and ps
158 * will show whitespace in their place.
159 */
160 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000161 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000162
Scott James Remnant505f9282007-01-10 18:44:38 +0000163 arg_end = argv[argc-1] + strlen (argv[argc-1]);
164 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000165 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000166
Scott James Remnantfa733382008-01-15 15:33:27 +0000167
168 /* Become the leader of a new session and process group, shedding
169 * any controlling tty (which we shouldn't have had anyway - but
170 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000171 */
172 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100173
Scott James Remnantfa733382008-01-15 15:33:27 +0000174 /* Set the standard file descriptors to the ordinary console device,
175 * resetting it to sane defaults unless we're inheriting from another
176 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200177 */
Scott James Remnantf2f69d02008-04-29 23:38:23 +0100178 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0)
Scott James Remnant5d702952007-01-05 17:21:34 +0000179 nih_free (nih_error_get ());
Scott James Remnant77e8db32006-08-21 08:47:50 +0200180
Scott James Remnant2e204b72007-02-03 23:15:28 +0000181 /* Set the PATH environment variable */
182 setenv ("PATH", PATH, TRUE);
183
Scott James Remnantfa733382008-01-15 15:33:27 +0000184 /* Switch to the root directory in case we were started from some
185 * strange place, or worse, some directory in the initramfs that's
186 * going to go away soon.
187 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100188 if (chdir ("/"))
189 nih_warn ("%s: %s", _("Unable to set root directory"),
190 strerror (errno));
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000191#else /* DEBUG */
192 nih_log_set_priority (NIH_LOG_DEBUG);
193#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000194
Scott James Remnant2e204b72007-02-03 23:15:28 +0000195
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100196 /* Reset the signal state and install the signal handler for those
197 * signals we actually want to catch; this also sets those that
198 * can be sent to us, because we're special
199 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100200 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000201 nih_signal_reset ();
202
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100203#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000204 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000205 * iteration through the main loop.
206 */
207 nih_signal_set_handler (SIGSEGV, crash_handler);
208 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000209#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000210
Scott James Remnant5c408432007-11-07 21:42:25 -0500211 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
212 * directly; it's enough that they interrupt the main loop and
213 * get dealt with during it.
214 */
215 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
216 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100217
Scott James Remnant5c408432007-11-07 21:42:25 -0500218 /* Allow SIGTSTP and SIGCONT to pause and unpause event processing */
219 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000220 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500221
222 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000223 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100224
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000225#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100226 /* Ask the kernel to send us SIGINT when control-alt-delete is
227 * pressed; generate an event with the same name.
228 */
229 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500230 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000231 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100232
233 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100234 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100235 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500236 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
237 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000238 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
239 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500240 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100241
Scott James Remnant2c950692007-02-25 09:13:38 +0000242 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500243 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000244 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000245#endif /* DEBUG */
Scott James Remnant2c950692007-02-25 09:13:38 +0000246
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100247 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500248 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100249 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
250
Scott James Remnantfa733382008-01-15 15:33:27 +0000251 /* SIGTERM instructs us to re-exec ourselves; this should be the
252 * last in the list to ensure that all other signals are handled
253 * before a SIGTERM.
254 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500255 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100256 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, term_handler, NULL));
Scott James Remnant1db88042006-09-01 03:14:19 +0100257
Scott James Remnantfa733382008-01-15 15:33:27 +0000258
Scott James Remnant0eade492007-11-15 05:48:07 +0000259 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000260 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100261 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100262
Scott James Remnant5c408432007-11-07 21:42:25 -0500263 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000264 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000265 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100266
Scott James Remnant94d00982006-08-25 15:38:22 +0200267
Scott James Remnant54b2a952007-06-10 22:15:24 +0100268 /* Read configuration */
Scott James Remnant9fa1ce82007-11-03 02:06:54 -0400269 NIH_MUST (conf_source_new (NULL, CONFDIR "/init.conf", CONF_FILE));
270 NIH_MUST (conf_source_new (NULL, CONFDIR "/conf.d", CONF_DIR));
271 NIH_MUST (conf_source_new (NULL, CONFDIR "/jobs.d", CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100272
273 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000274
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100275 /* Create a listening server for private connections. */
276 while (control_server_open () < 0) {
277 NihError *err;
278
279 err = nih_error_get ();
280 if (err->number != ENOMEM) {
281 nih_warn ("%s: %s", _("Unable to listen for private connections"),
282 err->message);
283 nih_free (err);
284 break;
285 }
286 nih_free (err);
287 }
288
Scott James Remnantf8491442008-04-18 13:19:24 +0100289 /* Open connection to the system bus; we normally expect this to
290 * fail and will try again later - don't let ENOMEM stop us though.
291 */
292 while (control_bus_open () < 0) {
293 NihError *err;
294 int number;
295
296 err = nih_error_get ();
297 number = err->number;
298 nih_free (err);
299
300 if (number != ENOMEM)
301 break;
302 }
303
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100304#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000305 /* Now that the startup is complete, send all further logging output
306 * to syslog instead of to the console.
307 */
308 openlog (program_name, LOG_CONS, LOG_DAEMON);
309 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000310#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000311
312
Scott James Remnant3401ab72006-09-01 02:14:47 +0100313 /* Generate and run the startup event or read the state from the
314 * init daemon that exec'd us
315 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100316 if (! restart) {
Scott James Remnant36eea462008-02-22 15:11:05 +0000317 event_new (NULL, STARTUP_EVENT, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100318 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100319 sigset_t mask;
320
Scott James Remnant1db88042006-09-01 03:14:19 +0100321 /* We're ok to receive signals again */
322 sigemptyset (&mask);
323 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100324 }
325
Scott James Remnantfa733382008-01-15 15:33:27 +0000326 /* Run through the loop at least once to deal with signals that were
327 * delivered to the previous process while the mask was set or to
328 * process the startup event we emitted.
329 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000330 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200331 ret = nih_main_loop ();
332
333 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100334}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100335
336
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000337#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100338/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000339 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100340 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100341 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000342 * Handle receiving the SEGV or ABRT signal, usually caused by one of
343 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100344 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000345 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100346 * Sadly there's no real alternative to the ensuing kernel panic. Our
347 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
348 * re-exec since the system will be suddenly lobotomised. We definitely
349 * don't want to start a root shell or anything like that. Best thing is
350 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100351 **/
352static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000353crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100354{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100355 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000356
357 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100358
359 pid = fork ();
360 if (pid == 0) {
361 struct sigaction act;
362 struct rlimit limit;
363 sigset_t mask;
364
365 /* Mask out all signals */
366 sigfillset (&mask);
367 sigprocmask (SIG_SETMASK, &mask, NULL);
368
Scott James Remnant16a286f2007-01-10 15:38:33 +0000369 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100370 act.sa_handler = SIG_DFL;
371 act.sa_flags = 0;
372 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000373 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100374
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100375 /* Don't limit the core dump size */
376 limit.rlim_cur = RLIM_INFINITY;
377 limit.rlim_max = RLIM_INFINITY;
378 setrlimit (RLIMIT_CORE, &limit);
379
Scott James Remnantfa733382008-01-15 15:33:27 +0000380 /* Dump in the root directory */
Scott James Remnant5635e072008-05-06 23:22:58 +0100381 nih_warn ("%s: %s", _("Unable to set root directory"),
382 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000383
Scott James Remnant16a286f2007-01-10 15:38:33 +0000384 /* Raise the signal again */
385 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100386
387 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000388 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100389 sigprocmask (SIG_SETMASK, &mask, NULL);
390
391 /* Wait for death */
392 pause ();
393 exit (0);
394 } else if (pid > 0) {
395 /* Wait for the core to be generated */
396 waitpid (pid, NULL, 0);
397
Scott James Remnant31421b72007-03-08 23:53:05 +0000398 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000399 (signum == SIGSEGV
400 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100401 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000402 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000403 (signum == SIGSEGV
404 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100405 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000406
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100407 /* Goodbye, cruel world. */
408 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100409}
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000410#endif
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100411
412/**
Scott James Remnantfa733382008-01-15 15:33:27 +0000413 * term_handler:
414 * @data: unused,
415 * @signal: signal caught.
416 *
417 * This is called when we receive the TERM signal, which instructs us
418 * to reexec ourselves.
419 **/
420static void
421term_handler (void *data,
422 NihSignal *signal)
423{
424 NihError *err;
425 const char *loglevel;
426 sigset_t mask, oldmask;
427
428 nih_assert (argv0 != NULL);
429 nih_assert (signal != NULL);
430
431 nih_warn (_("Re-executing %s"), argv0);
432
433 /* Block signals while we work. We're the last signal handler
434 * installed so this should mean that they're all handled now.
435 *
436 * The child must make sure that it unblocks these again when
437 * it's ready.
438 */
439 sigfillset (&mask);
440 sigprocmask (SIG_BLOCK, &mask, &oldmask);
441
442 /* Argument list */
443 if (nih_log_priority <= NIH_LOG_DEBUG) {
444 loglevel = "--debug";
445 } else if (nih_log_priority <= NIH_LOG_INFO) {
446 loglevel = "--verbose";
447 } else if (nih_log_priority >= NIH_LOG_ERROR) {
448 loglevel = "--error";
449 } else {
450 loglevel = NULL;
451 }
452 execl (argv0, argv0, "--restart", loglevel, NULL);
453 nih_error_raise_system ();
454
455 err = nih_error_get ();
456 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
457 nih_free (err);
458
459 sigprocmask (SIG_SETMASK, &oldmask, NULL);
460}
461
462
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000463#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000464/**
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100465 * cad_handler:
466 * @data: unused,
467 * @signal: signal that called this handler.
468 *
469 * Handle having recieved the SIGINT signal, sent to us when somebody
470 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100471 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100472 **/
473static void
474cad_handler (void *data,
475 NihSignal *signal)
476{
Scott James Remnant36eea462008-02-22 15:11:05 +0000477 event_new (NULL, CTRLALTDEL_EVENT, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100478}
479
480/**
481 * kbd_handler:
482 * @data: unused,
483 * @signal: signal that called this handler.
484 *
485 * Handle having recieved the SIGWINCH signal, sent to us when somebody
486 * presses Alt-UpArrow on the console. We just generate a
487 * kbdrequest event.
488 **/
489static void
490kbd_handler (void *data,
491 NihSignal *signal)
492{
Scott James Remnant36eea462008-02-22 15:11:05 +0000493 event_new (NULL, KBDREQUEST_EVENT, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100494}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100495
496/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000497 * pwr_handler:
498 * @data: unused,
499 * @signal: signal that called this handler.
500 *
501 * Handle having recieved the SIGPWR signal, sent to us when powstatd
502 * changes the /etc/powerstatus file. We just generate a
503 * power-status-changed event and jobs read the file.
504 **/
505static void
506pwr_handler (void *data,
507 NihSignal *signal)
508{
Scott James Remnant36eea462008-02-22 15:11:05 +0000509 event_new (NULL, PWRSTATUS_EVENT, NULL);
Scott James Remnant2c950692007-02-25 09:13:38 +0000510}
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000511#endif
Scott James Remnant2c950692007-02-25 09:13:38 +0000512
513/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100514 * hup_handler:
515 * @data: unused,
516 * @signal: signal that called this handler.
517 *
518 * Handle having recieved the SIGHUP signal, which we use to instruct us to
519 * reload our configuration.
520 **/
521static void
522hup_handler (void *data,
523 NihSignal *signal)
524{
525 nih_info (_("Reloading configuration"));
526 conf_reload ();
527}
528
529/**
Scott James Remnanteabb7802006-08-31 15:39:04 +0100530 * stop_handler:
531 * @data: unused,
532 * @signal: signal caught.
533 *
534 * This is called when we receive the STOP, TSTP or CONT signals; we
535 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000536 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100537 **/
538static void
539stop_handler (void *data,
540 NihSignal *signal)
541{
542 nih_assert (signal != NULL);
543
544 if (signal->signum == SIGCONT) {
545 nih_info (_("Event queue resumed"));
546 paused = FALSE;
547 } else {
548 nih_info (_("Event queue paused"));
549 paused = TRUE;
550 }
551}