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