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