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