1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include <sys/reboot.h>
13 #if HAVE_VALGRIND_VALGRIND_H
14 #include <valgrind/valgrind.h>
18 #include "sd-daemon.h"
19 #include "sd-messages.h"
21 #include "alloc-util.h"
22 #include "apparmor-setup.h"
23 #include "architecture.h"
25 #include "bus-error.h"
27 #include "capability-util.h"
28 #include "cgroup-util.h"
29 #include "clock-util.h"
30 #include "conf-parser.h"
31 #include "cpu-set-util.h"
32 #include "dbus-manager.h"
35 #include "dev-setup.h"
36 #include "efi-random.h"
38 #include "emergency-action.h"
40 #include "exit-status.h"
44 #include "format-util.h"
46 #include "hexdecoct.h"
47 #include "hostname-setup.h"
48 #include "ima-setup.h"
50 #include "kmod-setup.h"
51 #include "limits-util.h"
52 #include "load-fragment.h"
54 #include "loopback-setup.h"
55 #include "machine-id-setup.h"
58 #include "mount-setup.h"
61 #include "parse-argument.h"
62 #include "parse-util.h"
63 #include "path-util.h"
64 #include "pretty-print.h"
65 #include "proc-cmdline.h"
66 #include "process-util.h"
67 #include "random-util.h"
68 #include "raw-clone.h"
69 #include "rlimit-util.h"
71 #include "seccomp-util.h"
73 #include "selinux-setup.h"
74 #include "selinux-util.h"
75 #include "signal-util.h"
76 #include "smack-setup.h"
78 #include "stat-util.h"
79 #include "stdio-util.h"
81 #include "switch-root.h"
82 #include "sysctl-util.h"
83 #include "terminal-util.h"
84 #include "umask-util.h"
85 #include "user-util.h"
90 #if HAS_FEATURE_ADDRESS_SANITIZER
91 #include <sanitizer/lsan_interface.h>
94 #define DEFAULT_TASKS_MAX ((TasksMax) { 15U, 100U }) /* 15% */
101 ACTION_DUMP_CONFIGURATION_ITEMS
,
102 ACTION_DUMP_BUS_PROPERTIES
,
103 ACTION_BUS_INTROSPECT
,
104 } arg_action
= ACTION_RUN
;
106 static const char *arg_bus_introspect
= NULL
;
108 /* Those variables are initialized to 0 automatically, so we avoid uninitialized memory access. Real
109 * defaults are assigned in reset_arguments() below. */
110 static char *arg_default_unit
;
111 static bool arg_system
;
112 static bool arg_dump_core
;
113 static int arg_crash_chvt
;
114 static bool arg_crash_shell
;
115 static bool arg_crash_reboot
;
116 static char *arg_confirm_spawn
;
117 static ShowStatus arg_show_status
;
118 static StatusUnitFormat arg_status_unit_format
;
119 static bool arg_switched_root
;
120 static PagerFlags arg_pager_flags
;
121 static bool arg_service_watchdogs
;
122 static ExecOutput arg_default_std_output
;
123 static ExecOutput arg_default_std_error
;
124 static usec_t arg_default_restart_usec
;
125 static usec_t arg_default_timeout_start_usec
;
126 static usec_t arg_default_timeout_stop_usec
;
127 static usec_t arg_default_timeout_abort_usec
;
128 static bool arg_default_timeout_abort_set
;
129 static usec_t arg_default_start_limit_interval
;
130 static unsigned arg_default_start_limit_burst
;
131 static usec_t arg_runtime_watchdog
;
132 static usec_t arg_reboot_watchdog
;
133 static usec_t arg_kexec_watchdog
;
134 static char *arg_early_core_pattern
;
135 static char *arg_watchdog_device
;
136 static char **arg_default_environment
;
137 static char **arg_manager_environment
;
138 static struct rlimit
*arg_default_rlimit
[_RLIMIT_MAX
];
139 static uint64_t arg_capability_bounding_set
;
140 static bool arg_no_new_privs
;
141 static nsec_t arg_timer_slack_nsec
;
142 static usec_t arg_default_timer_accuracy_usec
;
143 static Set
* arg_syscall_archs
;
144 static FILE* arg_serialization
;
145 static int arg_default_cpu_accounting
;
146 static bool arg_default_io_accounting
;
147 static bool arg_default_ip_accounting
;
148 static bool arg_default_blockio_accounting
;
149 static bool arg_default_memory_accounting
;
150 static bool arg_default_tasks_accounting
;
151 static TasksMax arg_default_tasks_max
;
152 static sd_id128_t arg_machine_id
;
153 static EmergencyAction arg_cad_burst_action
;
154 static OOMPolicy arg_default_oom_policy
;
155 static CPUSet arg_cpu_affinity
;
156 static NUMAPolicy arg_numa_policy
;
157 static usec_t arg_clock_usec
;
158 static void *arg_random_seed
;
159 static size_t arg_random_seed_size
;
161 /* A copy of the original environment block */
162 static char **saved_env
= NULL
;
164 static int parse_configuration(const struct rlimit
*saved_rlimit_nofile
,
165 const struct rlimit
*saved_rlimit_memlock
);
167 static int manager_find_user_config_paths(char ***ret_files
, char ***ret_dirs
) {
168 _cleanup_free_
char *base
= NULL
;
169 _cleanup_strv_free_
char **files
= NULL
, **dirs
= NULL
;
172 r
= xdg_user_config_dir(&base
, "/systemd");
176 r
= strv_extendf(&files
, "%s/user.conf", base
);
180 r
= strv_extend(&files
, PKGSYSCONFDIR
"/user.conf");
184 r
= strv_consume(&dirs
, TAKE_PTR(base
));
188 r
= strv_extend_strv(&dirs
, CONF_PATHS_STRV("systemd"), false);
192 *ret_files
= TAKE_PTR(files
);
193 *ret_dirs
= TAKE_PTR(dirs
);
197 _noreturn_
static void freeze_or_exit_or_reboot(void) {
199 /* If we are running in a container, let's prefer exiting, after all we can propagate an exit code to
200 * the container manager, and thus inform it that something went wrong. */
201 if (detect_container() > 0) {
202 log_emergency("Exiting PID 1...");
203 _exit(EXIT_EXCEPTION
);
206 if (arg_crash_reboot
) {
207 log_notice("Rebooting in 10s...");
210 log_notice("Rebooting now...");
211 (void) reboot(RB_AUTOBOOT
);
212 log_emergency_errno(errno
, "Failed to reboot: %m");
215 log_emergency("Freezing execution.");
219 _noreturn_
static void crash(int sig
) {
223 if (getpid_cached() != 1)
224 /* Pass this on immediately, if this is not PID 1 */
226 else if (!arg_dump_core
)
227 log_emergency("Caught <%s>, not dumping core.", signal_to_string(sig
));
229 sa
= (struct sigaction
) {
230 .sa_handler
= nop_signal_handler
,
231 .sa_flags
= SA_NOCLDSTOP
|SA_RESTART
,
234 /* We want to wait for the core process, hence let's enable SIGCHLD */
235 (void) sigaction(SIGCHLD
, &sa
, NULL
);
237 pid
= raw_clone(SIGCHLD
);
239 log_emergency_errno(errno
, "Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig
));
241 /* Enable default signal handler for core dump */
243 sa
= (struct sigaction
) {
244 .sa_handler
= SIG_DFL
,
246 (void) sigaction(sig
, &sa
, NULL
);
248 /* Don't limit the coredump size */
249 (void) setrlimit(RLIMIT_CORE
, &RLIMIT_MAKE_CONST(RLIM_INFINITY
));
251 /* Just to be sure... */
254 /* Raise the signal again */
256 (void) kill(pid
, sig
); /* raise() would kill the parent */
258 assert_not_reached("We shouldn't be here...");
259 _exit(EXIT_EXCEPTION
);
264 /* Order things nicely. */
265 r
= wait_for_terminate(pid
, &status
);
267 log_emergency_errno(r
, "Caught <%s>, waitpid() failed: %m", signal_to_string(sig
));
268 else if (status
.si_code
!= CLD_DUMPED
) {
269 const char *s
= status
.si_code
== CLD_EXITED
270 ? exit_status_to_string(status
.si_status
, EXIT_STATUS_LIBC
)
271 : signal_to_string(status
.si_status
);
273 log_emergency("Caught <%s>, core dump failed (child "PID_FMT
", code=%s, status=%i/%s).",
274 signal_to_string(sig
),
276 sigchld_code_to_string(status
.si_code
),
277 status
.si_status
, strna(s
));
279 log_emergency("Caught <%s>, dumped core as pid "PID_FMT
".",
280 signal_to_string(sig
), pid
);
284 if (arg_crash_chvt
>= 0)
285 (void) chvt(arg_crash_chvt
);
287 sa
= (struct sigaction
) {
288 .sa_handler
= SIG_IGN
,
289 .sa_flags
= SA_NOCLDSTOP
|SA_NOCLDWAIT
|SA_RESTART
,
292 /* Let the kernel reap children for us */
293 (void) sigaction(SIGCHLD
, &sa
, NULL
);
295 if (arg_crash_shell
) {
296 log_notice("Executing crash shell in 10s...");
299 pid
= raw_clone(SIGCHLD
);
301 log_emergency_errno(errno
, "Failed to fork off crash shell: %m");
304 (void) make_console_stdio();
305 (void) rlimit_nofile_safe();
306 (void) execle("/bin/sh", "/bin/sh", NULL
, environ
);
308 log_emergency_errno(errno
, "execle() failed: %m");
309 _exit(EXIT_EXCEPTION
);
311 log_info("Spawned crash shell as PID "PID_FMT
".", pid
);
312 (void) wait_for_terminate(pid
, NULL
);
316 freeze_or_exit_or_reboot();
319 static void install_crash_handler(void) {
320 static const struct sigaction sa
= {
322 .sa_flags
= SA_NODEFER
, /* So that we can raise the signal again from the signal handler */
326 /* We ignore the return value here, since, we don't mind if we
327 * cannot set up a crash handler */
328 r
= sigaction_many(&sa
, SIGNALS_CRASH_HANDLER
, -1);
330 log_debug_errno(r
, "I had trouble setting up the crash handler, ignoring: %m");
333 static int console_setup(void) {
334 _cleanup_close_
int tty_fd
= -1;
337 tty_fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
339 return log_error_errno(tty_fd
, "Failed to open /dev/console: %m");
341 /* We don't want to force text mode. plymouth may be showing
342 * pictures already from initrd. */
343 r
= reset_terminal_fd(tty_fd
, false);
345 return log_error_errno(r
, "Failed to reset /dev/console: %m");
350 static int set_machine_id(const char *m
) {
354 if (sd_id128_from_string(m
, &t
) < 0)
357 if (sd_id128_is_null(t
))
364 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
369 if (STR_IN_SET(key
, "systemd.unit", "rd.systemd.unit")) {
371 if (proc_cmdline_value_missing(key
, value
))
374 if (!unit_name_is_valid(value
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
))
375 log_warning("Unit name specified on %s= is not valid, ignoring: %s", key
, value
);
376 else if (in_initrd() == !!startswith(key
, "rd."))
377 return free_and_strdup_warn(&arg_default_unit
, value
);
379 } else if (proc_cmdline_key_streq(key
, "systemd.dump_core")) {
381 r
= value
? parse_boolean(value
) : true;
383 log_warning_errno(r
, "Failed to parse dump core switch %s, ignoring: %m", value
);
387 } else if (proc_cmdline_key_streq(key
, "systemd.early_core_pattern")) {
389 if (proc_cmdline_value_missing(key
, value
))
392 if (path_is_absolute(value
))
393 (void) parse_path_argument(value
, false, &arg_early_core_pattern
);
395 log_warning("Specified core pattern '%s' is not an absolute path, ignoring.", value
);
397 } else if (proc_cmdline_key_streq(key
, "systemd.crash_chvt")) {
400 arg_crash_chvt
= 0; /* turn on */
402 r
= parse_crash_chvt(value
, &arg_crash_chvt
);
404 log_warning_errno(r
, "Failed to parse crash chvt switch %s, ignoring: %m", value
);
407 } else if (proc_cmdline_key_streq(key
, "systemd.crash_shell")) {
409 r
= value
? parse_boolean(value
) : true;
411 log_warning_errno(r
, "Failed to parse crash shell switch %s, ignoring: %m", value
);
415 } else if (proc_cmdline_key_streq(key
, "systemd.crash_reboot")) {
417 r
= value
? parse_boolean(value
) : true;
419 log_warning_errno(r
, "Failed to parse crash reboot switch %s, ignoring: %m", value
);
421 arg_crash_reboot
= r
;
423 } else if (proc_cmdline_key_streq(key
, "systemd.confirm_spawn")) {
426 r
= parse_confirm_spawn(value
, &s
);
428 log_warning_errno(r
, "Failed to parse confirm_spawn switch %s, ignoring: %m", value
);
430 free_and_replace(arg_confirm_spawn
, s
);
432 } else if (proc_cmdline_key_streq(key
, "systemd.service_watchdogs")) {
434 r
= value
? parse_boolean(value
) : true;
436 log_warning_errno(r
, "Failed to parse service watchdog switch %s, ignoring: %m", value
);
438 arg_service_watchdogs
= r
;
440 } else if (proc_cmdline_key_streq(key
, "systemd.show_status")) {
443 r
= parse_show_status(value
, &arg_show_status
);
445 log_warning_errno(r
, "Failed to parse show status switch %s, ignoring: %m", value
);
447 arg_show_status
= SHOW_STATUS_YES
;
449 } else if (proc_cmdline_key_streq(key
, "systemd.status_unit_format")) {
451 if (proc_cmdline_value_missing(key
, value
))
454 r
= status_unit_format_from_string(value
);
456 log_warning_errno(r
, "Failed to parse %s=%s, ignoring: %m", key
, value
);
458 arg_status_unit_format
= r
;
460 } else if (proc_cmdline_key_streq(key
, "systemd.default_standard_output")) {
462 if (proc_cmdline_value_missing(key
, value
))
465 r
= exec_output_from_string(value
);
467 log_warning_errno(r
, "Failed to parse default standard output switch %s, ignoring: %m", value
);
469 arg_default_std_output
= r
;
471 } else if (proc_cmdline_key_streq(key
, "systemd.default_standard_error")) {
473 if (proc_cmdline_value_missing(key
, value
))
476 r
= exec_output_from_string(value
);
478 log_warning_errno(r
, "Failed to parse default standard error switch %s, ignoring: %m", value
);
480 arg_default_std_error
= r
;
482 } else if (streq(key
, "systemd.setenv")) {
484 if (proc_cmdline_value_missing(key
, value
))
487 if (!env_assignment_is_valid(value
))
488 log_warning("Environment variable assignment '%s' is not valid. Ignoring.", value
);
490 r
= strv_env_replace_strdup(&arg_default_environment
, value
);
495 } else if (proc_cmdline_key_streq(key
, "systemd.machine_id")) {
497 if (proc_cmdline_value_missing(key
, value
))
500 r
= set_machine_id(value
);
502 log_warning_errno(r
, "MachineID '%s' is not valid, ignoring: %m", value
);
504 } else if (proc_cmdline_key_streq(key
, "systemd.default_timeout_start_sec")) {
506 if (proc_cmdline_value_missing(key
, value
))
509 r
= parse_sec(value
, &arg_default_timeout_start_usec
);
511 log_warning_errno(r
, "Failed to parse default start timeout '%s', ignoring: %m", value
);
513 if (arg_default_timeout_start_usec
<= 0)
514 arg_default_timeout_start_usec
= USEC_INFINITY
;
516 } else if (proc_cmdline_key_streq(key
, "systemd.cpu_affinity")) {
518 if (proc_cmdline_value_missing(key
, value
))
521 r
= parse_cpu_set(value
, &arg_cpu_affinity
);
523 log_warning_errno(r
, "Failed to parse CPU affinity mask '%s', ignoring: %m", value
);
525 } else if (proc_cmdline_key_streq(key
, "systemd.watchdog_device")) {
527 if (proc_cmdline_value_missing(key
, value
))
530 (void) parse_path_argument(value
, false, &arg_watchdog_device
);
532 } else if (proc_cmdline_key_streq(key
, "systemd.clock_usec")) {
534 if (proc_cmdline_value_missing(key
, value
))
537 r
= safe_atou64(value
, &arg_clock_usec
);
539 log_warning_errno(r
, "Failed to parse systemd.clock_usec= argument, ignoring: %s", value
);
541 } else if (proc_cmdline_key_streq(key
, "systemd.random_seed")) {
545 if (proc_cmdline_value_missing(key
, value
))
548 r
= unbase64mem(value
, (size_t) -1, &p
, &sz
);
550 log_warning_errno(r
, "Failed to parse systemd.random_seed= argument, ignoring: %s", value
);
552 free(arg_random_seed
);
553 arg_random_seed
= sz
> 0 ? p
: mfree(p
);
554 arg_random_seed_size
= sz
;
556 } else if (streq(key
, "quiet") && !value
) {
558 if (arg_show_status
== _SHOW_STATUS_INVALID
)
559 arg_show_status
= SHOW_STATUS_ERROR
;
561 } else if (streq(key
, "debug") && !value
) {
563 /* Note that log_parse_environment() handles 'debug'
564 * too, and sets the log level to LOG_DEBUG. */
566 if (detect_container() > 0)
567 log_set_target(LOG_TARGET_CONSOLE
);
572 /* Compatible with SysV, but supported independently even if SysV compatibility is disabled. */
573 target
= runlevel_to_target(key
);
575 return free_and_strdup_warn(&arg_default_unit
, target
);
581 #define DEFINE_SETTER(name, func, descr) \
582 static int name(const char *unit, \
583 const char *filename, \
585 const char *section, \
586 unsigned section_line, \
587 const char *lvalue, \
589 const char *rvalue, \
601 log_syntax(unit, LOG_ERR, filename, line, r, \
602 "Invalid " descr "'%s': %m", \
608 DEFINE_SETTER(config_parse_level2
, log_set_max_level_from_string
, "log level");
609 DEFINE_SETTER(config_parse_target
, log_set_target_from_string
, "target");
610 DEFINE_SETTER(config_parse_color
, log_show_color_from_string
, "color");
611 DEFINE_SETTER(config_parse_location
, log_show_location_from_string
, "location");
612 DEFINE_SETTER(config_parse_time
, log_show_time_from_string
, "time");
614 static int config_parse_default_timeout_abort(
616 const char *filename
,
619 unsigned section_line
,
627 r
= config_parse_timeout_abort(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
,
628 &arg_default_timeout_abort_usec
, userdata
);
630 arg_default_timeout_abort_set
= r
;
634 static int parse_config_file(void) {
635 const ConfigTableItem items
[] = {
636 { "Manager", "LogLevel", config_parse_level2
, 0, NULL
},
637 { "Manager", "LogTarget", config_parse_target
, 0, NULL
},
638 { "Manager", "LogColor", config_parse_color
, 0, NULL
},
639 { "Manager", "LogLocation", config_parse_location
, 0, NULL
},
640 { "Manager", "LogTime", config_parse_time
, 0, NULL
},
641 { "Manager", "DumpCore", config_parse_bool
, 0, &arg_dump_core
},
642 { "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt
, 0, &arg_crash_chvt
},
643 { "Manager", "CrashChangeVT", config_parse_crash_chvt
, 0, &arg_crash_chvt
},
644 { "Manager", "CrashShell", config_parse_bool
, 0, &arg_crash_shell
},
645 { "Manager", "CrashReboot", config_parse_bool
, 0, &arg_crash_reboot
},
646 { "Manager", "ShowStatus", config_parse_show_status
, 0, &arg_show_status
},
647 { "Manager", "StatusUnitFormat", config_parse_status_unit_format
, 0, &arg_status_unit_format
},
648 { "Manager", "CPUAffinity", config_parse_cpu_affinity2
, 0, &arg_cpu_affinity
},
649 { "Manager", "NUMAPolicy", config_parse_numa_policy
, 0, &arg_numa_policy
.type
},
650 { "Manager", "NUMAMask", config_parse_numa_mask
, 0, &arg_numa_policy
},
651 { "Manager", "JoinControllers", config_parse_warn_compat
, DISABLED_CONFIGURATION
, NULL
},
652 { "Manager", "RuntimeWatchdogSec", config_parse_sec
, 0, &arg_runtime_watchdog
},
653 { "Manager", "RebootWatchdogSec", config_parse_sec
, 0, &arg_reboot_watchdog
},
654 { "Manager", "ShutdownWatchdogSec", config_parse_sec
, 0, &arg_reboot_watchdog
}, /* obsolete alias */
655 { "Manager", "KExecWatchdogSec", config_parse_sec
, 0, &arg_kexec_watchdog
},
656 { "Manager", "WatchdogDevice", config_parse_path
, 0, &arg_watchdog_device
},
657 { "Manager", "CapabilityBoundingSet", config_parse_capability_set
, 0, &arg_capability_bounding_set
},
658 { "Manager", "NoNewPrivileges", config_parse_bool
, 0, &arg_no_new_privs
},
660 { "Manager", "SystemCallArchitectures", config_parse_syscall_archs
, 0, &arg_syscall_archs
},
662 { "Manager", "TimerSlackNSec", config_parse_nsec
, 0, &arg_timer_slack_nsec
},
663 { "Manager", "DefaultTimerAccuracySec", config_parse_sec
, 0, &arg_default_timer_accuracy_usec
},
664 { "Manager", "DefaultStandardOutput", config_parse_output_restricted
, 0, &arg_default_std_output
},
665 { "Manager", "DefaultStandardError", config_parse_output_restricted
, 0, &arg_default_std_error
},
666 { "Manager", "DefaultTimeoutStartSec", config_parse_sec
, 0, &arg_default_timeout_start_usec
},
667 { "Manager", "DefaultTimeoutStopSec", config_parse_sec
, 0, &arg_default_timeout_stop_usec
},
668 { "Manager", "DefaultTimeoutAbortSec", config_parse_default_timeout_abort
, 0, NULL
},
669 { "Manager", "DefaultRestartSec", config_parse_sec
, 0, &arg_default_restart_usec
},
670 { "Manager", "DefaultStartLimitInterval", config_parse_sec
, 0, &arg_default_start_limit_interval
}, /* obsolete alias */
671 { "Manager", "DefaultStartLimitIntervalSec", config_parse_sec
, 0, &arg_default_start_limit_interval
},
672 { "Manager", "DefaultStartLimitBurst", config_parse_unsigned
, 0, &arg_default_start_limit_burst
},
673 { "Manager", "DefaultEnvironment", config_parse_environ
, 0, &arg_default_environment
},
674 { "Manager", "ManagerEnvironment", config_parse_environ
, 0, &arg_manager_environment
},
675 { "Manager", "DefaultLimitCPU", config_parse_rlimit
, RLIMIT_CPU
, arg_default_rlimit
},
676 { "Manager", "DefaultLimitFSIZE", config_parse_rlimit
, RLIMIT_FSIZE
, arg_default_rlimit
},
677 { "Manager", "DefaultLimitDATA", config_parse_rlimit
, RLIMIT_DATA
, arg_default_rlimit
},
678 { "Manager", "DefaultLimitSTACK", config_parse_rlimit
, RLIMIT_STACK
, arg_default_rlimit
},
679 { "Manager", "DefaultLimitCORE", config_parse_rlimit
, RLIMIT_CORE
, arg_default_rlimit
},
680 { "Manager", "DefaultLimitRSS", config_parse_rlimit
, RLIMIT_RSS
, arg_default_rlimit
},
681 { "Manager", "DefaultLimitNOFILE", config_parse_rlimit
, RLIMIT_NOFILE
, arg_default_rlimit
},
682 { "Manager", "DefaultLimitAS", config_parse_rlimit
, RLIMIT_AS
, arg_default_rlimit
},
683 { "Manager", "DefaultLimitNPROC", config_parse_rlimit
, RLIMIT_NPROC
, arg_default_rlimit
},
684 { "Manager", "DefaultLimitMEMLOCK", config_parse_rlimit
, RLIMIT_MEMLOCK
, arg_default_rlimit
},
685 { "Manager", "DefaultLimitLOCKS", config_parse_rlimit
, RLIMIT_LOCKS
, arg_default_rlimit
},
686 { "Manager", "DefaultLimitSIGPENDING", config_parse_rlimit
, RLIMIT_SIGPENDING
, arg_default_rlimit
},
687 { "Manager", "DefaultLimitMSGQUEUE", config_parse_rlimit
, RLIMIT_MSGQUEUE
, arg_default_rlimit
},
688 { "Manager", "DefaultLimitNICE", config_parse_rlimit
, RLIMIT_NICE
, arg_default_rlimit
},
689 { "Manager", "DefaultLimitRTPRIO", config_parse_rlimit
, RLIMIT_RTPRIO
, arg_default_rlimit
},
690 { "Manager", "DefaultLimitRTTIME", config_parse_rlimit
, RLIMIT_RTTIME
, arg_default_rlimit
},
691 { "Manager", "DefaultCPUAccounting", config_parse_tristate
, 0, &arg_default_cpu_accounting
},
692 { "Manager", "DefaultIOAccounting", config_parse_bool
, 0, &arg_default_io_accounting
},
693 { "Manager", "DefaultIPAccounting", config_parse_bool
, 0, &arg_default_ip_accounting
},
694 { "Manager", "DefaultBlockIOAccounting", config_parse_bool
, 0, &arg_default_blockio_accounting
},
695 { "Manager", "DefaultMemoryAccounting", config_parse_bool
, 0, &arg_default_memory_accounting
},
696 { "Manager", "DefaultTasksAccounting", config_parse_bool
, 0, &arg_default_tasks_accounting
},
697 { "Manager", "DefaultTasksMax", config_parse_tasks_max
, 0, &arg_default_tasks_max
},
698 { "Manager", "CtrlAltDelBurstAction", config_parse_emergency_action
, 0, &arg_cad_burst_action
},
699 { "Manager", "DefaultOOMPolicy", config_parse_oom_policy
, 0, &arg_default_oom_policy
},
703 _cleanup_strv_free_
char **_free_files
= NULL
, **_free_dirs
= NULL
;
705 const char *const *files
, *const *dirs
, *suffix
;
709 files
= STRV_MAKE_CONST(PKGSYSCONFDIR
"/system.conf");
710 dirs
= (const char* const*) CONF_PATHS_STRV("systemd");
711 suffix
= "system.conf.d";
713 r
= manager_find_user_config_paths(&_free_files
, &_free_dirs
);
715 return log_error_errno(r
, "Failed to determine config file paths: %m");
716 files
= (const char* const*) _free_files
;
717 dirs
= (const char* const*) _free_dirs
;
718 suffix
= "user.conf.d";
721 (void) config_parse_many(
724 config_item_table_lookup
, items
,
729 /* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we use
730 * USEC_INFINITY like everywhere else. */
731 if (arg_default_timeout_start_usec
<= 0)
732 arg_default_timeout_start_usec
= USEC_INFINITY
;
733 if (arg_default_timeout_stop_usec
<= 0)
734 arg_default_timeout_stop_usec
= USEC_INFINITY
;
739 static void set_manager_defaults(Manager
*m
) {
743 /* Propagates the various default unit property settings into the manager object, i.e. properties that do not
744 * affect the manager itself, but are just what newly allocated units will have set if they haven't set
745 * anything else. (Also see set_manager_settings() for the settings that affect the manager's own behaviour) */
747 m
->default_timer_accuracy_usec
= arg_default_timer_accuracy_usec
;
748 m
->default_std_output
= arg_default_std_output
;
749 m
->default_std_error
= arg_default_std_error
;
750 m
->default_timeout_start_usec
= arg_default_timeout_start_usec
;
751 m
->default_timeout_stop_usec
= arg_default_timeout_stop_usec
;
752 m
->default_timeout_abort_usec
= arg_default_timeout_abort_usec
;
753 m
->default_timeout_abort_set
= arg_default_timeout_abort_set
;
754 m
->default_restart_usec
= arg_default_restart_usec
;
755 m
->default_start_limit_interval
= arg_default_start_limit_interval
;
756 m
->default_start_limit_burst
= arg_default_start_limit_burst
;
758 /* On 4.15+ with unified hierarchy, CPU accounting is essentially free as it doesn't require the CPU
759 * controller to be enabled, so the default is to enable it unless we got told otherwise. */
760 if (arg_default_cpu_accounting
>= 0)
761 m
->default_cpu_accounting
= arg_default_cpu_accounting
;
763 m
->default_cpu_accounting
= cpu_accounting_is_cheap();
765 m
->default_io_accounting
= arg_default_io_accounting
;
766 m
->default_ip_accounting
= arg_default_ip_accounting
;
767 m
->default_blockio_accounting
= arg_default_blockio_accounting
;
768 m
->default_memory_accounting
= arg_default_memory_accounting
;
769 m
->default_tasks_accounting
= arg_default_tasks_accounting
;
770 m
->default_tasks_max
= arg_default_tasks_max
;
771 m
->default_oom_policy
= arg_default_oom_policy
;
773 (void) manager_set_default_rlimits(m
, arg_default_rlimit
);
775 (void) manager_default_environment(m
);
776 (void) manager_transient_environment_add(m
, arg_default_environment
);
779 static void set_manager_settings(Manager
*m
) {
783 /* Propagates the various manager settings into the manager object, i.e. properties that
784 * effect the manager itself (as opposed to just being inherited into newly allocated
785 * units, see set_manager_defaults() above). */
787 m
->confirm_spawn
= arg_confirm_spawn
;
788 m
->service_watchdogs
= arg_service_watchdogs
;
789 m
->cad_burst_action
= arg_cad_burst_action
;
791 manager_set_watchdog(m
, WATCHDOG_RUNTIME
, arg_runtime_watchdog
);
792 manager_set_watchdog(m
, WATCHDOG_REBOOT
, arg_reboot_watchdog
);
793 manager_set_watchdog(m
, WATCHDOG_KEXEC
, arg_kexec_watchdog
);
795 manager_set_show_status(m
, arg_show_status
, "commandline");
796 m
->status_unit_format
= arg_status_unit_format
;
799 static int parse_argv(int argc
, char *argv
[]) {
801 ARG_LOG_LEVEL
= 0x100,
812 ARG_DUMP_CONFIGURATION_ITEMS
,
813 ARG_DUMP_BUS_PROPERTIES
,
823 ARG_DEFAULT_STD_OUTPUT
,
824 ARG_DEFAULT_STD_ERROR
,
826 ARG_SERVICE_WATCHDOGS
,
829 static const struct option options
[] = {
830 { "log-level", required_argument
, NULL
, ARG_LOG_LEVEL
},
831 { "log-target", required_argument
, NULL
, ARG_LOG_TARGET
},
832 { "log-color", optional_argument
, NULL
, ARG_LOG_COLOR
},
833 { "log-location", optional_argument
, NULL
, ARG_LOG_LOCATION
},
834 { "log-time", optional_argument
, NULL
, ARG_LOG_TIME
},
835 { "unit", required_argument
, NULL
, ARG_UNIT
},
836 { "system", no_argument
, NULL
, ARG_SYSTEM
},
837 { "user", no_argument
, NULL
, ARG_USER
},
838 { "test", no_argument
, NULL
, ARG_TEST
},
839 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
840 { "help", no_argument
, NULL
, 'h' },
841 { "version", no_argument
, NULL
, ARG_VERSION
},
842 { "dump-configuration-items", no_argument
, NULL
, ARG_DUMP_CONFIGURATION_ITEMS
},
843 { "dump-bus-properties", no_argument
, NULL
, ARG_DUMP_BUS_PROPERTIES
},
844 { "bus-introspect", required_argument
, NULL
, ARG_BUS_INTROSPECT
},
845 { "dump-core", optional_argument
, NULL
, ARG_DUMP_CORE
},
846 { "crash-chvt", required_argument
, NULL
, ARG_CRASH_CHVT
},
847 { "crash-shell", optional_argument
, NULL
, ARG_CRASH_SHELL
},
848 { "crash-reboot", optional_argument
, NULL
, ARG_CRASH_REBOOT
},
849 { "confirm-spawn", optional_argument
, NULL
, ARG_CONFIRM_SPAWN
},
850 { "show-status", optional_argument
, NULL
, ARG_SHOW_STATUS
},
851 { "deserialize", required_argument
, NULL
, ARG_DESERIALIZE
},
852 { "switched-root", no_argument
, NULL
, ARG_SWITCHED_ROOT
},
853 { "default-standard-output", required_argument
, NULL
, ARG_DEFAULT_STD_OUTPUT
, },
854 { "default-standard-error", required_argument
, NULL
, ARG_DEFAULT_STD_ERROR
, },
855 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
856 { "service-watchdogs", required_argument
, NULL
, ARG_SERVICE_WATCHDOGS
},
861 bool user_arg_seen
= false;
866 if (getpid_cached() == 1)
869 while ((c
= getopt_long(argc
, argv
, "hDbsz:", options
, NULL
)) >= 0)
874 r
= log_set_max_level_from_string(optarg
);
876 return log_error_errno(r
, "Failed to parse log level \"%s\": %m", optarg
);
881 r
= log_set_target_from_string(optarg
);
883 return log_error_errno(r
, "Failed to parse log target \"%s\": %m", optarg
);
890 r
= log_show_color_from_string(optarg
);
892 return log_error_errno(r
, "Failed to parse log color setting \"%s\": %m",
895 log_show_color(true);
899 case ARG_LOG_LOCATION
:
901 r
= log_show_location_from_string(optarg
);
903 return log_error_errno(r
, "Failed to parse log location setting \"%s\": %m",
906 log_show_location(true);
913 r
= log_show_time_from_string(optarg
);
915 return log_error_errno(r
, "Failed to parse log time setting \"%s\": %m",
922 case ARG_DEFAULT_STD_OUTPUT
:
923 r
= exec_output_from_string(optarg
);
925 return log_error_errno(r
, "Failed to parse default standard output setting \"%s\": %m",
927 arg_default_std_output
= r
;
930 case ARG_DEFAULT_STD_ERROR
:
931 r
= exec_output_from_string(optarg
);
933 return log_error_errno(r
, "Failed to parse default standard error output setting \"%s\": %m",
935 arg_default_std_error
= r
;
939 r
= free_and_strdup(&arg_default_unit
, optarg
);
941 return log_error_errno(r
, "Failed to set default unit \"%s\": %m", optarg
);
951 user_arg_seen
= true;
955 arg_action
= ACTION_TEST
;
959 arg_pager_flags
|= PAGER_DISABLE
;
963 arg_action
= ACTION_VERSION
;
966 case ARG_DUMP_CONFIGURATION_ITEMS
:
967 arg_action
= ACTION_DUMP_CONFIGURATION_ITEMS
;
970 case ARG_DUMP_BUS_PROPERTIES
:
971 arg_action
= ACTION_DUMP_BUS_PROPERTIES
;
974 case ARG_BUS_INTROSPECT
:
975 arg_bus_introspect
= optarg
;
976 arg_action
= ACTION_BUS_INTROSPECT
;
980 r
= parse_boolean_argument("--dump-core", optarg
, &arg_dump_core
);
986 r
= parse_crash_chvt(optarg
, &arg_crash_chvt
);
988 return log_error_errno(r
, "Failed to parse crash virtual terminal index: \"%s\": %m",
992 case ARG_CRASH_SHELL
:
993 r
= parse_boolean_argument("--crash-shell", optarg
, &arg_crash_shell
);
998 case ARG_CRASH_REBOOT
:
999 r
= parse_boolean_argument("--crash-reboot", optarg
, &arg_crash_reboot
);
1004 case ARG_CONFIRM_SPAWN
:
1005 arg_confirm_spawn
= mfree(arg_confirm_spawn
);
1007 r
= parse_confirm_spawn(optarg
, &arg_confirm_spawn
);
1009 return log_error_errno(r
, "Failed to parse confirm spawn option: \"%s\": %m",
1013 case ARG_SERVICE_WATCHDOGS
:
1014 r
= parse_boolean_argument("--service-watchdogs=", optarg
, &arg_service_watchdogs
);
1019 case ARG_SHOW_STATUS
:
1021 r
= parse_show_status(optarg
, &arg_show_status
);
1023 return log_error_errno(r
, "Failed to parse show status boolean: \"%s\": %m",
1026 arg_show_status
= SHOW_STATUS_YES
;
1029 case ARG_DESERIALIZE
: {
1033 r
= safe_atoi(optarg
, &fd
);
1035 log_error_errno(r
, "Failed to parse deserialize option \"%s\": %m", optarg
);
1037 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1038 "Invalid deserialize fd: %d",
1041 (void) fd_cloexec(fd
, true);
1043 f
= fdopen(fd
, "r");
1045 return log_error_errno(errno
, "Failed to open serialization fd %d: %m", fd
);
1047 safe_fclose(arg_serialization
);
1048 arg_serialization
= f
;
1053 case ARG_SWITCHED_ROOT
:
1054 arg_switched_root
= true;
1057 case ARG_MACHINE_ID
:
1058 r
= set_machine_id(optarg
);
1060 return log_error_errno(r
, "MachineID '%s' is not valid: %m", optarg
);
1064 arg_action
= ACTION_HELP
;
1068 log_set_max_level(LOG_DEBUG
);
1074 /* Just to eat away the sysvinit kernel cmdline args that we'll parse in
1075 * parse_proc_cmdline_item() or ignore, without any getopt() error messages.
1078 if (getpid_cached() != 1)
1084 assert_not_reached("Unhandled option code.");
1087 if (optind
< argc
&& getpid_cached() != 1)
1088 /* Hmm, when we aren't run as init system let's complain about excess arguments */
1089 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Excess arguments.");
1091 if (arg_action
== ACTION_RUN
&& !arg_system
&& !user_arg_seen
)
1092 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1093 "Explicit --user argument required to run as user manager.");
1098 static int help(void) {
1099 _cleanup_free_
char *link
= NULL
;
1102 r
= terminal_urlify_man("systemd", "1", &link
);
1106 printf("%s [OPTIONS...]\n\n"
1107 "%sStarts and monitors system and user services.%s\n\n"
1108 "This program takes no positional arguments.\n\n"
1110 " -h --help Show this help\n"
1111 " --version Show version\n"
1112 " --test Determine initial transaction, dump it and exit\n"
1113 " --system In combination with --test: operate as system service manager\n"
1114 " --user In combination with --test: operate as per-user service manager\n"
1115 " --no-pager Do not pipe output into a pager\n"
1116 " --dump-configuration-items Dump understood unit configuration items\n"
1117 " --dump-bus-properties Dump exposed bus properties\n"
1118 " --bus-introspect=PATH Write XML introspection data\n"
1119 " --unit=UNIT Set default unit\n"
1120 " --dump-core[=BOOL] Dump core on crash\n"
1121 " --crash-vt=NR Change to specified VT on crash\n"
1122 " --crash-reboot[=BOOL] Reboot on crash\n"
1123 " --crash-shell[=BOOL] Run shell on crash\n"
1124 " --confirm-spawn[=BOOL] Ask for confirmation when spawning processes\n"
1125 " --show-status[=BOOL] Show status updates on the console during bootup\n"
1126 " --log-target=TARGET Set log target (console, journal, kmsg, journal-or-kmsg, null)\n"
1127 " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
1128 " --log-color[=BOOL] Highlight important log messages\n"
1129 " --log-location[=BOOL] Include code location in log messages\n"
1130 " --log-time[=BOOL] Prefix log messages with current time\n"
1131 " --default-standard-output= Set default standard output for services\n"
1132 " --default-standard-error= Set default standard error output for services\n"
1133 "\nSee the %s for details.\n",
1134 program_invocation_short_name
,
1144 static int prepare_reexecute(
1148 bool switching_root
) {
1150 _cleanup_fdset_free_ FDSet
*fds
= NULL
;
1151 _cleanup_fclose_
FILE *f
= NULL
;
1158 r
= manager_open_serialization(m
, &f
);
1160 return log_error_errno(r
, "Failed to create serialization file: %m");
1162 /* Make sure nothing is really destructed when we shut down */
1164 bus_manager_send_reloading(m
, true);
1170 r
= manager_serialize(m
, f
, fds
, switching_root
);
1174 if (fseeko(f
, 0, SEEK_SET
) == (off_t
) -1)
1175 return log_error_errno(errno
, "Failed to rewind serialization fd: %m");
1177 r
= fd_cloexec(fileno(f
), false);
1179 return log_error_errno(r
, "Failed to disable O_CLOEXEC for serialization: %m");
1181 r
= fdset_cloexec(fds
, false);
1183 return log_error_errno(r
, "Failed to disable O_CLOEXEC for serialization fds: %m");
1185 *ret_f
= TAKE_PTR(f
);
1186 *ret_fds
= TAKE_PTR(fds
);
1191 static void bump_file_max_and_nr_open(void) {
1193 /* Let's bump fs.file-max and fs.nr_open to their respective maximums. On current kernels large numbers of file
1194 * descriptors are no longer a performance problem and their memory is properly tracked by memcg, thus counting
1195 * them and limiting them in another two layers of limits is unnecessary and just complicates things. This
1196 * function hence turns off 2 of the 4 levels of limits on file descriptors, and makes RLIMIT_NOLIMIT (soft +
1197 * hard) the only ones that really matter. */
1199 #if BUMP_PROC_SYS_FS_FILE_MAX || BUMP_PROC_SYS_FS_NR_OPEN
1203 #if BUMP_PROC_SYS_FS_FILE_MAX
1204 /* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously thing where
1205 * different but the operation would fail silently.) */
1206 r
= sysctl_writef("fs/file-max", "%li\n", LONG_MAX
);
1208 log_full_errno(IN_SET(r
, -EROFS
, -EPERM
, -EACCES
) ? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to bump fs.file-max, ignoring: %m");
1211 #if BUMP_PROC_SYS_FS_NR_OPEN
1214 /* Arg! The kernel enforces maximum and minimum values on the fs.nr_open, but we don't really know what they
1215 * are. The expression by which the maximum is determined is dependent on the architecture, and is something we
1216 * don't really want to copy to userspace, as it is dependent on implementation details of the kernel. Since
1217 * the kernel doesn't expose the maximum value to us, we can only try and hope. Hence, let's start with
1218 * INT_MAX, and then keep halving the value until we find one that works. Ugly? Yes, absolutely, but kernel
1219 * APIs are kernel APIs, so what do can we do... 🤯 */
1224 v
&= ~(__SIZEOF_POINTER__
- 1); /* Round down to next multiple of the pointer size */
1226 log_warning("Can't bump fs.nr_open, value too small.");
1232 log_error_errno(k
, "Failed to read fs.nr_open: %m");
1235 if (k
>= v
) { /* Already larger */
1236 log_debug("Skipping bump, value is already larger.");
1240 r
= sysctl_writef("fs/nr_open", "%i\n", v
);
1242 log_debug("Couldn't write fs.nr_open as %i, halving it.", v
);
1247 log_full_errno(IN_SET(r
, -EROFS
, -EPERM
, -EACCES
) ? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to bump fs.nr_open, ignoring: %m");
1251 log_debug("Successfully bumped fs.nr_open to %i", v
);
1257 static int bump_rlimit_nofile(struct rlimit
*saved_rlimit
) {
1258 struct rlimit new_rlimit
;
1261 /* Get the underlying absolute limit the kernel enforces */
1262 nr
= read_nr_open();
1264 /* Calculate the new limits to use for us. Never lower from what we inherited. */
1265 new_rlimit
= (struct rlimit
) {
1266 .rlim_cur
= MAX((rlim_t
) nr
, saved_rlimit
->rlim_cur
),
1267 .rlim_max
= MAX((rlim_t
) nr
, saved_rlimit
->rlim_max
),
1270 /* Shortcut if nothing changes. */
1271 if (saved_rlimit
->rlim_max
>= new_rlimit
.rlim_max
&&
1272 saved_rlimit
->rlim_cur
>= new_rlimit
.rlim_cur
) {
1273 log_debug("RLIMIT_NOFILE is already as high or higher than we need it, not bumping.");
1277 /* Bump up the resource limit for ourselves substantially, all the way to the maximum the kernel allows, for
1278 * both hard and soft. */
1279 r
= setrlimit_closest(RLIMIT_NOFILE
, &new_rlimit
);
1281 return log_warning_errno(r
, "Setting RLIMIT_NOFILE failed, ignoring: %m");
1286 static int bump_rlimit_memlock(struct rlimit
*saved_rlimit
) {
1287 struct rlimit new_rlimit
;
1291 /* BPF_MAP_TYPE_LPM_TRIE bpf maps are charged against RLIMIT_MEMLOCK, even if we have CAP_IPC_LOCK which should
1292 * normally disable such checks. We need them to implement IPAddressAllow= and IPAddressDeny=, hence let's bump
1293 * the value high enough for our user. */
1295 /* Using MAX() on resource limits only is safe if RLIM_INFINITY is > 0. POSIX declares that rlim_t
1296 * must be unsigned, hence this is a given, but let's make this clear here. */
1297 assert_cc(RLIM_INFINITY
> 0);
1299 mm
= physical_memory_scale(1, 8); /* Let's scale how much we allow to be locked by the amount of physical
1300 * RAM. We allow an eighth to be locked by us, just to pick a value. */
1302 new_rlimit
= (struct rlimit
) {
1303 .rlim_cur
= MAX3(HIGH_RLIMIT_MEMLOCK
, saved_rlimit
->rlim_cur
, mm
),
1304 .rlim_max
= MAX3(HIGH_RLIMIT_MEMLOCK
, saved_rlimit
->rlim_max
, mm
),
1307 if (saved_rlimit
->rlim_max
>= new_rlimit
.rlim_cur
&&
1308 saved_rlimit
->rlim_cur
>= new_rlimit
.rlim_max
) {
1309 log_debug("RLIMIT_MEMLOCK is already as high or higher than we need it, not bumping.");
1313 r
= setrlimit_closest(RLIMIT_MEMLOCK
, &new_rlimit
);
1315 return log_warning_errno(r
, "Setting RLIMIT_MEMLOCK failed, ignoring: %m");
1320 static void test_usr(void) {
1322 /* Check that /usr is either on the same file system as / or mounted already. */
1324 if (dir_is_empty("/usr") <= 0)
1327 log_warning("/usr appears to be on its own filesystem and is not already mounted. This is not a supported setup. "
1328 "Some things will probably break (sometimes even silently) in mysterious ways. "
1329 "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information.");
1332 static int enforce_syscall_archs(Set
*archs
) {
1336 if (!is_seccomp_available())
1339 r
= seccomp_restrict_archs(arg_syscall_archs
);
1341 return log_error_errno(r
, "Failed to enforce system call architecture restrication: %m");
1346 static int status_welcome(void) {
1347 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
1350 if (!show_status_on(arg_show_status
))
1353 r
= parse_os_release(NULL
,
1354 "PRETTY_NAME", &pretty_name
,
1355 "ANSI_COLOR", &ansi_color
);
1357 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
1358 "Failed to read os-release file, ignoring: %m");
1360 if (log_get_show_color())
1361 return status_printf(NULL
, 0,
1362 "\nWelcome to \x1B[%sm%s\x1B[0m!\n",
1363 isempty(ansi_color
) ? "1" : ansi_color
,
1364 isempty(pretty_name
) ? "Linux" : pretty_name
);
1366 return status_printf(NULL
, 0,
1367 "\nWelcome to %s!\n",
1368 isempty(pretty_name
) ? "Linux" : pretty_name
);
1371 static int write_container_id(void) {
1375 c
= getenv("container");
1379 RUN_WITH_UMASK(0022)
1380 r
= write_string_file("/run/systemd/container", c
, WRITE_STRING_FILE_CREATE
);
1382 return log_warning_errno(r
, "Failed to write /run/systemd/container, ignoring: %m");
1387 static int bump_unix_max_dgram_qlen(void) {
1388 _cleanup_free_
char *qlen
= NULL
;
1392 /* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel default of 16 is simply too low. We set the value
1393 * really really early during boot, so that it is actually applied to all our sockets, including the
1394 * $NOTIFY_SOCKET one. */
1396 r
= read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen
);
1398 return log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
, "Failed to read AF_UNIX datagram queue length, ignoring: %m");
1400 r
= safe_atolu(qlen
, &v
);
1402 return log_warning_errno(r
, "Failed to parse AF_UNIX datagram queue length '%s', ignoring: %m", qlen
);
1404 if (v
>= DEFAULT_UNIX_MAX_DGRAM_QLEN
)
1407 r
= write_string_filef("/proc/sys/net/unix/max_dgram_qlen", WRITE_STRING_FILE_DISABLE_BUFFER
, "%lu", DEFAULT_UNIX_MAX_DGRAM_QLEN
);
1409 return log_full_errno(IN_SET(r
, -EROFS
, -EPERM
, -EACCES
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1410 "Failed to bump AF_UNIX datagram queue length, ignoring: %m");
1415 static int fixup_environment(void) {
1416 _cleanup_free_
char *term
= NULL
;
1420 /* Only fix up the environment when we are started as PID 1 */
1421 if (getpid_cached() != 1)
1424 /* We expect the environment to be set correctly if run inside a container. */
1425 if (detect_container() > 0)
1428 /* When started as PID1, the kernel uses /dev/console for our stdios and uses TERM=linux whatever the backend
1429 * device used by the console. We try to make a better guess here since some consoles might not have support
1430 * for color mode for example.
1432 * However if TERM was configured through the kernel command line then leave it alone. */
1433 r
= proc_cmdline_get_key("TERM", 0, &term
);
1437 t
= term
?: default_term_for_tty("/dev/console");
1439 if (setenv("TERM", t
, 1) < 0)
1442 /* The kernels sets HOME=/ for init. Let's undo this. */
1443 if (path_equal_ptr(getenv("HOME"), "/"))
1444 assert_se(unsetenv("HOME") == 0);
1449 static void redirect_telinit(int argc
, char *argv
[]) {
1451 /* This is compatibility support for SysV, where calling init as a user is identical to telinit. */
1453 #if HAVE_SYSV_COMPAT
1454 if (getpid_cached() == 1)
1457 if (!invoked_as(argv
, "init"))
1460 execv(SYSTEMCTL_BINARY_PATH
, argv
);
1461 log_error_errno(errno
, "Failed to exec " SYSTEMCTL_BINARY_PATH
": %m");
1466 static int become_shutdown(
1467 const char *shutdown_verb
,
1470 char log_level
[DECIMAL_STR_MAX(int) + 1],
1471 exit_code
[DECIMAL_STR_MAX(uint8_t) + 1],
1472 timeout
[DECIMAL_STR_MAX(usec_t
) + 1];
1474 const char* command_line
[13] = {
1475 SYSTEMD_SHUTDOWN_BINARY_PATH
,
1477 "--timeout", timeout
,
1478 "--log-level", log_level
,
1482 _cleanup_strv_free_
char **env_block
= NULL
;
1485 usec_t watchdog_timer
= 0;
1487 assert(shutdown_verb
);
1488 assert(!command_line
[pos
]);
1489 env_block
= strv_copy(environ
);
1491 xsprintf(log_level
, "%d", log_get_max_level());
1492 xsprintf(timeout
, "%" PRI_USEC
"us", arg_default_timeout_stop_usec
);
1494 switch (log_get_target()) {
1496 case LOG_TARGET_KMSG
:
1497 case LOG_TARGET_JOURNAL_OR_KMSG
:
1498 case LOG_TARGET_SYSLOG_OR_KMSG
:
1499 command_line
[pos
++] = "kmsg";
1502 case LOG_TARGET_NULL
:
1503 command_line
[pos
++] = "null";
1506 case LOG_TARGET_CONSOLE
:
1508 command_line
[pos
++] = "console";
1512 if (log_get_show_color())
1513 command_line
[pos
++] = "--log-color";
1515 if (log_get_show_location())
1516 command_line
[pos
++] = "--log-location";
1518 if (log_get_show_time())
1519 command_line
[pos
++] = "--log-time";
1521 if (streq(shutdown_verb
, "exit")) {
1522 command_line
[pos
++] = "--exit-code";
1523 command_line
[pos
++] = exit_code
;
1524 xsprintf(exit_code
, "%d", retval
);
1527 assert(pos
< ELEMENTSOF(command_line
));
1529 if (streq(shutdown_verb
, "reboot"))
1530 watchdog_timer
= arg_reboot_watchdog
;
1531 else if (streq(shutdown_verb
, "kexec"))
1532 watchdog_timer
= arg_kexec_watchdog
;
1534 if (watchdog_timer
> 0 && watchdog_timer
!= USEC_INFINITY
) {
1538 /* If we reboot or kexec let's set the shutdown
1539 * watchdog and tell the shutdown binary to
1540 * repeatedly ping it */
1541 r
= watchdog_set_timeout(&watchdog_timer
);
1542 watchdog_close(r
< 0);
1544 /* Tell the binary how often to ping, ignore failure */
1545 if (asprintf(&e
, "WATCHDOG_USEC="USEC_FMT
, watchdog_timer
) > 0)
1546 (void) strv_consume(&env_block
, e
);
1548 if (arg_watchdog_device
&&
1549 asprintf(&e
, "WATCHDOG_DEVICE=%s", arg_watchdog_device
) > 0)
1550 (void) strv_consume(&env_block
, e
);
1552 watchdog_close(true);
1554 /* Avoid the creation of new processes forked by the
1555 * kernel; at this point, we will not listen to the
1557 if (detect_container() <= 0)
1558 (void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER
);
1560 execve(SYSTEMD_SHUTDOWN_BINARY_PATH
, (char **) command_line
, env_block
);
1564 static void initialize_clock(void) {
1567 /* This is called very early on, before we parse the kernel command line or otherwise figure out why
1568 * we are running, but only once. */
1570 if (clock_is_localtime(NULL
) > 0) {
1574 * The very first call of settimeofday() also does a time warp in the kernel.
1576 * In the rtc-in-local time mode, we set the kernel's timezone, and rely on external tools to take care
1577 * of maintaining the RTC and do all adjustments. This matches the behavior of Windows, which leaves
1578 * the RTC alone if the registry tells that the RTC runs in UTC.
1580 r
= clock_set_timezone(&min
);
1582 log_error_errno(r
, "Failed to apply local time delta, ignoring: %m");
1584 log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min
);
1586 } else if (!in_initrd())
1588 * Do a dummy very first call to seal the kernel's time warp magic.
1590 * Do not call this from inside the initrd. The initrd might not carry /etc/adjtime with LOCAL, but the
1591 * real system could be set up that way. In such case, we need to delay the time-warp or the sealing
1592 * until we reach the real system.
1594 * Do no set the kernel's timezone. The concept of local time cannot be supported reliably, the time
1595 * will jump or be incorrect at every daylight saving time change. All kernel local time concepts will
1596 * be treated as UTC that way.
1598 (void) clock_reset_timewarp();
1600 r
= clock_apply_epoch();
1602 log_error_errno(r
, "Current system time is before build time, but cannot correct: %m");
1604 log_info("System time before build time, advancing clock.");
1607 static void apply_clock_update(void) {
1610 /* This is called later than initialize_clock(), i.e. after we parsed configuration files/kernel
1611 * command line and such. */
1613 if (arg_clock_usec
== 0)
1616 if (getpid_cached() != 1)
1619 if (clock_settime(CLOCK_REALTIME
, timespec_store(&ts
, arg_clock_usec
)) < 0)
1620 log_error_errno(errno
, "Failed to set system clock to time specified on kernel command line: %m");
1622 char buf
[FORMAT_TIMESTAMP_MAX
];
1624 log_info("Set system clock to %s, as specified on the kernel command line.",
1625 format_timestamp(buf
, sizeof(buf
), arg_clock_usec
));
1629 static void cmdline_take_random_seed(void) {
1633 if (arg_random_seed_size
== 0)
1636 if (getpid_cached() != 1)
1639 assert(arg_random_seed
);
1640 suggested
= random_pool_size();
1642 if (arg_random_seed_size
< suggested
)
1643 log_warning("Random seed specified on kernel command line has size %zu, but %zu bytes required to fill entropy pool.",
1644 arg_random_seed_size
, suggested
);
1646 r
= random_write_entropy(-1, arg_random_seed
, arg_random_seed_size
, true);
1648 log_warning_errno(r
, "Failed to credit entropy specified on kernel command line, ignoring: %m");
1652 log_notice("Successfully credited entropy passed on kernel command line.\n"
1653 "Note that the seed provided this way is accessible to unprivileged programs. This functionality should not be used outside of testing environments.");
1656 static void initialize_coredump(bool skip_setup
) {
1658 if (getpid_cached() != 1)
1661 /* Don't limit the core dump size, so that coredump handlers such as systemd-coredump (which honour the limit)
1662 * will process core dumps for system services by default. */
1663 if (setrlimit(RLIMIT_CORE
, &RLIMIT_MAKE_CONST(RLIM_INFINITY
)) < 0)
1664 log_warning_errno(errno
, "Failed to set RLIMIT_CORE: %m");
1666 /* But at the same time, turn off the core_pattern logic by default, so that no
1667 * coredumps are stored until the systemd-coredump tool is enabled via
1668 * sysctl. However it can be changed via the kernel command line later so core
1669 * dumps can still be generated during early startup and in initramfs. */
1671 disable_coredumps();
1675 static void initialize_core_pattern(bool skip_setup
) {
1678 if (skip_setup
|| !arg_early_core_pattern
)
1681 if (getpid_cached() != 1)
1684 r
= write_string_file("/proc/sys/kernel/core_pattern", arg_early_core_pattern
, WRITE_STRING_FILE_DISABLE_BUFFER
);
1686 log_warning_errno(r
, "Failed to write '%s' to /proc/sys/kernel/core_pattern, ignoring: %m", arg_early_core_pattern
);
1689 static void update_cpu_affinity(bool skip_setup
) {
1690 _cleanup_free_
char *mask
= NULL
;
1692 if (skip_setup
|| !arg_cpu_affinity
.set
)
1695 assert(arg_cpu_affinity
.allocated
> 0);
1697 mask
= cpu_set_to_string(&arg_cpu_affinity
);
1698 log_debug("Setting CPU affinity to %s.", strnull(mask
));
1700 if (sched_setaffinity(0, arg_cpu_affinity
.allocated
, arg_cpu_affinity
.set
) < 0)
1701 log_warning_errno(errno
, "Failed to set CPU affinity: %m");
1704 static void update_numa_policy(bool skip_setup
) {
1706 _cleanup_free_
char *nodes
= NULL
;
1707 const char * policy
= NULL
;
1709 if (skip_setup
|| !mpol_is_valid(numa_policy_get_type(&arg_numa_policy
)))
1712 if (DEBUG_LOGGING
) {
1713 policy
= mpol_to_string(numa_policy_get_type(&arg_numa_policy
));
1714 nodes
= cpu_set_to_range_string(&arg_numa_policy
.nodes
);
1715 log_debug("Setting NUMA policy to %s, with nodes %s.", strnull(policy
), strnull(nodes
));
1718 r
= apply_numa_policy(&arg_numa_policy
);
1719 if (r
== -EOPNOTSUPP
)
1720 log_debug_errno(r
, "NUMA support not available, ignoring.");
1722 log_warning_errno(r
, "Failed to set NUMA memory policy: %m");
1725 static void do_reexecute(
1728 const struct rlimit
*saved_rlimit_nofile
,
1729 const struct rlimit
*saved_rlimit_memlock
,
1731 const char *switch_root_dir
,
1732 const char *switch_root_init
,
1733 const char **ret_error_message
) {
1735 unsigned i
, j
, args_size
;
1739 assert(saved_rlimit_nofile
);
1740 assert(saved_rlimit_memlock
);
1741 assert(ret_error_message
);
1743 /* Close and disarm the watchdog, so that the new instance can reinitialize it, but doesn't get rebooted while
1745 watchdog_close(true);
1747 /* Reset RLIMIT_NOFILE + RLIMIT_MEMLOCK back to the kernel defaults, so that the new systemd can pass
1748 * the kernel default to its child processes */
1749 if (saved_rlimit_nofile
->rlim_cur
!= 0)
1750 (void) setrlimit(RLIMIT_NOFILE
, saved_rlimit_nofile
);
1751 if (saved_rlimit_memlock
->rlim_cur
!= RLIM_INFINITY
)
1752 (void) setrlimit(RLIMIT_MEMLOCK
, saved_rlimit_memlock
);
1754 if (switch_root_dir
) {
1755 /* Kill all remaining processes from the initrd, but don't wait for them, so that we can handle the
1756 * SIGCHLD for them after deserializing. */
1757 broadcast_signal(SIGTERM
, false, true, arg_default_timeout_stop_usec
);
1759 /* And switch root with MS_MOVE, because we remove the old directory afterwards and detach it. */
1760 r
= switch_root(switch_root_dir
, "/mnt", true, MS_MOVE
);
1762 log_error_errno(r
, "Failed to switch root, trying to continue: %m");
1765 args_size
= MAX(6, argc
+1);
1766 args
= newa(const char*, args_size
);
1768 if (!switch_root_init
) {
1769 char sfd
[DECIMAL_STR_MAX(int) + 1];
1771 /* First try to spawn ourselves with the right path, and with full serialization. We do this only if
1772 * the user didn't specify an explicit init to spawn. */
1774 assert(arg_serialization
);
1777 xsprintf(sfd
, "%i", fileno(arg_serialization
));
1780 args
[i
++] = SYSTEMD_BINARY_PATH
;
1781 if (switch_root_dir
)
1782 args
[i
++] = "--switched-root";
1783 args
[i
++] = arg_system
? "--system" : "--user";
1784 args
[i
++] = "--deserialize";
1788 assert(i
<= args_size
);
1791 * We want valgrind to print its memory usage summary before reexecution. Valgrind won't do this is on
1792 * its own on exec(), but it will do it on exit(). Hence, to ensure we get a summary here, fork() off
1793 * a child, let it exit() cleanly, so that it prints the summary, and wait() for it in the parent,
1794 * before proceeding into the exec().
1796 valgrind_summary_hack();
1798 (void) execv(args
[0], (char* const*) args
);
1799 log_debug_errno(errno
, "Failed to execute our own binary, trying fallback: %m");
1802 /* Try the fallback, if there is any, without any serialization. We pass the original argv[] and envp[]. (Well,
1803 * modulo the ordering changes due to getopt() in argv[], and some cleanups in envp[], but let's hope that
1804 * doesn't matter.) */
1806 arg_serialization
= safe_fclose(arg_serialization
);
1807 fds
= fdset_free(fds
);
1809 /* Reopen the console */
1810 (void) make_console_stdio();
1812 for (j
= 1, i
= 1; j
< (unsigned) argc
; j
++)
1813 args
[i
++] = argv
[j
];
1815 assert(i
<= args_size
);
1817 /* Re-enable any blocked signals, especially important if we switch from initial ramdisk to init=... */
1818 (void) reset_all_signal_handlers();
1819 (void) reset_signal_mask();
1820 (void) rlimit_nofile_safe();
1822 if (switch_root_init
) {
1823 args
[0] = switch_root_init
;
1824 (void) execve(args
[0], (char* const*) args
, saved_env
);
1825 log_warning_errno(errno
, "Failed to execute configured init, trying fallback: %m");
1828 args
[0] = "/sbin/init";
1829 (void) execv(args
[0], (char* const*) args
);
1832 manager_status_printf(NULL
, STATUS_TYPE_EMERGENCY
,
1833 ANSI_HIGHLIGHT_RED
" !! " ANSI_NORMAL
,
1834 "Failed to execute /sbin/init");
1837 log_warning("No /sbin/init, trying fallback");
1839 args
[0] = "/bin/sh";
1841 (void) execve(args
[0], (char* const*) args
, saved_env
);
1842 log_error_errno(errno
, "Failed to execute /bin/sh, giving up: %m");
1844 log_warning_errno(r
, "Failed to execute /sbin/init, giving up: %m");
1846 *ret_error_message
= "Failed to execute fallback shell";
1849 static int invoke_main_loop(
1851 const struct rlimit
*saved_rlimit_nofile
,
1852 const struct rlimit
*saved_rlimit_memlock
,
1853 bool *ret_reexecute
,
1854 int *ret_retval
, /* Return parameters relevant for shutting down */
1855 const char **ret_shutdown_verb
, /* … */
1856 FDSet
**ret_fds
, /* Return parameters for reexecuting */
1857 char **ret_switch_root_dir
, /* … */
1858 char **ret_switch_root_init
, /* … */
1859 const char **ret_error_message
) {
1864 assert(saved_rlimit_nofile
);
1865 assert(saved_rlimit_memlock
);
1866 assert(ret_reexecute
);
1868 assert(ret_shutdown_verb
);
1870 assert(ret_switch_root_dir
);
1871 assert(ret_switch_root_init
);
1872 assert(ret_error_message
);
1875 r
= manager_loop(m
);
1877 *ret_error_message
= "Failed to run main loop";
1878 return log_emergency_errno(r
, "Failed to run main loop: %m");
1881 switch ((ManagerObjective
) r
) {
1883 case MANAGER_RELOAD
: {
1884 LogTarget saved_log_target
;
1885 int saved_log_level
;
1887 log_info("Reloading.");
1889 /* First, save any overridden log level/target, then parse the configuration file, which might
1890 * change the log level to new settings. */
1892 saved_log_level
= m
->log_level_overridden
? log_get_max_level() : -1;
1893 saved_log_target
= m
->log_target_overridden
? log_get_target() : _LOG_TARGET_INVALID
;
1895 (void) parse_configuration(saved_rlimit_nofile
, saved_rlimit_memlock
);
1897 set_manager_defaults(m
);
1898 set_manager_settings(m
);
1900 update_cpu_affinity(false);
1901 update_numa_policy(false);
1903 if (saved_log_level
>= 0)
1904 manager_override_log_level(m
, saved_log_level
);
1905 if (saved_log_target
>= 0)
1906 manager_override_log_target(m
, saved_log_target
);
1908 r
= manager_reload(m
);
1910 /* Reloading failed before the point of no return. Let's continue running as if nothing happened. */
1911 m
->objective
= MANAGER_OK
;
1916 case MANAGER_REEXECUTE
:
1918 r
= prepare_reexecute(m
, &arg_serialization
, ret_fds
, false);
1920 *ret_error_message
= "Failed to prepare for reexecution";
1924 log_notice("Reexecuting.");
1926 *ret_reexecute
= true;
1927 *ret_retval
= EXIT_SUCCESS
;
1928 *ret_shutdown_verb
= NULL
;
1929 *ret_switch_root_dir
= *ret_switch_root_init
= NULL
;
1933 case MANAGER_SWITCH_ROOT
:
1934 if (!m
->switch_root_init
) {
1935 r
= prepare_reexecute(m
, &arg_serialization
, ret_fds
, true);
1937 *ret_error_message
= "Failed to prepare for reexecution";
1943 log_notice("Switching root.");
1945 *ret_reexecute
= true;
1946 *ret_retval
= EXIT_SUCCESS
;
1947 *ret_shutdown_verb
= NULL
;
1949 /* Steal the switch root parameters */
1950 *ret_switch_root_dir
= TAKE_PTR(m
->switch_root
);
1951 *ret_switch_root_init
= TAKE_PTR(m
->switch_root_init
);
1957 if (MANAGER_IS_USER(m
)) {
1960 *ret_reexecute
= false;
1961 *ret_retval
= m
->return_value
;
1962 *ret_shutdown_verb
= NULL
;
1964 *ret_switch_root_dir
= *ret_switch_root_init
= NULL
;
1970 case MANAGER_REBOOT
:
1971 case MANAGER_POWEROFF
:
1973 case MANAGER_KEXEC
: {
1974 static const char * const table
[_MANAGER_OBJECTIVE_MAX
] = {
1975 [MANAGER_EXIT
] = "exit",
1976 [MANAGER_REBOOT
] = "reboot",
1977 [MANAGER_POWEROFF
] = "poweroff",
1978 [MANAGER_HALT
] = "halt",
1979 [MANAGER_KEXEC
] = "kexec",
1982 log_notice("Shutting down.");
1984 *ret_reexecute
= false;
1985 *ret_retval
= m
->return_value
;
1986 assert_se(*ret_shutdown_verb
= table
[m
->objective
]);
1988 *ret_switch_root_dir
= *ret_switch_root_init
= NULL
;
1994 assert_not_reached("Unknown or unexpected manager objective.");
1999 static void log_execution_mode(bool *ret_first_boot
) {
2000 assert(ret_first_boot
);
2005 log_info("systemd " GIT_VERSION
" running in %ssystem mode. (%s)",
2006 arg_action
== ACTION_TEST
? "test " : "",
2009 v
= detect_virtualization();
2011 log_info("Detected virtualization %s.", virtualization_to_string(v
));
2013 log_info("Detected architecture %s.", architecture_to_string(uname_architecture()));
2016 *ret_first_boot
= false;
2017 log_info("Running in initial RAM disk.");
2020 _cleanup_free_
char *id_text
= NULL
;
2022 /* Let's check whether we are in first boot. We use /etc/machine-id as flag file
2023 * for this: If it is missing or contains the value "uninitialized", this is the
2024 * first boot. In any other case, it is not. This allows container managers and
2025 * installers to provision a couple of files already. If the container manager
2026 * wants to provision the machine ID itself it should pass $container_uuid to PID 1. */
2028 r
= read_one_line_file("/etc/machine-id", &id_text
);
2029 if (r
< 0 || streq(id_text
, "uninitialized")) {
2030 if (r
< 0 && r
!= -ENOENT
)
2031 log_warning_errno(r
, "Unexpected error while reading /etc/machine-id, ignoring: %m");
2033 *ret_first_boot
= true;
2034 log_info("Detected first boot.");
2036 *ret_first_boot
= false;
2037 log_debug("Detected initialized system, this is not the first boot.");
2041 if (DEBUG_LOGGING
) {
2042 _cleanup_free_
char *t
;
2044 t
= uid_to_name(getuid());
2045 log_debug("systemd " GIT_VERSION
" running in %suser mode for user " UID_FMT
"/%s. (%s)",
2046 arg_action
== ACTION_TEST
? " test" : "",
2047 getuid(), strna(t
), systemd_features
);
2050 *ret_first_boot
= false;
2054 static int initialize_runtime(
2057 struct rlimit
*saved_rlimit_nofile
,
2058 struct rlimit
*saved_rlimit_memlock
,
2059 const char **ret_error_message
) {
2062 assert(ret_error_message
);
2064 /* Sets up various runtime parameters. Many of these initializations are conditionalized:
2066 * - Some only apply to --system instances
2067 * - Some only apply to --user instances
2068 * - Some only apply when we first start up, but not when we reexecute
2071 if (arg_action
!= ACTION_RUN
)
2074 update_cpu_affinity(skip_setup
);
2075 update_numa_policy(skip_setup
);
2078 /* Make sure we leave a core dump without panicking the kernel. */
2079 install_crash_handler();
2082 r
= mount_cgroup_controllers();
2084 *ret_error_message
= "Failed to mount cgroup hierarchies";
2089 (void) hostname_setup(true);
2090 /* Force transient machine-id on first boot. */
2091 machine_id_setup(NULL
, first_boot
, arg_machine_id
, NULL
);
2092 (void) loopback_setup();
2093 bump_unix_max_dgram_qlen();
2094 bump_file_max_and_nr_open();
2096 write_container_id();
2099 if (arg_watchdog_device
) {
2100 r
= watchdog_set_device(arg_watchdog_device
);
2102 log_warning_errno(r
, "Failed to set watchdog device to %s, ignoring: %m", arg_watchdog_device
);
2105 _cleanup_free_
char *p
= NULL
;
2107 /* Create the runtime directory and place the inaccessible device nodes there, if we run in
2108 * user mode. In system mode mount_setup() already did that. */
2110 r
= xdg_user_runtime_dir(&p
, "/systemd");
2112 *ret_error_message
= "$XDG_RUNTIME_DIR is not set";
2113 return log_emergency_errno(r
, "Failed to determine $XDG_RUNTIME_DIR path: %m");
2116 (void) mkdir_p_label(p
, 0755);
2117 (void) make_inaccessible_nodes(p
, UID_INVALID
, GID_INVALID
);
2120 if (arg_timer_slack_nsec
!= NSEC_INFINITY
)
2121 if (prctl(PR_SET_TIMERSLACK
, arg_timer_slack_nsec
) < 0)
2122 log_warning_errno(errno
, "Failed to adjust timer slack, ignoring: %m");
2124 if (arg_system
&& !cap_test_all(arg_capability_bounding_set
)) {
2125 r
= capability_bounding_set_drop_usermode(arg_capability_bounding_set
);
2127 *ret_error_message
= "Failed to drop capability bounding set of usermode helpers";
2128 return log_emergency_errno(r
, "Failed to drop capability bounding set of usermode helpers: %m");
2131 r
= capability_bounding_set_drop(arg_capability_bounding_set
, true);
2133 *ret_error_message
= "Failed to drop capability bounding set";
2134 return log_emergency_errno(r
, "Failed to drop capability bounding set: %m");
2138 if (arg_system
&& arg_no_new_privs
) {
2139 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) < 0) {
2140 *ret_error_message
= "Failed to disable new privileges";
2141 return log_emergency_errno(errno
, "Failed to disable new privileges: %m");
2145 if (arg_syscall_archs
) {
2146 r
= enforce_syscall_archs(arg_syscall_archs
);
2148 *ret_error_message
= "Failed to set syscall architectures";
2154 /* Become reaper of our children */
2155 if (prctl(PR_SET_CHILD_SUBREAPER
, 1) < 0)
2156 log_warning_errno(errno
, "Failed to make us a subreaper: %m");
2158 /* Bump up RLIMIT_NOFILE for systemd itself */
2159 (void) bump_rlimit_nofile(saved_rlimit_nofile
);
2160 (void) bump_rlimit_memlock(saved_rlimit_memlock
);
2165 static int do_queue_default_job(
2167 const char **ret_error_message
) {
2169 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2175 if (arg_default_unit
)
2176 unit
= arg_default_unit
;
2177 else if (in_initrd())
2178 unit
= SPECIAL_INITRD_TARGET
;
2180 unit
= SPECIAL_DEFAULT_TARGET
;
2182 log_debug("Activating default unit: %s", unit
);
2184 r
= manager_load_startable_unit_or_warn(m
, unit
, NULL
, &target
);
2185 if (r
< 0 && in_initrd() && !arg_default_unit
) {
2186 /* Fall back to default.target, which we used to always use by default. Only do this if no
2187 * explicit configuration was given. */
2189 log_info("Falling back to " SPECIAL_DEFAULT_TARGET
".");
2191 r
= manager_load_startable_unit_or_warn(m
, SPECIAL_DEFAULT_TARGET
, NULL
, &target
);
2194 log_info("Falling back to " SPECIAL_RESCUE_TARGET
".");
2196 r
= manager_load_startable_unit_or_warn(m
, SPECIAL_RESCUE_TARGET
, NULL
, &target
);
2198 *ret_error_message
= r
== -ERFKILL
? SPECIAL_RESCUE_TARGET
" masked"
2199 : "Failed to load " SPECIAL_RESCUE_TARGET
;
2204 assert(target
->load_state
== UNIT_LOADED
);
2206 r
= manager_add_job(m
, JOB_START
, target
, JOB_ISOLATE
, NULL
, &error
, &job
);
2208 log_debug_errno(r
, "Default target could not be isolated, starting instead: %s", bus_error_message(&error
, r
));
2210 sd_bus_error_free(&error
);
2212 r
= manager_add_job(m
, JOB_START
, target
, JOB_REPLACE
, NULL
, &error
, &job
);
2214 *ret_error_message
= "Failed to start default target";
2215 return log_emergency_errno(r
, "Failed to start default target: %s", bus_error_message(&error
, r
));
2219 *ret_error_message
= "Failed to isolate default target";
2220 return log_emergency_errno(r
, "Failed to isolate default target: %s", bus_error_message(&error
, r
));
2222 log_info("Queued %s job for default target %s.",
2223 job_type_to_string(job
->type
),
2224 unit_status_string(job
->unit
));
2226 m
->default_unit_job_id
= job
->id
;
2231 static void save_rlimits(struct rlimit
*saved_rlimit_nofile
,
2232 struct rlimit
*saved_rlimit_memlock
) {
2234 assert(saved_rlimit_nofile
);
2235 assert(saved_rlimit_memlock
);
2237 if (getrlimit(RLIMIT_NOFILE
, saved_rlimit_nofile
) < 0)
2238 log_warning_errno(errno
, "Reading RLIMIT_NOFILE failed, ignoring: %m");
2240 if (getrlimit(RLIMIT_MEMLOCK
, saved_rlimit_memlock
) < 0)
2241 log_warning_errno(errno
, "Reading RLIMIT_MEMLOCK failed, ignoring: %m");
2244 static void fallback_rlimit_nofile(const struct rlimit
*saved_rlimit_nofile
) {
2247 if (arg_default_rlimit
[RLIMIT_NOFILE
])
2250 /* Make sure forked processes get limits based on the original kernel setting */
2252 rl
= newdup(struct rlimit
, saved_rlimit_nofile
, 1);
2258 /* Bump the hard limit for system services to a substantially higher value. The default
2259 * hard limit current kernels set is pretty low (4K), mostly for historical
2260 * reasons. According to kernel developers, the fd handling in recent kernels has been
2261 * optimized substantially enough, so that we can bump the limit now, without paying too
2262 * high a price in memory or performance. Note however that we only bump the hard limit,
2263 * not the soft limit. That's because select() works the way it works, and chokes on fds
2264 * >= 1024. If we'd bump the soft limit globally, it might accidentally happen to
2265 * unexpecting programs that they get fds higher than what they can process using
2266 * select(). By only bumping the hard limit but leaving the low limit as it is we avoid
2267 * this pitfall: programs that are written by folks aware of the select() problem in mind
2268 * (and thus use poll()/epoll instead of select(), the way everybody should) can
2269 * explicitly opt into high fds by bumping their soft limit beyond 1024, to the hard limit
2274 /* Get the underlying absolute limit the kernel enforces */
2275 nr
= read_nr_open();
2277 rl
->rlim_max
= MIN((rlim_t
) nr
, MAX(rl
->rlim_max
, (rlim_t
) HIGH_RLIMIT_NOFILE
));
2280 /* If for some reason we were invoked with a soft limit above 1024 (which should never
2281 * happen!, but who knows what we get passed in from pam_limit when invoked as --user
2282 * instance), then lower what we pass on to not confuse our children */
2283 rl
->rlim_cur
= MIN(rl
->rlim_cur
, (rlim_t
) FD_SETSIZE
);
2285 arg_default_rlimit
[RLIMIT_NOFILE
] = rl
;
2288 static void fallback_rlimit_memlock(const struct rlimit
*saved_rlimit_memlock
) {
2291 /* Pass the original value down to invoked processes */
2293 if (arg_default_rlimit
[RLIMIT_MEMLOCK
])
2296 rl
= newdup(struct rlimit
, saved_rlimit_memlock
, 1);
2302 arg_default_rlimit
[RLIMIT_MEMLOCK
] = rl
;
2305 static void setenv_manager_environment(void) {
2309 STRV_FOREACH(p
, arg_manager_environment
) {
2310 log_debug("Setting '%s' in our own environment.", *p
);
2312 r
= putenv_dup(*p
, true);
2314 log_warning_errno(errno
, "Failed to setenv \"%s\", ignoring: %m", *p
);
2318 static void reset_arguments(void) {
2319 /* Frees/resets arg_* variables, with a few exceptions commented below. */
2321 arg_default_unit
= mfree(arg_default_unit
);
2323 /* arg_system — ignore */
2325 arg_dump_core
= true;
2326 arg_crash_chvt
= -1;
2327 arg_crash_shell
= false;
2328 arg_crash_reboot
= false;
2329 arg_confirm_spawn
= mfree(arg_confirm_spawn
);
2330 arg_show_status
= _SHOW_STATUS_INVALID
;
2331 arg_status_unit_format
= STATUS_UNIT_FORMAT_DEFAULT
;
2332 arg_switched_root
= false;
2333 arg_pager_flags
= 0;
2334 arg_service_watchdogs
= true;
2335 arg_default_std_output
= EXEC_OUTPUT_JOURNAL
;
2336 arg_default_std_error
= EXEC_OUTPUT_INHERIT
;
2337 arg_default_restart_usec
= DEFAULT_RESTART_USEC
;
2338 arg_default_timeout_start_usec
= DEFAULT_TIMEOUT_USEC
;
2339 arg_default_timeout_stop_usec
= DEFAULT_TIMEOUT_USEC
;
2340 arg_default_timeout_abort_usec
= DEFAULT_TIMEOUT_USEC
;
2341 arg_default_timeout_abort_set
= false;
2342 arg_default_start_limit_interval
= DEFAULT_START_LIMIT_INTERVAL
;
2343 arg_default_start_limit_burst
= DEFAULT_START_LIMIT_BURST
;
2344 arg_runtime_watchdog
= 0;
2345 arg_reboot_watchdog
= 10 * USEC_PER_MINUTE
;
2346 arg_kexec_watchdog
= 0;
2347 arg_early_core_pattern
= NULL
;
2348 arg_watchdog_device
= NULL
;
2350 arg_default_environment
= strv_free(arg_default_environment
);
2351 arg_manager_environment
= strv_free(arg_manager_environment
);
2352 rlimit_free_all(arg_default_rlimit
);
2354 arg_capability_bounding_set
= CAP_ALL
;
2355 arg_no_new_privs
= false;
2356 arg_timer_slack_nsec
= NSEC_INFINITY
;
2357 arg_default_timer_accuracy_usec
= 1 * USEC_PER_MINUTE
;
2359 arg_syscall_archs
= set_free(arg_syscall_archs
);
2361 /* arg_serialization — ignore */
2363 arg_default_cpu_accounting
= -1;
2364 arg_default_io_accounting
= false;
2365 arg_default_ip_accounting
= false;
2366 arg_default_blockio_accounting
= false;
2367 arg_default_memory_accounting
= MEMORY_ACCOUNTING_DEFAULT
;
2368 arg_default_tasks_accounting
= true;
2369 arg_default_tasks_max
= DEFAULT_TASKS_MAX
;
2370 arg_machine_id
= (sd_id128_t
) {};
2371 arg_cad_burst_action
= EMERGENCY_ACTION_REBOOT_FORCE
;
2372 arg_default_oom_policy
= OOM_STOP
;
2374 cpu_set_reset(&arg_cpu_affinity
);
2375 numa_policy_reset(&arg_numa_policy
);
2377 arg_random_seed
= mfree(arg_random_seed
);
2378 arg_random_seed_size
= 0;
2382 static int parse_configuration(const struct rlimit
*saved_rlimit_nofile
,
2383 const struct rlimit
*saved_rlimit_memlock
) {
2386 assert(saved_rlimit_nofile
);
2387 assert(saved_rlimit_memlock
);
2389 /* Assign configuration defaults */
2392 r
= parse_config_file();
2394 log_warning_errno(r
, "Failed to parse config file, ignoring: %m");
2397 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
2399 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
2402 /* Initialize some default rlimits for services if they haven't been configured */
2403 fallback_rlimit_nofile(saved_rlimit_nofile
);
2404 fallback_rlimit_memlock(saved_rlimit_memlock
);
2406 /* Note that this also parses bits from the kernel command line, including "debug". */
2407 log_parse_environment();
2409 /* Initialize the show status setting if it hasn't been set explicitly yet */
2410 if (arg_show_status
== _SHOW_STATUS_INVALID
)
2411 arg_show_status
= SHOW_STATUS_YES
;
2413 /* Push variables into the manager environment block */
2414 setenv_manager_environment();
2419 static int safety_checks(void) {
2421 if (getpid_cached() == 1 &&
2422 arg_action
!= ACTION_RUN
)
2423 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
2424 "Unsupported execution mode while PID 1.");
2426 if (getpid_cached() == 1 &&
2428 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
2429 "Can't run --user mode as PID 1.");
2431 if (arg_action
== ACTION_RUN
&&
2433 getpid_cached() != 1)
2434 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
2435 "Can't run system mode unless PID 1.");
2437 if (arg_action
== ACTION_TEST
&&
2439 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
2440 "Don't run test mode as root.");
2443 arg_action
== ACTION_RUN
&&
2445 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
2446 "Trying to run as user instance, but the system has not been booted with systemd.");
2449 arg_action
== ACTION_RUN
&&
2450 !getenv("XDG_RUNTIME_DIR"))
2451 return log_error_errno(SYNTHETIC_ERRNO(EUNATCH
),
2452 "Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");
2455 arg_action
== ACTION_RUN
&&
2456 running_in_chroot() > 0)
2457 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
2458 "Cannot be run in a chroot() environment.");
2463 static int initialize_security(
2464 bool *loaded_policy
,
2465 dual_timestamp
*security_start_timestamp
,
2466 dual_timestamp
*security_finish_timestamp
,
2467 const char **ret_error_message
) {
2471 assert(loaded_policy
);
2472 assert(security_start_timestamp
);
2473 assert(security_finish_timestamp
);
2474 assert(ret_error_message
);
2476 dual_timestamp_get(security_start_timestamp
);
2478 r
= mac_selinux_setup(loaded_policy
);
2480 *ret_error_message
= "Failed to load SELinux policy";
2484 r
= mac_smack_setup(loaded_policy
);
2486 *ret_error_message
= "Failed to load SMACK policy";
2490 r
= mac_apparmor_setup();
2492 *ret_error_message
= "Failed to load AppArmor policy";
2498 *ret_error_message
= "Failed to load IMA policy";
2502 dual_timestamp_get(security_finish_timestamp
);
2506 static void test_summary(Manager
*m
) {
2509 printf("-> By units:\n");
2510 manager_dump_units(m
, stdout
, "\t");
2512 printf("-> By jobs:\n");
2513 manager_dump_jobs(m
, stdout
, "\t");
2516 static int collect_fds(FDSet
**ret_fds
, const char **ret_error_message
) {
2520 assert(ret_error_message
);
2522 r
= fdset_new_fill(ret_fds
);
2524 *ret_error_message
= "Failed to allocate fd set";
2525 return log_emergency_errno(r
, "Failed to allocate fd set: %m");
2528 fdset_cloexec(*ret_fds
, true);
2530 if (arg_serialization
)
2531 assert_se(fdset_remove(*ret_fds
, fileno(arg_serialization
)) >= 0);
2536 static void setup_console_terminal(bool skip_setup
) {
2541 /* Become a session leader if we aren't one yet. */
2544 /* If we are init, we connect stdin/stdout/stderr to /dev/null and make sure we don't have a controlling
2546 (void) release_terminal();
2548 /* Reset the console, but only if this is really init and we are freshly booted */
2549 if (getpid_cached() == 1 && !skip_setup
)
2550 (void) console_setup();
2553 static bool early_skip_setup_check(int argc
, char *argv
[]) {
2554 bool found_deserialize
= false;
2557 /* Determine if this is a reexecution or normal bootup. We do the full command line parsing much later, so
2558 * let's just have a quick peek here. Note that if we have switched root, do all the special setup things
2559 * anyway, even if in that case we also do deserialization. */
2561 for (i
= 1; i
< argc
; i
++) {
2562 if (streq(argv
[i
], "--switched-root"))
2563 return false; /* If we switched root, don't skip the setup. */
2564 else if (streq(argv
[i
], "--deserialize"))
2565 found_deserialize
= true;
2568 return found_deserialize
; /* When we are deserializing, then we are reexecuting, hence avoid the extensive setup */
2571 static int save_env(void) {
2574 l
= strv_copy(environ
);
2578 strv_free_and_replace(saved_env
, l
);
2582 int main(int argc
, char *argv
[]) {
2584 dual_timestamp initrd_timestamp
= DUAL_TIMESTAMP_NULL
, userspace_timestamp
= DUAL_TIMESTAMP_NULL
, kernel_timestamp
= DUAL_TIMESTAMP_NULL
,
2585 security_start_timestamp
= DUAL_TIMESTAMP_NULL
, security_finish_timestamp
= DUAL_TIMESTAMP_NULL
;
2586 struct rlimit saved_rlimit_nofile
= RLIMIT_MAKE_CONST(0),
2587 saved_rlimit_memlock
= RLIMIT_MAKE_CONST(RLIM_INFINITY
); /* The original rlimits we passed
2588 * in. Note we use different values
2589 * for the two that indicate whether
2590 * these fields are initialized! */
2591 bool skip_setup
, loaded_policy
= false, queue_default_job
= false, first_boot
= false, reexecute
= false;
2592 char *switch_root_dir
= NULL
, *switch_root_init
= NULL
;
2593 usec_t before_startup
, after_startup
;
2594 static char systemd
[] = "systemd";
2595 char timespan
[FORMAT_TIMESPAN_MAX
];
2596 const char *shutdown_verb
= NULL
, *error_message
= NULL
;
2597 int r
, retval
= EXIT_FAILURE
;
2601 /* SysV compatibility: redirect init → telinit */
2602 redirect_telinit(argc
, argv
);
2604 /* Take timestamps early on */
2605 dual_timestamp_from_monotonic(&kernel_timestamp
, 0);
2606 dual_timestamp_get(&userspace_timestamp
);
2608 /* Figure out whether we need to do initialize the system, or if we already did that because we are
2610 skip_setup
= early_skip_setup_check(argc
, argv
);
2612 /* If we get started via the /sbin/init symlink then we are called 'init'. After a subsequent reexecution we
2613 * are then called 'systemd'. That is confusing, hence let's call us systemd right-away. */
2614 program_invocation_short_name
= systemd
;
2615 (void) prctl(PR_SET_NAME
, systemd
);
2617 /* Save the original command line */
2618 save_argc_argv(argc
, argv
);
2620 /* Save the original environment as we might need to restore it if we're requested to execute another
2621 * system manager later. */
2624 error_message
= "Failed to copy environment block";
2628 /* Make sure that if the user says "syslog" we actually log to the journal. */
2629 log_set_upgrade_syslog_to_journal(true);
2631 if (getpid_cached() == 1) {
2632 /* When we run as PID 1 force system mode */
2635 /* Disable the umask logic */
2638 /* Make sure that at least initially we do not ever log to journald/syslogd, because it might not be
2639 * activated yet (even though the log socket for it exists). */
2640 log_set_prohibit_ipc(true);
2642 /* Always reopen /dev/console when running as PID 1 or one of its pre-execve() children. This is
2643 * important so that we never end up logging to any foreign stderr, for example if we have to log in a
2644 * child process right before execve()'ing the actual binary, at a point in time where socket
2645 * activation stderr/stdout area already set up. */
2646 log_set_always_reopen_console(true);
2648 if (detect_container() <= 0) {
2650 /* Running outside of a container as PID 1 */
2651 log_set_target(LOG_TARGET_KMSG
);
2655 initrd_timestamp
= userspace_timestamp
;
2658 r
= mount_setup_early();
2660 error_message
= "Failed to mount early API filesystems";
2664 /* Let's open the log backend a second time, in case the first time didn't
2665 * work. Quite possibly we have mounted /dev just now, so /dev/kmsg became
2666 * available, and it previously wasn't. */
2669 disable_printk_ratelimit();
2671 r
= initialize_security(
2673 &security_start_timestamp
,
2674 &security_finish_timestamp
,
2680 if (mac_selinux_init() < 0) {
2681 error_message
= "Failed to initialize SELinux support";
2688 /* Set the default for later on, but don't actually open the logs like this for now. Note that
2689 * if we are transitioning from the initrd there might still be journal fd open, and we
2690 * shouldn't attempt opening that before we parsed /proc/cmdline which might redirect output
2692 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG
);
2695 /* Running inside a container, as PID 1 */
2696 log_set_target(LOG_TARGET_CONSOLE
);
2699 /* For later on, see above... */
2700 log_set_target(LOG_TARGET_JOURNAL
);
2702 /* clear the kernel timestamp, because we are in a container */
2703 kernel_timestamp
= DUAL_TIMESTAMP_NULL
;
2706 initialize_coredump(skip_setup
);
2708 r
= fixup_environment();
2710 log_emergency_errno(r
, "Failed to fix up PID 1 environment: %m");
2711 error_message
= "Failed to fix up PID1 environment";
2715 /* Try to figure out if we can use colors with the console. No need to do that for user instances since
2716 * they never log into the console. */
2717 log_show_color(colors_enabled());
2719 r
= make_null_stdio();
2721 log_warning_errno(r
, "Failed to redirect standard streams to /dev/null, ignoring: %m");
2723 /* Load the kernel modules early. */
2727 /* Mount /proc, /sys and friends, so that /proc/cmdline and /proc/$PID/fd is available. */
2728 r
= mount_setup(loaded_policy
, skip_setup
);
2730 error_message
= "Failed to mount API filesystems";
2734 /* The efivarfs is now mounted, let's read the random seed off it */
2735 (void) efi_take_random_seed();
2737 /* Cache command-line options passed from EFI variables */
2739 (void) cache_efi_options_variable();
2741 /* Running as user instance */
2743 log_set_target(LOG_TARGET_AUTO
);
2746 /* clear the kernel timestamp, because we are not PID 1 */
2747 kernel_timestamp
= DUAL_TIMESTAMP_NULL
;
2749 if (mac_selinux_init() < 0) {
2750 error_message
= "Failed to initialize SELinux support";
2755 /* Save the original RLIMIT_NOFILE/RLIMIT_MEMLOCK so that we can reset it later when
2756 * transitioning from the initrd to the main systemd or suchlike. */
2757 save_rlimits(&saved_rlimit_nofile
, &saved_rlimit_memlock
);
2759 /* Reset all signal handlers. */
2760 (void) reset_all_signal_handlers();
2761 (void) ignore_signals(SIGNALS_IGNORE
, -1);
2763 (void) parse_configuration(&saved_rlimit_nofile
, &saved_rlimit_memlock
);
2765 r
= parse_argv(argc
, argv
);
2767 error_message
= "Failed to parse commandline arguments";
2771 r
= safety_checks();
2775 if (IN_SET(arg_action
, ACTION_TEST
, ACTION_HELP
, ACTION_DUMP_CONFIGURATION_ITEMS
, ACTION_DUMP_BUS_PROPERTIES
, ACTION_BUS_INTROSPECT
))
2776 (void) pager_open(arg_pager_flags
);
2778 if (arg_action
!= ACTION_RUN
)
2781 if (arg_action
== ACTION_HELP
) {
2782 retval
= help() < 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
2784 } else if (arg_action
== ACTION_VERSION
) {
2787 } else if (arg_action
== ACTION_DUMP_CONFIGURATION_ITEMS
) {
2788 unit_dump_config_items(stdout
);
2789 retval
= EXIT_SUCCESS
;
2791 } else if (arg_action
== ACTION_DUMP_BUS_PROPERTIES
) {
2792 dump_bus_properties(stdout
);
2793 retval
= EXIT_SUCCESS
;
2795 } else if (arg_action
== ACTION_BUS_INTROSPECT
) {
2796 r
= bus_manager_introspect_implementations(stdout
, arg_bus_introspect
);
2797 retval
= r
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
2801 assert_se(IN_SET(arg_action
, ACTION_RUN
, ACTION_TEST
));
2803 /* Move out of the way, so that we won't block unmounts */
2804 assert_se(chdir("/") == 0);
2806 if (arg_action
== ACTION_RUN
) {
2808 /* Apply the systemd.clock_usec= kernel command line switch */
2809 apply_clock_update();
2811 /* Apply random seed from kernel command line */
2812 cmdline_take_random_seed();
2815 /* A core pattern might have been specified via the cmdline. */
2816 initialize_core_pattern(skip_setup
);
2818 /* Close logging fds, in order not to confuse collecting passed fds and terminal logic below */
2821 /* Remember open file descriptors for later deserialization */
2822 r
= collect_fds(&fds
, &error_message
);
2826 /* Give up any control of the console, but make sure its initialized. */
2827 setup_console_terminal(skip_setup
);
2829 /* Open the logging devices, if possible and necessary */
2833 log_execution_mode(&first_boot
);
2835 r
= initialize_runtime(skip_setup
,
2837 &saved_rlimit_nofile
,
2838 &saved_rlimit_memlock
,
2843 r
= manager_new(arg_system
? UNIT_FILE_SYSTEM
: UNIT_FILE_USER
,
2844 arg_action
== ACTION_TEST
? MANAGER_TEST_FULL
: 0,
2847 log_emergency_errno(r
, "Failed to allocate manager object: %m");
2848 error_message
= "Failed to allocate manager object";
2852 m
->timestamps
[MANAGER_TIMESTAMP_KERNEL
] = kernel_timestamp
;
2853 m
->timestamps
[MANAGER_TIMESTAMP_INITRD
] = initrd_timestamp
;
2854 m
->timestamps
[MANAGER_TIMESTAMP_USERSPACE
] = userspace_timestamp
;
2855 m
->timestamps
[manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_SECURITY_START
)] = security_start_timestamp
;
2856 m
->timestamps
[manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_SECURITY_FINISH
)] = security_finish_timestamp
;
2858 set_manager_defaults(m
);
2859 set_manager_settings(m
);
2860 manager_set_first_boot(m
, first_boot
);
2862 /* Remember whether we should queue the default job */
2863 queue_default_job
= !arg_serialization
|| arg_switched_root
;
2865 before_startup
= now(CLOCK_MONOTONIC
);
2867 r
= manager_startup(m
, arg_serialization
, fds
);
2869 error_message
= "Failed to start up manager";
2873 /* This will close all file descriptors that were opened, but not claimed by any unit. */
2874 fds
= fdset_free(fds
);
2875 arg_serialization
= safe_fclose(arg_serialization
);
2877 if (queue_default_job
) {
2878 r
= do_queue_default_job(m
, &error_message
);
2883 after_startup
= now(CLOCK_MONOTONIC
);
2885 log_full(arg_action
== ACTION_TEST
? LOG_INFO
: LOG_DEBUG
,
2886 "Loaded units and determined initial transaction in %s.",
2887 format_timespan(timespan
, sizeof(timespan
), after_startup
- before_startup
, 100 * USEC_PER_MSEC
));
2889 if (arg_action
== ACTION_TEST
) {
2891 retval
= EXIT_SUCCESS
;
2895 (void) invoke_main_loop(m
,
2896 &saved_rlimit_nofile
,
2897 &saved_rlimit_memlock
,
2910 arg_reboot_watchdog
= manager_get_watchdog(m
, WATCHDOG_REBOOT
);
2911 arg_kexec_watchdog
= manager_get_watchdog(m
, WATCHDOG_KEXEC
);
2912 m
= manager_free(m
);
2915 mac_selinux_finish();
2918 do_reexecute(argc
, argv
,
2919 &saved_rlimit_nofile
,
2920 &saved_rlimit_memlock
,
2924 &error_message
); /* This only returns if reexecution failed */
2926 arg_serialization
= safe_fclose(arg_serialization
);
2927 fds
= fdset_free(fds
);
2929 saved_env
= strv_free(saved_env
);
2931 #if HAVE_VALGRIND_VALGRIND_H
2932 /* If we are PID 1 and running under valgrind, then let's exit
2933 * here explicitly. valgrind will only generate nice output on
2934 * exit(), not on exec(), hence let's do the former not the
2936 if (getpid_cached() == 1 && RUNNING_ON_VALGRIND
) {
2937 /* Cleanup watchdog_device strings for valgrind. We need them
2938 * in become_shutdown() so normally we cannot free them yet. */
2939 watchdog_free_device();
2940 arg_watchdog_device
= mfree(arg_watchdog_device
);
2946 #if HAS_FEATURE_ADDRESS_SANITIZER
2947 __lsan_do_leak_check();
2950 if (shutdown_verb
) {
2951 r
= become_shutdown(shutdown_verb
, retval
);
2952 log_error_errno(r
, "Failed to execute shutdown binary, %s: %m", getpid_cached() == 1 ? "freezing" : "quitting");
2953 error_message
= "Failed to execute shutdown binary";
2956 watchdog_free_device();
2957 arg_watchdog_device
= mfree(arg_watchdog_device
);
2959 if (getpid_cached() == 1) {
2961 manager_status_printf(NULL
, STATUS_TYPE_EMERGENCY
,
2962 ANSI_HIGHLIGHT_RED
"!!!!!!" ANSI_NORMAL
,
2963 "%s.", error_message
);
2964 freeze_or_exit_or_reboot();