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