]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
60918275 | 3 | #include <errno.h> |
3dfc9763 | 4 | #include <fcntl.h> |
f170852a | 5 | #include <getopt.h> |
d4a402e4 | 6 | #include <linux/oom.h> |
664f88a7 | 7 | #include <sys/mount.h> |
3dfc9763 | 8 | #include <sys/prctl.h> |
b9e74c39 | 9 | #include <sys/reboot.h> |
3dfc9763 | 10 | #include <unistd.h> |
349cc4a5 | 11 | #if HAVE_SECCOMP |
b64a3d86 LP |
12 | #include <seccomp.h> |
13 | #endif | |
349cc4a5 | 14 | #if HAVE_VALGRIND_VALGRIND_H |
3dfc9763 LP |
15 | #include <valgrind/valgrind.h> |
16 | #endif | |
54b434b1 | 17 | |
718db961 | 18 | #include "sd-bus.h" |
cf0fbc49 | 19 | #include "sd-daemon.h" |
b2e7486c | 20 | #include "sd-messages.h" |
3dfc9763 | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
2ffadd3c | 23 | #include "apparmor-setup.h" |
d9d93745 | 24 | #include "architecture.h" |
b1994387 ILG |
25 | #if HAVE_LIBBPF |
26 | #include "bpf-lsm.h" | |
27 | #endif | |
3dfc9763 LP |
28 | #include "build.h" |
29 | #include "bus-error.h" | |
30 | #include "bus-util.h" | |
430f0182 | 31 | #include "capability-util.h" |
a88c5b8a | 32 | #include "cgroup-util.h" |
24efb112 | 33 | #include "clock-util.h" |
3dfc9763 | 34 | #include "conf-parser.h" |
618234a5 | 35 | #include "cpu-set-util.h" |
3dfc9763 | 36 | #include "dbus-manager.h" |
c18ecf03 | 37 | #include "dbus.h" |
3dfc9763 | 38 | #include "def.h" |
32429805 | 39 | #include "dev-setup.h" |
c18ecf03 | 40 | #include "efi-random.h" |
209b2592 | 41 | #include "efivars.h" |
eee8b7ab | 42 | #include "emergency-action.h" |
3dfc9763 | 43 | #include "env-util.h" |
57b7a260 | 44 | #include "exit-status.h" |
3ffd4af2 | 45 | #include "fd-util.h" |
3dfc9763 | 46 | #include "fdset.h" |
718db961 | 47 | #include "fileio.h" |
f97b34a6 | 48 | #include "format-util.h" |
f4f15635 | 49 | #include "fs-util.h" |
d247f232 | 50 | #include "hexdecoct.h" |
3dfc9763 LP |
51 | #include "hostname-setup.h" |
52 | #include "ima-setup.h" | |
53 | #include "killall.h" | |
54 | #include "kmod-setup.h" | |
eefc66aa | 55 | #include "limits-util.h" |
d7b8eec7 | 56 | #include "load-fragment.h" |
3dfc9763 | 57 | #include "log.h" |
b6e66135 | 58 | #include "loopback-setup.h" |
b6e66135 | 59 | #include "machine-id-setup.h" |
3dfc9763 | 60 | #include "manager.h" |
2a341bb9 | 61 | #include "manager-dump.h" |
a01ba4b2 | 62 | #include "manager-serialize.h" |
32429805 | 63 | #include "mkdir.h" |
3dfc9763 | 64 | #include "mount-setup.h" |
d58ad743 | 65 | #include "os-util.h" |
3dfc9763 | 66 | #include "pager.h" |
614b022c | 67 | #include "parse-argument.h" |
6bedfcbb | 68 | #include "parse-util.h" |
7d5ceb64 | 69 | #include "path-util.h" |
294bf0c3 | 70 | #include "pretty-print.h" |
4e731273 | 71 | #include "proc-cmdline.h" |
3dfc9763 | 72 | #include "process-util.h" |
d247f232 | 73 | #include "random-util.h" |
8869a0b4 | 74 | #include "raw-clone.h" |
78f22b97 | 75 | #include "rlimit-util.h" |
349cc4a5 | 76 | #if HAVE_SECCOMP |
83f12b27 FS |
77 | #include "seccomp-util.h" |
78 | #endif | |
b6e66135 | 79 | #include "selinux-setup.h" |
3dfc9763 LP |
80 | #include "selinux-util.h" |
81 | #include "signal-util.h" | |
ffbd2c4d | 82 | #include "smack-setup.h" |
3dfc9763 | 83 | #include "special.h" |
8fcde012 | 84 | #include "stat-util.h" |
15a5e950 | 85 | #include "stdio-util.h" |
3dfc9763 LP |
86 | #include "strv.h" |
87 | #include "switch-root.h" | |
a8b627aa | 88 | #include "sysctl-util.h" |
3dfc9763 | 89 | #include "terminal-util.h" |
b10abe4b | 90 | #include "time-util.h" |
8612da97 | 91 | #include "umask-util.h" |
b1d4f8e1 | 92 | #include "user-util.h" |
9ce17593 | 93 | #include "util.h" |
3dfc9763 LP |
94 | #include "virt.h" |
95 | #include "watchdog.h" | |
b6e66135 | 96 | |
7e11a95e EV |
97 | #if HAS_FEATURE_ADDRESS_SANITIZER |
98 | #include <sanitizer/lsan_interface.h> | |
99 | #endif | |
100 | ||
3a0f06c4 ZJS |
101 | #define DEFAULT_TASKS_MAX ((TasksMax) { 15U, 100U }) /* 15% */ |
102 | ||
f170852a LP |
103 | static enum { |
104 | ACTION_RUN, | |
e965d56d | 105 | ACTION_HELP, |
9ba0bc4e | 106 | ACTION_VERSION, |
e537352b | 107 | ACTION_TEST, |
bbc1acab YW |
108 | ACTION_DUMP_CONFIGURATION_ITEMS, |
109 | ACTION_DUMP_BUS_PROPERTIES, | |
5c08257b | 110 | ACTION_BUS_INTROSPECT, |
fa0f4d8a | 111 | } arg_action = ACTION_RUN; |
fb39af4c | 112 | |
5c08257b ZJS |
113 | static const char *arg_bus_introspect = NULL; |
114 | ||
45250e66 LP |
115 | /* Those variables are initialized to 0 automatically, so we avoid uninitialized memory access. Real |
116 | * defaults are assigned in reset_arguments() below. */ | |
fb39af4c ZJS |
117 | static char *arg_default_unit; |
118 | static bool arg_system; | |
119 | static bool arg_dump_core; | |
120 | static int arg_crash_chvt; | |
121 | static bool arg_crash_shell; | |
122 | static bool arg_crash_reboot; | |
123 | static char *arg_confirm_spawn; | |
124 | static ShowStatus arg_show_status; | |
36cf4507 | 125 | static StatusUnitFormat arg_status_unit_format; |
fb39af4c ZJS |
126 | static bool arg_switched_root; |
127 | static PagerFlags arg_pager_flags; | |
128 | static bool arg_service_watchdogs; | |
129 | static ExecOutput arg_default_std_output; | |
130 | static ExecOutput arg_default_std_error; | |
131 | static usec_t arg_default_restart_usec; | |
132 | static usec_t arg_default_timeout_start_usec; | |
133 | static usec_t arg_default_timeout_stop_usec; | |
134 | static usec_t arg_default_timeout_abort_usec; | |
135 | static bool arg_default_timeout_abort_set; | |
136 | static usec_t arg_default_start_limit_interval; | |
137 | static unsigned arg_default_start_limit_burst; | |
138 | static usec_t arg_runtime_watchdog; | |
65224c1d | 139 | static usec_t arg_reboot_watchdog; |
acafd7d8 | 140 | static usec_t arg_kexec_watchdog; |
fb39af4c ZJS |
141 | static char *arg_early_core_pattern; |
142 | static char *arg_watchdog_device; | |
143 | static char **arg_default_environment; | |
d55ed7de | 144 | static char **arg_manager_environment; |
fb39af4c ZJS |
145 | static struct rlimit *arg_default_rlimit[_RLIMIT_MAX]; |
146 | static uint64_t arg_capability_bounding_set; | |
147 | static bool arg_no_new_privs; | |
148 | static nsec_t arg_timer_slack_nsec; | |
149 | static usec_t arg_default_timer_accuracy_usec; | |
150 | static Set* arg_syscall_archs; | |
151 | static FILE* arg_serialization; | |
152 | static int arg_default_cpu_accounting; | |
153 | static bool arg_default_io_accounting; | |
154 | static bool arg_default_ip_accounting; | |
155 | static bool arg_default_blockio_accounting; | |
156 | static bool arg_default_memory_accounting; | |
157 | static bool arg_default_tasks_accounting; | |
3a0f06c4 | 158 | static TasksMax arg_default_tasks_max; |
fb39af4c ZJS |
159 | static sd_id128_t arg_machine_id; |
160 | static EmergencyAction arg_cad_burst_action; | |
161 | static OOMPolicy arg_default_oom_policy; | |
162 | static CPUSet arg_cpu_affinity; | |
b070c7c0 | 163 | static NUMAPolicy arg_numa_policy; |
3753325b | 164 | static usec_t arg_clock_usec; |
d247f232 LP |
165 | static void *arg_random_seed; |
166 | static size_t arg_random_seed_size; | |
d4a402e4 LP |
167 | static int arg_default_oom_score_adjust; |
168 | static bool arg_default_oom_score_adjust_set; | |
61fbbac1 | 169 | |
0e06a031 LP |
170 | /* A copy of the original environment block */ |
171 | static char **saved_env = NULL; | |
172 | ||
a9fd4cd1 FB |
173 | static int parse_configuration(const struct rlimit *saved_rlimit_nofile, |
174 | const struct rlimit *saved_rlimit_memlock); | |
4fc935ca | 175 | |
f70e6fb4 ZJS |
176 | static int manager_find_user_config_paths(char ***ret_files, char ***ret_dirs) { |
177 | _cleanup_free_ char *base = NULL; | |
178 | _cleanup_strv_free_ char **files = NULL, **dirs = NULL; | |
179 | int r; | |
180 | ||
181 | r = xdg_user_config_dir(&base, "/systemd"); | |
182 | if (r < 0) | |
183 | return r; | |
184 | ||
185 | r = strv_extendf(&files, "%s/user.conf", base); | |
186 | if (r < 0) | |
187 | return r; | |
188 | ||
189 | r = strv_extend(&files, PKGSYSCONFDIR "/user.conf"); | |
190 | if (r < 0) | |
191 | return r; | |
192 | ||
193 | r = strv_consume(&dirs, TAKE_PTR(base)); | |
194 | if (r < 0) | |
195 | return r; | |
196 | ||
197 | r = strv_extend_strv(&dirs, CONF_PATHS_STRV("systemd"), false); | |
198 | if (r < 0) | |
199 | return r; | |
200 | ||
201 | *ret_files = TAKE_PTR(files); | |
202 | *ret_dirs = TAKE_PTR(dirs); | |
203 | return 0; | |
204 | } | |
205 | ||
bb259772 LP |
206 | _noreturn_ static void freeze_or_exit_or_reboot(void) { |
207 | ||
c3b6a348 LP |
208 | /* If we are running in a container, let's prefer exiting, after all we can propagate an exit code to |
209 | * the container manager, and thus inform it that something went wrong. */ | |
bb259772 LP |
210 | if (detect_container() > 0) { |
211 | log_emergency("Exiting PID 1..."); | |
c3b6a348 | 212 | _exit(EXIT_EXCEPTION); |
bb259772 | 213 | } |
b9e74c39 LP |
214 | |
215 | if (arg_crash_reboot) { | |
216 | log_notice("Rebooting in 10s..."); | |
217 | (void) sleep(10); | |
218 | ||
219 | log_notice("Rebooting now..."); | |
220 | (void) reboot(RB_AUTOBOOT); | |
221 | log_emergency_errno(errno, "Failed to reboot: %m"); | |
222 | } | |
223 | ||
224 | log_emergency("Freezing execution."); | |
43cac490 | 225 | sync(); |
b9e74c39 LP |
226 | freeze(); |
227 | } | |
228 | ||
848e863a | 229 | _noreturn_ static void crash(int sig) { |
7d06dad9 MS |
230 | struct sigaction sa; |
231 | pid_t pid; | |
97c4f35c | 232 | |
df0ff127 | 233 | if (getpid_cached() != 1) |
abb26902 | 234 | /* Pass this on immediately, if this is not PID 1 */ |
92ca4cac | 235 | (void) raise(sig); |
abb26902 | 236 | else if (!arg_dump_core) |
4104970e | 237 | log_emergency("Caught <%s>, not dumping core.", signal_to_string(sig)); |
97c4f35c | 238 | else { |
7d06dad9 | 239 | sa = (struct sigaction) { |
189d5bac | 240 | .sa_handler = nop_signal_handler, |
b92bea5d ZJS |
241 | .sa_flags = SA_NOCLDSTOP|SA_RESTART, |
242 | }; | |
97c4f35c | 243 | |
6f5e3f35 | 244 | /* We want to wait for the core process, hence let's enable SIGCHLD */ |
92ca4cac | 245 | (void) sigaction(SIGCHLD, &sa, NULL); |
6f5e3f35 | 246 | |
8869a0b4 | 247 | pid = raw_clone(SIGCHLD); |
e62d8c39 | 248 | if (pid < 0) |
56f64d95 | 249 | log_emergency_errno(errno, "Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig)); |
97c4f35c | 250 | else if (pid == 0) { |
97c4f35c | 251 | /* Enable default signal handler for core dump */ |
15a90032 | 252 | |
92ca4cac LP |
253 | sa = (struct sigaction) { |
254 | .sa_handler = SIG_DFL, | |
255 | }; | |
256 | (void) sigaction(sig, &sa, NULL); | |
97c4f35c | 257 | |
15a90032 LP |
258 | /* Don't limit the coredump size */ |
259 | (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)); | |
97c4f35c LP |
260 | |
261 | /* Just to be sure... */ | |
e62d9b81 | 262 | (void) chdir("/"); |
97c4f35c LP |
263 | |
264 | /* Raise the signal again */ | |
ee05e779 | 265 | pid = raw_getpid(); |
92ca4cac | 266 | (void) kill(pid, sig); /* raise() would kill the parent */ |
97c4f35c | 267 | |
04499a70 | 268 | assert_not_reached(); |
bb85a582 | 269 | _exit(EXIT_EXCEPTION); |
4fc935ca | 270 | } else { |
8e12a6ae LP |
271 | siginfo_t status; |
272 | int r; | |
4fc935ca LP |
273 | |
274 | /* Order things nicely. */ | |
e62d8c39 ZJS |
275 | r = wait_for_terminate(pid, &status); |
276 | if (r < 0) | |
da927ba9 | 277 | log_emergency_errno(r, "Caught <%s>, waitpid() failed: %m", signal_to_string(sig)); |
e1714f02 ZJS |
278 | else if (status.si_code != CLD_DUMPED) { |
279 | const char *s = status.si_code == CLD_EXITED | |
e04ed6db | 280 | ? exit_status_to_string(status.si_status, EXIT_STATUS_LIBC) |
e1714f02 ZJS |
281 | : signal_to_string(status.si_status); |
282 | ||
ee05e779 ZJS |
283 | log_emergency("Caught <%s>, core dump failed (child "PID_FMT", code=%s, status=%i/%s).", |
284 | signal_to_string(sig), | |
e1714f02 ZJS |
285 | pid, |
286 | sigchld_code_to_string(status.si_code), | |
287 | status.si_status, strna(s)); | |
288 | } else | |
289 | log_emergency("Caught <%s>, dumped core as pid "PID_FMT".", | |
290 | signal_to_string(sig), pid); | |
97c4f35c LP |
291 | } |
292 | } | |
293 | ||
b9e74c39 | 294 | if (arg_crash_chvt >= 0) |
92ca4cac | 295 | (void) chvt(arg_crash_chvt); |
601f6a1e | 296 | |
7d06dad9 MS |
297 | sa = (struct sigaction) { |
298 | .sa_handler = SIG_IGN, | |
299 | .sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART, | |
300 | }; | |
301 | ||
302 | /* Let the kernel reap children for us */ | |
303 | (void) sigaction(SIGCHLD, &sa, NULL); | |
8c43883a | 304 | |
7d06dad9 | 305 | if (arg_crash_shell) { |
b9e74c39 | 306 | log_notice("Executing crash shell in 10s..."); |
92ca4cac | 307 | (void) sleep(10); |
4fc935ca | 308 | |
8869a0b4 | 309 | pid = raw_clone(SIGCHLD); |
cd3bd60a | 310 | if (pid < 0) |
56f64d95 | 311 | log_emergency_errno(errno, "Failed to fork off crash shell: %m"); |
6f5e3f35 | 312 | else if (pid == 0) { |
b9e74c39 | 313 | (void) setsid(); |
92ca4cac | 314 | (void) make_console_stdio(); |
595225af | 315 | (void) rlimit_nofile_safe(); |
92ca4cac | 316 | (void) execle("/bin/sh", "/bin/sh", NULL, environ); |
6f5e3f35 | 317 | |
ee05e779 | 318 | log_emergency_errno(errno, "execle() failed: %m"); |
bb85a582 | 319 | _exit(EXIT_EXCEPTION); |
b9e74c39 LP |
320 | } else { |
321 | log_info("Spawned crash shell as PID "PID_FMT".", pid); | |
4cf0b03b | 322 | (void) wait_for_terminate(pid, NULL); |
b9e74c39 | 323 | } |
4fc935ca LP |
324 | } |
325 | ||
bb259772 | 326 | freeze_or_exit_or_reboot(); |
97c4f35c LP |
327 | } |
328 | ||
329 | static void install_crash_handler(void) { | |
297d563d | 330 | static const struct sigaction sa = { |
b92bea5d | 331 | .sa_handler = crash, |
297d563d | 332 | .sa_flags = SA_NODEFER, /* So that we can raise the signal again from the signal handler */ |
b92bea5d | 333 | }; |
297d563d | 334 | int r; |
97c4f35c | 335 | |
9c274488 LP |
336 | /* We ignore the return value here, since, we don't mind if we cannot set up a crash handler */ |
337 | r = sigaction_many(&sa, SIGNALS_CRASH_HANDLER); | |
297d563d LP |
338 | if (r < 0) |
339 | log_debug_errno(r, "I had trouble setting up the crash handler, ignoring: %m"); | |
97c4f35c | 340 | } |
f170852a | 341 | |
56d96fc0 LP |
342 | static int console_setup(void) { |
343 | _cleanup_close_ int tty_fd = -1; | |
344 | int r; | |
80876c20 | 345 | |
512947d4 | 346 | tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
23bbb0de MS |
347 | if (tty_fd < 0) |
348 | return log_error_errno(tty_fd, "Failed to open /dev/console: %m"); | |
80876c20 | 349 | |
56d96fc0 LP |
350 | /* We don't want to force text mode. plymouth may be showing |
351 | * pictures already from initrd. */ | |
512947d4 | 352 | r = reset_terminal_fd(tty_fd, false); |
23bbb0de MS |
353 | if (r < 0) |
354 | return log_error_errno(r, "Failed to reset /dev/console: %m"); | |
843d2643 | 355 | |
56d96fc0 | 356 | return 0; |
80876c20 LP |
357 | } |
358 | ||
ee48dbd5 | 359 | static int set_machine_id(const char *m) { |
e042eab7 | 360 | sd_id128_t t; |
8b26cdbd | 361 | assert(m); |
ee48dbd5 | 362 | |
e042eab7 | 363 | if (sd_id128_from_string(m, &t) < 0) |
ee48dbd5 NC |
364 | return -EINVAL; |
365 | ||
e042eab7 | 366 | if (sd_id128_is_null(t)) |
ee48dbd5 NC |
367 | return -EINVAL; |
368 | ||
e042eab7 | 369 | arg_machine_id = t; |
ee48dbd5 NC |
370 | return 0; |
371 | } | |
372 | ||
96287a49 | 373 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
059cb385 | 374 | int r; |
f170852a | 375 | |
059cb385 | 376 | assert(key); |
5192bd19 | 377 | |
1d84ad94 | 378 | if (STR_IN_SET(key, "systemd.unit", "rd.systemd.unit")) { |
bf4df7c3 | 379 | |
1d84ad94 LP |
380 | if (proc_cmdline_value_missing(key, value)) |
381 | return 0; | |
bf4df7c3 | 382 | |
1d84ad94 LP |
383 | if (!unit_name_is_valid(value, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
384 | log_warning("Unit name specified on %s= is not valid, ignoring: %s", key, value); | |
cd57038a ZJS |
385 | else if (in_initrd() == !!startswith(key, "rd.")) |
386 | return free_and_strdup_warn(&arg_default_unit, value); | |
f170852a | 387 | |
1d84ad94 | 388 | } else if (proc_cmdline_key_streq(key, "systemd.dump_core")) { |
4fc935ca | 389 | |
1d84ad94 | 390 | r = value ? parse_boolean(value) : true; |
fb472900 | 391 | if (r < 0) |
5e1ee764 | 392 | log_warning_errno(r, "Failed to parse dump core switch %s, ignoring: %m", value); |
4fc935ca | 393 | else |
fa0f4d8a | 394 | arg_dump_core = r; |
4fc935ca | 395 | |
c6885f5f FB |
396 | } else if (proc_cmdline_key_streq(key, "systemd.early_core_pattern")) { |
397 | ||
398 | if (proc_cmdline_value_missing(key, value)) | |
399 | return 0; | |
400 | ||
401 | if (path_is_absolute(value)) | |
614b022c | 402 | (void) parse_path_argument(value, false, &arg_early_core_pattern); |
c6885f5f FB |
403 | else |
404 | log_warning("Specified core pattern '%s' is not an absolute path, ignoring.", value); | |
405 | ||
1d84ad94 | 406 | } else if (proc_cmdline_key_streq(key, "systemd.crash_chvt")) { |
b9e74c39 | 407 | |
1d84ad94 LP |
408 | if (!value) |
409 | arg_crash_chvt = 0; /* turn on */ | |
5e1ee764 | 410 | else { |
a07a7324 | 411 | r = parse_crash_chvt(value, &arg_crash_chvt); |
5e1ee764 YW |
412 | if (r < 0) |
413 | log_warning_errno(r, "Failed to parse crash chvt switch %s, ignoring: %m", value); | |
414 | } | |
b9e74c39 | 415 | |
1d84ad94 | 416 | } else if (proc_cmdline_key_streq(key, "systemd.crash_shell")) { |
4fc935ca | 417 | |
1d84ad94 | 418 | r = value ? parse_boolean(value) : true; |
fb472900 | 419 | if (r < 0) |
5e1ee764 | 420 | log_warning_errno(r, "Failed to parse crash shell switch %s, ignoring: %m", value); |
4fc935ca | 421 | else |
fa0f4d8a | 422 | arg_crash_shell = r; |
5e7ee61c | 423 | |
1d84ad94 | 424 | } else if (proc_cmdline_key_streq(key, "systemd.crash_reboot")) { |
5e7ee61c | 425 | |
1d84ad94 | 426 | r = value ? parse_boolean(value) : true; |
b9e74c39 | 427 | if (r < 0) |
5e1ee764 | 428 | log_warning_errno(r, "Failed to parse crash reboot switch %s, ignoring: %m", value); |
5e7ee61c | 429 | else |
b9e74c39 | 430 | arg_crash_reboot = r; |
5e7ee61c | 431 | |
1d84ad94 LP |
432 | } else if (proc_cmdline_key_streq(key, "systemd.confirm_spawn")) { |
433 | char *s; | |
7d5ceb64 | 434 | |
1d84ad94 | 435 | r = parse_confirm_spawn(value, &s); |
059cb385 | 436 | if (r < 0) |
5e1ee764 YW |
437 | log_warning_errno(r, "Failed to parse confirm_spawn switch %s, ignoring: %m", value); |
438 | else | |
439 | free_and_replace(arg_confirm_spawn, s); | |
601f6a1e | 440 | |
2a12e32e JK |
441 | } else if (proc_cmdline_key_streq(key, "systemd.service_watchdogs")) { |
442 | ||
443 | r = value ? parse_boolean(value) : true; | |
444 | if (r < 0) | |
5e1ee764 | 445 | log_warning_errno(r, "Failed to parse service watchdog switch %s, ignoring: %m", value); |
2a12e32e JK |
446 | else |
447 | arg_service_watchdogs = r; | |
448 | ||
1d84ad94 | 449 | } else if (proc_cmdline_key_streq(key, "systemd.show_status")) { |
9e58ff9c | 450 | |
1d84ad94 LP |
451 | if (value) { |
452 | r = parse_show_status(value, &arg_show_status); | |
453 | if (r < 0) | |
5e1ee764 | 454 | log_warning_errno(r, "Failed to parse show status switch %s, ignoring: %m", value); |
1d84ad94 LP |
455 | } else |
456 | arg_show_status = SHOW_STATUS_YES; | |
059cb385 | 457 | |
36cf4507 ZJS |
458 | } else if (proc_cmdline_key_streq(key, "systemd.status_unit_format")) { |
459 | ||
460 | if (proc_cmdline_value_missing(key, value)) | |
461 | return 0; | |
462 | ||
463 | r = status_unit_format_from_string(value); | |
464 | if (r < 0) | |
465 | log_warning_errno(r, "Failed to parse %s=%s, ignoring: %m", key, value); | |
466 | else | |
467 | arg_status_unit_format = r; | |
468 | ||
1d84ad94 LP |
469 | } else if (proc_cmdline_key_streq(key, "systemd.default_standard_output")) { |
470 | ||
471 | if (proc_cmdline_value_missing(key, value)) | |
472 | return 0; | |
0a494f1f | 473 | |
059cb385 | 474 | r = exec_output_from_string(value); |
fb472900 | 475 | if (r < 0) |
5e1ee764 | 476 | log_warning_errno(r, "Failed to parse default standard output switch %s, ignoring: %m", value); |
0a494f1f LP |
477 | else |
478 | arg_default_std_output = r; | |
0a494f1f | 479 | |
1d84ad94 LP |
480 | } else if (proc_cmdline_key_streq(key, "systemd.default_standard_error")) { |
481 | ||
482 | if (proc_cmdline_value_missing(key, value)) | |
483 | return 0; | |
059cb385 LP |
484 | |
485 | r = exec_output_from_string(value); | |
fb472900 | 486 | if (r < 0) |
5e1ee764 | 487 | log_warning_errno(r, "Failed to parse default standard error switch %s, ignoring: %m", value); |
0a494f1f LP |
488 | else |
489 | arg_default_std_error = r; | |
9e7c5357 | 490 | |
1d84ad94 LP |
491 | } else if (streq(key, "systemd.setenv")) { |
492 | ||
493 | if (proc_cmdline_value_missing(key, value)) | |
494 | return 0; | |
059cb385 | 495 | |
b70935ac ZJS |
496 | if (!env_assignment_is_valid(value)) |
497 | log_warning("Environment variable assignment '%s' is not valid. Ignoring.", value); | |
498 | else { | |
499 | r = strv_env_replace_strdup(&arg_default_environment, value); | |
500 | if (r < 0) | |
1d84ad94 | 501 | return log_oom(); |
b70935ac | 502 | } |
9e58ff9c | 503 | |
1d84ad94 LP |
504 | } else if (proc_cmdline_key_streq(key, "systemd.machine_id")) { |
505 | ||
506 | if (proc_cmdline_value_missing(key, value)) | |
507 | return 0; | |
508 | ||
509 | r = set_machine_id(value); | |
510 | if (r < 0) | |
5e1ee764 | 511 | log_warning_errno(r, "MachineID '%s' is not valid, ignoring: %m", value); |
ee48dbd5 | 512 | |
1d84ad94 LP |
513 | } else if (proc_cmdline_key_streq(key, "systemd.default_timeout_start_sec")) { |
514 | ||
515 | if (proc_cmdline_value_missing(key, value)) | |
516 | return 0; | |
517 | ||
518 | r = parse_sec(value, &arg_default_timeout_start_usec); | |
519 | if (r < 0) | |
5e1ee764 | 520 | log_warning_errno(r, "Failed to parse default start timeout '%s', ignoring: %m", value); |
1d84ad94 LP |
521 | |
522 | if (arg_default_timeout_start_usec <= 0) | |
523 | arg_default_timeout_start_usec = USEC_INFINITY; | |
ee48dbd5 | 524 | |
68d58f38 LP |
525 | } else if (proc_cmdline_key_streq(key, "systemd.cpu_affinity")) { |
526 | ||
527 | if (proc_cmdline_value_missing(key, value)) | |
528 | return 0; | |
529 | ||
530 | r = parse_cpu_set(value, &arg_cpu_affinity); | |
531 | if (r < 0) | |
162392b7 | 532 | log_warning_errno(r, "Failed to parse CPU affinity mask '%s', ignoring: %m", value); |
68d58f38 | 533 | |
8a2c1fbf EJ |
534 | } else if (proc_cmdline_key_streq(key, "systemd.watchdog_device")) { |
535 | ||
536 | if (proc_cmdline_value_missing(key, value)) | |
537 | return 0; | |
538 | ||
614b022c | 539 | (void) parse_path_argument(value, false, &arg_watchdog_device); |
8a2c1fbf | 540 | |
b3aa73e4 FB |
541 | } else if (proc_cmdline_key_streq(key, "systemd.watchdog_sec")) { |
542 | ||
543 | if (proc_cmdline_value_missing(key, value)) | |
544 | return 0; | |
545 | ||
8a85c5b6 FB |
546 | if (streq(value, "default")) |
547 | arg_runtime_watchdog = USEC_INFINITY; | |
c91c95e6 LP |
548 | else if (streq(value, "off")) |
549 | arg_runtime_watchdog = 0; | |
8a85c5b6 FB |
550 | else { |
551 | r = parse_sec(value, &arg_runtime_watchdog); | |
552 | if (r < 0) { | |
553 | log_warning_errno(r, "Failed to parse systemd.watchdog_sec= argument '%s', ignoring: %m", value); | |
554 | return 0; | |
555 | } | |
556 | } | |
557 | ||
558 | arg_kexec_watchdog = arg_reboot_watchdog = arg_runtime_watchdog; | |
b3aa73e4 | 559 | |
3753325b LP |
560 | } else if (proc_cmdline_key_streq(key, "systemd.clock_usec")) { |
561 | ||
562 | if (proc_cmdline_value_missing(key, value)) | |
563 | return 0; | |
564 | ||
565 | r = safe_atou64(value, &arg_clock_usec); | |
566 | if (r < 0) | |
567 | log_warning_errno(r, "Failed to parse systemd.clock_usec= argument, ignoring: %s", value); | |
568 | ||
d247f232 LP |
569 | } else if (proc_cmdline_key_streq(key, "systemd.random_seed")) { |
570 | void *p; | |
571 | size_t sz; | |
572 | ||
573 | if (proc_cmdline_value_missing(key, value)) | |
574 | return 0; | |
575 | ||
f5fbe71d | 576 | r = unbase64mem(value, SIZE_MAX, &p, &sz); |
d247f232 LP |
577 | if (r < 0) |
578 | log_warning_errno(r, "Failed to parse systemd.random_seed= argument, ignoring: %s", value); | |
579 | ||
580 | free(arg_random_seed); | |
581 | arg_random_seed = sz > 0 ? p : mfree(p); | |
582 | arg_random_seed_size = sz; | |
583 | ||
059cb385 | 584 | } else if (streq(key, "quiet") && !value) { |
d7b15e0a | 585 | |
7a293242 | 586 | if (arg_show_status == _SHOW_STATUS_INVALID) |
0d066dd1 | 587 | arg_show_status = SHOW_STATUS_ERROR; |
059cb385 LP |
588 | |
589 | } else if (streq(key, "debug") && !value) { | |
d7b15e0a | 590 | |
1de1c9c3 LP |
591 | /* Note that log_parse_environment() handles 'debug' |
592 | * too, and sets the log level to LOG_DEBUG. */ | |
d7b15e0a | 593 | |
75f86906 | 594 | if (detect_container() > 0) |
b2103dcc | 595 | log_set_target(LOG_TARGET_CONSOLE); |
059cb385 | 596 | |
dcd61450 | 597 | } else if (!value) { |
e2c9a131 | 598 | const char *target; |
f170852a | 599 | |
ceae6295 | 600 | /* Compatible with SysV, but supported independently even if SysV compatibility is disabled. */ |
e2c9a131 EV |
601 | target = runlevel_to_target(key); |
602 | if (target) | |
cd57038a | 603 | return free_and_strdup_warn(&arg_default_unit, target); |
f170852a LP |
604 | } |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
e8e581bf ZJS |
609 | #define DEFINE_SETTER(name, func, descr) \ |
610 | static int name(const char *unit, \ | |
611 | const char *filename, \ | |
612 | unsigned line, \ | |
613 | const char *section, \ | |
71a61510 | 614 | unsigned section_line, \ |
e8e581bf ZJS |
615 | const char *lvalue, \ |
616 | int ltype, \ | |
617 | const char *rvalue, \ | |
618 | void *data, \ | |
619 | void *userdata) { \ | |
620 | \ | |
621 | int r; \ | |
622 | \ | |
623 | assert(filename); \ | |
624 | assert(lvalue); \ | |
625 | assert(rvalue); \ | |
626 | \ | |
627 | r = func(rvalue); \ | |
628 | if (r < 0) \ | |
d1cefe0a LP |
629 | log_syntax(unit, LOG_ERR, filename, line, r, \ |
630 | "Invalid " descr "'%s': %m", \ | |
631 | rvalue); \ | |
e8e581bf ZJS |
632 | \ |
633 | return 0; \ | |
634 | } | |
487393e9 | 635 | |
a6ecbf83 FB |
636 | DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level"); |
637 | DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target"); | |
c5673ed0 | 638 | DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color"); |
a6ecbf83 | 639 | DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location"); |
c5673ed0 | 640 | DEFINE_SETTER(config_parse_time, log_show_time_from_string, "time"); |
487393e9 | 641 | |
a61d6874 ZJS |
642 | static int config_parse_default_timeout_abort( |
643 | const char *unit, | |
644 | const char *filename, | |
645 | unsigned line, | |
646 | const char *section, | |
647 | unsigned section_line, | |
648 | const char *lvalue, | |
649 | int ltype, | |
650 | const char *rvalue, | |
651 | void *data, | |
652 | void *userdata) { | |
653 | int r; | |
654 | ||
655 | r = config_parse_timeout_abort(unit, filename, line, section, section_line, lvalue, ltype, rvalue, | |
656 | &arg_default_timeout_abort_usec, userdata); | |
657 | if (r >= 0) | |
658 | arg_default_timeout_abort_set = r; | |
659 | return 0; | |
660 | } | |
487393e9 | 661 | |
d4a402e4 LP |
662 | static int config_parse_oom_score_adjust( |
663 | const char *unit, | |
664 | const char *filename, | |
665 | unsigned line, | |
666 | const char *section, | |
667 | unsigned section_line, | |
668 | const char *lvalue, | |
669 | int ltype, | |
670 | const char *rvalue, | |
671 | void *data, | |
672 | void *userdata) { | |
673 | ||
674 | int oa, r; | |
675 | ||
676 | if (isempty(rvalue)) { | |
677 | arg_default_oom_score_adjust_set = false; | |
678 | return 0; | |
679 | } | |
680 | ||
681 | r = parse_oom_score_adjust(rvalue, &oa); | |
682 | if (r < 0) { | |
683 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue); | |
684 | return 0; | |
685 | } | |
686 | ||
687 | arg_default_oom_score_adjust = oa; | |
688 | arg_default_oom_score_adjust_set = true; | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
a61d6874 | 693 | static int parse_config_file(void) { |
f975e971 | 694 | const ConfigTableItem items[] = { |
a61d6874 ZJS |
695 | { "Manager", "LogLevel", config_parse_level2, 0, NULL }, |
696 | { "Manager", "LogTarget", config_parse_target, 0, NULL }, | |
697 | { "Manager", "LogColor", config_parse_color, 0, NULL }, | |
698 | { "Manager", "LogLocation", config_parse_location, 0, NULL }, | |
c5673ed0 | 699 | { "Manager", "LogTime", config_parse_time, 0, NULL }, |
a61d6874 ZJS |
700 | { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core }, |
701 | { "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt, 0, &arg_crash_chvt }, | |
702 | { "Manager", "CrashChangeVT", config_parse_crash_chvt, 0, &arg_crash_chvt }, | |
703 | { "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell }, | |
704 | { "Manager", "CrashReboot", config_parse_bool, 0, &arg_crash_reboot }, | |
705 | { "Manager", "ShowStatus", config_parse_show_status, 0, &arg_show_status }, | |
706 | { "Manager", "StatusUnitFormat", config_parse_status_unit_format, 0, &arg_status_unit_format }, | |
707 | { "Manager", "CPUAffinity", config_parse_cpu_affinity2, 0, &arg_cpu_affinity }, | |
708 | { "Manager", "NUMAPolicy", config_parse_numa_policy, 0, &arg_numa_policy.type }, | |
709 | { "Manager", "NUMAMask", config_parse_numa_mask, 0, &arg_numa_policy }, | |
710 | { "Manager", "JoinControllers", config_parse_warn_compat, DISABLED_CONFIGURATION, NULL }, | |
8a85c5b6 FB |
711 | { "Manager", "RuntimeWatchdogSec", config_parse_watchdog_sec, 0, &arg_runtime_watchdog }, |
712 | { "Manager", "RebootWatchdogSec", config_parse_watchdog_sec, 0, &arg_reboot_watchdog }, | |
713 | { "Manager", "ShutdownWatchdogSec", config_parse_watchdog_sec, 0, &arg_reboot_watchdog }, /* obsolete alias */ | |
714 | { "Manager", "KExecWatchdogSec", config_parse_watchdog_sec, 0, &arg_kexec_watchdog }, | |
a61d6874 ZJS |
715 | { "Manager", "WatchdogDevice", config_parse_path, 0, &arg_watchdog_device }, |
716 | { "Manager", "CapabilityBoundingSet", config_parse_capability_set, 0, &arg_capability_bounding_set }, | |
717 | { "Manager", "NoNewPrivileges", config_parse_bool, 0, &arg_no_new_privs }, | |
349cc4a5 | 718 | #if HAVE_SECCOMP |
a61d6874 | 719 | { "Manager", "SystemCallArchitectures", config_parse_syscall_archs, 0, &arg_syscall_archs }, |
89fffa27 | 720 | #endif |
a61d6874 ZJS |
721 | { "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec }, |
722 | { "Manager", "DefaultTimerAccuracySec", config_parse_sec, 0, &arg_default_timer_accuracy_usec }, | |
723 | { "Manager", "DefaultStandardOutput", config_parse_output_restricted, 0, &arg_default_std_output }, | |
724 | { "Manager", "DefaultStandardError", config_parse_output_restricted, 0, &arg_default_std_error }, | |
725 | { "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_default_timeout_start_usec }, | |
726 | { "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_default_timeout_stop_usec }, | |
d4a402e4 | 727 | { "Manager", "DefaultTimeoutAbortSec", config_parse_default_timeout_abort, 0, NULL }, |
a61d6874 ZJS |
728 | { "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_default_restart_usec }, |
729 | { "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_default_start_limit_interval }, /* obsolete alias */ | |
730 | { "Manager", "DefaultStartLimitIntervalSec", config_parse_sec, 0, &arg_default_start_limit_interval }, | |
731 | { "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_default_start_limit_burst }, | |
732 | { "Manager", "DefaultEnvironment", config_parse_environ, 0, &arg_default_environment }, | |
d55ed7de | 733 | { "Manager", "ManagerEnvironment", config_parse_environ, 0, &arg_manager_environment }, |
a61d6874 ZJS |
734 | { "Manager", "DefaultLimitCPU", config_parse_rlimit, RLIMIT_CPU, arg_default_rlimit }, |
735 | { "Manager", "DefaultLimitFSIZE", config_parse_rlimit, RLIMIT_FSIZE, arg_default_rlimit }, | |
736 | { "Manager", "DefaultLimitDATA", config_parse_rlimit, RLIMIT_DATA, arg_default_rlimit }, | |
737 | { "Manager", "DefaultLimitSTACK", config_parse_rlimit, RLIMIT_STACK, arg_default_rlimit }, | |
738 | { "Manager", "DefaultLimitCORE", config_parse_rlimit, RLIMIT_CORE, arg_default_rlimit }, | |
739 | { "Manager", "DefaultLimitRSS", config_parse_rlimit, RLIMIT_RSS, arg_default_rlimit }, | |
740 | { "Manager", "DefaultLimitNOFILE", config_parse_rlimit, RLIMIT_NOFILE, arg_default_rlimit }, | |
741 | { "Manager", "DefaultLimitAS", config_parse_rlimit, RLIMIT_AS, arg_default_rlimit }, | |
742 | { "Manager", "DefaultLimitNPROC", config_parse_rlimit, RLIMIT_NPROC, arg_default_rlimit }, | |
743 | { "Manager", "DefaultLimitMEMLOCK", config_parse_rlimit, RLIMIT_MEMLOCK, arg_default_rlimit }, | |
744 | { "Manager", "DefaultLimitLOCKS", config_parse_rlimit, RLIMIT_LOCKS, arg_default_rlimit }, | |
745 | { "Manager", "DefaultLimitSIGPENDING", config_parse_rlimit, RLIMIT_SIGPENDING, arg_default_rlimit }, | |
746 | { "Manager", "DefaultLimitMSGQUEUE", config_parse_rlimit, RLIMIT_MSGQUEUE, arg_default_rlimit }, | |
747 | { "Manager", "DefaultLimitNICE", config_parse_rlimit, RLIMIT_NICE, arg_default_rlimit }, | |
748 | { "Manager", "DefaultLimitRTPRIO", config_parse_rlimit, RLIMIT_RTPRIO, arg_default_rlimit }, | |
749 | { "Manager", "DefaultLimitRTTIME", config_parse_rlimit, RLIMIT_RTTIME, arg_default_rlimit }, | |
750 | { "Manager", "DefaultCPUAccounting", config_parse_tristate, 0, &arg_default_cpu_accounting }, | |
751 | { "Manager", "DefaultIOAccounting", config_parse_bool, 0, &arg_default_io_accounting }, | |
752 | { "Manager", "DefaultIPAccounting", config_parse_bool, 0, &arg_default_ip_accounting }, | |
753 | { "Manager", "DefaultBlockIOAccounting", config_parse_bool, 0, &arg_default_blockio_accounting }, | |
754 | { "Manager", "DefaultMemoryAccounting", config_parse_bool, 0, &arg_default_memory_accounting }, | |
755 | { "Manager", "DefaultTasksAccounting", config_parse_bool, 0, &arg_default_tasks_accounting }, | |
756 | { "Manager", "DefaultTasksMax", config_parse_tasks_max, 0, &arg_default_tasks_max }, | |
757 | { "Manager", "CtrlAltDelBurstAction", config_parse_emergency_action, 0, &arg_cad_burst_action }, | |
758 | { "Manager", "DefaultOOMPolicy", config_parse_oom_policy, 0, &arg_default_oom_policy }, | |
d4a402e4 | 759 | { "Manager", "DefaultOOMScoreAdjust", config_parse_oom_score_adjust, 0, NULL }, |
d3b1c508 | 760 | {} |
487393e9 LP |
761 | }; |
762 | ||
e94a009c YW |
763 | _cleanup_strv_free_ char **files = NULL, **dirs = NULL; |
764 | const char *suffix; | |
f70e6fb4 | 765 | int r; |
75eb6154 | 766 | |
e94a009c | 767 | if (arg_system) |
f70e6fb4 | 768 | suffix = "system.conf.d"; |
e94a009c YW |
769 | else { |
770 | r = manager_find_user_config_paths(&files, &dirs); | |
f70e6fb4 ZJS |
771 | if (r < 0) |
772 | return log_error_errno(r, "Failed to determine config file paths: %m"); | |
e94a009c | 773 | |
f70e6fb4 ZJS |
774 | suffix = "user.conf.d"; |
775 | } | |
75eb6154 | 776 | |
f70e6fb4 | 777 | (void) config_parse_many( |
e94a009c YW |
778 | (const char* const*) (files ?: STRV_MAKE(PKGSYSCONFDIR "/system.conf")), |
779 | (const char* const*) (dirs ?: CONF_PATHS_STRV("systemd")), | |
780 | suffix, | |
4f9ff96a LP |
781 | "Manager\0", |
782 | config_item_table_lookup, items, | |
783 | CONFIG_PARSE_WARN, | |
784 | NULL, | |
785 | NULL); | |
36c16a7c | 786 | |
f70e6fb4 ZJS |
787 | /* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we use |
788 | * USEC_INFINITY like everywhere else. */ | |
36c16a7c LP |
789 | if (arg_default_timeout_start_usec <= 0) |
790 | arg_default_timeout_start_usec = USEC_INFINITY; | |
791 | if (arg_default_timeout_stop_usec <= 0) | |
792 | arg_default_timeout_stop_usec = USEC_INFINITY; | |
487393e9 | 793 | |
487393e9 LP |
794 | return 0; |
795 | } | |
796 | ||
85cb4151 | 797 | static void set_manager_defaults(Manager *m) { |
06af2a04 TB |
798 | |
799 | assert(m); | |
800 | ||
5b65ae15 LP |
801 | /* Propagates the various default unit property settings into the manager object, i.e. properties that do not |
802 | * affect the manager itself, but are just what newly allocated units will have set if they haven't set | |
803 | * anything else. (Also see set_manager_settings() for the settings that affect the manager's own behaviour) */ | |
804 | ||
06af2a04 TB |
805 | m->default_timer_accuracy_usec = arg_default_timer_accuracy_usec; |
806 | m->default_std_output = arg_default_std_output; | |
807 | m->default_std_error = arg_default_std_error; | |
808 | m->default_timeout_start_usec = arg_default_timeout_start_usec; | |
809 | m->default_timeout_stop_usec = arg_default_timeout_stop_usec; | |
dc653bf4 JK |
810 | m->default_timeout_abort_usec = arg_default_timeout_abort_usec; |
811 | m->default_timeout_abort_set = arg_default_timeout_abort_set; | |
06af2a04 TB |
812 | m->default_restart_usec = arg_default_restart_usec; |
813 | m->default_start_limit_interval = arg_default_start_limit_interval; | |
814 | m->default_start_limit_burst = arg_default_start_limit_burst; | |
a88c5b8a CD |
815 | |
816 | /* On 4.15+ with unified hierarchy, CPU accounting is essentially free as it doesn't require the CPU | |
817 | * controller to be enabled, so the default is to enable it unless we got told otherwise. */ | |
818 | if (arg_default_cpu_accounting >= 0) | |
819 | m->default_cpu_accounting = arg_default_cpu_accounting; | |
820 | else | |
821 | m->default_cpu_accounting = cpu_accounting_is_cheap(); | |
822 | ||
13c31542 | 823 | m->default_io_accounting = arg_default_io_accounting; |
377bfd2d | 824 | m->default_ip_accounting = arg_default_ip_accounting; |
06af2a04 TB |
825 | m->default_blockio_accounting = arg_default_blockio_accounting; |
826 | m->default_memory_accounting = arg_default_memory_accounting; | |
03a7b521 | 827 | m->default_tasks_accounting = arg_default_tasks_accounting; |
0af20ea2 | 828 | m->default_tasks_max = arg_default_tasks_max; |
afcfaa69 | 829 | m->default_oom_policy = arg_default_oom_policy; |
d4a402e4 LP |
830 | m->default_oom_score_adjust_set = arg_default_oom_score_adjust_set; |
831 | m->default_oom_score_adjust = arg_default_oom_score_adjust; | |
06af2a04 | 832 | |
79a224c4 LP |
833 | (void) manager_set_default_rlimits(m, arg_default_rlimit); |
834 | ||
835 | (void) manager_default_environment(m); | |
836 | (void) manager_transient_environment_add(m, arg_default_environment); | |
06af2a04 TB |
837 | } |
838 | ||
7b46fc6a LP |
839 | static void set_manager_settings(Manager *m) { |
840 | ||
841 | assert(m); | |
842 | ||
986935cf FB |
843 | /* Propagates the various manager settings into the manager object, i.e. properties that |
844 | * effect the manager itself (as opposed to just being inherited into newly allocated | |
845 | * units, see set_manager_defaults() above). */ | |
5b65ae15 | 846 | |
7b46fc6a | 847 | m->confirm_spawn = arg_confirm_spawn; |
2a12e32e | 848 | m->service_watchdogs = arg_service_watchdogs; |
7b46fc6a LP |
849 | m->cad_burst_action = arg_cad_burst_action; |
850 | ||
986935cf FB |
851 | manager_set_watchdog(m, WATCHDOG_RUNTIME, arg_runtime_watchdog); |
852 | manager_set_watchdog(m, WATCHDOG_REBOOT, arg_reboot_watchdog); | |
853 | manager_set_watchdog(m, WATCHDOG_KEXEC, arg_kexec_watchdog); | |
854 | ||
7365a296 | 855 | manager_set_show_status(m, arg_show_status, "commandline"); |
36cf4507 | 856 | m->status_unit_format = arg_status_unit_format; |
7b46fc6a LP |
857 | } |
858 | ||
f170852a | 859 | static int parse_argv(int argc, char *argv[]) { |
f170852a LP |
860 | enum { |
861 | ARG_LOG_LEVEL = 0x100, | |
862 | ARG_LOG_TARGET, | |
bbe63281 LP |
863 | ARG_LOG_COLOR, |
864 | ARG_LOG_LOCATION, | |
c5673ed0 | 865 | ARG_LOG_TIME, |
2f198e2f | 866 | ARG_UNIT, |
edb9aaa8 | 867 | ARG_SYSTEM, |
af2d49f7 | 868 | ARG_USER, |
e537352b | 869 | ARG_TEST, |
b87c2aa6 | 870 | ARG_NO_PAGER, |
9ba0bc4e | 871 | ARG_VERSION, |
80876c20 | 872 | ARG_DUMP_CONFIGURATION_ITEMS, |
bbc1acab | 873 | ARG_DUMP_BUS_PROPERTIES, |
5c08257b | 874 | ARG_BUS_INTROSPECT, |
9e58ff9c | 875 | ARG_DUMP_CORE, |
b9e74c39 | 876 | ARG_CRASH_CHVT, |
9e58ff9c | 877 | ARG_CRASH_SHELL, |
b9e74c39 | 878 | ARG_CRASH_REBOOT, |
a16e1123 | 879 | ARG_CONFIRM_SPAWN, |
9e58ff9c | 880 | ARG_SHOW_STATUS, |
4288f619 | 881 | ARG_DESERIALIZE, |
2660882b | 882 | ARG_SWITCHED_ROOT, |
0a494f1f | 883 | ARG_DEFAULT_STD_OUTPUT, |
ee48dbd5 | 884 | ARG_DEFAULT_STD_ERROR, |
2a12e32e JK |
885 | ARG_MACHINE_ID, |
886 | ARG_SERVICE_WATCHDOGS, | |
f170852a LP |
887 | }; |
888 | ||
889 | static const struct option options[] = { | |
a16e1123 LP |
890 | { "log-level", required_argument, NULL, ARG_LOG_LEVEL }, |
891 | { "log-target", required_argument, NULL, ARG_LOG_TARGET }, | |
bbe63281 LP |
892 | { "log-color", optional_argument, NULL, ARG_LOG_COLOR }, |
893 | { "log-location", optional_argument, NULL, ARG_LOG_LOCATION }, | |
c5673ed0 | 894 | { "log-time", optional_argument, NULL, ARG_LOG_TIME }, |
2f198e2f | 895 | { "unit", required_argument, NULL, ARG_UNIT }, |
edb9aaa8 | 896 | { "system", no_argument, NULL, ARG_SYSTEM }, |
af2d49f7 | 897 | { "user", no_argument, NULL, ARG_USER }, |
a16e1123 | 898 | { "test", no_argument, NULL, ARG_TEST }, |
b87c2aa6 | 899 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
a16e1123 | 900 | { "help", no_argument, NULL, 'h' }, |
9ba0bc4e | 901 | { "version", no_argument, NULL, ARG_VERSION }, |
a16e1123 | 902 | { "dump-configuration-items", no_argument, NULL, ARG_DUMP_CONFIGURATION_ITEMS }, |
bbc1acab | 903 | { "dump-bus-properties", no_argument, NULL, ARG_DUMP_BUS_PROPERTIES }, |
5c08257b | 904 | { "bus-introspect", required_argument, NULL, ARG_BUS_INTROSPECT }, |
a5d87bf0 | 905 | { "dump-core", optional_argument, NULL, ARG_DUMP_CORE }, |
b9e74c39 | 906 | { "crash-chvt", required_argument, NULL, ARG_CRASH_CHVT }, |
a5d87bf0 | 907 | { "crash-shell", optional_argument, NULL, ARG_CRASH_SHELL }, |
b9e74c39 | 908 | { "crash-reboot", optional_argument, NULL, ARG_CRASH_REBOOT }, |
a5d87bf0 | 909 | { "confirm-spawn", optional_argument, NULL, ARG_CONFIRM_SPAWN }, |
6e98720f | 910 | { "show-status", optional_argument, NULL, ARG_SHOW_STATUS }, |
a16e1123 | 911 | { "deserialize", required_argument, NULL, ARG_DESERIALIZE }, |
2660882b | 912 | { "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT }, |
0a494f1f LP |
913 | { "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, }, |
914 | { "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, }, | |
ee48dbd5 | 915 | { "machine-id", required_argument, NULL, ARG_MACHINE_ID }, |
2a12e32e | 916 | { "service-watchdogs", required_argument, NULL, ARG_SERVICE_WATCHDOGS }, |
fb472900 | 917 | {} |
f170852a LP |
918 | }; |
919 | ||
920 | int c, r; | |
9a9ca408 | 921 | bool user_arg_seen = false; |
f170852a LP |
922 | |
923 | assert(argc >= 1); | |
924 | assert(argv); | |
925 | ||
df0ff127 | 926 | if (getpid_cached() == 1) |
b770165a LP |
927 | opterr = 0; |
928 | ||
099663ff | 929 | while ((c = getopt_long(argc, argv, "hDbsz:", options, NULL)) >= 0) |
f170852a LP |
930 | |
931 | switch (c) { | |
932 | ||
933 | case ARG_LOG_LEVEL: | |
fb472900 | 934 | r = log_set_max_level_from_string(optarg); |
2b5107e1 ZJS |
935 | if (r < 0) |
936 | return log_error_errno(r, "Failed to parse log level \"%s\": %m", optarg); | |
f170852a LP |
937 | |
938 | break; | |
939 | ||
940 | case ARG_LOG_TARGET: | |
fb472900 | 941 | r = log_set_target_from_string(optarg); |
2b5107e1 ZJS |
942 | if (r < 0) |
943 | return log_error_errno(r, "Failed to parse log target \"%s\": %m", optarg); | |
f170852a LP |
944 | |
945 | break; | |
946 | ||
bbe63281 LP |
947 | case ARG_LOG_COLOR: |
948 | ||
d0b170c8 | 949 | if (optarg) { |
fb472900 | 950 | r = log_show_color_from_string(optarg); |
2b5107e1 ZJS |
951 | if (r < 0) |
952 | return log_error_errno(r, "Failed to parse log color setting \"%s\": %m", | |
953 | optarg); | |
d0b170c8 LP |
954 | } else |
955 | log_show_color(true); | |
bbe63281 LP |
956 | |
957 | break; | |
958 | ||
959 | case ARG_LOG_LOCATION: | |
d0b170c8 | 960 | if (optarg) { |
fb472900 | 961 | r = log_show_location_from_string(optarg); |
2b5107e1 ZJS |
962 | if (r < 0) |
963 | return log_error_errno(r, "Failed to parse log location setting \"%s\": %m", | |
964 | optarg); | |
d0b170c8 LP |
965 | } else |
966 | log_show_location(true); | |
bbe63281 LP |
967 | |
968 | break; | |
969 | ||
c5673ed0 DS |
970 | case ARG_LOG_TIME: |
971 | ||
972 | if (optarg) { | |
973 | r = log_show_time_from_string(optarg); | |
974 | if (r < 0) | |
975 | return log_error_errno(r, "Failed to parse log time setting \"%s\": %m", | |
976 | optarg); | |
977 | } else | |
978 | log_show_time(true); | |
979 | ||
980 | break; | |
981 | ||
0a494f1f | 982 | case ARG_DEFAULT_STD_OUTPUT: |
fb472900 | 983 | r = exec_output_from_string(optarg); |
2b5107e1 ZJS |
984 | if (r < 0) |
985 | return log_error_errno(r, "Failed to parse default standard output setting \"%s\": %m", | |
986 | optarg); | |
987 | arg_default_std_output = r; | |
0a494f1f LP |
988 | break; |
989 | ||
990 | case ARG_DEFAULT_STD_ERROR: | |
fb472900 | 991 | r = exec_output_from_string(optarg); |
2b5107e1 ZJS |
992 | if (r < 0) |
993 | return log_error_errno(r, "Failed to parse default standard error output setting \"%s\": %m", | |
994 | optarg); | |
995 | arg_default_std_error = r; | |
0a494f1f LP |
996 | break; |
997 | ||
2f198e2f | 998 | case ARG_UNIT: |
e6e242ad | 999 | r = free_and_strdup(&arg_default_unit, optarg); |
23bbb0de | 1000 | if (r < 0) |
2b5107e1 | 1001 | return log_error_errno(r, "Failed to set default unit \"%s\": %m", optarg); |
f170852a LP |
1002 | |
1003 | break; | |
1004 | ||
edb9aaa8 | 1005 | case ARG_SYSTEM: |
463d0d15 | 1006 | arg_system = true; |
edb9aaa8 | 1007 | break; |
a5dab5ce | 1008 | |
af2d49f7 | 1009 | case ARG_USER: |
463d0d15 | 1010 | arg_system = false; |
9a9ca408 | 1011 | user_arg_seen = true; |
a5dab5ce | 1012 | break; |
a5dab5ce | 1013 | |
e965d56d | 1014 | case ARG_TEST: |
fa0f4d8a | 1015 | arg_action = ACTION_TEST; |
b87c2aa6 ZJS |
1016 | break; |
1017 | ||
1018 | case ARG_NO_PAGER: | |
0221d68a | 1019 | arg_pager_flags |= PAGER_DISABLE; |
e965d56d LP |
1020 | break; |
1021 | ||
9ba0bc4e ZJS |
1022 | case ARG_VERSION: |
1023 | arg_action = ACTION_VERSION; | |
1024 | break; | |
1025 | ||
e537352b | 1026 | case ARG_DUMP_CONFIGURATION_ITEMS: |
fa0f4d8a | 1027 | arg_action = ACTION_DUMP_CONFIGURATION_ITEMS; |
e537352b LP |
1028 | break; |
1029 | ||
bbc1acab YW |
1030 | case ARG_DUMP_BUS_PROPERTIES: |
1031 | arg_action = ACTION_DUMP_BUS_PROPERTIES; | |
1032 | break; | |
1033 | ||
5c08257b ZJS |
1034 | case ARG_BUS_INTROSPECT: |
1035 | arg_bus_introspect = optarg; | |
1036 | arg_action = ACTION_BUS_INTROSPECT; | |
1037 | break; | |
1038 | ||
9e58ff9c | 1039 | case ARG_DUMP_CORE: |
599c7c54 ZJS |
1040 | r = parse_boolean_argument("--dump-core", optarg, &arg_dump_core); |
1041 | if (r < 0) | |
1042 | return r; | |
b9e74c39 LP |
1043 | break; |
1044 | ||
1045 | case ARG_CRASH_CHVT: | |
a07a7324 | 1046 | r = parse_crash_chvt(optarg, &arg_crash_chvt); |
b9e74c39 | 1047 | if (r < 0) |
2b5107e1 ZJS |
1048 | return log_error_errno(r, "Failed to parse crash virtual terminal index: \"%s\": %m", |
1049 | optarg); | |
9e58ff9c LP |
1050 | break; |
1051 | ||
1052 | case ARG_CRASH_SHELL: | |
599c7c54 ZJS |
1053 | r = parse_boolean_argument("--crash-shell", optarg, &arg_crash_shell); |
1054 | if (r < 0) | |
1055 | return r; | |
b9e74c39 LP |
1056 | break; |
1057 | ||
1058 | case ARG_CRASH_REBOOT: | |
599c7c54 ZJS |
1059 | r = parse_boolean_argument("--crash-reboot", optarg, &arg_crash_reboot); |
1060 | if (r < 0) | |
1061 | return r; | |
9e58ff9c LP |
1062 | break; |
1063 | ||
80876c20 | 1064 | case ARG_CONFIRM_SPAWN: |
7d5ceb64 FB |
1065 | arg_confirm_spawn = mfree(arg_confirm_spawn); |
1066 | ||
1067 | r = parse_confirm_spawn(optarg, &arg_confirm_spawn); | |
1068 | if (r < 0) | |
2b5107e1 ZJS |
1069 | return log_error_errno(r, "Failed to parse confirm spawn option: \"%s\": %m", |
1070 | optarg); | |
80876c20 LP |
1071 | break; |
1072 | ||
2a12e32e | 1073 | case ARG_SERVICE_WATCHDOGS: |
599c7c54 | 1074 | r = parse_boolean_argument("--service-watchdogs=", optarg, &arg_service_watchdogs); |
2a12e32e | 1075 | if (r < 0) |
599c7c54 | 1076 | return r; |
2a12e32e JK |
1077 | break; |
1078 | ||
9e58ff9c | 1079 | case ARG_SHOW_STATUS: |
d450b6f2 ZJS |
1080 | if (optarg) { |
1081 | r = parse_show_status(optarg, &arg_show_status); | |
ac7ec288 | 1082 | if (r < 0) |
2b5107e1 ZJS |
1083 | return log_error_errno(r, "Failed to parse show status boolean: \"%s\": %m", |
1084 | optarg); | |
d450b6f2 ZJS |
1085 | } else |
1086 | arg_show_status = SHOW_STATUS_YES; | |
6e98720f | 1087 | break; |
a5d87bf0 | 1088 | |
a16e1123 LP |
1089 | case ARG_DESERIALIZE: { |
1090 | int fd; | |
1091 | FILE *f; | |
1092 | ||
01e10de3 | 1093 | r = safe_atoi(optarg, &fd); |
2b5107e1 ZJS |
1094 | if (r < 0) |
1095 | log_error_errno(r, "Failed to parse deserialize option \"%s\": %m", optarg); | |
baaa35ad ZJS |
1096 | if (fd < 0) |
1097 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1098 | "Invalid deserialize fd: %d", | |
1099 | fd); | |
a16e1123 | 1100 | |
b9e74c39 | 1101 | (void) fd_cloexec(fd, true); |
01e10de3 LP |
1102 | |
1103 | f = fdopen(fd, "r"); | |
4a62c710 | 1104 | if (!f) |
2b5107e1 | 1105 | return log_error_errno(errno, "Failed to open serialization fd %d: %m", fd); |
a16e1123 | 1106 | |
74ca738f | 1107 | safe_fclose(arg_serialization); |
d3b1c508 | 1108 | arg_serialization = f; |
a16e1123 LP |
1109 | |
1110 | break; | |
1111 | } | |
1112 | ||
2660882b | 1113 | case ARG_SWITCHED_ROOT: |
bf4df7c3 | 1114 | arg_switched_root = true; |
d03bc1b8 HH |
1115 | break; |
1116 | ||
ee48dbd5 NC |
1117 | case ARG_MACHINE_ID: |
1118 | r = set_machine_id(optarg); | |
54500613 | 1119 | if (r < 0) |
2b5107e1 | 1120 | return log_error_errno(r, "MachineID '%s' is not valid: %m", optarg); |
ee48dbd5 NC |
1121 | break; |
1122 | ||
f170852a | 1123 | case 'h': |
fa0f4d8a | 1124 | arg_action = ACTION_HELP; |
f170852a LP |
1125 | break; |
1126 | ||
1d2e23ab LP |
1127 | case 'D': |
1128 | log_set_max_level(LOG_DEBUG); | |
1129 | break; | |
1130 | ||
099663ff LP |
1131 | case 'b': |
1132 | case 's': | |
1133 | case 'z': | |
cd57038a ZJS |
1134 | /* Just to eat away the sysvinit kernel cmdline args that we'll parse in |
1135 | * parse_proc_cmdline_item() or ignore, without any getopt() error messages. | |
1136 | */ | |
099663ff | 1137 | case '?': |
df0ff127 | 1138 | if (getpid_cached() != 1) |
099663ff | 1139 | return -EINVAL; |
601185b4 ZJS |
1140 | else |
1141 | return 0; | |
099663ff | 1142 | |
601185b4 | 1143 | default: |
04499a70 | 1144 | assert_not_reached(); |
f170852a LP |
1145 | } |
1146 | ||
d7a0f1f4 | 1147 | if (optind < argc && getpid_cached() != 1) |
9a9ca408 ZJS |
1148 | /* Hmm, when we aren't run as init system let's complain about excess arguments */ |
1149 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Excess arguments."); | |
1150 | ||
1151 | if (arg_action == ACTION_RUN && !arg_system && !user_arg_seen) | |
baaa35ad | 1152 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
9a9ca408 | 1153 | "Explicit --user argument required to run as user manager."); |
d821e6d6 | 1154 | |
f170852a LP |
1155 | return 0; |
1156 | } | |
1157 | ||
1158 | static int help(void) { | |
37ec0fdd LP |
1159 | _cleanup_free_ char *link = NULL; |
1160 | int r; | |
1161 | ||
1162 | r = terminal_urlify_man("systemd", "1", &link); | |
1163 | if (r < 0) | |
1164 | return log_oom(); | |
f170852a | 1165 | |
2e33c433 | 1166 | printf("%s [OPTIONS...]\n\n" |
7ae47326 ZJS |
1167 | "%sStarts and monitors system and user services.%s\n\n" |
1168 | "This program takes no positional arguments.\n\n" | |
1169 | "%sOptions%s:\n" | |
e537352b | 1170 | " -h --help Show this help\n" |
cb4069d9 | 1171 | " --version Show version\n" |
cd69e88b LP |
1172 | " --test Determine initial transaction, dump it and exit\n" |
1173 | " --system In combination with --test: operate as system service manager\n" | |
1174 | " --user In combination with --test: operate as per-user service manager\n" | |
b87c2aa6 | 1175 | " --no-pager Do not pipe output into a pager\n" |
80876c20 | 1176 | " --dump-configuration-items Dump understood unit configuration items\n" |
bbc1acab | 1177 | " --dump-bus-properties Dump exposed bus properties\n" |
5c08257b | 1178 | " --bus-introspect=PATH Write XML introspection data\n" |
9e58ff9c | 1179 | " --unit=UNIT Set default unit\n" |
b9e74c39 LP |
1180 | " --dump-core[=BOOL] Dump core on crash\n" |
1181 | " --crash-vt=NR Change to specified VT on crash\n" | |
1182 | " --crash-reboot[=BOOL] Reboot on crash\n" | |
1183 | " --crash-shell[=BOOL] Run shell on crash\n" | |
1184 | " --confirm-spawn[=BOOL] Ask for confirmation when spawning processes\n" | |
1185 | " --show-status[=BOOL] Show status updates on the console during bootup\n" | |
c1dc6153 | 1186 | " --log-target=TARGET Set log target (console, journal, kmsg, journal-or-kmsg, null)\n" |
9e58ff9c | 1187 | " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n" |
b9e74c39 LP |
1188 | " --log-color[=BOOL] Highlight important log messages\n" |
1189 | " --log-location[=BOOL] Include code location in log messages\n" | |
c5673ed0 | 1190 | " --log-time[=BOOL] Prefix log messages with current time\n" |
0a494f1f | 1191 | " --default-standard-output= Set default standard output for services\n" |
37ec0fdd | 1192 | " --default-standard-error= Set default standard error output for services\n" |
bc556335 DDM |
1193 | "\nSee the %s for details.\n", |
1194 | program_invocation_short_name, | |
1195 | ansi_highlight(), | |
1196 | ansi_normal(), | |
1197 | ansi_underline(), | |
1198 | ansi_normal(), | |
1199 | link); | |
f170852a LP |
1200 | |
1201 | return 0; | |
1202 | } | |
1203 | ||
2cc856ac LP |
1204 | static int prepare_reexecute( |
1205 | Manager *m, | |
1206 | FILE **ret_f, | |
1207 | FDSet **ret_fds, | |
1208 | bool switching_root) { | |
1209 | ||
48b90859 LP |
1210 | _cleanup_fdset_free_ FDSet *fds = NULL; |
1211 | _cleanup_fclose_ FILE *f = NULL; | |
a16e1123 LP |
1212 | int r; |
1213 | ||
1214 | assert(m); | |
2cc856ac LP |
1215 | assert(ret_f); |
1216 | assert(ret_fds); | |
a16e1123 | 1217 | |
6b78f9b4 | 1218 | r = manager_open_serialization(m, &f); |
48b90859 LP |
1219 | if (r < 0) |
1220 | return log_error_errno(r, "Failed to create serialization file: %m"); | |
a16e1123 | 1221 | |
71445ae7 | 1222 | /* Make sure nothing is really destructed when we shut down */ |
313cefa1 | 1223 | m->n_reloading++; |
718db961 | 1224 | bus_manager_send_reloading(m, true); |
71445ae7 | 1225 | |
6b78f9b4 | 1226 | fds = fdset_new(); |
48b90859 LP |
1227 | if (!fds) |
1228 | return log_oom(); | |
a16e1123 | 1229 | |
b3680f49 | 1230 | r = manager_serialize(m, f, fds, switching_root); |
48b90859 | 1231 | if (r < 0) |
d68c645b | 1232 | return r; |
a16e1123 | 1233 | |
48b90859 LP |
1234 | if (fseeko(f, 0, SEEK_SET) == (off_t) -1) |
1235 | return log_error_errno(errno, "Failed to rewind serialization fd: %m"); | |
a16e1123 | 1236 | |
6b78f9b4 | 1237 | r = fd_cloexec(fileno(f), false); |
48b90859 LP |
1238 | if (r < 0) |
1239 | return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization: %m"); | |
a16e1123 | 1240 | |
6b78f9b4 | 1241 | r = fdset_cloexec(fds, false); |
48b90859 LP |
1242 | if (r < 0) |
1243 | return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization fds: %m"); | |
a16e1123 | 1244 | |
2cc856ac LP |
1245 | *ret_f = TAKE_PTR(f); |
1246 | *ret_fds = TAKE_PTR(fds); | |
a16e1123 | 1247 | |
48b90859 | 1248 | return 0; |
a16e1123 LP |
1249 | } |
1250 | ||
a8b627aa LP |
1251 | static void bump_file_max_and_nr_open(void) { |
1252 | ||
ff3a7019 ZJS |
1253 | /* Let's bump fs.file-max and fs.nr_open to their respective maximums. On current kernels large |
1254 | * numbers of file descriptors are no longer a performance problem and their memory is properly | |
1255 | * tracked by memcg, thus counting them and limiting them in another two layers of limits is | |
1256 | * unnecessary and just complicates things. This function hence turns off 2 of the 4 levels of limits | |
1257 | * on file descriptors, and makes RLIMIT_NOLIMIT (soft + hard) the only ones that really matter. */ | |
a8b627aa LP |
1258 | |
1259 | #if BUMP_PROC_SYS_FS_FILE_MAX || BUMP_PROC_SYS_FS_NR_OPEN | |
a8b627aa LP |
1260 | int r; |
1261 | #endif | |
1262 | ||
1263 | #if BUMP_PROC_SYS_FS_FILE_MAX | |
409607c1 ZJS |
1264 | /* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously things were |
1265 | * different, but the operation would fail silently.) */ | |
56e8419a | 1266 | r = sysctl_writef("fs/file-max", "%li\n", LONG_MAX); |
a8b627aa LP |
1267 | if (r < 0) |
1268 | log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to bump fs.file-max, ignoring: %m"); | |
1269 | #endif | |
1270 | ||
a8b627aa LP |
1271 | #if BUMP_PROC_SYS_FS_NR_OPEN |
1272 | int v = INT_MAX; | |
1273 | ||
ff3a7019 ZJS |
1274 | /* Argh! The kernel enforces maximum and minimum values on the fs.nr_open, but we don't really know |
1275 | * what they are. The expression by which the maximum is determined is dependent on the architecture, | |
1276 | * and is something we don't really want to copy to userspace, as it is dependent on implementation | |
1277 | * details of the kernel. Since the kernel doesn't expose the maximum value to us, we can only try | |
1278 | * and hope. Hence, let's start with INT_MAX, and then keep halving the value until we find one that | |
1279 | * works. Ugly? Yes, absolutely, but kernel APIs are kernel APIs, so what do can we do... 🤯 */ | |
a8b627aa LP |
1280 | |
1281 | for (;;) { | |
1282 | int k; | |
1283 | ||
1284 | v &= ~(__SIZEOF_POINTER__ - 1); /* Round down to next multiple of the pointer size */ | |
1285 | if (v < 1024) { | |
1286 | log_warning("Can't bump fs.nr_open, value too small."); | |
1287 | break; | |
1288 | } | |
1289 | ||
1290 | k = read_nr_open(); | |
1291 | if (k < 0) { | |
1292 | log_error_errno(k, "Failed to read fs.nr_open: %m"); | |
1293 | break; | |
1294 | } | |
1295 | if (k >= v) { /* Already larger */ | |
1296 | log_debug("Skipping bump, value is already larger."); | |
1297 | break; | |
1298 | } | |
1299 | ||
56e8419a | 1300 | r = sysctl_writef("fs/nr_open", "%i\n", v); |
a8b627aa LP |
1301 | if (r == -EINVAL) { |
1302 | log_debug("Couldn't write fs.nr_open as %i, halving it.", v); | |
1303 | v /= 2; | |
1304 | continue; | |
1305 | } | |
1306 | if (r < 0) { | |
1307 | log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to bump fs.nr_open, ignoring: %m"); | |
1308 | break; | |
1309 | } | |
1310 | ||
1311 | log_debug("Successfully bumped fs.nr_open to %i", v); | |
1312 | break; | |
1313 | } | |
1314 | #endif | |
1315 | } | |
1316 | ||
4096d6f5 | 1317 | static int bump_rlimit_nofile(struct rlimit *saved_rlimit) { |
cda7faa9 | 1318 | struct rlimit new_rlimit; |
9264cc39 | 1319 | int r, nr; |
4096d6f5 | 1320 | |
52d62075 LP |
1321 | /* Get the underlying absolute limit the kernel enforces */ |
1322 | nr = read_nr_open(); | |
1323 | ||
cda7faa9 LP |
1324 | /* Calculate the new limits to use for us. Never lower from what we inherited. */ |
1325 | new_rlimit = (struct rlimit) { | |
1326 | .rlim_cur = MAX((rlim_t) nr, saved_rlimit->rlim_cur), | |
1327 | .rlim_max = MAX((rlim_t) nr, saved_rlimit->rlim_max), | |
1328 | }; | |
1329 | ||
1330 | /* Shortcut if nothing changes. */ | |
1331 | if (saved_rlimit->rlim_max >= new_rlimit.rlim_max && | |
1332 | saved_rlimit->rlim_cur >= new_rlimit.rlim_cur) { | |
1333 | log_debug("RLIMIT_NOFILE is already as high or higher than we need it, not bumping."); | |
1334 | return 0; | |
1335 | } | |
1336 | ||
52d62075 LP |
1337 | /* Bump up the resource limit for ourselves substantially, all the way to the maximum the kernel allows, for |
1338 | * both hard and soft. */ | |
cda7faa9 | 1339 | r = setrlimit_closest(RLIMIT_NOFILE, &new_rlimit); |
23bbb0de | 1340 | if (r < 0) |
3ce40911 | 1341 | return log_warning_errno(r, "Setting RLIMIT_NOFILE failed, ignoring: %m"); |
4096d6f5 LP |
1342 | |
1343 | return 0; | |
1344 | } | |
1345 | ||
fb3ae275 | 1346 | static int bump_rlimit_memlock(struct rlimit *saved_rlimit) { |
cda7faa9 | 1347 | struct rlimit new_rlimit; |
04d1ee0f | 1348 | uint64_t mm; |
fb3ae275 LP |
1349 | int r; |
1350 | ||
ff3a7019 ZJS |
1351 | /* BPF_MAP_TYPE_LPM_TRIE bpf maps are charged against RLIMIT_MEMLOCK, even if we have CAP_IPC_LOCK |
1352 | * which should normally disable such checks. We need them to implement IPAddressAllow= and | |
1353 | * IPAddressDeny=, hence let's bump the value high enough for our user. */ | |
fb3ae275 | 1354 | |
cda7faa9 LP |
1355 | /* Using MAX() on resource limits only is safe if RLIM_INFINITY is > 0. POSIX declares that rlim_t |
1356 | * must be unsigned, hence this is a given, but let's make this clear here. */ | |
1357 | assert_cc(RLIM_INFINITY > 0); | |
1358 | ||
ff3a7019 ZJS |
1359 | mm = physical_memory_scale(1, 8); /* Let's scale how much we allow to be locked by the amount of |
1360 | * physical RAM. We allow an eighth to be locked by us, just to | |
1361 | * pick a value. */ | |
04d1ee0f | 1362 | |
cda7faa9 | 1363 | new_rlimit = (struct rlimit) { |
04d1ee0f LP |
1364 | .rlim_cur = MAX3(HIGH_RLIMIT_MEMLOCK, saved_rlimit->rlim_cur, mm), |
1365 | .rlim_max = MAX3(HIGH_RLIMIT_MEMLOCK, saved_rlimit->rlim_max, mm), | |
cda7faa9 LP |
1366 | }; |
1367 | ||
1368 | if (saved_rlimit->rlim_max >= new_rlimit.rlim_cur && | |
1369 | saved_rlimit->rlim_cur >= new_rlimit.rlim_max) { | |
1370 | log_debug("RLIMIT_MEMLOCK is already as high or higher than we need it, not bumping."); | |
1371 | return 0; | |
1372 | } | |
1373 | ||
1374 | r = setrlimit_closest(RLIMIT_MEMLOCK, &new_rlimit); | |
fb3ae275 LP |
1375 | if (r < 0) |
1376 | return log_warning_errno(r, "Setting RLIMIT_MEMLOCK failed, ignoring: %m"); | |
1377 | ||
1378 | return 0; | |
1379 | } | |
1380 | ||
80758717 | 1381 | static void test_usr(void) { |
80758717 | 1382 | |
796ac4c1 | 1383 | /* Check that /usr is either on the same file system as / or mounted already. */ |
80758717 | 1384 | |
871c44a7 LP |
1385 | if (dir_is_empty("/usr") <= 0) |
1386 | return; | |
1387 | ||
8b173b5e | 1388 | log_warning("/usr appears to be on its own filesystem and is not already mounted. This is not a supported setup. " |
871c44a7 LP |
1389 | "Some things will probably break (sometimes even silently) in mysterious ways. " |
1390 | "Consult http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken for more information."); | |
1391 | } | |
1392 | ||
d3b1c508 | 1393 | static int enforce_syscall_archs(Set *archs) { |
349cc4a5 | 1394 | #if HAVE_SECCOMP |
d3b1c508 LP |
1395 | int r; |
1396 | ||
83f12b27 FS |
1397 | if (!is_seccomp_available()) |
1398 | return 0; | |
1399 | ||
469830d1 | 1400 | r = seccomp_restrict_archs(arg_syscall_archs); |
d3b1c508 | 1401 | if (r < 0) |
469830d1 | 1402 | return log_error_errno(r, "Failed to enforce system call architecture restrication: %m"); |
d3b1c508 | 1403 | #endif |
469830d1 | 1404 | return 0; |
d3b1c508 LP |
1405 | } |
1406 | ||
b6e2f329 LP |
1407 | static int status_welcome(void) { |
1408 | _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL; | |
1409 | int r; | |
1410 | ||
5ca02bfc | 1411 | if (!show_status_on(arg_show_status)) |
fd8c85c6 LP |
1412 | return 0; |
1413 | ||
d58ad743 LP |
1414 | r = parse_os_release(NULL, |
1415 | "PRETTY_NAME", &pretty_name, | |
209c1470 | 1416 | "ANSI_COLOR", &ansi_color); |
d58ad743 LP |
1417 | if (r < 0) |
1418 | log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, | |
1419 | "Failed to read os-release file, ignoring: %m"); | |
b6e2f329 | 1420 | |
dc9b5816 | 1421 | if (log_get_show_color()) |
a885727a | 1422 | return status_printf(NULL, 0, |
dc9b5816 ZJS |
1423 | "\nWelcome to \x1B[%sm%s\x1B[0m!\n", |
1424 | isempty(ansi_color) ? "1" : ansi_color, | |
1425 | isempty(pretty_name) ? "Linux" : pretty_name); | |
1426 | else | |
a885727a | 1427 | return status_printf(NULL, 0, |
dc9b5816 ZJS |
1428 | "\nWelcome to %s!\n", |
1429 | isempty(pretty_name) ? "Linux" : pretty_name); | |
b6e2f329 LP |
1430 | } |
1431 | ||
fdd25311 LP |
1432 | static int write_container_id(void) { |
1433 | const char *c; | |
7756528e | 1434 | int r = 0; /* avoid false maybe-uninitialized warning */ |
fdd25311 LP |
1435 | |
1436 | c = getenv("container"); | |
1437 | if (isempty(c)) | |
1438 | return 0; | |
1439 | ||
8612da97 LP |
1440 | RUN_WITH_UMASK(0022) |
1441 | r = write_string_file("/run/systemd/container", c, WRITE_STRING_FILE_CREATE); | |
19854865 | 1442 | if (r < 0) |
f1f849b0 | 1443 | return log_warning_errno(r, "Failed to write /run/systemd/container, ignoring: %m"); |
19854865 LP |
1444 | |
1445 | return 1; | |
1446 | } | |
1447 | ||
1448 | static int bump_unix_max_dgram_qlen(void) { | |
1449 | _cleanup_free_ char *qlen = NULL; | |
1450 | unsigned long v; | |
1451 | int r; | |
1452 | ||
ff3a7019 ZJS |
1453 | /* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel default of 16 is simply too low. We set |
1454 | * the value really really early during boot, so that it is actually applied to all our sockets, | |
1455 | * including the $NOTIFY_SOCKET one. */ | |
19854865 LP |
1456 | |
1457 | r = read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen); | |
1458 | if (r < 0) | |
ff3a7019 ZJS |
1459 | return log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, |
1460 | "Failed to read AF_UNIX datagram queue length, ignoring: %m"); | |
19854865 LP |
1461 | |
1462 | r = safe_atolu(qlen, &v); | |
1463 | if (r < 0) | |
3130fca5 | 1464 | return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length '%s', ignoring: %m", qlen); |
19854865 LP |
1465 | |
1466 | if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN) | |
1467 | return 0; | |
1468 | ||
ff3a7019 ZJS |
1469 | r = write_string_filef("/proc/sys/net/unix/max_dgram_qlen", WRITE_STRING_FILE_DISABLE_BUFFER, |
1470 | "%lu", DEFAULT_UNIX_MAX_DGRAM_QLEN); | |
19854865 LP |
1471 | if (r < 0) |
1472 | return log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, | |
1473 | "Failed to bump AF_UNIX datagram queue length, ignoring: %m"); | |
1474 | ||
1475 | return 1; | |
fdd25311 LP |
1476 | } |
1477 | ||
32391275 FB |
1478 | static int fixup_environment(void) { |
1479 | _cleanup_free_ char *term = NULL; | |
4dc63c4b | 1480 | const char *t; |
32391275 FB |
1481 | int r; |
1482 | ||
43db615b LP |
1483 | /* Only fix up the environment when we are started as PID 1 */ |
1484 | if (getpid_cached() != 1) | |
1485 | return 0; | |
1486 | ||
1487 | /* We expect the environment to be set correctly if run inside a container. */ | |
84af7821 LP |
1488 | if (detect_container() > 0) |
1489 | return 0; | |
1490 | ||
ff3a7019 ZJS |
1491 | /* When started as PID1, the kernel uses /dev/console for our stdios and uses TERM=linux whatever the |
1492 | * backend device used by the console. We try to make a better guess here since some consoles might | |
1493 | * not have support for color mode for example. | |
32391275 | 1494 | * |
43db615b | 1495 | * However if TERM was configured through the kernel command line then leave it alone. */ |
1d84ad94 | 1496 | r = proc_cmdline_get_key("TERM", 0, &term); |
32391275 FB |
1497 | if (r < 0) |
1498 | return r; | |
32391275 | 1499 | |
4dc63c4b LP |
1500 | t = term ?: default_term_for_tty("/dev/console"); |
1501 | ||
1502 | if (setenv("TERM", t, 1) < 0) | |
32391275 FB |
1503 | return -errno; |
1504 | ||
9d48671c | 1505 | /* The kernels sets HOME=/ for init. Let's undo this. */ |
44ee03d1 ZJS |
1506 | if (path_equal_ptr(getenv("HOME"), "/")) |
1507 | assert_se(unsetenv("HOME") == 0); | |
9d48671c | 1508 | |
32391275 FB |
1509 | return 0; |
1510 | } | |
1511 | ||
6808a0bc LP |
1512 | static void redirect_telinit(int argc, char *argv[]) { |
1513 | ||
1514 | /* This is compatibility support for SysV, where calling init as a user is identical to telinit. */ | |
1515 | ||
1516 | #if HAVE_SYSV_COMPAT | |
1517 | if (getpid_cached() == 1) | |
1518 | return; | |
1519 | ||
2306d177 | 1520 | if (!invoked_as(argv, "init")) |
6808a0bc LP |
1521 | return; |
1522 | ||
1523 | execv(SYSTEMCTL_BINARY_PATH, argv); | |
1524 | log_error_errno(errno, "Failed to exec " SYSTEMCTL_BINARY_PATH ": %m"); | |
a45d7127 | 1525 | exit(EXIT_FAILURE); |
6808a0bc LP |
1526 | #endif |
1527 | } | |
1528 | ||
4a36297c LP |
1529 | static int become_shutdown( |
1530 | const char *shutdown_verb, | |
7eb35049 | 1531 | int retval) { |
4a36297c LP |
1532 | |
1533 | char log_level[DECIMAL_STR_MAX(int) + 1], | |
e73c54b8 JK |
1534 | exit_code[DECIMAL_STR_MAX(uint8_t) + 1], |
1535 | timeout[DECIMAL_STR_MAX(usec_t) + 1]; | |
4a36297c | 1536 | |
e73c54b8 | 1537 | const char* command_line[13] = { |
4a36297c LP |
1538 | SYSTEMD_SHUTDOWN_BINARY_PATH, |
1539 | shutdown_verb, | |
e73c54b8 | 1540 | "--timeout", timeout, |
4a36297c LP |
1541 | "--log-level", log_level, |
1542 | "--log-target", | |
1543 | }; | |
1544 | ||
1545 | _cleanup_strv_free_ char **env_block = NULL; | |
f16890f8 | 1546 | usec_t watchdog_timer = 0; |
e73c54b8 | 1547 | size_t pos = 7; |
4a36297c LP |
1548 | int r; |
1549 | ||
7eb35049 | 1550 | assert(shutdown_verb); |
234519ae | 1551 | assert(!command_line[pos]); |
4a36297c LP |
1552 | env_block = strv_copy(environ); |
1553 | ||
1554 | xsprintf(log_level, "%d", log_get_max_level()); | |
e73c54b8 | 1555 | xsprintf(timeout, "%" PRI_USEC "us", arg_default_timeout_stop_usec); |
4a36297c LP |
1556 | |
1557 | switch (log_get_target()) { | |
1558 | ||
1559 | case LOG_TARGET_KMSG: | |
1560 | case LOG_TARGET_JOURNAL_OR_KMSG: | |
1561 | case LOG_TARGET_SYSLOG_OR_KMSG: | |
1562 | command_line[pos++] = "kmsg"; | |
1563 | break; | |
1564 | ||
1565 | case LOG_TARGET_NULL: | |
1566 | command_line[pos++] = "null"; | |
1567 | break; | |
1568 | ||
1569 | case LOG_TARGET_CONSOLE: | |
1570 | default: | |
1571 | command_line[pos++] = "console"; | |
1572 | break; | |
1573 | }; | |
1574 | ||
1575 | if (log_get_show_color()) | |
1576 | command_line[pos++] = "--log-color"; | |
1577 | ||
1578 | if (log_get_show_location()) | |
1579 | command_line[pos++] = "--log-location"; | |
1580 | ||
c5673ed0 DS |
1581 | if (log_get_show_time()) |
1582 | command_line[pos++] = "--log-time"; | |
1583 | ||
4a36297c LP |
1584 | if (streq(shutdown_verb, "exit")) { |
1585 | command_line[pos++] = "--exit-code"; | |
1586 | command_line[pos++] = exit_code; | |
1587 | xsprintf(exit_code, "%d", retval); | |
1588 | } | |
1589 | ||
1590 | assert(pos < ELEMENTSOF(command_line)); | |
1591 | ||
acafd7d8 | 1592 | if (streq(shutdown_verb, "reboot")) |
65224c1d | 1593 | watchdog_timer = arg_reboot_watchdog; |
acafd7d8 LB |
1594 | else if (streq(shutdown_verb, "kexec")) |
1595 | watchdog_timer = arg_kexec_watchdog; | |
1596 | ||
f16890f8 FB |
1597 | /* If we reboot or kexec let's set the shutdown watchdog and tell the |
1598 | * shutdown binary to repeatedly ping it */ | |
1599 | r = watchdog_setup(watchdog_timer); | |
1600 | watchdog_close(r < 0); | |
4a36297c | 1601 | |
f16890f8 FB |
1602 | /* Tell the binary how often to ping, ignore failure */ |
1603 | (void) strv_extendf(&env_block, "WATCHDOG_USEC="USEC_FMT, watchdog_timer); | |
8a2c1fbf | 1604 | |
f16890f8 FB |
1605 | if (arg_watchdog_device) |
1606 | (void) strv_extendf(&env_block, "WATCHDOG_DEVICE=%s", arg_watchdog_device); | |
4a36297c | 1607 | |
3178d23d FB |
1608 | /* Avoid the creation of new processes forked by the kernel; at this |
1609 | * point, we will not listen to the signals anyway */ | |
4a36297c LP |
1610 | if (detect_container() <= 0) |
1611 | (void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER); | |
1612 | ||
1613 | execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block); | |
1614 | return -errno; | |
1615 | } | |
1616 | ||
e839bafd LP |
1617 | static void initialize_clock(void) { |
1618 | int r; | |
1619 | ||
3753325b LP |
1620 | /* This is called very early on, before we parse the kernel command line or otherwise figure out why |
1621 | * we are running, but only once. */ | |
1622 | ||
e839bafd LP |
1623 | if (clock_is_localtime(NULL) > 0) { |
1624 | int min; | |
1625 | ||
ff3a7019 | 1626 | /* The very first call of settimeofday() also does a time warp in the kernel. |
e839bafd | 1627 | * |
ff3a7019 ZJS |
1628 | * In the rtc-in-local time mode, we set the kernel's timezone, and rely on external tools to |
1629 | * take care of maintaining the RTC and do all adjustments. This matches the behavior of | |
1630 | * Windows, which leaves the RTC alone if the registry tells that the RTC runs in UTC. | |
e839bafd LP |
1631 | */ |
1632 | r = clock_set_timezone(&min); | |
1633 | if (r < 0) | |
1634 | log_error_errno(r, "Failed to apply local time delta, ignoring: %m"); | |
1635 | else | |
1636 | log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min); | |
1637 | ||
d46b79bb | 1638 | } else if (!in_initrd()) |
e839bafd LP |
1639 | /* |
1640 | * Do a dummy very first call to seal the kernel's time warp magic. | |
1641 | * | |
ff3a7019 ZJS |
1642 | * Do not call this from inside the initrd. The initrd might not carry /etc/adjtime with |
1643 | * LOCAL, but the real system could be set up that way. In such case, we need to delay the | |
1644 | * time-warp or the sealing until we reach the real system. | |
e839bafd | 1645 | * |
ff3a7019 ZJS |
1646 | * Do no set the kernel's timezone. The concept of local time cannot be supported reliably, |
1647 | * the time will jump or be incorrect at every daylight saving time change. All kernel local | |
1648 | * time concepts will be treated as UTC that way. | |
e839bafd LP |
1649 | */ |
1650 | (void) clock_reset_timewarp(); | |
e839bafd | 1651 | |
b10abe4b EI |
1652 | ClockChangeDirection change_dir; |
1653 | r = clock_apply_epoch(&change_dir); | |
1654 | if (r > 0 && change_dir == CLOCK_CHANGE_FORWARD) | |
e839bafd | 1655 | log_info("System time before build time, advancing clock."); |
b10abe4b EI |
1656 | else if (r > 0 && change_dir == CLOCK_CHANGE_BACKWARD) |
1657 | log_info("System time is further ahead than %s after build time, resetting clock to build time.", | |
1658 | FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY)); | |
1659 | else if (r < 0 && change_dir == CLOCK_CHANGE_FORWARD) | |
1660 | log_error_errno(r, "Current system time is before build time, but cannot correct: %m"); | |
1661 | else if (r < 0 && change_dir == CLOCK_CHANGE_BACKWARD) | |
1662 | log_error_errno(r, "Current system time is further ahead %s after build time, but cannot correct: %m", | |
1663 | FORMAT_TIMESPAN(CLOCK_VALID_RANGE_USEC_MAX, USEC_PER_DAY)); | |
e839bafd LP |
1664 | } |
1665 | ||
3753325b LP |
1666 | static void apply_clock_update(void) { |
1667 | struct timespec ts; | |
1668 | ||
1669 | /* This is called later than initialize_clock(), i.e. after we parsed configuration files/kernel | |
1670 | * command line and such. */ | |
1671 | ||
1672 | if (arg_clock_usec == 0) | |
1673 | return; | |
1674 | ||
45250e66 LP |
1675 | if (getpid_cached() != 1) |
1676 | return; | |
1677 | ||
3753325b LP |
1678 | if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, arg_clock_usec)) < 0) |
1679 | log_error_errno(errno, "Failed to set system clock to time specified on kernel command line: %m"); | |
04f5c018 | 1680 | else |
3753325b | 1681 | log_info("Set system clock to %s, as specified on the kernel command line.", |
04f5c018 | 1682 | FORMAT_TIMESTAMP(arg_clock_usec)); |
3753325b LP |
1683 | } |
1684 | ||
d247f232 | 1685 | static void cmdline_take_random_seed(void) { |
d247f232 LP |
1686 | size_t suggested; |
1687 | int r; | |
1688 | ||
1689 | if (arg_random_seed_size == 0) | |
1690 | return; | |
1691 | ||
1692 | if (getpid_cached() != 1) | |
1693 | return; | |
1694 | ||
1695 | assert(arg_random_seed); | |
1696 | suggested = random_pool_size(); | |
1697 | ||
1698 | if (arg_random_seed_size < suggested) | |
1699 | log_warning("Random seed specified on kernel command line has size %zu, but %zu bytes required to fill entropy pool.", | |
1700 | arg_random_seed_size, suggested); | |
1701 | ||
61bd7d1e | 1702 | r = random_write_entropy(-1, arg_random_seed, arg_random_seed_size, true); |
d247f232 LP |
1703 | if (r < 0) { |
1704 | log_warning_errno(r, "Failed to credit entropy specified on kernel command line, ignoring: %m"); | |
1705 | return; | |
1706 | } | |
1707 | ||
1708 | log_notice("Successfully credited entropy passed on kernel command line.\n" | |
ff3a7019 ZJS |
1709 | "Note that the seed provided this way is accessible to unprivileged programs. " |
1710 | "This functionality should not be used outside of testing environments."); | |
d247f232 LP |
1711 | } |
1712 | ||
1e41242e | 1713 | static void initialize_coredump(bool skip_setup) { |
752bcb77 | 1714 | #if ENABLE_COREDUMP |
1e41242e LP |
1715 | if (getpid_cached() != 1) |
1716 | return; | |
1717 | ||
ff3a7019 ZJS |
1718 | /* Don't limit the core dump size, so that coredump handlers such as systemd-coredump (which honour |
1719 | * the limit) will process core dumps for system services by default. */ | |
1e41242e LP |
1720 | if (setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0) |
1721 | log_warning_errno(errno, "Failed to set RLIMIT_CORE: %m"); | |
1722 | ||
ff3a7019 ZJS |
1723 | /* But at the same time, turn off the core_pattern logic by default, so that no coredumps are stored |
1724 | * until the systemd-coredump tool is enabled via sysctl. However it can be changed via the kernel | |
1725 | * command line later so core dumps can still be generated during early startup and in initramfs. */ | |
1e41242e | 1726 | if (!skip_setup) |
e557b1a6 | 1727 | disable_coredumps(); |
752bcb77 | 1728 | #endif |
1e41242e LP |
1729 | } |
1730 | ||
c6885f5f FB |
1731 | static void initialize_core_pattern(bool skip_setup) { |
1732 | int r; | |
1733 | ||
1734 | if (skip_setup || !arg_early_core_pattern) | |
1735 | return; | |
1736 | ||
1737 | if (getpid_cached() != 1) | |
1738 | return; | |
1739 | ||
57512c89 | 1740 | r = write_string_file("/proc/sys/kernel/core_pattern", arg_early_core_pattern, WRITE_STRING_FILE_DISABLE_BUFFER); |
c6885f5f | 1741 | if (r < 0) |
ff3a7019 ZJS |
1742 | log_warning_errno(r, "Failed to write '%s' to /proc/sys/kernel/core_pattern, ignoring: %m", |
1743 | arg_early_core_pattern); | |
c6885f5f FB |
1744 | } |
1745 | ||
61fbbac1 ZJS |
1746 | static void update_cpu_affinity(bool skip_setup) { |
1747 | _cleanup_free_ char *mask = NULL; | |
1748 | ||
1749 | if (skip_setup || !arg_cpu_affinity.set) | |
1750 | return; | |
1751 | ||
1752 | assert(arg_cpu_affinity.allocated > 0); | |
1753 | ||
1754 | mask = cpu_set_to_string(&arg_cpu_affinity); | |
1755 | log_debug("Setting CPU affinity to %s.", strnull(mask)); | |
1756 | ||
1757 | if (sched_setaffinity(0, arg_cpu_affinity.allocated, arg_cpu_affinity.set) < 0) | |
1758 | log_warning_errno(errno, "Failed to set CPU affinity: %m"); | |
1759 | } | |
1760 | ||
b070c7c0 MS |
1761 | static void update_numa_policy(bool skip_setup) { |
1762 | int r; | |
1763 | _cleanup_free_ char *nodes = NULL; | |
1764 | const char * policy = NULL; | |
1765 | ||
1766 | if (skip_setup || !mpol_is_valid(numa_policy_get_type(&arg_numa_policy))) | |
1767 | return; | |
1768 | ||
1769 | if (DEBUG_LOGGING) { | |
1770 | policy = mpol_to_string(numa_policy_get_type(&arg_numa_policy)); | |
1771 | nodes = cpu_set_to_range_string(&arg_numa_policy.nodes); | |
1772 | log_debug("Setting NUMA policy to %s, with nodes %s.", strnull(policy), strnull(nodes)); | |
1773 | } | |
1774 | ||
1775 | r = apply_numa_policy(&arg_numa_policy); | |
1776 | if (r == -EOPNOTSUPP) | |
1777 | log_debug_errno(r, "NUMA support not available, ignoring."); | |
1778 | else if (r < 0) | |
1779 | log_warning_errno(r, "Failed to set NUMA memory policy: %m"); | |
1780 | } | |
1781 | ||
19fd72df LP |
1782 | static void filter_args( |
1783 | const char* dst[], | |
1784 | size_t *dst_index, | |
1785 | char **src, | |
1786 | int argc) { | |
1787 | ||
846f1da4 | 1788 | assert(dst); |
19fd72df | 1789 | assert(dst_index); |
846f1da4 ZJS |
1790 | |
1791 | /* Copy some filtered arguments into the dst array from src. */ | |
1792 | for (int i = 1; i < argc; i++) { | |
1793 | if (STR_IN_SET(src[i], | |
1794 | "--switched-root", | |
1795 | "--system", | |
1796 | "--user")) | |
1797 | continue; | |
1798 | ||
1799 | if (startswith(src[i], "--deserialize=")) | |
1800 | continue; | |
1801 | if (streq(src[i], "--deserialize")) { | |
1802 | i++; /* Skip the argument too */ | |
1803 | continue; | |
1804 | } | |
1805 | ||
1806 | /* Skip target unit designators. We already acted upon this information and have queued | |
1807 | * appropriate jobs. We don't want to redo all this after reexecution. */ | |
1808 | if (startswith(src[i], "--unit=")) | |
1809 | continue; | |
1810 | if (streq(src[i], "--unit")) { | |
1811 | i++; /* Skip the argument too */ | |
1812 | continue; | |
1813 | } | |
1814 | ||
1815 | if (startswith(src[i], | |
1816 | in_initrd() ? "rd.systemd.unit=" : "systemd.unit=")) | |
1817 | continue; | |
1818 | ||
1819 | if (runlevel_to_target(src[i])) | |
1820 | continue; | |
1821 | ||
1822 | /* Seems we have a good old option. Let's pass it over to the new instance. */ | |
19fd72df | 1823 | dst[(*dst_index)++] = src[i]; |
846f1da4 ZJS |
1824 | } |
1825 | } | |
1826 | ||
3c7878f9 LP |
1827 | static void do_reexecute( |
1828 | int argc, | |
846f1da4 | 1829 | char* argv[], |
3c7878f9 LP |
1830 | const struct rlimit *saved_rlimit_nofile, |
1831 | const struct rlimit *saved_rlimit_memlock, | |
1832 | FDSet *fds, | |
1833 | const char *switch_root_dir, | |
1834 | const char *switch_root_init, | |
1835 | const char **ret_error_message) { | |
1836 | ||
19fd72df | 1837 | size_t i, args_size; |
3c7878f9 LP |
1838 | const char **args; |
1839 | int r; | |
1840 | ||
19fd72df | 1841 | assert(argc >= 0); |
3c7878f9 LP |
1842 | assert(saved_rlimit_nofile); |
1843 | assert(saved_rlimit_memlock); | |
1844 | assert(ret_error_message); | |
1845 | ||
ff3a7019 ZJS |
1846 | /* Close and disarm the watchdog, so that the new instance can reinitialize it, but doesn't get |
1847 | * rebooted while we do that */ | |
3c7878f9 LP |
1848 | watchdog_close(true); |
1849 | ||
ddfa8b0b LP |
1850 | /* Reset RLIMIT_NOFILE + RLIMIT_MEMLOCK back to the kernel defaults, so that the new systemd can pass |
1851 | * the kernel default to its child processes */ | |
1852 | if (saved_rlimit_nofile->rlim_cur != 0) | |
3c7878f9 | 1853 | (void) setrlimit(RLIMIT_NOFILE, saved_rlimit_nofile); |
ddfa8b0b | 1854 | if (saved_rlimit_memlock->rlim_cur != RLIM_INFINITY) |
3c7878f9 LP |
1855 | (void) setrlimit(RLIMIT_MEMLOCK, saved_rlimit_memlock); |
1856 | ||
1857 | if (switch_root_dir) { | |
ff3a7019 ZJS |
1858 | /* Kill all remaining processes from the initrd, but don't wait for them, so that we can |
1859 | * handle the SIGCHLD for them after deserializing. */ | |
e73c54b8 | 1860 | broadcast_signal(SIGTERM, false, true, arg_default_timeout_stop_usec); |
3c7878f9 LP |
1861 | |
1862 | /* And switch root with MS_MOVE, because we remove the old directory afterwards and detach it. */ | |
1863 | r = switch_root(switch_root_dir, "/mnt", true, MS_MOVE); | |
1864 | if (r < 0) | |
1865 | log_error_errno(r, "Failed to switch root, trying to continue: %m"); | |
1866 | } | |
1867 | ||
846f1da4 | 1868 | args_size = argc + 6; |
3c7878f9 LP |
1869 | args = newa(const char*, args_size); |
1870 | ||
1871 | if (!switch_root_init) { | |
846f1da4 | 1872 | char sfd[DECIMAL_STR_MAX(int)]; |
3c7878f9 | 1873 | |
ff3a7019 ZJS |
1874 | /* First try to spawn ourselves with the right path, and with full serialization. We do this |
1875 | * only if the user didn't specify an explicit init to spawn. */ | |
3c7878f9 LP |
1876 | |
1877 | assert(arg_serialization); | |
1878 | assert(fds); | |
1879 | ||
1880 | xsprintf(sfd, "%i", fileno(arg_serialization)); | |
1881 | ||
846f1da4 ZJS |
1882 | i = 1; /* Leave args[0] empty for now. */ |
1883 | filter_args(args, &i, argv, argc); | |
1884 | ||
3c7878f9 LP |
1885 | if (switch_root_dir) |
1886 | args[i++] = "--switched-root"; | |
1887 | args[i++] = arg_system ? "--system" : "--user"; | |
1888 | args[i++] = "--deserialize"; | |
1889 | args[i++] = sfd; | |
1890 | args[i++] = NULL; | |
1891 | ||
1892 | assert(i <= args_size); | |
1893 | ||
1894 | /* | |
ff3a7019 ZJS |
1895 | * We want valgrind to print its memory usage summary before reexecution. Valgrind won't do |
1896 | * this is on its own on exec(), but it will do it on exit(). Hence, to ensure we get a | |
1897 | * summary here, fork() off a child, let it exit() cleanly, so that it prints the summary, | |
1898 | * and wait() for it in the parent, before proceeding into the exec(). | |
3c7878f9 LP |
1899 | */ |
1900 | valgrind_summary_hack(); | |
1901 | ||
846f1da4 | 1902 | args[0] = SYSTEMD_BINARY_PATH; |
3c7878f9 | 1903 | (void) execv(args[0], (char* const*) args); |
846f1da4 | 1904 | log_debug_errno(errno, "Failed to execute our own binary %s, trying fallback: %m", args[0]); |
3c7878f9 LP |
1905 | } |
1906 | ||
ff3a7019 ZJS |
1907 | /* Try the fallback, if there is any, without any serialization. We pass the original argv[] and |
1908 | * envp[]. (Well, modulo the ordering changes due to getopt() in argv[], and some cleanups in envp[], | |
1909 | * but let's hope that doesn't matter.) */ | |
3c7878f9 LP |
1910 | |
1911 | arg_serialization = safe_fclose(arg_serialization); | |
1912 | fds = fdset_free(fds); | |
1913 | ||
1914 | /* Reopen the console */ | |
1915 | (void) make_console_stdio(); | |
1916 | ||
846f1da4 ZJS |
1917 | i = 1; /* Leave args[0] empty for now. */ |
1918 | for (int j = 1; j <= argc; j++) | |
3c7878f9 | 1919 | args[i++] = argv[j]; |
3c7878f9 LP |
1920 | assert(i <= args_size); |
1921 | ||
5238e957 | 1922 | /* Re-enable any blocked signals, especially important if we switch from initial ramdisk to init=... */ |
3c7878f9 LP |
1923 | (void) reset_all_signal_handlers(); |
1924 | (void) reset_signal_mask(); | |
595225af | 1925 | (void) rlimit_nofile_safe(); |
3c7878f9 LP |
1926 | |
1927 | if (switch_root_init) { | |
1928 | args[0] = switch_root_init; | |
a5cede8c | 1929 | (void) execve(args[0], (char* const*) args, saved_env); |
846f1da4 | 1930 | log_warning_errno(errno, "Failed to execute configured init %s, trying fallback: %m", args[0]); |
3c7878f9 LP |
1931 | } |
1932 | ||
1933 | args[0] = "/sbin/init"; | |
1934 | (void) execv(args[0], (char* const*) args); | |
1935 | r = -errno; | |
1936 | ||
1937 | manager_status_printf(NULL, STATUS_TYPE_EMERGENCY, | |
1938 | ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL, | |
1939 | "Failed to execute /sbin/init"); | |
1940 | ||
1941 | if (r == -ENOENT) { | |
1942 | log_warning("No /sbin/init, trying fallback"); | |
1943 | ||
1944 | args[0] = "/bin/sh"; | |
1945 | args[1] = NULL; | |
a5cede8c | 1946 | (void) execve(args[0], (char* const*) args, saved_env); |
3c7878f9 LP |
1947 | log_error_errno(errno, "Failed to execute /bin/sh, giving up: %m"); |
1948 | } else | |
1949 | log_warning_errno(r, "Failed to execute /sbin/init, giving up: %m"); | |
1950 | ||
1951 | *ret_error_message = "Failed to execute fallback shell"; | |
1952 | } | |
1953 | ||
7eb35049 LP |
1954 | static int invoke_main_loop( |
1955 | Manager *m, | |
a9fd4cd1 FB |
1956 | const struct rlimit *saved_rlimit_nofile, |
1957 | const struct rlimit *saved_rlimit_memlock, | |
7eb35049 LP |
1958 | bool *ret_reexecute, |
1959 | int *ret_retval, /* Return parameters relevant for shutting down */ | |
1960 | const char **ret_shutdown_verb, /* … */ | |
1961 | FDSet **ret_fds, /* Return parameters for reexecuting */ | |
1962 | char **ret_switch_root_dir, /* … */ | |
1963 | char **ret_switch_root_init, /* … */ | |
1964 | const char **ret_error_message) { | |
1965 | ||
1966 | int r; | |
1967 | ||
1968 | assert(m); | |
a9fd4cd1 FB |
1969 | assert(saved_rlimit_nofile); |
1970 | assert(saved_rlimit_memlock); | |
7eb35049 LP |
1971 | assert(ret_reexecute); |
1972 | assert(ret_retval); | |
1973 | assert(ret_shutdown_verb); | |
1974 | assert(ret_fds); | |
1975 | assert(ret_switch_root_dir); | |
1976 | assert(ret_switch_root_init); | |
1977 | assert(ret_error_message); | |
1978 | ||
1979 | for (;;) { | |
1980 | r = manager_loop(m); | |
1981 | if (r < 0) { | |
1982 | *ret_error_message = "Failed to run main loop"; | |
1983 | return log_emergency_errno(r, "Failed to run main loop: %m"); | |
1984 | } | |
1985 | ||
3ca4d0b3 | 1986 | switch ((ManagerObjective) r) { |
7eb35049 | 1987 | |
a6ecbf83 | 1988 | case MANAGER_RELOAD: { |
bda7d78b | 1989 | LogTarget saved_log_target; |
a6ecbf83 FB |
1990 | int saved_log_level; |
1991 | ||
7eb35049 LP |
1992 | log_info("Reloading."); |
1993 | ||
ff3a7019 ZJS |
1994 | /* First, save any overridden log level/target, then parse the configuration file, |
1995 | * which might change the log level to new settings. */ | |
bda7d78b | 1996 | |
a6ecbf83 | 1997 | saved_log_level = m->log_level_overridden ? log_get_max_level() : -1; |
bda7d78b | 1998 | saved_log_target = m->log_target_overridden ? log_get_target() : _LOG_TARGET_INVALID; |
a6ecbf83 | 1999 | |
a9fd4cd1 | 2000 | (void) parse_configuration(saved_rlimit_nofile, saved_rlimit_memlock); |
7eb35049 LP |
2001 | |
2002 | set_manager_defaults(m); | |
986935cf | 2003 | set_manager_settings(m); |
7eb35049 | 2004 | |
61fbbac1 | 2005 | update_cpu_affinity(false); |
b070c7c0 | 2006 | update_numa_policy(false); |
61fbbac1 | 2007 | |
a6ecbf83 FB |
2008 | if (saved_log_level >= 0) |
2009 | manager_override_log_level(m, saved_log_level); | |
bda7d78b FB |
2010 | if (saved_log_target >= 0) |
2011 | manager_override_log_target(m, saved_log_target); | |
a6ecbf83 | 2012 | |
7eb35049 LP |
2013 | r = manager_reload(m); |
2014 | if (r < 0) | |
ff3a7019 ZJS |
2015 | /* Reloading failed before the point of no return. |
2016 | * Let's continue running as if nothing happened. */ | |
7a35fa24 | 2017 | m->objective = MANAGER_OK; |
7eb35049 LP |
2018 | |
2019 | break; | |
a6ecbf83 | 2020 | } |
7eb35049 LP |
2021 | |
2022 | case MANAGER_REEXECUTE: | |
2023 | ||
2024 | r = prepare_reexecute(m, &arg_serialization, ret_fds, false); | |
2025 | if (r < 0) { | |
2026 | *ret_error_message = "Failed to prepare for reexecution"; | |
2027 | return r; | |
2028 | } | |
2029 | ||
2030 | log_notice("Reexecuting."); | |
2031 | ||
2032 | *ret_reexecute = true; | |
2033 | *ret_retval = EXIT_SUCCESS; | |
2034 | *ret_shutdown_verb = NULL; | |
2035 | *ret_switch_root_dir = *ret_switch_root_init = NULL; | |
2036 | ||
2037 | return 0; | |
2038 | ||
2039 | case MANAGER_SWITCH_ROOT: | |
2040 | if (!m->switch_root_init) { | |
2041 | r = prepare_reexecute(m, &arg_serialization, ret_fds, true); | |
2042 | if (r < 0) { | |
2043 | *ret_error_message = "Failed to prepare for reexecution"; | |
2044 | return r; | |
2045 | } | |
2046 | } else | |
2047 | *ret_fds = NULL; | |
2048 | ||
2049 | log_notice("Switching root."); | |
2050 | ||
2051 | *ret_reexecute = true; | |
2052 | *ret_retval = EXIT_SUCCESS; | |
2053 | *ret_shutdown_verb = NULL; | |
2054 | ||
2055 | /* Steal the switch root parameters */ | |
49052946 YW |
2056 | *ret_switch_root_dir = TAKE_PTR(m->switch_root); |
2057 | *ret_switch_root_init = TAKE_PTR(m->switch_root_init); | |
7eb35049 LP |
2058 | |
2059 | return 0; | |
2060 | ||
2061 | case MANAGER_EXIT: | |
2062 | ||
2063 | if (MANAGER_IS_USER(m)) { | |
2064 | log_debug("Exit."); | |
2065 | ||
2066 | *ret_reexecute = false; | |
2067 | *ret_retval = m->return_value; | |
2068 | *ret_shutdown_verb = NULL; | |
2069 | *ret_fds = NULL; | |
2070 | *ret_switch_root_dir = *ret_switch_root_init = NULL; | |
2071 | ||
2072 | return 0; | |
2073 | } | |
2074 | ||
2075 | _fallthrough_; | |
2076 | case MANAGER_REBOOT: | |
2077 | case MANAGER_POWEROFF: | |
2078 | case MANAGER_HALT: | |
2079 | case MANAGER_KEXEC: { | |
af41e508 LP |
2080 | static const char * const table[_MANAGER_OBJECTIVE_MAX] = { |
2081 | [MANAGER_EXIT] = "exit", | |
2082 | [MANAGER_REBOOT] = "reboot", | |
7eb35049 | 2083 | [MANAGER_POWEROFF] = "poweroff", |
af41e508 LP |
2084 | [MANAGER_HALT] = "halt", |
2085 | [MANAGER_KEXEC] = "kexec", | |
7eb35049 LP |
2086 | }; |
2087 | ||
2088 | log_notice("Shutting down."); | |
2089 | ||
2090 | *ret_reexecute = false; | |
2091 | *ret_retval = m->return_value; | |
af41e508 | 2092 | assert_se(*ret_shutdown_verb = table[m->objective]); |
7eb35049 LP |
2093 | *ret_fds = NULL; |
2094 | *ret_switch_root_dir = *ret_switch_root_init = NULL; | |
2095 | ||
2096 | return 0; | |
2097 | } | |
2098 | ||
2099 | default: | |
04499a70 | 2100 | assert_not_reached(); |
7eb35049 LP |
2101 | } |
2102 | } | |
2103 | } | |
2104 | ||
31aef7ff LP |
2105 | static void log_execution_mode(bool *ret_first_boot) { |
2106 | assert(ret_first_boot); | |
2107 | ||
2108 | if (arg_system) { | |
2109 | int v; | |
2110 | ||
e7b18106 | 2111 | log_info("systemd " GIT_VERSION " running in %ssystem mode (%s)", |
91b79ba8 ZJS |
2112 | arg_action == ACTION_TEST ? "test " : "", |
2113 | systemd_features); | |
31aef7ff LP |
2114 | |
2115 | v = detect_virtualization(); | |
2116 | if (v > 0) | |
2117 | log_info("Detected virtualization %s.", virtualization_to_string(v)); | |
2118 | ||
2119 | log_info("Detected architecture %s.", architecture_to_string(uname_architecture())); | |
2120 | ||
2121 | if (in_initrd()) { | |
2122 | *ret_first_boot = false; | |
2123 | log_info("Running in initial RAM disk."); | |
2124 | } else { | |
583cef3b HS |
2125 | int r; |
2126 | _cleanup_free_ char *id_text = NULL; | |
2127 | ||
2128 | /* Let's check whether we are in first boot. We use /etc/machine-id as flag file | |
2129 | * for this: If it is missing or contains the value "uninitialized", this is the | |
2130 | * first boot. In any other case, it is not. This allows container managers and | |
2131 | * installers to provision a couple of files already. If the container manager | |
2132 | * wants to provision the machine ID itself it should pass $container_uuid to PID 1. */ | |
2133 | ||
2134 | r = read_one_line_file("/etc/machine-id", &id_text); | |
2135 | if (r < 0 || streq(id_text, "uninitialized")) { | |
2136 | if (r < 0 && r != -ENOENT) | |
2137 | log_warning_errno(r, "Unexpected error while reading /etc/machine-id, ignoring: %m"); | |
2138 | ||
2139 | *ret_first_boot = true; | |
2140 | log_info("Detected first boot."); | |
2141 | } else { | |
2142 | *ret_first_boot = false; | |
2143 | log_debug("Detected initialized system, this is not the first boot."); | |
2144 | } | |
31aef7ff LP |
2145 | } |
2146 | } else { | |
b9e90f3a | 2147 | if (DEBUG_LOGGING) { |
c2b2df60 | 2148 | _cleanup_free_ char *t = NULL; |
31aef7ff | 2149 | |
b9e90f3a | 2150 | t = uid_to_name(getuid()); |
91b79ba8 ZJS |
2151 | log_debug("systemd " GIT_VERSION " running in %suser mode for user " UID_FMT "/%s. (%s)", |
2152 | arg_action == ACTION_TEST ? " test" : "", | |
2153 | getuid(), strna(t), systemd_features); | |
b9e90f3a | 2154 | } |
31aef7ff LP |
2155 | |
2156 | *ret_first_boot = false; | |
2157 | } | |
2158 | } | |
2159 | ||
5afbaa36 LP |
2160 | static int initialize_runtime( |
2161 | bool skip_setup, | |
3023f2fe | 2162 | bool first_boot, |
5afbaa36 LP |
2163 | struct rlimit *saved_rlimit_nofile, |
2164 | struct rlimit *saved_rlimit_memlock, | |
2165 | const char **ret_error_message) { | |
5afbaa36 LP |
2166 | int r; |
2167 | ||
2168 | assert(ret_error_message); | |
2169 | ||
2170 | /* Sets up various runtime parameters. Many of these initializations are conditionalized: | |
2171 | * | |
2172 | * - Some only apply to --system instances | |
2173 | * - Some only apply to --user instances | |
2174 | * - Some only apply when we first start up, but not when we reexecute | |
2175 | */ | |
2176 | ||
2d776038 LP |
2177 | if (arg_action != ACTION_RUN) |
2178 | return 0; | |
2179 | ||
61fbbac1 | 2180 | update_cpu_affinity(skip_setup); |
b070c7c0 | 2181 | update_numa_policy(skip_setup); |
61fbbac1 | 2182 | |
3c3c6cb9 | 2183 | if (arg_system) { |
5238e957 | 2184 | /* Make sure we leave a core dump without panicking the kernel. */ |
3c3c6cb9 | 2185 | install_crash_handler(); |
5afbaa36 | 2186 | |
3c3c6cb9 | 2187 | if (!skip_setup) { |
143fadf3 | 2188 | r = mount_cgroup_controllers(); |
3c3c6cb9 LP |
2189 | if (r < 0) { |
2190 | *ret_error_message = "Failed to mount cgroup hierarchies"; | |
2191 | return r; | |
2192 | } | |
2193 | ||
2194 | status_welcome(); | |
b6fad306 | 2195 | (void) hostname_setup(true); |
3023f2fe HS |
2196 | /* Force transient machine-id on first boot. */ |
2197 | machine_id_setup(NULL, first_boot, arg_machine_id, NULL); | |
df883de9 | 2198 | (void) loopback_setup(); |
3c3c6cb9 | 2199 | bump_unix_max_dgram_qlen(); |
a8b627aa | 2200 | bump_file_max_and_nr_open(); |
3c3c6cb9 LP |
2201 | test_usr(); |
2202 | write_container_id(); | |
2203 | } | |
8a2c1fbf | 2204 | |
3c3c6cb9 LP |
2205 | if (arg_watchdog_device) { |
2206 | r = watchdog_set_device(arg_watchdog_device); | |
2207 | if (r < 0) | |
2208 | log_warning_errno(r, "Failed to set watchdog device to %s, ignoring: %m", arg_watchdog_device); | |
2209 | } | |
32429805 LP |
2210 | } else { |
2211 | _cleanup_free_ char *p = NULL; | |
2212 | ||
2213 | /* Create the runtime directory and place the inaccessible device nodes there, if we run in | |
2214 | * user mode. In system mode mount_setup() already did that. */ | |
2215 | ||
2216 | r = xdg_user_runtime_dir(&p, "/systemd"); | |
2217 | if (r < 0) { | |
2218 | *ret_error_message = "$XDG_RUNTIME_DIR is not set"; | |
2219 | return log_emergency_errno(r, "Failed to determine $XDG_RUNTIME_DIR path: %m"); | |
2220 | } | |
2221 | ||
e813a74a | 2222 | (void) mkdir_p_label(p, 0755); |
32429805 | 2223 | (void) make_inaccessible_nodes(p, UID_INVALID, GID_INVALID); |
3c3c6cb9 | 2224 | } |
5afbaa36 LP |
2225 | |
2226 | if (arg_timer_slack_nsec != NSEC_INFINITY) | |
2227 | if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0) | |
3a671cd1 | 2228 | log_warning_errno(errno, "Failed to adjust timer slack, ignoring: %m"); |
5afbaa36 LP |
2229 | |
2230 | if (arg_system && !cap_test_all(arg_capability_bounding_set)) { | |
2231 | r = capability_bounding_set_drop_usermode(arg_capability_bounding_set); | |
2232 | if (r < 0) { | |
2233 | *ret_error_message = "Failed to drop capability bounding set of usermode helpers"; | |
2234 | return log_emergency_errno(r, "Failed to drop capability bounding set of usermode helpers: %m"); | |
2235 | } | |
2236 | ||
2237 | r = capability_bounding_set_drop(arg_capability_bounding_set, true); | |
2238 | if (r < 0) { | |
2239 | *ret_error_message = "Failed to drop capability bounding set"; | |
2240 | return log_emergency_errno(r, "Failed to drop capability bounding set: %m"); | |
2241 | } | |
2242 | } | |
2243 | ||
39362f6f JB |
2244 | if (arg_system && arg_no_new_privs) { |
2245 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
2246 | *ret_error_message = "Failed to disable new privileges"; | |
2247 | return log_emergency_errno(errno, "Failed to disable new privileges: %m"); | |
2248 | } | |
2249 | } | |
2250 | ||
5afbaa36 LP |
2251 | if (arg_syscall_archs) { |
2252 | r = enforce_syscall_archs(arg_syscall_archs); | |
2253 | if (r < 0) { | |
2254 | *ret_error_message = "Failed to set syscall architectures"; | |
2255 | return r; | |
2256 | } | |
2257 | } | |
2258 | ||
2259 | if (!arg_system) | |
2260 | /* Become reaper of our children */ | |
2261 | if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) | |
2262 | log_warning_errno(errno, "Failed to make us a subreaper: %m"); | |
2263 | ||
a17c1712 LP |
2264 | /* Bump up RLIMIT_NOFILE for systemd itself */ |
2265 | (void) bump_rlimit_nofile(saved_rlimit_nofile); | |
2266 | (void) bump_rlimit_memlock(saved_rlimit_memlock); | |
5afbaa36 LP |
2267 | |
2268 | return 0; | |
2269 | } | |
2270 | ||
6acca5fc LP |
2271 | static int do_queue_default_job( |
2272 | Manager *m, | |
2273 | const char **ret_error_message) { | |
2274 | ||
2275 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
f1d075dc ZJS |
2276 | const char *unit; |
2277 | Job *job; | |
2278 | Unit *target; | |
6acca5fc LP |
2279 | int r; |
2280 | ||
8755dbad | 2281 | if (arg_default_unit) |
f1d075dc | 2282 | unit = arg_default_unit; |
8755dbad | 2283 | else if (in_initrd()) |
f1d075dc | 2284 | unit = SPECIAL_INITRD_TARGET; |
8755dbad | 2285 | else |
f1d075dc | 2286 | unit = SPECIAL_DEFAULT_TARGET; |
8755dbad | 2287 | |
f1d075dc | 2288 | log_debug("Activating default unit: %s", unit); |
8755dbad | 2289 | |
f1d075dc | 2290 | r = manager_load_startable_unit_or_warn(m, unit, NULL, &target); |
8755dbad ZJS |
2291 | if (r < 0 && in_initrd() && !arg_default_unit) { |
2292 | /* Fall back to default.target, which we used to always use by default. Only do this if no | |
2293 | * explicit configuration was given. */ | |
2294 | ||
2295 | log_info("Falling back to " SPECIAL_DEFAULT_TARGET "."); | |
6acca5fc | 2296 | |
8755dbad ZJS |
2297 | r = manager_load_startable_unit_or_warn(m, SPECIAL_DEFAULT_TARGET, NULL, &target); |
2298 | } | |
4109ede7 | 2299 | if (r < 0) { |
8755dbad | 2300 | log_info("Falling back to " SPECIAL_RESCUE_TARGET "."); |
6acca5fc | 2301 | |
4109ede7 | 2302 | r = manager_load_startable_unit_or_warn(m, SPECIAL_RESCUE_TARGET, NULL, &target); |
6acca5fc | 2303 | if (r < 0) { |
8755dbad ZJS |
2304 | *ret_error_message = r == -ERFKILL ? SPECIAL_RESCUE_TARGET " masked" |
2305 | : "Failed to load " SPECIAL_RESCUE_TARGET; | |
4109ede7 | 2306 | return r; |
6acca5fc LP |
2307 | } |
2308 | } | |
2309 | ||
2310 | assert(target->load_state == UNIT_LOADED); | |
2311 | ||
f1d075dc | 2312 | r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, NULL, &error, &job); |
6acca5fc LP |
2313 | if (r == -EPERM) { |
2314 | log_debug_errno(r, "Default target could not be isolated, starting instead: %s", bus_error_message(&error, r)); | |
2315 | ||
2316 | sd_bus_error_free(&error); | |
2317 | ||
f1d075dc | 2318 | r = manager_add_job(m, JOB_START, target, JOB_REPLACE, NULL, &error, &job); |
6acca5fc LP |
2319 | if (r < 0) { |
2320 | *ret_error_message = "Failed to start default target"; | |
2321 | return log_emergency_errno(r, "Failed to start default target: %s", bus_error_message(&error, r)); | |
2322 | } | |
2323 | ||
2324 | } else if (r < 0) { | |
2325 | *ret_error_message = "Failed to isolate default target"; | |
2326 | return log_emergency_errno(r, "Failed to isolate default target: %s", bus_error_message(&error, r)); | |
c86c31d9 ZJS |
2327 | } else |
2328 | log_info("Queued %s job for default target %s.", | |
2329 | job_type_to_string(job->type), | |
04d232d8 | 2330 | unit_status_string(job->unit, NULL)); |
6acca5fc | 2331 | |
f1d075dc | 2332 | m->default_unit_job_id = job->id; |
6acca5fc LP |
2333 | |
2334 | return 0; | |
2335 | } | |
2336 | ||
a9fd4cd1 FB |
2337 | static void save_rlimits(struct rlimit *saved_rlimit_nofile, |
2338 | struct rlimit *saved_rlimit_memlock) { | |
2339 | ||
2340 | assert(saved_rlimit_nofile); | |
2341 | assert(saved_rlimit_memlock); | |
2342 | ||
2343 | if (getrlimit(RLIMIT_NOFILE, saved_rlimit_nofile) < 0) | |
2344 | log_warning_errno(errno, "Reading RLIMIT_NOFILE failed, ignoring: %m"); | |
2345 | ||
2346 | if (getrlimit(RLIMIT_MEMLOCK, saved_rlimit_memlock) < 0) | |
2347 | log_warning_errno(errno, "Reading RLIMIT_MEMLOCK failed, ignoring: %m"); | |
2348 | } | |
2349 | ||
2350 | static void fallback_rlimit_nofile(const struct rlimit *saved_rlimit_nofile) { | |
2351 | struct rlimit *rl; | |
2352 | ||
2353 | if (arg_default_rlimit[RLIMIT_NOFILE]) | |
2354 | return; | |
2355 | ||
2356 | /* Make sure forked processes get limits based on the original kernel setting */ | |
2357 | ||
2358 | rl = newdup(struct rlimit, saved_rlimit_nofile, 1); | |
2359 | if (!rl) { | |
2360 | log_oom(); | |
2361 | return; | |
2362 | } | |
2363 | ||
2364 | /* Bump the hard limit for system services to a substantially higher value. The default | |
2365 | * hard limit current kernels set is pretty low (4K), mostly for historical | |
2366 | * reasons. According to kernel developers, the fd handling in recent kernels has been | |
2367 | * optimized substantially enough, so that we can bump the limit now, without paying too | |
2368 | * high a price in memory or performance. Note however that we only bump the hard limit, | |
2369 | * not the soft limit. That's because select() works the way it works, and chokes on fds | |
2370 | * >= 1024. If we'd bump the soft limit globally, it might accidentally happen to | |
2371 | * unexpecting programs that they get fds higher than what they can process using | |
2372 | * select(). By only bumping the hard limit but leaving the low limit as it is we avoid | |
2373 | * this pitfall: programs that are written by folks aware of the select() problem in mind | |
2374 | * (and thus use poll()/epoll instead of select(), the way everybody should) can | |
2375 | * explicitly opt into high fds by bumping their soft limit beyond 1024, to the hard limit | |
2376 | * we pass. */ | |
2377 | if (arg_system) { | |
2378 | int nr; | |
2379 | ||
2380 | /* Get the underlying absolute limit the kernel enforces */ | |
2381 | nr = read_nr_open(); | |
2382 | ||
2383 | rl->rlim_max = MIN((rlim_t) nr, MAX(rl->rlim_max, (rlim_t) HIGH_RLIMIT_NOFILE)); | |
2384 | } | |
2385 | ||
2386 | /* If for some reason we were invoked with a soft limit above 1024 (which should never | |
2387 | * happen!, but who knows what we get passed in from pam_limit when invoked as --user | |
2388 | * instance), then lower what we pass on to not confuse our children */ | |
2389 | rl->rlim_cur = MIN(rl->rlim_cur, (rlim_t) FD_SETSIZE); | |
2390 | ||
2391 | arg_default_rlimit[RLIMIT_NOFILE] = rl; | |
2392 | } | |
2393 | ||
2394 | static void fallback_rlimit_memlock(const struct rlimit *saved_rlimit_memlock) { | |
2395 | struct rlimit *rl; | |
2396 | ||
2397 | /* Pass the original value down to invoked processes */ | |
2398 | ||
2399 | if (arg_default_rlimit[RLIMIT_MEMLOCK]) | |
2400 | return; | |
2401 | ||
2402 | rl = newdup(struct rlimit, saved_rlimit_memlock, 1); | |
2403 | if (!rl) { | |
2404 | log_oom(); | |
2405 | return; | |
2406 | } | |
2407 | ||
2408 | arg_default_rlimit[RLIMIT_MEMLOCK] = rl; | |
2409 | } | |
2410 | ||
d55ed7de ZJS |
2411 | static void setenv_manager_environment(void) { |
2412 | char **p; | |
2413 | int r; | |
2414 | ||
2415 | STRV_FOREACH(p, arg_manager_environment) { | |
2416 | log_debug("Setting '%s' in our own environment.", *p); | |
2417 | ||
2418 | r = putenv_dup(*p, true); | |
2419 | if (r < 0) | |
2420 | log_warning_errno(errno, "Failed to setenv \"%s\", ignoring: %m", *p); | |
2421 | } | |
2422 | } | |
2423 | ||
fb39af4c ZJS |
2424 | static void reset_arguments(void) { |
2425 | /* Frees/resets arg_* variables, with a few exceptions commented below. */ | |
970777b5 LP |
2426 | |
2427 | arg_default_unit = mfree(arg_default_unit); | |
fb39af4c ZJS |
2428 | |
2429 | /* arg_system — ignore */ | |
2430 | ||
2431 | arg_dump_core = true; | |
2432 | arg_crash_chvt = -1; | |
2433 | arg_crash_shell = false; | |
2434 | arg_crash_reboot = false; | |
970777b5 | 2435 | arg_confirm_spawn = mfree(arg_confirm_spawn); |
fb39af4c | 2436 | arg_show_status = _SHOW_STATUS_INVALID; |
36cf4507 | 2437 | arg_status_unit_format = STATUS_UNIT_FORMAT_DEFAULT; |
fb39af4c ZJS |
2438 | arg_switched_root = false; |
2439 | arg_pager_flags = 0; | |
2440 | arg_service_watchdogs = true; | |
2441 | arg_default_std_output = EXEC_OUTPUT_JOURNAL; | |
2442 | arg_default_std_error = EXEC_OUTPUT_INHERIT; | |
2443 | arg_default_restart_usec = DEFAULT_RESTART_USEC; | |
2444 | arg_default_timeout_start_usec = DEFAULT_TIMEOUT_USEC; | |
2445 | arg_default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC; | |
2446 | arg_default_timeout_abort_usec = DEFAULT_TIMEOUT_USEC; | |
2447 | arg_default_timeout_abort_set = false; | |
2448 | arg_default_start_limit_interval = DEFAULT_START_LIMIT_INTERVAL; | |
2449 | arg_default_start_limit_burst = DEFAULT_START_LIMIT_BURST; | |
2450 | arg_runtime_watchdog = 0; | |
65224c1d | 2451 | arg_reboot_watchdog = 10 * USEC_PER_MINUTE; |
acafd7d8 | 2452 | arg_kexec_watchdog = 0; |
fb39af4c ZJS |
2453 | arg_early_core_pattern = NULL; |
2454 | arg_watchdog_device = NULL; | |
2455 | ||
970777b5 | 2456 | arg_default_environment = strv_free(arg_default_environment); |
d55ed7de | 2457 | arg_manager_environment = strv_free(arg_manager_environment); |
fb39af4c ZJS |
2458 | rlimit_free_all(arg_default_rlimit); |
2459 | ||
2460 | arg_capability_bounding_set = CAP_ALL; | |
2461 | arg_no_new_privs = false; | |
2462 | arg_timer_slack_nsec = NSEC_INFINITY; | |
2463 | arg_default_timer_accuracy_usec = 1 * USEC_PER_MINUTE; | |
2464 | ||
970777b5 | 2465 | arg_syscall_archs = set_free(arg_syscall_archs); |
61fbbac1 | 2466 | |
fb39af4c ZJS |
2467 | /* arg_serialization — ignore */ |
2468 | ||
2469 | arg_default_cpu_accounting = -1; | |
2470 | arg_default_io_accounting = false; | |
2471 | arg_default_ip_accounting = false; | |
2472 | arg_default_blockio_accounting = false; | |
2473 | arg_default_memory_accounting = MEMORY_ACCOUNTING_DEFAULT; | |
2474 | arg_default_tasks_accounting = true; | |
3a0f06c4 | 2475 | arg_default_tasks_max = DEFAULT_TASKS_MAX; |
fb39af4c ZJS |
2476 | arg_machine_id = (sd_id128_t) {}; |
2477 | arg_cad_burst_action = EMERGENCY_ACTION_REBOOT_FORCE; | |
2478 | arg_default_oom_policy = OOM_STOP; | |
2479 | ||
61fbbac1 | 2480 | cpu_set_reset(&arg_cpu_affinity); |
b070c7c0 | 2481 | numa_policy_reset(&arg_numa_policy); |
d247f232 LP |
2482 | |
2483 | arg_random_seed = mfree(arg_random_seed); | |
2484 | arg_random_seed_size = 0; | |
33d943d1 | 2485 | arg_clock_usec = 0; |
d4a402e4 LP |
2486 | |
2487 | arg_default_oom_score_adjust_set = false; | |
2488 | } | |
2489 | ||
2490 | static void determine_default_oom_score_adjust(void) { | |
2491 | int r, a, b; | |
2492 | ||
2493 | /* Run our services at slightly higher OOM score than ourselves. But let's be conservative here, and | |
2494 | * do this only if we don't run as root (i.e. only if we are run in user mode, for an unprivileged | |
2495 | * user). */ | |
2496 | ||
2497 | if (arg_default_oom_score_adjust_set) | |
2498 | return; | |
2499 | ||
2500 | if (getuid() == 0) | |
2501 | return; | |
2502 | ||
2503 | r = get_oom_score_adjust(&a); | |
2504 | if (r < 0) | |
2505 | return (void) log_warning_errno(r, "Failed to determine current OOM score adjustment value, ignoring: %m"); | |
2506 | ||
2507 | assert_cc(100 <= OOM_SCORE_ADJ_MAX); | |
2508 | b = a >= OOM_SCORE_ADJ_MAX - 100 ? OOM_SCORE_ADJ_MAX : a + 100; | |
2509 | ||
2510 | if (a == b) | |
2511 | return; | |
2512 | ||
2513 | arg_default_oom_score_adjust = b; | |
2514 | arg_default_oom_score_adjust_set = true; | |
970777b5 LP |
2515 | } |
2516 | ||
a9fd4cd1 FB |
2517 | static int parse_configuration(const struct rlimit *saved_rlimit_nofile, |
2518 | const struct rlimit *saved_rlimit_memlock) { | |
97d1fb94 LP |
2519 | int r; |
2520 | ||
a9fd4cd1 FB |
2521 | assert(saved_rlimit_nofile); |
2522 | assert(saved_rlimit_memlock); | |
2523 | ||
fb39af4c ZJS |
2524 | /* Assign configuration defaults */ |
2525 | reset_arguments(); | |
2526 | ||
97d1fb94 | 2527 | r = parse_config_file(); |
470a5e6d ZJS |
2528 | if (r < 0) |
2529 | log_warning_errno(r, "Failed to parse config file, ignoring: %m"); | |
97d1fb94 LP |
2530 | |
2531 | if (arg_system) { | |
2532 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); | |
2533 | if (r < 0) | |
2534 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); | |
2535 | } | |
2536 | ||
a9fd4cd1 FB |
2537 | /* Initialize some default rlimits for services if they haven't been configured */ |
2538 | fallback_rlimit_nofile(saved_rlimit_nofile); | |
2539 | fallback_rlimit_memlock(saved_rlimit_memlock); | |
2540 | ||
97d1fb94 LP |
2541 | /* Note that this also parses bits from the kernel command line, including "debug". */ |
2542 | log_parse_environment(); | |
2543 | ||
db33214b | 2544 | /* Initialize the show status setting if it hasn't been set explicitly yet */ |
7a293242 | 2545 | if (arg_show_status == _SHOW_STATUS_INVALID) |
db33214b LP |
2546 | arg_show_status = SHOW_STATUS_YES; |
2547 | ||
d4a402e4 LP |
2548 | /* Slightly raise the OOM score for our services if we are running for unprivileged users. */ |
2549 | determine_default_oom_score_adjust(); | |
2550 | ||
d55ed7de ZJS |
2551 | /* Push variables into the manager environment block */ |
2552 | setenv_manager_environment(); | |
2553 | ||
a4303b40 DDM |
2554 | /* Parse log environment variables again to take into account any new environment variables. */ |
2555 | log_parse_environment(); | |
2556 | ||
97d1fb94 LP |
2557 | return 0; |
2558 | } | |
2559 | ||
b0d7c989 LP |
2560 | static int safety_checks(void) { |
2561 | ||
febf46a4 | 2562 | if (getpid_cached() == 1 && |
baaa35ad ZJS |
2563 | arg_action != ACTION_RUN) |
2564 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
2565 | "Unsupported execution mode while PID 1."); | |
febf46a4 LP |
2566 | |
2567 | if (getpid_cached() == 1 && | |
baaa35ad ZJS |
2568 | !arg_system) |
2569 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
2570 | "Can't run --user mode as PID 1."); | |
febf46a4 LP |
2571 | |
2572 | if (arg_action == ACTION_RUN && | |
2573 | arg_system && | |
baaa35ad ZJS |
2574 | getpid_cached() != 1) |
2575 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
2576 | "Can't run system mode unless PID 1."); | |
febf46a4 | 2577 | |
b0d7c989 | 2578 | if (arg_action == ACTION_TEST && |
baaa35ad ZJS |
2579 | geteuid() == 0) |
2580 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
2581 | "Don't run test mode as root."); | |
b0d7c989 LP |
2582 | |
2583 | if (!arg_system && | |
2584 | arg_action == ACTION_RUN && | |
baaa35ad ZJS |
2585 | sd_booted() <= 0) |
2586 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
2587 | "Trying to run as user instance, but the system has not been booted with systemd."); | |
b0d7c989 LP |
2588 | |
2589 | if (!arg_system && | |
2590 | arg_action == ACTION_RUN && | |
baaa35ad ZJS |
2591 | !getenv("XDG_RUNTIME_DIR")) |
2592 | return log_error_errno(SYNTHETIC_ERRNO(EUNATCH), | |
2593 | "Trying to run as user instance, but $XDG_RUNTIME_DIR is not set."); | |
b0d7c989 LP |
2594 | |
2595 | if (arg_system && | |
2596 | arg_action == ACTION_RUN && | |
baaa35ad ZJS |
2597 | running_in_chroot() > 0) |
2598 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
2599 | "Cannot be run in a chroot() environment."); | |
b0d7c989 LP |
2600 | |
2601 | return 0; | |
2602 | } | |
2603 | ||
74da609f LP |
2604 | static int initialize_security( |
2605 | bool *loaded_policy, | |
2606 | dual_timestamp *security_start_timestamp, | |
2607 | dual_timestamp *security_finish_timestamp, | |
2608 | const char **ret_error_message) { | |
2609 | ||
2610 | int r; | |
2611 | ||
2612 | assert(loaded_policy); | |
2613 | assert(security_start_timestamp); | |
2614 | assert(security_finish_timestamp); | |
2615 | assert(ret_error_message); | |
2616 | ||
2617 | dual_timestamp_get(security_start_timestamp); | |
2618 | ||
97149f40 | 2619 | r = mac_selinux_setup(loaded_policy); |
74da609f LP |
2620 | if (r < 0) { |
2621 | *ret_error_message = "Failed to load SELinux policy"; | |
2622 | return r; | |
2623 | } | |
2624 | ||
2625 | r = mac_smack_setup(loaded_policy); | |
2626 | if (r < 0) { | |
2627 | *ret_error_message = "Failed to load SMACK policy"; | |
2628 | return r; | |
2629 | } | |
2630 | ||
2ffadd3c Y |
2631 | r = mac_apparmor_setup(); |
2632 | if (r < 0) { | |
2633 | *ret_error_message = "Failed to load AppArmor policy"; | |
2634 | return r; | |
2635 | } | |
2636 | ||
74da609f LP |
2637 | r = ima_setup(); |
2638 | if (r < 0) { | |
2639 | *ret_error_message = "Failed to load IMA policy"; | |
2640 | return r; | |
2641 | } | |
2642 | ||
2643 | dual_timestamp_get(security_finish_timestamp); | |
2644 | return 0; | |
2645 | } | |
2646 | ||
efeb853f LP |
2647 | static int collect_fds(FDSet **ret_fds, const char **ret_error_message) { |
2648 | int r; | |
2649 | ||
2650 | assert(ret_fds); | |
2651 | assert(ret_error_message); | |
2652 | ||
2653 | r = fdset_new_fill(ret_fds); | |
2654 | if (r < 0) { | |
2655 | *ret_error_message = "Failed to allocate fd set"; | |
2656 | return log_emergency_errno(r, "Failed to allocate fd set: %m"); | |
2657 | } | |
2658 | ||
2659 | fdset_cloexec(*ret_fds, true); | |
2660 | ||
2661 | if (arg_serialization) | |
2662 | assert_se(fdset_remove(*ret_fds, fileno(arg_serialization)) >= 0); | |
2663 | ||
2664 | return 0; | |
2665 | } | |
2666 | ||
2e51b31c LP |
2667 | static void setup_console_terminal(bool skip_setup) { |
2668 | ||
2669 | if (!arg_system) | |
2670 | return; | |
2671 | ||
2672 | /* Become a session leader if we aren't one yet. */ | |
2673 | (void) setsid(); | |
2674 | ||
ff3a7019 ZJS |
2675 | /* If we are init, we connect stdin/stdout/stderr to /dev/null and make sure we don't have a |
2676 | * controlling tty. */ | |
2e51b31c LP |
2677 | (void) release_terminal(); |
2678 | ||
2679 | /* Reset the console, but only if this is really init and we are freshly booted */ | |
2680 | if (getpid_cached() == 1 && !skip_setup) | |
2681 | (void) console_setup(); | |
2682 | } | |
2683 | ||
aa40ff07 LP |
2684 | static bool early_skip_setup_check(int argc, char *argv[]) { |
2685 | bool found_deserialize = false; | |
aa40ff07 | 2686 | |
ff3a7019 ZJS |
2687 | /* Determine if this is a reexecution or normal bootup. We do the full command line parsing much |
2688 | * later, so let's just have a quick peek here. Note that if we have switched root, do all the | |
2689 | * special setup things anyway, even if in that case we also do deserialization. */ | |
aa40ff07 | 2690 | |
431733b8 | 2691 | for (int i = 1; i < argc; i++) |
aa40ff07 LP |
2692 | if (streq(argv[i], "--switched-root")) |
2693 | return false; /* If we switched root, don't skip the setup. */ | |
2694 | else if (streq(argv[i], "--deserialize")) | |
2695 | found_deserialize = true; | |
aa40ff07 LP |
2696 | |
2697 | return found_deserialize; /* When we are deserializing, then we are reexecuting, hence avoid the extensive setup */ | |
2698 | } | |
2699 | ||
0e06a031 LP |
2700 | static int save_env(void) { |
2701 | char **l; | |
2702 | ||
2703 | l = strv_copy(environ); | |
2704 | if (!l) | |
2705 | return -ENOMEM; | |
2706 | ||
2707 | strv_free_and_replace(saved_env, l); | |
2708 | return 0; | |
2709 | } | |
2710 | ||
60918275 | 2711 | int main(int argc, char *argv[]) { |
625e8690 LP |
2712 | |
2713 | dual_timestamp initrd_timestamp = DUAL_TIMESTAMP_NULL, userspace_timestamp = DUAL_TIMESTAMP_NULL, kernel_timestamp = DUAL_TIMESTAMP_NULL, | |
2714 | security_start_timestamp = DUAL_TIMESTAMP_NULL, security_finish_timestamp = DUAL_TIMESTAMP_NULL; | |
ddfa8b0b LP |
2715 | struct rlimit saved_rlimit_nofile = RLIMIT_MAKE_CONST(0), |
2716 | saved_rlimit_memlock = RLIMIT_MAKE_CONST(RLIM_INFINITY); /* The original rlimits we passed | |
2717 | * in. Note we use different values | |
2718 | * for the two that indicate whether | |
2719 | * these fields are initialized! */ | |
625e8690 LP |
2720 | bool skip_setup, loaded_policy = false, queue_default_job = false, first_boot = false, reexecute = false; |
2721 | char *switch_root_dir = NULL, *switch_root_init = NULL; | |
9d76d730 | 2722 | usec_t before_startup, after_startup; |
625e8690 | 2723 | static char systemd[] = "systemd"; |
625e8690 LP |
2724 | const char *shutdown_verb = NULL, *error_message = NULL; |
2725 | int r, retval = EXIT_FAILURE; | |
2726 | Manager *m = NULL; | |
a16e1123 | 2727 | FDSet *fds = NULL; |
27b14a22 | 2728 | |
d72a8f10 | 2729 | /* SysV compatibility: redirect init → telinit */ |
6808a0bc | 2730 | redirect_telinit(argc, argv); |
2cb1a60d | 2731 | |
d72a8f10 | 2732 | /* Take timestamps early on */ |
c3a170f3 HH |
2733 | dual_timestamp_from_monotonic(&kernel_timestamp, 0); |
2734 | dual_timestamp_get(&userspace_timestamp); | |
2735 | ||
d72a8f10 | 2736 | /* Figure out whether we need to do initialize the system, or if we already did that because we are |
ff3a7019 | 2737 | * reexecuting. */ |
aa40ff07 | 2738 | skip_setup = early_skip_setup_check(argc, argv); |
d03bc1b8 | 2739 | |
ff3a7019 ZJS |
2740 | /* If we get started via the /sbin/init symlink then we are called 'init'. After a subsequent |
2741 | * reexecution we are then called 'systemd'. That is confusing, hence let's call us systemd | |
2742 | * right-away. */ | |
f3b6a3ed | 2743 | program_invocation_short_name = systemd; |
eee8b7ab | 2744 | (void) prctl(PR_SET_NAME, systemd); |
5d6b1584 | 2745 | |
d72a8f10 | 2746 | /* Save the original command line */ |
36fea155 | 2747 | save_argc_argv(argc, argv); |
f3b6a3ed | 2748 | |
0e06a031 LP |
2749 | /* Save the original environment as we might need to restore it if we're requested to execute another |
2750 | * system manager later. */ | |
2751 | r = save_env(); | |
2752 | if (r < 0) { | |
2753 | error_message = "Failed to copy environment block"; | |
2754 | goto finish; | |
2755 | } | |
a5cede8c | 2756 | |
6fdb8de4 | 2757 | /* Make sure that if the user says "syslog" we actually log to the journal. */ |
c1dc6153 | 2758 | log_set_upgrade_syslog_to_journal(true); |
bbe63281 | 2759 | |
df0ff127 | 2760 | if (getpid_cached() == 1) { |
b5752d23 LP |
2761 | /* When we run as PID 1 force system mode */ |
2762 | arg_system = true; | |
2763 | ||
48a601fe | 2764 | /* Disable the umask logic */ |
90dc8c2e MG |
2765 | umask(0); |
2766 | ||
ff3a7019 ZJS |
2767 | /* Make sure that at least initially we do not ever log to journald/syslogd, because it might |
2768 | * not be activated yet (even though the log socket for it exists). */ | |
d075092f LP |
2769 | log_set_prohibit_ipc(true); |
2770 | ||
ff3a7019 ZJS |
2771 | /* Always reopen /dev/console when running as PID 1 or one of its pre-execve() children. This |
2772 | * is important so that we never end up logging to any foreign stderr, for example if we have | |
2773 | * to log in a child process right before execve()'ing the actual binary, at a point in time | |
2774 | * where socket activation stderr/stdout area already set up. */ | |
48a601fe | 2775 | log_set_always_reopen_console(true); |
48a601fe | 2776 | |
92890452 | 2777 | if (detect_container() <= 0) { |
4f8d551f | 2778 | |
92890452 | 2779 | /* Running outside of a container as PID 1 */ |
92890452 LP |
2780 | log_set_target(LOG_TARGET_KMSG); |
2781 | log_open(); | |
a866073d | 2782 | |
92890452 LP |
2783 | if (in_initrd()) |
2784 | initrd_timestamp = userspace_timestamp; | |
c3ba6250 | 2785 | |
92890452 LP |
2786 | if (!skip_setup) { |
2787 | r = mount_setup_early(); | |
2788 | if (r < 0) { | |
2789 | error_message = "Failed to mount early API filesystems"; | |
2790 | goto finish; | |
2791 | } | |
2792 | ||
0a2eef1e LP |
2793 | /* Let's open the log backend a second time, in case the first time didn't |
2794 | * work. Quite possibly we have mounted /dev just now, so /dev/kmsg became | |
2795 | * available, and it previously wasn't. */ | |
2796 | log_open(); | |
2797 | ||
6123dfaa ZJS |
2798 | disable_printk_ratelimit(); |
2799 | ||
92890452 LP |
2800 | r = initialize_security( |
2801 | &loaded_policy, | |
2802 | &security_start_timestamp, | |
2803 | &security_finish_timestamp, | |
2804 | &error_message); | |
2805 | if (r < 0) | |
2806 | goto finish; | |
d723cd65 | 2807 | } |
eee8b7ab | 2808 | |
92890452 | 2809 | if (mac_selinux_init() < 0) { |
a9ba0e32 | 2810 | error_message = "Failed to initialize SELinux support"; |
96694e99 | 2811 | goto finish; |
92890452 | 2812 | } |
0b3325e7 | 2813 | |
92890452 LP |
2814 | if (!skip_setup) |
2815 | initialize_clock(); | |
2816 | ||
ff3a7019 ZJS |
2817 | /* Set the default for later on, but don't actually open the logs like this for |
2818 | * now. Note that if we are transitioning from the initrd there might still be | |
2819 | * journal fd open, and we shouldn't attempt opening that before we parsed | |
2820 | * /proc/cmdline which might redirect output elsewhere. */ | |
92890452 LP |
2821 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); |
2822 | ||
2823 | } else { | |
2824 | /* Running inside a container, as PID 1 */ | |
92890452 LP |
2825 | log_set_target(LOG_TARGET_CONSOLE); |
2826 | log_open(); | |
2827 | ||
2828 | /* For later on, see above... */ | |
2829 | log_set_target(LOG_TARGET_JOURNAL); | |
2830 | ||
45250e66 | 2831 | /* clear the kernel timestamp, because we are in a container */ |
92890452 | 2832 | kernel_timestamp = DUAL_TIMESTAMP_NULL; |
cb6531be | 2833 | } |
7948c4df | 2834 | |
92890452 | 2835 | initialize_coredump(skip_setup); |
a866073d | 2836 | |
92890452 LP |
2837 | r = fixup_environment(); |
2838 | if (r < 0) { | |
2839 | log_emergency_errno(r, "Failed to fix up PID 1 environment: %m"); | |
2840 | error_message = "Failed to fix up PID1 environment"; | |
2841 | goto finish; | |
2842 | } | |
a866073d | 2843 | |
ff3a7019 ZJS |
2844 | /* Try to figure out if we can use colors with the console. No need to do that for user |
2845 | * instances since they never log into the console. */ | |
3a18b604 | 2846 | log_show_color(colors_enabled()); |
92890452 | 2847 | |
c76cf844 AK |
2848 | r = make_null_stdio(); |
2849 | if (r < 0) | |
92890452 | 2850 | log_warning_errno(r, "Failed to redirect standard streams to /dev/null, ignoring: %m"); |
f84f9974 | 2851 | |
a132bef0 | 2852 | /* Load the kernel modules early. */ |
2e75e2a8 DM |
2853 | if (!skip_setup) |
2854 | kmod_setup(); | |
2e75e2a8 | 2855 | |
3196e423 | 2856 | /* Mount /proc, /sys and friends, so that /proc/cmdline and /proc/$PID/fd is available. */ |
f74349d8 | 2857 | r = mount_setup(loaded_policy, skip_setup); |
cb6531be ZJS |
2858 | if (r < 0) { |
2859 | error_message = "Failed to mount API filesystems"; | |
8efe3c01 | 2860 | goto finish; |
cb6531be | 2861 | } |
c18ecf03 LP |
2862 | |
2863 | /* The efivarfs is now mounted, let's read the random seed off it */ | |
2864 | (void) efi_take_random_seed(); | |
209b2592 FB |
2865 | |
2866 | /* Cache command-line options passed from EFI variables */ | |
2867 | if (!skip_setup) | |
2868 | (void) cache_efi_options_variable(); | |
3196e423 LP |
2869 | } else { |
2870 | /* Running as user instance */ | |
2871 | arg_system = false; | |
2872 | log_set_target(LOG_TARGET_AUTO); | |
2873 | log_open(); | |
2874 | ||
2875 | /* clear the kernel timestamp, because we are not PID 1 */ | |
2876 | kernel_timestamp = DUAL_TIMESTAMP_NULL; | |
2877 | ||
2878 | if (mac_selinux_init() < 0) { | |
2879 | error_message = "Failed to initialize SELinux support"; | |
2880 | goto finish; | |
2881 | } | |
0c85a4f3 | 2882 | } |
4ade7963 | 2883 | |
a9fd4cd1 FB |
2884 | /* Save the original RLIMIT_NOFILE/RLIMIT_MEMLOCK so that we can reset it later when |
2885 | * transitioning from the initrd to the main systemd or suchlike. */ | |
2886 | save_rlimits(&saved_rlimit_nofile, &saved_rlimit_memlock); | |
2887 | ||
4ade7963 | 2888 | /* Reset all signal handlers. */ |
ce30c8dc | 2889 | (void) reset_all_signal_handlers(); |
9c274488 | 2890 | (void) ignore_signals(SIGNALS_IGNORE); |
078e4539 | 2891 | |
ffe5c01e FB |
2892 | (void) parse_configuration(&saved_rlimit_nofile, &saved_rlimit_memlock); |
2893 | ||
2894 | r = parse_argv(argc, argv); | |
2895 | if (r < 0) { | |
2896 | error_message = "Failed to parse commandline arguments"; | |
f170852a | 2897 | goto finish; |
ffe5c01e | 2898 | } |
10c961b9 | 2899 | |
b0d7c989 LP |
2900 | r = safety_checks(); |
2901 | if (r < 0) | |
fe783b03 | 2902 | goto finish; |
fe783b03 | 2903 | |
5c08257b | 2904 | if (IN_SET(arg_action, ACTION_TEST, ACTION_HELP, ACTION_DUMP_CONFIGURATION_ITEMS, ACTION_DUMP_BUS_PROPERTIES, ACTION_BUS_INTROSPECT)) |
384c2c32 | 2905 | pager_open(arg_pager_flags); |
b0d7c989 LP |
2906 | |
2907 | if (arg_action != ACTION_RUN) | |
74e7579c | 2908 | skip_setup = true; |
b87c2aa6 | 2909 | |
fa0f4d8a | 2910 | if (arg_action == ACTION_HELP) { |
37ec0fdd | 2911 | retval = help() < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
f170852a | 2912 | goto finish; |
9ba0bc4e ZJS |
2913 | } else if (arg_action == ACTION_VERSION) { |
2914 | retval = version(); | |
2915 | goto finish; | |
fa0f4d8a | 2916 | } else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) { |
e537352b | 2917 | unit_dump_config_items(stdout); |
22f4096c | 2918 | retval = EXIT_SUCCESS; |
e537352b | 2919 | goto finish; |
bbc1acab YW |
2920 | } else if (arg_action == ACTION_DUMP_BUS_PROPERTIES) { |
2921 | dump_bus_properties(stdout); | |
2922 | retval = EXIT_SUCCESS; | |
2923 | goto finish; | |
5c08257b ZJS |
2924 | } else if (arg_action == ACTION_BUS_INTROSPECT) { |
2925 | r = bus_manager_introspect_implementations(stdout, arg_bus_introspect); | |
2926 | retval = r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
2927 | goto finish; | |
f170852a LP |
2928 | } |
2929 | ||
4c701096 | 2930 | assert_se(IN_SET(arg_action, ACTION_RUN, ACTION_TEST)); |
f170852a | 2931 | |
5a2e0c62 LP |
2932 | /* Move out of the way, so that we won't block unmounts */ |
2933 | assert_se(chdir("/") == 0); | |
2934 | ||
dea374e8 | 2935 | if (arg_action == ACTION_RUN) { |
d247f232 LP |
2936 | if (!skip_setup) { |
2937 | /* Apply the systemd.clock_usec= kernel command line switch */ | |
45250e66 | 2938 | apply_clock_update(); |
a70c72a0 | 2939 | |
d247f232 LP |
2940 | /* Apply random seed from kernel command line */ |
2941 | cmdline_take_random_seed(); | |
2942 | } | |
2943 | ||
c6885f5f FB |
2944 | /* A core pattern might have been specified via the cmdline. */ |
2945 | initialize_core_pattern(skip_setup); | |
2946 | ||
efeb853f | 2947 | /* Close logging fds, in order not to confuse collecting passed fds and terminal logic below */ |
a70c72a0 LP |
2948 | log_close(); |
2949 | ||
2950 | /* Remember open file descriptors for later deserialization */ | |
efeb853f LP |
2951 | r = collect_fds(&fds, &error_message); |
2952 | if (r < 0) | |
dea374e8 | 2953 | goto finish; |
a16e1123 | 2954 | |
2e51b31c LP |
2955 | /* Give up any control of the console, but make sure its initialized. */ |
2956 | setup_console_terminal(skip_setup); | |
56d96fc0 | 2957 | |
a70c72a0 LP |
2958 | /* Open the logging devices, if possible and necessary */ |
2959 | log_open(); | |
56d96fc0 | 2960 | } |
4ade7963 | 2961 | |
31aef7ff | 2962 | log_execution_mode(&first_boot); |
a5dab5ce | 2963 | |
2d776038 | 2964 | r = initialize_runtime(skip_setup, |
3023f2fe | 2965 | first_boot, |
2d776038 LP |
2966 | &saved_rlimit_nofile, |
2967 | &saved_rlimit_memlock, | |
2968 | &error_message); | |
2969 | if (r < 0) | |
2970 | goto finish; | |
4096d6f5 | 2971 | |
e0a3da1f ZJS |
2972 | r = manager_new(arg_system ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, |
2973 | arg_action == ACTION_TEST ? MANAGER_TEST_FULL : 0, | |
2974 | &m); | |
e96d6be7 | 2975 | if (r < 0) { |
da927ba9 | 2976 | log_emergency_errno(r, "Failed to allocate manager object: %m"); |
cb6531be | 2977 | error_message = "Failed to allocate manager object"; |
60918275 LP |
2978 | goto finish; |
2979 | } | |
2980 | ||
9f9f0342 LP |
2981 | m->timestamps[MANAGER_TIMESTAMP_KERNEL] = kernel_timestamp; |
2982 | m->timestamps[MANAGER_TIMESTAMP_INITRD] = initrd_timestamp; | |
2983 | m->timestamps[MANAGER_TIMESTAMP_USERSPACE] = userspace_timestamp; | |
d4ee7bd8 YW |
2984 | m->timestamps[manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_SECURITY_START)] = security_start_timestamp; |
2985 | m->timestamps[manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_SECURITY_FINISH)] = security_finish_timestamp; | |
9e58ff9c | 2986 | |
85cb4151 | 2987 | set_manager_defaults(m); |
7b46fc6a | 2988 | set_manager_settings(m); |
fd130612 | 2989 | manager_set_first_boot(m, first_boot); |
27d340c7 | 2990 | |
bf4df7c3 | 2991 | /* Remember whether we should queue the default job */ |
d3b1c508 | 2992 | queue_default_job = !arg_serialization || arg_switched_root; |
bf4df7c3 | 2993 | |
9d76d730 LP |
2994 | before_startup = now(CLOCK_MONOTONIC); |
2995 | ||
2a7cf953 | 2996 | r = manager_startup(m, arg_serialization, fds, /* root= */ NULL); |
58f88d92 | 2997 | if (r < 0) { |
cefb3eda | 2998 | error_message = "Failed to start up manager"; |
58f88d92 ZJS |
2999 | goto finish; |
3000 | } | |
a16e1123 | 3001 | |
6acca5fc | 3002 | /* This will close all file descriptors that were opened, but not claimed by any unit. */ |
2feceb5e | 3003 | fds = fdset_free(fds); |
74ca738f | 3004 | arg_serialization = safe_fclose(arg_serialization); |
bf4df7c3 LP |
3005 | |
3006 | if (queue_default_job) { | |
6acca5fc | 3007 | r = do_queue_default_job(m, &error_message); |
718db961 | 3008 | if (r < 0) |
37d88da7 | 3009 | goto finish; |
6acca5fc | 3010 | } |
ab17a050 | 3011 | |
6acca5fc | 3012 | after_startup = now(CLOCK_MONOTONIC); |
60918275 | 3013 | |
6acca5fc LP |
3014 | log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG, |
3015 | "Loaded units and determined initial transaction in %s.", | |
5291f26d | 3016 | FORMAT_TIMESPAN(after_startup - before_startup, 100 * USEC_PER_MSEC)); |
07672f49 | 3017 | |
6acca5fc | 3018 | if (arg_action == ACTION_TEST) { |
2a341bb9 | 3019 | manager_test_summary(m); |
6acca5fc LP |
3020 | retval = EXIT_SUCCESS; |
3021 | goto finish; | |
e965d56d | 3022 | } |
d46de8a1 | 3023 | |
3046b6db | 3024 | (void) invoke_main_loop(m, |
a9fd4cd1 FB |
3025 | &saved_rlimit_nofile, |
3026 | &saved_rlimit_memlock, | |
3046b6db LP |
3027 | &reexecute, |
3028 | &retval, | |
3029 | &shutdown_verb, | |
3030 | &fds, | |
3031 | &switch_root_dir, | |
3032 | &switch_root_init, | |
3033 | &error_message); | |
f170852a | 3034 | |
60918275 | 3035 | finish: |
b87c2aa6 ZJS |
3036 | pager_close(); |
3037 | ||
92890452 | 3038 | if (m) { |
986935cf FB |
3039 | arg_reboot_watchdog = manager_get_watchdog(m, WATCHDOG_REBOOT); |
3040 | arg_kexec_watchdog = manager_get_watchdog(m, WATCHDOG_KEXEC); | |
92890452 LP |
3041 | m = manager_free(m); |
3042 | } | |
60918275 | 3043 | |
cc56fafe | 3044 | mac_selinux_finish(); |
b2bb3dbe | 3045 | |
3c7878f9 LP |
3046 | if (reexecute) |
3047 | do_reexecute(argc, argv, | |
3048 | &saved_rlimit_nofile, | |
3049 | &saved_rlimit_memlock, | |
3050 | fds, | |
3051 | switch_root_dir, | |
3052 | switch_root_init, | |
3053 | &error_message); /* This only returns if reexecution failed */ | |
a16e1123 | 3054 | |
74ca738f | 3055 | arg_serialization = safe_fclose(arg_serialization); |
2feceb5e | 3056 | fds = fdset_free(fds); |
a16e1123 | 3057 | |
0e06a031 LP |
3058 | saved_env = strv_free(saved_env); |
3059 | ||
349cc4a5 | 3060 | #if HAVE_VALGRIND_VALGRIND_H |
54b434b1 LP |
3061 | /* If we are PID 1 and running under valgrind, then let's exit |
3062 | * here explicitly. valgrind will only generate nice output on | |
3063 | * exit(), not on exec(), hence let's do the former not the | |
3064 | * latter here. */ | |
8a2c1fbf EJ |
3065 | if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) { |
3066 | /* Cleanup watchdog_device strings for valgrind. We need them | |
3067 | * in become_shutdown() so normally we cannot free them yet. */ | |
3068 | watchdog_free_device(); | |
3069 | arg_watchdog_device = mfree(arg_watchdog_device); | |
7d9eea2b | 3070 | reset_arguments(); |
27fe58b7 | 3071 | return retval; |
8a2c1fbf | 3072 | } |
54b434b1 LP |
3073 | #endif |
3074 | ||
7e11a95e EV |
3075 | #if HAS_FEATURE_ADDRESS_SANITIZER |
3076 | __lsan_do_leak_check(); | |
3077 | #endif | |
3078 | ||
b9080b03 | 3079 | if (shutdown_verb) { |
7eb35049 | 3080 | r = become_shutdown(shutdown_verb, retval); |
4a36297c | 3081 | log_error_errno(r, "Failed to execute shutdown binary, %s: %m", getpid_cached() == 1 ? "freezing" : "quitting"); |
9b9881d7 | 3082 | error_message = "Failed to execute shutdown binary"; |
b9080b03 FF |
3083 | } |
3084 | ||
8a2c1fbf EJ |
3085 | watchdog_free_device(); |
3086 | arg_watchdog_device = mfree(arg_watchdog_device); | |
3087 | ||
df0ff127 | 3088 | if (getpid_cached() == 1) { |
cb6531be ZJS |
3089 | if (error_message) |
3090 | manager_status_printf(NULL, STATUS_TYPE_EMERGENCY, | |
1fc464f6 | 3091 | ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL, |
bb259772 LP |
3092 | "%s.", error_message); |
3093 | freeze_or_exit_or_reboot(); | |
cb6531be | 3094 | } |
c3b3c274 | 3095 | |
7d9eea2b | 3096 | reset_arguments(); |
60918275 LP |
3097 | return retval; |
3098 | } |