]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/main.c
move /usr/bin/systemd to /usr/lib/systemd/systemd
[thirdparty/systemd.git] / src / main.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
60918275 2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
ea430986
LP
22#include <dbus/dbus.h>
23
60918275
LP
24#include <stdio.h>
25#include <errno.h>
26#include <string.h>
16354eff 27#include <unistd.h>
4ade7963
LP
28#include <sys/types.h>
29#include <sys/stat.h>
f170852a 30#include <getopt.h>
97c4f35c 31#include <signal.h>
4fc935ca 32#include <sys/wait.h>
80876c20 33#include <fcntl.h>
f3b6a3ed 34#include <sys/prctl.h>
60918275
LP
35
36#include "manager.h"
16354eff 37#include "log.h"
4ade7963 38#include "mount-setup.h"
302e8c4c 39#include "hostname-setup.h"
af5bc85d 40#include "loopback-setup.h"
11c3a4ee 41#include "kmod-setup.h"
72bca11b 42#include "locale-setup.h"
c4dcdb9f 43#include "selinux-setup.h"
d7ccca2e 44#include "machine-id-setup.h"
302e8c4c 45#include "load-fragment.h"
a16e1123 46#include "fdset.h"
514f4ef5 47#include "special.h"
487393e9 48#include "conf-parser.h"
398ef8ba 49#include "bus-errors.h"
ad780f19 50#include "missing.h"
e51bc1a2 51#include "label.h"
302e27c8 52#include "build.h"
06d4c99a 53#include "strv.h"
f6a6225e 54#include "def.h"
b52aae1d 55#include "virt.h"
60918275 56
f170852a
LP
57static enum {
58 ACTION_RUN,
e965d56d 59 ACTION_HELP,
e537352b 60 ACTION_TEST,
4288f619
LP
61 ACTION_DUMP_CONFIGURATION_ITEMS,
62 ACTION_DONE
fa0f4d8a 63} arg_action = ACTION_RUN;
f170852a 64
fa0f4d8a
LP
65static char *arg_default_unit = NULL;
66static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
67
68static bool arg_dump_core = true;
69static bool arg_crash_shell = false;
70static int arg_crash_chvt = -1;
71static bool arg_confirm_spawn = false;
9e58ff9c 72static bool arg_show_status = true;
07459bb6 73#ifdef HAVE_SYSV_COMPAT
6e98720f 74static bool arg_sysv_console = true;
07459bb6 75#endif
d3689161 76static bool arg_mount_auto = true;
173a8d04 77static bool arg_swap_auto = true;
06d4c99a 78static char **arg_default_controllers = NULL;
0c85a4f3 79static char ***arg_join_controllers = NULL;
706343f4 80static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
0a494f1f 81static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
4fc935ca 82
a16e1123 83static FILE* serialization = NULL;
80876c20 84
6f5e3f35
LP
85static void nop_handler(int sig) {
86}
87
93a46b0b 88_noreturn_ static void crash(int sig) {
97c4f35c 89
fa0f4d8a 90 if (!arg_dump_core)
582a507f 91 log_error("Caught <%s>, not dumping core.", signal_to_string(sig));
97c4f35c 92 else {
6f5e3f35 93 struct sigaction sa;
97c4f35c
LP
94 pid_t pid;
95
6f5e3f35
LP
96 /* We want to wait for the core process, hence let's enable SIGCHLD */
97 zero(sa);
98 sa.sa_handler = nop_handler;
99 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
100 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
101
97c4f35c 102 if ((pid = fork()) < 0)
582a507f 103 log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno));
97c4f35c
LP
104
105 else if (pid == 0) {
97c4f35c
LP
106 struct rlimit rl;
107
108 /* Enable default signal handler for core dump */
109 zero(sa);
110 sa.sa_handler = SIG_DFL;
111 assert_se(sigaction(sig, &sa, NULL) == 0);
112
113 /* Don't limit the core dump size */
114 zero(rl);
115 rl.rlim_cur = RLIM_INFINITY;
116 rl.rlim_max = RLIM_INFINITY;
117 setrlimit(RLIMIT_CORE, &rl);
118
119 /* Just to be sure... */
120 assert_se(chdir("/") == 0);
121
122 /* Raise the signal again */
123 raise(sig);
124
125 assert_not_reached("We shouldn't be here...");
126 _exit(1);
4fc935ca
LP
127
128 } else {
8e12a6ae
LP
129 siginfo_t status;
130 int r;
4fc935ca
LP
131
132 /* Order things nicely. */
8e12a6ae
LP
133 if ((r = wait_for_terminate(pid, &status)) < 0)
134 log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r));
135 else if (status.si_code != CLD_DUMPED)
582a507f 136 log_error("Caught <%s>, core dump failed.", signal_to_string(sig));
4fc935ca 137 else
582a507f 138 log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid);
97c4f35c
LP
139 }
140 }
141
fa0f4d8a
LP
142 if (arg_crash_chvt)
143 chvt(arg_crash_chvt);
601f6a1e 144
fa0f4d8a 145 if (arg_crash_shell) {
6f5e3f35
LP
146 struct sigaction sa;
147 pid_t pid;
8c43883a 148
4fc935ca
LP
149 log_info("Executing crash shell in 10s...");
150 sleep(10);
151
6f5e3f35
LP
152 /* Let the kernel reap children for us */
153 zero(sa);
154 sa.sa_handler = SIG_IGN;
155 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
156 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
8c43883a 157
6f5e3f35
LP
158 if ((pid = fork()) < 0)
159 log_error("Failed to fork off crash shell: %s", strerror(errno));
160 else if (pid == 0) {
843d2643 161 int fd, r;
ea5652c2 162
21de3988 163 if ((fd = acquire_terminal("/dev/console", false, true, true)) < 0)
ea5652c2 164 log_error("Failed to acquire terminal: %s", strerror(-fd));
5b2a0903 165 else if ((r = make_stdio(fd)) < 0)
843d2643 166 log_error("Failed to duplicate terminal fd: %s", strerror(-r));
ea5652c2 167
6f5e3f35
LP
168 execl("/bin/sh", "/bin/sh", NULL);
169
170 log_error("execl() failed: %s", strerror(errno));
171 _exit(1);
172 }
c99b188e 173
f8e08a77 174 log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid);
4fc935ca
LP
175 }
176
177 log_info("Freezing execution.");
97c4f35c
LP
178 freeze();
179}
180
181static void install_crash_handler(void) {
182 struct sigaction sa;
183
184 zero(sa);
185
186 sa.sa_handler = crash;
187 sa.sa_flags = SA_NODEFER;
188
1b91d3e8 189 sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
97c4f35c 190}
f170852a 191
843d2643
LP
192static int console_setup(bool do_reset) {
193 int tty_fd, r;
80876c20 194
843d2643
LP
195 /* If we are init, we connect stdin/stdout/stderr to /dev/null
196 * and make sure we don't have a controlling tty. */
80876c20 197
843d2643
LP
198 release_terminal();
199
200 if (!do_reset)
201 return 0;
80876c20 202
512947d4
MS
203 tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
204 if (tty_fd < 0) {
843d2643
LP
205 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
206 return -tty_fd;
207 }
80876c20 208
512947d4
MS
209 /* We don't want to force text mode.
210 * plymouth may be showing pictures already from initrd. */
211 r = reset_terminal_fd(tty_fd, false);
212 if (r < 0)
843d2643
LP
213 log_error("Failed to reset /dev/console: %s", strerror(-r));
214
215 close_nointr_nofail(tty_fd);
80876c20
LP
216 return r;
217}
218
f170852a
LP
219static int set_default_unit(const char *u) {
220 char *c;
221
222 assert(u);
223
224 if (!(c = strdup(u)))
225 return -ENOMEM;
226
fa0f4d8a
LP
227 free(arg_default_unit);
228 arg_default_unit = c;
f170852a
LP
229 return 0;
230}
231
232static int parse_proc_cmdline_word(const char *word) {
233
234 static const char * const rlmap[] = {
ed370f5d 235 "emergency", SPECIAL_EMERGENCY_TARGET,
099663ff 236 "-b", SPECIAL_EMERGENCY_TARGET,
ed370f5d
LP
237 "single", SPECIAL_RESCUE_TARGET,
238 "-s", SPECIAL_RESCUE_TARGET,
239 "s", SPECIAL_RESCUE_TARGET,
240 "S", SPECIAL_RESCUE_TARGET,
241 "1", SPECIAL_RESCUE_TARGET,
242 "2", SPECIAL_RUNLEVEL2_TARGET,
243 "3", SPECIAL_RUNLEVEL3_TARGET,
244 "4", SPECIAL_RUNLEVEL4_TARGET,
245 "5", SPECIAL_RUNLEVEL5_TARGET,
f170852a
LP
246 };
247
5192bd19
LP
248 assert(word);
249
2f198e2f
LP
250 if (startswith(word, "systemd.unit="))
251 return set_default_unit(word + 13);
f170852a
LP
252
253 else if (startswith(word, "systemd.log_target=")) {
254
255 if (log_set_target_from_string(word + 19) < 0)
256 log_warning("Failed to parse log target %s. Ignoring.", word + 19);
257
258 } else if (startswith(word, "systemd.log_level=")) {
259
260 if (log_set_max_level_from_string(word + 18) < 0)
261 log_warning("Failed to parse log level %s. Ignoring.", word + 18);
262
bbe63281
LP
263 } else if (startswith(word, "systemd.log_color=")) {
264
265 if (log_show_color_from_string(word + 18) < 0)
266 log_warning("Failed to parse log color setting %s. Ignoring.", word + 18);
267
268 } else if (startswith(word, "systemd.log_location=")) {
269
270 if (log_show_location_from_string(word + 21) < 0)
271 log_warning("Failed to parse log location setting %s. Ignoring.", word + 21);
272
4fc935ca
LP
273 } else if (startswith(word, "systemd.dump_core=")) {
274 int r;
275
276 if ((r = parse_boolean(word + 18)) < 0)
509b6efb 277 log_warning("Failed to parse dump core switch %s. Ignoring.", word + 18);
4fc935ca 278 else
fa0f4d8a 279 arg_dump_core = r;
4fc935ca
LP
280
281 } else if (startswith(word, "systemd.crash_shell=")) {
282 int r;
283
284 if ((r = parse_boolean(word + 20)) < 0)
509b6efb 285 log_warning("Failed to parse crash shell switch %s. Ignoring.", word + 20);
4fc935ca 286 else
fa0f4d8a 287 arg_crash_shell = r;
5e7ee61c
LP
288
289 } else if (startswith(word, "systemd.confirm_spawn=")) {
290 int r;
291
292 if ((r = parse_boolean(word + 22)) < 0)
509b6efb 293 log_warning("Failed to parse confirm spawn switch %s. Ignoring.", word + 22);
5e7ee61c 294 else
fa0f4d8a 295 arg_confirm_spawn = r;
5e7ee61c 296
601f6a1e
LP
297 } else if (startswith(word, "systemd.crash_chvt=")) {
298 int k;
299
300 if (safe_atoi(word + 19, &k) < 0)
509b6efb 301 log_warning("Failed to parse crash chvt switch %s. Ignoring.", word + 19);
601f6a1e 302 else
fa0f4d8a 303 arg_crash_chvt = k;
601f6a1e 304
9e58ff9c
LP
305 } else if (startswith(word, "systemd.show_status=")) {
306 int r;
307
308 if ((r = parse_boolean(word + 20)) < 0)
509b6efb 309 log_warning("Failed to parse show status switch %s. Ignoring.", word + 20);
6e98720f 310 else
9e58ff9c 311 arg_show_status = r;
0a494f1f
LP
312 } else if (startswith(word, "systemd.default_standard_output=")) {
313 int r;
314
315 if ((r = exec_output_from_string(word + 32)) < 0)
509b6efb 316 log_warning("Failed to parse default standard output switch %s. Ignoring.", word + 32);
0a494f1f
LP
317 else
318 arg_default_std_output = r;
319 } else if (startswith(word, "systemd.default_standard_error=")) {
320 int r;
321
322 if ((r = exec_output_from_string(word + 31)) < 0)
509b6efb 323 log_warning("Failed to parse default standard error switch %s. Ignoring.", word + 31);
0a494f1f
LP
324 else
325 arg_default_std_error = r;
07459bb6 326#ifdef HAVE_SYSV_COMPAT
6e98720f
LP
327 } else if (startswith(word, "systemd.sysv_console=")) {
328 int r;
329
330 if ((r = parse_boolean(word + 21)) < 0)
509b6efb 331 log_warning("Failed to parse SysV console switch %s. Ignoring.", word + 20);
6e98720f
LP
332 else
333 arg_sysv_console = r;
07459bb6 334#endif
9e58ff9c 335
4fc935ca
LP
336 } else if (startswith(word, "systemd.")) {
337
338 log_warning("Unknown kernel switch %s. Ignoring.", word);
339
bbe63281
LP
340 log_info("Supported kernel switches:\n"
341 "systemd.unit=UNIT Default unit to start\n"
bbe63281 342 "systemd.dump_core=0|1 Dump core on crash\n"
9e58ff9c 343 "systemd.crash_shell=0|1 Run shell on crash\n"
bbe63281 344 "systemd.crash_chvt=N Change to VT #N on crash\n"
9e58ff9c 345 "systemd.confirm_spawn=0|1 Confirm every process spawn\n"
6e98720f 346 "systemd.show_status=0|1 Show status updates on the console during bootup\n"
07459bb6 347#ifdef HAVE_SYSV_COMPAT
6e98720f 348 "systemd.sysv_console=0|1 Connect output of SysV scripts to console\n"
07459bb6 349#endif
4cfa2c99 350 "systemd.log_target=console|kmsg|journal|journal-or-kmsg|syslog|syslog-or-kmsg|null\n"
6e98720f
LP
351 " Log target\n"
352 "systemd.log_level=LEVEL Log level\n"
353 "systemd.log_color=0|1 Highlight important log messages\n"
0a494f1f 354 "systemd.log_location=0|1 Include code location in log messages\n"
706343f4 355 "systemd.default_standard_output=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
0a494f1f 356 " Set default log output for services\n"
706343f4 357 "systemd.default_standard_error=null|tty|syslog|syslog+console|kmsg|kmsg+console|journal|journal+console\n"
0a494f1f 358 " Set default log error output for services\n");
4fc935ca 359
a9c501a5 360 } else if (streq(word, "quiet")) {
6e98720f 361 arg_show_status = false;
07459bb6 362#ifdef HAVE_SYSV_COMPAT
6e98720f 363 arg_sysv_console = false;
07459bb6 364#endif
9e58ff9c 365 } else {
f170852a
LP
366 unsigned i;
367
368 /* SysV compatibility */
f170852a
LP
369 for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
370 if (streq(word, rlmap[i]))
371 return set_default_unit(rlmap[i+1]);
372 }
373
374 return 0;
375}
376
f975e971 377static int config_parse_level2(
487393e9
LP
378 const char *filename,
379 unsigned line,
380 const char *section,
381 const char *lvalue,
3731f1ea 382 int ltype,
487393e9
LP
383 const char *rvalue,
384 void *data,
385 void *userdata) {
386
387 assert(filename);
388 assert(lvalue);
389 assert(rvalue);
390
391 log_set_max_level_from_string(rvalue);
392 return 0;
393}
394
395static int config_parse_target(
396 const char *filename,
397 unsigned line,
398 const char *section,
399 const char *lvalue,
3731f1ea 400 int ltype,
487393e9
LP
401 const char *rvalue,
402 void *data,
403 void *userdata) {
404
405 assert(filename);
406 assert(lvalue);
407 assert(rvalue);
408
409 log_set_target_from_string(rvalue);
410 return 0;
411}
412
413static int config_parse_color(
414 const char *filename,
415 unsigned line,
416 const char *section,
417 const char *lvalue,
3731f1ea 418 int ltype,
487393e9
LP
419 const char *rvalue,
420 void *data,
421 void *userdata) {
422
423 assert(filename);
424 assert(lvalue);
425 assert(rvalue);
426
427 log_show_color_from_string(rvalue);
428 return 0;
429}
430
431static int config_parse_location(
432 const char *filename,
433 unsigned line,
434 const char *section,
435 const char *lvalue,
3731f1ea 436 int ltype,
487393e9
LP
437 const char *rvalue,
438 void *data,
439 void *userdata) {
440
441 assert(filename);
442 assert(lvalue);
443 assert(rvalue);
444
445 log_show_location_from_string(rvalue);
446 return 0;
447}
448
f975e971 449static int config_parse_cpu_affinity2(
487393e9
LP
450 const char *filename,
451 unsigned line,
452 const char *section,
453 const char *lvalue,
3731f1ea 454 int ltype,
487393e9
LP
455 const char *rvalue,
456 void *data,
457 void *userdata) {
458
459 char *w;
460 size_t l;
461 char *state;
462 cpu_set_t *c = NULL;
463 unsigned ncpus = 0;
464
465 assert(filename);
466 assert(lvalue);
467 assert(rvalue);
468
f60f22df 469 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
487393e9
LP
470 char *t;
471 int r;
472 unsigned cpu;
473
474 if (!(t = strndup(w, l)))
475 return -ENOMEM;
476
477 r = safe_atou(t, &cpu);
478 free(t);
479
480 if (!c)
481 if (!(c = cpu_set_malloc(&ncpus)))
482 return -ENOMEM;
483
484 if (r < 0 || cpu >= ncpus) {
485 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
486 CPU_FREE(c);
487 return -EBADMSG;
488 }
489
490 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
491 }
492
493 if (c) {
494 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
495 log_warning("Failed to set CPU affinity: %m");
496
497 CPU_FREE(c);
498 }
499
500 return 0;
501}
502
0c85a4f3
LP
503static void strv_free_free(char ***l) {
504 char ***i;
505
506 if (!l)
507 return;
508
509 for (i = l; *i; i++)
510 strv_free(*i);
511
512 free(l);
513}
514
515static void free_join_controllers(void) {
516 if (!arg_join_controllers)
517 return;
518
519 strv_free_free(arg_join_controllers);
520 arg_join_controllers = NULL;
521}
522
523static int config_parse_join_controllers(
524 const char *filename,
525 unsigned line,
526 const char *section,
527 const char *lvalue,
528 int ltype,
529 const char *rvalue,
530 void *data,
531 void *userdata) {
532
533 unsigned n = 0;
534 char *state, *w;
535 size_t length;
536
537 assert(filename);
538 assert(lvalue);
539 assert(rvalue);
540
541 free_join_controllers();
542
543 FOREACH_WORD_QUOTED(w, length, rvalue, state) {
544 char *s, **l;
545
546 s = strndup(w, length);
547 if (!s)
548 return -ENOMEM;
549
550 l = strv_split(s, ",");
551 free(s);
552
553 strv_uniq(l);
554
555 if (strv_length(l) <= 1) {
556 strv_free(l);
557 continue;
558 }
559
560 if (!arg_join_controllers) {
561 arg_join_controllers = new(char**, 2);
562 if (!arg_join_controllers) {
563 strv_free(l);
564 return -ENOMEM;
565 }
566
567 arg_join_controllers[0] = l;
568 arg_join_controllers[1] = NULL;
569
570 n = 1;
571 } else {
572 char ***a;
573 char ***t;
574
575 t = new0(char**, n+2);
576 if (!t) {
577 strv_free(l);
578 return -ENOMEM;
579 }
580
581 n = 0;
582
583 for (a = arg_join_controllers; *a; a++) {
584
585 if (strv_overlap(*a, l)) {
586 char **c;
587
588 c = strv_merge(*a, l);
589 if (!c) {
590 strv_free(l);
591 strv_free_free(t);
592 return -ENOMEM;
593 }
594
595 strv_free(l);
596 l = c;
597 } else {
598 char **c;
599
600 c = strv_copy(*a);
601 if (!c) {
602 strv_free(l);
603 strv_free_free(t);
604 return -ENOMEM;
605 }
606
607 t[n++] = c;
608 }
609 }
610
611 t[n++] = strv_uniq(l);
612
613 strv_free_free(arg_join_controllers);
614 arg_join_controllers = t;
615 }
616 }
617
618 return 0;
619}
620
487393e9
LP
621static int parse_config_file(void) {
622
f975e971
LP
623 const ConfigTableItem items[] = {
624 { "Manager", "LogLevel", config_parse_level2, 0, NULL },
625 { "Manager", "LogTarget", config_parse_target, 0, NULL },
626 { "Manager", "LogColor", config_parse_color, 0, NULL },
627 { "Manager", "LogLocation", config_parse_location, 0, NULL },
628 { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
629 { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
630 { "Manager", "ShowStatus", config_parse_bool, 0, &arg_show_status },
07459bb6 631#ifdef HAVE_SYSV_COMPAT
f975e971 632 { "Manager", "SysVConsole", config_parse_bool, 0, &arg_sysv_console },
07459bb6 633#endif
f975e971
LP
634 { "Manager", "CrashChVT", config_parse_int, 0, &arg_crash_chvt },
635 { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
636 { "Manager", "MountAuto", config_parse_bool, 0, &arg_mount_auto },
637 { "Manager", "SwapAuto", config_parse_bool, 0, &arg_swap_auto },
638 { "Manager", "DefaultControllers", config_parse_strv, 0, &arg_default_controllers },
639 { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
640 { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
0c85a4f3 641 { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
f975e971 642 { NULL, NULL, NULL, 0, NULL }
487393e9
LP
643 };
644
645 FILE *f;
646 const char *fn;
647 int r;
648
af2d49f7 649 fn = arg_running_as == MANAGER_SYSTEM ? SYSTEM_CONFIG_FILE : USER_CONFIG_FILE;
f975e971
LP
650 f = fopen(fn, "re");
651 if (!f) {
487393e9
LP
652 if (errno == ENOENT)
653 return 0;
654
655 log_warning("Failed to open configuration file '%s': %m", fn);
656 return 0;
657 }
658
f975e971
LP
659 r = config_parse(fn, f, "Manager\0", config_item_table_lookup, (void*) items, false, NULL);
660 if (r < 0)
487393e9
LP
661 log_warning("Failed to parse configuration file: %s", strerror(-r));
662
663 fclose(f);
664
665 return 0;
666}
667
f170852a 668static int parse_proc_cmdline(void) {
52661efd 669 char *line, *w, *state;
f170852a 670 int r;
f170852a 671 size_t l;
f170852a 672
b770165a
LP
673 /* Don't read /proc/cmdline if we are in a container, since
674 * that is only relevant for the host system */
675 if (detect_container(NULL) > 0)
676 return 0;
677
f170852a 678 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
e364ad06 679 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
f170852a
LP
680 return 0;
681 }
682
683 FOREACH_WORD_QUOTED(w, l, line, state) {
684 char *word;
685
686 if (!(word = strndup(w, l))) {
687 r = -ENOMEM;
688 goto finish;
689 }
690
691 r = parse_proc_cmdline_word(word);
692 free(word);
693
694 if (r < 0)
695 goto finish;
696 }
697
698 r = 0;
699
700finish:
701 free(line);
702 return r;
703}
704
705static int parse_argv(int argc, char *argv[]) {
706
707 enum {
708 ARG_LOG_LEVEL = 0x100,
709 ARG_LOG_TARGET,
bbe63281
LP
710 ARG_LOG_COLOR,
711 ARG_LOG_LOCATION,
2f198e2f 712 ARG_UNIT,
edb9aaa8 713 ARG_SYSTEM,
af2d49f7 714 ARG_USER,
e537352b 715 ARG_TEST,
80876c20 716 ARG_DUMP_CONFIGURATION_ITEMS,
9e58ff9c
LP
717 ARG_DUMP_CORE,
718 ARG_CRASH_SHELL,
a16e1123 719 ARG_CONFIRM_SPAWN,
9e58ff9c 720 ARG_SHOW_STATUS,
6e98720f 721 ARG_SYSV_CONSOLE,
4288f619 722 ARG_DESERIALIZE,
0a494f1f
LP
723 ARG_INTROSPECT,
724 ARG_DEFAULT_STD_OUTPUT,
725 ARG_DEFAULT_STD_ERROR
f170852a
LP
726 };
727
728 static const struct option options[] = {
a16e1123
LP
729 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
730 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
bbe63281
LP
731 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
732 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
2f198e2f 733 { "unit", required_argument, NULL, ARG_UNIT },
edb9aaa8 734 { "system", no_argument, NULL, ARG_SYSTEM },
af2d49f7 735 { "user", no_argument, NULL, ARG_USER },
a16e1123
LP
736 { "test", no_argument, NULL, ARG_TEST },
737 { "help", no_argument, NULL, 'h' },
738 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
9e58ff9c
LP
739 { "dump-core", no_argument, NULL, ARG_DUMP_CORE },
740 { "crash-shell", no_argument, NULL, ARG_CRASH_SHELL },
a16e1123 741 { "confirm-spawn", no_argument, NULL, ARG_CONFIRM_SPAWN },
6e98720f 742 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
07459bb6 743#ifdef HAVE_SYSV_COMPAT
6e98720f 744 { "sysv-console", optional_argument, NULL, ARG_SYSV_CONSOLE },
07459bb6 745#endif
a16e1123 746 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
4288f619 747 { "introspect", optional_argument, NULL, ARG_INTROSPECT },
0a494f1f
LP
748 { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
749 { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
a16e1123 750 { NULL, 0, NULL, 0 }
f170852a
LP
751 };
752
753 int c, r;
754
755 assert(argc >= 1);
756 assert(argv);
757
b770165a
LP
758 if (getpid() == 1)
759 opterr = 0;
760
099663ff 761 while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
f170852a
LP
762
763 switch (c) {
764
765 case ARG_LOG_LEVEL:
766 if ((r = log_set_max_level_from_string(optarg)) < 0) {
767 log_error("Failed to parse log level %s.", optarg);
768 return r;
769 }
770
771 break;
772
773 case ARG_LOG_TARGET:
774
775 if ((r = log_set_target_from_string(optarg)) < 0) {
776 log_error("Failed to parse log target %s.", optarg);
777 return r;
778 }
779
780 break;
781
bbe63281
LP
782 case ARG_LOG_COLOR:
783
d0b170c8
LP
784 if (optarg) {
785 if ((r = log_show_color_from_string(optarg)) < 0) {
786 log_error("Failed to parse log color setting %s.", optarg);
787 return r;
788 }
789 } else
790 log_show_color(true);
bbe63281
LP
791
792 break;
793
794 case ARG_LOG_LOCATION:
795
d0b170c8
LP
796 if (optarg) {
797 if ((r = log_show_location_from_string(optarg)) < 0) {
798 log_error("Failed to parse log location setting %s.", optarg);
799 return r;
800 }
801 } else
802 log_show_location(true);
bbe63281
LP
803
804 break;
805
0a494f1f
LP
806 case ARG_DEFAULT_STD_OUTPUT:
807
808 if ((r = exec_output_from_string(optarg)) < 0) {
809 log_error("Failed to parse default standard output setting %s.", optarg);
810 return r;
811 } else
812 arg_default_std_output = r;
813 break;
814
815 case ARG_DEFAULT_STD_ERROR:
816
817 if ((r = exec_output_from_string(optarg)) < 0) {
818 log_error("Failed to parse default standard error output setting %s.", optarg);
819 return r;
820 } else
821 arg_default_std_error = r;
822 break;
823
2f198e2f 824 case ARG_UNIT:
f170852a
LP
825
826 if ((r = set_default_unit(optarg)) < 0) {
827 log_error("Failed to set default unit %s: %s", optarg, strerror(-r));
828 return r;
829 }
830
831 break;
832
edb9aaa8
LP
833 case ARG_SYSTEM:
834 arg_running_as = MANAGER_SYSTEM;
835 break;
a5dab5ce 836
af2d49f7
LP
837 case ARG_USER:
838 arg_running_as = MANAGER_USER;
a5dab5ce 839 break;
a5dab5ce 840
e965d56d 841 case ARG_TEST:
fa0f4d8a 842 arg_action = ACTION_TEST;
e965d56d
LP
843 break;
844
e537352b 845 case ARG_DUMP_CONFIGURATION_ITEMS:
fa0f4d8a 846 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
e537352b
LP
847 break;
848
9e58ff9c
LP
849 case ARG_DUMP_CORE:
850 arg_dump_core = true;
851 break;
852
853 case ARG_CRASH_SHELL:
854 arg_crash_shell = true;
855 break;
856
80876c20 857 case ARG_CONFIRM_SPAWN:
fa0f4d8a 858 arg_confirm_spawn = true;
80876c20
LP
859 break;
860
9e58ff9c 861 case ARG_SHOW_STATUS:
6e98720f
LP
862
863 if (optarg) {
864 if ((r = parse_boolean(optarg)) < 0) {
865 log_error("Failed to show status boolean %s.", optarg);
866 return r;
867 }
868 arg_show_status = r;
869 } else
870 arg_show_status = true;
871 break;
07459bb6 872#ifdef HAVE_SYSV_COMPAT
6e98720f
LP
873 case ARG_SYSV_CONSOLE:
874
875 if (optarg) {
876 if ((r = parse_boolean(optarg)) < 0) {
877 log_error("Failed to SysV console boolean %s.", optarg);
878 return r;
879 }
880 arg_sysv_console = r;
881 } else
882 arg_sysv_console = true;
9e58ff9c 883 break;
07459bb6 884#endif
9e58ff9c 885
a16e1123
LP
886 case ARG_DESERIALIZE: {
887 int fd;
888 FILE *f;
889
890 if ((r = safe_atoi(optarg, &fd)) < 0 || fd < 0) {
891 log_error("Failed to parse deserialize option %s.", optarg);
892 return r;
893 }
894
895 if (!(f = fdopen(fd, "r"))) {
896 log_error("Failed to open serialization fd: %m");
897 return r;
898 }
899
900 if (serialization)
901 fclose(serialization);
902
903 serialization = f;
904
905 break;
906 }
907
4288f619
LP
908 case ARG_INTROSPECT: {
909 const char * const * i = NULL;
910
911 for (i = bus_interface_table; *i; i += 2)
912 if (!optarg || streq(i[0], optarg)) {
913 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
914 "<node>\n", stdout);
915 fputs(i[1], stdout);
916 fputs("</node>\n", stdout);
917
918 if (optarg)
919 break;
920 }
921
922 if (!i[0] && optarg)
923 log_error("Unknown interface %s.", optarg);
924
fa0f4d8a 925 arg_action = ACTION_DONE;
4288f619
LP
926 break;
927 }
928
f170852a 929 case 'h':
fa0f4d8a 930 arg_action = ACTION_HELP;
f170852a
LP
931 break;
932
1d2e23ab
LP
933 case 'D':
934 log_set_max_level(LOG_DEBUG);
935 break;
936
099663ff
LP
937 case 'b':
938 case 's':
939 case 'z':
940 /* Just to eat away the sysvinit kernel
941 * cmdline args without getopt() error
942 * messages that we'll parse in
943 * parse_proc_cmdline_word() or ignore. */
f170852a 944
099663ff 945 case '?':
f170852a 946 default:
099663ff
LP
947 if (getpid() != 1) {
948 log_error("Unknown option code %c", c);
949 return -EINVAL;
950 }
951
952 break;
f170852a
LP
953 }
954
d821e6d6
LP
955 if (optind < argc && getpid() != 1) {
956 /* Hmm, when we aren't run as init system
957 * let's complain about excess arguments */
958
959 log_error("Excess arguments.");
960 return -EINVAL;
961 }
962
963 if (detect_container(NULL) > 0) {
964 char **a;
965
966 /* All /proc/cmdline arguments the kernel didn't
967 * understand it passed to us. We're not really
968 * interested in that usually since /proc/cmdline is
969 * more interesting and complete. With one exception:
970 * if we are run in a container /proc/cmdline is not
971 * relevant for the container, hence we rely on argv[]
972 * instead. */
973
974 for (a = argv; a < argv + argc; a++)
975 if ((r = parse_proc_cmdline_word(*a)) < 0)
976 return r;
51f0e189
LP
977 }
978
f170852a
LP
979 return 0;
980}
981
982static int help(void) {
983
2e33c433 984 printf("%s [OPTIONS...]\n\n"
af2d49f7 985 "Starts up and maintains the system or user services.\n\n"
e537352b 986 " -h --help Show this help\n"
e537352b 987 " --test Determine startup sequence, dump it and exit\n"
80876c20 988 " --dump-configuration-items Dump understood unit configuration items\n"
bbe63281 989 " --introspect[=INTERFACE] Extract D-Bus interface data\n"
9e58ff9c 990 " --unit=UNIT Set default unit\n"
edb9aaa8 991 " --system Run a system instance, even if PID != 1\n"
af2d49f7 992 " --user Run a user instance\n"
9e58ff9c
LP
993 " --dump-core Dump core on crash\n"
994 " --crash-shell Run shell on crash\n"
995 " --confirm-spawn Ask for confirmation when spawning processes\n"
6e98720f 996 " --show-status[=0|1] Show status updates on the console during bootup\n"
07459bb6 997#ifdef HAVE_SYSV_COMPAT
6e98720f 998 " --sysv-console[=0|1] Connect output of SysV scripts to console\n"
07459bb6 999#endif
4cfa2c99 1000 " --log-target=TARGET Set log target (console, journal, syslog, kmsg, journal-or-kmsg, syslog-or-kmsg, null)\n"
9e58ff9c 1001 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
2218198b 1002 " --log-color[=0|1] Highlight important log messages\n"
0a494f1f
LP
1003 " --log-location[=0|1] Include code location in log messages\n"
1004 " --default-standard-output= Set default standard output for services\n"
1005 " --default-standard-error= Set default standard error output for services\n",
5b6319dc 1006 program_invocation_short_name);
f170852a
LP
1007
1008 return 0;
1009}
1010
a16e1123
LP
1011static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
1012 FILE *f = NULL;
1013 FDSet *fds = NULL;
1014 int r;
1015
1016 assert(m);
1017 assert(_f);
1018 assert(_fds);
1019
a7556052
LP
1020 /* Make sure nothing is really destructed when we shut down */
1021 m->n_reloading ++;
1022
d8d5ab98 1023 if ((r = manager_open_serialization(m, &f)) < 0) {
35b8ca3a 1024 log_error("Failed to create serialization file: %s", strerror(-r));
a16e1123
LP
1025 goto fail;
1026 }
1027
1028 if (!(fds = fdset_new())) {
1029 r = -ENOMEM;
1030 log_error("Failed to allocate fd set: %s", strerror(-r));
1031 goto fail;
1032 }
1033
1034 if ((r = manager_serialize(m, f, fds)) < 0) {
1035 log_error("Failed to serialize state: %s", strerror(-r));
1036 goto fail;
1037 }
1038
1039 if (fseeko(f, 0, SEEK_SET) < 0) {
1040 log_error("Failed to rewind serialization fd: %m");
1041 goto fail;
1042 }
1043
1044 if ((r = fd_cloexec(fileno(f), false)) < 0) {
1045 log_error("Failed to disable O_CLOEXEC for serialization: %s", strerror(-r));
1046 goto fail;
1047 }
1048
1049 if ((r = fdset_cloexec(fds, false)) < 0) {
1050 log_error("Failed to disable O_CLOEXEC for serialization fds: %s", strerror(-r));
1051 goto fail;
1052 }
1053
1054 *_f = f;
1055 *_fds = fds;
1056
1057 return 0;
1058
1059fail:
1060 fdset_free(fds);
1061
1062 if (f)
1063 fclose(f);
1064
1065 return r;
1066}
1067
e9ddabc2
LP
1068static struct dual_timestamp* parse_initrd_timestamp(struct dual_timestamp *t) {
1069 const char *e;
1070 unsigned long long a, b;
1071
1072 assert(t);
1073
1074 if (!(e = getenv("RD_TIMESTAMP")))
1075 return NULL;
1076
1077 if (sscanf(e, "%llu %llu", &a, &b) != 2)
1078 return NULL;
1079
1080 t->realtime = (usec_t) a;
1081 t->monotonic = (usec_t) b;
1082
1083 return t;
1084}
1085
6ee5bbf8
LP
1086static void test_mtab(void) {
1087 char *p;
1088
80758717
LP
1089 /* Check that /etc/mtab is a symlink */
1090
6ee5bbf8
LP
1091 if (readlink_malloc("/etc/mtab", &p) >= 0) {
1092 bool b;
1093
ed86ebc4 1094 b = streq(p, "/proc/self/mounts") || streq(p, "/proc/mounts");
6ee5bbf8
LP
1095 free(p);
1096
1097 if (b)
1098 return;
1099 }
1100
80758717
LP
1101 log_warning("/etc/mtab is not a symlink or not pointing to /proc/self/mounts. "
1102 "This is not supported anymore. "
1103 "Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.");
1104}
1105
1106static void test_usr(void) {
80758717 1107
ed1c99fc 1108 /* Check that /usr is not a separate fs */
80758717 1109
871c44a7
LP
1110 if (dir_is_empty("/usr") <= 0)
1111 return;
1112
2376ce13 1113 log_warning("/usr appears to be on its own filesytem and is not already mounted. This is not a supported setup. "
871c44a7
LP
1114 "Some things will probably break (sometimes even silently) in mysterious ways. "
1115 "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
1116}
1117
1118static void test_cgroups(void) {
1119
1120 if (access("/proc/cgroups", F_OK) >= 0)
1121 return;
1122
1123 log_warning("CONFIG_CGROUPS was not set when your kernel was compiled. "
1124 "Systems without control groups are not supported. "
1125 "We will now sleep for 10s, and then continue boot-up. "
1126 "Expect breakage and please do not file bugs. "
1127 "Instead fix your kernel and enable CONFIG_CGROUPS." );
1128
1129 sleep(10);
6ee5bbf8
LP
1130}
1131
60918275
LP
1132int main(int argc, char *argv[]) {
1133 Manager *m = NULL;
22f4096c 1134 int r, retval = EXIT_FAILURE;
9d76d730
LP
1135 usec_t before_startup, after_startup;
1136 char timespan[FORMAT_TIMESPAN_MAX];
a16e1123
LP
1137 FDSet *fds = NULL;
1138 bool reexecute = false;
b9080b03 1139 const char *shutdown_verb = NULL;
e9ddabc2 1140 dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
5d6b1584 1141 static char systemd[] = "systemd";
0b3325e7
LP
1142 bool is_reexec = false;
1143 int j;
1144 bool loaded_policy = false;
27b14a22 1145
058dc6f3 1146#ifdef HAVE_SYSV_COMPAT
2cb1a60d 1147 if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
35b8ca3a 1148 /* This is compatibility support for SysV, where
2cb1a60d
LP
1149 * calling init as a user is identical to telinit. */
1150
1151 errno = -ENOENT;
1152 execv(SYSTEMCTL_BINARY_PATH, argv);
1153 log_error("Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
1154 return 1;
1155 }
058dc6f3 1156#endif
2cb1a60d 1157
0b3325e7
LP
1158 /* Determine if this is a reexecution or normal bootup. We do
1159 * the full command line parsing much later, so let's just
1160 * have a quick peek here. */
1161
1162 for (j = 1; j < argc; j++)
1163 if (streq(argv[j], "--deserialize")) {
0b3325e7 1164 is_reexec = true;
7aaa27f2 1165 break;
0b3325e7
LP
1166 }
1167
f3b6a3ed
LP
1168 /* If we get started via the /sbin/init symlink then we are
1169 called 'init'. After a subsequent reexecution we are then
1170 called 'systemd'. That is confusing, hence let's call us
1171 systemd right-away. */
f3b6a3ed
LP
1172 program_invocation_short_name = systemd;
1173 prctl(PR_SET_NAME, systemd);
5d6b1584 1174
9a0e6896
LP
1175 saved_argv = argv;
1176 saved_argc = argc;
f3b6a3ed 1177
2cc59dbf 1178 log_show_color(isatty(STDERR_FILENO) > 0);
bbe63281 1179 log_show_location(false);
7c706717 1180 log_set_max_level(LOG_INFO);
bbe63281 1181
843d2643 1182 if (getpid() == 1) {
fa0f4d8a 1183 arg_running_as = MANAGER_SYSTEM;
4cfa2c99 1184 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_CONSOLE : LOG_TARGET_JOURNAL_OR_KMSG);
0ff4cdd9 1185
0b3325e7
LP
1186 if (!is_reexec)
1187 if (selinux_setup(&loaded_policy) < 0)
1188 goto finish;
1189
1190 log_open();
c4dcdb9f 1191
0ff4cdd9
LP
1192 if (label_init() < 0)
1193 goto finish;
7948c4df 1194
0b3325e7
LP
1195 if (!is_reexec)
1196 if (hwclock_is_localtime() > 0) {
1197 int min;
7948c4df 1198
0b3325e7
LP
1199 r = hwclock_apply_localtime_delta(&min);
1200 if (r < 0)
1201 log_error("Failed to apply local time delta, ignoring: %s", strerror(-r));
1202 else
1203 log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
1204 }
871e5809 1205
bbe63281 1206 } else {
af2d49f7 1207 arg_running_as = MANAGER_USER;
eeecf6e6 1208 log_set_target(LOG_TARGET_AUTO);
871e5809 1209 log_open();
bbe63281 1210 }
a5dab5ce 1211
0c85a4f3 1212 /* Initialize default unit */
f170852a
LP
1213 if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0)
1214 goto finish;
60918275 1215
0c85a4f3
LP
1216 /* By default, mount "cpu" and "cpuacct" together */
1217 arg_join_controllers = new(char**, 2);
1218 if (!arg_join_controllers)
1219 goto finish;
1220
1221 arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
1222 arg_join_controllers[1] = NULL;
1223
1224 if (!arg_join_controllers[0])
1225 goto finish;
1226
f170852a
LP
1227 /* Mount /proc, /sys and friends, so that /proc/cmdline and
1228 * /proc/$PID/fd is available. */
0c85a4f3
LP
1229 if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS")) {
1230 r = mount_setup(loaded_policy);
1231 if (r < 0)
8efe3c01 1232 goto finish;
0c85a4f3 1233 }
4ade7963
LP
1234
1235 /* Reset all signal handlers. */
1236 assert_se(reset_all_signal_handlers() == 0);
1237
078e4539 1238 /* If we are init, we can block sigkill. Yay. */
9a34ec5f 1239 ignore_signals(SIGNALS_IGNORE, -1);
078e4539 1240
487393e9
LP
1241 if (parse_config_file() < 0)
1242 goto finish;
1243
fa0f4d8a 1244 if (arg_running_as == MANAGER_SYSTEM)
a5dab5ce
LP
1245 if (parse_proc_cmdline() < 0)
1246 goto finish;
f170852a
LP
1247
1248 log_parse_environment();
1249
1250 if (parse_argv(argc, argv) < 0)
1251 goto finish;
1252
b5c6cf87
LP
1253 if (arg_action == ACTION_TEST && geteuid() == 0) {
1254 log_error("Don't run test mode as root.");
1255 goto finish;
1256 }
1257
fe783b03
LP
1258 if (arg_running_as == MANAGER_SYSTEM &&
1259 arg_action == ACTION_RUN &&
1260 running_in_chroot() > 0) {
1261 log_error("Cannot be run in a chroot() environment.");
1262 goto finish;
1263 }
1264
fa0f4d8a 1265 if (arg_action == ACTION_HELP) {
f170852a
LP
1266 retval = help();
1267 goto finish;
fa0f4d8a 1268 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
e537352b 1269 unit_dump_config_items(stdout);
22f4096c 1270 retval = EXIT_SUCCESS;
e537352b 1271 goto finish;
fa0f4d8a 1272 } else if (arg_action == ACTION_DONE) {
22f4096c 1273 retval = EXIT_SUCCESS;
4288f619 1274 goto finish;
f170852a
LP
1275 }
1276
fa0f4d8a 1277 assert_se(arg_action == ACTION_RUN || arg_action == ACTION_TEST);
f170852a 1278
871e5809
LP
1279 /* Close logging fds, in order not to confuse fdset below */
1280 log_close();
1281
a16e1123
LP
1282 /* Remember open file descriptors for later deserialization */
1283 if (serialization) {
1284 if ((r = fdset_new_fill(&fds)) < 0) {
1285 log_error("Failed to allocate fd set: %s", strerror(-r));
1286 goto finish;
1287 }
1288
1289 assert_se(fdset_remove(fds, fileno(serialization)) >= 0);
1290 } else
1291 close_all_fds(NULL, 0);
1292
09082a94 1293 /* Set up PATH unless it is already set */
e537352b 1294 setenv("PATH",
2c6db6fb 1295#ifdef HAVE_SPLIT_USR
e537352b 1296 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
2c6db6fb
LP
1297#else
1298 "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
1299#endif
fa0f4d8a 1300 arg_running_as == MANAGER_SYSTEM);
09082a94 1301
39439087 1302 if (arg_running_as == MANAGER_SYSTEM) {
e9ddabc2
LP
1303 /* Parse the data passed to us by the initrd and unset it */
1304 parse_initrd_timestamp(&initrd_timestamp);
1305 filter_environ("RD_");
1306
1307 /* Unset some environment variables passed in from the
1308 * kernel that don't really make sense for us. */
39439087
LP
1309 unsetenv("HOME");
1310 unsetenv("TERM");
b770165a
LP
1311
1312 /* All other variables are left as is, so that clients
1313 * can still read them via /proc/1/environ */
39439087 1314 }
1104f3c1 1315
f170852a
LP
1316 /* Move out of the way, so that we won't block unmounts */
1317 assert_se(chdir("/") == 0);
1318
fa0f4d8a 1319 if (arg_running_as == MANAGER_SYSTEM) {
80876c20
LP
1320 /* Become a session leader if we aren't one yet. */
1321 setsid();
4ade7963 1322
80876c20
LP
1323 /* Disable the umask logic */
1324 umask(0);
1325 }
1326
843d2643
LP
1327 /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
1328 dbus_connection_set_change_sigpipe(FALSE);
1329
2146621b
LP
1330 /* Reset the console, but only if this is really init and we
1331 * are freshly booted */
fa0f4d8a 1332 if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
0b3325e7 1333 console_setup(getpid() == 1 && !is_reexec);
843d2643
LP
1334 make_null_stdio();
1335 }
4ade7963 1336
18149b9f 1337 /* Open the logging devices, if possible and necessary */
843d2643 1338 log_open();
4ade7963 1339
5373d602
LP
1340 /* Make sure we leave a core dump without panicing the
1341 * kernel. */
4fc935ca
LP
1342 if (getpid() == 1)
1343 install_crash_handler();
97c4f35c 1344
0c85a4f3
LP
1345 if (geteuid() == 0 && !getenv("SYSTEMD_SKIP_API_MOUNTS")) {
1346 r = mount_cgroup_controllers(arg_join_controllers);
1347 if (r < 0)
1348 goto finish;
1349 }
1350
302e27c8 1351 log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
7d568925 1352 PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
a5dab5ce 1353
0b3325e7 1354 if (arg_running_as == MANAGER_SYSTEM && !is_reexec) {
72bca11b
LP
1355 locale_setup();
1356
6faa1114 1357 if (arg_show_status || plymouth_running())
888c6216
LP
1358 status_welcome();
1359
888c6216
LP
1360 kmod_setup();
1361 hostname_setup();
d7ccca2e 1362 machine_id_setup();
888c6216 1363 loopback_setup();
490aed58 1364
6ee5bbf8 1365 test_mtab();
80758717 1366 test_usr();
871c44a7 1367 test_cgroups();
af5bc85d 1368 }
302e8c4c 1369
9e58ff9c 1370 if ((r = manager_new(arg_running_as, &m)) < 0) {
8e274523 1371 log_error("Failed to allocate manager object: %s", strerror(-r));
60918275
LP
1372 goto finish;
1373 }
1374
9e58ff9c 1375 m->confirm_spawn = arg_confirm_spawn;
07459bb6 1376#ifdef HAVE_SYSV_COMPAT
6e98720f 1377 m->sysv_console = arg_sysv_console;
07459bb6 1378#endif
d3689161 1379 m->mount_auto = arg_mount_auto;
173a8d04 1380 m->swap_auto = arg_swap_auto;
0a494f1f
LP
1381 m->default_std_output = arg_default_std_output;
1382 m->default_std_error = arg_default_std_error;
9e58ff9c 1383
e9ddabc2
LP
1384 if (dual_timestamp_is_set(&initrd_timestamp))
1385 m->initrd_timestamp = initrd_timestamp;
1386
06d4c99a
LP
1387 if (arg_default_controllers)
1388 manager_set_default_controllers(m, arg_default_controllers);
1389
27d340c7
LP
1390 manager_set_show_status(m, arg_show_status);
1391
9d76d730
LP
1392 before_startup = now(CLOCK_MONOTONIC);
1393
a16e1123 1394 if ((r = manager_startup(m, serialization, fds)) < 0)
6e2ef85b 1395 log_error("Failed to fully start up daemon: %s", strerror(-r));
a16e1123
LP
1396
1397 if (fds) {
1398 /* This will close all file descriptors that were opened, but
1399 * not claimed by any unit. */
1400
1401 fdset_free(fds);
1402 fds = NULL;
f50e0a01
LP
1403 }
1404
a16e1123
LP
1405 if (serialization) {
1406 fclose(serialization);
1407 serialization = NULL;
1408 } else {
398ef8ba 1409 DBusError error;
1c27d3f3 1410 Unit *target = NULL;
bacbccb7 1411 Job *default_unit_job;
398ef8ba
LP
1412
1413 dbus_error_init(&error);
1414
fa0f4d8a 1415 log_debug("Activating default unit: %s", arg_default_unit);
a16e1123 1416
398ef8ba
LP
1417 if ((r = manager_load_unit(m, arg_default_unit, NULL, &error, &target)) < 0) {
1418 log_error("Failed to load default target: %s", bus_error(&error, r));
1419 dbus_error_free(&error);
ac155bb8
MS
1420 } else if (target->load_state == UNIT_ERROR)
1421 log_error("Failed to load default target: %s", strerror(-target->load_error));
1422 else if (target->load_state == UNIT_MASKED)
6daf4f90 1423 log_error("Default target masked.");
27b14a22 1424
ac155bb8 1425 if (!target || target->load_state != UNIT_LOADED) {
a16e1123 1426 log_info("Trying to load rescue target...");
1c27d3f3 1427
398ef8ba
LP
1428 if ((r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target)) < 0) {
1429 log_error("Failed to load rescue target: %s", bus_error(&error, r));
1430 dbus_error_free(&error);
a16e1123 1431 goto finish;
ac155bb8
MS
1432 } else if (target->load_state == UNIT_ERROR) {
1433 log_error("Failed to load rescue target: %s", strerror(-target->load_error));
1c27d3f3 1434 goto finish;
ac155bb8 1435 } else if (target->load_state == UNIT_MASKED) {
6daf4f90 1436 log_error("Rescue target masked.");
00dc5d76 1437 goto finish;
a16e1123
LP
1438 }
1439 }
37d88da7 1440
ac155bb8 1441 assert(target->load_state == UNIT_LOADED);
00dc5d76 1442
fa0f4d8a 1443 if (arg_action == ACTION_TEST) {
40d50879 1444 printf("-> By units:\n");
a16e1123
LP
1445 manager_dump_units(m, stdout, "\t");
1446 }
1447
bacbccb7
MS
1448 r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, &default_unit_job);
1449 if (r < 0) {
398ef8ba
LP
1450 log_error("Failed to start default target: %s", bus_error(&error, r));
1451 dbus_error_free(&error);
37d88da7
LP
1452 goto finish;
1453 }
bacbccb7 1454 m->default_unit_job_id = default_unit_job->id;
60918275 1455
07672f49
LP
1456 after_startup = now(CLOCK_MONOTONIC);
1457 log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
1458 "Loaded units and determined initial transaction in %s.",
1459 format_timespan(timespan, sizeof(timespan), after_startup - before_startup));
1460
fa0f4d8a 1461 if (arg_action == ACTION_TEST) {
40d50879 1462 printf("-> By jobs:\n");
a16e1123 1463 manager_dump_jobs(m, stdout, "\t");
22f4096c 1464 retval = EXIT_SUCCESS;
a16e1123
LP
1465 goto finish;
1466 }
e965d56d 1467 }
d46de8a1 1468
a16e1123
LP
1469 for (;;) {
1470 if ((r = manager_loop(m)) < 0) {
1471 log_error("Failed to run mainloop: %s", strerror(-r));
1472 goto finish;
1473 }
11dd41ce 1474
a16e1123 1475 switch (m->exit_code) {
e965d56d 1476
a16e1123 1477 case MANAGER_EXIT:
22f4096c 1478 retval = EXIT_SUCCESS;
a16e1123
LP
1479 log_debug("Exit.");
1480 goto finish;
e965d56d 1481
a16e1123 1482 case MANAGER_RELOAD:
e015090f 1483 log_info("Reloading.");
a16e1123
LP
1484 if ((r = manager_reload(m)) < 0)
1485 log_error("Failed to reload: %s", strerror(-r));
1486 break;
cea8e32e 1487
a16e1123 1488 case MANAGER_REEXECUTE:
a16e1123
LP
1489 if (prepare_reexecute(m, &serialization, &fds) < 0)
1490 goto finish;
60918275 1491
a16e1123 1492 reexecute = true;
e015090f 1493 log_notice("Reexecuting.");
a16e1123
LP
1494 goto finish;
1495
b9080b03
FF
1496 case MANAGER_REBOOT:
1497 case MANAGER_POWEROFF:
1498 case MANAGER_HALT:
1499 case MANAGER_KEXEC: {
1500 static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
1501 [MANAGER_REBOOT] = "reboot",
1502 [MANAGER_POWEROFF] = "poweroff",
1503 [MANAGER_HALT] = "halt",
1504 [MANAGER_KEXEC] = "kexec"
1505 };
1506
1507 assert_se(shutdown_verb = table[m->exit_code]);
1508
1509 log_notice("Shutting down.");
1510 goto finish;
1511 }
1512
a16e1123
LP
1513 default:
1514 assert_not_reached("Unknown exit code.");
1515 }
1516 }
f170852a 1517
60918275
LP
1518finish:
1519 if (m)
1520 manager_free(m);
1521
fa0f4d8a 1522 free(arg_default_unit);
06d4c99a 1523 strv_free(arg_default_controllers);
0c85a4f3 1524 free_join_controllers();
b9cd2ec1 1525
ea430986
LP
1526 dbus_shutdown();
1527
b2bb3dbe
LP
1528 label_finish();
1529
a16e1123 1530 if (reexecute) {
6e98720f 1531 const char *args[15];
a16e1123
LP
1532 unsigned i = 0;
1533 char sfd[16];
1534
1535 assert(serialization);
1536 assert(fds);
1537
1538 args[i++] = SYSTEMD_BINARY_PATH;
1539
1540 args[i++] = "--log-level";
1541 args[i++] = log_level_to_string(log_get_max_level());
1542
1543 args[i++] = "--log-target";
1544 args[i++] = log_target_to_string(log_get_target());
1545
edb9aaa8
LP
1546 if (arg_running_as == MANAGER_SYSTEM)
1547 args[i++] = "--system";
1548 else
af2d49f7 1549 args[i++] = "--user";
edb9aaa8
LP
1550
1551 if (arg_dump_core)
1552 args[i++] = "--dump-core";
1553
1554 if (arg_crash_shell)
1555 args[i++] = "--crash-shell";
1556
1557 if (arg_confirm_spawn)
1558 args[i++] = "--confirm-spawn";
1559
1560 if (arg_show_status)
6e98720f
LP
1561 args[i++] = "--show-status=1";
1562 else
1563 args[i++] = "--show-status=0";
1564
07459bb6 1565#ifdef HAVE_SYSV_COMPAT
6e98720f
LP
1566 if (arg_sysv_console)
1567 args[i++] = "--sysv-console=1";
1568 else
1569 args[i++] = "--sysv-console=0";
07459bb6 1570#endif
a16e1123
LP
1571
1572 snprintf(sfd, sizeof(sfd), "%i", fileno(serialization));
1573 char_array_0(sfd);
1574
1575 args[i++] = "--deserialize";
1576 args[i++] = sfd;
1577
a16e1123
LP
1578 args[i++] = NULL;
1579
1580 assert(i <= ELEMENTSOF(args));
1581
1582 execv(args[0], (char* const*) args);
1583
1584 log_error("Failed to reexecute: %m");
1585 }
1586
1587 if (serialization)
1588 fclose(serialization);
1589
1590 if (fds)
1591 fdset_free(fds);
1592
b9080b03
FF
1593 if (shutdown_verb) {
1594 const char * command_line[] = {
1595 SYSTEMD_SHUTDOWN_BINARY_PATH,
1596 shutdown_verb,
1597 NULL
1598 };
1599
1600 execv(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line);
1601 log_error("Failed to execute shutdown binary, freezing: %m");
1602 }
1603
c3b3c274
LP
1604 if (getpid() == 1)
1605 freeze();
1606
60918275
LP
1607 return retval;
1608}