blob: b84e90c5e7f93e75a50b310300c96ac0b30e4a85 [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
Luigi Semenzato94964962016-01-29 13:59:20 -080032#include <sys/stat.h>
33#include <fcntl.h>
34
Scott James Remnant3401ab72006-09-01 02:14:47 +010035#include <errno.h>
36#include <stdio.h>
Scott James Remnant91da63a2011-08-11 13:47:00 -070037#include <limits.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010038#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020039#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010040#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020041#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020042#include <unistd.h>
43
Luigi Semenzato94964962016-01-29 13:59:20 -080044#ifdef HAVE_SELINUX
45#include <selinux/selinux.h>
46#endif
47
Scott James Remnant097b2a92006-09-01 19:30:37 +010048#include <linux/kd.h>
49
Scott James Remnant77e8db32006-08-21 08:47:50 +020050#include <nih/macros.h>
51#include <nih/alloc.h>
52#include <nih/list.h>
53#include <nih/timer.h>
54#include <nih/signal.h>
55#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010056#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020057#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010058#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020059#include <nih/logging.h>
60
Scott James Remnantf2f69d02008-04-29 23:38:23 +010061#include "paths.h"
Scott James Remnant5d82d902008-04-30 00:03:29 +010062#include "events.h"
Scott James Remnantf2f69d02008-04-29 23:38:23 +010063#include "system.h"
Scott James Remnant91da63a2011-08-11 13:47:00 -070064#include "job_class.h"
Scott James Remnant63fd5c72008-04-30 21:34:31 +010065#include "job_process.h"
Scott James Remnant77e8db32006-08-21 08:47:50 +020066#include "event.h"
Scott James Remnant54b2a952007-06-10 22:15:24 +010067#include "conf.h"
Scott James Remnantf8491442008-04-18 13:19:24 +010068#include "control.h"
Scott James Remnant50748842006-05-16 21:02:31 +010069
70
Scott James Remnantf43bdf32006-08-27 18:20:29 +010071/* Prototypes for static functions */
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000072#ifndef DEBUG
Scott James Remnant02977f82011-02-17 15:33:04 -080073static int logger_kmsg (NihLogLevel priority, const char *message);
Scott James Remnant16a286f2007-01-10 15:38:33 +000074static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010075static void cad_handler (void *data, NihSignal *signal);
76static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000077static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +010078static void hup_handler (void *data, NihSignal *signal);
Scott James Remnante7138052010-02-04 00:41:25 -080079static void usr1_handler (void *data, NihSignal *signal);
Scott James Remnantf3ef5112008-06-05 01:26:10 +010080#endif /* DEBUG */
Scott James Remnant3401ab72006-09-01 02:14:47 +010081
Scott James Remnant1db88042006-09-01 03:14:19 +010082
Scott James Remnant3401ab72006-09-01 02:14:47 +010083/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000084 * argv0:
85 *
86 * Path to program executed, used for re-executing the init binary from the
87 * same location we were executed from.
88 **/
89static const char *argv0 = NULL;
90
91/**
Scott James Remnant1db88042006-09-01 03:14:19 +010092 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010093 *
Scott James Remnant8b227402006-10-11 17:55:27 +010094 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010095 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010096 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010097static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010098
99
100/**
101 * options:
102 *
103 * Command-line options we accept.
104 **/
105static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100106 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100107
108 /* Ignore invalid options */
109 { '-', "--", NULL, NULL, NULL, NULL, NULL },
110
111 NIH_OPTION_LAST
112};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100113
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100114
Scott James Remnant50748842006-05-16 21:02:31 +0100115int
116main (int argc,
117 char *argv[])
118{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100119 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000120 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100121
Luigi Semenzato94964962016-01-29 13:59:20 -0800122#ifdef HAVE_SELINUX
123 int enforce = 0;
124
125 if (getpid() == 1 && getenv ("SELINUX_INIT") == NULL) {
126 const char *old_program_name = program_name;
127 program_name = "selinux"; /* for logger_kmsg before NIH init */
128 putenv ("SELINUX_INIT=YES");
129 if (selinux_init_load_policy (&enforce) == 0 ) {
130 logger_kmsg (NIH_LOG_MESSAGE,
131 "policy loaded, doing self-exec\n");
132 execv (argv[0], argv);
133 } else {
134 logger_kmsg (NIH_LOG_WARN, "policy failed to load\n");
135 if (enforce > 0) {
136 /* Enforcing mode, must quit. */
137 logger_kmsg (NIH_LOG_FATAL,
138 "no policy in enforcing "
139 "mode: quit\n");
140 exit (1);
141 }
142 }
143 program_name = old_program_name; /* put things back */
144 }
145#endif
146
Scott James Remnant06abbec2007-03-09 13:02:38 +0000147 argv0 = argv[0];
148 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200149
Scott James Remnant930e25a2006-10-13 12:28:05 +0100150 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100151 nih_option_set_help (
152 _("This daemon is normally executed by the kernel and given "
153 "process id 1 to denote its special status. When executed "
154 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100155
Scott James Remnant3401ab72006-09-01 02:14:47 +0100156 args = nih_option_parser (NULL, argc, argv, options, FALSE);
157 if (! args)
158 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200159
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000160#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100161 /* Check we're root */
162 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000163 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100164 exit (1);
165 }
166
167 /* Check we're process #1 */
168 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100169 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100170 /* Ignore failure, probably just that telinit doesn't exist */
171
Scott James Remnant31421b72007-03-08 23:53:05 +0000172 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100173 exit (1);
174 }
175
Scott James Remnant78626fb2007-01-10 16:48:10 +0000176 /* Clear our arguments from the command-line, so that we show up in
177 * ps or top output as /sbin/init, with no extra flags.
178 *
179 * This is a very Linux-specific trick; by deleting the NULL
180 * terminator at the end of the last argument, we fool the kernel
181 * into believing we used a setproctitle()-a-like to extend the
182 * argument space into the environment space, and thus make it use
183 * strlen() instead of its own assumed length. In fact, we've done
184 * the exact opposite, and shrunk the command line length to just that
185 * of whatever is in argv[0].
186 *
187 * If we don't do this, and just write \0 over the rest of argv, for
188 * example; the command-line length still includes those \0s, and ps
189 * will show whitespace in their place.
190 */
191 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000192 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000193
Scott James Remnant505f9282007-01-10 18:44:38 +0000194 arg_end = argv[argc-1] + strlen (argv[argc-1]);
195 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000196 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000197
Scott James Remnantfa733382008-01-15 15:33:27 +0000198
199 /* Become the leader of a new session and process group, shedding
200 * any controlling tty (which we shouldn't have had anyway - but
201 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000202 */
203 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100204
Scott James Remnantfa733382008-01-15 15:33:27 +0000205 /* Set the standard file descriptors to the ordinary console device,
206 * resetting it to sane defaults unless we're inheriting from another
207 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200208 */
Scott James Remnant93216182011-08-11 13:30:15 -0700209 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) {
210 NihError *err;
211
212 err = nih_error_get ();
213 nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"),
214 err->message);
215 nih_free (err);
216
217 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
218 err = nih_error_get ();
219 nih_fatal ("%s: %s", _("Unable to initialize console as /dev/null"),
220 err->message);
221 nih_free (err);
222
223 exit (1);
224 }
225 }
Scott James Remnant77e8db32006-08-21 08:47:50 +0200226
Scott James Remnant2e204b72007-02-03 23:15:28 +0000227 /* Set the PATH environment variable */
228 setenv ("PATH", PATH, TRUE);
229
Scott James Remnantfa733382008-01-15 15:33:27 +0000230 /* Switch to the root directory in case we were started from some
231 * strange place, or worse, some directory in the initramfs that's
232 * going to go away soon.
233 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100234 if (chdir ("/"))
235 nih_warn ("%s: %s", _("Unable to set root directory"),
236 strerror (errno));
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000237
238 /* Mount the /proc and /sys filesystems, which are pretty much
239 * essential for any Linux system; not to mention used by
240 * ourselves.
241 */
242 if (system_mount ("proc", "/proc") < 0) {
243 NihError *err;
244
245 err = nih_error_get ();
246 nih_warn ("%s: %s", _("Unable to mount /proc filesystem"),
247 err->message);
248 nih_free (err);
249 }
250
251 if (system_mount ("sysfs", "/sys") < 0) {
252 NihError *err;
253
254 err = nih_error_get ();
255 nih_warn ("%s: %s", _("Unable to mount /sys filesystem"),
256 err->message);
257 nih_free (err);
258 }
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000259#else /* DEBUG */
260 nih_log_set_priority (NIH_LOG_DEBUG);
James Hunt39f1c4f2010-12-13 18:15:24 +0000261 nih_debug ("Running as PID %d (PPID %d)",
262 (int)getpid (), (int)getppid ());
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000263#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000264
Scott James Remnant2e204b72007-02-03 23:15:28 +0000265
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100266 /* Reset the signal state and install the signal handler for those
267 * signals we actually want to catch; this also sets those that
268 * can be sent to us, because we're special
269 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100270 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000271 nih_signal_reset ();
272
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100273#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000274 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000275 * iteration through the main loop.
276 */
277 nih_signal_set_handler (SIGSEGV, crash_handler);
278 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000279#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000280
Scott James Remnant5c408432007-11-07 21:42:25 -0500281 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
282 * directly; it's enough that they interrupt the main loop and
283 * get dealt with during it.
284 */
285 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
286 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100287
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000288#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100289 /* Ask the kernel to send us SIGINT when control-alt-delete is
290 * pressed; generate an event with the same name.
291 */
292 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500293 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000294 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100295
296 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100297 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100298 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500299 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
300 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000301 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
302 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500303 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100304
Scott James Remnant2c950692007-02-25 09:13:38 +0000305 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500306 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000307 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
308
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100309 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500310 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100311 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnante7138052010-02-04 00:41:25 -0800312
313 /* SIGUSR1 instructs us to reconnect to D-Bus */
314 nih_signal_set_handler (SIGUSR1, nih_signal_handler);
315 NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100316#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100317
Scott James Remnantfa733382008-01-15 15:33:27 +0000318
Scott James Remnant0eade492007-11-15 05:48:07 +0000319 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000320 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100321 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100322
Scott James Remnant5c408432007-11-07 21:42:25 -0500323 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000324 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000325 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100326
Scott James Remnant94d00982006-08-25 15:38:22 +0200327
Scott James Remnant91da63a2011-08-11 13:47:00 -0700328 /* Adjust our OOM priority to the default, which will be inherited
329 * by all jobs.
330 */
331 if (JOB_DEFAULT_OOM_SCORE_ADJ) {
332 char filename[PATH_MAX];
333 int oom_value;
334 FILE *fd;
335
336 snprintf (filename, sizeof (filename),
337 "/proc/%d/oom_score_adj", getpid ());
338 oom_value = JOB_DEFAULT_OOM_SCORE_ADJ;
339 fd = fopen (filename, "w");
Scott James Remnant4c0fa532011-08-11 13:52:28 -0700340 if ((! fd) && (errno == ENOENT)) {
Scott James Remnant91da63a2011-08-11 13:47:00 -0700341 snprintf (filename, sizeof (filename),
342 "/proc/%d/oom_adj", getpid ());
343 oom_value = (JOB_DEFAULT_OOM_SCORE_ADJ
344 * ((JOB_DEFAULT_OOM_SCORE_ADJ < 0) ? 17 : 15)) / 1000;
345 fd = fopen (filename, "w");
346 }
347 if (! fd) {
348 nih_warn ("%s: %s", _("Unable to set default oom score"),
349 strerror (errno));
350 } else {
351 fprintf (fd, "%d\n", oom_value);
352
353 if (fclose (fd))
354 nih_warn ("%s: %s", _("Unable to set default oom score"),
355 strerror (errno));
356 }
357 }
358
359
Scott James Remnant54b2a952007-06-10 22:15:24 +0100360 /* Read configuration */
Scott James Remnant854fda22009-06-23 01:21:55 +0100361 NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
362 NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100363
364 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000365
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100366 /* Create a listening server for private connections. */
367 while (control_server_open () < 0) {
368 NihError *err;
369
370 err = nih_error_get ();
371 if (err->number != ENOMEM) {
372 nih_warn ("%s: %s", _("Unable to listen for private connections"),
373 err->message);
374 nih_free (err);
375 break;
376 }
377 nih_free (err);
378 }
379
Scott James Remnantf8491442008-04-18 13:19:24 +0100380 /* Open connection to the system bus; we normally expect this to
381 * fail and will try again later - don't let ENOMEM stop us though.
382 */
383 while (control_bus_open () < 0) {
384 NihError *err;
385 int number;
386
387 err = nih_error_get ();
388 number = err->number;
389 nih_free (err);
390
391 if (number != ENOMEM)
392 break;
393 }
394
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100395#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000396 /* Now that the startup is complete, send all further logging output
Scott James Remnant02977f82011-02-17 15:33:04 -0800397 * to kmsg instead of to the console.
Scott James Remnantfa733382008-01-15 15:33:27 +0000398 */
Scott James Remnant93216182011-08-11 13:30:15 -0700399 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
400 NihError *err;
401
402 err = nih_error_get ();
403 nih_fatal ("%s: %s", _("Unable to setup standard file descriptors"),
404 err->message);
405 nih_free (err);
406
407 exit (1);
408 }
Scott James Remnant540dcfd2011-03-16 15:54:56 -0700409
Scott James Remnant02977f82011-02-17 15:33:04 -0800410 nih_log_set_logger (logger_kmsg);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000411#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000412
413
Scott James Remnant3401ab72006-09-01 02:14:47 +0100414 /* Generate and run the startup event or read the state from the
415 * init daemon that exec'd us
416 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100417 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100418 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100419 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100420 sigset_t mask;
421
Scott James Remnant1db88042006-09-01 03:14:19 +0100422 /* We're ok to receive signals again */
423 sigemptyset (&mask);
424 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100425 }
426
Scott James Remnantfa733382008-01-15 15:33:27 +0000427 /* Run through the loop at least once to deal with signals that were
428 * delivered to the previous process while the mask was set or to
429 * process the startup event we emitted.
430 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000431 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200432 ret = nih_main_loop ();
433
434 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100435}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100436
437
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000438#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100439/**
Scott James Remnant02977f82011-02-17 15:33:04 -0800440 * logger_kmsg:
441 * @priority: priority of message being logged,
442 * @message: message to log.
443 *
444 * Outputs the @message to the kernel log message socket prefixed with an
445 * appropriate tag based on @priority, the program name and terminated with
446 * a new line.
447 *
448 * Returns: zero on success, negative value on error.
449 **/
450static int
451logger_kmsg (NihLogLevel priority,
452 const char *message)
453{
454 int tag;
455 FILE *kmsg;
456
457 nih_assert (message != NULL);
458
459 switch (priority) {
460 case NIH_LOG_DEBUG:
461 tag = '7';
462 break;
463 case NIH_LOG_INFO:
464 tag = '6';
465 break;
466 case NIH_LOG_MESSAGE:
467 tag = '5';
468 break;
469 case NIH_LOG_WARN:
470 tag = '4';
471 break;
472 case NIH_LOG_ERROR:
473 tag = '3';
474 break;
475 case NIH_LOG_FATAL:
476 tag = '2';
477 break;
478 default:
479 tag = 'd';
480 }
481
482 kmsg = fopen ("/dev/kmsg", "w");
483 if (! kmsg)
484 return -1;
485
486 if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
487 int saved_errno = errno;
488 fclose (kmsg);
489 errno = saved_errno;
490 return -1;
491 }
492
493 if (fclose (kmsg) < 0)
494 return -1;
495
496 return 0;
497}
498
499
500/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000501 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100502 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100503 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000504 * Handle receiving the SEGV or ABRT signal, usually caused by one of
505 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100506 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000507 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100508 * Sadly there's no real alternative to the ensuing kernel panic. Our
509 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
510 * re-exec since the system will be suddenly lobotomised. We definitely
511 * don't want to start a root shell or anything like that. Best thing is
512 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100513 **/
514static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000515crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100516{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100517 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000518
519 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100520
521 pid = fork ();
522 if (pid == 0) {
523 struct sigaction act;
524 struct rlimit limit;
525 sigset_t mask;
526
527 /* Mask out all signals */
528 sigfillset (&mask);
529 sigprocmask (SIG_SETMASK, &mask, NULL);
530
Scott James Remnant16a286f2007-01-10 15:38:33 +0000531 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100532 act.sa_handler = SIG_DFL;
533 act.sa_flags = 0;
534 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000535 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100536
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100537 /* Don't limit the core dump size */
538 limit.rlim_cur = RLIM_INFINITY;
539 limit.rlim_max = RLIM_INFINITY;
540 setrlimit (RLIMIT_CORE, &limit);
541
Scott James Remnantfa733382008-01-15 15:33:27 +0000542 /* Dump in the root directory */
Scott James Remnant9d736bb2009-07-21 18:15:20 +0100543 if (chdir ("/"))
544 nih_warn ("%s: %s", _("Unable to set root directory"),
545 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000546
Scott James Remnant16a286f2007-01-10 15:38:33 +0000547 /* Raise the signal again */
548 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100549
550 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000551 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100552 sigprocmask (SIG_SETMASK, &mask, NULL);
553
554 /* Wait for death */
555 pause ();
556 exit (0);
557 } else if (pid > 0) {
558 /* Wait for the core to be generated */
559 waitpid (pid, NULL, 0);
560
Scott James Remnant31421b72007-03-08 23:53:05 +0000561 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000562 (signum == SIGSEGV
563 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100564 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000565 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000566 (signum == SIGSEGV
567 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100568 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000569
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100570 /* Goodbye, cruel world. */
571 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100572}
573
574/**
575 * cad_handler:
576 * @data: unused,
577 * @signal: signal that called this handler.
578 *
579 * Handle having recieved the SIGINT signal, sent to us when somebody
580 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100581 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100582 **/
583static void
584cad_handler (void *data,
585 NihSignal *signal)
586{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100587 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100588}
589
590/**
591 * kbd_handler:
592 * @data: unused,
593 * @signal: signal that called this handler.
594 *
595 * Handle having recieved the SIGWINCH signal, sent to us when somebody
596 * presses Alt-UpArrow on the console. We just generate a
597 * kbdrequest event.
598 **/
599static void
600kbd_handler (void *data,
601 NihSignal *signal)
602{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100603 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100604}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100605
606/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000607 * pwr_handler:
608 * @data: unused,
609 * @signal: signal that called this handler.
610 *
611 * Handle having recieved the SIGPWR signal, sent to us when powstatd
612 * changes the /etc/powerstatus file. We just generate a
613 * power-status-changed event and jobs read the file.
614 **/
615static void
616pwr_handler (void *data,
617 NihSignal *signal)
618{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100619 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000620}
621
622/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100623 * hup_handler:
624 * @data: unused,
625 * @signal: signal that called this handler.
626 *
627 * Handle having recieved the SIGHUP signal, which we use to instruct us to
628 * reload our configuration.
629 **/
630static void
631hup_handler (void *data,
632 NihSignal *signal)
633{
634 nih_info (_("Reloading configuration"));
635 conf_reload ();
Scott James Remnante7138052010-02-04 00:41:25 -0800636}
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100637
Scott James Remnante7138052010-02-04 00:41:25 -0800638/**
639 * usr1_handler:
640 * @data: unused,
641 * @signal: signal that called this handler.
642 *
643 * Handle having recieved the SIGUSR signal, which we use to instruct us to
644 * reconnect to D-Bus.
645 **/
646static void
647usr1_handler (void *data,
648 NihSignal *signal)
649{
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100650 if (! control_bus) {
651 nih_info (_("Reconnecting to system bus"));
652
653 if (control_bus_open () < 0) {
654 NihError *err;
655
656 err = nih_error_get ();
657 nih_warn ("%s: %s", _("Unable to connect to the system bus"),
658 err->message);
659 nih_free (err);
660 }
661 }
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100662}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100663#endif /* DEBUG */