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