blob: ece600206d6a94108cfd3fd37b40699bab14e181 [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);
Ricky Zhoubedce602016-02-16 23:45:19 -080080#else
81static int logger_kmsg (NihLogLevel priority, const char *message) {}
Scott James Remnantf3ef5112008-06-05 01:26:10 +010082#endif /* DEBUG */
Scott James Remnant3401ab72006-09-01 02:14:47 +010083
Ricky Zhoubedce602016-02-16 23:45:19 -080084#ifdef HAVE_SELINUX
85#define CHECKREQPROT_PATH "/sys/fs/selinux/checkreqprot"
86static void initialize_selinux (char **argv);
87#endif
88
Scott James Remnant1db88042006-09-01 03:14:19 +010089
Scott James Remnant3401ab72006-09-01 02:14:47 +010090/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000091 * argv0:
92 *
93 * Path to program executed, used for re-executing the init binary from the
94 * same location we were executed from.
95 **/
96static const char *argv0 = NULL;
97
98/**
Scott James Remnant1db88042006-09-01 03:14:19 +010099 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100100 *
Scott James Remnant8b227402006-10-11 17:55:27 +0100101 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +0100102 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +0100103 **/
Scott James Remnant1db88042006-09-01 03:14:19 +0100104static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100105
106
107/**
108 * options:
109 *
110 * Command-line options we accept.
111 **/
112static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100113 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100114
115 /* Ignore invalid options */
116 { '-', "--", NULL, NULL, NULL, NULL, NULL },
117
118 NIH_OPTION_LAST
119};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100120
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100121
Scott James Remnant50748842006-05-16 21:02:31 +0100122int
123main (int argc,
124 char *argv[])
125{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100126 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000127 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100128
Luigi Semenzato94964962016-01-29 13:59:20 -0800129#ifdef HAVE_SELINUX
Ricky Zhoubedce602016-02-16 23:45:19 -0800130 if (getpid () == 1 && getenv ("SELINUX_INIT") == NULL) {
131 initialize_selinux (argv);
Luigi Semenzato94964962016-01-29 13:59:20 -0800132 }
133#endif
134
Scott James Remnant06abbec2007-03-09 13:02:38 +0000135 argv0 = argv[0];
136 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200137
Scott James Remnant930e25a2006-10-13 12:28:05 +0100138 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100139 nih_option_set_help (
140 _("This daemon is normally executed by the kernel and given "
141 "process id 1 to denote its special status. When executed "
142 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100143
Scott James Remnant3401ab72006-09-01 02:14:47 +0100144 args = nih_option_parser (NULL, argc, argv, options, FALSE);
145 if (! args)
146 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200147
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000148#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100149 /* Check we're root */
150 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000151 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100152 exit (1);
153 }
154
155 /* Check we're process #1 */
156 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100157 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100158 /* Ignore failure, probably just that telinit doesn't exist */
159
Scott James Remnant31421b72007-03-08 23:53:05 +0000160 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100161 exit (1);
162 }
163
Scott James Remnant78626fb2007-01-10 16:48:10 +0000164 /* Clear our arguments from the command-line, so that we show up in
165 * ps or top output as /sbin/init, with no extra flags.
166 *
167 * This is a very Linux-specific trick; by deleting the NULL
168 * terminator at the end of the last argument, we fool the kernel
169 * into believing we used a setproctitle()-a-like to extend the
170 * argument space into the environment space, and thus make it use
171 * strlen() instead of its own assumed length. In fact, we've done
172 * the exact opposite, and shrunk the command line length to just that
173 * of whatever is in argv[0].
174 *
175 * If we don't do this, and just write \0 over the rest of argv, for
176 * example; the command-line length still includes those \0s, and ps
177 * will show whitespace in their place.
178 */
179 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000180 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000181
Scott James Remnant505f9282007-01-10 18:44:38 +0000182 arg_end = argv[argc-1] + strlen (argv[argc-1]);
183 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000184 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000185
Scott James Remnantfa733382008-01-15 15:33:27 +0000186
187 /* Become the leader of a new session and process group, shedding
188 * any controlling tty (which we shouldn't have had anyway - but
189 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000190 */
191 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100192
Scott James Remnantfa733382008-01-15 15:33:27 +0000193 /* Set the standard file descriptors to the ordinary console device,
194 * resetting it to sane defaults unless we're inheriting from another
195 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200196 */
Scott James Remnant93216182011-08-11 13:30:15 -0700197 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) {
198 NihError *err;
199
200 err = nih_error_get ();
201 nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"),
202 err->message);
203 nih_free (err);
204
205 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
206 err = nih_error_get ();
207 nih_fatal ("%s: %s", _("Unable to initialize console as /dev/null"),
208 err->message);
209 nih_free (err);
210
211 exit (1);
212 }
213 }
Scott James Remnant77e8db32006-08-21 08:47:50 +0200214
Scott James Remnant2e204b72007-02-03 23:15:28 +0000215 /* Set the PATH environment variable */
216 setenv ("PATH", PATH, TRUE);
217
Scott James Remnantfa733382008-01-15 15:33:27 +0000218 /* Switch to the root directory in case we were started from some
219 * strange place, or worse, some directory in the initramfs that's
220 * going to go away soon.
221 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100222 if (chdir ("/"))
223 nih_warn ("%s: %s", _("Unable to set root directory"),
224 strerror (errno));
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000225
226 /* Mount the /proc and /sys filesystems, which are pretty much
227 * essential for any Linux system; not to mention used by
228 * ourselves.
229 */
230 if (system_mount ("proc", "/proc") < 0) {
231 NihError *err;
232
233 err = nih_error_get ();
234 nih_warn ("%s: %s", _("Unable to mount /proc filesystem"),
235 err->message);
236 nih_free (err);
237 }
238
239 if (system_mount ("sysfs", "/sys") < 0) {
240 NihError *err;
241
242 err = nih_error_get ();
243 nih_warn ("%s: %s", _("Unable to mount /sys filesystem"),
244 err->message);
245 nih_free (err);
246 }
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000247#else /* DEBUG */
248 nih_log_set_priority (NIH_LOG_DEBUG);
James Hunt39f1c4f2010-12-13 18:15:24 +0000249 nih_debug ("Running as PID %d (PPID %d)",
250 (int)getpid (), (int)getppid ());
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000251#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000252
Scott James Remnant2e204b72007-02-03 23:15:28 +0000253
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100254 /* Reset the signal state and install the signal handler for those
255 * signals we actually want to catch; this also sets those that
256 * can be sent to us, because we're special
257 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100258 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000259 nih_signal_reset ();
260
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100261#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000262 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000263 * iteration through the main loop.
264 */
265 nih_signal_set_handler (SIGSEGV, crash_handler);
266 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000267#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000268
Scott James Remnant5c408432007-11-07 21:42:25 -0500269 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
270 * directly; it's enough that they interrupt the main loop and
271 * get dealt with during it.
272 */
273 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
274 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100275
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000276#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100277 /* Ask the kernel to send us SIGINT when control-alt-delete is
278 * pressed; generate an event with the same name.
279 */
280 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500281 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000282 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100283
284 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100285 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100286 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500287 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
288 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000289 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
290 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500291 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100292
Scott James Remnant2c950692007-02-25 09:13:38 +0000293 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500294 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000295 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
296
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100297 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500298 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100299 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnante7138052010-02-04 00:41:25 -0800300
301 /* SIGUSR1 instructs us to reconnect to D-Bus */
302 nih_signal_set_handler (SIGUSR1, nih_signal_handler);
303 NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100304#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100305
Scott James Remnantfa733382008-01-15 15:33:27 +0000306
Scott James Remnant0eade492007-11-15 05:48:07 +0000307 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000308 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100309 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100310
Scott James Remnant5c408432007-11-07 21:42:25 -0500311 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000312 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000313 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100314
Scott James Remnant94d00982006-08-25 15:38:22 +0200315
Scott James Remnant91da63a2011-08-11 13:47:00 -0700316 /* Adjust our OOM priority to the default, which will be inherited
317 * by all jobs.
318 */
319 if (JOB_DEFAULT_OOM_SCORE_ADJ) {
320 char filename[PATH_MAX];
321 int oom_value;
322 FILE *fd;
323
324 snprintf (filename, sizeof (filename),
325 "/proc/%d/oom_score_adj", getpid ());
326 oom_value = JOB_DEFAULT_OOM_SCORE_ADJ;
327 fd = fopen (filename, "w");
Scott James Remnant4c0fa532011-08-11 13:52:28 -0700328 if ((! fd) && (errno == ENOENT)) {
Scott James Remnant91da63a2011-08-11 13:47:00 -0700329 snprintf (filename, sizeof (filename),
330 "/proc/%d/oom_adj", getpid ());
331 oom_value = (JOB_DEFAULT_OOM_SCORE_ADJ
332 * ((JOB_DEFAULT_OOM_SCORE_ADJ < 0) ? 17 : 15)) / 1000;
333 fd = fopen (filename, "w");
334 }
335 if (! fd) {
336 nih_warn ("%s: %s", _("Unable to set default oom score"),
337 strerror (errno));
338 } else {
339 fprintf (fd, "%d\n", oom_value);
340
341 if (fclose (fd))
342 nih_warn ("%s: %s", _("Unable to set default oom score"),
343 strerror (errno));
344 }
345 }
346
347
Scott James Remnant54b2a952007-06-10 22:15:24 +0100348 /* Read configuration */
Scott James Remnant854fda22009-06-23 01:21:55 +0100349 NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
350 NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100351
352 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000353
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100354 /* Create a listening server for private connections. */
355 while (control_server_open () < 0) {
356 NihError *err;
357
358 err = nih_error_get ();
359 if (err->number != ENOMEM) {
360 nih_warn ("%s: %s", _("Unable to listen for private connections"),
361 err->message);
362 nih_free (err);
363 break;
364 }
365 nih_free (err);
366 }
367
Scott James Remnantf8491442008-04-18 13:19:24 +0100368 /* Open connection to the system bus; we normally expect this to
369 * fail and will try again later - don't let ENOMEM stop us though.
370 */
371 while (control_bus_open () < 0) {
372 NihError *err;
373 int number;
374
375 err = nih_error_get ();
376 number = err->number;
377 nih_free (err);
378
379 if (number != ENOMEM)
380 break;
381 }
382
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100383#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000384 /* Now that the startup is complete, send all further logging output
Scott James Remnant02977f82011-02-17 15:33:04 -0800385 * to kmsg instead of to the console.
Scott James Remnantfa733382008-01-15 15:33:27 +0000386 */
Scott James Remnant93216182011-08-11 13:30:15 -0700387 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
388 NihError *err;
389
390 err = nih_error_get ();
391 nih_fatal ("%s: %s", _("Unable to setup standard file descriptors"),
392 err->message);
393 nih_free (err);
394
395 exit (1);
396 }
Scott James Remnant540dcfd2011-03-16 15:54:56 -0700397
Scott James Remnant02977f82011-02-17 15:33:04 -0800398 nih_log_set_logger (logger_kmsg);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000399#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000400
401
Scott James Remnant3401ab72006-09-01 02:14:47 +0100402 /* Generate and run the startup event or read the state from the
403 * init daemon that exec'd us
404 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100405 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100406 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100407 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100408 sigset_t mask;
409
Scott James Remnant1db88042006-09-01 03:14:19 +0100410 /* We're ok to receive signals again */
411 sigemptyset (&mask);
412 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100413 }
414
Scott James Remnantfa733382008-01-15 15:33:27 +0000415 /* Run through the loop at least once to deal with signals that were
416 * delivered to the previous process while the mask was set or to
417 * process the startup event we emitted.
418 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000419 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200420 ret = nih_main_loop ();
421
422 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100423}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100424
425
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000426#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100427/**
Scott James Remnant02977f82011-02-17 15:33:04 -0800428 * logger_kmsg:
429 * @priority: priority of message being logged,
430 * @message: message to log.
431 *
432 * Outputs the @message to the kernel log message socket prefixed with an
433 * appropriate tag based on @priority, the program name and terminated with
434 * a new line.
435 *
436 * Returns: zero on success, negative value on error.
437 **/
438static int
439logger_kmsg (NihLogLevel priority,
440 const char *message)
441{
442 int tag;
443 FILE *kmsg;
444
445 nih_assert (message != NULL);
446
447 switch (priority) {
448 case NIH_LOG_DEBUG:
449 tag = '7';
450 break;
451 case NIH_LOG_INFO:
452 tag = '6';
453 break;
454 case NIH_LOG_MESSAGE:
455 tag = '5';
456 break;
457 case NIH_LOG_WARN:
458 tag = '4';
459 break;
460 case NIH_LOG_ERROR:
461 tag = '3';
462 break;
463 case NIH_LOG_FATAL:
464 tag = '2';
465 break;
466 default:
467 tag = 'd';
468 }
469
470 kmsg = fopen ("/dev/kmsg", "w");
471 if (! kmsg)
472 return -1;
473
474 if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
475 int saved_errno = errno;
476 fclose (kmsg);
477 errno = saved_errno;
478 return -1;
479 }
480
481 if (fclose (kmsg) < 0)
482 return -1;
483
484 return 0;
485}
486
487
488/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000489 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100490 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100491 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000492 * Handle receiving the SEGV or ABRT signal, usually caused by one of
493 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100494 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000495 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100496 * Sadly there's no real alternative to the ensuing kernel panic. Our
497 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
498 * re-exec since the system will be suddenly lobotomised. We definitely
499 * don't want to start a root shell or anything like that. Best thing is
500 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100501 **/
502static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000503crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100504{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100505 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000506
507 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100508
509 pid = fork ();
510 if (pid == 0) {
511 struct sigaction act;
512 struct rlimit limit;
513 sigset_t mask;
514
515 /* Mask out all signals */
516 sigfillset (&mask);
517 sigprocmask (SIG_SETMASK, &mask, NULL);
518
Scott James Remnant16a286f2007-01-10 15:38:33 +0000519 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100520 act.sa_handler = SIG_DFL;
521 act.sa_flags = 0;
522 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000523 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100524
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100525 /* Don't limit the core dump size */
526 limit.rlim_cur = RLIM_INFINITY;
527 limit.rlim_max = RLIM_INFINITY;
528 setrlimit (RLIMIT_CORE, &limit);
529
Scott James Remnantfa733382008-01-15 15:33:27 +0000530 /* Dump in the root directory */
Scott James Remnant9d736bb2009-07-21 18:15:20 +0100531 if (chdir ("/"))
532 nih_warn ("%s: %s", _("Unable to set root directory"),
533 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000534
Scott James Remnant16a286f2007-01-10 15:38:33 +0000535 /* Raise the signal again */
536 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100537
538 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000539 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100540 sigprocmask (SIG_SETMASK, &mask, NULL);
541
542 /* Wait for death */
543 pause ();
544 exit (0);
545 } else if (pid > 0) {
546 /* Wait for the core to be generated */
547 waitpid (pid, NULL, 0);
548
Scott James Remnant31421b72007-03-08 23:53:05 +0000549 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000550 (signum == SIGSEGV
551 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100552 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000553 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000554 (signum == SIGSEGV
555 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100556 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000557
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100558 /* Goodbye, cruel world. */
559 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100560}
561
562/**
563 * cad_handler:
564 * @data: unused,
565 * @signal: signal that called this handler.
566 *
567 * Handle having recieved the SIGINT signal, sent to us when somebody
568 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100569 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100570 **/
571static void
572cad_handler (void *data,
573 NihSignal *signal)
574{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100575 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100576}
577
578/**
579 * kbd_handler:
580 * @data: unused,
581 * @signal: signal that called this handler.
582 *
583 * Handle having recieved the SIGWINCH signal, sent to us when somebody
584 * presses Alt-UpArrow on the console. We just generate a
585 * kbdrequest event.
586 **/
587static void
588kbd_handler (void *data,
589 NihSignal *signal)
590{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100591 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100592}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100593
594/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000595 * pwr_handler:
596 * @data: unused,
597 * @signal: signal that called this handler.
598 *
599 * Handle having recieved the SIGPWR signal, sent to us when powstatd
600 * changes the /etc/powerstatus file. We just generate a
601 * power-status-changed event and jobs read the file.
602 **/
603static void
604pwr_handler (void *data,
605 NihSignal *signal)
606{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100607 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000608}
609
610/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100611 * hup_handler:
612 * @data: unused,
613 * @signal: signal that called this handler.
614 *
615 * Handle having recieved the SIGHUP signal, which we use to instruct us to
616 * reload our configuration.
617 **/
618static void
619hup_handler (void *data,
620 NihSignal *signal)
621{
622 nih_info (_("Reloading configuration"));
623 conf_reload ();
Scott James Remnante7138052010-02-04 00:41:25 -0800624}
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100625
Scott James Remnante7138052010-02-04 00:41:25 -0800626/**
627 * usr1_handler:
628 * @data: unused,
629 * @signal: signal that called this handler.
630 *
631 * Handle having recieved the SIGUSR signal, which we use to instruct us to
632 * reconnect to D-Bus.
633 **/
634static void
635usr1_handler (void *data,
636 NihSignal *signal)
637{
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100638 if (! control_bus) {
639 nih_info (_("Reconnecting to system bus"));
640
641 if (control_bus_open () < 0) {
642 NihError *err;
643
644 err = nih_error_get ();
645 nih_warn ("%s: %s", _("Unable to connect to the system bus"),
646 err->message);
647 nih_free (err);
648 }
649 }
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100650}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100651#endif /* DEBUG */
Ricky Zhoubedce602016-02-16 23:45:19 -0800652
653#ifdef HAVE_SELINUX
654/**
655 * initialize_selinux:
656 *
657 * Loads an SELinux policy and reexecs init to enter the the proper SELinux
658 * context.
659 **/
660void initialize_selinux (char **argv)
661{
662 int enforce = 0;
663 FILE *checkreqprot_file;
664 const char *errstr;
665
666 program_name = argv[0]; /* for logger_kmsg before NIH init */
667 putenv ("SELINUX_INIT=YES");
668 if (selinux_init_load_policy (&enforce) != 0) {
669 logger_kmsg (NIH_LOG_WARN, "SELinux policy failed to load");
670 if (enforce > 0) {
671 /* Enforcing mode, must quit. */
672 logger_kmsg (NIH_LOG_FATAL,
673 "no SELinux policy in enforcing mode: quit");
674 exit (1);
675 }
676 }
677
678 checkreqprot_file = fopen (CHECKREQPROT_PATH, "w");
679 if (checkreqprot_file == NULL) {
680 errstr = strerror(errno);
681 logger_kmsg (NIH_LOG_FATAL,
682 "Failed to open " CHECKREQPROT_PATH);
683 logger_kmsg (NIH_LOG_FATAL, errstr);
684 exit (1);
685 }
686 if (fputc ('0', checkreqprot_file) == EOF) {
687 errstr = strerror(errno);
688 logger_kmsg (NIH_LOG_FATAL,
689 "Failed to write " CHECKREQPROT_PATH);
690 logger_kmsg (NIH_LOG_FATAL, errstr);
691 exit (1);
692 }
693 if (fclose (checkreqprot_file) != 0) {
694 errstr = strerror(errno);
695 logger_kmsg (NIH_LOG_FATAL,
696 "Failed to close " CHECKREQPROT_PATH);
697 logger_kmsg (NIH_LOG_FATAL, errstr);
698 exit (1);
699 }
700
701 logger_kmsg (NIH_LOG_MESSAGE, "SELinux policy loaded, doing self-exec");
702 execv (argv[0], argv);
703 errstr = strerror(errno);
704
705 logger_kmsg (NIH_LOG_FATAL, "Failed to re-exec init.");
706 logger_kmsg (NIH_LOG_FATAL, errstr);
707 exit (1);
708}
709#endif /* HAVE_SELINUX */