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