blob: ed3ee6bf82bbe5bf052f3b2c1ce1d6256b69406c [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>
Dmitry Torokhov164e5352016-06-08 16:16:35 -070031#include <sys/mount.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010032
Luigi Semenzato94964962016-01-29 13:59:20 -080033#include <sys/stat.h>
34#include <fcntl.h>
35
Scott James Remnant3401ab72006-09-01 02:14:47 +010036#include <errno.h>
37#include <stdio.h>
Scott James Remnant91da63a2011-08-11 13:47:00 -070038#include <limits.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010039#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020040#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010041#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020042#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020043#include <unistd.h>
44
Gwendal Grignou8c5b5292016-03-07 11:51:37 -080045#ifdef ADD_DIRCRYPTO_RING
46#include <ext2fs/ext2_fs.h>
47#include <keyutils.h>
48#endif
49
Luigi Semenzato94964962016-01-29 13:59:20 -080050#ifdef HAVE_SELINUX
51#include <selinux/selinux.h>
52#endif
53
Scott James Remnant097b2a92006-09-01 19:30:37 +010054#include <linux/kd.h>
55
Scott James Remnant77e8db32006-08-21 08:47:50 +020056#include <nih/macros.h>
57#include <nih/alloc.h>
58#include <nih/list.h>
59#include <nih/timer.h>
60#include <nih/signal.h>
61#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010062#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020063#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010064#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020065#include <nih/logging.h>
66
Scott James Remnantf2f69d02008-04-29 23:38:23 +010067#include "paths.h"
Dmitry Torokhov164e5352016-06-08 16:16:35 -070068#include "errors.h"
Scott James Remnant5d82d902008-04-30 00:03:29 +010069#include "events.h"
Scott James Remnantf2f69d02008-04-29 23:38:23 +010070#include "system.h"
Scott James Remnant91da63a2011-08-11 13:47:00 -070071#include "job_class.h"
Scott James Remnant63fd5c72008-04-30 21:34:31 +010072#include "job_process.h"
Scott James Remnant77e8db32006-08-21 08:47:50 +020073#include "event.h"
Scott James Remnant54b2a952007-06-10 22:15:24 +010074#include "conf.h"
Scott James Remnantf8491442008-04-18 13:19:24 +010075#include "control.h"
Scott James Remnant50748842006-05-16 21:02:31 +010076
77
Scott James Remnantf43bdf32006-08-27 18:20:29 +010078/* Prototypes for static functions */
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000079#ifndef DEBUG
Scott James Remnant02977f82011-02-17 15:33:04 -080080static int logger_kmsg (NihLogLevel priority, const char *message);
Scott James Remnant16a286f2007-01-10 15:38:33 +000081static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010082static void cad_handler (void *data, NihSignal *signal);
83static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000084static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +010085static void hup_handler (void *data, NihSignal *signal);
Scott James Remnante7138052010-02-04 00:41:25 -080086static void usr1_handler (void *data, NihSignal *signal);
Scott James Remnantf3ef5112008-06-05 01:26:10 +010087#endif /* DEBUG */
Scott James Remnant3401ab72006-09-01 02:14:47 +010088
Ricky Zhoubedce602016-02-16 23:45:19 -080089#ifdef HAVE_SELINUX
Dmitry Torokhov164e5352016-06-08 16:16:35 -070090static int initialize_selinux (void);
Ricky Zhoubedce602016-02-16 23:45:19 -080091#endif
92
Scott James Remnant3401ab72006-09-01 02:14:47 +010093/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000094 * argv0:
95 *
96 * Path to program executed, used for re-executing the init binary from the
97 * same location we were executed from.
98 **/
99static const char *argv0 = NULL;
100
101/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100102 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100103 *
Scott James Remnant8b227402006-10-11 17:55:27 +0100104 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +0100105 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +0100106 **/
Scott James Remnant1db88042006-09-01 03:14:19 +0100107static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100108
109
110/**
111 * options:
112 *
113 * Command-line options we accept.
114 **/
115static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100116 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100117
118 /* Ignore invalid options */
119 { '-', "--", NULL, NULL, NULL, NULL, NULL },
120
121 NIH_OPTION_LAST
122};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100123
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100124
Scott James Remnant50748842006-05-16 21:02:31 +0100125int
126main (int argc,
127 char *argv[])
128{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100129 char **args;
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700130 char *arg_end = NULL;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000131 int ret;
Gwendal Grignou8c5b5292016-03-07 11:51:37 -0800132#ifdef ADD_DIRCRYPTO_RING
133 int root_fd;
134 struct ext4_encryption_policy policy;
135 key_serial_t keyring_id;
136#endif
Scott James Remnant50748842006-05-16 21:02:31 +0100137
Scott James Remnant06abbec2007-03-09 13:02:38 +0000138 argv0 = argv[0];
139 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200140
Scott James Remnant930e25a2006-10-13 12:28:05 +0100141 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100142 nih_option_set_help (
143 _("This daemon is normally executed by the kernel and given "
144 "process id 1 to denote its special status. When executed "
145 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100146
Scott James Remnant3401ab72006-09-01 02:14:47 +0100147 args = nih_option_parser (NULL, argc, argv, options, FALSE);
148 if (! args)
149 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200150
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000151#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100152 /* Check we're root */
153 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000154 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100155 exit (1);
156 }
157
158 /* Check we're process #1 */
159 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100160 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100161 /* Ignore failure, probably just that telinit doesn't exist */
162
Scott James Remnant31421b72007-03-08 23:53:05 +0000163 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100164 exit (1);
165 }
166
Scott James Remnant78626fb2007-01-10 16:48:10 +0000167 /* Clear our arguments from the command-line, so that we show up in
168 * ps or top output as /sbin/init, with no extra flags.
169 *
170 * This is a very Linux-specific trick; by deleting the NULL
171 * terminator at the end of the last argument, we fool the kernel
172 * into believing we used a setproctitle()-a-like to extend the
173 * argument space into the environment space, and thus make it use
174 * strlen() instead of its own assumed length. In fact, we've done
175 * the exact opposite, and shrunk the command line length to just that
176 * of whatever is in argv[0].
177 *
178 * If we don't do this, and just write \0 over the rest of argv, for
179 * example; the command-line length still includes those \0s, and ps
180 * will show whitespace in their place.
181 */
182 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000183 arg_end = argv[argc-1] + strlen (argv[argc-1]);
184 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000185 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000186
Scott James Remnantfa733382008-01-15 15:33:27 +0000187
188 /* Become the leader of a new session and process group, shedding
189 * any controlling tty (which we shouldn't have had anyway - but
190 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000191 */
192 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100193
Scott James Remnantfa733382008-01-15 15:33:27 +0000194 /* Set the standard file descriptors to the ordinary console device,
195 * resetting it to sane defaults unless we're inheriting from another
196 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200197 */
Scott James Remnant93216182011-08-11 13:30:15 -0700198 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) {
199 NihError *err;
200
201 err = nih_error_get ();
202 nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"),
203 err->message);
204 nih_free (err);
205
206 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
207 err = nih_error_get ();
208 nih_fatal ("%s: %s", _("Unable to initialize console as /dev/null"),
209 err->message);
210 nih_free (err);
211
212 exit (1);
213 }
214 }
Scott James Remnant77e8db32006-08-21 08:47:50 +0200215
Scott James Remnant2e204b72007-02-03 23:15:28 +0000216 /* Set the PATH environment variable */
217 setenv ("PATH", PATH, TRUE);
218
Scott James Remnantfa733382008-01-15 15:33:27 +0000219 /* Switch to the root directory in case we were started from some
220 * strange place, or worse, some directory in the initramfs that's
221 * going to go away soon.
222 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100223 if (chdir ("/"))
224 nih_warn ("%s: %s", _("Unable to set root directory"),
225 strerror (errno));
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000226
227 /* Mount the /proc and /sys filesystems, which are pretty much
228 * essential for any Linux system; not to mention used by
229 * ourselves.
230 */
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700231 if (system_mount ("proc", "/proc",
232 MS_NODEV | MS_NOEXEC | MS_NOSUID) < 0) {
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000233 NihError *err;
234
235 err = nih_error_get ();
236 nih_warn ("%s: %s", _("Unable to mount /proc filesystem"),
237 err->message);
238 nih_free (err);
239 }
240
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700241 if (system_mount ("sysfs", "/sys",
242 MS_NODEV | MS_NOEXEC | MS_NOSUID) < 0) {
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000243 NihError *err;
244
245 err = nih_error_get ();
246 nih_warn ("%s: %s", _("Unable to mount /sys filesystem"),
247 err->message);
248 nih_free (err);
249 }
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700250
251#ifdef HAVE_SELINUX
252 if (!getenv ("SELINUX_INIT")) {
253 /*
254 * We mount selinuxfs ourselves instead of letting
255 * libselinux do it so that our standard mount options
256 * (nosuid and noexec) will be applied. Note that
257 * we leave devices on since there is null device in
258 * selinuxfs.
259 */
260 if (system_mount ("selinuxfs", "/sys/fs/selinux",
261 MS_NOEXEC | MS_NOSUID) < 0) {
262 NihError *err;
263
264 err = nih_error_get ();
265 nih_fatal ("%s: %s",
266 _("Unable to mount /sys/fs/selinux filesystem"),
267 err->message);
268 nih_free (err);
269
270 exit (1);
271 }
272
273 if (initialize_selinux () < 0) {
274 NihError *err;
275
276 err = nih_error_get ();
277 nih_fatal ("%s: %s",
278 _("Failed to initialize SELinux"),
279 err->message);
280 nih_free (err);
281
282 exit (1);
283 }
284
285 putenv ("SELINUX_INIT=YES");
286 nih_info (_("SELinux policy loaded, doing self-exec"));
287
288 /* Unmangle argv and re-execute */
289 if (arg_end)
290 *arg_end = '\0';
291 execv (argv0, argv);
292
293 nih_fatal ("%s: %s",
294 _("Failed to re-exec init"),
295 strerror (errno));
296 exit (1);
297 }
298#endif
299
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000300#else /* DEBUG */
301 nih_log_set_priority (NIH_LOG_DEBUG);
James Hunt39f1c4f2010-12-13 18:15:24 +0000302 nih_debug ("Running as PID %d (PPID %d)",
303 (int)getpid (), (int)getppid ());
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000304#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000305
Gwendal Grignou8c5b5292016-03-07 11:51:37 -0800306#ifdef ADD_DIRCRYPTO_RING
307#define EXT4_IOC_GET_ENCRYPTION_POLICY \
308 _IOW('f', 21, struct ext4_encryption_policy)
309 /*
310 * Set a keyring for the session to hold ext4 crypto keys.
311 * The session is at the root of all processes, so any users who wish
312 * to access a directory protected by ext4 crypto can access the key.
313 *
314 * Set only a session keyring when needed.
315 * A kernel patch is needed (see crbug/593893).
316 * Upstream kernel does not have the patch yet
317 * (See https://lkml.org/lkml/2016/3/17/491).
318 */
319 int fd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
320 if (fd == -1) {
321 nih_warn("%s: %s", _("Unable to open / directory: %s"),
322 strerror (errno));
323 ret = EINVAL;
324 } else {
325 ret = ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &policy);
326 if (ret)
327 ret = errno;
328 close(fd);
329 }
330 if (ret != EINVAL && ret != EOPNOTSUPP && ret != ENOTTY) {
331 keyring_id = add_key ("keyring", "dircrypt", 0, 0,
332 KEY_SPEC_SESSION_KEYRING);
Gwendal Grignoue44d1832017-02-03 13:07:36 -0800333 if (keyring_id == -1) {
Gwendal Grignou8c5b5292016-03-07 11:51:37 -0800334 nih_warn ("%s: %s",
335 _("Unable to create dircrypt keyring: %s"),
336 strerror (errno));
Gwendal Grignoue44d1832017-02-03 13:07:36 -0800337 } else {
Gwendal Grignou8c5b5292016-03-07 11:51:37 -0800338 keyctl_setperm(keyring_id,
339 KEY_POS_VIEW | KEY_POS_SEARCH |
340 KEY_POS_LINK | KEY_POS_READ |
341 KEY_USR_ALL);
Gwendal Grignoue44d1832017-02-03 13:07:36 -0800342 keyctl_setperm(KEY_SPEC_SESSION_KEYRING,
343 KEY_POS_VIEW | KEY_POS_SEARCH |
344 KEY_POS_LINK | KEY_POS_READ |
345 KEY_USR_ALL);
346 }
Gwendal Grignou8c5b5292016-03-07 11:51:37 -0800347 }
348#endif
Scott James Remnant2e204b72007-02-03 23:15:28 +0000349
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100350 /* Reset the signal state and install the signal handler for those
351 * signals we actually want to catch; this also sets those that
352 * can be sent to us, because we're special
353 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100354 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000355 nih_signal_reset ();
356
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100357#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000358 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000359 * iteration through the main loop.
360 */
361 nih_signal_set_handler (SIGSEGV, crash_handler);
362 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000363#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000364
Scott James Remnant5c408432007-11-07 21:42:25 -0500365 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
366 * directly; it's enough that they interrupt the main loop and
367 * get dealt with during it.
368 */
369 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
370 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100371
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000372#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100373 /* Ask the kernel to send us SIGINT when control-alt-delete is
374 * pressed; generate an event with the same name.
375 */
376 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500377 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000378 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100379
380 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100381 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100382 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500383 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
384 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000385 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
386 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500387 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100388
Scott James Remnant2c950692007-02-25 09:13:38 +0000389 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500390 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000391 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
392
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100393 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500394 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100395 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnante7138052010-02-04 00:41:25 -0800396
397 /* SIGUSR1 instructs us to reconnect to D-Bus */
398 nih_signal_set_handler (SIGUSR1, nih_signal_handler);
399 NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100400#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100401
Scott James Remnantfa733382008-01-15 15:33:27 +0000402
Scott James Remnant0eade492007-11-15 05:48:07 +0000403 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000404 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100405 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100406
Scott James Remnant5c408432007-11-07 21:42:25 -0500407 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000408 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000409 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100410
Scott James Remnant94d00982006-08-25 15:38:22 +0200411
Scott James Remnant91da63a2011-08-11 13:47:00 -0700412 /* Adjust our OOM priority to the default, which will be inherited
413 * by all jobs.
414 */
415 if (JOB_DEFAULT_OOM_SCORE_ADJ) {
416 char filename[PATH_MAX];
417 int oom_value;
418 FILE *fd;
419
420 snprintf (filename, sizeof (filename),
421 "/proc/%d/oom_score_adj", getpid ());
422 oom_value = JOB_DEFAULT_OOM_SCORE_ADJ;
423 fd = fopen (filename, "w");
Scott James Remnant4c0fa532011-08-11 13:52:28 -0700424 if ((! fd) && (errno == ENOENT)) {
Scott James Remnant91da63a2011-08-11 13:47:00 -0700425 snprintf (filename, sizeof (filename),
426 "/proc/%d/oom_adj", getpid ());
427 oom_value = (JOB_DEFAULT_OOM_SCORE_ADJ
428 * ((JOB_DEFAULT_OOM_SCORE_ADJ < 0) ? 17 : 15)) / 1000;
429 fd = fopen (filename, "w");
430 }
431 if (! fd) {
432 nih_warn ("%s: %s", _("Unable to set default oom score"),
433 strerror (errno));
434 } else {
435 fprintf (fd, "%d\n", oom_value);
436
437 if (fclose (fd))
438 nih_warn ("%s: %s", _("Unable to set default oom score"),
439 strerror (errno));
440 }
441 }
442
443
Scott James Remnant54b2a952007-06-10 22:15:24 +0100444 /* Read configuration */
Scott James Remnant854fda22009-06-23 01:21:55 +0100445 NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
446 NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100447
448 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000449
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100450 /* Create a listening server for private connections. */
451 while (control_server_open () < 0) {
452 NihError *err;
453
454 err = nih_error_get ();
455 if (err->number != ENOMEM) {
456 nih_warn ("%s: %s", _("Unable to listen for private connections"),
457 err->message);
458 nih_free (err);
459 break;
460 }
461 nih_free (err);
462 }
463
Scott James Remnantf8491442008-04-18 13:19:24 +0100464 /* Open connection to the system bus; we normally expect this to
465 * fail and will try again later - don't let ENOMEM stop us though.
466 */
467 while (control_bus_open () < 0) {
468 NihError *err;
469 int number;
470
471 err = nih_error_get ();
472 number = err->number;
473 nih_free (err);
474
475 if (number != ENOMEM)
476 break;
477 }
478
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100479#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000480 /* Now that the startup is complete, send all further logging output
Scott James Remnant02977f82011-02-17 15:33:04 -0800481 * to kmsg instead of to the console.
Scott James Remnantfa733382008-01-15 15:33:27 +0000482 */
Scott James Remnant93216182011-08-11 13:30:15 -0700483 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
484 NihError *err;
485
486 err = nih_error_get ();
487 nih_fatal ("%s: %s", _("Unable to setup standard file descriptors"),
488 err->message);
489 nih_free (err);
490
491 exit (1);
492 }
Scott James Remnant540dcfd2011-03-16 15:54:56 -0700493
Scott James Remnant02977f82011-02-17 15:33:04 -0800494 nih_log_set_logger (logger_kmsg);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000495#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000496
497
Scott James Remnant3401ab72006-09-01 02:14:47 +0100498 /* Generate and run the startup event or read the state from the
499 * init daemon that exec'd us
500 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100501 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100502 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100503 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100504 sigset_t mask;
505
Scott James Remnant1db88042006-09-01 03:14:19 +0100506 /* We're ok to receive signals again */
507 sigemptyset (&mask);
508 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100509 }
510
Scott James Remnantfa733382008-01-15 15:33:27 +0000511 /* Run through the loop at least once to deal with signals that were
512 * delivered to the previous process while the mask was set or to
513 * process the startup event we emitted.
514 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000515 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200516 ret = nih_main_loop ();
517
518 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100519}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100520
521
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000522#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100523/**
Scott James Remnant02977f82011-02-17 15:33:04 -0800524 * logger_kmsg:
525 * @priority: priority of message being logged,
526 * @message: message to log.
527 *
528 * Outputs the @message to the kernel log message socket prefixed with an
529 * appropriate tag based on @priority, the program name and terminated with
530 * a new line.
531 *
532 * Returns: zero on success, negative value on error.
533 **/
534static int
535logger_kmsg (NihLogLevel priority,
536 const char *message)
537{
538 int tag;
539 FILE *kmsg;
540
541 nih_assert (message != NULL);
542
543 switch (priority) {
544 case NIH_LOG_DEBUG:
545 tag = '7';
546 break;
547 case NIH_LOG_INFO:
548 tag = '6';
549 break;
550 case NIH_LOG_MESSAGE:
551 tag = '5';
552 break;
553 case NIH_LOG_WARN:
554 tag = '4';
555 break;
556 case NIH_LOG_ERROR:
557 tag = '3';
558 break;
559 case NIH_LOG_FATAL:
560 tag = '2';
561 break;
562 default:
563 tag = 'd';
564 }
565
566 kmsg = fopen ("/dev/kmsg", "w");
567 if (! kmsg)
568 return -1;
569
570 if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
571 int saved_errno = errno;
572 fclose (kmsg);
573 errno = saved_errno;
574 return -1;
575 }
576
577 if (fclose (kmsg) < 0)
578 return -1;
579
580 return 0;
581}
582
583
584/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000585 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100586 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100587 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000588 * Handle receiving the SEGV or ABRT signal, usually caused by one of
589 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100590 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000591 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100592 * Sadly there's no real alternative to the ensuing kernel panic. Our
593 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
594 * re-exec since the system will be suddenly lobotomised. We definitely
595 * don't want to start a root shell or anything like that. Best thing is
596 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100597 **/
598static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000599crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100600{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100601 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000602
603 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100604
605 pid = fork ();
606 if (pid == 0) {
607 struct sigaction act;
608 struct rlimit limit;
609 sigset_t mask;
610
611 /* Mask out all signals */
612 sigfillset (&mask);
613 sigprocmask (SIG_SETMASK, &mask, NULL);
614
Scott James Remnant16a286f2007-01-10 15:38:33 +0000615 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100616 act.sa_handler = SIG_DFL;
617 act.sa_flags = 0;
618 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000619 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100620
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100621 /* Don't limit the core dump size */
622 limit.rlim_cur = RLIM_INFINITY;
623 limit.rlim_max = RLIM_INFINITY;
624 setrlimit (RLIMIT_CORE, &limit);
625
Scott James Remnantfa733382008-01-15 15:33:27 +0000626 /* Dump in the root directory */
Scott James Remnant9d736bb2009-07-21 18:15:20 +0100627 if (chdir ("/"))
628 nih_warn ("%s: %s", _("Unable to set root directory"),
629 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000630
Scott James Remnant16a286f2007-01-10 15:38:33 +0000631 /* Raise the signal again */
632 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100633
634 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000635 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100636 sigprocmask (SIG_SETMASK, &mask, NULL);
637
638 /* Wait for death */
639 pause ();
640 exit (0);
641 } else if (pid > 0) {
642 /* Wait for the core to be generated */
643 waitpid (pid, NULL, 0);
644
Scott James Remnant31421b72007-03-08 23:53:05 +0000645 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000646 (signum == SIGSEGV
647 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100648 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000649 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000650 (signum == SIGSEGV
651 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100652 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000653
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100654 /* Goodbye, cruel world. */
655 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100656}
657
658/**
659 * cad_handler:
660 * @data: unused,
661 * @signal: signal that called this handler.
662 *
663 * Handle having recieved the SIGINT signal, sent to us when somebody
664 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100665 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100666 **/
667static void
668cad_handler (void *data,
669 NihSignal *signal)
670{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100671 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100672}
673
674/**
675 * kbd_handler:
676 * @data: unused,
677 * @signal: signal that called this handler.
678 *
679 * Handle having recieved the SIGWINCH signal, sent to us when somebody
680 * presses Alt-UpArrow on the console. We just generate a
681 * kbdrequest event.
682 **/
683static void
684kbd_handler (void *data,
685 NihSignal *signal)
686{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100687 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100688}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100689
690/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000691 * pwr_handler:
692 * @data: unused,
693 * @signal: signal that called this handler.
694 *
695 * Handle having recieved the SIGPWR signal, sent to us when powstatd
696 * changes the /etc/powerstatus file. We just generate a
697 * power-status-changed event and jobs read the file.
698 **/
699static void
700pwr_handler (void *data,
701 NihSignal *signal)
702{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100703 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000704}
705
706/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100707 * hup_handler:
708 * @data: unused,
709 * @signal: signal that called this handler.
710 *
711 * Handle having recieved the SIGHUP signal, which we use to instruct us to
712 * reload our configuration.
713 **/
714static void
715hup_handler (void *data,
716 NihSignal *signal)
717{
718 nih_info (_("Reloading configuration"));
719 conf_reload ();
Scott James Remnante7138052010-02-04 00:41:25 -0800720}
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100721
Scott James Remnante7138052010-02-04 00:41:25 -0800722/**
723 * usr1_handler:
724 * @data: unused,
725 * @signal: signal that called this handler.
726 *
727 * Handle having recieved the SIGUSR signal, which we use to instruct us to
728 * reconnect to D-Bus.
729 **/
730static void
731usr1_handler (void *data,
732 NihSignal *signal)
733{
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100734 if (! control_bus) {
735 nih_info (_("Reconnecting to system bus"));
736
737 if (control_bus_open () < 0) {
738 NihError *err;
739
740 err = nih_error_get ();
741 nih_warn ("%s: %s", _("Unable to connect to the system bus"),
742 err->message);
743 nih_free (err);
744 }
745 }
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100746}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100747#endif /* DEBUG */
Ricky Zhoubedce602016-02-16 23:45:19 -0800748
749#ifdef HAVE_SELINUX
750/**
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700751 * selinux_set_checkreqprot:
752 *
753 * Forces /sys/fs/selinux/checkreqprot to 0 to ensure that
754 * SELinux will check the protection for mmap and mprotect
755 * calls that will be applied by the kernel and not the
756 * one requested by the application.
757 **/
758static int selinux_set_checkreqprot (void)
759{
760 static const char path[] = "/sys/fs/selinux/checkreqprot";
761 FILE *checkreqprot_file;
762
763 checkreqprot_file = fopen (path, "w");
764 if (!checkreqprot_file)
765 nih_return_system_error (-1);
766
767 if (fputc ('0', checkreqprot_file) == EOF)
768 nih_return_system_error (-1);
769
770 if (fclose (checkreqprot_file) != 0)
771 nih_return_system_error (-1);
772
773 return 0;
774}
775
776/**
Ricky Zhoubedce602016-02-16 23:45:19 -0800777 * initialize_selinux:
778 *
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700779 * Loads an SELinux policy.
Ricky Zhoubedce602016-02-16 23:45:19 -0800780 **/
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700781static int initialize_selinux (void)
Ricky Zhoubedce602016-02-16 23:45:19 -0800782{
783 int enforce = 0;
Ricky Zhoubedce602016-02-16 23:45:19 -0800784
Ricky Zhoubedce602016-02-16 23:45:19 -0800785 if (selinux_init_load_policy (&enforce) != 0) {
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700786 nih_warn (_("SELinux policy failed to load"));
Ricky Zhoubedce602016-02-16 23:45:19 -0800787 if (enforce > 0) {
788 /* Enforcing mode, must quit. */
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700789 nih_return_error (-1, SELINUX_POLICY_LOAD_FAIL,
790 _(SELINUX_POLICY_LOAD_FAIL_STR));
Ricky Zhoubedce602016-02-16 23:45:19 -0800791 }
792 }
793
Dmitry Torokhov164e5352016-06-08 16:16:35 -0700794 return selinux_set_checkreqprot ();
Ricky Zhoubedce602016-02-16 23:45:19 -0800795}
796#endif /* HAVE_SELINUX */