]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/main.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / core / main.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/mount.h>
28 #include <sys/prctl.h>
29 #include <sys/reboot.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #if HAVE_SECCOMP
33 #include <seccomp.h>
34 #endif
35 #if HAVE_VALGRIND_VALGRIND_H
36 #include <valgrind/valgrind.h>
37 #endif
38
39 #include "sd-bus.h"
40 #include "sd-daemon.h"
41
42 #include "alloc-util.h"
43 #include "architecture.h"
44 #include "build.h"
45 #include "bus-error.h"
46 #include "bus-util.h"
47 #include "capability-util.h"
48 #include "clock-util.h"
49 #include "conf-parser.h"
50 #include "cpu-set-util.h"
51 #include "dbus-manager.h"
52 #include "def.h"
53 #include "emergency-action.h"
54 #include "env-util.h"
55 #include "fd-util.h"
56 #include "fdset.h"
57 #include "fileio.h"
58 #include "format-util.h"
59 #include "fs-util.h"
60 #include "hostname-setup.h"
61 #include "ima-setup.h"
62 #include "killall.h"
63 #include "kmod-setup.h"
64 #include "load-fragment.h"
65 #include "log.h"
66 #include "loopback-setup.h"
67 #include "machine-id-setup.h"
68 #include "manager.h"
69 #include "missing.h"
70 #include "mount-setup.h"
71 #include "pager.h"
72 #include "parse-util.h"
73 #include "path-util.h"
74 #include "proc-cmdline.h"
75 #include "process-util.h"
76 #include "raw-clone.h"
77 #include "rlimit-util.h"
78 #if HAVE_SECCOMP
79 #include "seccomp-util.h"
80 #endif
81 #include "selinux-setup.h"
82 #include "selinux-util.h"
83 #include "signal-util.h"
84 #include "smack-setup.h"
85 #include "special.h"
86 #include "stat-util.h"
87 #include "stdio-util.h"
88 #include "strv.h"
89 #include "switch-root.h"
90 #include "terminal-util.h"
91 #include "umask-util.h"
92 #include "user-util.h"
93 #include "virt.h"
94 #include "watchdog.h"
95
96 static enum {
97 ACTION_RUN,
98 ACTION_HELP,
99 ACTION_VERSION,
100 ACTION_TEST,
101 ACTION_DUMP_CONFIGURATION_ITEMS
102 } arg_action = ACTION_RUN;
103 static char *arg_default_unit = NULL;
104 static bool arg_system = false;
105 static bool arg_dump_core = true;
106 static int arg_crash_chvt = -1;
107 static bool arg_crash_shell = false;
108 static bool arg_crash_reboot = false;
109 static char *arg_confirm_spawn = NULL;
110 static ShowStatus arg_show_status = _SHOW_STATUS_UNSET;
111 static bool arg_switched_root = false;
112 static bool arg_no_pager = false;
113 static char ***arg_join_controllers = NULL;
114 static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
115 static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
116 static usec_t arg_default_restart_usec = DEFAULT_RESTART_USEC;
117 static usec_t arg_default_timeout_start_usec = DEFAULT_TIMEOUT_USEC;
118 static usec_t arg_default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
119 static usec_t arg_default_start_limit_interval = DEFAULT_START_LIMIT_INTERVAL;
120 static unsigned arg_default_start_limit_burst = DEFAULT_START_LIMIT_BURST;
121 static usec_t arg_runtime_watchdog = 0;
122 static usec_t arg_shutdown_watchdog = 10 * USEC_PER_MINUTE;
123 static char **arg_default_environment = NULL;
124 static struct rlimit *arg_default_rlimit[_RLIMIT_MAX] = {};
125 static uint64_t arg_capability_bounding_set = CAP_ALL;
126 static nsec_t arg_timer_slack_nsec = NSEC_INFINITY;
127 static usec_t arg_default_timer_accuracy_usec = 1 * USEC_PER_MINUTE;
128 static Set* arg_syscall_archs = NULL;
129 static FILE* arg_serialization = NULL;
130 static bool arg_default_cpu_accounting = false;
131 static bool arg_default_io_accounting = false;
132 static bool arg_default_ip_accounting = false;
133 static bool arg_default_blockio_accounting = false;
134 static bool arg_default_memory_accounting = false;
135 static bool arg_default_tasks_accounting = true;
136 static uint64_t arg_default_tasks_max = UINT64_MAX;
137 static sd_id128_t arg_machine_id = {};
138 static EmergencyAction arg_cad_burst_action = EMERGENCY_ACTION_REBOOT_FORCE;
139
140 noreturn static void freeze_or_reboot(void) {
141
142 if (arg_crash_reboot) {
143 log_notice("Rebooting in 10s...");
144 (void) sleep(10);
145
146 log_notice("Rebooting now...");
147 (void) reboot(RB_AUTOBOOT);
148 log_emergency_errno(errno, "Failed to reboot: %m");
149 }
150
151 log_emergency("Freezing execution.");
152 freeze();
153 }
154
155 noreturn static void crash(int sig) {
156 struct sigaction sa;
157 pid_t pid;
158
159 if (getpid_cached() != 1)
160 /* Pass this on immediately, if this is not PID 1 */
161 (void) raise(sig);
162 else if (!arg_dump_core)
163 log_emergency("Caught <%s>, not dumping core.", signal_to_string(sig));
164 else {
165 sa = (struct sigaction) {
166 .sa_handler = nop_signal_handler,
167 .sa_flags = SA_NOCLDSTOP|SA_RESTART,
168 };
169
170 /* We want to wait for the core process, hence let's enable SIGCHLD */
171 (void) sigaction(SIGCHLD, &sa, NULL);
172
173 pid = raw_clone(SIGCHLD);
174 if (pid < 0)
175 log_emergency_errno(errno, "Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig));
176 else if (pid == 0) {
177 /* Enable default signal handler for core dump */
178
179 sa = (struct sigaction) {
180 .sa_handler = SIG_DFL,
181 };
182 (void) sigaction(sig, &sa, NULL);
183
184 /* Don't limit the coredump size */
185 (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY));
186
187 /* Just to be sure... */
188 (void) chdir("/");
189
190 /* Raise the signal again */
191 pid = raw_getpid();
192 (void) kill(pid, sig); /* raise() would kill the parent */
193
194 assert_not_reached("We shouldn't be here...");
195 _exit(EXIT_FAILURE);
196 } else {
197 siginfo_t status;
198 int r;
199
200 /* Order things nicely. */
201 r = wait_for_terminate(pid, &status);
202 if (r < 0)
203 log_emergency_errno(r, "Caught <%s>, waitpid() failed: %m", signal_to_string(sig));
204 else if (status.si_code != CLD_DUMPED)
205 log_emergency("Caught <%s>, core dump failed (child "PID_FMT", code=%s, status=%i/%s).",
206 signal_to_string(sig),
207 pid, sigchld_code_to_string(status.si_code),
208 status.si_status,
209 strna(status.si_code == CLD_EXITED
210 ? exit_status_to_string(status.si_status, EXIT_STATUS_MINIMAL)
211 : signal_to_string(status.si_status)));
212 else
213 log_emergency("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid);
214 }
215 }
216
217 if (arg_crash_chvt >= 0)
218 (void) chvt(arg_crash_chvt);
219
220 sa = (struct sigaction) {
221 .sa_handler = SIG_IGN,
222 .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART,
223 };
224
225 /* Let the kernel reap children for us */
226 (void) sigaction(SIGCHLD, &sa, NULL);
227
228 if (arg_crash_shell) {
229 log_notice("Executing crash shell in 10s...");
230 (void) sleep(10);
231
232 pid = raw_clone(SIGCHLD);
233 if (pid < 0)
234 log_emergency_errno(errno, "Failed to fork off crash shell: %m");
235 else if (pid == 0) {
236 (void) setsid();
237 (void) make_console_stdio();
238 (void) execle("/bin/sh", "/bin/sh", NULL, environ);
239
240 log_emergency_errno(errno, "execle() failed: %m");
241 _exit(EXIT_FAILURE);
242 } else {
243 log_info("Spawned crash shell as PID "PID_FMT".", pid);
244 (void) wait_for_terminate(pid, NULL);
245 }
246 }
247
248 freeze_or_reboot();
249 }
250
251 static void install_crash_handler(void) {
252 static const struct sigaction sa = {
253 .sa_handler = crash,
254 .sa_flags = SA_NODEFER, /* So that we can raise the signal again from the signal handler */
255 };
256 int r;
257
258 /* We ignore the return value here, since, we don't mind if we
259 * cannot set up a crash handler */
260 r = sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1);
261 if (r < 0)
262 log_debug_errno(r, "I had trouble setting up the crash handler, ignoring: %m");
263 }
264
265 static int console_setup(void) {
266 _cleanup_close_ int tty_fd = -1;
267 int r;
268
269 tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
270 if (tty_fd < 0)
271 return log_error_errno(tty_fd, "Failed to open /dev/console: %m");
272
273 /* We don't want to force text mode. plymouth may be showing
274 * pictures already from initrd. */
275 r = reset_terminal_fd(tty_fd, false);
276 if (r < 0)
277 return log_error_errno(r, "Failed to reset /dev/console: %m");
278
279 return 0;
280 }
281
282 static int parse_crash_chvt(const char *value) {
283 int b;
284
285 if (safe_atoi(value, &arg_crash_chvt) >= 0)
286 return 0;
287
288 b = parse_boolean(value);
289 if (b < 0)
290 return b;
291
292 if (b > 0)
293 arg_crash_chvt = 0; /* switch to where kmsg goes */
294 else
295 arg_crash_chvt = -1; /* turn off switching */
296
297 return 0;
298 }
299
300 static int parse_confirm_spawn(const char *value, char **console) {
301 char *s;
302 int r;
303
304 r = value ? parse_boolean(value) : 1;
305 if (r == 0) {
306 *console = NULL;
307 return 0;
308 }
309
310 if (r > 0) /* on with default tty */
311 s = strdup("/dev/console");
312 else if (is_path(value)) /* on with fully qualified path */
313 s = strdup(value);
314 else /* on with only a tty file name, not a fully qualified path */
315 s = strjoin("/dev/", value);
316 if (!s)
317 return -ENOMEM;
318 *console = s;
319 return 0;
320 }
321
322 static int set_machine_id(const char *m) {
323 sd_id128_t t;
324 assert(m);
325
326 if (sd_id128_from_string(m, &t) < 0)
327 return -EINVAL;
328
329 if (sd_id128_is_null(t))
330 return -EINVAL;
331
332 arg_machine_id = t;
333 return 0;
334 }
335
336 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
337
338 int r;
339
340 assert(key);
341
342 if (STR_IN_SET(key, "systemd.unit", "rd.systemd.unit")) {
343
344 if (proc_cmdline_value_missing(key, value))
345 return 0;
346
347 if (!unit_name_is_valid(value, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
348 log_warning("Unit name specified on %s= is not valid, ignoring: %s", key, value);
349 else if (in_initrd() == !!startswith(key, "rd.")) {
350 if (free_and_strdup(&arg_default_unit, value) < 0)
351 return log_oom();
352 }
353
354 } else if (proc_cmdline_key_streq(key, "systemd.dump_core")) {
355
356 r = value ? parse_boolean(value) : true;
357 if (r < 0)
358 log_warning("Failed to parse dump core switch %s. Ignoring.", value);
359 else
360 arg_dump_core = r;
361
362 } else if (proc_cmdline_key_streq(key, "systemd.crash_chvt")) {
363
364 if (!value)
365 arg_crash_chvt = 0; /* turn on */
366 else if (parse_crash_chvt(value) < 0)
367 log_warning("Failed to parse crash chvt switch %s. Ignoring.", value);
368
369 } else if (proc_cmdline_key_streq(key, "systemd.crash_shell")) {
370
371 r = value ? parse_boolean(value) : true;
372 if (r < 0)
373 log_warning("Failed to parse crash shell switch %s. Ignoring.", value);
374 else
375 arg_crash_shell = r;
376
377 } else if (proc_cmdline_key_streq(key, "systemd.crash_reboot")) {
378
379 r = value ? parse_boolean(value) : true;
380 if (r < 0)
381 log_warning("Failed to parse crash reboot switch %s. Ignoring.", value);
382 else
383 arg_crash_reboot = r;
384
385 } else if (proc_cmdline_key_streq(key, "systemd.confirm_spawn")) {
386 char *s;
387
388 r = parse_confirm_spawn(value, &s);
389 if (r < 0)
390 log_warning_errno(r, "Failed to parse confirm_spawn switch %s. Ignoring.", value);
391 else {
392 free(arg_confirm_spawn);
393 arg_confirm_spawn = s;
394 }
395
396 } else if (proc_cmdline_key_streq(key, "systemd.show_status")) {
397
398 if (value) {
399 r = parse_show_status(value, &arg_show_status);
400 if (r < 0)
401 log_warning("Failed to parse show status switch %s. Ignoring.", value);
402 } else
403 arg_show_status = SHOW_STATUS_YES;
404
405 } else if (proc_cmdline_key_streq(key, "systemd.default_standard_output")) {
406
407 if (proc_cmdline_value_missing(key, value))
408 return 0;
409
410 r = exec_output_from_string(value);
411 if (r < 0)
412 log_warning("Failed to parse default standard output switch %s. Ignoring.", value);
413 else
414 arg_default_std_output = r;
415
416 } else if (proc_cmdline_key_streq(key, "systemd.default_standard_error")) {
417
418 if (proc_cmdline_value_missing(key, value))
419 return 0;
420
421 r = exec_output_from_string(value);
422 if (r < 0)
423 log_warning("Failed to parse default standard error switch %s. Ignoring.", value);
424 else
425 arg_default_std_error = r;
426
427 } else if (streq(key, "systemd.setenv")) {
428
429 if (proc_cmdline_value_missing(key, value))
430 return 0;
431
432 if (env_assignment_is_valid(value)) {
433 char **env;
434
435 env = strv_env_set(arg_default_environment, value);
436 if (!env)
437 return log_oom();
438
439 arg_default_environment = env;
440 } else
441 log_warning("Environment variable name '%s' is not valid. Ignoring.", value);
442
443 } else if (proc_cmdline_key_streq(key, "systemd.machine_id")) {
444
445 if (proc_cmdline_value_missing(key, value))
446 return 0;
447
448 r = set_machine_id(value);
449 if (r < 0)
450 log_warning("MachineID '%s' is not valid. Ignoring.", value);
451
452 } else if (proc_cmdline_key_streq(key, "systemd.default_timeout_start_sec")) {
453
454 if (proc_cmdline_value_missing(key, value))
455 return 0;
456
457 r = parse_sec(value, &arg_default_timeout_start_usec);
458 if (r < 0)
459 log_warning_errno(r, "Failed to parse default start timeout: %s, ignoring.", value);
460
461 if (arg_default_timeout_start_usec <= 0)
462 arg_default_timeout_start_usec = USEC_INFINITY;
463
464 } else if (streq(key, "quiet") && !value) {
465
466 if (arg_show_status == _SHOW_STATUS_UNSET)
467 arg_show_status = SHOW_STATUS_AUTO;
468
469 } else if (streq(key, "debug") && !value) {
470
471 /* Note that log_parse_environment() handles 'debug'
472 * too, and sets the log level to LOG_DEBUG. */
473
474 if (detect_container() > 0)
475 log_set_target(LOG_TARGET_CONSOLE);
476
477 } else if (!value) {
478 const char *target;
479
480 /* SysV compatibility */
481 target = runlevel_to_target(key);
482 if (target)
483 return free_and_strdup(&arg_default_unit, target);
484 }
485
486 return 0;
487 }
488
489 #define DEFINE_SETTER(name, func, descr) \
490 static int name(const char *unit, \
491 const char *filename, \
492 unsigned line, \
493 const char *section, \
494 unsigned section_line, \
495 const char *lvalue, \
496 int ltype, \
497 const char *rvalue, \
498 void *data, \
499 void *userdata) { \
500 \
501 int r; \
502 \
503 assert(filename); \
504 assert(lvalue); \
505 assert(rvalue); \
506 \
507 r = func(rvalue); \
508 if (r < 0) \
509 log_syntax(unit, LOG_ERR, filename, line, r, \
510 "Invalid " descr "'%s': %m", \
511 rvalue); \
512 \
513 return 0; \
514 }
515
516 DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level")
517 DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target")
518 DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" )
519 DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location")
520
521 static int config_parse_cpu_affinity2(
522 const char *unit,
523 const char *filename,
524 unsigned line,
525 const char *section,
526 unsigned section_line,
527 const char *lvalue,
528 int ltype,
529 const char *rvalue,
530 void *data,
531 void *userdata) {
532
533 _cleanup_cpu_free_ cpu_set_t *c = NULL;
534 int ncpus;
535
536 ncpus = parse_cpu_set_and_warn(rvalue, &c, unit, filename, line, lvalue);
537 if (ncpus < 0)
538 return ncpus;
539
540 if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
541 log_warning_errno(errno, "Failed to set CPU affinity: %m");
542
543 return 0;
544 }
545
546 static int config_parse_show_status(
547 const char* unit,
548 const char *filename,
549 unsigned line,
550 const char *section,
551 unsigned section_line,
552 const char *lvalue,
553 int ltype,
554 const char *rvalue,
555 void *data,
556 void *userdata) {
557
558 int k;
559 ShowStatus *b = data;
560
561 assert(filename);
562 assert(lvalue);
563 assert(rvalue);
564 assert(data);
565
566 k = parse_show_status(rvalue, b);
567 if (k < 0) {
568 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse show status setting, ignoring: %s", rvalue);
569 return 0;
570 }
571
572 return 0;
573 }
574
575 static int config_parse_crash_chvt(
576 const char* unit,
577 const char *filename,
578 unsigned line,
579 const char *section,
580 unsigned section_line,
581 const char *lvalue,
582 int ltype,
583 const char *rvalue,
584 void *data,
585 void *userdata) {
586
587 int r;
588
589 assert(filename);
590 assert(lvalue);
591 assert(rvalue);
592
593 r = parse_crash_chvt(rvalue);
594 if (r < 0) {
595 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CrashChangeVT= setting, ignoring: %s", rvalue);
596 return 0;
597 }
598
599 return 0;
600 }
601
602 static int config_parse_join_controllers(const char *unit,
603 const char *filename,
604 unsigned line,
605 const char *section,
606 unsigned section_line,
607 const char *lvalue,
608 int ltype,
609 const char *rvalue,
610 void *data,
611 void *userdata) {
612
613 const char *whole_rvalue = rvalue;
614 unsigned n = 0;
615
616 assert(filename);
617 assert(lvalue);
618 assert(rvalue);
619
620 arg_join_controllers = strv_free_free(arg_join_controllers);
621
622 for (;;) {
623 _cleanup_free_ char *word = NULL;
624 char **l;
625 int r;
626
627 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
628 if (r < 0) {
629 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
630 return r;
631 }
632 if (r == 0)
633 break;
634
635 l = strv_split(word, ",");
636 if (!l)
637 return log_oom();
638 strv_uniq(l);
639
640 if (strv_length(l) <= 1) {
641 strv_free(l);
642 continue;
643 }
644
645 if (!arg_join_controllers) {
646 arg_join_controllers = new(char**, 2);
647 if (!arg_join_controllers) {
648 strv_free(l);
649 return log_oom();
650 }
651
652 arg_join_controllers[0] = l;
653 arg_join_controllers[1] = NULL;
654
655 n = 1;
656 } else {
657 char ***a;
658 char ***t;
659
660 t = new0(char**, n+2);
661 if (!t) {
662 strv_free(l);
663 return log_oom();
664 }
665
666 n = 0;
667
668 for (a = arg_join_controllers; *a; a++) {
669
670 if (strv_overlap(*a, l)) {
671 if (strv_extend_strv(&l, *a, false) < 0) {
672 strv_free(l);
673 strv_free_free(t);
674 return log_oom();
675 }
676
677 } else {
678 char **c;
679
680 c = strv_copy(*a);
681 if (!c) {
682 strv_free(l);
683 strv_free_free(t);
684 return log_oom();
685 }
686
687 t[n++] = c;
688 }
689 }
690
691 t[n++] = strv_uniq(l);
692
693 strv_free_free(arg_join_controllers);
694 arg_join_controllers = t;
695 }
696 }
697 if (!isempty(rvalue))
698 log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
699
700 return 0;
701 }
702
703 static int parse_config_file(void) {
704
705 const ConfigTableItem items[] = {
706 { "Manager", "LogLevel", config_parse_level2, 0, NULL },
707 { "Manager", "LogTarget", config_parse_target, 0, NULL },
708 { "Manager", "LogColor", config_parse_color, 0, NULL },
709 { "Manager", "LogLocation", config_parse_location, 0, NULL },
710 { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core },
711 { "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt, 0, NULL },
712 { "Manager", "CrashChangeVT", config_parse_crash_chvt, 0, NULL },
713 { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell },
714 { "Manager", "CrashReboot", config_parse_bool, 0, &arg_crash_reboot },
715 { "Manager", "ShowStatus", config_parse_show_status, 0, &arg_show_status },
716 { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, NULL },
717 { "Manager", "JoinControllers", config_parse_join_controllers, 0, &arg_join_controllers },
718 { "Manager", "RuntimeWatchdogSec", config_parse_sec, 0, &arg_runtime_watchdog },
719 { "Manager", "ShutdownWatchdogSec", config_parse_sec, 0, &arg_shutdown_watchdog },
720 { "Manager", "CapabilityBoundingSet", config_parse_capability_set, 0, &arg_capability_bounding_set },
721 #if HAVE_SECCOMP
722 { "Manager", "SystemCallArchitectures", config_parse_syscall_archs, 0, &arg_syscall_archs },
723 #endif
724 { "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec },
725 { "Manager", "DefaultTimerAccuracySec", config_parse_sec, 0, &arg_default_timer_accuracy_usec },
726 { "Manager", "DefaultStandardOutput", config_parse_output, 0, &arg_default_std_output },
727 { "Manager", "DefaultStandardError", config_parse_output, 0, &arg_default_std_error },
728 { "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_default_timeout_start_usec },
729 { "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_default_timeout_stop_usec },
730 { "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_default_restart_usec },
731 { "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_default_start_limit_interval }, /* obsolete alias */
732 { "Manager", "DefaultStartLimitIntervalSec",config_parse_sec, 0, &arg_default_start_limit_interval },
733 { "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_default_start_limit_burst },
734 { "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment },
735 { "Manager", "DefaultLimitCPU", config_parse_limit, RLIMIT_CPU, arg_default_rlimit },
736 { "Manager", "DefaultLimitFSIZE", config_parse_limit, RLIMIT_FSIZE, arg_default_rlimit },
737 { "Manager", "DefaultLimitDATA", config_parse_limit, RLIMIT_DATA, arg_default_rlimit },
738 { "Manager", "DefaultLimitSTACK", config_parse_limit, RLIMIT_STACK, arg_default_rlimit },
739 { "Manager", "DefaultLimitCORE", config_parse_limit, RLIMIT_CORE, arg_default_rlimit },
740 { "Manager", "DefaultLimitRSS", config_parse_limit, RLIMIT_RSS, arg_default_rlimit },
741 { "Manager", "DefaultLimitNOFILE", config_parse_limit, RLIMIT_NOFILE, arg_default_rlimit },
742 { "Manager", "DefaultLimitAS", config_parse_limit, RLIMIT_AS, arg_default_rlimit },
743 { "Manager", "DefaultLimitNPROC", config_parse_limit, RLIMIT_NPROC, arg_default_rlimit },
744 { "Manager", "DefaultLimitMEMLOCK", config_parse_limit, RLIMIT_MEMLOCK, arg_default_rlimit },
745 { "Manager", "DefaultLimitLOCKS", config_parse_limit, RLIMIT_LOCKS, arg_default_rlimit },
746 { "Manager", "DefaultLimitSIGPENDING", config_parse_limit, RLIMIT_SIGPENDING, arg_default_rlimit },
747 { "Manager", "DefaultLimitMSGQUEUE", config_parse_limit, RLIMIT_MSGQUEUE, arg_default_rlimit },
748 { "Manager", "DefaultLimitNICE", config_parse_limit, RLIMIT_NICE, arg_default_rlimit },
749 { "Manager", "DefaultLimitRTPRIO", config_parse_limit, RLIMIT_RTPRIO, arg_default_rlimit },
750 { "Manager", "DefaultLimitRTTIME", config_parse_limit, RLIMIT_RTTIME, arg_default_rlimit },
751 { "Manager", "DefaultCPUAccounting", config_parse_bool, 0, &arg_default_cpu_accounting },
752 { "Manager", "DefaultIOAccounting", config_parse_bool, 0, &arg_default_io_accounting },
753 { "Manager", "DefaultIPAccounting", config_parse_bool, 0, &arg_default_ip_accounting },
754 { "Manager", "DefaultBlockIOAccounting", config_parse_bool, 0, &arg_default_blockio_accounting },
755 { "Manager", "DefaultMemoryAccounting", config_parse_bool, 0, &arg_default_memory_accounting },
756 { "Manager", "DefaultTasksAccounting", config_parse_bool, 0, &arg_default_tasks_accounting },
757 { "Manager", "DefaultTasksMax", config_parse_tasks_max, 0, &arg_default_tasks_max },
758 { "Manager", "CtrlAltDelBurstAction", config_parse_emergency_action, 0, &arg_cad_burst_action },
759 {}
760 };
761
762 const char *fn, *conf_dirs_nulstr;
763
764 fn = arg_system ?
765 PKGSYSCONFDIR "/system.conf" :
766 PKGSYSCONFDIR "/user.conf";
767
768 conf_dirs_nulstr = arg_system ?
769 CONF_PATHS_NULSTR("systemd/system.conf.d") :
770 CONF_PATHS_NULSTR("systemd/user.conf.d");
771
772 (void) config_parse_many_nulstr(fn, conf_dirs_nulstr, "Manager\0", config_item_table_lookup, items, CONFIG_PARSE_WARN, NULL);
773
774 /* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we used USEC_INFINITY
775 * like everywhere else. */
776 if (arg_default_timeout_start_usec <= 0)
777 arg_default_timeout_start_usec = USEC_INFINITY;
778 if (arg_default_timeout_stop_usec <= 0)
779 arg_default_timeout_stop_usec = USEC_INFINITY;
780
781 return 0;
782 }
783
784 static void set_manager_defaults(Manager *m) {
785
786 assert(m);
787
788 m->default_timer_accuracy_usec = arg_default_timer_accuracy_usec;
789 m->default_std_output = arg_default_std_output;
790 m->default_std_error = arg_default_std_error;
791 m->default_timeout_start_usec = arg_default_timeout_start_usec;
792 m->default_timeout_stop_usec = arg_default_timeout_stop_usec;
793 m->default_restart_usec = arg_default_restart_usec;
794 m->default_start_limit_interval = arg_default_start_limit_interval;
795 m->default_start_limit_burst = arg_default_start_limit_burst;
796 m->default_cpu_accounting = arg_default_cpu_accounting;
797 m->default_io_accounting = arg_default_io_accounting;
798 m->default_ip_accounting = arg_default_ip_accounting;
799 m->default_blockio_accounting = arg_default_blockio_accounting;
800 m->default_memory_accounting = arg_default_memory_accounting;
801 m->default_tasks_accounting = arg_default_tasks_accounting;
802 m->default_tasks_max = arg_default_tasks_max;
803
804 manager_set_default_rlimits(m, arg_default_rlimit);
805 manager_environment_add(m, NULL, arg_default_environment);
806 }
807
808 static void set_manager_settings(Manager *m) {
809
810 assert(m);
811
812 m->confirm_spawn = arg_confirm_spawn;
813 m->runtime_watchdog = arg_runtime_watchdog;
814 m->shutdown_watchdog = arg_shutdown_watchdog;
815 m->cad_burst_action = arg_cad_burst_action;
816
817 manager_set_show_status(m, arg_show_status);
818 }
819
820 static int parse_argv(int argc, char *argv[]) {
821
822 enum {
823 ARG_LOG_LEVEL = 0x100,
824 ARG_LOG_TARGET,
825 ARG_LOG_COLOR,
826 ARG_LOG_LOCATION,
827 ARG_UNIT,
828 ARG_SYSTEM,
829 ARG_USER,
830 ARG_TEST,
831 ARG_NO_PAGER,
832 ARG_VERSION,
833 ARG_DUMP_CONFIGURATION_ITEMS,
834 ARG_DUMP_CORE,
835 ARG_CRASH_CHVT,
836 ARG_CRASH_SHELL,
837 ARG_CRASH_REBOOT,
838 ARG_CONFIRM_SPAWN,
839 ARG_SHOW_STATUS,
840 ARG_DESERIALIZE,
841 ARG_SWITCHED_ROOT,
842 ARG_DEFAULT_STD_OUTPUT,
843 ARG_DEFAULT_STD_ERROR,
844 ARG_MACHINE_ID
845 };
846
847 static const struct option options[] = {
848 { "log-level", required_argument, NULL, ARG_LOG_LEVEL },
849 { "log-target", required_argument, NULL, ARG_LOG_TARGET },
850 { "log-color", optional_argument, NULL, ARG_LOG_COLOR },
851 { "log-location", optional_argument, NULL, ARG_LOG_LOCATION },
852 { "unit", required_argument, NULL, ARG_UNIT },
853 { "system", no_argument, NULL, ARG_SYSTEM },
854 { "user", no_argument, NULL, ARG_USER },
855 { "test", no_argument, NULL, ARG_TEST },
856 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
857 { "help", no_argument, NULL, 'h' },
858 { "version", no_argument, NULL, ARG_VERSION },
859 { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS },
860 { "dump-core", optional_argument, NULL, ARG_DUMP_CORE },
861 { "crash-chvt", required_argument, NULL, ARG_CRASH_CHVT },
862 { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL },
863 { "crash-reboot", optional_argument, NULL, ARG_CRASH_REBOOT },
864 { "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN },
865 { "show-status", optional_argument, NULL, ARG_SHOW_STATUS },
866 { "deserialize", required_argument, NULL, ARG_DESERIALIZE },
867 { "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT },
868 { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
869 { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
870 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
871 {}
872 };
873
874 int c, r;
875
876 assert(argc >= 1);
877 assert(argv);
878
879 if (getpid_cached() == 1)
880 opterr = 0;
881
882 while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0)
883
884 switch (c) {
885
886 case ARG_LOG_LEVEL:
887 r = log_set_max_level_from_string(optarg);
888 if (r < 0) {
889 log_error("Failed to parse log level %s.", optarg);
890 return r;
891 }
892
893 break;
894
895 case ARG_LOG_TARGET:
896 r = log_set_target_from_string(optarg);
897 if (r < 0) {
898 log_error("Failed to parse log target %s.", optarg);
899 return r;
900 }
901
902 break;
903
904 case ARG_LOG_COLOR:
905
906 if (optarg) {
907 r = log_show_color_from_string(optarg);
908 if (r < 0) {
909 log_error("Failed to parse log color setting %s.", optarg);
910 return r;
911 }
912 } else
913 log_show_color(true);
914
915 break;
916
917 case ARG_LOG_LOCATION:
918 if (optarg) {
919 r = log_show_location_from_string(optarg);
920 if (r < 0) {
921 log_error("Failed to parse log location setting %s.", optarg);
922 return r;
923 }
924 } else
925 log_show_location(true);
926
927 break;
928
929 case ARG_DEFAULT_STD_OUTPUT:
930 r = exec_output_from_string(optarg);
931 if (r < 0) {
932 log_error("Failed to parse default standard output setting %s.", optarg);
933 return r;
934 } else
935 arg_default_std_output = r;
936 break;
937
938 case ARG_DEFAULT_STD_ERROR:
939 r = exec_output_from_string(optarg);
940 if (r < 0) {
941 log_error("Failed to parse default standard error output setting %s.", optarg);
942 return r;
943 } else
944 arg_default_std_error = r;
945 break;
946
947 case ARG_UNIT:
948 r = free_and_strdup(&arg_default_unit, optarg);
949 if (r < 0)
950 return log_error_errno(r, "Failed to set default unit %s: %m", optarg);
951
952 break;
953
954 case ARG_SYSTEM:
955 arg_system = true;
956 break;
957
958 case ARG_USER:
959 arg_system = false;
960 break;
961
962 case ARG_TEST:
963 arg_action = ACTION_TEST;
964 break;
965
966 case ARG_NO_PAGER:
967 arg_no_pager = true;
968 break;
969
970 case ARG_VERSION:
971 arg_action = ACTION_VERSION;
972 break;
973
974 case ARG_DUMP_CONFIGURATION_ITEMS:
975 arg_action = ACTION_DUMP_CONFIGURATION_ITEMS;
976 break;
977
978 case ARG_DUMP_CORE:
979 if (!optarg)
980 arg_dump_core = true;
981 else {
982 r = parse_boolean(optarg);
983 if (r < 0)
984 return log_error_errno(r, "Failed to parse dump core boolean: %s", optarg);
985 arg_dump_core = r;
986 }
987 break;
988
989 case ARG_CRASH_CHVT:
990 r = parse_crash_chvt(optarg);
991 if (r < 0)
992 return log_error_errno(r, "Failed to parse crash virtual terminal index: %s", optarg);
993 break;
994
995 case ARG_CRASH_SHELL:
996 if (!optarg)
997 arg_crash_shell = true;
998 else {
999 r = parse_boolean(optarg);
1000 if (r < 0)
1001 return log_error_errno(r, "Failed to parse crash shell boolean: %s", optarg);
1002 arg_crash_shell = r;
1003 }
1004 break;
1005
1006 case ARG_CRASH_REBOOT:
1007 if (!optarg)
1008 arg_crash_reboot = true;
1009 else {
1010 r = parse_boolean(optarg);
1011 if (r < 0)
1012 return log_error_errno(r, "Failed to parse crash shell boolean: %s", optarg);
1013 arg_crash_reboot = r;
1014 }
1015 break;
1016
1017 case ARG_CONFIRM_SPAWN:
1018 arg_confirm_spawn = mfree(arg_confirm_spawn);
1019
1020 r = parse_confirm_spawn(optarg, &arg_confirm_spawn);
1021 if (r < 0)
1022 return log_error_errno(r, "Failed to parse confirm spawn option: %m");
1023 break;
1024
1025 case ARG_SHOW_STATUS:
1026 if (optarg) {
1027 r = parse_show_status(optarg, &arg_show_status);
1028 if (r < 0) {
1029 log_error("Failed to parse show status boolean %s.", optarg);
1030 return r;
1031 }
1032 } else
1033 arg_show_status = SHOW_STATUS_YES;
1034 break;
1035
1036 case ARG_DESERIALIZE: {
1037 int fd;
1038 FILE *f;
1039
1040 r = safe_atoi(optarg, &fd);
1041 if (r < 0 || fd < 0) {
1042 log_error("Failed to parse deserialize option %s.", optarg);
1043 return -EINVAL;
1044 }
1045
1046 (void) fd_cloexec(fd, true);
1047
1048 f = fdopen(fd, "r");
1049 if (!f)
1050 return log_error_errno(errno, "Failed to open serialization fd: %m");
1051
1052 safe_fclose(arg_serialization);
1053 arg_serialization = f;
1054
1055 break;
1056 }
1057
1058 case ARG_SWITCHED_ROOT:
1059 arg_switched_root = true;
1060 break;
1061
1062 case ARG_MACHINE_ID:
1063 r = set_machine_id(optarg);
1064 if (r < 0)
1065 return log_error_errno(r, "MachineID '%s' is not valid.", optarg);
1066 break;
1067
1068 case 'h':
1069 arg_action = ACTION_HELP;
1070 break;
1071
1072 case 'D':
1073 log_set_max_level(LOG_DEBUG);
1074 break;
1075
1076 case 'b':
1077 case 's':
1078 case 'z':
1079 /* Just to eat away the sysvinit kernel
1080 * cmdline args without getopt() error
1081 * messages that we'll parse in
1082 * parse_proc_cmdline_word() or ignore. */
1083
1084 case '?':
1085 if (getpid_cached() != 1)
1086 return -EINVAL;
1087 else
1088 return 0;
1089
1090 default:
1091 assert_not_reached("Unhandled option code.");
1092 }
1093
1094 if (optind < argc && getpid_cached() != 1) {
1095 /* Hmm, when we aren't run as init system
1096 * let's complain about excess arguments */
1097
1098 log_error("Excess arguments.");
1099 return -EINVAL;
1100 }
1101
1102 return 0;
1103 }
1104
1105 static int help(void) {
1106
1107 printf("%s [OPTIONS...]\n\n"
1108 "Starts up and maintains the system or user services.\n\n"
1109 " -h --help Show this help\n"
1110 " --version Show version\n"
1111 " --test Determine startup sequence, dump it and exit\n"
1112 " --no-pager Do not pipe output into a pager\n"
1113 " --dump-configuration-items Dump understood unit configuration items\n"
1114 " --unit=UNIT Set default unit\n"
1115 " --system Run a system instance, even if PID != 1\n"
1116 " --user Run a user instance\n"
1117 " --dump-core[=BOOL] Dump core on crash\n"
1118 " --crash-vt=NR Change to specified VT on crash\n"
1119 " --crash-reboot[=BOOL] Reboot on crash\n"
1120 " --crash-shell[=BOOL] Run shell on crash\n"
1121 " --confirm-spawn[=BOOL] Ask for confirmation when spawning processes\n"
1122 " --show-status[=BOOL] Show status updates on the console during bootup\n"
1123 " --log-target=TARGET Set log target (console, journal, kmsg, journal-or-kmsg, null)\n"
1124 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
1125 " --log-color[=BOOL] Highlight important log messages\n"
1126 " --log-location[=BOOL] Include code location in log messages\n"
1127 " --default-standard-output= Set default standard output for services\n"
1128 " --default-standard-error= Set default standard error output for services\n",
1129 program_invocation_short_name);
1130
1131 return 0;
1132 }
1133
1134 static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds, bool switching_root) {
1135 _cleanup_fdset_free_ FDSet *fds = NULL;
1136 _cleanup_fclose_ FILE *f = NULL;
1137 int r;
1138
1139 assert(m);
1140 assert(_f);
1141 assert(_fds);
1142
1143 r = manager_open_serialization(m, &f);
1144 if (r < 0)
1145 return log_error_errno(r, "Failed to create serialization file: %m");
1146
1147 /* Make sure nothing is really destructed when we shut down */
1148 m->n_reloading++;
1149 bus_manager_send_reloading(m, true);
1150
1151 fds = fdset_new();
1152 if (!fds)
1153 return log_oom();
1154
1155 r = manager_serialize(m, f, fds, switching_root);
1156 if (r < 0)
1157 return log_error_errno(r, "Failed to serialize state: %m");
1158
1159 if (fseeko(f, 0, SEEK_SET) == (off_t) -1)
1160 return log_error_errno(errno, "Failed to rewind serialization fd: %m");
1161
1162 r = fd_cloexec(fileno(f), false);
1163 if (r < 0)
1164 return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization: %m");
1165
1166 r = fdset_cloexec(fds, false);
1167 if (r < 0)
1168 return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization fds: %m");
1169
1170 *_f = f;
1171 *_fds = fds;
1172
1173 f = NULL;
1174 fds = NULL;
1175
1176 return 0;
1177 }
1178
1179 static int bump_rlimit_nofile(struct rlimit *saved_rlimit) {
1180 struct rlimit nl;
1181 int r;
1182 int min_max;
1183 _cleanup_free_ char *nr_open = NULL;
1184
1185 assert(saved_rlimit);
1186
1187 /* Save the original RLIMIT_NOFILE so that we can reset it
1188 * later when transitioning from the initrd to the main
1189 * systemd or suchlike. */
1190 if (getrlimit(RLIMIT_NOFILE, saved_rlimit) < 0)
1191 return log_warning_errno(errno, "Reading RLIMIT_NOFILE failed, ignoring: %m");
1192
1193 /* Make sure forked processes get the default kernel setting */
1194 if (!arg_default_rlimit[RLIMIT_NOFILE]) {
1195 struct rlimit *rl;
1196
1197 rl = newdup(struct rlimit, saved_rlimit, 1);
1198 if (!rl)
1199 return log_oom();
1200
1201 arg_default_rlimit[RLIMIT_NOFILE] = rl;
1202 }
1203
1204 /* Get current RLIMIT_NOFILE maximum compiled into the kernel. */
1205 r = read_one_line_file("/proc/sys/fs/nr_open", &nr_open);
1206 if (r == 0)
1207 r = safe_atoi(nr_open, &min_max);
1208 /* If we fail, fallback to the hard-coded kernel limit of 1024 * 1024. */
1209 if (r < 0)
1210 min_max = 1024 * 1024;
1211
1212 /* Bump up the resource limit for ourselves substantially */
1213 nl.rlim_cur = nl.rlim_max = min_max;
1214 r = setrlimit_closest(RLIMIT_NOFILE, &nl);
1215 if (r < 0)
1216 return log_warning_errno(r, "Setting RLIMIT_NOFILE failed, ignoring: %m");
1217
1218 return 0;
1219 }
1220
1221 static int bump_rlimit_memlock(struct rlimit *saved_rlimit) {
1222 int r;
1223
1224 assert(saved_rlimit);
1225 assert(getuid() == 0);
1226
1227 /* BPF_MAP_TYPE_LPM_TRIE bpf maps are charged against RLIMIT_MEMLOCK, even though we have CAP_IPC_LOCK which
1228 * should normally disable such checks. We need them to implement IPAccessAllow= and IPAccessDeny=, hence let's
1229 * bump the value high enough for the root user. */
1230
1231 if (getrlimit(RLIMIT_MEMLOCK, saved_rlimit) < 0)
1232 return log_warning_errno(errno, "Reading RLIMIT_MEMLOCK failed, ignoring: %m");
1233
1234 r = setrlimit_closest(RLIMIT_MEMLOCK, &RLIMIT_MAKE_CONST(1024ULL*1024ULL*16ULL));
1235 if (r < 0)
1236 return log_warning_errno(r, "Setting RLIMIT_MEMLOCK failed, ignoring: %m");
1237
1238 return 0;
1239 }
1240
1241 static void test_usr(void) {
1242
1243 /* Check that /usr is not a separate fs */
1244
1245 if (dir_is_empty("/usr") <= 0)
1246 return;
1247
1248 log_warning("/usr appears to be on its own filesystem and is not already mounted. This is not a supported setup. "
1249 "Some things will probably break (sometimes even silently) in mysterious ways. "
1250 "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
1251 }
1252
1253 static int initialize_join_controllers(void) {
1254 /* By default, mount "cpu" + "cpuacct" together, and "net_cls"
1255 * + "net_prio". We'd like to add "cpuset" to the mix, but
1256 * "cpuset" doesn't really work for groups with no initialized
1257 * attributes. */
1258
1259 arg_join_controllers = new(char**, 3);
1260 if (!arg_join_controllers)
1261 return -ENOMEM;
1262
1263 arg_join_controllers[0] = strv_new("cpu", "cpuacct", NULL);
1264 if (!arg_join_controllers[0])
1265 goto oom;
1266
1267 arg_join_controllers[1] = strv_new("net_cls", "net_prio", NULL);
1268 if (!arg_join_controllers[1])
1269 goto oom;
1270
1271 arg_join_controllers[2] = NULL;
1272 return 0;
1273
1274 oom:
1275 arg_join_controllers = strv_free_free(arg_join_controllers);
1276 return -ENOMEM;
1277 }
1278
1279 static int enforce_syscall_archs(Set *archs) {
1280 #if HAVE_SECCOMP
1281 int r;
1282
1283 if (!is_seccomp_available())
1284 return 0;
1285
1286 r = seccomp_restrict_archs(arg_syscall_archs);
1287 if (r < 0)
1288 return log_error_errno(r, "Failed to enforce system call architecture restrication: %m");
1289 #endif
1290 return 0;
1291 }
1292
1293 static int status_welcome(void) {
1294 _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL;
1295 int r;
1296
1297 r = parse_env_file("/etc/os-release", NEWLINE,
1298 "PRETTY_NAME", &pretty_name,
1299 "ANSI_COLOR", &ansi_color,
1300 NULL);
1301 if (r == -ENOENT)
1302 r = parse_env_file("/usr/lib/os-release", NEWLINE,
1303 "PRETTY_NAME", &pretty_name,
1304 "ANSI_COLOR", &ansi_color,
1305 NULL);
1306
1307 if (r < 0 && r != -ENOENT)
1308 log_warning_errno(r, "Failed to read os-release file: %m");
1309
1310 if (log_get_show_color())
1311 return status_printf(NULL, false, false,
1312 "\nWelcome to \x1B[%sm%s\x1B[0m!\n",
1313 isempty(ansi_color) ? "1" : ansi_color,
1314 isempty(pretty_name) ? "Linux" : pretty_name);
1315 else
1316 return status_printf(NULL, false, false,
1317 "\nWelcome to %s!\n",
1318 isempty(pretty_name) ? "Linux" : pretty_name);
1319 }
1320
1321 static int write_container_id(void) {
1322 const char *c;
1323 int r;
1324
1325 c = getenv("container");
1326 if (isempty(c))
1327 return 0;
1328
1329 RUN_WITH_UMASK(0022)
1330 r = write_string_file("/run/systemd/container", c, WRITE_STRING_FILE_CREATE);
1331 if (r < 0)
1332 return log_warning_errno(r, "Failed to write /run/systemd/container, ignoring: %m");
1333
1334 return 1;
1335 }
1336
1337 static int bump_unix_max_dgram_qlen(void) {
1338 _cleanup_free_ char *qlen = NULL;
1339 unsigned long v;
1340 int r;
1341
1342 /* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel
1343 * default of 16 is simply too low. We set the value really
1344 * really early during boot, so that it is actually applied to
1345 * all our sockets, including the $NOTIFY_SOCKET one. */
1346
1347 r = read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen);
1348 if (r < 0)
1349 return log_warning_errno(r, "Failed to read AF_UNIX datagram queue length, ignoring: %m");
1350
1351 r = safe_atolu(qlen, &v);
1352 if (r < 0)
1353 return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length, ignoring: %m");
1354
1355 if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN)
1356 return 0;
1357
1358 qlen = mfree(qlen);
1359 if (asprintf(&qlen, "%lu\n", DEFAULT_UNIX_MAX_DGRAM_QLEN) < 0)
1360 return log_oom();
1361
1362 r = write_string_file("/proc/sys/net/unix/max_dgram_qlen", qlen, 0);
1363 if (r < 0)
1364 return log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
1365 "Failed to bump AF_UNIX datagram queue length, ignoring: %m");
1366
1367 return 1;
1368 }
1369
1370 static int fixup_environment(void) {
1371 _cleanup_free_ char *term = NULL;
1372 int r;
1373
1374 /* We expect the environment to be set correctly
1375 * if run inside a container. */
1376 if (detect_container() > 0)
1377 return 0;
1378
1379 /* When started as PID1, the kernel uses /dev/console
1380 * for our stdios and uses TERM=linux whatever the
1381 * backend device used by the console. We try to make
1382 * a better guess here since some consoles might not
1383 * have support for color mode for example.
1384 *
1385 * However if TERM was configured through the kernel
1386 * command line then leave it alone. */
1387
1388 r = proc_cmdline_get_key("TERM", 0, &term);
1389 if (r < 0)
1390 return r;
1391 if (r == 0) {
1392 term = strdup(default_term_for_tty("/dev/console"));
1393 if (!term)
1394 return -ENOMEM;
1395 }
1396
1397 if (setenv("TERM", term, 1) < 0)
1398 return -errno;
1399
1400 return 0;
1401 }
1402
1403 static void redirect_telinit(int argc, char *argv[]) {
1404
1405 /* This is compatibility support for SysV, where calling init as a user is identical to telinit. */
1406
1407 #if HAVE_SYSV_COMPAT
1408 if (getpid_cached() == 1)
1409 return;
1410
1411 if (!strstr(program_invocation_short_name, "init"))
1412 return;
1413
1414 execv(SYSTEMCTL_BINARY_PATH, argv);
1415 log_error_errno(errno, "Failed to exec " SYSTEMCTL_BINARY_PATH ": %m");
1416 exit(1);
1417 #endif
1418 }
1419
1420 static int become_shutdown(
1421 const char *shutdown_verb,
1422 int retval,
1423 bool arm_reboot_watchdog) {
1424
1425 char log_level[DECIMAL_STR_MAX(int) + 1],
1426 exit_code[DECIMAL_STR_MAX(uint8_t) + 1];
1427
1428 const char* command_line[11] = {
1429 SYSTEMD_SHUTDOWN_BINARY_PATH,
1430 shutdown_verb,
1431 "--log-level", log_level,
1432 "--log-target",
1433 };
1434
1435 _cleanup_strv_free_ char **env_block = NULL;
1436 size_t pos = 5;
1437 int r;
1438
1439 assert(command_line[pos] == NULL);
1440 env_block = strv_copy(environ);
1441
1442 xsprintf(log_level, "%d", log_get_max_level());
1443
1444 switch (log_get_target()) {
1445
1446 case LOG_TARGET_KMSG:
1447 case LOG_TARGET_JOURNAL_OR_KMSG:
1448 case LOG_TARGET_SYSLOG_OR_KMSG:
1449 command_line[pos++] = "kmsg";
1450 break;
1451
1452 case LOG_TARGET_NULL:
1453 command_line[pos++] = "null";
1454 break;
1455
1456 case LOG_TARGET_CONSOLE:
1457 default:
1458 command_line[pos++] = "console";
1459 break;
1460 };
1461
1462 if (log_get_show_color())
1463 command_line[pos++] = "--log-color";
1464
1465 if (log_get_show_location())
1466 command_line[pos++] = "--log-location";
1467
1468 if (streq(shutdown_verb, "exit")) {
1469 command_line[pos++] = "--exit-code";
1470 command_line[pos++] = exit_code;
1471 xsprintf(exit_code, "%d", retval);
1472 }
1473
1474 assert(pos < ELEMENTSOF(command_line));
1475
1476 if (arm_reboot_watchdog && arg_shutdown_watchdog > 0 && arg_shutdown_watchdog != USEC_INFINITY) {
1477 char *e;
1478
1479 /* If we reboot let's set the shutdown
1480 * watchdog and tell the shutdown binary to
1481 * repeatedly ping it */
1482 r = watchdog_set_timeout(&arg_shutdown_watchdog);
1483 watchdog_close(r < 0);
1484
1485 /* Tell the binary how often to ping, ignore failure */
1486 if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0)
1487 (void) strv_push(&env_block, e);
1488 } else
1489 watchdog_close(true);
1490
1491 /* Avoid the creation of new processes forked by the
1492 * kernel; at this point, we will not listen to the
1493 * signals anyway */
1494 if (detect_container() <= 0)
1495 (void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
1496
1497 execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
1498 return -errno;
1499 }
1500
1501 static void initialize_clock(void) {
1502 int r;
1503
1504 if (clock_is_localtime(NULL) > 0) {
1505 int min;
1506
1507 /*
1508 * The very first call of settimeofday() also does a time warp in the kernel.
1509 *
1510 * In the rtc-in-local time mode, we set the kernel's timezone, and rely on external tools to take care
1511 * of maintaining the RTC and do all adjustments. This matches the behavior of Windows, which leaves
1512 * the RTC alone if the registry tells that the RTC runs in UTC.
1513 */
1514 r = clock_set_timezone(&min);
1515 if (r < 0)
1516 log_error_errno(r, "Failed to apply local time delta, ignoring: %m");
1517 else
1518 log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min);
1519
1520 } else if (!in_initrd()) {
1521 /*
1522 * Do a dummy very first call to seal the kernel's time warp magic.
1523 *
1524 * Do not call this from inside the initrd. The initrd might not carry /etc/adjtime with LOCAL, but the
1525 * real system could be set up that way. In such case, we need to delay the time-warp or the sealing
1526 * until we reach the real system.
1527 *
1528 * Do no set the kernel's timezone. The concept of local time cannot be supported reliably, the time
1529 * will jump or be incorrect at every daylight saving time change. All kernel local time concepts will
1530 * be treated as UTC that way.
1531 */
1532 (void) clock_reset_timewarp();
1533 }
1534
1535 r = clock_apply_epoch();
1536 if (r < 0)
1537 log_error_errno(r, "Current system time is before build time, but cannot correct: %m");
1538 else if (r > 0)
1539 log_info("System time before build time, advancing clock.");
1540 }
1541
1542 static void initialize_coredump(bool skip_setup) {
1543
1544 if (getpid_cached() != 1)
1545 return;
1546
1547 /* Don't limit the core dump size, so that coredump handlers such as systemd-coredump (which honour the limit)
1548 * will process core dumps for system services by default. */
1549 if (setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0)
1550 log_warning_errno(errno, "Failed to set RLIMIT_CORE: %m");
1551
1552 /* But at the same time, turn off the core_pattern logic by default, so that no coredumps are stored
1553 * until the systemd-coredump tool is enabled via sysctl. */
1554 if (!skip_setup)
1555 (void) write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0);
1556 }
1557
1558 int main(int argc, char *argv[]) {
1559 Manager *m = NULL;
1560 int r, retval = EXIT_FAILURE;
1561 usec_t before_startup, after_startup;
1562 char timespan[FORMAT_TIMESPAN_MAX];
1563 FDSet *fds = NULL;
1564 bool reexecute = false;
1565 const char *shutdown_verb = NULL;
1566 dual_timestamp initrd_timestamp = DUAL_TIMESTAMP_NULL;
1567 dual_timestamp userspace_timestamp = DUAL_TIMESTAMP_NULL;
1568 dual_timestamp kernel_timestamp = DUAL_TIMESTAMP_NULL;
1569 dual_timestamp security_start_timestamp = DUAL_TIMESTAMP_NULL;
1570 dual_timestamp security_finish_timestamp = DUAL_TIMESTAMP_NULL;
1571 static char systemd[] = "systemd";
1572 bool skip_setup = false;
1573 unsigned j;
1574 bool loaded_policy = false;
1575 bool arm_reboot_watchdog = false;
1576 bool queue_default_job = false;
1577 bool first_boot = false;
1578 char *switch_root_dir = NULL, *switch_root_init = NULL;
1579 struct rlimit saved_rlimit_nofile = RLIMIT_MAKE_CONST(0), saved_rlimit_memlock = RLIMIT_MAKE_CONST((rlim_t) -1);
1580 const char *error_message = NULL;
1581
1582 redirect_telinit(argc, argv);
1583
1584 dual_timestamp_from_monotonic(&kernel_timestamp, 0);
1585 dual_timestamp_get(&userspace_timestamp);
1586
1587 /* Determine if this is a reexecution or normal bootup. We do
1588 * the full command line parsing much later, so let's just
1589 * have a quick peek here. */
1590 if (strv_find(argv+1, "--deserialize"))
1591 skip_setup = true;
1592
1593 /* If we have switched root, do all the special setup
1594 * things */
1595 if (strv_find(argv+1, "--switched-root"))
1596 skip_setup = false;
1597
1598 /* If we get started via the /sbin/init symlink then we are
1599 called 'init'. After a subsequent reexecution we are then
1600 called 'systemd'. That is confusing, hence let's call us
1601 systemd right-away. */
1602 program_invocation_short_name = systemd;
1603 (void) prctl(PR_SET_NAME, systemd);
1604
1605 saved_argv = argv;
1606 saved_argc = argc;
1607
1608 log_set_upgrade_syslog_to_journal(true);
1609
1610 if (getpid_cached() == 1) {
1611 /* Disable the umask logic */
1612 umask(0);
1613
1614 /* Always reopen /dev/console when running as PID 1 or one of its pre-execve() children. This is
1615 * important so that we never end up logging to any foreign stderr, for example if we have to log in a
1616 * child process right before execve()'ing the actual binary, at a point in time where socket
1617 * activation stderr/stdout area already set up. */
1618 log_set_always_reopen_console(true);
1619 }
1620
1621 if (getpid_cached() == 1 && detect_container() <= 0) {
1622
1623 /* Running outside of a container as PID 1 */
1624 arg_system = true;
1625 log_set_target(LOG_TARGET_KMSG);
1626 log_open();
1627
1628 if (in_initrd())
1629 initrd_timestamp = userspace_timestamp;
1630
1631 if (!skip_setup) {
1632 r = mount_setup_early();
1633 if (r < 0) {
1634 error_message = "Failed to mount early API filesystems";
1635 goto finish;
1636 }
1637
1638 dual_timestamp_get(&security_start_timestamp);
1639 if (mac_selinux_setup(&loaded_policy) < 0) {
1640 error_message = "Failed to load SELinux policy";
1641 goto finish;
1642 } else if (mac_smack_setup(&loaded_policy) < 0) {
1643 error_message = "Failed to load SMACK policy";
1644 goto finish;
1645 } else if (ima_setup() < 0) {
1646 error_message = "Failed to load IMA policy";
1647 goto finish;
1648 }
1649 dual_timestamp_get(&security_finish_timestamp);
1650 }
1651
1652 if (mac_selinux_init() < 0) {
1653 error_message = "Failed to initialize SELinux policy";
1654 goto finish;
1655 }
1656
1657 if (!skip_setup)
1658 initialize_clock();
1659
1660 /* Set the default for later on, but don't actually
1661 * open the logs like this for now. Note that if we
1662 * are transitioning from the initrd there might still
1663 * be journal fd open, and we shouldn't attempt
1664 * opening that before we parsed /proc/cmdline which
1665 * might redirect output elsewhere. */
1666 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1667
1668 } else if (getpid_cached() == 1) {
1669 /* Running inside a container, as PID 1 */
1670 arg_system = true;
1671 log_set_target(LOG_TARGET_CONSOLE);
1672 log_close_console(); /* force reopen of /dev/console */
1673 log_open();
1674
1675 /* For later on, see above... */
1676 log_set_target(LOG_TARGET_JOURNAL);
1677
1678 /* clear the kernel timestamp,
1679 * because we are in a container */
1680 kernel_timestamp = DUAL_TIMESTAMP_NULL;
1681 } else {
1682 /* Running as user instance */
1683 arg_system = false;
1684 log_set_target(LOG_TARGET_AUTO);
1685 log_open();
1686
1687 /* clear the kernel timestamp,
1688 * because we are not PID 1 */
1689 kernel_timestamp = DUAL_TIMESTAMP_NULL;
1690 }
1691
1692 initialize_coredump(skip_setup);
1693
1694 if (arg_system) {
1695 if (fixup_environment() < 0) {
1696 error_message = "Failed to fix up PID1 environment";
1697 goto finish;
1698 }
1699
1700 /* Try to figure out if we can use colors with the console. No
1701 * need to do that for user instances since they never log
1702 * into the console. */
1703 log_show_color(colors_enabled());
1704 r = make_null_stdio();
1705 if (r < 0)
1706 log_warning_errno(r, "Failed to redirect standard streams to /dev/null: %m");
1707 }
1708
1709 r = initialize_join_controllers();
1710 if (r < 0) {
1711 error_message = "Failed to initialize cgroup controllers";
1712 goto finish;
1713 }
1714
1715 /* Mount /proc, /sys and friends, so that /proc/cmdline and
1716 * /proc/$PID/fd is available. */
1717 if (getpid_cached() == 1) {
1718
1719 /* Load the kernel modules early. */
1720 if (!skip_setup)
1721 kmod_setup();
1722
1723 r = mount_setup(loaded_policy);
1724 if (r < 0) {
1725 error_message = "Failed to mount API filesystems";
1726 goto finish;
1727 }
1728 }
1729
1730 /* Reset all signal handlers. */
1731 (void) reset_all_signal_handlers();
1732 (void) ignore_signals(SIGNALS_IGNORE, -1);
1733
1734 arg_default_tasks_max = system_tasks_max_scale(DEFAULT_TASKS_MAX_PERCENTAGE, 100U);
1735
1736 if (parse_config_file() < 0) {
1737 error_message = "Failed to parse config file";
1738 goto finish;
1739 }
1740
1741 if (arg_system) {
1742 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
1743 if (r < 0)
1744 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
1745 }
1746
1747 /* Note that this also parses bits from the kernel command
1748 * line, including "debug". */
1749 log_parse_environment();
1750
1751 if (parse_argv(argc, argv) < 0) {
1752 error_message = "Failed to parse commandline arguments";
1753 goto finish;
1754 }
1755
1756 /* Initialize default unit */
1757 if (!arg_default_unit) {
1758 arg_default_unit = strdup(SPECIAL_DEFAULT_TARGET);
1759 if (!arg_default_unit) {
1760 r = log_oom();
1761 error_message = "Failed to set default unit";
1762 goto finish;
1763 }
1764 }
1765
1766 if (arg_action == ACTION_TEST &&
1767 geteuid() == 0) {
1768 log_error("Don't run test mode as root.");
1769 goto finish;
1770 }
1771
1772 if (!arg_system &&
1773 arg_action == ACTION_RUN &&
1774 sd_booted() <= 0) {
1775 log_error("Trying to run as user instance, but the system has not been booted with systemd.");
1776 goto finish;
1777 }
1778
1779 if (arg_system &&
1780 arg_action == ACTION_RUN &&
1781 running_in_chroot() > 0) {
1782 log_error("Cannot be run in a chroot() environment.");
1783 goto finish;
1784 }
1785
1786 if (IN_SET(arg_action, ACTION_TEST, ACTION_HELP)) {
1787 pager_open(arg_no_pager, false);
1788 skip_setup = true;
1789 }
1790
1791 if (arg_action == ACTION_HELP) {
1792 retval = help();
1793 goto finish;
1794 } else if (arg_action == ACTION_VERSION) {
1795 retval = version();
1796 goto finish;
1797 } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) {
1798 pager_open(arg_no_pager, false);
1799 unit_dump_config_items(stdout);
1800 retval = EXIT_SUCCESS;
1801 goto finish;
1802 }
1803
1804 if (!arg_system &&
1805 !getenv("XDG_RUNTIME_DIR")) {
1806 log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");
1807 goto finish;
1808 }
1809
1810 assert_se(IN_SET(arg_action, ACTION_RUN, ACTION_TEST));
1811
1812 /* Close logging fds, in order not to confuse fdset below */
1813 log_close();
1814
1815 /* Remember open file descriptors for later deserialization */
1816 if (arg_action == ACTION_RUN) {
1817 r = fdset_new_fill(&fds);
1818 if (r < 0) {
1819 log_emergency_errno(r, "Failed to allocate fd set: %m");
1820 error_message = "Failed to allocate fd set";
1821 goto finish;
1822 } else
1823 fdset_cloexec(fds, true);
1824
1825 if (arg_serialization)
1826 assert_se(fdset_remove(fds, fileno(arg_serialization)) >= 0);
1827
1828 if (arg_system)
1829 /* Become a session leader if we aren't one yet. */
1830 setsid();
1831 }
1832
1833 /* Move out of the way, so that we won't block unmounts */
1834 assert_se(chdir("/") == 0);
1835
1836 /* Reset the console, but only if this is really init and we
1837 * are freshly booted */
1838 if (arg_system && arg_action == ACTION_RUN) {
1839
1840 /* If we are init, we connect stdin/stdout/stderr to
1841 * /dev/null and make sure we don't have a controlling
1842 * tty. */
1843 release_terminal();
1844
1845 if (getpid_cached() == 1 && !skip_setup)
1846 console_setup();
1847 }
1848
1849 /* Open the logging devices, if possible and necessary */
1850 log_open();
1851
1852 if (arg_show_status == _SHOW_STATUS_UNSET)
1853 arg_show_status = SHOW_STATUS_YES;
1854
1855 /* Make sure we leave a core dump without panicing the
1856 * kernel. */
1857 if (getpid_cached() == 1) {
1858 install_crash_handler();
1859
1860 r = mount_cgroup_controllers(arg_join_controllers);
1861 if (r < 0)
1862 goto finish;
1863 }
1864
1865 if (arg_system) {
1866 int v;
1867
1868 log_info(PACKAGE_STRING " running in %ssystem mode. (" SYSTEMD_FEATURES ")",
1869 arg_action == ACTION_TEST ? "test " : "" );
1870
1871 v = detect_virtualization();
1872 if (v > 0)
1873 log_info("Detected virtualization %s.", virtualization_to_string(v));
1874
1875 write_container_id();
1876
1877 log_info("Detected architecture %s.", architecture_to_string(uname_architecture()));
1878
1879 if (in_initrd())
1880 log_info("Running in initial RAM disk.");
1881 else {
1882 /* Let's check whether we are in first boot, i.e. whether /etc is still unpopulated. We use
1883 * /etc/machine-id as flag file, for this: if it exists we assume /etc is populated, if it
1884 * doesn't it's unpopulated. This allows container managers and installers to provision a
1885 * couple of files already. If the container manager wants to provision the machine ID itself
1886 * it should pass $container_uuid to PID 1. */
1887
1888 first_boot = access("/etc/machine-id", F_OK) < 0;
1889 if (first_boot)
1890 log_info("Running with unpopulated /etc.");
1891 }
1892 } else {
1893 _cleanup_free_ char *t;
1894
1895 t = uid_to_name(getuid());
1896 log_debug(PACKAGE_STRING " running in %suser mode for user " UID_FMT "/%s. (" SYSTEMD_FEATURES ")",
1897 arg_action == ACTION_TEST ? " test" : "", getuid(), strna(t));
1898 }
1899
1900 if (arg_action == ACTION_RUN) {
1901 if (arg_system && !skip_setup) {
1902 if (arg_show_status > 0)
1903 status_welcome();
1904
1905 hostname_setup();
1906 machine_id_setup(NULL, arg_machine_id, NULL);
1907 loopback_setup();
1908 bump_unix_max_dgram_qlen();
1909
1910 test_usr();
1911 }
1912
1913 if (arg_system && arg_runtime_watchdog > 0 && arg_runtime_watchdog != USEC_INFINITY)
1914 watchdog_set_timeout(&arg_runtime_watchdog);
1915
1916 if (arg_timer_slack_nsec != NSEC_INFINITY)
1917 if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0)
1918 log_error_errno(errno, "Failed to adjust timer slack: %m");
1919
1920 if (arg_system && !cap_test_all(arg_capability_bounding_set)) {
1921 r = capability_bounding_set_drop_usermode(arg_capability_bounding_set);
1922 if (r < 0) {
1923 log_emergency_errno(r, "Failed to drop capability bounding set of usermode helpers: %m");
1924 error_message = "Failed to drop capability bounding set of usermode helpers";
1925 goto finish;
1926 }
1927 r = capability_bounding_set_drop(arg_capability_bounding_set, true);
1928 if (r < 0) {
1929 log_emergency_errno(r, "Failed to drop capability bounding set: %m");
1930 error_message = "Failed to drop capability bounding set";
1931 goto finish;
1932 }
1933 }
1934
1935 if (arg_syscall_archs) {
1936 r = enforce_syscall_archs(arg_syscall_archs);
1937 if (r < 0) {
1938 error_message = "Failed to set syscall architectures";
1939 goto finish;
1940 }
1941 }
1942
1943 if (!arg_system)
1944 /* Become reaper of our children */
1945 if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0)
1946 log_warning_errno(errno, "Failed to make us a subreaper: %m");
1947
1948 if (arg_system) {
1949 /* Bump up RLIMIT_NOFILE for systemd itself */
1950 (void) bump_rlimit_nofile(&saved_rlimit_nofile);
1951 (void) bump_rlimit_memlock(&saved_rlimit_memlock);
1952 }
1953 }
1954
1955 r = manager_new(arg_system ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
1956 arg_action == ACTION_TEST ? MANAGER_TEST_FULL : 0,
1957 &m);
1958 if (r < 0) {
1959 log_emergency_errno(r, "Failed to allocate manager object: %m");
1960 error_message = "Failed to allocate manager object";
1961 goto finish;
1962 }
1963
1964 m->userspace_timestamp = userspace_timestamp;
1965 m->kernel_timestamp = kernel_timestamp;
1966 m->initrd_timestamp = initrd_timestamp;
1967 m->security_start_timestamp = security_start_timestamp;
1968 m->security_finish_timestamp = security_finish_timestamp;
1969
1970 set_manager_defaults(m);
1971 set_manager_settings(m);
1972 manager_set_first_boot(m, first_boot);
1973
1974 /* Remember whether we should queue the default job */
1975 queue_default_job = !arg_serialization || arg_switched_root;
1976
1977 before_startup = now(CLOCK_MONOTONIC);
1978
1979 r = manager_startup(m, arg_serialization, fds);
1980 if (r < 0) {
1981 log_error_errno(r, "Failed to fully start up daemon: %m");
1982 goto finish;
1983 }
1984
1985 /* This will close all file descriptors that were opened, but
1986 * not claimed by any unit. */
1987 fds = fdset_free(fds);
1988
1989 arg_serialization = safe_fclose(arg_serialization);
1990
1991 if (queue_default_job) {
1992 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1993 Unit *target = NULL;
1994 Job *default_unit_job;
1995
1996 log_debug("Activating default unit: %s", arg_default_unit);
1997
1998 r = manager_load_unit(m, arg_default_unit, NULL, &error, &target);
1999 if (r < 0)
2000 log_error("Failed to load default target: %s", bus_error_message(&error, r));
2001 else if (IN_SET(target->load_state, UNIT_ERROR, UNIT_NOT_FOUND))
2002 log_error_errno(target->load_error, "Failed to load default target: %m");
2003 else if (target->load_state == UNIT_MASKED)
2004 log_error("Default target masked.");
2005
2006 if (!target || target->load_state != UNIT_LOADED) {
2007 log_info("Trying to load rescue target...");
2008
2009 r = manager_load_unit(m, SPECIAL_RESCUE_TARGET, NULL, &error, &target);
2010 if (r < 0) {
2011 log_emergency("Failed to load rescue target: %s", bus_error_message(&error, r));
2012 error_message = "Failed to load rescue target";
2013 goto finish;
2014 } else if (IN_SET(target->load_state, UNIT_ERROR, UNIT_NOT_FOUND)) {
2015 log_emergency_errno(target->load_error, "Failed to load rescue target: %m");
2016 error_message = "Failed to load rescue target";
2017 goto finish;
2018 } else if (target->load_state == UNIT_MASKED) {
2019 log_emergency("Rescue target masked.");
2020 error_message = "Rescue target masked";
2021 goto finish;
2022 }
2023 }
2024
2025 assert(target->load_state == UNIT_LOADED);
2026
2027 if (arg_action == ACTION_TEST) {
2028 printf("-> By units:\n");
2029 manager_dump_units(m, stdout, "\t");
2030 }
2031
2032 r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, &error, &default_unit_job);
2033 if (r == -EPERM) {
2034 log_debug("Default target could not be isolated, starting instead: %s", bus_error_message(&error, r));
2035
2036 sd_bus_error_free(&error);
2037
2038 r = manager_add_job(m, JOB_START, target, JOB_REPLACE, &error, &default_unit_job);
2039 if (r < 0) {
2040 log_emergency("Failed to start default target: %s", bus_error_message(&error, r));
2041 error_message = "Failed to start default target";
2042 goto finish;
2043 }
2044 } else if (r < 0) {
2045 log_emergency("Failed to isolate default target: %s", bus_error_message(&error, r));
2046 error_message = "Failed to isolate default target";
2047 goto finish;
2048 }
2049
2050 m->default_unit_job_id = default_unit_job->id;
2051
2052 after_startup = now(CLOCK_MONOTONIC);
2053 log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG,
2054 "Loaded units and determined initial transaction in %s.",
2055 format_timespan(timespan, sizeof(timespan), after_startup - before_startup, 100 * USEC_PER_MSEC));
2056
2057 if (arg_action == ACTION_TEST) {
2058 printf("-> By jobs:\n");
2059 manager_dump_jobs(m, stdout, "\t");
2060 retval = EXIT_SUCCESS;
2061 goto finish;
2062 }
2063 }
2064
2065 for (;;) {
2066 r = manager_loop(m);
2067 if (r < 0) {
2068 log_emergency_errno(r, "Failed to run main loop: %m");
2069 error_message = "Failed to run main loop";
2070 goto finish;
2071 }
2072
2073 switch (m->exit_code) {
2074
2075 case MANAGER_RELOAD:
2076 log_info("Reloading.");
2077
2078 r = parse_config_file();
2079 if (r < 0)
2080 log_error("Failed to parse config file.");
2081
2082 set_manager_defaults(m);
2083
2084 r = manager_reload(m);
2085 if (r < 0)
2086 log_error_errno(r, "Failed to reload: %m");
2087 break;
2088
2089 case MANAGER_REEXECUTE:
2090
2091 if (prepare_reexecute(m, &arg_serialization, &fds, false) < 0) {
2092 error_message = "Failed to prepare for reexecution";
2093 goto finish;
2094 }
2095
2096 reexecute = true;
2097 log_notice("Reexecuting.");
2098 goto finish;
2099
2100 case MANAGER_SWITCH_ROOT:
2101 /* Steal the switch root parameters */
2102 switch_root_dir = m->switch_root;
2103 switch_root_init = m->switch_root_init;
2104 m->switch_root = m->switch_root_init = NULL;
2105
2106 if (!switch_root_init)
2107 if (prepare_reexecute(m, &arg_serialization, &fds, true) < 0) {
2108 error_message = "Failed to prepare for reexecution";
2109 goto finish;
2110 }
2111
2112 reexecute = true;
2113 log_notice("Switching root.");
2114 goto finish;
2115
2116 case MANAGER_EXIT:
2117 retval = m->return_value;
2118
2119 if (MANAGER_IS_USER(m)) {
2120 log_debug("Exit.");
2121 goto finish;
2122 }
2123
2124 /* fallthrough */
2125 case MANAGER_REBOOT:
2126 case MANAGER_POWEROFF:
2127 case MANAGER_HALT:
2128 case MANAGER_KEXEC: {
2129 static const char * const table[_MANAGER_EXIT_CODE_MAX] = {
2130 [MANAGER_EXIT] = "exit",
2131 [MANAGER_REBOOT] = "reboot",
2132 [MANAGER_POWEROFF] = "poweroff",
2133 [MANAGER_HALT] = "halt",
2134 [MANAGER_KEXEC] = "kexec"
2135 };
2136
2137 assert_se(shutdown_verb = table[m->exit_code]);
2138 arm_reboot_watchdog = m->exit_code == MANAGER_REBOOT;
2139
2140 log_notice("Shutting down.");
2141 goto finish;
2142 }
2143
2144 default:
2145 assert_not_reached("Unknown exit code.");
2146 }
2147 }
2148
2149 finish:
2150 pager_close();
2151
2152 if (m)
2153 arg_shutdown_watchdog = m->shutdown_watchdog;
2154
2155 m = manager_free(m);
2156
2157 for (j = 0; j < ELEMENTSOF(arg_default_rlimit); j++)
2158 arg_default_rlimit[j] = mfree(arg_default_rlimit[j]);
2159
2160 arg_default_unit = mfree(arg_default_unit);
2161 arg_confirm_spawn = mfree(arg_confirm_spawn);
2162 arg_join_controllers = strv_free_free(arg_join_controllers);
2163 arg_default_environment = strv_free(arg_default_environment);
2164 arg_syscall_archs = set_free(arg_syscall_archs);
2165
2166 mac_selinux_finish();
2167
2168 if (reexecute) {
2169 const char **args;
2170 unsigned i, args_size;
2171
2172 /* Close and disarm the watchdog, so that the new
2173 * instance can reinitialize it, but doesn't get
2174 * rebooted while we do that */
2175 watchdog_close(true);
2176
2177 /* Reset the RLIMIT_NOFILE to the kernel default, so
2178 * that the new systemd can pass the kernel default to
2179 * its child processes */
2180 if (saved_rlimit_nofile.rlim_cur > 0)
2181 (void) setrlimit(RLIMIT_NOFILE, &saved_rlimit_nofile);
2182 if (saved_rlimit_memlock.rlim_cur != (rlim_t) -1)
2183 (void) setrlimit(RLIMIT_MEMLOCK, &saved_rlimit_memlock);
2184
2185 if (switch_root_dir) {
2186 /* Kill all remaining processes from the
2187 * initrd, but don't wait for them, so that we
2188 * can handle the SIGCHLD for them after
2189 * deserializing. */
2190 broadcast_signal(SIGTERM, false, true);
2191
2192 /* And switch root with MS_MOVE, because we remove the old directory afterwards and detach it. */
2193 r = switch_root(switch_root_dir, "/mnt", true, MS_MOVE);
2194 if (r < 0)
2195 log_error_errno(r, "Failed to switch root, trying to continue: %m");
2196 }
2197
2198 args_size = MAX(6, argc+1);
2199 args = newa(const char*, args_size);
2200
2201 if (!switch_root_init) {
2202 char sfd[DECIMAL_STR_MAX(int) + 1];
2203
2204 /* First try to spawn ourselves with the right
2205 * path, and with full serialization. We do
2206 * this only if the user didn't specify an
2207 * explicit init to spawn. */
2208
2209 assert(arg_serialization);
2210 assert(fds);
2211
2212 xsprintf(sfd, "%i", fileno(arg_serialization));
2213
2214 i = 0;
2215 args[i++] = SYSTEMD_BINARY_PATH;
2216 if (switch_root_dir)
2217 args[i++] = "--switched-root";
2218 args[i++] = arg_system ? "--system" : "--user";
2219 args[i++] = "--deserialize";
2220 args[i++] = sfd;
2221 args[i++] = NULL;
2222
2223 assert(i <= args_size);
2224
2225 /*
2226 * We want valgrind to print its memory usage summary before reexecution.
2227 * Valgrind won't do this is on its own on exec(), but it will do it on exit().
2228 * Hence, to ensure we get a summary here, fork() off a child, let it exit() cleanly,
2229 * so that it prints the summary, and wait() for it in the parent, before proceeding into the exec().
2230 */
2231 valgrind_summary_hack();
2232
2233 (void) execv(args[0], (char* const*) args);
2234 }
2235
2236 /* Try the fallback, if there is any, without any
2237 * serialization. We pass the original argv[] and
2238 * envp[]. (Well, modulo the ordering changes due to
2239 * getopt() in argv[], and some cleanups in envp[],
2240 * but let's hope that doesn't matter.) */
2241
2242 arg_serialization = safe_fclose(arg_serialization);
2243 fds = fdset_free(fds);
2244
2245 /* Reopen the console */
2246 (void) make_console_stdio();
2247
2248 for (j = 1, i = 1; j < (unsigned) argc; j++)
2249 args[i++] = argv[j];
2250 args[i++] = NULL;
2251 assert(i <= args_size);
2252
2253 /* Reenable any blocked signals, especially important
2254 * if we switch from initial ramdisk to init=... */
2255 (void) reset_all_signal_handlers();
2256 (void) reset_signal_mask();
2257
2258 if (switch_root_init) {
2259 args[0] = switch_root_init;
2260 (void) execv(args[0], (char* const*) args);
2261 log_warning_errno(errno, "Failed to execute configured init, trying fallback: %m");
2262 }
2263
2264 args[0] = "/sbin/init";
2265 (void) execv(args[0], (char* const*) args);
2266
2267 manager_status_printf(NULL, STATUS_TYPE_EMERGENCY,
2268 ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL,
2269 "Failed to execute /sbin/init");
2270
2271 if (errno == ENOENT) {
2272 log_warning("No /sbin/init, trying fallback");
2273
2274 args[0] = "/bin/sh";
2275 args[1] = NULL;
2276 (void) execv(args[0], (char* const*) args);
2277 log_error_errno(errno, "Failed to execute /bin/sh, giving up: %m");
2278 } else
2279 log_warning_errno(errno, "Failed to execute /sbin/init, giving up: %m");
2280
2281 error_message = "Failed to execute fallback shell";
2282 }
2283
2284 arg_serialization = safe_fclose(arg_serialization);
2285 fds = fdset_free(fds);
2286
2287 #if HAVE_VALGRIND_VALGRIND_H
2288 /* If we are PID 1 and running under valgrind, then let's exit
2289 * here explicitly. valgrind will only generate nice output on
2290 * exit(), not on exec(), hence let's do the former not the
2291 * latter here. */
2292 if (getpid_cached() == 1 && RUNNING_ON_VALGRIND)
2293 return 0;
2294 #endif
2295
2296 if (shutdown_verb) {
2297 r = become_shutdown(shutdown_verb, retval, arm_reboot_watchdog);
2298
2299 log_error_errno(r, "Failed to execute shutdown binary, %s: %m", getpid_cached() == 1 ? "freezing" : "quitting");
2300 error_message = "Failed to execute shutdown binary";
2301 }
2302
2303 if (getpid_cached() == 1) {
2304 if (error_message)
2305 manager_status_printf(NULL, STATUS_TYPE_EMERGENCY,
2306 ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL,
2307 "%s, freezing.", error_message);
2308 freeze_or_reboot();
2309 }
2310
2311 return retval;
2312 }