| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/oom.h> |
| 5 |
#include <linux/vt.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <sys/mount.h> |
| 8 |
#include <sys/prctl.h> |
| 9 |
#include <sys/utsname.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
#if HAVE_VALGRIND_VALGRIND_H |
| 13 |
# include <valgrind/valgrind.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
#include "sd-bus.h" |
| 17 |
#include "sd-daemon.h" |
| 18 |
#include "sd-json.h" |
| 19 |
#include "sd-messages.h" |
| 20 |
|
| 21 |
#include "alloc-util.h" |
| 22 |
#include "ansi-color.h" |
| 23 |
#include "apparmor-setup.h" |
| 24 |
#include "architecture.h" |
| 25 |
#include "argv-util.h" |
| 26 |
#include "bpf-restrict-fsaccess.h" |
| 27 |
#include "build.h" |
| 28 |
#include "bus-error.h" |
| 29 |
#include "capability-util.h" |
| 30 |
#include "cgroup-setup.h" |
| 31 |
#include "chase.h" |
| 32 |
#include "clock-util.h" |
| 33 |
#include "clock-warp.h" |
| 34 |
#include "conf-parser.h" |
| 35 |
#include "confidential-virt.h" |
| 36 |
#include "constants.h" |
| 37 |
#include "copy.h" |
| 38 |
#include "coredump-util.h" |
| 39 |
#include "cpu-set-util.h" |
| 40 |
#include "crash-handler.h" |
| 41 |
#include "dbus.h" |
| 42 |
#include "dbus-manager.h" |
| 43 |
#include "dev-setup.h" |
| 44 |
#include "efi-random.h" |
| 45 |
#include "emergency-action.h" |
| 46 |
#include "env-util.h" |
| 47 |
#include "escape.h" |
| 48 |
#include "extract-word.h" |
| 49 |
#include "fd-util.h" |
| 50 |
#include "fdset.h" |
| 51 |
#include "fileio.h" |
| 52 |
#include "format-table.h" |
| 53 |
#include "format-util.h" |
| 54 |
#include "glyph-util.h" |
| 55 |
#include "hash-funcs.h" |
| 56 |
#include "hashmap.h" |
| 57 |
#include "help-util.h" |
| 58 |
#include "hexdecoct.h" |
| 59 |
#include "hostname-setup.h" |
| 60 |
#include "id128-util.h" |
| 61 |
#include "ima-setup.h" |
| 62 |
#include "import-creds.h" |
| 63 |
#include "initrd-util.h" |
| 64 |
#include "io-util.h" |
| 65 |
#include "ipe-setup.h" |
| 66 |
#include "json-util.h" |
| 67 |
#include "killall.h" |
| 68 |
#include "kmod-setup.h" |
| 69 |
#include "label-util.h" |
| 70 |
#include "libmount-util.h" |
| 71 |
#include "limits-util.h" |
| 72 |
#include "load-fragment.h" |
| 73 |
#include "log.h" |
| 74 |
#include "loopback-setup.h" |
| 75 |
#include "luo.h" |
| 76 |
#include "machine-id-setup.h" |
| 77 |
#include "main.h" |
| 78 |
#include "manager.h" |
| 79 |
#include "manager-dump.h" |
| 80 |
#include "manager-serialize.h" |
| 81 |
#include "mkdir.h" |
| 82 |
#include "mount-setup.h" |
| 83 |
#include "mount-util.h" |
| 84 |
#include "options.h" |
| 85 |
#include "os-util.h" |
| 86 |
#include "osc-context.h" |
| 87 |
#include "pager.h" |
| 88 |
#include "parse-argument.h" |
| 89 |
#include "parse-util.h" |
| 90 |
#include "path-util.h" |
| 91 |
#include "pidfd-util.h" |
| 92 |
#include "pretty-print.h" |
| 93 |
#include "proc-cmdline.h" |
| 94 |
#include "process-util.h" |
| 95 |
#include "random-util.h" |
| 96 |
#include "rlimit-util.h" |
| 97 |
#include "rm-rf.h" |
| 98 |
#include "seccomp-util.h" |
| 99 |
#include "selinux-setup.h" |
| 100 |
#include "selinux-util.h" |
| 101 |
#include "serialize.h" |
| 102 |
#include "service.h" |
| 103 |
#include "set.h" |
| 104 |
#include "signal-util.h" |
| 105 |
#include "smack-setup.h" |
| 106 |
#include "special.h" |
| 107 |
#include "stat-util.h" |
| 108 |
#include "stdio-util.h" |
| 109 |
#include "strv.h" |
| 110 |
#include "switch-root.h" |
| 111 |
#include "sysctl-util.h" |
| 112 |
#include "terminal-util.h" |
| 113 |
#include "time-util.h" |
| 114 |
#include "umask-util.h" |
| 115 |
#include "unit-name.h" |
| 116 |
#include "user-util.h" |
| 117 |
#include "utf8.h" |
| 118 |
#include "version.h" |
| 119 |
#include "virt.h" |
| 120 |
#include "watchdog.h" |
| 121 |
|
| 122 |
#if HAS_FEATURE_ADDRESS_SANITIZER |
| 123 |
#include <sanitizer/lsan_interface.h> |
| 124 |
#endif |
| 125 |
|
| 126 |
static enum { |
| 127 |
ACTION_RUN, |
| 128 |
ACTION_HELP, |
| 129 |
ACTION_VERSION, |
| 130 |
ACTION_TEST, |
| 131 |
ACTION_DUMP_CONFIGURATION_ITEMS, |
| 132 |
ACTION_DUMP_BUS_PROPERTIES, |
| 133 |
ACTION_BUS_INTROSPECT, |
| 134 |
} arg_action = ACTION_RUN; |
| 135 |
|
| 136 |
static const char *arg_bus_introspect = NULL; |
| 137 |
|
| 138 |
/* Those variables are initialized to 0 automatically, so we avoid uninitialized memory access. Real |
| 139 |
* defaults are assigned in reset_arguments() below. */ |
| 140 |
static char *arg_default_unit; |
| 141 |
static RuntimeScope arg_runtime_scope; |
| 142 |
bool arg_dump_core; |
| 143 |
int arg_crash_chvt; |
| 144 |
bool arg_crash_shell; |
| 145 |
CrashAction arg_crash_action; |
| 146 |
static char *arg_confirm_spawn; |
| 147 |
static ShowStatus arg_show_status; |
| 148 |
static StatusUnitFormat arg_status_unit_format; |
| 149 |
static bool arg_switched_root; |
| 150 |
static PagerFlags arg_pager_flags; |
| 151 |
static bool arg_service_watchdogs; |
| 152 |
static UnitDefaults arg_defaults; |
| 153 |
static usec_t arg_runtime_watchdog; |
| 154 |
static usec_t arg_reboot_watchdog; |
| 155 |
static usec_t arg_kexec_watchdog; |
| 156 |
static usec_t arg_pretimeout_watchdog; |
| 157 |
static char *arg_early_core_pattern; |
| 158 |
static char *arg_watchdog_pretimeout_governor; |
| 159 |
static char *arg_watchdog_device; |
| 160 |
static char **arg_default_environment; |
| 161 |
static char **arg_manager_environment; |
| 162 |
static uint64_t arg_capability_bounding_set; |
| 163 |
static bool arg_no_new_privs; |
| 164 |
static int arg_protect_system; |
| 165 |
static RestrictFileSystemAccess arg_restrict_filesystem_access; |
| 166 |
static nsec_t arg_timer_slack_nsec; |
| 167 |
static Set* arg_syscall_archs; |
| 168 |
static FILE* arg_serialization; |
| 169 |
static FILE* arg_luo_serialization; |
| 170 |
static sd_id128_t arg_machine_id; |
| 171 |
static bool arg_machine_id_from_firmware = false; |
| 172 |
static EmergencyAction arg_cad_burst_action; |
| 173 |
static CPUSet arg_cpu_affinity; |
| 174 |
static NUMAPolicy arg_numa_policy; |
| 175 |
static usec_t arg_clock_usec; |
| 176 |
static void *arg_random_seed; |
| 177 |
static size_t arg_random_seed_size; |
| 178 |
static usec_t arg_reload_limit_interval_sec; |
| 179 |
static unsigned arg_reload_limit_burst; |
| 180 |
static usec_t arg_event_loop_ratelimit_interval_sec; |
| 181 |
static unsigned arg_event_loop_ratelimit_burst; |
| 182 |
static usec_t arg_minimum_uptime_usec; |
| 183 |
|
| 184 |
/* A copy of the original environment block */ |
| 185 |
static char **saved_env = NULL; |
| 186 |
|
| 187 |
static int parse_configuration(const struct rlimit *saved_rlimit_nofile, |
| 188 |
const struct rlimit *saved_rlimit_memlock); |
| 189 |
|
| 190 |
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_crash_action, crash_action, CrashAction, CRASH_FREEZE); |
| 191 |
|
| 192 |
static int manager_find_user_config_paths(char ***ret_files, char ***ret_dirs) { |
| 193 |
_cleanup_free_ char *base = NULL; |
| 194 |
_cleanup_strv_free_ char **files = NULL, **dirs = NULL; |
| 195 |
int r; |
| 196 |
|
| 197 |
r = xdg_user_config_dir("/systemd", &base); |
| 198 |
if (r < 0) |
| 199 |
return r; |
| 200 |
|
| 201 |
r = strv_extendf(&files, "%s/user.conf", base); |
| 202 |
if (r < 0) |
| 203 |
return r; |
| 204 |
|
| 205 |
r = strv_extend(&files, PKGSYSCONFDIR "/user.conf"); |
| 206 |
if (r < 0) |
| 207 |
return r; |
| 208 |
|
| 209 |
r = strv_consume(&dirs, TAKE_PTR(base)); |
| 210 |
if (r < 0) |
| 211 |
return r; |
| 212 |
|
| 213 |
r = strv_extend_strv(&dirs, CONF_PATHS_STRV("systemd"), false); |
| 214 |
if (r < 0) |
| 215 |
return r; |
| 216 |
|
| 217 |
*ret_files = TAKE_PTR(files); |
| 218 |
*ret_dirs = TAKE_PTR(dirs); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int save_console_winsize_in_environment(int tty_fd) { |
| 223 |
int r; |
| 224 |
|
| 225 |
assert(tty_fd >= 0); |
| 226 |
|
| 227 |
struct winsize ws = {}; |
| 228 |
if (ioctl(tty_fd, TIOCGWINSZ, &ws) < 0) { |
| 229 |
log_debug_errno(errno, "Failed to acquire console window size, ignoring."); |
| 230 |
goto unset; |
| 231 |
} |
| 232 |
|
| 233 |
if (ws.ws_col <= 0 && ws.ws_row <= 0) { |
| 234 |
log_debug("No console window size set, ignoring."); |
| 235 |
goto unset; |
| 236 |
} |
| 237 |
|
| 238 |
r = setenvf("COLUMNS", /* overwrite= */ true, "%u", ws.ws_col); |
| 239 |
if (r < 0) { |
| 240 |
log_debug_errno(r, "Failed to set $COLUMNS, ignoring: %m"); |
| 241 |
goto unset; |
| 242 |
} |
| 243 |
|
| 244 |
r = setenvf("LINES", /* overwrite= */ true, "%u", ws.ws_row); |
| 245 |
if (r < 0) { |
| 246 |
log_debug_errno(r, "Failed to set $LINES, ignoring: %m"); |
| 247 |
goto unset; |
| 248 |
} |
| 249 |
|
| 250 |
log_debug("Recorded console dimensions in environment: $COLUMNS=%u $LINES=%u.", ws.ws_col, ws.ws_row); |
| 251 |
return 1; |
| 252 |
|
| 253 |
unset: |
| 254 |
(void) unsetenv("COLUMNS"); |
| 255 |
(void) unsetenv("LINES"); |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
static int console_setup(void) { |
| 260 |
|
| 261 |
if (getpid_cached() != 1) |
| 262 |
return 0; |
| 263 |
|
| 264 |
_cleanup_close_ int tty_fd = -EBADF; |
| 265 |
|
| 266 |
tty_fd = open_terminal("/dev/console", O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 267 |
if (tty_fd < 0) |
| 268 |
return log_error_errno(tty_fd, "Failed to open %s: %m", "/dev/console"); |
| 269 |
|
| 270 |
/* We don't want to force text mode. Plymouth may be showing pictures already from initrd. */ |
| 271 |
reset_dev_console_fd(tty_fd, /* switch_to_text= */ false); |
| 272 |
|
| 273 |
save_console_winsize_in_environment(tty_fd); |
| 274 |
|
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
static int parse_timeout(const char *value, usec_t *ret) { |
| 279 |
int r = 0; |
| 280 |
|
| 281 |
assert(value); |
| 282 |
assert(ret); |
| 283 |
|
| 284 |
if (streq(value, "default")) |
| 285 |
*ret = USEC_INFINITY; |
| 286 |
else if (streq(value, "off")) |
| 287 |
*ret = 0; |
| 288 |
else |
| 289 |
r = parse_sec(value, ret); |
| 290 |
|
| 291 |
return r; |
| 292 |
} |
| 293 |
|
| 294 |
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
| 295 |
int r; |
| 296 |
|
| 297 |
assert(key); |
| 298 |
|
| 299 |
if (STR_IN_SET(key, "systemd.unit", "rd.systemd.unit")) { |
| 300 |
|
| 301 |
if (proc_cmdline_value_missing(key, value)) |
| 302 |
return 0; |
| 303 |
|
| 304 |
if (!unit_name_is_valid(value, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
| 305 |
log_warning("Unit name specified on %s= is not valid, ignoring: %s", key, value); |
| 306 |
else if (in_initrd() == !!startswith(key, "rd.")) |
| 307 |
return free_and_strdup_warn(&arg_default_unit, value); |
| 308 |
|
| 309 |
} else if (proc_cmdline_key_streq(key, "systemd.dump_core")) { |
| 310 |
|
| 311 |
r = value ? parse_boolean(value) : true; |
| 312 |
if (r < 0) |
| 313 |
log_warning_errno(r, "Failed to parse dump core switch %s, ignoring: %m", value); |
| 314 |
else |
| 315 |
arg_dump_core = r; |
| 316 |
|
| 317 |
} else if (proc_cmdline_key_streq(key, "systemd.early_core_pattern")) { |
| 318 |
|
| 319 |
if (proc_cmdline_value_missing(key, value)) |
| 320 |
return 0; |
| 321 |
|
| 322 |
if (path_is_absolute(value)) |
| 323 |
(void) parse_path_argument(value, false, &arg_early_core_pattern); |
| 324 |
else |
| 325 |
log_warning("Specified core pattern '%s' is not an absolute path, ignoring.", value); |
| 326 |
|
| 327 |
} else if (proc_cmdline_key_streq(key, "systemd.crash_chvt")) { |
| 328 |
|
| 329 |
if (!value) |
| 330 |
arg_crash_chvt = 0; /* turn on */ |
| 331 |
else { |
| 332 |
r = parse_crash_chvt(value, &arg_crash_chvt); |
| 333 |
if (r < 0) |
| 334 |
log_warning_errno(r, "Failed to parse crash chvt switch %s, ignoring: %m", value); |
| 335 |
} |
| 336 |
|
| 337 |
} else if (proc_cmdline_key_streq(key, "systemd.crash_shell")) { |
| 338 |
|
| 339 |
r = value ? parse_boolean(value) : true; |
| 340 |
if (r < 0) |
| 341 |
log_warning_errno(r, "Failed to parse crash shell switch %s, ignoring: %m", value); |
| 342 |
else |
| 343 |
arg_crash_shell = r; |
| 344 |
|
| 345 |
} else if (proc_cmdline_key_streq(key, "systemd.crash_reboot")) { |
| 346 |
|
| 347 |
r = value ? parse_boolean(value) : true; |
| 348 |
if (r < 0) |
| 349 |
log_warning_errno(r, "Failed to parse crash reboot switch %s, ignoring: %m", value); |
| 350 |
else |
| 351 |
arg_crash_action = r ? CRASH_REBOOT : CRASH_FREEZE; |
| 352 |
|
| 353 |
} else if (proc_cmdline_key_streq(key, "systemd.crash_action")) { |
| 354 |
|
| 355 |
if (proc_cmdline_value_missing(key, value)) |
| 356 |
return 0; |
| 357 |
|
| 358 |
r = crash_action_from_string(value); |
| 359 |
if (r < 0) |
| 360 |
log_warning_errno(r, "Failed to parse crash action switch %s, ignoring: %m", value); |
| 361 |
else |
| 362 |
arg_crash_action = r; |
| 363 |
|
| 364 |
} else if (proc_cmdline_key_streq(key, "systemd.confirm_spawn")) { |
| 365 |
char *s; |
| 366 |
|
| 367 |
r = parse_confirm_spawn(value, &s); |
| 368 |
if (r < 0) |
| 369 |
log_warning_errno(r, "Failed to parse confirm_spawn switch %s, ignoring: %m", value); |
| 370 |
else |
| 371 |
free_and_replace(arg_confirm_spawn, s); |
| 372 |
|
| 373 |
} else if (proc_cmdline_key_streq(key, "systemd.service_watchdogs")) { |
| 374 |
|
| 375 |
r = value ? parse_boolean(value) : true; |
| 376 |
if (r < 0) |
| 377 |
log_warning_errno(r, "Failed to parse service watchdog switch %s, ignoring: %m", value); |
| 378 |
else |
| 379 |
arg_service_watchdogs = r; |
| 380 |
|
| 381 |
} else if (proc_cmdline_key_streq(key, "systemd.show_status")) { |
| 382 |
|
| 383 |
if (value) { |
| 384 |
r = parse_show_status(value, &arg_show_status); |
| 385 |
if (r < 0) |
| 386 |
log_warning_errno(r, "Failed to parse show status switch %s, ignoring: %m", value); |
| 387 |
} else |
| 388 |
arg_show_status = SHOW_STATUS_YES; |
| 389 |
|
| 390 |
} else if (proc_cmdline_key_streq(key, "systemd.status_unit_format")) { |
| 391 |
|
| 392 |
if (proc_cmdline_value_missing(key, value)) |
| 393 |
return 0; |
| 394 |
|
| 395 |
r = status_unit_format_from_string(value); |
| 396 |
if (r < 0) |
| 397 |
log_warning_errno(r, "Failed to parse %s=%s, ignoring: %m", key, value); |
| 398 |
else |
| 399 |
arg_status_unit_format = r; |
| 400 |
|
| 401 |
} else if (proc_cmdline_key_streq(key, "systemd.default_standard_output")) { |
| 402 |
|
| 403 |
if (proc_cmdline_value_missing(key, value)) |
| 404 |
return 0; |
| 405 |
|
| 406 |
r = exec_output_from_string(value); |
| 407 |
if (r < 0) |
| 408 |
log_warning_errno(r, "Failed to parse default standard output switch %s, ignoring: %m", value); |
| 409 |
else |
| 410 |
arg_defaults.std_output = r; |
| 411 |
|
| 412 |
} else if (proc_cmdline_key_streq(key, "systemd.default_standard_error")) { |
| 413 |
|
| 414 |
if (proc_cmdline_value_missing(key, value)) |
| 415 |
return 0; |
| 416 |
|
| 417 |
r = exec_output_from_string(value); |
| 418 |
if (r < 0) |
| 419 |
log_warning_errno(r, "Failed to parse default standard error switch %s, ignoring: %m", value); |
| 420 |
else |
| 421 |
arg_defaults.std_error = r; |
| 422 |
|
| 423 |
} else if (streq(key, "systemd.setenv")) { |
| 424 |
|
| 425 |
if (proc_cmdline_value_missing(key, value)) |
| 426 |
return 0; |
| 427 |
|
| 428 |
if (!env_assignment_is_valid(value)) |
| 429 |
log_warning("Environment variable assignment '%s' is not valid. Ignoring.", value); |
| 430 |
else { |
| 431 |
r = strv_env_replace_strdup(&arg_default_environment, value); |
| 432 |
if (r < 0) |
| 433 |
return log_oom(); |
| 434 |
} |
| 435 |
|
| 436 |
} else if (proc_cmdline_key_streq(key, "systemd.machine_id")) { |
| 437 |
|
| 438 |
if (proc_cmdline_value_missing(key, value)) |
| 439 |
return 0; |
| 440 |
|
| 441 |
if (streq(value, "firmware")) |
| 442 |
arg_machine_id_from_firmware = true; |
| 443 |
else { |
| 444 |
r = id128_from_string_nonzero(value, &arg_machine_id); |
| 445 |
if (r < 0) |
| 446 |
log_warning_errno(r, "MachineID '%s' is not valid, ignoring: %m", value); |
| 447 |
else |
| 448 |
arg_machine_id_from_firmware = false; |
| 449 |
} |
| 450 |
} else if (proc_cmdline_key_streq(key, "systemd.default_timeout_start_sec")) { |
| 451 |
|
| 452 |
if (proc_cmdline_value_missing(key, value)) |
| 453 |
return 0; |
| 454 |
|
| 455 |
r = parse_sec(value, &arg_defaults.timeout_start_usec); |
| 456 |
if (r < 0) |
| 457 |
log_warning_errno(r, "Failed to parse default start timeout '%s', ignoring: %m", value); |
| 458 |
|
| 459 |
if (arg_defaults.timeout_start_usec <= 0) |
| 460 |
arg_defaults.timeout_start_usec = USEC_INFINITY; |
| 461 |
|
| 462 |
} else if (proc_cmdline_key_streq(key, "systemd.default_device_timeout_sec")) { |
| 463 |
|
| 464 |
if (proc_cmdline_value_missing(key, value)) |
| 465 |
return 0; |
| 466 |
|
| 467 |
r = parse_sec(value, &arg_defaults.device_timeout_usec); |
| 468 |
if (r < 0) |
| 469 |
log_warning_errno(r, "Failed to parse default device timeout '%s', ignoring: %m", value); |
| 470 |
|
| 471 |
if (arg_defaults.device_timeout_usec <= 0) |
| 472 |
arg_defaults.device_timeout_usec = USEC_INFINITY; |
| 473 |
|
| 474 |
} else if (proc_cmdline_key_streq(key, "systemd.cpu_affinity")) { |
| 475 |
|
| 476 |
if (proc_cmdline_value_missing(key, value)) |
| 477 |
return 0; |
| 478 |
|
| 479 |
r = parse_cpu_set(value, &arg_cpu_affinity); |
| 480 |
if (r < 0) |
| 481 |
log_warning_errno(r, "Failed to parse CPU affinity mask '%s', ignoring: %m", value); |
| 482 |
|
| 483 |
} else if (proc_cmdline_key_streq(key, "systemd.watchdog_device")) { |
| 484 |
|
| 485 |
if (proc_cmdline_value_missing(key, value)) |
| 486 |
return 0; |
| 487 |
|
| 488 |
(void) parse_path_argument(value, false, &arg_watchdog_device); |
| 489 |
|
| 490 |
} else if (proc_cmdline_key_streq(key, "systemd.watchdog_sec")) { |
| 491 |
|
| 492 |
if (proc_cmdline_value_missing(key, value)) |
| 493 |
return 0; |
| 494 |
|
| 495 |
r = parse_timeout(value, &arg_runtime_watchdog); |
| 496 |
if (r < 0) { |
| 497 |
log_warning_errno(r, "Failed to parse systemd.watchdog_sec= argument '%s', ignoring: %m", value); |
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
arg_kexec_watchdog = arg_reboot_watchdog = arg_runtime_watchdog; |
| 502 |
|
| 503 |
} else if (proc_cmdline_key_streq(key, "systemd.watchdog_pre_sec")) { |
| 504 |
|
| 505 |
if (proc_cmdline_value_missing(key, value)) |
| 506 |
return 0; |
| 507 |
|
| 508 |
r = parse_timeout(value, &arg_pretimeout_watchdog); |
| 509 |
if (r < 0) { |
| 510 |
log_warning_errno(r, "Failed to parse systemd.watchdog_pre_sec= argument '%s', ignoring: %m", value); |
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
} else if (proc_cmdline_key_streq(key, "systemd.watchdog_pretimeout_governor")) { |
| 515 |
|
| 516 |
if (proc_cmdline_value_missing(key, value) || isempty(value)) { |
| 517 |
arg_watchdog_pretimeout_governor = mfree(arg_watchdog_pretimeout_governor); |
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
if (!string_is_safe(value, /* flags= */ 0)) { |
| 522 |
log_warning("Watchdog pretimeout governor '%s' is not valid, ignoring.", value); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
return free_and_strdup_warn(&arg_watchdog_pretimeout_governor, value); |
| 527 |
|
| 528 |
} else if (proc_cmdline_key_streq(key, "systemd.clock_usec")) { |
| 529 |
|
| 530 |
if (proc_cmdline_value_missing(key, value)) |
| 531 |
return 0; |
| 532 |
|
| 533 |
r = safe_atou64(value, &arg_clock_usec); |
| 534 |
if (r < 0) |
| 535 |
log_warning_errno(r, "Failed to parse systemd.clock_usec= argument, ignoring: %s", value); |
| 536 |
|
| 537 |
} else if (proc_cmdline_key_streq(key, "systemd.random_seed")) { |
| 538 |
void *p; |
| 539 |
size_t sz; |
| 540 |
|
| 541 |
if (proc_cmdline_value_missing(key, value)) |
| 542 |
return 0; |
| 543 |
|
| 544 |
r = unbase64mem(value, &p, &sz); |
| 545 |
if (r < 0) |
| 546 |
log_warning_errno(r, "Failed to parse systemd.random_seed= argument, ignoring: %s", value); |
| 547 |
|
| 548 |
free(arg_random_seed); |
| 549 |
arg_random_seed = sz > 0 ? p : mfree(p); |
| 550 |
arg_random_seed_size = sz; |
| 551 |
|
| 552 |
} else if (proc_cmdline_key_streq(key, "systemd.reload_limit_interval_sec")) { |
| 553 |
|
| 554 |
if (proc_cmdline_value_missing(key, value)) |
| 555 |
return 0; |
| 556 |
|
| 557 |
r = parse_sec(value, &arg_reload_limit_interval_sec); |
| 558 |
if (r < 0) { |
| 559 |
log_warning_errno(r, "Failed to parse systemd.reload_limit_interval_sec= argument '%s', ignoring: %m", value); |
| 560 |
return 0; |
| 561 |
} |
| 562 |
|
| 563 |
} else if (proc_cmdline_key_streq(key, "systemd.reload_limit_burst")) { |
| 564 |
|
| 565 |
if (proc_cmdline_value_missing(key, value)) |
| 566 |
return 0; |
| 567 |
|
| 568 |
r = safe_atou(value, &arg_reload_limit_burst); |
| 569 |
if (r < 0) { |
| 570 |
log_warning_errno(r, "Failed to parse systemd.reload_limit_burst= argument '%s', ignoring: %m", value); |
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
} else if (proc_cmdline_key_streq(key, "systemd.event_loop_ratelimit_interval_sec")) { |
| 575 |
|
| 576 |
if (proc_cmdline_value_missing(key, value)) |
| 577 |
return 0; |
| 578 |
|
| 579 |
r = parse_sec(value, &arg_event_loop_ratelimit_interval_sec); |
| 580 |
if (r < 0) { |
| 581 |
log_warning_errno(r, "Failed to parse systemd.event_loop_ratelimit_interval_sec= argument '%s', ignoring: %m", value); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
} else if (proc_cmdline_key_streq(key, "systemd.event_loop_ratelimit_burst")) { |
| 586 |
|
| 587 |
if (proc_cmdline_value_missing(key, value)) |
| 588 |
return 0; |
| 589 |
|
| 590 |
r = safe_atou(value, &arg_event_loop_ratelimit_burst); |
| 591 |
if (r < 0) { |
| 592 |
log_warning_errno(r, "Failed to parse systemd.event_loop_ratelimit_burst= argument '%s', ignoring: %m", value); |
| 593 |
return 0; |
| 594 |
} |
| 595 |
|
| 596 |
} else if (proc_cmdline_key_streq(key, "systemd.minimum_uptime_sec")) { |
| 597 |
|
| 598 |
if (proc_cmdline_value_missing(key, value)) |
| 599 |
return 0; |
| 600 |
|
| 601 |
r = parse_sec(value, &arg_minimum_uptime_usec); |
| 602 |
if (r < 0) { |
| 603 |
log_warning_errno(r, "Failed to parse systemd.minimum_uptime_sec= argument '%s', ignoring: %m", value); |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
} else if (proc_cmdline_key_streq(key, "systemd.restrict_filesystem_access")) { |
| 608 |
|
| 609 |
if (value) { |
| 610 |
r = restrict_filesystem_access_from_string(value); |
| 611 |
if (r < 0) |
| 612 |
log_warning_errno(r, "Failed to parse systemd.restrict_filesystem_access= argument '%s', ignoring: %m", value); |
| 613 |
else |
| 614 |
arg_restrict_filesystem_access = r; |
| 615 |
} else |
| 616 |
arg_restrict_filesystem_access = RESTRICT_FILESYSTEM_ACCESS_EXEC; |
| 617 |
|
| 618 |
} else if (streq(key, "quiet") && !value) { |
| 619 |
|
| 620 |
if (arg_show_status == _SHOW_STATUS_INVALID) |
| 621 |
arg_show_status = SHOW_STATUS_ERROR; |
| 622 |
|
| 623 |
} else if (streq(key, "debug") && !value) { |
| 624 |
|
| 625 |
/* Note that log_parse_environment() handles 'debug' |
| 626 |
* too, and sets the log level to LOG_DEBUG. */ |
| 627 |
|
| 628 |
if (detect_container() > 0) |
| 629 |
log_set_target(LOG_TARGET_CONSOLE); |
| 630 |
|
| 631 |
} else if (!value) { |
| 632 |
const char *target; |
| 633 |
|
| 634 |
/* Compatible with SysV, but supported independently even if SysV compatibility is disabled. */ |
| 635 |
target = runlevel_to_target(key); |
| 636 |
if (target) |
| 637 |
return free_and_strdup_warn(&arg_default_unit, target); |
| 638 |
} |
| 639 |
|
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
#define DEFINE_SETTER(name, func, descr) \ |
| 644 |
static int name(const char *unit, \ |
| 645 |
const char *filename, \ |
| 646 |
unsigned line, \ |
| 647 |
const char *section, \ |
| 648 |
unsigned section_line, \ |
| 649 |
const char *lvalue, \ |
| 650 |
int ltype, \ |
| 651 |
const char *rvalue, \ |
| 652 |
void *data, \ |
| 653 |
void *userdata) { \ |
| 654 |
\ |
| 655 |
int r; \ |
| 656 |
\ |
| 657 |
assert(filename); \ |
| 658 |
assert(lvalue); \ |
| 659 |
assert(rvalue); \ |
| 660 |
\ |
| 661 |
r = func(rvalue); \ |
| 662 |
if (r < 0) \ |
| 663 |
log_syntax(unit, LOG_ERR, filename, line, r, \ |
| 664 |
"Invalid " descr "'%s': %m", \ |
| 665 |
rvalue); \ |
| 666 |
\ |
| 667 |
return 0; \ |
| 668 |
} |
| 669 |
|
| 670 |
DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level"); |
| 671 |
DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target"); |
| 672 |
DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color"); |
| 673 |
DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location"); |
| 674 |
DEFINE_SETTER(config_parse_time, log_show_time_from_string, "time"); |
| 675 |
|
| 676 |
static int config_parse_default_timeout_abort( |
| 677 |
const char *unit, |
| 678 |
const char *filename, |
| 679 |
unsigned line, |
| 680 |
const char *section, |
| 681 |
unsigned section_line, |
| 682 |
const char *lvalue, |
| 683 |
int ltype, |
| 684 |
const char *rvalue, |
| 685 |
void *data, |
| 686 |
void *userdata) { |
| 687 |
int r; |
| 688 |
|
| 689 |
r = config_parse_timeout_abort( |
| 690 |
unit, |
| 691 |
filename, |
| 692 |
line, |
| 693 |
section, |
| 694 |
section_line, |
| 695 |
lvalue, |
| 696 |
ltype, |
| 697 |
rvalue, |
| 698 |
&arg_defaults.timeout_abort_usec, |
| 699 |
userdata); |
| 700 |
if (r >= 0) |
| 701 |
arg_defaults.timeout_abort_set = r; |
| 702 |
return 0; |
| 703 |
} |
| 704 |
|
| 705 |
static int config_parse_oom_score_adjust( |
| 706 |
const char *unit, |
| 707 |
const char *filename, |
| 708 |
unsigned line, |
| 709 |
const char *section, |
| 710 |
unsigned section_line, |
| 711 |
const char *lvalue, |
| 712 |
int ltype, |
| 713 |
const char *rvalue, |
| 714 |
void *data, |
| 715 |
void *userdata) { |
| 716 |
|
| 717 |
int oa, r; |
| 718 |
|
| 719 |
if (isempty(rvalue)) { |
| 720 |
arg_defaults.oom_score_adjust_set = false; |
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
r = parse_oom_score_adjust(rvalue, &oa); |
| 725 |
if (r < 0) |
| 726 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 727 |
|
| 728 |
arg_defaults.oom_score_adjust = oa; |
| 729 |
arg_defaults.oom_score_adjust_set = true; |
| 730 |
|
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
static int config_parse_protect_system_pid1( |
| 735 |
const char *unit, |
| 736 |
const char *filename, |
| 737 |
unsigned line, |
| 738 |
const char *section, |
| 739 |
unsigned section_line, |
| 740 |
const char *lvalue, |
| 741 |
int ltype, |
| 742 |
const char *rvalue, |
| 743 |
void *data, |
| 744 |
void *userdata) { |
| 745 |
|
| 746 |
int *v = ASSERT_PTR(data), r; |
| 747 |
|
| 748 |
/* This is modelled after the per-service ProtectSystem= setting, but a bit more restricted on one |
| 749 |
* hand, and more automatic in another. i.e. we currently only support yes/no (not "strict" or |
| 750 |
* "full"). And we will enable this automatically for the initrd unless configured otherwise. |
| 751 |
* |
| 752 |
* We might extend this later to match more closely what the per-service ProtectSystem= can do, but |
| 753 |
* this is not trivial, due to ordering constraints: besides /usr/ we don't really have much mounted |
| 754 |
* at the moment we enable this logic. */ |
| 755 |
|
| 756 |
if (isempty(rvalue) || streq(rvalue, "auto")) { |
| 757 |
*v = -1; |
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
r = parse_boolean(rvalue); |
| 762 |
if (r < 0) |
| 763 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 764 |
|
| 765 |
*v = r; |
| 766 |
return 0; |
| 767 |
} |
| 768 |
|
| 769 |
static int config_parse_restrict_filesystem_access( |
| 770 |
const char *unit, |
| 771 |
const char *filename, |
| 772 |
unsigned line, |
| 773 |
const char *section, |
| 774 |
unsigned section_line, |
| 775 |
const char *lvalue, |
| 776 |
int ltype, |
| 777 |
const char *rvalue, |
| 778 |
void *data, |
| 779 |
void *userdata) { |
| 780 |
|
| 781 |
RestrictFileSystemAccess *v = ASSERT_PTR(data); |
| 782 |
RestrictFileSystemAccess re; |
| 783 |
|
| 784 |
re = restrict_filesystem_access_from_string(rvalue); |
| 785 |
if (re < 0) |
| 786 |
return log_syntax_parse_error(unit, filename, line, re, lvalue, rvalue); |
| 787 |
|
| 788 |
*v = re; |
| 789 |
return 0; |
| 790 |
} |
| 791 |
|
| 792 |
static int config_parse_crash_reboot( |
| 793 |
const char *unit, |
| 794 |
const char *filename, |
| 795 |
unsigned line, |
| 796 |
const char *section, |
| 797 |
unsigned section_line, |
| 798 |
const char *lvalue, |
| 799 |
int ltype, |
| 800 |
const char *rvalue, |
| 801 |
void *data, |
| 802 |
void *userdata) { |
| 803 |
|
| 804 |
CrashAction *v = ASSERT_PTR(data); |
| 805 |
int r; |
| 806 |
|
| 807 |
if (isempty(rvalue)) { |
| 808 |
*v = CRASH_REBOOT; |
| 809 |
return 0; |
| 810 |
} |
| 811 |
|
| 812 |
r = parse_boolean(rvalue); |
| 813 |
if (r < 0) |
| 814 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 815 |
|
| 816 |
*v = r > 0 ? CRASH_REBOOT : CRASH_FREEZE; |
| 817 |
return 0; |
| 818 |
} |
| 819 |
|
| 820 |
static int parse_config_file(void) { |
| 821 |
const ConfigTableItem items[] = { |
| 822 |
{ "Manager", "LogLevel", config_parse_level2, 0, NULL }, |
| 823 |
{ "Manager", "LogTarget", config_parse_target, 0, NULL }, |
| 824 |
{ "Manager", "LogColor", config_parse_color, 0, NULL }, |
| 825 |
{ "Manager", "LogLocation", config_parse_location, 0, NULL }, |
| 826 |
{ "Manager", "LogTime", config_parse_time, 0, NULL }, |
| 827 |
{ "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core }, |
| 828 |
{ "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt, 0, &arg_crash_chvt }, |
| 829 |
{ "Manager", "CrashChangeVT", config_parse_crash_chvt, 0, &arg_crash_chvt }, |
| 830 |
{ "Manager", "CrashShell", config_parse_bool, 0, &arg_crash_shell }, |
| 831 |
{ "Manager", "CrashReboot", config_parse_crash_reboot, 0, &arg_crash_action }, |
| 832 |
{ "Manager", "CrashAction", config_parse_crash_action, 0, &arg_crash_action }, |
| 833 |
{ "Manager", "ShowStatus", config_parse_show_status, 0, &arg_show_status }, |
| 834 |
{ "Manager", "StatusUnitFormat", config_parse_status_unit_format, 0, &arg_status_unit_format }, |
| 835 |
{ "Manager", "CPUAffinity", config_parse_cpu_set, 0, &arg_cpu_affinity }, |
| 836 |
{ "Manager", "NUMAPolicy", config_parse_numa_policy, 0, &arg_numa_policy.type }, |
| 837 |
{ "Manager", "NUMAMask", config_parse_numa_mask, 0, &arg_numa_policy.nodes }, |
| 838 |
{ "Manager", "JoinControllers", config_parse_warn_compat, DISABLED_LEGACY, NULL }, |
| 839 |
{ "Manager", "RuntimeWatchdogSec", config_parse_watchdog_sec, 0, &arg_runtime_watchdog }, |
| 840 |
{ "Manager", "RuntimeWatchdogPreSec", config_parse_watchdog_sec, 0, &arg_pretimeout_watchdog }, |
| 841 |
{ "Manager", "RebootWatchdogSec", config_parse_watchdog_sec, 0, &arg_reboot_watchdog }, |
| 842 |
{ "Manager", "ShutdownWatchdogSec", config_parse_watchdog_sec, 0, &arg_reboot_watchdog }, /* obsolete alias */ |
| 843 |
{ "Manager", "KExecWatchdogSec", config_parse_watchdog_sec, 0, &arg_kexec_watchdog }, |
| 844 |
{ "Manager", "WatchdogDevice", config_parse_path, 0, &arg_watchdog_device }, |
| 845 |
{ "Manager", "RuntimeWatchdogPreGovernor", config_parse_string, CONFIG_PARSE_STRING_SAFE, &arg_watchdog_pretimeout_governor }, |
| 846 |
{ "Manager", "CapabilityBoundingSet", config_parse_capability_set, 0, &arg_capability_bounding_set }, |
| 847 |
{ "Manager", "NoNewPrivileges", config_parse_bool, 0, &arg_no_new_privs }, |
| 848 |
{ "Manager", "ProtectSystem", config_parse_protect_system_pid1, 0, &arg_protect_system }, |
| 849 |
{ "Manager", "RestrictFileSystemAccess", config_parse_restrict_filesystem_access, 0, &arg_restrict_filesystem_access }, |
| 850 |
#if HAVE_SECCOMP |
| 851 |
{ "Manager", "SystemCallArchitectures", config_parse_syscall_archs, 0, &arg_syscall_archs }, |
| 852 |
#else |
| 853 |
{ "Manager", "SystemCallArchitectures", config_parse_warn_compat, DISABLED_CONFIGURATION, NULL }, |
| 854 |
#endif |
| 855 |
{ "Manager", "TimerSlackNSec", config_parse_nsec, 0, &arg_timer_slack_nsec }, |
| 856 |
{ "Manager", "DefaultTimerAccuracySec", config_parse_sec, 0, &arg_defaults.timer_accuracy_usec }, |
| 857 |
{ "Manager", "DefaultStandardOutput", config_parse_output_restricted, 0, &arg_defaults.std_output }, |
| 858 |
{ "Manager", "DefaultStandardError", config_parse_output_restricted, 0, &arg_defaults.std_error }, |
| 859 |
{ "Manager", "DefaultTimeoutStartSec", config_parse_sec, 0, &arg_defaults.timeout_start_usec }, |
| 860 |
{ "Manager", "DefaultTimeoutStopSec", config_parse_sec, 0, &arg_defaults.timeout_stop_usec }, |
| 861 |
{ "Manager", "DefaultTimeoutAbortSec", config_parse_default_timeout_abort, 0, NULL }, |
| 862 |
{ "Manager", "DefaultDeviceTimeoutSec", config_parse_sec, 0, &arg_defaults.device_timeout_usec }, |
| 863 |
{ "Manager", "DefaultRestartSec", config_parse_sec, 0, &arg_defaults.restart_usec }, |
| 864 |
{ "Manager", "DefaultStartLimitInterval", config_parse_sec, 0, &arg_defaults.start_limit.interval }, /* obsolete alias */ |
| 865 |
{ "Manager", "DefaultStartLimitIntervalSec", config_parse_sec, 0, &arg_defaults.start_limit.interval }, |
| 866 |
{ "Manager", "DefaultStartLimitBurst", config_parse_unsigned, 0, &arg_defaults.start_limit.burst }, |
| 867 |
{ "Manager", "DefaultRestrictSUIDSGID", config_parse_bool, 0, &arg_defaults.restrict_suid_sgid }, |
| 868 |
{ "Manager", "DefaultEnvironment", config_parse_environ, arg_runtime_scope, &arg_default_environment }, |
| 869 |
{ "Manager", "ManagerEnvironment", config_parse_environ, arg_runtime_scope, &arg_manager_environment }, |
| 870 |
{ "Manager", "DefaultLimitCPU", config_parse_rlimit, RLIMIT_CPU, arg_defaults.rlimit }, |
| 871 |
{ "Manager", "DefaultLimitFSIZE", config_parse_rlimit, RLIMIT_FSIZE, arg_defaults.rlimit }, |
| 872 |
{ "Manager", "DefaultLimitDATA", config_parse_rlimit, RLIMIT_DATA, arg_defaults.rlimit }, |
| 873 |
{ "Manager", "DefaultLimitSTACK", config_parse_rlimit, RLIMIT_STACK, arg_defaults.rlimit }, |
| 874 |
{ "Manager", "DefaultLimitCORE", config_parse_rlimit, RLIMIT_CORE, arg_defaults.rlimit }, |
| 875 |
{ "Manager", "DefaultLimitRSS", config_parse_rlimit, RLIMIT_RSS, arg_defaults.rlimit }, |
| 876 |
{ "Manager", "DefaultLimitNOFILE", config_parse_rlimit, RLIMIT_NOFILE, arg_defaults.rlimit }, |
| 877 |
{ "Manager", "DefaultLimitAS", config_parse_rlimit, RLIMIT_AS, arg_defaults.rlimit }, |
| 878 |
{ "Manager", "DefaultLimitNPROC", config_parse_rlimit, RLIMIT_NPROC, arg_defaults.rlimit }, |
| 879 |
{ "Manager", "DefaultLimitMEMLOCK", config_parse_rlimit, RLIMIT_MEMLOCK, arg_defaults.rlimit }, |
| 880 |
{ "Manager", "DefaultLimitLOCKS", config_parse_rlimit, RLIMIT_LOCKS, arg_defaults.rlimit }, |
| 881 |
{ "Manager", "DefaultLimitSIGPENDING", config_parse_rlimit, RLIMIT_SIGPENDING, arg_defaults.rlimit }, |
| 882 |
{ "Manager", "DefaultLimitMSGQUEUE", config_parse_rlimit, RLIMIT_MSGQUEUE, arg_defaults.rlimit }, |
| 883 |
{ "Manager", "DefaultLimitNICE", config_parse_rlimit, RLIMIT_NICE, arg_defaults.rlimit }, |
| 884 |
{ "Manager", "DefaultLimitRTPRIO", config_parse_rlimit, RLIMIT_RTPRIO, arg_defaults.rlimit }, |
| 885 |
{ "Manager", "DefaultLimitRTTIME", config_parse_rlimit, RLIMIT_RTTIME, arg_defaults.rlimit }, |
| 886 |
{ "Manager", "DefaultCPUAccounting", config_parse_warn_compat, DISABLED_LEGACY, NULL }, |
| 887 |
{ "Manager", "DefaultIOAccounting", config_parse_bool, 0, &arg_defaults.io_accounting }, |
| 888 |
{ "Manager", "DefaultIPAccounting", config_parse_bool, 0, &arg_defaults.ip_accounting }, |
| 889 |
{ "Manager", "DefaultBlockIOAccounting", config_parse_warn_compat, DISABLED_LEGACY, NULL }, |
| 890 |
{ "Manager", "DefaultMemoryAccounting", config_parse_bool, 0, &arg_defaults.memory_accounting }, |
| 891 |
{ "Manager", "DefaultTasksAccounting", config_parse_bool, 0, &arg_defaults.tasks_accounting }, |
| 892 |
{ "Manager", "DefaultTasksMax", config_parse_tasks_max, 0, &arg_defaults.tasks_max }, |
| 893 |
{ "Manager", "DefaultMemoryPressureThresholdSec", config_parse_sec, 0, &arg_defaults.pressure[PRESSURE_MEMORY].threshold_usec }, |
| 894 |
{ "Manager", "DefaultMemoryPressureWatch", config_parse_pressure_watch, 0, &arg_defaults.pressure[PRESSURE_MEMORY].watch }, |
| 895 |
{ "Manager", "DefaultCPUPressureThresholdSec", config_parse_sec, 0, &arg_defaults.pressure[PRESSURE_CPU].threshold_usec }, |
| 896 |
{ "Manager", "DefaultCPUPressureWatch", config_parse_pressure_watch, 0, &arg_defaults.pressure[PRESSURE_CPU].watch }, |
| 897 |
{ "Manager", "DefaultIOPressureThresholdSec", config_parse_sec, 0, &arg_defaults.pressure[PRESSURE_IO].threshold_usec }, |
| 898 |
{ "Manager", "DefaultIOPressureWatch", config_parse_pressure_watch, 0, &arg_defaults.pressure[PRESSURE_IO].watch }, |
| 899 |
{ "Manager", "CtrlAltDelBurstAction", config_parse_emergency_action, arg_runtime_scope, &arg_cad_burst_action }, |
| 900 |
{ "Manager", "DefaultOOMPolicy", config_parse_oom_policy, 0, &arg_defaults.oom_policy }, |
| 901 |
{ "Manager", "DefaultOOMScoreAdjust", config_parse_oom_score_adjust, 0, NULL }, |
| 902 |
{ "Manager", "ReloadLimitIntervalSec", config_parse_sec, 0, &arg_reload_limit_interval_sec }, |
| 903 |
{ "Manager", "ReloadLimitBurst", config_parse_unsigned, 0, &arg_reload_limit_burst }, |
| 904 |
{ "Manager", "EventLoopRateLimitIntervalSec", config_parse_sec, 0, &arg_event_loop_ratelimit_interval_sec }, |
| 905 |
{ "Manager", "EventLoopRateLimitBurst", config_parse_unsigned, 0, &arg_event_loop_ratelimit_burst }, |
| 906 |
{ "Manager", "DefaultMemoryZSwapWriteback", config_parse_bool, 0, &arg_defaults.memory_zswap_writeback }, |
| 907 |
{ "Manager", "MinimumUptimeSec", config_parse_sec, 0, &arg_minimum_uptime_usec }, |
| 908 |
#if ENABLE_SMACK |
| 909 |
{ "Manager", "DefaultSmackProcessLabel", config_parse_string, 0, &arg_defaults.smack_process_label }, |
| 910 |
#else |
| 911 |
{ "Manager", "DefaultSmackProcessLabel", config_parse_warn_compat, DISABLED_CONFIGURATION, NULL }, |
| 912 |
#endif |
| 913 |
{} |
| 914 |
}; |
| 915 |
|
| 916 |
if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM) |
| 917 |
(void) config_parse_standard_file_with_dropins( |
| 918 |
"systemd/system.conf", |
| 919 |
"Manager\0", |
| 920 |
config_item_table_lookup, items, |
| 921 |
CONFIG_PARSE_WARN, |
| 922 |
/* userdata= */ NULL); |
| 923 |
else { |
| 924 |
_cleanup_strv_free_ char **files = NULL, **dirs = NULL; |
| 925 |
int r; |
| 926 |
|
| 927 |
assert(arg_runtime_scope == RUNTIME_SCOPE_USER); |
| 928 |
|
| 929 |
r = manager_find_user_config_paths(&files, &dirs); |
| 930 |
if (r < 0) |
| 931 |
return log_error_errno(r, "Failed to determine config file paths: %m"); |
| 932 |
|
| 933 |
(void) config_parse_many( |
| 934 |
(const char* const*) files, |
| 935 |
(const char* const*) dirs, |
| 936 |
"user.conf.d", |
| 937 |
"Manager\0", |
| 938 |
config_item_table_lookup, items, |
| 939 |
CONFIG_PARSE_WARN, |
| 940 |
/* userdata= */ NULL); |
| 941 |
} |
| 942 |
|
| 943 |
/* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we use |
| 944 |
* USEC_INFINITY like everywhere else. */ |
| 945 |
if (arg_defaults.timeout_start_usec <= 0) |
| 946 |
arg_defaults.timeout_start_usec = USEC_INFINITY; |
| 947 |
if (arg_defaults.timeout_stop_usec <= 0) |
| 948 |
arg_defaults.timeout_stop_usec = USEC_INFINITY; |
| 949 |
|
| 950 |
return 0; |
| 951 |
} |
| 952 |
|
| 953 |
static void set_manager_defaults(Manager *m) { |
| 954 |
int r; |
| 955 |
|
| 956 |
assert(m); |
| 957 |
|
| 958 |
/* Propagates the various default unit property settings into the manager object, i.e. properties |
| 959 |
* that do not affect the manager itself, but are just what newly allocated units will have set if |
| 960 |
* they haven't set anything else. (Also see set_manager_settings() for the settings that affect the |
| 961 |
* manager's own behaviour) */ |
| 962 |
|
| 963 |
r = manager_set_unit_defaults(m, &arg_defaults); |
| 964 |
if (r < 0) |
| 965 |
log_warning_errno(r, "Failed to set manager defaults, ignoring: %m"); |
| 966 |
|
| 967 |
r = manager_default_environment(m); |
| 968 |
if (r < 0) |
| 969 |
log_warning_errno(r, "Failed to set manager default environment, ignoring: %m"); |
| 970 |
|
| 971 |
r = manager_transient_environment_add(m, arg_default_environment); |
| 972 |
if (r < 0) |
| 973 |
log_warning_errno(r, "Failed to add to transient environment, ignoring: %m"); |
| 974 |
} |
| 975 |
|
| 976 |
static void set_manager_settings(Manager *m) { |
| 977 |
int r; |
| 978 |
|
| 979 |
assert(m); |
| 980 |
|
| 981 |
/* Propagates the various manager settings into the manager object, i.e. properties that |
| 982 |
* affect the manager itself (as opposed to just being inherited into newly allocated |
| 983 |
* units, see set_manager_defaults() above). */ |
| 984 |
|
| 985 |
m->confirm_spawn = arg_confirm_spawn; |
| 986 |
m->service_watchdogs = arg_service_watchdogs; |
| 987 |
m->cad_burst_action = arg_cad_burst_action; |
| 988 |
/* Note that we don't do structured initialization here, otherwise it will reset the rate limit |
| 989 |
* counter on every daemon-reload. */ |
| 990 |
m->reload_reexec_ratelimit.interval = arg_reload_limit_interval_sec; |
| 991 |
m->reload_reexec_ratelimit.burst = arg_reload_limit_burst; |
| 992 |
m->event_loop_ratelimit.interval = arg_event_loop_ratelimit_interval_sec; |
| 993 |
m->event_loop_ratelimit.burst = arg_event_loop_ratelimit_burst; |
| 994 |
|
| 995 |
manager_set_watchdog(m, WATCHDOG_RUNTIME, arg_runtime_watchdog); |
| 996 |
manager_set_watchdog(m, WATCHDOG_REBOOT, arg_reboot_watchdog); |
| 997 |
manager_set_watchdog(m, WATCHDOG_KEXEC, arg_kexec_watchdog); |
| 998 |
manager_set_watchdog(m, WATCHDOG_PRETIMEOUT, arg_pretimeout_watchdog); |
| 999 |
r = manager_set_watchdog_pretimeout_governor(m, arg_watchdog_pretimeout_governor); |
| 1000 |
if (r < 0) |
| 1001 |
log_warning_errno(r, "Failed to set watchdog pretimeout governor to '%s', ignoring: %m", arg_watchdog_pretimeout_governor); |
| 1002 |
|
| 1003 |
manager_set_show_status(m, arg_show_status, "command line"); |
| 1004 |
m->status_unit_format = arg_status_unit_format; |
| 1005 |
m->restrict_filesystem_access = arg_restrict_filesystem_access; |
| 1006 |
} |
| 1007 |
|
| 1008 |
static int redirect_telinit(char *argv[], char **args) { |
| 1009 |
/* Check if we are invoked through the legacy interface, where init would be symlinked as telinit |
| 1010 |
* and allow users to call 'init 0' and such. If we detect such use, tell the user that this is not |
| 1011 |
* supported anymore. */ |
| 1012 |
|
| 1013 |
if (getpid_cached() == 1 || !invoked_as(argv, "init")) |
| 1014 |
return 0; |
| 1015 |
|
| 1016 |
/* Check if the user specified one of the telinit commands in args. */ |
| 1017 |
if (!strv_overlap(args, |
| 1018 |
STRV_MAKE("0", "1", "2", "3", "4", "5", "6", |
| 1019 |
"s", "S", "q", "Q", "u", "U"))) |
| 1020 |
return 0; |
| 1021 |
|
| 1022 |
_cleanup_free_ char *a = NULL, *b = NULL, *c = NULL; |
| 1023 |
(void) terminal_urlify_man_full("shutdown", "8", /* suffix= */ NULL, &a); |
| 1024 |
(void) terminal_urlify_man_full("reboot", "8", /* suffix= */ NULL, &b); |
| 1025 |
(void) terminal_urlify_man_full("systemctl", "1", /* suffix= */ NULL, &c); |
| 1026 |
|
| 1027 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 1028 |
"Program 'systemd' called as 'init' with a legacy telinit command.\n" |
| 1029 |
"Call %s, %s, or %s instead.", |
| 1030 |
strnull(a), strnull(b), strnull(c)); |
| 1031 |
} |
| 1032 |
|
| 1033 |
static int parse_argv(int argc, char *argv[]) { |
| 1034 |
bool user_arg_seen = false; |
| 1035 |
int r; |
| 1036 |
|
| 1037 |
assert(argc >= 1); |
| 1038 |
assert(argv); |
| 1039 |
|
| 1040 |
int log_level_shift = getpid_cached() == 1 ? LOG_DEBUG - LOG_ERR : 0; |
| 1041 |
OptionParser opts = { argc, argv, .log_level_shift = log_level_shift }; |
| 1042 |
|
| 1043 |
/* Note: when new options are added here, also add them to the exclusion list in proc-cmdline.c! */ |
| 1044 |
|
| 1045 |
FOREACH_OPTION(c, &opts) |
| 1046 |
switch (c) { |
| 1047 |
|
| 1048 |
OPTION_COMMON_HELP: |
| 1049 |
arg_action = ACTION_HELP; |
| 1050 |
break; |
| 1051 |
|
| 1052 |
OPTION_COMMON_VERSION: |
| 1053 |
arg_action = ACTION_VERSION; |
| 1054 |
break; |
| 1055 |
|
| 1056 |
OPTION_LONG("test", NULL, "Determine initial transaction, dump it and exit"): |
| 1057 |
arg_action = ACTION_TEST; |
| 1058 |
break; |
| 1059 |
|
| 1060 |
OPTION_LONG("system", NULL, "Combined with --test: operate in system mode"): |
| 1061 |
arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 1062 |
break; |
| 1063 |
|
| 1064 |
OPTION_LONG("user", NULL, "Combined with --test: operate in user mode"): |
| 1065 |
arg_runtime_scope = RUNTIME_SCOPE_USER; |
| 1066 |
user_arg_seen = true; |
| 1067 |
break; |
| 1068 |
|
| 1069 |
OPTION_LONG("dump-configuration-items", NULL, "Dump understood unit configuration items"): |
| 1070 |
arg_action = ACTION_DUMP_CONFIGURATION_ITEMS; |
| 1071 |
break; |
| 1072 |
|
| 1073 |
OPTION_LONG("dump-bus-properties", NULL, "Dump exposed bus properties"): |
| 1074 |
arg_action = ACTION_DUMP_BUS_PROPERTIES; |
| 1075 |
break; |
| 1076 |
|
| 1077 |
OPTION_LONG("bus-introspect", "PATH", "Write XML introspection data"): |
| 1078 |
arg_bus_introspect = opts.arg; |
| 1079 |
arg_action = ACTION_BUS_INTROSPECT; |
| 1080 |
break; |
| 1081 |
|
| 1082 |
OPTION_LONG("unit", "UNIT", "Set default unit"): |
| 1083 |
r = free_and_strdup(&arg_default_unit, opts.arg); |
| 1084 |
if (r < 0) |
| 1085 |
return log_error_errno(r, "Failed to set default unit \"%s\": %m", opts.arg); |
| 1086 |
break; |
| 1087 |
|
| 1088 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "dump-core", "BOOL", "Dump core on crash"): |
| 1089 |
r = parse_boolean_argument("--dump-core", opts.arg, &arg_dump_core); |
| 1090 |
if (r < 0) |
| 1091 |
return r; |
| 1092 |
break; |
| 1093 |
|
| 1094 |
OPTION_LONG("crash-chvt", "NR", "Change to specified VT on crash"): {} |
| 1095 |
OPTION_LONG("crash-vt", "NR", /* help= */ NULL): /* compat option that was previously documented in --help */ |
| 1096 |
r = parse_crash_chvt(opts.arg, &arg_crash_chvt); |
| 1097 |
if (r < 0) |
| 1098 |
return log_error_errno(r, "Failed to parse crash virtual terminal index '%s': %m", |
| 1099 |
opts.arg); |
| 1100 |
break; |
| 1101 |
|
| 1102 |
OPTION_LONG("crash-action", "ACTION", "Specify what to do on crash"): |
| 1103 |
r = crash_action_from_string(opts.arg); |
| 1104 |
if (r < 0) |
| 1105 |
return log_error_errno(r, "Failed to parse crash action \"%s\": %m", opts.arg); |
| 1106 |
arg_crash_action = r; |
| 1107 |
break; |
| 1108 |
|
| 1109 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "crash-shell", "BOOL", "Run shell on crash"): |
| 1110 |
r = parse_boolean_argument("--crash-shell", opts.arg, &arg_crash_shell); |
| 1111 |
if (r < 0) |
| 1112 |
return r; |
| 1113 |
break; |
| 1114 |
|
| 1115 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "confirm-spawn", "BOOL", |
| 1116 |
"Ask for confirmation when spawning processes"): |
| 1117 |
arg_confirm_spawn = mfree(arg_confirm_spawn); |
| 1118 |
|
| 1119 |
r = parse_confirm_spawn(opts.arg, &arg_confirm_spawn); |
| 1120 |
if (r < 0) |
| 1121 |
return log_error_errno(r, "Failed to parse confirm spawn option: \"%s\": %m", |
| 1122 |
opts.arg); |
| 1123 |
break; |
| 1124 |
|
| 1125 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "show-status", "BOOL", |
| 1126 |
"Show status updates on the console during boot"): |
| 1127 |
if (opts.arg) { |
| 1128 |
r = parse_show_status(opts.arg, &arg_show_status); |
| 1129 |
if (r < 0) |
| 1130 |
return log_error_errno(r, "Failed to parse show status boolean: \"%s\": %m", |
| 1131 |
opts.arg); |
| 1132 |
} else |
| 1133 |
arg_show_status = SHOW_STATUS_YES; |
| 1134 |
break; |
| 1135 |
|
| 1136 |
OPTION_LONG("log-target", "TARGET", |
| 1137 |
"Set log target (console, journal, kmsg, journal-or-kmsg, null)"): |
| 1138 |
r = log_set_target_from_string(opts.arg); |
| 1139 |
if (r < 0) |
| 1140 |
return log_error_errno(r, "Failed to parse log target \"%s\": %m", opts.arg); |
| 1141 |
break; |
| 1142 |
|
| 1143 |
OPTION_LONG("log-level", "LEVEL", |
| 1144 |
"Set log level (debug, info, notice, warning, err, crit, alert, emerg)"): |
| 1145 |
r = log_set_max_level_from_string(opts.arg); |
| 1146 |
if (r < 0) |
| 1147 |
return log_error_errno(r, "Failed to parse log level \"%s\": %m", opts.arg); |
| 1148 |
break; |
| 1149 |
|
| 1150 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "log-color", "BOOL", "Highlight important log messages"): |
| 1151 |
if (opts.arg) { |
| 1152 |
r = log_show_color_from_string(opts.arg); |
| 1153 |
if (r < 0) |
| 1154 |
return log_error_errno(r, "Failed to parse log color setting \"%s\": %m", |
| 1155 |
opts.arg); |
| 1156 |
} else |
| 1157 |
log_show_color(true); |
| 1158 |
break; |
| 1159 |
|
| 1160 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "log-location", "BOOL", |
| 1161 |
"Include code location in log messages"): |
| 1162 |
if (opts.arg) { |
| 1163 |
r = log_show_location_from_string(opts.arg); |
| 1164 |
if (r < 0) |
| 1165 |
return log_error_errno(r, "Failed to parse log location setting \"%s\": %m", |
| 1166 |
opts.arg); |
| 1167 |
} else |
| 1168 |
log_show_location(true); |
| 1169 |
break; |
| 1170 |
|
| 1171 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "log-time", "BOOL", "Prefix log messages with current time"): |
| 1172 |
if (opts.arg) { |
| 1173 |
r = log_show_time_from_string(opts.arg); |
| 1174 |
if (r < 0) |
| 1175 |
return log_error_errno(r, "Failed to parse log time setting \"%s\": %m", |
| 1176 |
opts.arg); |
| 1177 |
} else |
| 1178 |
log_show_time(true); |
| 1179 |
break; |
| 1180 |
|
| 1181 |
OPTION_LONG("default-standard-output", "…", "Set default standard output for services"): |
| 1182 |
r = exec_output_from_string(opts.arg); |
| 1183 |
if (r < 0) |
| 1184 |
return log_error_errno(r, "Failed to parse default standard output setting \"%s\": %m", |
| 1185 |
opts.arg); |
| 1186 |
arg_defaults.std_output = r; |
| 1187 |
break; |
| 1188 |
|
| 1189 |
OPTION_LONG("default-standard-error", "…", "Set default standard error output for services"): |
| 1190 |
r = exec_output_from_string(opts.arg); |
| 1191 |
if (r < 0) |
| 1192 |
return log_error_errno(r, "Failed to parse default standard error output setting \"%s\": %m", |
| 1193 |
opts.arg); |
| 1194 |
arg_defaults.std_error = r; |
| 1195 |
break; |
| 1196 |
|
| 1197 |
OPTION_COMMON_NO_PAGER: |
| 1198 |
arg_pager_flags |= PAGER_DISABLE; |
| 1199 |
break; |
| 1200 |
|
| 1201 |
/* Options not shown in --help. */ |
| 1202 |
|
| 1203 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "crash-reboot", "BOOL", /* help= */ NULL): |
| 1204 |
r = parse_boolean_argument("--crash-reboot", opts.arg, NULL); |
| 1205 |
if (r < 0) |
| 1206 |
return r; |
| 1207 |
arg_crash_action = r > 0 ? CRASH_REBOOT : CRASH_FREEZE; |
| 1208 |
break; |
| 1209 |
|
| 1210 |
OPTION_LONG("service-watchdogs", "BOOL", /* help= */ NULL): |
| 1211 |
r = parse_boolean_argument("--service-watchdogs=", opts.arg, &arg_service_watchdogs); |
| 1212 |
if (r < 0) |
| 1213 |
return r; |
| 1214 |
break; |
| 1215 |
|
| 1216 |
OPTION_LONG("deserialize", "FD", /* help= */ NULL): { |
| 1217 |
int fd = parse_fd(opts.arg); |
| 1218 |
if (fd < 0) |
| 1219 |
return log_error_errno(fd, "Failed to parse serialization fd \"%s\": %m", opts.arg); |
| 1220 |
|
| 1221 |
(void) fd_cloexec(fd, true); |
| 1222 |
|
| 1223 |
FILE *f = fdopen(fd, "r"); |
| 1224 |
if (!f) |
| 1225 |
return log_error_errno(errno, "Failed to open serialization fd %d: %m", fd); |
| 1226 |
|
| 1227 |
safe_fclose(arg_serialization); |
| 1228 |
arg_serialization = f; |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
|
| 1232 |
OPTION_LONG("switched-root", NULL, /* help= */ NULL): |
| 1233 |
arg_switched_root = true; |
| 1234 |
break; |
| 1235 |
|
| 1236 |
OPTION_LONG("machine-id", "ID", /* help= */ NULL): |
| 1237 |
r = id128_from_string_nonzero(opts.arg, &arg_machine_id); |
| 1238 |
if (r < 0) |
| 1239 |
return log_error_errno(r, "MachineID '%s' is not valid: %m", opts.arg); |
| 1240 |
break; |
| 1241 |
|
| 1242 |
OPTION_SHORT('D', NULL, /* help= */ NULL): |
| 1243 |
log_set_max_level(LOG_DEBUG); |
| 1244 |
break; |
| 1245 |
|
| 1246 |
/* Eat the sysvinit kernel cmdline args that we'll parse in |
| 1247 |
* parse_proc_cmdline_item() or ignore. */ |
| 1248 |
OPTION_SHORT('b', NULL, /* help= */ NULL): {} |
| 1249 |
OPTION_SHORT('s', NULL, /* help= */ NULL): {} |
| 1250 |
OPTION_SHORT('z', "ARG", /* help= */ NULL): |
| 1251 |
if (getpid_cached() != 1) |
| 1252 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1253 |
"Switch -%c is only supported when running as PID 1.", opts.opt->short_code); |
| 1254 |
/* In PID1, do nothing and continue with option parsing. */ |
| 1255 |
break; |
| 1256 |
OPTION_ERROR: |
| 1257 |
if (getpid_cached() != 1) |
| 1258 |
return -EINVAL; |
| 1259 |
/* In PID1, silently terminate option parsing. */ |
| 1260 |
return 0; |
| 1261 |
} |
| 1262 |
|
| 1263 |
r = redirect_telinit(argv, option_parser_get_args(&opts)); |
| 1264 |
if (r < 0) |
| 1265 |
return r; |
| 1266 |
|
| 1267 |
if (option_parser_get_n_args(&opts) > 0 && getpid_cached() != 1) |
| 1268 |
/* Hmm, when we aren't run as init system let's complain about excess arguments */ |
| 1269 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Excess arguments."); |
| 1270 |
|
| 1271 |
if (arg_action == ACTION_RUN && arg_runtime_scope == RUNTIME_SCOPE_USER && !user_arg_seen) |
| 1272 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1273 |
"Explicit --user argument required to run as user manager."); |
| 1274 |
|
| 1275 |
return 0; |
| 1276 |
} |
| 1277 |
|
| 1278 |
static int help(void) { |
| 1279 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 1280 |
int r; |
| 1281 |
|
| 1282 |
r = option_parser_get_help_table(&options); |
| 1283 |
if (r < 0) |
| 1284 |
return r; |
| 1285 |
|
| 1286 |
help_cmdline("[OPTIONS...]"); |
| 1287 |
help_abstract("Starts and monitors system and user services."); |
| 1288 |
|
| 1289 |
printf("\nThis program takes no positional arguments.\n"); |
| 1290 |
|
| 1291 |
help_section("Options"); |
| 1292 |
r = table_print_or_warn(options); |
| 1293 |
if (r < 0) |
| 1294 |
return r; |
| 1295 |
|
| 1296 |
help_man_page_reference("systemd", "1"); |
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
static int prepare_reexecute( |
| 1301 |
Manager *m, |
| 1302 |
FILE **ret_f, |
| 1303 |
FDSet **ret_fds, |
| 1304 |
bool switching_root) { |
| 1305 |
|
| 1306 |
_cleanup_fdset_free_ FDSet *fds = NULL; |
| 1307 |
_cleanup_fclose_ FILE *f = NULL; |
| 1308 |
int r; |
| 1309 |
|
| 1310 |
assert(m); |
| 1311 |
assert(ret_f); |
| 1312 |
assert(ret_fds); |
| 1313 |
|
| 1314 |
/* Make sure nothing is really destructed when we shut down */ |
| 1315 |
m->n_reloading++; |
| 1316 |
bus_manager_send_reloading(m, true); |
| 1317 |
|
| 1318 |
/* Only close the initramfs trust window when actually switching root. |
| 1319 |
* During a plain daemon-reexec in the initrd, PID1 still needs to |
| 1320 |
* execv() itself from the initramfs — clearing trust here would cause |
| 1321 |
* the BPF bprm_check_security hook to deny the exec. */ |
| 1322 |
if (switching_root) { |
| 1323 |
r = bpf_restrict_fsaccess_close_initramfs_trust(m); |
| 1324 |
if (r < 0) |
| 1325 |
return r; |
| 1326 |
} |
| 1327 |
|
| 1328 |
r = manager_open_serialization(m, &f); |
| 1329 |
if (r < 0) |
| 1330 |
return log_error_errno(r, "Failed to create serialization file: %m"); |
| 1331 |
|
| 1332 |
fds = fdset_new(); |
| 1333 |
if (!fds) |
| 1334 |
return log_oom(); |
| 1335 |
|
| 1336 |
r = manager_serialize(m, f, fds, switching_root); |
| 1337 |
if (r < 0) |
| 1338 |
return r; |
| 1339 |
|
| 1340 |
r = finish_serialization_file(f); |
| 1341 |
if (r < 0) |
| 1342 |
return log_error_errno(r, "Failed to finish serialization file: %m"); |
| 1343 |
|
| 1344 |
r = fd_cloexec(fileno(f), false); |
| 1345 |
if (r < 0) |
| 1346 |
return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization: %m"); |
| 1347 |
|
| 1348 |
r = fdset_cloexec(fds, false); |
| 1349 |
if (r < 0) |
| 1350 |
return log_error_errno(r, "Failed to disable O_CLOEXEC for serialization fds: %m"); |
| 1351 |
|
| 1352 |
*ret_f = TAKE_PTR(f); |
| 1353 |
*ret_fds = TAKE_PTR(fds); |
| 1354 |
|
| 1355 |
return 0; |
| 1356 |
} |
| 1357 |
|
| 1358 |
static void bump_file_max_and_nr_open(void) { |
| 1359 |
|
| 1360 |
/* Let's bump fs.file-max and fs.nr_open to their respective maximums. On current kernels large |
| 1361 |
* numbers of file descriptors are no longer a performance problem and their memory is properly |
| 1362 |
* tracked by memcg, thus counting them and limiting them in another two layers of limits is |
| 1363 |
* unnecessary and just complicates things. This function hence turns off 2 of the 4 levels of limits |
| 1364 |
* on file descriptors, and makes RLIMIT_NOLIMIT (soft + hard) the only ones that really matter. */ |
| 1365 |
|
| 1366 |
#if BUMP_PROC_SYS_FS_FILE_MAX || BUMP_PROC_SYS_FS_NR_OPEN |
| 1367 |
int r; |
| 1368 |
#endif |
| 1369 |
|
| 1370 |
#if BUMP_PROC_SYS_FS_FILE_MAX |
| 1371 |
/* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously things were |
| 1372 |
* different, but the operation would fail silently.) */ |
| 1373 |
r = sysctl_write("fs/file-max", LONG_MAX_STR); |
| 1374 |
if (r < 0) |
| 1375 |
log_full_errno(ERRNO_IS_NEG_FS_WRITE_REFUSED(r) ? LOG_DEBUG : LOG_WARNING, r, |
| 1376 |
"Failed to bump fs.file-max, ignoring: %m"); |
| 1377 |
#endif |
| 1378 |
|
| 1379 |
#if BUMP_PROC_SYS_FS_NR_OPEN |
| 1380 |
/* The kernel enforces maximum and minimum values on the fs.nr_open, but they are not directly |
| 1381 |
* exposed, but hardcoded in fs/file.c. Hopefully, these values will not be changed, but not sure. |
| 1382 |
* Let's first try the hardcoded maximum value, and if it does not work, try the half of it. */ |
| 1383 |
|
| 1384 |
for (unsigned v = NR_OPEN_MAXIMUM; v >= NR_OPEN_MINIMUM; v /= 2) { |
| 1385 |
unsigned k = read_nr_open(); |
| 1386 |
if (k >= v) { /* Already larger */ |
| 1387 |
log_debug("Skipping bump, value is already larger."); |
| 1388 |
break; |
| 1389 |
} |
| 1390 |
|
| 1391 |
r = sysctl_writef("fs/nr_open", "%u", v); |
| 1392 |
if (r == -EINVAL) { |
| 1393 |
log_debug("Couldn't write fs.nr_open as %u, halving it.", v); |
| 1394 |
continue; |
| 1395 |
} |
| 1396 |
if (r < 0) { |
| 1397 |
log_full_errno(ERRNO_IS_NEG_FS_WRITE_REFUSED(r) ? LOG_DEBUG : LOG_WARNING, r, |
| 1398 |
"Failed to bump fs.nr_open, ignoring: %m"); |
| 1399 |
break; |
| 1400 |
} |
| 1401 |
|
| 1402 |
log_debug("Successfully bumped fs.nr_open to %u", v); |
| 1403 |
break; |
| 1404 |
} |
| 1405 |
#endif |
| 1406 |
} |
| 1407 |
|
| 1408 |
static int bump_rlimit_nofile(const struct rlimit *saved_rlimit) { |
| 1409 |
struct rlimit new_rlimit; |
| 1410 |
int r; |
| 1411 |
|
| 1412 |
/* Get the underlying absolute limit the kernel enforces */ |
| 1413 |
unsigned nr = read_nr_open(); |
| 1414 |
|
| 1415 |
/* Calculate the new limits to use for us. Never lower from what we inherited. */ |
| 1416 |
new_rlimit = (struct rlimit) { |
| 1417 |
.rlim_cur = MAX((rlim_t) nr, saved_rlimit->rlim_cur), |
| 1418 |
.rlim_max = MAX((rlim_t) nr, saved_rlimit->rlim_max), |
| 1419 |
}; |
| 1420 |
|
| 1421 |
/* Shortcut if nothing changes. */ |
| 1422 |
if (saved_rlimit->rlim_max >= new_rlimit.rlim_max && |
| 1423 |
saved_rlimit->rlim_cur >= new_rlimit.rlim_cur) { |
| 1424 |
log_debug("RLIMIT_NOFILE is already as high or higher than we need it, not bumping."); |
| 1425 |
return 0; |
| 1426 |
} |
| 1427 |
|
| 1428 |
/* Bump up the resource limit for ourselves substantially, all the way to the maximum the kernel allows, for |
| 1429 |
* both hard and soft. */ |
| 1430 |
r = setrlimit_closest(RLIMIT_NOFILE, &new_rlimit); |
| 1431 |
if (r < 0) |
| 1432 |
return log_warning_errno(r, "Setting RLIMIT_NOFILE failed, ignoring: %m"); |
| 1433 |
|
| 1434 |
return 0; |
| 1435 |
} |
| 1436 |
|
| 1437 |
static int bump_rlimit_memlock(const struct rlimit *saved_rlimit) { |
| 1438 |
struct rlimit new_rlimit; |
| 1439 |
uint64_t mm; |
| 1440 |
int r; |
| 1441 |
|
| 1442 |
/* BPF_MAP_TYPE_LPM_TRIE bpf maps are charged against RLIMIT_MEMLOCK, even if we have CAP_IPC_LOCK |
| 1443 |
* which should normally disable such checks. We need them to implement IPAddressAllow= and |
| 1444 |
* IPAddressDeny=, hence let's bump the value high enough for our user. */ |
| 1445 |
|
| 1446 |
/* Using MAX() on resource limits only is safe if RLIM_INFINITY is > 0. POSIX declares that rlim_t |
| 1447 |
* must be unsigned, hence this is a given, but let's make this clear here. */ |
| 1448 |
assert_cc(RLIM_INFINITY > 0); |
| 1449 |
|
| 1450 |
mm = physical_memory_scale(1, 8); /* Let's scale how much we allow to be locked by the amount of |
| 1451 |
* physical RAM. We allow an eighth to be locked by us, just to |
| 1452 |
* pick a value. */ |
| 1453 |
|
| 1454 |
new_rlimit = (struct rlimit) { |
| 1455 |
.rlim_cur = MAX3(HIGH_RLIMIT_MEMLOCK, saved_rlimit->rlim_cur, mm), |
| 1456 |
.rlim_max = MAX3(HIGH_RLIMIT_MEMLOCK, saved_rlimit->rlim_max, mm), |
| 1457 |
}; |
| 1458 |
|
| 1459 |
if (saved_rlimit->rlim_max >= new_rlimit.rlim_cur && |
| 1460 |
saved_rlimit->rlim_cur >= new_rlimit.rlim_max) { |
| 1461 |
log_debug("RLIMIT_MEMLOCK is already as high or higher than we need it, not bumping."); |
| 1462 |
return 0; |
| 1463 |
} |
| 1464 |
|
| 1465 |
r = setrlimit_closest(RLIMIT_MEMLOCK, &new_rlimit); |
| 1466 |
if (r < 0) |
| 1467 |
return log_warning_errno(r, "Setting RLIMIT_MEMLOCK failed, ignoring: %m"); |
| 1468 |
|
| 1469 |
return 0; |
| 1470 |
} |
| 1471 |
|
| 1472 |
static int enforce_syscall_archs(Set *archs) { |
| 1473 |
#if HAVE_SECCOMP |
| 1474 |
int r; |
| 1475 |
|
| 1476 |
if (!is_seccomp_available()) |
| 1477 |
return 0; |
| 1478 |
|
| 1479 |
r = seccomp_restrict_archs(arg_syscall_archs); |
| 1480 |
if (r < 0) |
| 1481 |
return log_error_errno(r, "Failed to enforce system call architecture restriction: %m"); |
| 1482 |
#endif |
| 1483 |
return 0; |
| 1484 |
} |
| 1485 |
|
| 1486 |
static int os_release_status(void) { |
| 1487 |
_cleanup_free_ char *pretty_name = NULL, *fancy_name = NULL, |
| 1488 |
*name = NULL, *version = NULL, *ansi_color = NULL, *support_end = NULL, *codename = NULL; |
| 1489 |
int r; |
| 1490 |
|
| 1491 |
r = parse_os_release(NULL, |
| 1492 |
"PRETTY_NAME", &pretty_name, |
| 1493 |
"FANCY_NAME", &fancy_name, |
| 1494 |
"NAME", &name, |
| 1495 |
"VERSION", &version, |
| 1496 |
"VERSION_CODENAME", &codename, |
| 1497 |
"ANSI_COLOR", &ansi_color, |
| 1498 |
"SUPPORT_END", &support_end); |
| 1499 |
if (r < 0) |
| 1500 |
return log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, |
| 1501 |
"Failed to read os-release file, ignoring: %m"); |
| 1502 |
|
| 1503 |
const char *label = os_release_pretty_name(pretty_name, name); |
| 1504 |
|
| 1505 |
if (show_status_on(arg_show_status)) { |
| 1506 |
const char *color = empty_to_null(ansi_color) ?: "1"; |
| 1507 |
|
| 1508 |
/* The fancy name may contain emoji characters and ANSI sequences. Don't use it if our locale |
| 1509 |
* doesn't allow that, or ANSI sequences are off, or if it is empty. */ |
| 1510 |
|
| 1511 |
if (!isempty(fancy_name)) { |
| 1512 |
_cleanup_free_ char *unescaped = NULL; |
| 1513 |
|
| 1514 |
/* Undo one level of C-style unescaping for this one */ |
| 1515 |
ssize_t l = cunescape(fancy_name, /* flags= */ 0, &unescaped); |
| 1516 |
if (l < 0) { |
| 1517 |
log_debug_errno(l, "Failed to unescape FANCY_NAME=, ignoring: %m"); |
| 1518 |
fancy_name = mfree(fancy_name); |
| 1519 |
} else if (!utf8_is_valid(fancy_name)) { |
| 1520 |
log_debug("Unescaped FANCY_NAME= contains invalid UTF-8, ignoring."); |
| 1521 |
fancy_name = mfree(fancy_name); |
| 1522 |
} else { |
| 1523 |
free_and_replace(fancy_name, unescaped); |
| 1524 |
|
| 1525 |
/* FANCY_NAME= does not contain version/codename info (unlike PRETTY_NAME=), |
| 1526 |
* but in this context it makes sense to show them if defined, hence append |
| 1527 |
* them here. */ |
| 1528 |
if (version && !strextend(&fancy_name, " ", version)) |
| 1529 |
return log_oom(); |
| 1530 |
|
| 1531 |
if (codename && !strextend(&fancy_name, " (", codename, ")")) |
| 1532 |
return log_oom(); |
| 1533 |
} |
| 1534 |
} |
| 1535 |
|
| 1536 |
if (isempty(fancy_name) || |
| 1537 |
(!emoji_enabled() && !ascii_is_valid(fancy_name)) || |
| 1538 |
!log_get_show_color()) |
| 1539 |
fancy_name = mfree(fancy_name); |
| 1540 |
|
| 1541 |
if (!fancy_name && log_get_show_color()) { |
| 1542 |
fancy_name = strjoin("\x1B[", color, "m", label); |
| 1543 |
if (!fancy_name) |
| 1544 |
return log_oom(); |
| 1545 |
} |
| 1546 |
|
| 1547 |
if (in_initrd()) { |
| 1548 |
if (log_get_show_color()) |
| 1549 |
status_printf(NULL, 0, |
| 1550 |
ANSI_HIGHLIGHT "Booting initrd of " ANSI_NORMAL "%s" ANSI_NORMAL ANSI_HIGHLIGHT "." ANSI_NORMAL, |
| 1551 |
fancy_name); |
| 1552 |
else |
| 1553 |
status_printf(NULL, 0, |
| 1554 |
"Booting initrd of %s...", label); |
| 1555 |
} else { |
| 1556 |
if (log_get_show_color()) |
| 1557 |
status_printf(NULL, 0, |
| 1558 |
"\n" ANSI_HIGHLIGHT "Welcome to " ANSI_NORMAL "%s" ANSI_NORMAL ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", |
| 1559 |
fancy_name); |
| 1560 |
else |
| 1561 |
status_printf(NULL, 0, |
| 1562 |
"\nWelcome to %s!\n", |
| 1563 |
label); |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
if (support_end && os_release_support_ended(support_end, /* quiet= */ false, /* ret_eol= */ NULL) > 0) |
| 1568 |
/* pretty_name may include the version already, so we'll print the version only if we |
| 1569 |
* have it and we're not using pretty_name. */ |
| 1570 |
status_printf(ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL, 0, |
| 1571 |
"This OS version (%s%s%s) is past its end-of-support date (%s)", |
| 1572 |
label, |
| 1573 |
(pretty_name || !version) ? "" : " version ", |
| 1574 |
(pretty_name || !version) ? "" : version, |
| 1575 |
support_end); |
| 1576 |
|
| 1577 |
return 0; |
| 1578 |
} |
| 1579 |
|
| 1580 |
static int setup_os_release(RuntimeScope scope) { |
| 1581 |
char os_release_dst[STRLEN("/run/user//systemd/propagate/.os-release-stage/os-release") + DECIMAL_STR_MAX(uid_t)] = |
| 1582 |
"/run/systemd/propagate/.os-release-stage/os-release"; |
| 1583 |
const char *os_release_src = "/etc/os-release"; |
| 1584 |
int r; |
| 1585 |
|
| 1586 |
assert(IN_SET(scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER)); |
| 1587 |
|
| 1588 |
if (access("/etc/os-release", F_OK) < 0) { |
| 1589 |
if (errno != ENOENT) |
| 1590 |
log_debug_errno(errno, "Failed to check if /etc/os-release exists, ignoring: %m"); |
| 1591 |
|
| 1592 |
os_release_src = "/usr/lib/os-release"; |
| 1593 |
} |
| 1594 |
|
| 1595 |
if (scope == RUNTIME_SCOPE_USER) |
| 1596 |
xsprintf(os_release_dst, "/run/user/" UID_FMT "/systemd/propagate/.os-release-stage/os-release", geteuid()); |
| 1597 |
|
| 1598 |
r = mkdir_parents_label(os_release_dst, 0755); |
| 1599 |
if (r < 0) |
| 1600 |
return log_debug_errno(r, "Failed to create parent directory of '%s', ignoring: %m", os_release_dst); |
| 1601 |
|
| 1602 |
r = copy_file_atomic(os_release_src, os_release_dst, 0644, COPY_MAC_CREATE|COPY_REPLACE); |
| 1603 |
if (r < 0) |
| 1604 |
return log_debug_errno(r, "Failed to copy '%s' to '%s', ignoring: %m", |
| 1605 |
os_release_src, os_release_dst); |
| 1606 |
|
| 1607 |
return 0; |
| 1608 |
} |
| 1609 |
|
| 1610 |
static int write_container_id(void) { |
| 1611 |
const char *c; |
| 1612 |
int r = 0; /* avoid false maybe-uninitialized warning */ |
| 1613 |
|
| 1614 |
c = getenv("container"); |
| 1615 |
if (isempty(c)) |
| 1616 |
return 0; |
| 1617 |
|
| 1618 |
WITH_UMASK(0022) |
| 1619 |
r = write_string_file("/run/systemd/container", c, WRITE_STRING_FILE_CREATE); |
| 1620 |
if (r < 0) |
| 1621 |
return log_warning_errno(r, "Failed to write /run/systemd/container, ignoring: %m"); |
| 1622 |
|
| 1623 |
return 1; |
| 1624 |
} |
| 1625 |
|
| 1626 |
static int write_boot_or_shutdown_osc(const char *type) { |
| 1627 |
int r; |
| 1628 |
|
| 1629 |
assert(STRPTR_IN_SET(type, "boot", "shutdown")); |
| 1630 |
|
| 1631 |
if (getenv_terminal_is_dumb()) |
| 1632 |
return 0; |
| 1633 |
|
| 1634 |
_cleanup_close_ int fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
| 1635 |
if (fd < 0) |
| 1636 |
return log_debug_errno(fd, "Failed to open /dev/console to print %s OSC, ignoring: %m", type); |
| 1637 |
|
| 1638 |
_cleanup_free_ char *seq = NULL; |
| 1639 |
if (streq(type, "boot")) |
| 1640 |
r = osc_context_open_boot(&seq); |
| 1641 |
else |
| 1642 |
r = osc_context_close(SD_ID128_ALLF, &seq); |
| 1643 |
if (r < 0) |
| 1644 |
return log_debug_errno(r, "Failed to acquire %s OSC sequence, ignoring: %m", type); |
| 1645 |
|
| 1646 |
r = loop_write(fd, seq, SIZE_MAX); |
| 1647 |
if (r < 0) |
| 1648 |
return log_debug_errno(r, "Failed to write %s OSC sequence, ignoring: %m", type); |
| 1649 |
|
| 1650 |
if (DEBUG_LOGGING) { |
| 1651 |
_cleanup_free_ char *h = cescape(seq); |
| 1652 |
log_debug("OSC sequence for %s successfully written: %s", type, strna(h)); |
| 1653 |
} |
| 1654 |
|
| 1655 |
return 0; |
| 1656 |
} |
| 1657 |
|
| 1658 |
static int bump_unix_max_dgram_qlen(void) { |
| 1659 |
_cleanup_free_ char *qlen = NULL; |
| 1660 |
unsigned long v; |
| 1661 |
int r; |
| 1662 |
|
| 1663 |
/* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel default of 16 is simply too low. We set |
| 1664 |
* the value really really early during boot, so that it is actually applied to all our sockets, |
| 1665 |
* including the $NOTIFY_SOCKET one. */ |
| 1666 |
|
| 1667 |
r = read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen); |
| 1668 |
if (r < 0) |
| 1669 |
return log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, |
| 1670 |
"Failed to read AF_UNIX datagram queue length, ignoring: %m"); |
| 1671 |
|
| 1672 |
r = safe_atolu(qlen, &v); |
| 1673 |
if (r < 0) |
| 1674 |
return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length '%s', ignoring: %m", qlen); |
| 1675 |
|
| 1676 |
if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN) |
| 1677 |
return 0; |
| 1678 |
|
| 1679 |
r = sysctl_write("net/unix/max_dgram_qlen", STRINGIFY(DEFAULT_UNIX_MAX_DGRAM_QLEN)); |
| 1680 |
if (r < 0) |
| 1681 |
return log_full_errno(ERRNO_IS_NEG_FS_WRITE_REFUSED(r) ? LOG_DEBUG : LOG_WARNING, r, |
| 1682 |
"Failed to bump AF_UNIX datagram queue length, ignoring: %m"); |
| 1683 |
|
| 1684 |
return 1; |
| 1685 |
} |
| 1686 |
|
| 1687 |
static int fixup_environment(void) { |
| 1688 |
int r; |
| 1689 |
|
| 1690 |
/* Only fix up the environment when we are started as PID 1 */ |
| 1691 |
if (getpid_cached() != 1) |
| 1692 |
return 0; |
| 1693 |
|
| 1694 |
/* We expect the environment to be set correctly if run inside a container. */ |
| 1695 |
if (detect_container() > 0) |
| 1696 |
return 0; |
| 1697 |
|
| 1698 |
/* When started as PID1, the kernel uses /dev/console for our stdios and uses TERM=linux whatever the |
| 1699 |
* backend device used by the console. We try to make a better guess here since some consoles might |
| 1700 |
* not have support for color mode for example. |
| 1701 |
* |
| 1702 |
* However if TERM was configured through the kernel command line then leave it alone. */ |
| 1703 |
_cleanup_free_ char *term = NULL; |
| 1704 |
r = proc_cmdline_get_key("TERM", 0, &term); |
| 1705 |
if (r < 0) |
| 1706 |
return r; |
| 1707 |
if (r > 0) { |
| 1708 |
/* If we pick up $TERM, then also pick up $COLORTERM, $NO_COLOR */ |
| 1709 |
FOREACH_STRING(v, "COLORTERM", "NO_COLOR") { |
| 1710 |
_cleanup_free_ char *vv = NULL; |
| 1711 |
r = proc_cmdline_get_key(v, 0, &vv); |
| 1712 |
if (r < 0) |
| 1713 |
return r; |
| 1714 |
if (r > 0 && setenv(v, vv, /* overwrite= */ true) < 0) |
| 1715 |
return -errno; |
| 1716 |
} |
| 1717 |
} else { |
| 1718 |
/* If no $TERM is set then look for the per-tty variable instead */ |
| 1719 |
r = proc_cmdline_get_key("systemd.tty.term.console", 0, &term); |
| 1720 |
if (r < 0) |
| 1721 |
return r; |
| 1722 |
} |
| 1723 |
|
| 1724 |
if (!term) |
| 1725 |
(void) query_term_for_tty("/dev/console", &term); |
| 1726 |
|
| 1727 |
if (setenv("TERM", term ?: FALLBACK_TERM, /* overwrite= */ true) < 0) |
| 1728 |
return -errno; |
| 1729 |
|
| 1730 |
/* The kernels sets HOME=/ for init. Let's undo this. */ |
| 1731 |
if (path_equal(getenv("HOME"), "/")) |
| 1732 |
assert_se(unsetenv("HOME") == 0); |
| 1733 |
|
| 1734 |
return 0; |
| 1735 |
} |
| 1736 |
|
| 1737 |
static int become_shutdown(int objective, int retval) { |
| 1738 |
static const char* const table[_MANAGER_OBJECTIVE_MAX] = { |
| 1739 |
[MANAGER_EXIT] = "exit", |
| 1740 |
[MANAGER_REBOOT] = "reboot", |
| 1741 |
[MANAGER_POWEROFF] = "poweroff", |
| 1742 |
[MANAGER_HALT] = "halt", |
| 1743 |
[MANAGER_KEXEC] = "kexec", |
| 1744 |
}; |
| 1745 |
|
| 1746 |
char timeout[STRLEN("--timeout=") + DECIMAL_STR_MAX(usec_t) + STRLEN("us")], |
| 1747 |
exit_code[STRLEN("--exit-code=") + DECIMAL_STR_MAX(uint8_t)]; |
| 1748 |
|
| 1749 |
_cleanup_strv_free_ char **env_block = NULL; |
| 1750 |
_cleanup_free_ char *max_log_levels = NULL; |
| 1751 |
usec_t watchdog_timer = 0; |
| 1752 |
int r; |
| 1753 |
|
| 1754 |
assert(objective >= 0 && objective < _MANAGER_OBJECTIVE_MAX); |
| 1755 |
assert(table[objective]); |
| 1756 |
|
| 1757 |
xsprintf(timeout, "--timeout=%" PRI_USEC "us", arg_defaults.timeout_stop_usec); |
| 1758 |
|
| 1759 |
const char* command_line[11] = { |
| 1760 |
SYSTEMD_SHUTDOWN_BINARY_PATH, |
| 1761 |
table[objective], |
| 1762 |
timeout, |
| 1763 |
/* Note that the last position is a terminator and must contain NULL. */ |
| 1764 |
}; |
| 1765 |
size_t pos = 3; |
| 1766 |
|
| 1767 |
assert(command_line[pos-1]); |
| 1768 |
assert(!command_line[pos]); |
| 1769 |
|
| 1770 |
(void) log_max_levels_to_string(log_get_max_level(), &max_log_levels); |
| 1771 |
|
| 1772 |
if (max_log_levels) { |
| 1773 |
command_line[pos++] = "--log-level"; |
| 1774 |
command_line[pos++] = max_log_levels; |
| 1775 |
} |
| 1776 |
|
| 1777 |
switch (log_get_target()) { |
| 1778 |
|
| 1779 |
case LOG_TARGET_KMSG: |
| 1780 |
case LOG_TARGET_JOURNAL_OR_KMSG: |
| 1781 |
case LOG_TARGET_SYSLOG_OR_KMSG: |
| 1782 |
command_line[pos++] = "--log-target=kmsg"; |
| 1783 |
break; |
| 1784 |
|
| 1785 |
case LOG_TARGET_NULL: |
| 1786 |
command_line[pos++] = "--log-target=null"; |
| 1787 |
break; |
| 1788 |
|
| 1789 |
case LOG_TARGET_CONSOLE: |
| 1790 |
default: |
| 1791 |
command_line[pos++] = "--log-target=console"; |
| 1792 |
}; |
| 1793 |
|
| 1794 |
if (log_get_show_color()) |
| 1795 |
command_line[pos++] = "--log-color"; |
| 1796 |
|
| 1797 |
if (log_get_show_location()) |
| 1798 |
command_line[pos++] = "--log-location"; |
| 1799 |
|
| 1800 |
if (log_get_show_time()) |
| 1801 |
command_line[pos++] = "--log-time"; |
| 1802 |
|
| 1803 |
xsprintf(exit_code, "--exit-code=%d", retval); |
| 1804 |
command_line[pos++] = exit_code; |
| 1805 |
|
| 1806 |
assert(pos < ELEMENTSOF(command_line)); |
| 1807 |
|
| 1808 |
/* The watchdog: */ |
| 1809 |
|
| 1810 |
if (objective == MANAGER_REBOOT) |
| 1811 |
watchdog_timer = arg_reboot_watchdog; |
| 1812 |
else if (objective == MANAGER_KEXEC) |
| 1813 |
watchdog_timer = arg_kexec_watchdog; |
| 1814 |
|
| 1815 |
/* If we reboot or kexec let's set the shutdown watchdog and tell the |
| 1816 |
* shutdown binary to repeatedly ping it. |
| 1817 |
* Disable the pretimeout watchdog, as we do not support it from the shutdown binary. */ |
| 1818 |
(void) watchdog_setup_pretimeout(0); |
| 1819 |
(void) watchdog_setup_pretimeout_governor(NULL); |
| 1820 |
r = watchdog_setup(watchdog_timer); |
| 1821 |
watchdog_close(/* disarm= */ r < 0); |
| 1822 |
|
| 1823 |
/* The environment block: */ |
| 1824 |
|
| 1825 |
env_block = strv_copy(environ); |
| 1826 |
|
| 1827 |
/* Tell the binary how often to ping, ignore failure */ |
| 1828 |
(void) strv_extendf(&env_block, "WATCHDOG_USEC="USEC_FMT, watchdog_timer); |
| 1829 |
|
| 1830 |
/* Make sure that tools that look for $WATCHDOG_USEC (and might get started by the exitrd) don't get |
| 1831 |
* confused by the variable, because the sd_watchdog_enabled() protocol uses the same variable for |
| 1832 |
* the same purposes. */ |
| 1833 |
(void) strv_extendf(&env_block, "WATCHDOG_PID=" PID_FMT, getpid_cached()); |
| 1834 |
|
| 1835 |
if (arg_watchdog_device) |
| 1836 |
(void) strv_extendf(&env_block, "WATCHDOG_DEVICE=%s", arg_watchdog_device); |
| 1837 |
|
| 1838 |
if (arg_minimum_uptime_usec != USEC_INFINITY) |
| 1839 |
(void) strv_extendf(&env_block, "MINIMUM_UPTIME_USEC=" USEC_FMT, arg_minimum_uptime_usec); |
| 1840 |
|
| 1841 |
/* If we have a LUO serialization file, pass the fd to systemd-shutdown so it can |
| 1842 |
* preserve FD store entries across kexec via the kernel Live Update Orchestrator. */ |
| 1843 |
if (arg_luo_serialization) { |
| 1844 |
log_debug("Passing LUO serialization fd to systemd-shutdown."); |
| 1845 |
(void) strv_extendf(&env_block, "SYSTEMD_LUO_SERIALIZE_FD=%i", fileno(arg_luo_serialization)); |
| 1846 |
} |
| 1847 |
|
| 1848 |
(void) write_boot_or_shutdown_osc("shutdown"); |
| 1849 |
|
| 1850 |
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block); |
| 1851 |
return -errno; |
| 1852 |
} |
| 1853 |
|
| 1854 |
static void initialize_clock_timewarp(void) { |
| 1855 |
int r; |
| 1856 |
|
| 1857 |
/* This is called very early on, before we parse the kernel command line or otherwise figure out why |
| 1858 |
* we are running, but only once. */ |
| 1859 |
|
| 1860 |
if (clock_is_localtime(NULL) > 0) { |
| 1861 |
int min; |
| 1862 |
|
| 1863 |
/* The very first call of settimeofday() also does a time warp in the kernel. |
| 1864 |
* |
| 1865 |
* In the rtc-in-local time mode, we set the kernel's timezone, and rely on external tools to |
| 1866 |
* take care of maintaining the RTC and do all adjustments. This matches the behavior of |
| 1867 |
* Windows, which leaves the RTC alone if the registry tells that the RTC runs in UTC. |
| 1868 |
*/ |
| 1869 |
r = clock_set_timezone(&min); |
| 1870 |
if (r < 0) |
| 1871 |
log_error_errno(r, "Failed to apply local time delta, ignoring: %m"); |
| 1872 |
else |
| 1873 |
log_info("RTC configured in localtime, applying delta of %i minutes to system time.", min); |
| 1874 |
|
| 1875 |
} else if (!in_initrd()) |
| 1876 |
/* |
| 1877 |
* Do a dummy very first call to seal the kernel's time warp magic. |
| 1878 |
* |
| 1879 |
* Do not call this from inside the initrd. The initrd might not carry /etc/adjtime with |
| 1880 |
* LOCAL, but the real system could be set up that way. In such case, we need to delay the |
| 1881 |
* time-warp or the sealing until we reach the real system. |
| 1882 |
* |
| 1883 |
* Do no set the kernel's timezone. The concept of local time cannot be supported reliably, |
| 1884 |
* the time will jump or be incorrect at every daylight saving time change. All kernel local |
| 1885 |
* time concepts will be treated as UTC that way. |
| 1886 |
*/ |
| 1887 |
(void) clock_reset_timewarp(); |
| 1888 |
} |
| 1889 |
|
| 1890 |
static void apply_clock_update(void) { |
| 1891 |
/* This is called later than clock_apply_epoch(), i.e. after we have parsed |
| 1892 |
* configuration files/kernel command line and such. */ |
| 1893 |
|
| 1894 |
if (arg_clock_usec == 0) |
| 1895 |
return; |
| 1896 |
|
| 1897 |
if (getpid_cached() != 1) |
| 1898 |
return; |
| 1899 |
|
| 1900 |
if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(arg_clock_usec)) < 0) |
| 1901 |
log_error_errno(errno, "Failed to set system clock to time specified on kernel command line: %m"); |
| 1902 |
else |
| 1903 |
log_info("Set system clock to %s, as specified on the kernel command line.", |
| 1904 |
FORMAT_TIMESTAMP(arg_clock_usec)); |
| 1905 |
} |
| 1906 |
|
| 1907 |
static void cmdline_take_random_seed(void) { |
| 1908 |
size_t suggested; |
| 1909 |
int r; |
| 1910 |
|
| 1911 |
if (arg_random_seed_size == 0) |
| 1912 |
return; |
| 1913 |
|
| 1914 |
if (getpid_cached() != 1) |
| 1915 |
return; |
| 1916 |
|
| 1917 |
assert(arg_random_seed); |
| 1918 |
suggested = random_pool_size(); |
| 1919 |
|
| 1920 |
if (arg_random_seed_size < suggested) |
| 1921 |
log_warning("Random seed specified on kernel command line has size %zu, but %zu bytes required to fill entropy pool.", |
| 1922 |
arg_random_seed_size, suggested); |
| 1923 |
|
| 1924 |
r = random_write_entropy(-1, arg_random_seed, arg_random_seed_size, true); |
| 1925 |
if (r < 0) { |
| 1926 |
log_warning_errno(r, "Failed to credit entropy specified on kernel command line, ignoring: %m"); |
| 1927 |
return; |
| 1928 |
} |
| 1929 |
|
| 1930 |
log_notice("Successfully credited entropy passed on kernel command line.\n" |
| 1931 |
"Note that the seed provided this way is accessible to unprivileged programs. " |
| 1932 |
"This functionality should not be used outside of testing environments."); |
| 1933 |
} |
| 1934 |
|
| 1935 |
static void initialize_coredump(bool skip_setup) { |
| 1936 |
if (getpid_cached() != 1) |
| 1937 |
return; |
| 1938 |
|
| 1939 |
/* Don't limit the core dump size, so that coredump handlers such as systemd-coredump (which honour |
| 1940 |
* the limit) will process core dumps for system services by default. */ |
| 1941 |
if (setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)) < 0) |
| 1942 |
log_warning_errno(errno, "Failed to set RLIMIT_CORE: %m"); |
| 1943 |
|
| 1944 |
/* But at the same time, turn off the core_pattern logic by default, so that no coredumps are stored |
| 1945 |
* until the systemd-coredump tool is enabled via sysctl. However it can be changed via the kernel |
| 1946 |
* command line later so core dumps can still be generated during early startup and in initrd. */ |
| 1947 |
if (!skip_setup) |
| 1948 |
disable_coredumps(); |
| 1949 |
} |
| 1950 |
|
| 1951 |
static void initialize_core_pattern(bool skip_setup) { |
| 1952 |
int r; |
| 1953 |
|
| 1954 |
if (skip_setup || !arg_early_core_pattern) |
| 1955 |
return; |
| 1956 |
|
| 1957 |
if (getpid_cached() != 1) |
| 1958 |
return; |
| 1959 |
|
| 1960 |
r = write_string_file("/proc/sys/kernel/core_pattern", arg_early_core_pattern, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 1961 |
if (r < 0) |
| 1962 |
log_warning_errno(r, "Failed to write '%s' to /proc/sys/kernel/core_pattern, ignoring: %m", |
| 1963 |
arg_early_core_pattern); |
| 1964 |
} |
| 1965 |
|
| 1966 |
static void apply_protect_system(bool skip_setup) { |
| 1967 |
int r; |
| 1968 |
|
| 1969 |
if (skip_setup || getpid_cached() != 1 || arg_protect_system == 0) |
| 1970 |
return; |
| 1971 |
|
| 1972 |
if (arg_protect_system < 0 && !in_initrd()) { |
| 1973 |
log_debug("ProtectSystem=auto selected, but not running in an initrd, skipping."); |
| 1974 |
return; |
| 1975 |
} |
| 1976 |
|
| 1977 |
r = make_mount_point("/usr"); |
| 1978 |
if (r < 0) { |
| 1979 |
log_warning_errno(r, "Failed to make /usr/ a mount point, ignoring: %m"); |
| 1980 |
return; |
| 1981 |
} |
| 1982 |
|
| 1983 |
if (mount_nofollow_verbose( |
| 1984 |
LOG_WARNING, |
| 1985 |
/* what= */ NULL, |
| 1986 |
"/usr", |
| 1987 |
/* fstype= */ NULL, |
| 1988 |
MS_BIND|MS_REMOUNT|MS_RDONLY, |
| 1989 |
/* options= */ NULL) < 0) |
| 1990 |
return; |
| 1991 |
|
| 1992 |
log_info("Successfully made /usr/ read-only."); |
| 1993 |
} |
| 1994 |
|
| 1995 |
static void update_cpu_affinity(bool skip_setup) { |
| 1996 |
_cleanup_free_ char *mask = NULL; |
| 1997 |
|
| 1998 |
if (skip_setup || !arg_cpu_affinity.set) |
| 1999 |
return; |
| 2000 |
|
| 2001 |
assert(arg_cpu_affinity.allocated > 0); |
| 2002 |
|
| 2003 |
mask = cpu_set_to_range_string(&arg_cpu_affinity); |
| 2004 |
log_debug("Setting CPU affinity to {%s}.", strnull(mask)); |
| 2005 |
|
| 2006 |
if (sched_setaffinity(0, arg_cpu_affinity.allocated, arg_cpu_affinity.set) < 0) |
| 2007 |
log_warning_errno(errno, "Failed to set CPU affinity, ignoring: %m"); |
| 2008 |
} |
| 2009 |
|
| 2010 |
static void update_numa_policy(bool skip_setup) { |
| 2011 |
int r; |
| 2012 |
_cleanup_free_ char *nodes = NULL; |
| 2013 |
const char * policy = NULL; |
| 2014 |
|
| 2015 |
if (skip_setup || !mpol_is_valid(numa_policy_get_type(&arg_numa_policy))) |
| 2016 |
return; |
| 2017 |
|
| 2018 |
if (DEBUG_LOGGING) { |
| 2019 |
policy = mpol_to_string(numa_policy_get_type(&arg_numa_policy)); |
| 2020 |
nodes = cpu_set_to_range_string(&arg_numa_policy.nodes); |
| 2021 |
log_debug("Setting NUMA policy to %s, with nodes {%s}.", strnull(policy), strnull(nodes)); |
| 2022 |
} |
| 2023 |
|
| 2024 |
r = apply_numa_policy(&arg_numa_policy); |
| 2025 |
if (r == -EOPNOTSUPP) |
| 2026 |
log_debug_errno(r, "NUMA support not available, ignoring."); |
| 2027 |
else if (r < 0) |
| 2028 |
log_warning_errno(r, "Failed to set NUMA memory policy, ignoring: %m"); |
| 2029 |
} |
| 2030 |
|
| 2031 |
static void filter_args( |
| 2032 |
const char* dst[], |
| 2033 |
size_t *dst_index, |
| 2034 |
char **src, |
| 2035 |
int argc) { |
| 2036 |
|
| 2037 |
assert(dst); |
| 2038 |
assert(dst_index); |
| 2039 |
|
| 2040 |
/* Copy some filtered arguments into the dst array from src. */ |
| 2041 |
for (int i = 1; i < argc; i++) { |
| 2042 |
if (STR_IN_SET(src[i], |
| 2043 |
"--switched-root", |
| 2044 |
"--system", |
| 2045 |
"--user")) |
| 2046 |
continue; |
| 2047 |
|
| 2048 |
if (startswith(src[i], "--deserialize=")) |
| 2049 |
continue; |
| 2050 |
if (streq(src[i], "--deserialize")) { |
| 2051 |
i++; /* Skip the argument too */ |
| 2052 |
continue; |
| 2053 |
} |
| 2054 |
|
| 2055 |
/* Skip target unit designators. We already acted upon this information and have queued |
| 2056 |
* appropriate jobs. We don't want to redo all this after reexecution. */ |
| 2057 |
if (startswith(src[i], "--unit=")) |
| 2058 |
continue; |
| 2059 |
if (streq(src[i], "--unit")) { |
| 2060 |
i++; /* Skip the argument too */ |
| 2061 |
continue; |
| 2062 |
} |
| 2063 |
|
| 2064 |
/* Seems we have a good old option. Let's pass it over to the new instance. */ |
| 2065 |
dst[(*dst_index)++] = src[i]; |
| 2066 |
} |
| 2067 |
} |
| 2068 |
|
| 2069 |
static void finish_remaining_processes(ManagerObjective objective) { |
| 2070 |
assert(objective >= 0 && objective < _MANAGER_OBJECTIVE_MAX); |
| 2071 |
|
| 2072 |
/* Kill all remaining processes from the initrd, but don't wait for them, so that we can handle the |
| 2073 |
* SIGCHLD for them after deserializing. */ |
| 2074 |
if (IN_SET(objective, MANAGER_SWITCH_ROOT, MANAGER_SOFT_REBOOT)) |
| 2075 |
broadcast_signal(SIGTERM, /* wait_for_exit= */ false, /* send_sighup= */ true, arg_defaults.timeout_stop_usec); |
| 2076 |
|
| 2077 |
/* On soft reboot really make sure nothing is left. Note that this will skip cgroups |
| 2078 |
* of units that were configured with SurviveFinalKillSignal=yes. */ |
| 2079 |
if (objective == MANAGER_SOFT_REBOOT) |
| 2080 |
broadcast_signal(SIGKILL, /* wait_for_exit= */ false, /* send_sighup= */ false, arg_defaults.timeout_stop_usec); |
| 2081 |
} |
| 2082 |
|
| 2083 |
static void reduce_vt(ManagerObjective objective) { |
| 2084 |
int r; |
| 2085 |
|
| 2086 |
if (objective != MANAGER_SOFT_REBOOT) |
| 2087 |
return; |
| 2088 |
|
| 2089 |
/* Switches back to VT 1, and releases all other VTs, in an attempt to return to a situation similar |
| 2090 |
* to how it was during the original kernel initialization. This is important because if some random |
| 2091 |
* TTY is in foreground, /dev/console will end up pointing to it, where the future init system will |
| 2092 |
* then write its status output to, but where it probably shouldn't be writing to. */ |
| 2093 |
|
| 2094 |
r = chvt(1); |
| 2095 |
if (r < 0) |
| 2096 |
log_debug_errno(r, "Failed to switch to VT TTY 1, ignoring: %m"); |
| 2097 |
|
| 2098 |
_cleanup_close_ int tty0_fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); |
| 2099 |
if (tty0_fd < 0) |
| 2100 |
return (void) log_debug_errno(tty0_fd, "Failed to open '/dev/tty0', ignoring: %m"); |
| 2101 |
|
| 2102 |
for (int ttynr = 2; ttynr <= VTNR_MAX; ttynr++) |
| 2103 |
if (ioctl(tty0_fd, VT_DISALLOCATE, ttynr) < 0) |
| 2104 |
log_debug_errno(errno, "Failed to disallocate VT TTY %i, ignoring: %m", ttynr); |
| 2105 |
else |
| 2106 |
log_debug("Successfully disallocated VT TTY %i.", ttynr); |
| 2107 |
} |
| 2108 |
|
| 2109 |
static int do_reexecute( |
| 2110 |
ManagerObjective objective, |
| 2111 |
int argc, |
| 2112 |
char* argv[], |
| 2113 |
const struct rlimit *saved_rlimit_nofile, |
| 2114 |
const struct rlimit *saved_rlimit_memlock, |
| 2115 |
FDSet *fds, |
| 2116 |
const char *switch_root_dir, |
| 2117 |
const char *switch_root_init, |
| 2118 |
uint64_t saved_capability_ambient_set, |
| 2119 |
const char **ret_error_message) { |
| 2120 |
|
| 2121 |
size_t i, args_size; |
| 2122 |
const char **args; |
| 2123 |
int r; |
| 2124 |
|
| 2125 |
assert(IN_SET(objective, MANAGER_REEXECUTE, MANAGER_SWITCH_ROOT, MANAGER_SOFT_REBOOT)); |
| 2126 |
assert(argc >= 0); |
| 2127 |
assert(saved_rlimit_nofile); |
| 2128 |
assert(saved_rlimit_memlock); |
| 2129 |
assert(ret_error_message); |
| 2130 |
|
| 2131 |
/* Close and disarm the watchdog, so that the new instance can reinitialize it, but the machine |
| 2132 |
* doesn't get rebooted while we do that. */ |
| 2133 |
watchdog_close(/* disarm= */ true); |
| 2134 |
|
| 2135 |
if (!switch_root_dir && objective == MANAGER_SOFT_REBOOT) { |
| 2136 |
/* If no switch root dir is specified, then check if /run/nextroot/ qualifies and use that */ |
| 2137 |
r = path_is_os_tree("/run/nextroot"); |
| 2138 |
if (r < 0 && r != -ENOENT) |
| 2139 |
log_debug_errno(r, "Failed to determine if /run/nextroot/ is a valid OS tree, ignoring: %m"); |
| 2140 |
else if (r > 0) |
| 2141 |
switch_root_dir = "/run/nextroot"; |
| 2142 |
} |
| 2143 |
|
| 2144 |
if (switch_root_dir) { |
| 2145 |
/* If we're supposed to switch root, preemptively check the existence of a usable init. |
| 2146 |
* Otherwise the system might end up in a completely undebuggable state afterwards. */ |
| 2147 |
if (switch_root_init) { |
| 2148 |
r = chase_and_access(switch_root_init, switch_root_dir, CHASE_PREFIX_ROOT, X_OK, /* ret_path= */ NULL); |
| 2149 |
if (r < 0) |
| 2150 |
log_warning_errno(r, "Failed to chase configured init %s/%s: %m", |
| 2151 |
switch_root_dir, switch_root_init); |
| 2152 |
} else { |
| 2153 |
r = chase_and_access(SYSTEMD_BINARY_PATH, switch_root_dir, CHASE_PREFIX_ROOT, X_OK, /* ret_path= */ NULL); |
| 2154 |
if (r < 0) |
| 2155 |
log_debug_errno(r, "Failed to chase our own binary %s/%s: %m", |
| 2156 |
switch_root_dir, SYSTEMD_BINARY_PATH); |
| 2157 |
} |
| 2158 |
|
| 2159 |
if (r < 0) { |
| 2160 |
r = chase_and_access("/sbin/init", switch_root_dir, CHASE_PREFIX_ROOT, X_OK, /* ret_path= */ NULL); |
| 2161 |
if (r < 0) { |
| 2162 |
*ret_error_message = "Switch root target contains no usable init"; |
| 2163 |
return log_error_errno(r, "Failed to chase %s/sbin/init", switch_root_dir); |
| 2164 |
} |
| 2165 |
} |
| 2166 |
} |
| 2167 |
|
| 2168 |
/* Reset RLIMIT_NOFILE + RLIMIT_MEMLOCK back to the kernel defaults, so that the new systemd can pass |
| 2169 |
* the kernel default to its child processes */ |
| 2170 |
if (saved_rlimit_nofile->rlim_cur != 0) |
| 2171 |
(void) setrlimit(RLIMIT_NOFILE, saved_rlimit_nofile); |
| 2172 |
if (saved_rlimit_memlock->rlim_cur != RLIM_INFINITY) |
| 2173 |
(void) setrlimit(RLIMIT_MEMLOCK, saved_rlimit_memlock); |
| 2174 |
|
| 2175 |
finish_remaining_processes(objective); |
| 2176 |
reduce_vt(objective); |
| 2177 |
|
| 2178 |
if (switch_root_dir) { |
| 2179 |
r = switch_root(/* new_root= */ switch_root_dir, |
| 2180 |
/* old_root_after= */ NULL, |
| 2181 |
/* flags= */ (objective == MANAGER_SWITCH_ROOT ? SWITCH_ROOT_DESTROY_OLD_ROOT : 0) | |
| 2182 |
(objective == MANAGER_SOFT_REBOOT ? 0 : SWITCH_ROOT_RECURSIVE_RUN)); |
| 2183 |
if (r < 0) |
| 2184 |
log_error_errno(r, "Failed to switch root, trying to continue: %m"); |
| 2185 |
} |
| 2186 |
|
| 2187 |
r = capability_ambient_set_apply(saved_capability_ambient_set, /* also_inherit= */ false); |
| 2188 |
if (r < 0) |
| 2189 |
log_warning_errno(r, "Failed to apply the starting ambient set, ignoring: %m"); |
| 2190 |
|
| 2191 |
args_size = argc + 5; |
| 2192 |
args = newa(const char*, args_size); |
| 2193 |
|
| 2194 |
if (!switch_root_init) { |
| 2195 |
char sfd[STRLEN("--deserialize=") + DECIMAL_STR_MAX(int)]; |
| 2196 |
|
| 2197 |
/* First try to spawn ourselves with the right path, and with full serialization. We do this |
| 2198 |
* only if the user didn't specify an explicit init to spawn. */ |
| 2199 |
|
| 2200 |
assert(arg_serialization); |
| 2201 |
assert(fds); |
| 2202 |
|
| 2203 |
xsprintf(sfd, "--deserialize=%i", fileno(arg_serialization)); |
| 2204 |
|
| 2205 |
i = 1; /* Leave args[0] empty for now. */ |
| 2206 |
|
| 2207 |
/* Put our stuff first to make sure it always gets parsed in case |
| 2208 |
* we get weird stuff from the kernel cmdline (like --) */ |
| 2209 |
if (IN_SET(objective, MANAGER_SWITCH_ROOT, MANAGER_SOFT_REBOOT)) |
| 2210 |
args[i++] = "--switched-root"; |
| 2211 |
args[i++] = runtime_scope_cmdline_option_to_string(arg_runtime_scope); |
| 2212 |
args[i++] = sfd; |
| 2213 |
|
| 2214 |
filter_args(args, &i, argv, argc); |
| 2215 |
|
| 2216 |
args[i++] = NULL; |
| 2217 |
|
| 2218 |
assert(i <= args_size); |
| 2219 |
|
| 2220 |
/* |
| 2221 |
* We want valgrind to print its memory usage summary before reexecution. Valgrind won't do |
| 2222 |
* this is on its own on exec(), but it will do it on exit(). Hence, to ensure we get a |
| 2223 |
* summary here, fork() off a child, let it exit() cleanly, so that it prints the summary, |
| 2224 |
* and wait() for it in the parent, before proceeding into the exec(). |
| 2225 |
*/ |
| 2226 |
valgrind_summary_hack(); |
| 2227 |
|
| 2228 |
args[0] = SYSTEMD_BINARY_PATH; |
| 2229 |
(void) execv(args[0], (char* const*) args); |
| 2230 |
|
| 2231 |
if (objective == MANAGER_REEXECUTE) { |
| 2232 |
*ret_error_message = "Failed to execute our own binary"; |
| 2233 |
return log_error_errno(errno, "Failed to execute our own binary %s: %m", args[0]); |
| 2234 |
} |
| 2235 |
|
| 2236 |
log_debug_errno(errno, "Failed to execute our own binary %s, trying fallback: %m", args[0]); |
| 2237 |
} |
| 2238 |
|
| 2239 |
/* Try the fallback, if there is any, without any serialization. We pass the original argv[] and |
| 2240 |
* envp[]. (Well, modulo the ordering changes due to option parsing in argv[], and some cleanups in |
| 2241 |
* envp[], but let's hope that doesn't matter.) */ |
| 2242 |
|
| 2243 |
arg_serialization = safe_fclose(arg_serialization); |
| 2244 |
fds = fdset_free(fds); |
| 2245 |
|
| 2246 |
/* Drop /run/systemd directory. Some of its content can be used as a flag indicating that systemd is |
| 2247 |
* the init system but we might be replacing it with something different. If systemd is used again it |
| 2248 |
* will recreate the directory and its content anyway. */ |
| 2249 |
r = rm_rf("/run/systemd.pre-switch-root", REMOVE_ROOT|REMOVE_MISSING_OK); |
| 2250 |
if (r < 0) |
| 2251 |
log_warning_errno(r, "Failed to prepare /run/systemd.pre-switch-root/, ignoring: %m"); |
| 2252 |
|
| 2253 |
r = RET_NERRNO(rename("/run/systemd", "/run/systemd.pre-switch-root")); |
| 2254 |
if (r < 0) |
| 2255 |
log_warning_errno(r, "Failed to move /run/systemd/ to /run/systemd.pre-switch-root/, ignoring: %m"); |
| 2256 |
|
| 2257 |
/* Reopen the console */ |
| 2258 |
(void) make_console_stdio(); |
| 2259 |
|
| 2260 |
i = 1; /* Leave args[0] empty for now. */ |
| 2261 |
for (int j = 1; j <= argc; j++) |
| 2262 |
args[i++] = argv[j]; |
| 2263 |
assert(i <= args_size); |
| 2264 |
|
| 2265 |
/* Re-enable any blocked signals, especially important if we switch from initrd to init=... */ |
| 2266 |
(void) reset_all_signal_handlers(); |
| 2267 |
(void) reset_signal_mask(); |
| 2268 |
(void) rlimit_nofile_safe(); |
| 2269 |
|
| 2270 |
if (switch_root_init) { |
| 2271 |
args[0] = switch_root_init; |
| 2272 |
(void) execve(args[0], (char* const*) args, saved_env); |
| 2273 |
log_warning_errno(errno, "Failed to execute configured init %s, trying fallback: %m", args[0]); |
| 2274 |
} |
| 2275 |
|
| 2276 |
args[0] = "/sbin/init"; |
| 2277 |
(void) execv(args[0], (char* const*) args); |
| 2278 |
r = -errno; |
| 2279 |
*ret_error_message = "Failed to execute /sbin/init"; |
| 2280 |
|
| 2281 |
if (r == -ENOENT) { |
| 2282 |
manager_status_printf(NULL, STATUS_TYPE_EMERGENCY, |
| 2283 |
ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL, |
| 2284 |
"%s", *ret_error_message); |
| 2285 |
|
| 2286 |
log_warning_errno(r, "No /sbin/init, trying fallback shell"); |
| 2287 |
|
| 2288 |
args[0] = "/bin/sh"; |
| 2289 |
args[1] = NULL; |
| 2290 |
(void) execve(args[0], (char* const*) args, saved_env); |
| 2291 |
r = -errno; |
| 2292 |
*ret_error_message = "Failed to execute fallback shell"; |
| 2293 |
} |
| 2294 |
|
| 2295 |
return log_error_errno(r, "%s, giving up: %m", *ret_error_message); |
| 2296 |
} |
| 2297 |
|
| 2298 |
static int invoke_main_loop( |
| 2299 |
Manager *m, |
| 2300 |
const struct rlimit *saved_rlimit_nofile, |
| 2301 |
const struct rlimit *saved_rlimit_memlock, |
| 2302 |
int *ret_retval, /* Return parameters relevant for shutting down */ |
| 2303 |
FDSet **ret_fds, /* Return parameters for reexecuting */ |
| 2304 |
char **ret_switch_root_dir, /* … */ |
| 2305 |
char **ret_switch_root_init, /* … */ |
| 2306 |
const char **ret_error_message) { |
| 2307 |
|
| 2308 |
int r; |
| 2309 |
|
| 2310 |
assert(m); |
| 2311 |
assert(saved_rlimit_nofile); |
| 2312 |
assert(saved_rlimit_memlock); |
| 2313 |
assert(ret_retval); |
| 2314 |
assert(ret_fds); |
| 2315 |
assert(ret_switch_root_dir); |
| 2316 |
assert(ret_switch_root_init); |
| 2317 |
assert(ret_error_message); |
| 2318 |
|
| 2319 |
for (;;) { |
| 2320 |
int objective = manager_loop(m); |
| 2321 |
if (objective < 0) { |
| 2322 |
*ret_error_message = "Failed to run main loop"; |
| 2323 |
return log_struct_errno(LOG_EMERG, objective, |
| 2324 |
LOG_MESSAGE("Failed to run main loop: %m"), |
| 2325 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_MAINLOOP_FAILED_STR)); |
| 2326 |
} |
| 2327 |
|
| 2328 |
/* Ensure shutdown timestamp is taken even when bypassing the job engine */ |
| 2329 |
if (IN_SET(objective, |
| 2330 |
MANAGER_SOFT_REBOOT, |
| 2331 |
MANAGER_REBOOT, |
| 2332 |
MANAGER_KEXEC, |
| 2333 |
MANAGER_HALT, |
| 2334 |
MANAGER_POWEROFF) && |
| 2335 |
!dual_timestamp_is_set(m->timestamps + MANAGER_TIMESTAMP_SHUTDOWN_START)) |
| 2336 |
dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_SHUTDOWN_START); |
| 2337 |
|
| 2338 |
switch (objective) { |
| 2339 |
|
| 2340 |
case MANAGER_RELOAD: { |
| 2341 |
LogTarget saved_log_target; |
| 2342 |
int saved_log_level; |
| 2343 |
|
| 2344 |
manager_send_reloading(m); |
| 2345 |
|
| 2346 |
log_info("Reloading..."); |
| 2347 |
|
| 2348 |
/* First, save any overridden log level/target, then parse the configuration file, |
| 2349 |
* which might change the log level to new settings. */ |
| 2350 |
|
| 2351 |
saved_log_level = m->log_level_overridden ? log_get_max_level() : -1; |
| 2352 |
saved_log_target = m->log_target_overridden ? log_get_target() : _LOG_TARGET_INVALID; |
| 2353 |
|
| 2354 |
(void) parse_configuration(saved_rlimit_nofile, saved_rlimit_memlock); |
| 2355 |
|
| 2356 |
set_manager_defaults(m); |
| 2357 |
set_manager_settings(m); |
| 2358 |
|
| 2359 |
update_cpu_affinity(false); |
| 2360 |
update_numa_policy(false); |
| 2361 |
|
| 2362 |
if (saved_log_level >= 0) |
| 2363 |
manager_override_log_level(m, saved_log_level); |
| 2364 |
if (saved_log_target >= 0) |
| 2365 |
manager_override_log_target(m, saved_log_target); |
| 2366 |
|
| 2367 |
if (manager_reload(m) < 0) |
| 2368 |
/* Reloading failed before the point of no return. |
| 2369 |
* Let's continue running as if nothing happened. */ |
| 2370 |
m->objective = MANAGER_OK; |
| 2371 |
else |
| 2372 |
log_info("Reloading finished in " USEC_FMT " ms.", |
| 2373 |
usec_sub_unsigned(now(CLOCK_MONOTONIC), m->timestamps[MANAGER_TIMESTAMP_UNITS_LOAD].monotonic) / USEC_PER_MSEC); |
| 2374 |
|
| 2375 |
continue; |
| 2376 |
} |
| 2377 |
|
| 2378 |
case MANAGER_REEXECUTE: |
| 2379 |
|
| 2380 |
manager_send_reloading(m); /* From the perspective of the manager calling us this is |
| 2381 |
* pretty much the same as a reload */ |
| 2382 |
|
| 2383 |
r = prepare_reexecute(m, &arg_serialization, ret_fds, false); |
| 2384 |
if (r < 0) { |
| 2385 |
*ret_error_message = "Failed to prepare for reexecution"; |
| 2386 |
return r; |
| 2387 |
} |
| 2388 |
|
| 2389 |
log_notice("Reexecuting."); |
| 2390 |
|
| 2391 |
*ret_retval = EXIT_FAILURE; |
| 2392 |
*ret_switch_root_dir = *ret_switch_root_init = NULL; |
| 2393 |
|
| 2394 |
return objective; |
| 2395 |
|
| 2396 |
case MANAGER_SWITCH_ROOT: |
| 2397 |
|
| 2398 |
manager_send_reloading(m); /* From the perspective of the manager calling us this is |
| 2399 |
* pretty much the same as a reload */ |
| 2400 |
|
| 2401 |
manager_set_switching_root(m, true); |
| 2402 |
|
| 2403 |
if (!m->switch_root_init) { |
| 2404 |
r = prepare_reexecute(m, &arg_serialization, ret_fds, true); |
| 2405 |
if (r < 0) { |
| 2406 |
*ret_error_message = "Failed to prepare for reexecution"; |
| 2407 |
return r; |
| 2408 |
} |
| 2409 |
} else |
| 2410 |
*ret_fds = NULL; |
| 2411 |
|
| 2412 |
log_notice("Switching root."); |
| 2413 |
|
| 2414 |
*ret_retval = EXIT_FAILURE; |
| 2415 |
|
| 2416 |
/* Steal the switch root parameters */ |
| 2417 |
*ret_switch_root_dir = TAKE_PTR(m->switch_root); |
| 2418 |
*ret_switch_root_init = TAKE_PTR(m->switch_root_init); |
| 2419 |
|
| 2420 |
return objective; |
| 2421 |
|
| 2422 |
case MANAGER_SOFT_REBOOT: |
| 2423 |
manager_send_reloading(m); |
| 2424 |
manager_set_switching_root(m, true); |
| 2425 |
|
| 2426 |
r = prepare_reexecute(m, &arg_serialization, ret_fds, /* switching_root= */ true); |
| 2427 |
if (r < 0) { |
| 2428 |
*ret_error_message = "Failed to prepare for reexecution"; |
| 2429 |
return r; |
| 2430 |
} |
| 2431 |
|
| 2432 |
log_notice("Soft-rebooting."); |
| 2433 |
|
| 2434 |
*ret_retval = EXIT_FAILURE; |
| 2435 |
*ret_switch_root_dir = TAKE_PTR(m->switch_root); |
| 2436 |
*ret_switch_root_init = NULL; |
| 2437 |
|
| 2438 |
return objective; |
| 2439 |
|
| 2440 |
case MANAGER_EXIT: |
| 2441 |
if (MANAGER_IS_USER(m)) { |
| 2442 |
log_debug("Exit."); |
| 2443 |
|
| 2444 |
*ret_retval = m->return_value; |
| 2445 |
*ret_fds = NULL; |
| 2446 |
*ret_switch_root_dir = *ret_switch_root_init = NULL; |
| 2447 |
|
| 2448 |
return objective; |
| 2449 |
} |
| 2450 |
|
| 2451 |
_fallthrough_; |
| 2452 |
case MANAGER_REBOOT: |
| 2453 |
case MANAGER_POWEROFF: |
| 2454 |
case MANAGER_HALT: |
| 2455 |
case MANAGER_KEXEC: { |
| 2456 |
log_notice("Shutting down."); |
| 2457 |
|
| 2458 |
*ret_retval = m->return_value; |
| 2459 |
*ret_fds = NULL; |
| 2460 |
*ret_switch_root_dir = *ret_switch_root_init = NULL; |
| 2461 |
|
| 2462 |
return objective; |
| 2463 |
} |
| 2464 |
|
| 2465 |
default: |
| 2466 |
assert_not_reached(); |
| 2467 |
} |
| 2468 |
} |
| 2469 |
} |
| 2470 |
|
| 2471 |
static void log_execution_mode(bool *ret_first_boot) { |
| 2472 |
bool first_boot = false; |
| 2473 |
int r; |
| 2474 |
|
| 2475 |
assert(ret_first_boot); |
| 2476 |
|
| 2477 |
switch (arg_runtime_scope) { |
| 2478 |
|
| 2479 |
case RUNTIME_SCOPE_SYSTEM: { |
| 2480 |
struct utsname uts; |
| 2481 |
int v; |
| 2482 |
|
| 2483 |
log_info("systemd " GIT_VERSION " running in %ssystem mode (%s)", |
| 2484 |
arg_action == ACTION_TEST ? "test " : "", |
| 2485 |
systemd_features); |
| 2486 |
|
| 2487 |
v = detect_virtualization(); |
| 2488 |
if (v > 0) |
| 2489 |
log_info("Detected virtualization %s.", virtualization_to_string(v)); |
| 2490 |
|
| 2491 |
v = detect_confidential_virtualization(); |
| 2492 |
if (v > 0) |
| 2493 |
log_info("Detected confidential virtualization %s.", confidential_virtualization_to_string(v)); |
| 2494 |
|
| 2495 |
log_info("Detected architecture %s.", architecture_to_string(uname_architecture())); |
| 2496 |
|
| 2497 |
if (in_initrd()) |
| 2498 |
log_info("Running in initrd."); |
| 2499 |
else { |
| 2500 |
_cleanup_free_ char *id_text = NULL; |
| 2501 |
|
| 2502 |
/* Let's check whether we are in first boot. First, check if an override was |
| 2503 |
* specified on the kernel command line. If yes, we honour that. */ |
| 2504 |
|
| 2505 |
r = proc_cmdline_get_bool("systemd.condition_first_boot", /* flags= */ 0, &first_boot); |
| 2506 |
if (r < 0) |
| 2507 |
log_debug_errno(r, "Failed to parse systemd.condition_first_boot= kernel command line argument, ignoring: %m"); |
| 2508 |
|
| 2509 |
if (r > 0) |
| 2510 |
log_full(first_boot ? LOG_INFO : LOG_DEBUG, |
| 2511 |
"Kernel command line argument says we are %s first boot.", |
| 2512 |
first_boot ? "in" : "not in"); |
| 2513 |
else { |
| 2514 |
/* Second, perform autodetection. We use /etc/machine-id as flag file for |
| 2515 |
* this: If it is missing or contains the value "uninitialized", this is the |
| 2516 |
* first boot. In other cases, it is not. This allows container managers and |
| 2517 |
* installers to provision a couple of files in /etc but still permit the |
| 2518 |
* first-boot initialization to occur. If the container manager wants to |
| 2519 |
* provision the machine ID it should pass $container_uuid to PID 1. */ |
| 2520 |
|
| 2521 |
r = read_one_line_file("/etc/machine-id", &id_text); |
| 2522 |
if (r < 0 || streq(id_text, "uninitialized")) { |
| 2523 |
if (r < 0 && r != -ENOENT) |
| 2524 |
log_warning_errno(r, "Unexpected error while reading /etc/machine-id, assuming first boot: %m"); |
| 2525 |
|
| 2526 |
first_boot = true; |
| 2527 |
log_info("Detected first boot."); |
| 2528 |
} else |
| 2529 |
log_debug("Detected initialized system, this is not the first boot."); |
| 2530 |
} |
| 2531 |
} |
| 2532 |
|
| 2533 |
assert_se(uname(&uts) >= 0); |
| 2534 |
|
| 2535 |
if (strverscmp_improved(uts.release, KERNEL_BASELINE_VERSION) < 0) |
| 2536 |
log_warning("Warning! Reported kernel version %s is older than systemd's required baseline kernel version %s. " |
| 2537 |
"Your mileage may vary.", uts.release, KERNEL_BASELINE_VERSION); |
| 2538 |
else |
| 2539 |
log_debug("Kernel version %s, our baseline is %s", uts.release, KERNEL_BASELINE_VERSION); |
| 2540 |
|
| 2541 |
break; |
| 2542 |
} |
| 2543 |
|
| 2544 |
case RUNTIME_SCOPE_USER: |
| 2545 |
if (DEBUG_LOGGING) { |
| 2546 |
_cleanup_free_ char *t = NULL; |
| 2547 |
|
| 2548 |
t = uid_to_name(getuid()); |
| 2549 |
log_debug("systemd " GIT_VERSION " running in %suser mode for user " UID_FMT "/%s. (%s)", |
| 2550 |
arg_action == ACTION_TEST ? " test" : "", |
| 2551 |
getuid(), strna(t), systemd_features); |
| 2552 |
} |
| 2553 |
|
| 2554 |
break; |
| 2555 |
|
| 2556 |
default: |
| 2557 |
assert_not_reached(); |
| 2558 |
} |
| 2559 |
|
| 2560 |
*ret_first_boot = first_boot; |
| 2561 |
} |
| 2562 |
|
| 2563 |
static int initialize_runtime( |
| 2564 |
bool skip_setup, |
| 2565 |
bool first_boot, |
| 2566 |
struct rlimit *saved_rlimit_nofile, |
| 2567 |
struct rlimit *saved_rlimit_memlock, |
| 2568 |
uint64_t *saved_ambient_set, |
| 2569 |
const char **ret_error_message) { |
| 2570 |
|
| 2571 |
int r; |
| 2572 |
|
| 2573 |
assert(saved_ambient_set); |
| 2574 |
assert(ret_error_message); |
| 2575 |
|
| 2576 |
/* Sets up various runtime parameters. Many of these initializations are conditionalized: |
| 2577 |
* |
| 2578 |
* - Some only apply to --system instances |
| 2579 |
* - Some only apply to --user instances |
| 2580 |
* - Some only apply when we first start up, but not when we reexecute |
| 2581 |
*/ |
| 2582 |
|
| 2583 |
if (arg_action != ACTION_RUN) |
| 2584 |
return 0; |
| 2585 |
|
| 2586 |
update_cpu_affinity(skip_setup); |
| 2587 |
update_numa_policy(skip_setup); |
| 2588 |
|
| 2589 |
switch (arg_runtime_scope) { |
| 2590 |
|
| 2591 |
case RUNTIME_SCOPE_SYSTEM: |
| 2592 |
/* Make sure we leave a core dump without panicking the kernel. */ |
| 2593 |
install_crash_handler(); |
| 2594 |
|
| 2595 |
if (!skip_setup) { |
| 2596 |
/* Check that /usr/ is either on the same file system as / or mounted already. */ |
| 2597 |
if (dir_is_empty("/usr", /* ignore_hidden_or_backup= */ true) > 0) { |
| 2598 |
*ret_error_message = "Refusing to run in unsupported environment where /usr/ is not populated"; |
| 2599 |
return -ENOEXEC; |
| 2600 |
} |
| 2601 |
|
| 2602 |
/* Pull credentials from various sources into a common credential directory (we do |
| 2603 |
* this here, before setting up the machine ID, so that we can use credential info |
| 2604 |
* for setting up the machine ID) */ |
| 2605 |
(void) import_credentials(); |
| 2606 |
|
| 2607 |
(void) os_release_status(); |
| 2608 |
(void) machine_id_setup(/* root= */ NULL, arg_machine_id, |
| 2609 |
(first_boot ? MACHINE_ID_SETUP_FORCE_TRANSIENT : 0) | |
| 2610 |
(arg_machine_id_from_firmware ? MACHINE_ID_SETUP_FORCE_FIRMWARE : 0), |
| 2611 |
/* ret= */ NULL); |
| 2612 |
(void) hostname_setup(/* really= */ true); |
| 2613 |
(void) loopback_setup(); |
| 2614 |
|
| 2615 |
bump_unix_max_dgram_qlen(); |
| 2616 |
bump_file_max_and_nr_open(); |
| 2617 |
|
| 2618 |
write_container_id(); |
| 2619 |
|
| 2620 |
(void) write_boot_or_shutdown_osc("boot"); |
| 2621 |
|
| 2622 |
/* Copy os-release to the propagate directory, so that we update it for services running |
| 2623 |
* under RootDirectory=/RootImage= when we do a soft reboot. */ |
| 2624 |
r = setup_os_release(RUNTIME_SCOPE_SYSTEM); |
| 2625 |
if (r < 0) |
| 2626 |
log_warning_errno(r, "Failed to copy os-release for propagation, ignoring: %m"); |
| 2627 |
} |
| 2628 |
|
| 2629 |
r = watchdog_set_device(arg_watchdog_device); |
| 2630 |
if (r < 0) |
| 2631 |
log_warning_errno(r, "Failed to set watchdog device to %s, ignoring: %m", arg_watchdog_device); |
| 2632 |
|
| 2633 |
if (!cap_test_all(arg_capability_bounding_set)) { |
| 2634 |
r = capability_bounding_set_drop_usermode(arg_capability_bounding_set); |
| 2635 |
if (r < 0) { |
| 2636 |
*ret_error_message = "Failed to drop capability bounding set of usermode helpers"; |
| 2637 |
return log_struct_errno(LOG_EMERG, r, |
| 2638 |
LOG_MESSAGE("Failed to drop capability bounding set of usermode helpers: %m"), |
| 2639 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_CAPABILITY_BOUNDING_USER_STR)); |
| 2640 |
} |
| 2641 |
|
| 2642 |
r = capability_bounding_set_drop(arg_capability_bounding_set, true); |
| 2643 |
if (r < 0) { |
| 2644 |
*ret_error_message = "Failed to drop capability bounding set"; |
| 2645 |
return log_struct_errno(LOG_EMERG, r, |
| 2646 |
LOG_MESSAGE("Failed to drop capability bounding set: %m"), |
| 2647 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_CAPABILITY_BOUNDING_STR)); |
| 2648 |
} |
| 2649 |
} |
| 2650 |
|
| 2651 |
if (arg_no_new_privs) { |
| 2652 |
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { |
| 2653 |
*ret_error_message = "Failed to disable new privileges"; |
| 2654 |
return log_struct_errno(LOG_EMERG, errno, |
| 2655 |
LOG_MESSAGE("Failed to disable new privileges: %m"), |
| 2656 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_DISABLE_PRIVILEGES_STR)); |
| 2657 |
} |
| 2658 |
} |
| 2659 |
|
| 2660 |
break; |
| 2661 |
|
| 2662 |
case RUNTIME_SCOPE_USER: { |
| 2663 |
_cleanup_free_ char *p = NULL; |
| 2664 |
|
| 2665 |
/* Create the runtime directory and place the inaccessible device nodes there, if we run in |
| 2666 |
* user mode. In system mode mount_setup() already did that. */ |
| 2667 |
|
| 2668 |
r = xdg_user_runtime_dir("/systemd", &p); |
| 2669 |
if (r < 0) { |
| 2670 |
*ret_error_message = "$XDG_RUNTIME_DIR is not set"; |
| 2671 |
return log_struct_errno(LOG_EMERG, r, |
| 2672 |
LOG_MESSAGE("Failed to determine $XDG_RUNTIME_DIR path: %m"), |
| 2673 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_NO_XDGDIR_PATH_STR)); |
| 2674 |
} |
| 2675 |
|
| 2676 |
if (!skip_setup) { |
| 2677 |
(void) mkdir_p_label(p, 0755); |
| 2678 |
(void) make_inaccessible_nodes(p, UID_INVALID, GID_INVALID); |
| 2679 |
|
| 2680 |
r = setup_os_release(RUNTIME_SCOPE_USER); |
| 2681 |
if (r < 0) |
| 2682 |
log_warning_errno(r, "Failed to copy os-release for propagation, ignoring: %m"); |
| 2683 |
} |
| 2684 |
|
| 2685 |
break; |
| 2686 |
} |
| 2687 |
|
| 2688 |
default: |
| 2689 |
assert_not_reached(); |
| 2690 |
} |
| 2691 |
|
| 2692 |
/* The two operations on the ambient set are meant for a user serssion manager. They do not affect |
| 2693 |
* system manager operation, because by default it starts with an empty ambient set. |
| 2694 |
* |
| 2695 |
* Preserve the ambient set for later use with sd-executor processes. */ |
| 2696 |
r = capability_get_ambient(saved_ambient_set); |
| 2697 |
if (r < 0) |
| 2698 |
log_warning_errno(r, "Failed to save ambient capabilities, ignoring: %m"); |
| 2699 |
|
| 2700 |
/* Clear ambient capabilities, so services do not inherit them implicitly. Dropping them does |
| 2701 |
* not affect the permitted and effective sets which are important for the manager itself to |
| 2702 |
* operate. */ |
| 2703 |
r = capability_ambient_set_apply(0, /* also_inherit= */ false); |
| 2704 |
if (r < 0) |
| 2705 |
log_warning_errno(r, "Failed to reset ambient capability set, ignoring: %m"); |
| 2706 |
|
| 2707 |
if (arg_timer_slack_nsec != NSEC_INFINITY) |
| 2708 |
if (prctl(PR_SET_TIMERSLACK, arg_timer_slack_nsec) < 0) |
| 2709 |
log_warning_errno(errno, "Failed to adjust timer slack, ignoring: %m"); |
| 2710 |
|
| 2711 |
if (arg_syscall_archs) { |
| 2712 |
r = enforce_syscall_archs(arg_syscall_archs); |
| 2713 |
if (r < 0) { |
| 2714 |
*ret_error_message = "Failed to set syscall architectures"; |
| 2715 |
return r; |
| 2716 |
} |
| 2717 |
} |
| 2718 |
|
| 2719 |
r = make_reaper_process(true); |
| 2720 |
if (r < 0) |
| 2721 |
log_warning_errno(r, "Failed to make us a subreaper, ignoring: %m"); |
| 2722 |
|
| 2723 |
/* Bump up RLIMIT_NOFILE for systemd itself */ |
| 2724 |
(void) bump_rlimit_nofile(saved_rlimit_nofile); |
| 2725 |
(void) bump_rlimit_memlock(saved_rlimit_memlock); |
| 2726 |
|
| 2727 |
return 0; |
| 2728 |
} |
| 2729 |
|
| 2730 |
static int do_queue_default_job( |
| 2731 |
Manager *m, |
| 2732 |
const char **ret_error_message) { |
| 2733 |
|
| 2734 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2735 |
const char *unit; |
| 2736 |
Job *job; |
| 2737 |
Unit *target; |
| 2738 |
int r; |
| 2739 |
|
| 2740 |
assert(ret_error_message); |
| 2741 |
|
| 2742 |
if (arg_default_unit) |
| 2743 |
unit = arg_default_unit; |
| 2744 |
else if (in_initrd()) |
| 2745 |
unit = SPECIAL_INITRD_TARGET; |
| 2746 |
else |
| 2747 |
unit = SPECIAL_DEFAULT_TARGET; |
| 2748 |
|
| 2749 |
log_debug("Activating default unit: %s", unit); |
| 2750 |
|
| 2751 |
r = manager_load_startable_unit_or_warn(m, unit, NULL, &target); |
| 2752 |
if (r < 0 && in_initrd() && !arg_default_unit) { |
| 2753 |
/* Fall back to default.target, which we used to always use by default. Only do this if no |
| 2754 |
* explicit configuration was given. */ |
| 2755 |
|
| 2756 |
log_info("Falling back to %s.", SPECIAL_DEFAULT_TARGET); |
| 2757 |
|
| 2758 |
r = manager_load_startable_unit_or_warn(m, SPECIAL_DEFAULT_TARGET, NULL, &target); |
| 2759 |
} |
| 2760 |
if (r < 0) { |
| 2761 |
log_info("Falling back to %s.", SPECIAL_RESCUE_TARGET); |
| 2762 |
|
| 2763 |
r = manager_load_startable_unit_or_warn(m, SPECIAL_RESCUE_TARGET, NULL, &target); |
| 2764 |
if (r < 0) { |
| 2765 |
*ret_error_message = r == -ERFKILL ? SPECIAL_RESCUE_TARGET " masked" |
| 2766 |
: "Failed to load " SPECIAL_RESCUE_TARGET; |
| 2767 |
return r; |
| 2768 |
} |
| 2769 |
} |
| 2770 |
|
| 2771 |
assert(target->load_state == UNIT_LOADED); |
| 2772 |
|
| 2773 |
r = manager_add_job(m, JOB_START, target, JOB_ISOLATE, &error, &job); |
| 2774 |
if (r == -EPERM) { |
| 2775 |
log_debug_errno(r, "Default target could not be isolated, starting instead: %s", bus_error_message(&error, r)); |
| 2776 |
|
| 2777 |
sd_bus_error_free(&error); |
| 2778 |
|
| 2779 |
r = manager_add_job(m, JOB_START, target, JOB_REPLACE, &error, &job); |
| 2780 |
if (r < 0) { |
| 2781 |
*ret_error_message = "Failed to start default target"; |
| 2782 |
return log_struct_errno(LOG_EMERG, r, |
| 2783 |
LOG_MESSAGE("Failed to start default target: %s", bus_error_message(&error, r)), |
| 2784 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_START_TARGET_FAILED_STR)); |
| 2785 |
} |
| 2786 |
|
| 2787 |
} else if (r < 0) { |
| 2788 |
*ret_error_message = "Failed to isolate default target"; |
| 2789 |
return log_struct_errno(LOG_EMERG, r, |
| 2790 |
LOG_MESSAGE("Failed to isolate default target: %s", bus_error_message(&error, r)), |
| 2791 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_ISOLATE_TARGET_FAILED_STR)); |
| 2792 |
} |
| 2793 |
|
| 2794 |
log_info("Queued %s job for default target %s.", |
| 2795 |
job_type_to_string(job->type), unit_status_string(job->unit, NULL)); |
| 2796 |
|
| 2797 |
return 0; |
| 2798 |
} |
| 2799 |
|
| 2800 |
static void save_rlimits(struct rlimit *saved_rlimit_nofile, |
| 2801 |
struct rlimit *saved_rlimit_memlock) { |
| 2802 |
|
| 2803 |
assert(saved_rlimit_nofile); |
| 2804 |
assert(saved_rlimit_memlock); |
| 2805 |
|
| 2806 |
if (getrlimit(RLIMIT_NOFILE, saved_rlimit_nofile) < 0) |
| 2807 |
log_warning_errno(errno, "Reading RLIMIT_NOFILE failed, ignoring: %m"); |
| 2808 |
|
| 2809 |
if (getrlimit(RLIMIT_MEMLOCK, saved_rlimit_memlock) < 0) |
| 2810 |
log_warning_errno(errno, "Reading RLIMIT_MEMLOCK failed, ignoring: %m"); |
| 2811 |
} |
| 2812 |
|
| 2813 |
static void fallback_rlimit_nofile(const struct rlimit *saved_rlimit_nofile) { |
| 2814 |
struct rlimit *rl; |
| 2815 |
|
| 2816 |
if (arg_defaults.rlimit[RLIMIT_NOFILE]) |
| 2817 |
return; |
| 2818 |
|
| 2819 |
/* Make sure forked processes get limits based on the original kernel setting */ |
| 2820 |
|
| 2821 |
rl = newdup(struct rlimit, saved_rlimit_nofile, 1); |
| 2822 |
if (!rl) { |
| 2823 |
log_oom(); |
| 2824 |
return; |
| 2825 |
} |
| 2826 |
|
| 2827 |
/* Bump the hard limit for system services to a substantially higher value. The default |
| 2828 |
* hard limit current kernels set is pretty low (4K), mostly for historical |
| 2829 |
* reasons. According to kernel developers, the fd handling in recent kernels has been |
| 2830 |
* optimized substantially enough, so that we can bump the limit now, without paying too |
| 2831 |
* high a price in memory or performance. Note however that we only bump the hard limit, |
| 2832 |
* not the soft limit. That's because select() works the way it works, and chokes on fds |
| 2833 |
* >= 1024. If we'd bump the soft limit globally, it might accidentally happen to |
| 2834 |
* unexpecting programs that they get fds higher than what they can process using |
| 2835 |
* select(). By only bumping the hard limit but leaving the low limit as it is we avoid |
| 2836 |
* this pitfall: programs that are written by folks aware of the select() problem in mind |
| 2837 |
* (and thus use poll()/epoll instead of select(), the way everybody should) can |
| 2838 |
* explicitly opt into high fds by bumping their soft limit beyond 1024, to the hard limit |
| 2839 |
* we pass. */ |
| 2840 |
if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM) |
| 2841 |
rl->rlim_max = MIN((rlim_t) read_nr_open(), MAX(rl->rlim_max, (rlim_t) HIGH_RLIMIT_NOFILE)); |
| 2842 |
|
| 2843 |
/* If for some reason we were invoked with a soft limit above 1024 (which should never |
| 2844 |
* happen!, but who knows what we get passed in from pam_limit when invoked as --user |
| 2845 |
* instance), then lower what we pass on to not confuse our children */ |
| 2846 |
rl->rlim_cur = MIN(rl->rlim_cur, (rlim_t) FD_SETSIZE); |
| 2847 |
|
| 2848 |
arg_defaults.rlimit[RLIMIT_NOFILE] = rl; |
| 2849 |
} |
| 2850 |
|
| 2851 |
static void fallback_rlimit_memlock(const struct rlimit *saved_rlimit_memlock) { |
| 2852 |
struct rlimit *rl; |
| 2853 |
|
| 2854 |
/* Pass the original value down to invoked processes */ |
| 2855 |
|
| 2856 |
if (arg_defaults.rlimit[RLIMIT_MEMLOCK]) |
| 2857 |
return; |
| 2858 |
|
| 2859 |
rl = newdup(struct rlimit, saved_rlimit_memlock, 1); |
| 2860 |
if (!rl) { |
| 2861 |
log_oom(); |
| 2862 |
return; |
| 2863 |
} |
| 2864 |
|
| 2865 |
if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM) { |
| 2866 |
/* Raise the default limit to 8M also on old kernels and in containers (8M is the kernel |
| 2867 |
* default for this since kernel 5.16) */ |
| 2868 |
rl->rlim_max = MAX(rl->rlim_max, (rlim_t) DEFAULT_RLIMIT_MEMLOCK); |
| 2869 |
rl->rlim_cur = MAX(rl->rlim_cur, (rlim_t) DEFAULT_RLIMIT_MEMLOCK); |
| 2870 |
} |
| 2871 |
|
| 2872 |
arg_defaults.rlimit[RLIMIT_MEMLOCK] = rl; |
| 2873 |
} |
| 2874 |
|
| 2875 |
static void setenv_manager_environment(void) { |
| 2876 |
int r; |
| 2877 |
|
| 2878 |
STRV_FOREACH(p, arg_manager_environment) { |
| 2879 |
log_debug("Setting '%s' in our own environment.", *p); |
| 2880 |
|
| 2881 |
r = putenv_dup(*p, true); |
| 2882 |
if (r < 0) |
| 2883 |
log_warning_errno(r, "Failed to setenv \"%s\", ignoring: %m", *p); |
| 2884 |
} |
| 2885 |
} |
| 2886 |
|
| 2887 |
static void reset_arguments(void) { |
| 2888 |
/* Frees/resets arg_* variables, with a few exceptions commented below. */ |
| 2889 |
|
| 2890 |
arg_default_unit = mfree(arg_default_unit); |
| 2891 |
|
| 2892 |
/* arg_runtime_scope — ignore */ |
| 2893 |
|
| 2894 |
arg_dump_core = true; |
| 2895 |
arg_crash_chvt = -1; |
| 2896 |
arg_crash_shell = false; |
| 2897 |
arg_crash_action = CRASH_FREEZE; |
| 2898 |
arg_confirm_spawn = mfree(arg_confirm_spawn); |
| 2899 |
arg_show_status = _SHOW_STATUS_INVALID; |
| 2900 |
arg_status_unit_format = STATUS_UNIT_FORMAT_DEFAULT; |
| 2901 |
arg_switched_root = false; |
| 2902 |
arg_pager_flags = 0; |
| 2903 |
arg_service_watchdogs = true; |
| 2904 |
|
| 2905 |
unit_defaults_done(&arg_defaults); |
| 2906 |
unit_defaults_init(&arg_defaults, arg_runtime_scope); |
| 2907 |
|
| 2908 |
arg_runtime_watchdog = 0; |
| 2909 |
arg_reboot_watchdog = 10 * USEC_PER_MINUTE; |
| 2910 |
arg_kexec_watchdog = 0; |
| 2911 |
arg_pretimeout_watchdog = 0; |
| 2912 |
arg_early_core_pattern = mfree(arg_early_core_pattern); |
| 2913 |
arg_watchdog_device = mfree(arg_watchdog_device); |
| 2914 |
arg_watchdog_pretimeout_governor = mfree(arg_watchdog_pretimeout_governor); |
| 2915 |
|
| 2916 |
arg_default_environment = strv_free(arg_default_environment); |
| 2917 |
arg_manager_environment = strv_free(arg_manager_environment); |
| 2918 |
|
| 2919 |
arg_capability_bounding_set = CAP_MASK_ALL; |
| 2920 |
arg_no_new_privs = false; |
| 2921 |
arg_protect_system = -1; |
| 2922 |
arg_restrict_filesystem_access = RESTRICT_FILESYSTEM_ACCESS_NO; |
| 2923 |
arg_timer_slack_nsec = NSEC_INFINITY; |
| 2924 |
|
| 2925 |
arg_syscall_archs = set_free(arg_syscall_archs); |
| 2926 |
|
| 2927 |
/* arg_serialization — ignore */ |
| 2928 |
|
| 2929 |
arg_machine_id = (sd_id128_t) {}; |
| 2930 |
arg_cad_burst_action = EMERGENCY_ACTION_REBOOT_FORCE; |
| 2931 |
|
| 2932 |
cpu_set_done(&arg_cpu_affinity); |
| 2933 |
numa_policy_reset(&arg_numa_policy); |
| 2934 |
|
| 2935 |
arg_random_seed = mfree(arg_random_seed); |
| 2936 |
arg_random_seed_size = 0; |
| 2937 |
arg_clock_usec = 0; |
| 2938 |
|
| 2939 |
arg_reload_limit_interval_sec = 0; |
| 2940 |
arg_reload_limit_burst = 0; |
| 2941 |
|
| 2942 |
arg_event_loop_ratelimit_interval_sec = 1 * USEC_PER_SEC; |
| 2943 |
arg_event_loop_ratelimit_burst = 50000; |
| 2944 |
|
| 2945 |
arg_minimum_uptime_usec = USEC_INFINITY; |
| 2946 |
} |
| 2947 |
|
| 2948 |
static void determine_default_oom_score_adjust(void) { |
| 2949 |
int r, a, b; |
| 2950 |
|
| 2951 |
/* Run our services at slightly higher OOM score than ourselves. But let's be conservative here, and |
| 2952 |
* do this only if we don't run as root (i.e. only if we are run in user mode, for an unprivileged |
| 2953 |
* user). */ |
| 2954 |
|
| 2955 |
if (arg_defaults.oom_score_adjust_set) |
| 2956 |
return; |
| 2957 |
|
| 2958 |
if (getuid() == 0) |
| 2959 |
return; |
| 2960 |
|
| 2961 |
r = get_oom_score_adjust(&a); |
| 2962 |
if (r < 0) |
| 2963 |
return (void) log_warning_errno(r, "Failed to determine current OOM score adjustment value, ignoring: %m"); |
| 2964 |
|
| 2965 |
assert_cc(100 <= OOM_SCORE_ADJ_MAX); |
| 2966 |
b = saturate_add(a, 100, OOM_SCORE_ADJ_MAX); |
| 2967 |
|
| 2968 |
if (a == b) |
| 2969 |
return; |
| 2970 |
|
| 2971 |
arg_defaults.oom_score_adjust = b; |
| 2972 |
arg_defaults.oom_score_adjust_set = true; |
| 2973 |
} |
| 2974 |
|
| 2975 |
static int parse_configuration(const struct rlimit *saved_rlimit_nofile, |
| 2976 |
const struct rlimit *saved_rlimit_memlock) { |
| 2977 |
int r; |
| 2978 |
|
| 2979 |
assert(saved_rlimit_nofile); |
| 2980 |
assert(saved_rlimit_memlock); |
| 2981 |
|
| 2982 |
/* Assign configuration defaults */ |
| 2983 |
reset_arguments(); |
| 2984 |
|
| 2985 |
r = parse_config_file(); |
| 2986 |
if (r < 0) |
| 2987 |
log_warning_errno(r, "Failed to parse config file, ignoring: %m"); |
| 2988 |
|
| 2989 |
if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM) { |
| 2990 |
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); |
| 2991 |
if (r < 0) |
| 2992 |
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
| 2993 |
} |
| 2994 |
|
| 2995 |
/* Initialize the show status setting if it hasn't been explicitly set yet */ |
| 2996 |
if (arg_show_status == _SHOW_STATUS_INVALID) |
| 2997 |
arg_show_status = SHOW_STATUS_YES; |
| 2998 |
|
| 2999 |
/* Push variables into the manager environment block */ |
| 3000 |
setenv_manager_environment(); |
| 3001 |
|
| 3002 |
/* Parse log environment variables to take into account any new environment variables. |
| 3003 |
* Note that this also parses bits from the kernel command line, including "debug". */ |
| 3004 |
log_parse_environment(); |
| 3005 |
|
| 3006 |
/* Initialize some default rlimits for services if they haven't been configured */ |
| 3007 |
fallback_rlimit_nofile(saved_rlimit_nofile); |
| 3008 |
fallback_rlimit_memlock(saved_rlimit_memlock); |
| 3009 |
|
| 3010 |
/* Slightly raise the OOM score for our services if we are running for unprivileged users. */ |
| 3011 |
determine_default_oom_score_adjust(); |
| 3012 |
|
| 3013 |
return 0; |
| 3014 |
} |
| 3015 |
|
| 3016 |
static int safety_checks(void) { |
| 3017 |
|
| 3018 |
if (getpid_cached() == 1 && |
| 3019 |
arg_action != ACTION_RUN) |
| 3020 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 3021 |
"Unsupported execution mode while PID 1."); |
| 3022 |
|
| 3023 |
if (getpid_cached() == 1 && |
| 3024 |
arg_runtime_scope == RUNTIME_SCOPE_USER) |
| 3025 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 3026 |
"Can't run --user mode as PID 1."); |
| 3027 |
|
| 3028 |
if (arg_action == ACTION_RUN && |
| 3029 |
arg_runtime_scope == RUNTIME_SCOPE_SYSTEM && |
| 3030 |
getpid_cached() != 1) |
| 3031 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 3032 |
"Can't run system mode unless PID 1."); |
| 3033 |
|
| 3034 |
if (arg_action == ACTION_TEST && |
| 3035 |
geteuid() == 0) |
| 3036 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 3037 |
"Don't run test mode as root."); |
| 3038 |
|
| 3039 |
switch (arg_runtime_scope) { |
| 3040 |
|
| 3041 |
case RUNTIME_SCOPE_USER: |
| 3042 |
|
| 3043 |
if (arg_action == ACTION_RUN && |
| 3044 |
sd_booted() <= 0) |
| 3045 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 3046 |
"Trying to run as user instance, but the system has not been booted with systemd."); |
| 3047 |
|
| 3048 |
if (arg_action == ACTION_RUN && |
| 3049 |
!getenv("XDG_RUNTIME_DIR")) |
| 3050 |
return log_error_errno(SYNTHETIC_ERRNO(EUNATCH), |
| 3051 |
"Trying to run as user instance, but $XDG_RUNTIME_DIR is not set."); |
| 3052 |
|
| 3053 |
break; |
| 3054 |
|
| 3055 |
case RUNTIME_SCOPE_SYSTEM: |
| 3056 |
if (arg_action == ACTION_RUN && |
| 3057 |
running_in_chroot() > 0) |
| 3058 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 3059 |
"Cannot be run in a chroot() environment."); |
| 3060 |
break; |
| 3061 |
|
| 3062 |
default: |
| 3063 |
assert_not_reached(); |
| 3064 |
} |
| 3065 |
|
| 3066 |
return 0; |
| 3067 |
} |
| 3068 |
|
| 3069 |
static int initialize_security( |
| 3070 |
bool *loaded_policy, |
| 3071 |
dual_timestamp *security_start_timestamp, |
| 3072 |
dual_timestamp *security_finish_timestamp, |
| 3073 |
const char **ret_error_message) { |
| 3074 |
|
| 3075 |
int r; |
| 3076 |
|
| 3077 |
assert(loaded_policy); |
| 3078 |
assert(security_start_timestamp); |
| 3079 |
assert(security_finish_timestamp); |
| 3080 |
assert(ret_error_message); |
| 3081 |
|
| 3082 |
dual_timestamp_now(security_start_timestamp); |
| 3083 |
|
| 3084 |
r = mac_selinux_setup(loaded_policy); |
| 3085 |
if (r < 0) { |
| 3086 |
*ret_error_message = "Failed to load SELinux policy"; |
| 3087 |
return r; |
| 3088 |
} |
| 3089 |
|
| 3090 |
r = mac_smack_setup(loaded_policy); |
| 3091 |
if (r < 0) { |
| 3092 |
*ret_error_message = "Failed to load SMACK policy"; |
| 3093 |
return r; |
| 3094 |
} |
| 3095 |
|
| 3096 |
r = mac_apparmor_setup(); |
| 3097 |
if (r < 0) { |
| 3098 |
*ret_error_message = "Failed to load AppArmor policy"; |
| 3099 |
return r; |
| 3100 |
} |
| 3101 |
|
| 3102 |
r = ima_setup(); |
| 3103 |
if (r < 0) { |
| 3104 |
*ret_error_message = "Failed to load IMA policy"; |
| 3105 |
return r; |
| 3106 |
} |
| 3107 |
|
| 3108 |
r = ipe_setup(); |
| 3109 |
if (r < 0) { |
| 3110 |
*ret_error_message = "Failed to load IPE policy"; |
| 3111 |
return r; |
| 3112 |
} |
| 3113 |
|
| 3114 |
dual_timestamp_now(security_finish_timestamp); |
| 3115 |
return 0; |
| 3116 |
} |
| 3117 |
|
| 3118 |
static int parse_listen_fds_env(unsigned *ret_n_fds, char ***ret_names) { |
| 3119 |
_cleanup_strv_free_ char **names = NULL; |
| 3120 |
const char *e; |
| 3121 |
unsigned n_fds; |
| 3122 |
int r; |
| 3123 |
|
| 3124 |
assert(ret_n_fds); |
| 3125 |
assert(ret_names); |
| 3126 |
|
| 3127 |
/* Parse and validate the LISTEN_PID=/LISTEN_PIDFDID=/LISTEN_FDS=/LISTEN_FDNAMES= environment |
| 3128 |
* variables. */ |
| 3129 |
|
| 3130 |
e = secure_getenv("LISTEN_PID"); |
| 3131 |
if (!e) |
| 3132 |
return -ENXIO; |
| 3133 |
|
| 3134 |
pid_t pid; |
| 3135 |
r = parse_pid(e, &pid); |
| 3136 |
if (r < 0) |
| 3137 |
return log_debug_errno(r, "Failed to parse LISTEN_PID=%s: %m", e); |
| 3138 |
if (pid != getpid_cached()) |
| 3139 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 3140 |
"LISTEN_PID=%s does not match our own PID " PID_FMT ", ignoring.", |
| 3141 |
e, |
| 3142 |
getpid_cached()); |
| 3143 |
|
| 3144 |
e = secure_getenv("LISTEN_PIDFDID"); |
| 3145 |
if (e) { |
| 3146 |
uint64_t own_pidfdid, pidfdid; |
| 3147 |
|
| 3148 |
r = safe_atou64(e, &pidfdid); |
| 3149 |
if (r < 0) |
| 3150 |
return log_debug_errno(r, "Failed to parse LISTEN_PIDFDID=%s: %m", e); |
| 3151 |
|
| 3152 |
if (pidfd_get_inode_id_self_cached(&own_pidfdid) >= 0 && pidfdid != own_pidfdid) |
| 3153 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 3154 |
"LISTEN_PIDFDID=%s does not match our own pidfdid %" PRIu64 ", ignoring.", |
| 3155 |
e, |
| 3156 |
own_pidfdid); |
| 3157 |
} |
| 3158 |
|
| 3159 |
e = secure_getenv("LISTEN_FDS"); |
| 3160 |
if (!e) |
| 3161 |
return -ENXIO; |
| 3162 |
|
| 3163 |
r = safe_atou(e, &n_fds); |
| 3164 |
if (r < 0) |
| 3165 |
return log_debug_errno(r, "Failed to parse LISTEN_FDS= value '%s': %m", e); |
| 3166 |
if (n_fds == 0) |
| 3167 |
return -ENXIO; |
| 3168 |
if (n_fds > INT_MAX - SD_LISTEN_FDS_START) |
| 3169 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid number of fds in LISTEN_FDS= value '%s'", e); |
| 3170 |
|
| 3171 |
e = secure_getenv("LISTEN_FDNAMES"); |
| 3172 |
if (!e) |
| 3173 |
return -ENXIO; |
| 3174 |
|
| 3175 |
r = strv_split_full(&names, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 3176 |
if (r < 0) |
| 3177 |
return log_debug_errno(r, "Failed to parse LISTEN_FDNAMES=%s: %m", e); |
| 3178 |
if (strv_length(names) != (size_t) n_fds) |
| 3179 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 3180 |
"Mismatch between number of LISTEN_FDS= and LISTEN_FDNAMES= entries: %u vs %zu", |
| 3181 |
n_fds, strv_length(names)); |
| 3182 |
|
| 3183 |
*ret_n_fds = n_fds; |
| 3184 |
*ret_names = TAKE_PTR(names); |
| 3185 |
return 0; |
| 3186 |
} |
| 3187 |
|
| 3188 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 3189 |
index_to_tag_hash_ops, |
| 3190 |
uint64_t, uint64_hash_func, uint64_compare_func, |
| 3191 |
ListenFDsTag, listen_fds_tag_free); |
| 3192 |
|
| 3193 |
static int parse_listen_fds_mapping(int mapping_fd, Hashmap **ret_index_to_tag) { |
| 3194 |
int r; |
| 3195 |
|
| 3196 |
assert(mapping_fd >= 0); |
| 3197 |
assert(ret_index_to_tag); |
| 3198 |
|
| 3199 |
/* Parse the JSON mapping memfd that the downstream manager pushed alongside the indexed fds: |
| 3200 |
* { "unit-name.service": [ { "name": "fdname1", "index": 1 }, ... ], ... } |
| 3201 |
* Returns a hashmap keyed by stringified index ("1", "2", ...) with ListenFDsTag* values |
| 3202 |
* carrying the resolved (unit_id, original fdname, upstream index). */ |
| 3203 |
|
| 3204 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *root = NULL; |
| 3205 |
r = sd_json_parse_fd( |
| 3206 |
"fdstore-mapping", |
| 3207 |
mapping_fd, |
| 3208 |
SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_REOPEN_FD, |
| 3209 |
&root, |
| 3210 |
/* reterr_line= */ NULL, |
| 3211 |
/* reterr_column= */ NULL); |
| 3212 |
if (r < 0) |
| 3213 |
return log_warning_errno(r, "Failed to parse fdstore-mapping JSON: %m"); |
| 3214 |
|
| 3215 |
_cleanup_hashmap_free_ Hashmap *index_to_tag = NULL; |
| 3216 |
sd_json_variant *fds_json; |
| 3217 |
const char *unit_id; |
| 3218 |
JSON_VARIANT_OBJECT_FOREACH(unit_id, fds_json, root) { |
| 3219 |
sd_json_variant *entry; |
| 3220 |
|
| 3221 |
if (!unit_name_is_valid(unit_id, UNIT_NAME_ANY)) { |
| 3222 |
log_warning("fdstore-mapping has invalid unit name '%s', skipping.", unit_id); |
| 3223 |
continue; |
| 3224 |
} |
| 3225 |
|
| 3226 |
JSON_VARIANT_ARRAY_FOREACH(entry, fds_json) { |
| 3227 |
struct { |
| 3228 |
const char *name; |
| 3229 |
uint64_t index; |
| 3230 |
} p = { }; |
| 3231 |
|
| 3232 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 3233 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, name), SD_JSON_MANDATORY }, |
| 3234 |
{ "index", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, voffsetof(p, index), SD_JSON_MANDATORY }, |
| 3235 |
{} |
| 3236 |
}; |
| 3237 |
|
| 3238 |
r = sd_json_dispatch(entry, dispatch_table, SD_JSON_ALLOW_EXTENSIONS|SD_JSON_LOG|SD_JSON_WARNING, &p); |
| 3239 |
if (r < 0) |
| 3240 |
continue; |
| 3241 |
|
| 3242 |
if (p.index == 0) { |
| 3243 |
log_warning("fdstore-mapping entry for unit '%s' name '%s' has zero index, skipping.", unit_id, p.name); |
| 3244 |
continue; |
| 3245 |
} |
| 3246 |
|
| 3247 |
_cleanup_(listen_fds_tag_freep) ListenFDsTag *t = new(ListenFDsTag, 1); |
| 3248 |
if (!t) |
| 3249 |
return log_oom(); |
| 3250 |
|
| 3251 |
*t = (ListenFDsTag) { |
| 3252 |
.index = p.index, |
| 3253 |
}; |
| 3254 |
|
| 3255 |
t->unit_id = strdup(unit_id); |
| 3256 |
t->fdname = strdup(p.name); |
| 3257 |
if (!t->unit_id || !t->fdname) |
| 3258 |
return log_oom(); |
| 3259 |
|
| 3260 |
/* Key points into the value struct, so freeing the value frees the key. */ |
| 3261 |
r = hashmap_ensure_put(&index_to_tag, &index_to_tag_hash_ops, &t->index, t); |
| 3262 |
if (r < 0) |
| 3263 |
return log_warning_errno(r, "Failed to insert fdstore-mapping entry into hashmap: %m"); |
| 3264 |
if (r > 0) |
| 3265 |
TAKE_PTR(t); |
| 3266 |
} |
| 3267 |
} |
| 3268 |
|
| 3269 |
*ret_index_to_tag = TAKE_PTR(index_to_tag); |
| 3270 |
return 0; |
| 3271 |
} |
| 3272 |
|
| 3273 |
static int collect_listen_fds_named(FDSet *fds, Hashmap **ret_named_fds) { |
| 3274 |
_cleanup_hashmap_free_ Hashmap *named_fds = NULL, *index_to_tag = NULL; |
| 3275 |
_cleanup_strv_free_ char **names = NULL; |
| 3276 |
unsigned n_fds; |
| 3277 |
int r; |
| 3278 |
|
| 3279 |
assert(fds); |
| 3280 |
assert(ret_named_fds); |
| 3281 |
|
| 3282 |
/* Pull entries from the LISTEN_FDS=/LISTEN_FDNAMES= protocol out of 'fds' into a hashmap |
| 3283 |
* keyed by fd. Two flavours of named entries are recognized: |
| 3284 |
* |
| 3285 |
* - A single mapping memfd whose fdname matches SERVICE_FDSTORE_MAPPING_FDNAME, which |
| 3286 |
* contains a JSON map pairing numeric indices to (unit-id, original-fdname). |
| 3287 |
* - Numeric indices (matching entries in the mapping document) for the actual fds. |
| 3288 |
* |
| 3289 |
* The hashmap owns the fds (closed via destructor on cleanup) so any entries the dispatcher |
| 3290 |
* does not consume are correctly cleaned up. */ |
| 3291 |
|
| 3292 |
r = parse_listen_fds_env(&n_fds, &names); |
| 3293 |
if (r < 0) { |
| 3294 |
/* Fail gracefully here, just warn and ignore but otherwise proceed on parsing failure */ |
| 3295 |
if (r != -ENXIO) |
| 3296 |
log_warning_errno(r, "Failed to parse LISTEN_FDS environment, ignoring: %m"); |
| 3297 |
*ret_named_fds = NULL; |
| 3298 |
return 0; |
| 3299 |
} |
| 3300 |
|
| 3301 |
/* First pass: locate and parse the mapping memfd, if any. */ |
| 3302 |
for (unsigned i = 0; i < n_fds; i++) { |
| 3303 |
int fd = SD_LISTEN_FDS_START + i; |
| 3304 |
|
| 3305 |
if (!streq(names[i], SERVICE_FDSTORE_MAPPING_FDNAME)) |
| 3306 |
continue; |
| 3307 |
|
| 3308 |
if (!fdset_contains(fds, fd)) |
| 3309 |
continue; |
| 3310 |
|
| 3311 |
(void) parse_listen_fds_mapping(fd, &index_to_tag); |
| 3312 |
|
| 3313 |
/* The mapping memfd itself is not routed to any unit; close it and remove from fds |
| 3314 |
* so it doesn't get redistributed */ |
| 3315 |
assert_se(fdset_remove(fds, fd) == fd); |
| 3316 |
safe_close(fd); |
| 3317 |
break; |
| 3318 |
} |
| 3319 |
|
| 3320 |
/* Second pass: route fds whose name matches an entry in the mapping. */ |
| 3321 |
for (unsigned i = 0; i < n_fds; i++) { |
| 3322 |
int fd = SD_LISTEN_FDS_START + i; |
| 3323 |
const char *name = names[i], *suffix; |
| 3324 |
ListenFDsTag *t; |
| 3325 |
uint64_t idx; |
| 3326 |
|
| 3327 |
if (!fdset_contains(fds, fd)) |
| 3328 |
continue; |
| 3329 |
|
| 3330 |
if (!index_to_tag) |
| 3331 |
continue; |
| 3332 |
|
| 3333 |
suffix = startswith(name, SERVICE_FDSTORE_SUB_FDNAME_PREFIX); |
| 3334 |
if (!suffix || safe_atou64(suffix, &idx) < 0) |
| 3335 |
continue; |
| 3336 |
|
| 3337 |
/* Steal the matching mapping entry — we transfer ownership of the parsed |
| 3338 |
* (unit_id, fdname, index) struct into the per-fd hashmap that the manager |
| 3339 |
* will consume. */ |
| 3340 |
t = hashmap_remove(index_to_tag, &idx); |
| 3341 |
if (!t) |
| 3342 |
continue; |
| 3343 |
|
| 3344 |
_cleanup_(listen_fds_tag_freep) ListenFDsTag *t_owned = t; |
| 3345 |
|
| 3346 |
r = hashmap_ensure_put(&named_fds, &fd_to_listen_fds_tag_hash_ops, FD_TO_PTR(fd), t_owned); |
| 3347 |
if (r < 0) |
| 3348 |
return log_debug_errno(r, "Failed to insert named fd into hashmap: %m"); |
| 3349 |
if (r == 0) |
| 3350 |
continue; /* fd already inserted, cannot really happen */ |
| 3351 |
|
| 3352 |
TAKE_PTR(t_owned); |
| 3353 |
|
| 3354 |
assert_se(fdset_remove(fds, fd) == fd); |
| 3355 |
} |
| 3356 |
|
| 3357 |
*ret_named_fds = TAKE_PTR(named_fds); |
| 3358 |
return 1; |
| 3359 |
} |
| 3360 |
|
| 3361 |
static int collect_fds(FDSet **ret_fds, Hashmap **ret_named_fds, const char **ret_error_message) { |
| 3362 |
int r; |
| 3363 |
|
| 3364 |
assert(ret_fds); |
| 3365 |
assert(ret_named_fds); |
| 3366 |
assert(ret_error_message); |
| 3367 |
|
| 3368 |
/* Pick up all fds passed to us. We apply a filter here: we only take the fds that have O_CLOEXEC |
| 3369 |
* off. All fds passed via execve() to us must have O_CLOEXEC off, and our own code and dependencies |
| 3370 |
* should be clean enough to set O_CLOEXEC universally. Thus checking the bit should be a safe |
| 3371 |
* mechanism to distinguish passed in fds from our own. |
| 3372 |
* |
| 3373 |
* Why bother? Some subsystems we initialize early, specifically selinux might keep fds open in our |
| 3374 |
* process behind our back. We should not take possession of that (and then accidentally close |
| 3375 |
* it). SELinux thankfully sets O_CLOEXEC on its fds, so this test should work. */ |
| 3376 |
r = fdset_new_fill(/* filter_cloexec= */ 0, ret_fds); |
| 3377 |
if (r < 0) { |
| 3378 |
*ret_error_message = "Failed to allocate fd set"; |
| 3379 |
return log_struct_errno(LOG_EMERG, r, |
| 3380 |
LOG_MESSAGE("Failed to allocate fd set: %m"), |
| 3381 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_FD_SET_FAILED_STR)); |
| 3382 |
} |
| 3383 |
|
| 3384 |
/* The serialization fd should have O_CLOEXEC turned on already, let's verify that we didn't pick it up here */ |
| 3385 |
assert_se(!arg_serialization || !fdset_contains(*ret_fds, fileno(arg_serialization))); |
| 3386 |
|
| 3387 |
(void) collect_listen_fds_named(*ret_fds, ret_named_fds); |
| 3388 |
|
| 3389 |
return 0; |
| 3390 |
} |
| 3391 |
|
| 3392 |
static void setup_console_terminal(bool skip_setup) { |
| 3393 |
|
| 3394 |
if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM) |
| 3395 |
return; |
| 3396 |
|
| 3397 |
/* If we are init, we connect stdin/stdout/stderr to /dev/null and make sure we don't have a |
| 3398 |
* controlling tty. */ |
| 3399 |
terminal_detach_session(); |
| 3400 |
|
| 3401 |
/* Reset the console, but only if this is really init and we are freshly booted */ |
| 3402 |
if (!skip_setup) |
| 3403 |
(void) console_setup(); |
| 3404 |
} |
| 3405 |
|
| 3406 |
static bool early_skip_setup_check(int argc, char *argv[]) { |
| 3407 |
bool found_deserialize = false; |
| 3408 |
|
| 3409 |
/* Determine if this is a reexecution or normal bootup. We do the full command line parsing much |
| 3410 |
* later, so let's just have a quick peek here. Note that if we have switched root, do all the |
| 3411 |
* special setup things anyway, even if in that case we also do deserialization. */ |
| 3412 |
|
| 3413 |
for (int i = 1; i < argc; i++) |
| 3414 |
if (streq(argv[i], "--switched-root")) |
| 3415 |
return false; /* If we switched root, don't skip the setup. */ |
| 3416 |
else if (startswith(argv[i], "--deserialize=") || streq(argv[i], "--deserialize")) |
| 3417 |
found_deserialize = true; |
| 3418 |
|
| 3419 |
return found_deserialize; /* When we are deserializing, then we are reexecuting, hence avoid the extensive setup */ |
| 3420 |
} |
| 3421 |
|
| 3422 |
static int save_env(void) { |
| 3423 |
char **l; |
| 3424 |
|
| 3425 |
l = strv_copy(environ); |
| 3426 |
if (!l) |
| 3427 |
return log_oom(); |
| 3428 |
|
| 3429 |
strv_free_and_replace(saved_env, l); |
| 3430 |
return 0; |
| 3431 |
} |
| 3432 |
|
| 3433 |
int main(int argc, char *argv[]) { |
| 3434 |
dual_timestamp |
| 3435 |
initrd_timestamp = DUAL_TIMESTAMP_NULL, |
| 3436 |
userspace_timestamp = DUAL_TIMESTAMP_NULL, |
| 3437 |
kernel_timestamp = DUAL_TIMESTAMP_NULL, |
| 3438 |
security_start_timestamp = DUAL_TIMESTAMP_NULL, |
| 3439 |
security_finish_timestamp = DUAL_TIMESTAMP_NULL; |
| 3440 |
struct rlimit saved_rlimit_nofile = RLIMIT_MAKE_CONST(0), |
| 3441 |
saved_rlimit_memlock = RLIMIT_MAKE_CONST(RLIM_INFINITY); /* The original rlimits we passed |
| 3442 |
* in. Note we use different values |
| 3443 |
* for the two that indicate whether |
| 3444 |
* these fields are initialized! */ |
| 3445 |
bool skip_setup, loaded_policy = false, queue_default_job = false, first_boot = false; |
| 3446 |
_cleanup_hashmap_free_ Hashmap *named_listen_fds = NULL; |
| 3447 |
char *switch_root_dir = NULL, *switch_root_init = NULL; |
| 3448 |
usec_t before_startup, after_startup; |
| 3449 |
static char systemd[] = "systemd"; |
| 3450 |
const char *error_message = NULL; |
| 3451 |
uint64_t saved_ambient_set = 0; |
| 3452 |
int r, retval = EXIT_FAILURE; |
| 3453 |
Manager *m = NULL; |
| 3454 |
FDSet *fds = NULL; |
| 3455 |
|
| 3456 |
assert_se(argc > 0 && !isempty(argv[0])); |
| 3457 |
|
| 3458 |
/* Take timestamps early on */ |
| 3459 |
dual_timestamp_from_monotonic(&kernel_timestamp, 0); |
| 3460 |
dual_timestamp_now(&userspace_timestamp); |
| 3461 |
|
| 3462 |
/* Figure out whether we need to do initialize the system, or if we already did that because we are |
| 3463 |
* reexecuting. */ |
| 3464 |
skip_setup = early_skip_setup_check(argc, argv); |
| 3465 |
|
| 3466 |
/* If we get started via the /sbin/init symlink then we are called 'init'. After a subsequent |
| 3467 |
* reexecution we are then called 'systemd'. That is confusing, hence let's call us systemd |
| 3468 |
* right-away. */ |
| 3469 |
program_invocation_short_name = systemd; |
| 3470 |
(void) prctl(PR_SET_NAME, systemd); |
| 3471 |
|
| 3472 |
/* Save the original command line */ |
| 3473 |
save_argc_argv(argc, argv); |
| 3474 |
|
| 3475 |
/* Save the original environment as we might need to restore it if we're requested to execute another |
| 3476 |
* system manager later. */ |
| 3477 |
r = save_env(); |
| 3478 |
if (r < 0) { |
| 3479 |
error_message = "Failed to copy environment block"; |
| 3480 |
goto finish; |
| 3481 |
} |
| 3482 |
|
| 3483 |
/* Make sure that if the user says "syslog" we actually log to the journal. */ |
| 3484 |
log_set_upgrade_syslog_to_journal(true); |
| 3485 |
|
| 3486 |
if (getpid_cached() == 1) { |
| 3487 |
/* When we run as PID 1 force system mode */ |
| 3488 |
arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 3489 |
|
| 3490 |
/* Disable the umask logic */ |
| 3491 |
umask(0); |
| 3492 |
|
| 3493 |
/* Make sure that at least initially we do not ever log to journald/syslogd, because it might |
| 3494 |
* not be activated yet (even though the log socket for it exists). */ |
| 3495 |
log_set_prohibit_ipc(true); |
| 3496 |
|
| 3497 |
/* Always reopen /dev/console when running as PID 1 or one of its pre-execve() children. This |
| 3498 |
* is important so that we never end up logging to any foreign stderr, for example if we have |
| 3499 |
* to log in a child process right before execve()'ing the actual binary, at a point in time |
| 3500 |
* where socket activation stderr/stdout area already set up. */ |
| 3501 |
log_set_always_reopen_console(true); |
| 3502 |
|
| 3503 |
if (detect_container() <= 0) { |
| 3504 |
|
| 3505 |
/* Running outside of a container as PID 1 */ |
| 3506 |
log_set_target_and_open(LOG_TARGET_KMSG); |
| 3507 |
|
| 3508 |
if (in_initrd()) |
| 3509 |
initrd_timestamp = userspace_timestamp; |
| 3510 |
|
| 3511 |
if (!skip_setup) { |
| 3512 |
r = mount_setup_early(); |
| 3513 |
if (r < 0) { |
| 3514 |
error_message = "Failed to mount early API filesystems"; |
| 3515 |
goto finish; |
| 3516 |
} |
| 3517 |
} |
| 3518 |
|
| 3519 |
/* We might have just mounted /proc, so let's try to parse the kernel |
| 3520 |
* command line log arguments immediately. */ |
| 3521 |
log_parse_environment(); |
| 3522 |
|
| 3523 |
/* Let's open the log backend a second time, in case the first time didn't |
| 3524 |
* work. Quite possibly we have mounted /dev just now, so /dev/kmsg became |
| 3525 |
* available, and it previously wasn't. */ |
| 3526 |
log_open(); |
| 3527 |
|
| 3528 |
if (!skip_setup) { |
| 3529 |
disable_printk_ratelimit(); |
| 3530 |
|
| 3531 |
r = initialize_security( |
| 3532 |
&loaded_policy, |
| 3533 |
&security_start_timestamp, |
| 3534 |
&security_finish_timestamp, |
| 3535 |
&error_message); |
| 3536 |
if (r < 0) |
| 3537 |
goto finish; |
| 3538 |
} |
| 3539 |
|
| 3540 |
r = mac_init(); |
| 3541 |
if (r < 0) { |
| 3542 |
error_message = "Failed to initialize MAC support"; |
| 3543 |
goto finish; |
| 3544 |
} |
| 3545 |
|
| 3546 |
if (!skip_setup) |
| 3547 |
initialize_clock_timewarp(); |
| 3548 |
|
| 3549 |
clock_apply_epoch(/* allow_backwards= */ !skip_setup); |
| 3550 |
|
| 3551 |
/* Set the default for later on, but don't actually open the logs like this for |
| 3552 |
* now. Note that if we are transitioning from the initrd there might still be |
| 3553 |
* journal fd open, and we shouldn't attempt opening that before we parsed |
| 3554 |
* /proc/cmdline which might redirect output elsewhere. */ |
| 3555 |
log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); |
| 3556 |
|
| 3557 |
} else { |
| 3558 |
/* Running inside a container, as PID 1 */ |
| 3559 |
log_set_target_and_open(LOG_TARGET_CONSOLE); |
| 3560 |
|
| 3561 |
/* For later on, see above... */ |
| 3562 |
log_set_target(LOG_TARGET_JOURNAL); |
| 3563 |
|
| 3564 |
/* clear the kernel timestamp, because we are in a container */ |
| 3565 |
kernel_timestamp = DUAL_TIMESTAMP_NULL; |
| 3566 |
} |
| 3567 |
|
| 3568 |
initialize_coredump(skip_setup); |
| 3569 |
|
| 3570 |
r = fixup_environment(); |
| 3571 |
if (r < 0) { |
| 3572 |
log_struct_errno(LOG_EMERG, r, |
| 3573 |
LOG_MESSAGE("Failed to fix up PID 1 environment: %m"), |
| 3574 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_PID1_ENVIRONMENT_STR)); |
| 3575 |
error_message = "Failed to fix up PID1 environment"; |
| 3576 |
goto finish; |
| 3577 |
} |
| 3578 |
|
| 3579 |
/* Try to figure out if we can use colors with the console. No need to do that for user |
| 3580 |
* instances since they never log into the console. */ |
| 3581 |
log_show_color(colors_enabled()); |
| 3582 |
|
| 3583 |
r = make_null_stdio(); |
| 3584 |
if (r < 0) |
| 3585 |
log_warning_errno(r, "Failed to redirect standard streams to /dev/null, ignoring: %m"); |
| 3586 |
|
| 3587 |
/* Load the kernel modules early. */ |
| 3588 |
if (!skip_setup) |
| 3589 |
(void) kmod_setup(); |
| 3590 |
|
| 3591 |
/* Mount /proc, /sys and friends, so that /proc/cmdline and /proc/$PID/fd is available. */ |
| 3592 |
r = mount_setup(loaded_policy, skip_setup); |
| 3593 |
if (r < 0) { |
| 3594 |
error_message = "Failed to mount API filesystems"; |
| 3595 |
goto finish; |
| 3596 |
} |
| 3597 |
|
| 3598 |
/* The efivarfs is now mounted, let's lock down the system token. */ |
| 3599 |
lock_down_efi_variables(); |
| 3600 |
|
| 3601 |
} else { |
| 3602 |
/* Running as user instance */ |
| 3603 |
arg_runtime_scope = RUNTIME_SCOPE_USER; |
| 3604 |
|
| 3605 |
/* clear the kernel timestamp, because we are not PID 1 */ |
| 3606 |
kernel_timestamp = DUAL_TIMESTAMP_NULL; |
| 3607 |
|
| 3608 |
if (invoked_by_systemd()) { |
| 3609 |
log_set_always_reopen_console(true); |
| 3610 |
log_set_target_and_open(LOG_TARGET_AUTO); |
| 3611 |
} |
| 3612 |
|
| 3613 |
r = mac_init(); |
| 3614 |
if (r < 0) { |
| 3615 |
error_message = "Failed to initialize MAC support"; |
| 3616 |
goto finish; |
| 3617 |
} |
| 3618 |
} |
| 3619 |
|
| 3620 |
/* Save the original RLIMIT_NOFILE/RLIMIT_MEMLOCK so that we can reset it later when |
| 3621 |
* transitioning from the initrd to the main systemd or suchlike. */ |
| 3622 |
save_rlimits(&saved_rlimit_nofile, &saved_rlimit_memlock); |
| 3623 |
|
| 3624 |
/* Reset all signal handlers to clean up after reexec. */ |
| 3625 |
(void) reset_all_signal_handlers(); |
| 3626 |
|
| 3627 |
if (getpid_cached() == 1 || invoked_by_systemd()) |
| 3628 |
(void) ignore_signals(SIGNALS_IGNORE); |
| 3629 |
|
| 3630 |
(void) parse_configuration(&saved_rlimit_nofile, &saved_rlimit_memlock); |
| 3631 |
|
| 3632 |
r = parse_argv(argc, argv); |
| 3633 |
if (r < 0) { |
| 3634 |
error_message = "Failed to parse command line arguments"; |
| 3635 |
goto finish; |
| 3636 |
} |
| 3637 |
|
| 3638 |
r = safety_checks(); |
| 3639 |
if (r < 0) |
| 3640 |
goto finish; |
| 3641 |
|
| 3642 |
if (IN_SET(arg_action, ACTION_TEST, ACTION_HELP, ACTION_DUMP_CONFIGURATION_ITEMS, ACTION_DUMP_BUS_PROPERTIES, ACTION_BUS_INTROSPECT)) |
| 3643 |
pager_open(arg_pager_flags); |
| 3644 |
|
| 3645 |
if (arg_action != ACTION_RUN) |
| 3646 |
skip_setup = true; |
| 3647 |
|
| 3648 |
if (arg_action == ACTION_HELP) { |
| 3649 |
retval = help() < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
| 3650 |
goto finish; |
| 3651 |
} else if (arg_action == ACTION_VERSION) { |
| 3652 |
retval = version(); |
| 3653 |
goto finish; |
| 3654 |
} else if (arg_action == ACTION_DUMP_CONFIGURATION_ITEMS) { |
| 3655 |
unit_dump_config_items(stdout); |
| 3656 |
retval = EXIT_SUCCESS; |
| 3657 |
goto finish; |
| 3658 |
} else if (arg_action == ACTION_DUMP_BUS_PROPERTIES) { |
| 3659 |
dump_bus_properties(stdout); |
| 3660 |
retval = EXIT_SUCCESS; |
| 3661 |
goto finish; |
| 3662 |
} else if (arg_action == ACTION_BUS_INTROSPECT) { |
| 3663 |
r = bus_manager_introspect_implementations(stdout, arg_bus_introspect); |
| 3664 |
retval = r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 3665 |
goto finish; |
| 3666 |
} |
| 3667 |
|
| 3668 |
assert_se(IN_SET(arg_action, ACTION_RUN, ACTION_TEST)); |
| 3669 |
|
| 3670 |
/* Move out of the way, so that we won't block unmounts */ |
| 3671 |
assert_se(chdir("/") == 0); |
| 3672 |
|
| 3673 |
if (arg_action == ACTION_RUN) { |
| 3674 |
if (!skip_setup) { |
| 3675 |
/* Apply the systemd.clock_usec= kernel command line switch */ |
| 3676 |
apply_clock_update(); |
| 3677 |
|
| 3678 |
/* Apply random seed from kernel command line */ |
| 3679 |
cmdline_take_random_seed(); |
| 3680 |
} |
| 3681 |
|
| 3682 |
/* A core pattern might have been specified via the cmdline. */ |
| 3683 |
initialize_core_pattern(skip_setup); |
| 3684 |
|
| 3685 |
/* Make /usr/ read-only */ |
| 3686 |
apply_protect_system(skip_setup); |
| 3687 |
|
| 3688 |
/* Close logging fds, in order not to confuse collecting passed fds and terminal logic below */ |
| 3689 |
log_close(); |
| 3690 |
|
| 3691 |
/* Remember open file descriptors for later deserialization */ |
| 3692 |
r = collect_fds(&fds, &named_listen_fds, &error_message); |
| 3693 |
if (r < 0) |
| 3694 |
goto finish; |
| 3695 |
|
| 3696 |
/* Give up any control of the console, but make sure its initialized. */ |
| 3697 |
setup_console_terminal(skip_setup); |
| 3698 |
|
| 3699 |
/* Open the logging devices, if possible and necessary */ |
| 3700 |
log_open(); |
| 3701 |
} |
| 3702 |
|
| 3703 |
log_execution_mode(&first_boot); |
| 3704 |
|
| 3705 |
r = cg_has_legacy(); |
| 3706 |
if (r < 0) { |
| 3707 |
error_message = "Failed to check cgroup hierarchy"; |
| 3708 |
goto finish; |
| 3709 |
} |
| 3710 |
if (r > 0) { |
| 3711 |
r = log_full_errno(LOG_EMERG, SYNTHETIC_ERRNO(EPROTO), |
| 3712 |
"Detected cgroup v1 hierarchy at /sys/fs/cgroup/, which is no longer supported by current version of systemd.\n" |
| 3713 |
"Please instruct your initrd to mount cgroup v2 (unified) hierarchy,\n" |
| 3714 |
"possibly by removing any stale kernel command line options, such as:\n" |
| 3715 |
" systemd.legacy_systemd_cgroup_controller=1\n" |
| 3716 |
" systemd.unified_cgroup_hierarchy=0"); |
| 3717 |
|
| 3718 |
error_message = "Detected unsupported legacy cgroup hierarchy, refusing execution"; |
| 3719 |
goto finish; |
| 3720 |
} |
| 3721 |
|
| 3722 |
/* Building without libmount is allowed, but if it is compiled in, then we must be able to load it */ |
| 3723 |
r = DLOPEN_LIBMOUNT(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 3724 |
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) { |
| 3725 |
error_message = "Failed to load libmount.so"; |
| 3726 |
goto finish; |
| 3727 |
} |
| 3728 |
|
| 3729 |
r = initialize_runtime(skip_setup, |
| 3730 |
first_boot, |
| 3731 |
&saved_rlimit_nofile, |
| 3732 |
&saved_rlimit_memlock, |
| 3733 |
&saved_ambient_set, |
| 3734 |
&error_message); |
| 3735 |
if (r < 0) |
| 3736 |
goto finish; |
| 3737 |
|
| 3738 |
r = manager_new(arg_runtime_scope, |
| 3739 |
arg_action == ACTION_TEST ? MANAGER_TEST_FULL : 0, |
| 3740 |
&m); |
| 3741 |
if (r < 0) { |
| 3742 |
log_struct_errno(LOG_EMERG, r, |
| 3743 |
LOG_MESSAGE("Failed to allocate manager object: %m"), |
| 3744 |
LOG_MESSAGE_ID(SD_MESSAGE_CORE_MANAGER_ALLOCATE_STR)); |
| 3745 |
error_message = "Failed to allocate manager object"; |
| 3746 |
goto finish; |
| 3747 |
} |
| 3748 |
|
| 3749 |
m->timestamps[MANAGER_TIMESTAMP_KERNEL] = kernel_timestamp; |
| 3750 |
m->timestamps[MANAGER_TIMESTAMP_INITRD] = initrd_timestamp; |
| 3751 |
m->timestamps[MANAGER_TIMESTAMP_USERSPACE] = userspace_timestamp; |
| 3752 |
m->timestamps[manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_SECURITY_START)] = security_start_timestamp; |
| 3753 |
m->timestamps[manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_SECURITY_FINISH)] = security_finish_timestamp; |
| 3754 |
|
| 3755 |
m->saved_ambient_set = saved_ambient_set; |
| 3756 |
|
| 3757 |
set_manager_defaults(m); |
| 3758 |
set_manager_settings(m); |
| 3759 |
manager_set_first_boot(m, first_boot); |
| 3760 |
manager_set_switching_root(m, arg_switched_root); |
| 3761 |
|
| 3762 |
/* Remember whether we should queue the default job */ |
| 3763 |
queue_default_job = !arg_serialization || arg_switched_root; |
| 3764 |
|
| 3765 |
before_startup = now(CLOCK_MONOTONIC); |
| 3766 |
|
| 3767 |
r = manager_startup(m, arg_serialization, fds, named_listen_fds, /* root= */ NULL); |
| 3768 |
if (r < 0) { |
| 3769 |
error_message = "Failed to start up manager"; |
| 3770 |
goto finish; |
| 3771 |
} |
| 3772 |
|
| 3773 |
/* This will close all file descriptors that were opened, but not claimed by any unit. */ |
| 3774 |
fds = fdset_free(fds); |
| 3775 |
arg_serialization = safe_fclose(arg_serialization); |
| 3776 |
|
| 3777 |
if (queue_default_job) { |
| 3778 |
r = do_queue_default_job(m, &error_message); |
| 3779 |
if (r < 0) |
| 3780 |
goto finish; |
| 3781 |
} |
| 3782 |
|
| 3783 |
after_startup = now(CLOCK_MONOTONIC); |
| 3784 |
|
| 3785 |
log_full(arg_action == ACTION_TEST ? LOG_INFO : LOG_DEBUG, |
| 3786 |
"Loaded units and determined initial transaction in %s.", |
| 3787 |
FORMAT_TIMESPAN(after_startup - before_startup, 100 * USEC_PER_MSEC)); |
| 3788 |
|
| 3789 |
if (arg_action == ACTION_TEST) { |
| 3790 |
manager_test_summary(m); |
| 3791 |
retval = EXIT_SUCCESS; |
| 3792 |
goto finish; |
| 3793 |
} |
| 3794 |
|
| 3795 |
r = invoke_main_loop(m, |
| 3796 |
&saved_rlimit_nofile, |
| 3797 |
&saved_rlimit_memlock, |
| 3798 |
&retval, |
| 3799 |
&fds, |
| 3800 |
&switch_root_dir, |
| 3801 |
&switch_root_init, |
| 3802 |
&error_message); |
| 3803 |
/* MANAGER_OK and MANAGER_RELOAD are not expected here. */ |
| 3804 |
assert(r < 0 || IN_SET(r, MANAGER_REEXECUTE, MANAGER_EXIT) || |
| 3805 |
(arg_runtime_scope == RUNTIME_SCOPE_SYSTEM && |
| 3806 |
IN_SET(r, MANAGER_REBOOT, |
| 3807 |
MANAGER_SOFT_REBOOT, |
| 3808 |
MANAGER_POWEROFF, |
| 3809 |
MANAGER_HALT, |
| 3810 |
MANAGER_KEXEC, |
| 3811 |
MANAGER_SWITCH_ROOT))); |
| 3812 |
|
| 3813 |
finish: |
| 3814 |
pager_close(); |
| 3815 |
|
| 3816 |
if (m) { |
| 3817 |
arg_reboot_watchdog = manager_get_watchdog(m, WATCHDOG_REBOOT); |
| 3818 |
arg_kexec_watchdog = manager_get_watchdog(m, WATCHDOG_KEXEC); |
| 3819 |
|
| 3820 |
/* For kexec, serialize fd stores now. Services have stopped and sent |
| 3821 |
* their FDs to the store, but the manager (and its fd stores) is still alive. */ |
| 3822 |
if (r == MANAGER_KEXEC) |
| 3823 |
(void) manager_luo_serialize_fd_stores(m, &arg_luo_serialization, &fds); |
| 3824 |
|
| 3825 |
m = manager_free(m); |
| 3826 |
} |
| 3827 |
|
| 3828 |
mac_selinux_finish(); |
| 3829 |
|
| 3830 |
if (IN_SET(r, MANAGER_REEXECUTE, MANAGER_SWITCH_ROOT, MANAGER_SOFT_REBOOT)) |
| 3831 |
r = do_reexecute(r, |
| 3832 |
argc, argv, |
| 3833 |
&saved_rlimit_nofile, |
| 3834 |
&saved_rlimit_memlock, |
| 3835 |
fds, |
| 3836 |
switch_root_dir, |
| 3837 |
switch_root_init, |
| 3838 |
saved_ambient_set, |
| 3839 |
&error_message); /* This only returns if reexecution failed */ |
| 3840 |
|
| 3841 |
arg_serialization = safe_fclose(arg_serialization); |
| 3842 |
|
| 3843 |
/* For kexec, the FDSet and LUO serialization file must survive until become_shutdown() calls |
| 3844 |
* execve() (CLOEXEC is already cleared on these FDs). For all other paths, free them now. */ |
| 3845 |
if (r != MANAGER_KEXEC) { |
| 3846 |
fds = fdset_free(fds); |
| 3847 |
arg_luo_serialization = safe_fclose(arg_luo_serialization); |
| 3848 |
} |
| 3849 |
|
| 3850 |
saved_env = strv_free(saved_env); |
| 3851 |
|
| 3852 |
#if HAVE_VALGRIND_VALGRIND_H |
| 3853 |
/* If we are PID 1 and running under valgrind, then let's exit |
| 3854 |
* here explicitly. valgrind will only generate nice output on |
| 3855 |
* exit(), not on exec(), hence let's do the former not the |
| 3856 |
* latter here. */ |
| 3857 |
if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) { |
| 3858 |
/* Cleanup watchdog_device strings for valgrind. We need them |
| 3859 |
* in become_shutdown() so normally we cannot free them yet. */ |
| 3860 |
watchdog_free_device(); |
| 3861 |
reset_arguments(); |
| 3862 |
return retval; |
| 3863 |
} |
| 3864 |
#endif |
| 3865 |
|
| 3866 |
#if HAS_FEATURE_ADDRESS_SANITIZER |
| 3867 |
/* At this stage we most likely don't have stdio/stderr open, so the following |
| 3868 |
* LSan check would not print any actionable information and would just crash |
| 3869 |
* PID 1. To make this a bit more helpful, let's try to open /dev/console, |
| 3870 |
* and if we succeed redirect LSan's report there. */ |
| 3871 |
if (getpid_cached() == 1) { |
| 3872 |
_cleanup_close_ int tty_fd = -EBADF; |
| 3873 |
|
| 3874 |
tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
| 3875 |
if (tty_fd >= 0) |
| 3876 |
__sanitizer_set_report_fd((void*) (intptr_t) tty_fd); |
| 3877 |
|
| 3878 |
__lsan_do_leak_check(); |
| 3879 |
} |
| 3880 |
#endif |
| 3881 |
|
| 3882 |
if (r < 0) |
| 3883 |
(void) sd_notifyf(/* unset_environment= */ false, |
| 3884 |
"ERRNO=%i", -r); |
| 3885 |
|
| 3886 |
/* Try to invoke the shutdown binary unless we already failed. |
| 3887 |
* If we failed above, we want to freeze after finishing cleanup. */ |
| 3888 |
if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM && |
| 3889 |
IN_SET(r, MANAGER_EXIT, MANAGER_REBOOT, MANAGER_POWEROFF, MANAGER_HALT, MANAGER_KEXEC)) { |
| 3890 |
r = become_shutdown(r, retval); |
| 3891 |
log_error_errno(r, "Failed to execute shutdown binary, %s: %m", getpid_cached() == 1 ? "freezing" : "quitting"); |
| 3892 |
error_message = "Failed to execute shutdown binary"; |
| 3893 |
} |
| 3894 |
|
| 3895 |
/* This is primarily useful when running systemd in a VM, as it provides the user running the VM with |
| 3896 |
* a mechanism to pick up systemd's exit status in the VM. */ |
| 3897 |
(void) sd_notifyf(/* unset_environment= */ false, |
| 3898 |
"EXIT_STATUS=%i", retval); |
| 3899 |
|
| 3900 |
watchdog_free_device(); |
| 3901 |
arg_watchdog_device = mfree(arg_watchdog_device); |
| 3902 |
|
| 3903 |
if (getpid_cached() == 1) { |
| 3904 |
if (error_message) |
| 3905 |
manager_status_printf(NULL, STATUS_TYPE_EMERGENCY, |
| 3906 |
ANSI_HIGHLIGHT_RED "!!!!!!" ANSI_NORMAL, |
| 3907 |
"%s.", error_message); |
| 3908 |
freeze_or_exit_or_reboot(); |
| 3909 |
} |
| 3910 |
|
| 3911 |
reset_arguments(); |
| 3912 |
return retval; |
| 3913 |
} |