| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2010 ProFUSION embedded systems |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
#include <sys/mman.h> |
| 8 |
#include <sys/mount.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
#include "sd-daemon.h" |
| 13 |
#include "sd-json.h" |
| 14 |
#include "sd-messages.h" |
| 15 |
|
| 16 |
#include "alloc-util.h" |
| 17 |
#include "async.h" |
| 18 |
#include "binfmt-util.h" |
| 19 |
#include "cgroup-setup.h" |
| 20 |
#include "cgroup-util.h" |
| 21 |
#include "constants.h" |
| 22 |
#include "coredump-util.h" |
| 23 |
#include "detach-dm.h" |
| 24 |
#include "detach-loopback.h" |
| 25 |
#include "detach-md.h" |
| 26 |
#include "detach-swap.h" |
| 27 |
#include "errno-util.h" |
| 28 |
#include "exec-util.h" |
| 29 |
#include "fd-util.h" |
| 30 |
#include "fileio.h" |
| 31 |
#include "format-util.h" |
| 32 |
#include "initrd-util.h" |
| 33 |
#include "killall.h" |
| 34 |
#include "log.h" |
| 35 |
#include "luo-util.h" |
| 36 |
#include "options.h" |
| 37 |
#include "parse-util.h" |
| 38 |
#include "pidref.h" |
| 39 |
#include "printk-util.h" |
| 40 |
#include "process-util.h" |
| 41 |
#include "reboot-util.h" |
| 42 |
#include "rlimit-util.h" |
| 43 |
#include "shutdown.h" |
| 44 |
#include "signal-util.h" |
| 45 |
#include "string-util.h" |
| 46 |
#include "switch-root.h" |
| 47 |
#include "terminal-util.h" |
| 48 |
#include "time-util.h" |
| 49 |
#include "umount.h" |
| 50 |
#include "virt.h" |
| 51 |
#include "watchdog.h" |
| 52 |
|
| 53 |
#define SYNC_PROGRESS_ATTEMPTS 3 |
| 54 |
#define SYNC_TIMEOUT_USEC (10*USEC_PER_SEC) |
| 55 |
#define DEFAULT_MINIMUM_UPTIME_USEC (15U * USEC_PER_SEC) |
| 56 |
|
| 57 |
static const char *arg_verb = NULL; |
| 58 |
static uint8_t arg_exit_code = 0; |
| 59 |
static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC; |
| 60 |
|
| 61 |
static int parse_argv(int argc, char *argv[]) { |
| 62 |
assert(argc >= 1); |
| 63 |
assert(argv); |
| 64 |
|
| 65 |
/* The interface is: the verb must stay in argv[1]. Any extra positional arguments |
| 66 |
* are warned about and ignored. See 4b5d8d0f22ae61ceb45a25391354ba53b43ee992. |
| 67 |
* |
| 68 |
* Note: when new options are added here, also add them to the exclusion list in proc-cmdline.c! */ |
| 69 |
|
| 70 |
OptionParser opts = { argc, argv, OPTION_PARSER_RETURN_POSITIONAL_ARGS }; |
| 71 |
int r; |
| 72 |
|
| 73 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 74 |
switch (c) { |
| 75 |
|
| 76 |
OPTION_COMMON_LOG_LEVEL: |
| 77 |
r = log_set_max_level_from_string(opts.arg); |
| 78 |
if (r < 0) |
| 79 |
log_warning_errno(r, "Failed to parse log level %s, ignoring: %m", opts.arg); |
| 80 |
|
| 81 |
break; |
| 82 |
|
| 83 |
OPTION_COMMON_LOG_TARGET: |
| 84 |
r = log_set_target_from_string(opts.arg); |
| 85 |
if (r < 0) |
| 86 |
log_warning_errno(r, "Failed to parse log target %s, ignoring: %m", opts.arg); |
| 87 |
|
| 88 |
break; |
| 89 |
|
| 90 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "log-color", "BOOL", |
| 91 |
"Highlight important messages"): |
| 92 |
if (opts.arg) { |
| 93 |
r = log_show_color_from_string(opts.arg); |
| 94 |
if (r < 0) |
| 95 |
log_warning_errno(r, "Failed to parse log color setting %s, ignoring: %m", opts.arg); |
| 96 |
} else |
| 97 |
log_show_color(true); |
| 98 |
|
| 99 |
break; |
| 100 |
|
| 101 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "log-location", "BOOL", |
| 102 |
"Include code location in messages"): |
| 103 |
if (opts.arg) { |
| 104 |
r = log_show_location_from_string(opts.arg); |
| 105 |
if (r < 0) |
| 106 |
log_warning_errno(r, "Failed to parse log location setting %s, ignoring: %m", opts.arg); |
| 107 |
} else |
| 108 |
log_show_location(true); |
| 109 |
|
| 110 |
break; |
| 111 |
|
| 112 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "log-time", "BOOL", |
| 113 |
"Prefix messages with current time"): |
| 114 |
if (opts.arg) { |
| 115 |
r = log_show_time_from_string(opts.arg); |
| 116 |
if (r < 0) |
| 117 |
log_warning_errno(r, "Failed to parse log time setting %s, ignoring: %m", opts.arg); |
| 118 |
} else |
| 119 |
log_show_time(true); |
| 120 |
|
| 121 |
break; |
| 122 |
|
| 123 |
OPTION_LONG("exit-code", "N", |
| 124 |
"Exit code for reboot/kexec"): |
| 125 |
r = safe_atou8(opts.arg, &arg_exit_code); |
| 126 |
if (r < 0) |
| 127 |
log_warning_errno(r, "Failed to parse exit code %s, ignoring: %m", opts.arg); |
| 128 |
|
| 129 |
break; |
| 130 |
|
| 131 |
OPTION_LONG("timeout", "TIME", |
| 132 |
"Overall shutdown timeout"): |
| 133 |
r = parse_sec(opts.arg, &arg_timeout); |
| 134 |
if (r < 0) |
| 135 |
log_warning_errno(r, "Failed to parse shutdown timeout %s, ignoring: %m", opts.arg); |
| 136 |
|
| 137 |
break; |
| 138 |
|
| 139 |
OPTION_POSITIONAL: |
| 140 |
if (!arg_verb) |
| 141 |
arg_verb = opts.arg; |
| 142 |
else |
| 143 |
log_warning("Got extraneous argument, ignoring."); |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
if (!arg_verb) |
| 148 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Verb argument missing."); |
| 149 |
|
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
static int switch_root_initramfs(void) { |
| 154 |
/* Do not detach the old root, because /run/initramfs/shutdown needs to access it. |
| 155 |
* |
| 156 |
* Disable sync() during switch-root, we after all sync'ed here plenty, and a dumb sync (as opposed |
| 157 |
* to the "smart" sync() we did here that looks at progress parameters) would defeat much of our |
| 158 |
* efforts here. As the new root will be /run/initramfs/, it is not necessary to mount /run/ |
| 159 |
* recursively. */ |
| 160 |
return switch_root( |
| 161 |
/* new_root= */ "/run/initramfs", |
| 162 |
/* old_root_after= */ "/oldroot", |
| 163 |
/* flags= */ SWITCH_ROOT_DONT_SYNC); |
| 164 |
} |
| 165 |
|
| 166 |
/* Read the following fields from /proc/meminfo: |
| 167 |
* |
| 168 |
* NFS_Unstable |
| 169 |
* Writeback |
| 170 |
* Dirty |
| 171 |
* |
| 172 |
* Return true if the sum of these fields is greater than the previous |
| 173 |
* value input. For all other issues, report the failure and indicate that |
| 174 |
* the sync is not making progress. |
| 175 |
*/ |
| 176 |
static int sync_making_progress(unsigned long long *prev_dirty) { |
| 177 |
_cleanup_fclose_ FILE *f = NULL; |
| 178 |
unsigned long long val = 0; |
| 179 |
int r; |
| 180 |
|
| 181 |
assert(prev_dirty); |
| 182 |
|
| 183 |
f = fopen("/proc/meminfo", "re"); |
| 184 |
if (!f) |
| 185 |
return log_warning_errno(errno, "Failed to open %s: %m", "/proc/meminfo"); |
| 186 |
|
| 187 |
for (;;) { |
| 188 |
_cleanup_free_ char *line = NULL; |
| 189 |
unsigned long long ull; |
| 190 |
|
| 191 |
r = read_line(f, LONG_LINE_MAX, &line); |
| 192 |
if (r < 0) |
| 193 |
return log_warning_errno(r, "Failed to parse /proc/meminfo: %m"); |
| 194 |
if (r == 0) |
| 195 |
break; |
| 196 |
|
| 197 |
if (!first_word(line, "NFS_Unstable:") && !first_word(line, "Writeback:") && !first_word(line, "Dirty:")) |
| 198 |
continue; |
| 199 |
|
| 200 |
errno = 0; |
| 201 |
if (sscanf(line, "%*s %llu %*s", &ull) != 1) |
| 202 |
return log_warning_errno(errno_or_else(EIO), "Failed to parse /proc/meminfo field: %m"); |
| 203 |
|
| 204 |
val += ull; |
| 205 |
} |
| 206 |
|
| 207 |
r = *prev_dirty > val; |
| 208 |
*prev_dirty = val; |
| 209 |
return r; |
| 210 |
} |
| 211 |
|
| 212 |
int sync_with_progress(int fd) { |
| 213 |
unsigned long long dirty = ULLONG_MAX; |
| 214 |
_cleanup_free_ char *path = NULL; |
| 215 |
const char *what; |
| 216 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 217 |
int r; |
| 218 |
|
| 219 |
/* Due to the possibility of the sync operation hanging, we fork a child process and monitor |
| 220 |
* the progress. If the timeout lapses, the assumption is that the particular sync stalled. */ |
| 221 |
|
| 222 |
if (fd >= 0) { |
| 223 |
r = asynchronous_fsync(fd, &pidref); |
| 224 |
if (r < 0) |
| 225 |
return log_error_errno(r, "Failed to fork fsync(): %m"); |
| 226 |
|
| 227 |
(void) fd_get_path(fd, &path); |
| 228 |
} else { |
| 229 |
r = asynchronous_sync(&pidref); |
| 230 |
if (r < 0) |
| 231 |
return log_error_errno(r, "Failed to fork sync(): %m"); |
| 232 |
} |
| 233 |
|
| 234 |
what = path ?: "filesystems and block devices"; |
| 235 |
log_info("Syncing %s.", what); |
| 236 |
|
| 237 |
/* Start monitoring the sync operation. If more than |
| 238 |
* SYNC_PROGRESS_ATTEMPTS lapse without progress being made, |
| 239 |
* we assume that the sync is stalled */ |
| 240 |
for (unsigned checks = 0; checks < SYNC_PROGRESS_ATTEMPTS; checks++) { |
| 241 |
siginfo_t si; |
| 242 |
|
| 243 |
r = pidref_wait_for_terminate_full(&pidref, SYNC_TIMEOUT_USEC, &si); |
| 244 |
if (r >= 0 && si.si_code == CLD_EXITED && si.si_status == EXIT_SUCCESS) |
| 245 |
/* Sync finished without error (sync() call itself does not return an error code) */ |
| 246 |
return 0; |
| 247 |
if (r != -ETIMEDOUT) |
| 248 |
return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EPROTO), |
| 249 |
"Failed to sync %s: %m", what); |
| 250 |
|
| 251 |
/* Reset the check counter if we made some progress */ |
| 252 |
if (sync_making_progress(&dirty) > 0) |
| 253 |
checks = 0; |
| 254 |
} |
| 255 |
|
| 256 |
/* Only reached in the event of a timeout. We should issue a kill to the stray process. */ |
| 257 |
r = log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT), |
| 258 |
"Syncing %s - timed out, issuing SIGKILL to PID "PID_FMT".", what, pidref.pid); |
| 259 |
(void) pidref_kill(&pidref, SIGKILL); |
| 260 |
return r; |
| 261 |
} |
| 262 |
|
| 263 |
static void bump_sysctl_printk_log_level(int min_level) { |
| 264 |
/* Set the logging level to be able to see messages with log level smaller or equal to min_level */ |
| 265 |
|
| 266 |
int current_lvl = sysctl_printk_read(); |
| 267 |
if (current_lvl < 0 || current_lvl >= min_level + 1) |
| 268 |
return; |
| 269 |
|
| 270 |
(void) sysctl_printk_write(min_level + 1); |
| 271 |
} |
| 272 |
|
| 273 |
static void init_watchdog(void) { |
| 274 |
const char *s; |
| 275 |
int r; |
| 276 |
|
| 277 |
/* NB: we do not insist on $WATCHDOG_PID being set because old systemd versions didn't set it at all, |
| 278 |
* and we want to retain some basic compatibility between an old service manager and a new shutdown |
| 279 |
* binary. If it *is* set we'll insist on it being set to 1 however. */ |
| 280 |
s = secure_getenv("WATCHDOG_PID"); |
| 281 |
if (s) { |
| 282 |
pid_t pid; |
| 283 |
|
| 284 |
r = parse_pid(s, &pid); |
| 285 |
if (r < 0) |
| 286 |
log_warning_errno(r, "Failed to parse $WATCHDOG_PID, ignoring: %s", s); |
| 287 |
else if (pid != getpid_cached()) { |
| 288 |
log_warning("$WATCHDOG_PID set, but not to " PID_FMT ", skipping watchdog logic.", getpid_cached()); |
| 289 |
return; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
s = secure_getenv("WATCHDOG_DEVICE"); |
| 294 |
if (s) { |
| 295 |
r = watchdog_set_device(s); |
| 296 |
if (r < 0) |
| 297 |
log_warning_errno(r, "Failed to set watchdog device to '%s', ignoring: %m", s); |
| 298 |
} |
| 299 |
|
| 300 |
s = secure_getenv("WATCHDOG_USEC"); |
| 301 |
if (s) { |
| 302 |
usec_t usec; |
| 303 |
|
| 304 |
r = safe_atou64(s, &usec); |
| 305 |
if (r < 0) |
| 306 |
log_warning_errno(r, "Failed to parse watchdog timeout '%s', ignoring: %m", s); |
| 307 |
else |
| 308 |
(void) watchdog_setup(usec); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
static void notify_supervisor(void) { |
| 313 |
/* Notify VMM/container manager of the desired mode of reboot and the boot parameter */ |
| 314 |
_cleanup_free_ char *reboot_parameter = NULL; |
| 315 |
int r; |
| 316 |
|
| 317 |
r = read_reboot_parameter(&reboot_parameter); |
| 318 |
if (r < 0 && r != -ENOENT) |
| 319 |
log_debug_errno(r, "Failed to read reboot parameter, ignoring: %m"); |
| 320 |
|
| 321 |
if (reboot_parameter) |
| 322 |
(void) sd_notifyf(/* unset_environment= */ false, |
| 323 |
"EXIT_STATUS=%i\n" |
| 324 |
"X_SYSTEMD_SHUTDOWN=%s\n" |
| 325 |
"X_SYSTEMD_REBOOT_PARAMETER=%s", |
| 326 |
arg_exit_code, arg_verb, reboot_parameter); |
| 327 |
else |
| 328 |
(void) sd_notifyf(/* unset_environment= */ false, |
| 329 |
"EXIT_STATUS=%i\n" |
| 330 |
"X_SYSTEMD_SHUTDOWN=%s", |
| 331 |
arg_exit_code, arg_verb); |
| 332 |
} |
| 333 |
|
| 334 |
static void sleep_until_minimum_uptime(void) { |
| 335 |
uint64_t minimum_uptime_usec = DEFAULT_MINIMUM_UPTIME_USEC; |
| 336 |
int r; |
| 337 |
|
| 338 |
const char *e = secure_getenv("MINIMUM_UPTIME_USEC"); |
| 339 |
if (e) { |
| 340 |
r = safe_atou64(e, &minimum_uptime_usec); |
| 341 |
if (r < 0) |
| 342 |
log_warning_errno(r, "Failed to parse $MINIMUM_UPTIME_USEC, ignoring: %s", e); |
| 343 |
} else if (detect_virtualization() != VIRTUALIZATION_NONE) |
| 344 |
/* Enforce the minimum uptime, but don't bother with it in containers/VMs, since – unlike on |
| 345 |
* bare metal – the screen output isn't flushed out immediately when we reboot (as real PC |
| 346 |
* firmwares do). But skip only if there wasn't an explicit configuration, to let users |
| 347 |
* override this. */ |
| 348 |
return; |
| 349 |
|
| 350 |
if (minimum_uptime_usec <= 0) /* turned off? */ |
| 351 |
return; |
| 352 |
|
| 353 |
for (;;) { |
| 354 |
usec_t n = now(CLOCK_BOOTTIME); |
| 355 |
if (n >= minimum_uptime_usec) |
| 356 |
break; |
| 357 |
|
| 358 |
usec_t m = minimum_uptime_usec - n; |
| 359 |
log_notice("Delaying shutdown for %s, in order to reach minimum uptime of %s.", |
| 360 |
FORMAT_TIMESPAN(m, USEC_PER_SEC), |
| 361 |
FORMAT_TIMESPAN(minimum_uptime_usec, USEC_PER_SEC)); |
| 362 |
|
| 363 |
/* Sleep for up to 3s, then show message again, as a progress indicator. */ |
| 364 |
usleep_safe(MIN(m, 3 * USEC_PER_SEC)); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
int main(int argc, char *argv[]) { |
| 369 |
static const char* const dirs[] = { |
| 370 |
SYSTEM_SHUTDOWN_PATH, |
| 371 |
NULL |
| 372 |
}; |
| 373 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *luo_serialization = NULL; |
| 374 |
_cleanup_close_ int luo_session_fd = -EBADF; |
| 375 |
_cleanup_free_ int *luo_fds = NULL; |
| 376 |
_cleanup_free_ char *cgroup = NULL; |
| 377 |
size_t n_luo_fds = 0; |
| 378 |
int cmd, r; |
| 379 |
|
| 380 |
/* If PID 1 passed us an LUO serialization fd, parse it first so we know which fds to keep open. */ |
| 381 |
(void) luo_parse_serialization(&luo_serialization, &luo_fds, &n_luo_fds); |
| 382 |
|
| 383 |
/* Close random fds we might have get passed, just for paranoia, before we open any new fds, for |
| 384 |
* example for logging. After all this tool's purpose is about detaching any pinned resources, and |
| 385 |
* open file descriptors are the primary way to pin resources. Note that we don't really expect any |
| 386 |
* fds to be passed here, except for LUO fds that need to survive until kexec. */ |
| 387 |
(void) close_all_fds(luo_fds, n_luo_fds); |
| 388 |
|
| 389 |
/* The log target defaults to console, but the original systemd process will pass its log target in through a |
| 390 |
* command line argument, which will override this default. Also, ensure we'll never log to the journal or |
| 391 |
* syslog, as these logging daemons are either already dead or will die very soon. */ |
| 392 |
|
| 393 |
log_set_target(LOG_TARGET_CONSOLE); |
| 394 |
log_set_prohibit_ipc(true); |
| 395 |
log_parse_environment(); |
| 396 |
|
| 397 |
if (getpid_cached() != 1) { |
| 398 |
log_error("Not executed by init (PID 1). Refusing to operate."); |
| 399 |
return EXIT_FAILURE; |
| 400 |
} |
| 401 |
|
| 402 |
log_set_always_reopen_console(true); |
| 403 |
|
| 404 |
/* Re-enable reboot on Ctrl-Alt-Delete, so that if close/broadcast_signal/umount/... stalls, |
| 405 |
* or an error is encountered and we freeze(), the user can still initiate a force reboot |
| 406 |
* through kernel. */ |
| 407 |
(void) reboot(RB_ENABLE_CAD); |
| 408 |
|
| 409 |
r = parse_argv(argc, argv); |
| 410 |
if (r < 0) |
| 411 |
goto error; |
| 412 |
|
| 413 |
log_open(); |
| 414 |
|
| 415 |
umask(0022); |
| 416 |
|
| 417 |
if (streq(arg_verb, "reboot")) |
| 418 |
cmd = RB_AUTOBOOT; |
| 419 |
else if (streq(arg_verb, "poweroff")) |
| 420 |
cmd = RB_POWER_OFF; |
| 421 |
else if (streq(arg_verb, "halt")) |
| 422 |
cmd = RB_HALT_SYSTEM; |
| 423 |
else if (streq(arg_verb, "kexec")) |
| 424 |
cmd = LINUX_REBOOT_CMD_KEXEC; |
| 425 |
else if (streq(arg_verb, "exit")) |
| 426 |
cmd = 0; /* ignored, just checking that arg_verb is valid */ |
| 427 |
else { |
| 428 |
r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown action '%s'.", arg_verb); |
| 429 |
goto error; |
| 430 |
} |
| 431 |
|
| 432 |
(void) cg_get_root_path(&cgroup); |
| 433 |
bool in_container = detect_container() > 0; |
| 434 |
|
| 435 |
/* If the logging messages are going to KMSG, and if we are not running from a container, then try to |
| 436 |
* update the sysctl kernel.printk current value in order to see "info" messages; This current log |
| 437 |
* level is not updated if already big enough. |
| 438 |
*/ |
| 439 |
if (!in_container && |
| 440 |
IN_SET(log_get_target(), |
| 441 |
LOG_TARGET_AUTO, |
| 442 |
LOG_TARGET_JOURNAL_OR_KMSG, |
| 443 |
LOG_TARGET_SYSLOG_OR_KMSG, |
| 444 |
LOG_TARGET_KMSG)) |
| 445 |
bump_sysctl_printk_log_level(LOG_WARNING); |
| 446 |
|
| 447 |
init_watchdog(); |
| 448 |
|
| 449 |
/* Lock us into memory. If the first mlockall call fails, don't attempt it again. */ |
| 450 |
if (safe_mlockall(MCL_FUTURE|MCL_ONFAULT) >= 0) |
| 451 |
(void) mlockall(MCL_CURRENT); |
| 452 |
|
| 453 |
/* We need to make mounts private so that we can MS_MOVE in unmount_all(). Kernel does not allow |
| 454 |
* MS_MOVE when parent mountpoints have shared propagation. */ |
| 455 |
if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0) |
| 456 |
log_warning_errno(errno, "Failed to make mounts private, ignoring: %m"); |
| 457 |
|
| 458 |
/* Synchronize everything that is not written to disk yet at this point already. This is a good idea so that |
| 459 |
* slow IO is processed here already and the final process killing spree is not impacted by processes |
| 460 |
* desperately trying to sync IO to disk within their timeout. Do not remove this sync, data corruption will |
| 461 |
* result. */ |
| 462 |
if (!in_container) |
| 463 |
(void) sync_with_progress(-EBADF); |
| 464 |
|
| 465 |
disable_coredumps(); |
| 466 |
(void) disable_binfmt(); |
| 467 |
|
| 468 |
log_info("Sending SIGTERM to remaining processes..."); |
| 469 |
broadcast_signal(SIGTERM, true, true, arg_timeout); |
| 470 |
|
| 471 |
log_info("Sending SIGKILL to remaining processes..."); |
| 472 |
broadcast_signal(SIGKILL, true, false, arg_timeout); |
| 473 |
|
| 474 |
bool need_umount = !in_container, need_swapoff = !in_container, need_loop_detach = !in_container, |
| 475 |
need_dm_detach = !in_container, need_md_detach = !in_container, |
| 476 |
can_exitrd = !in_container && !in_initrd() && access("/run/initramfs/shutdown", X_OK) >= 0; |
| 477 |
|
| 478 |
/* Unmount all mountpoints, swaps, and loopback devices */ |
| 479 |
for (bool last_try = false;;) { |
| 480 |
bool changed = false; |
| 481 |
|
| 482 |
(void) watchdog_ping(); |
| 483 |
|
| 484 |
/* Let's trim the cgroup tree on each iteration so that we leave an empty cgroup tree around, |
| 485 |
* so that container managers get a nice notify event when we are down */ |
| 486 |
if (cgroup) |
| 487 |
(void) cg_trim(cgroup, false); |
| 488 |
|
| 489 |
if (need_umount) { |
| 490 |
log_info("Unmounting file systems."); |
| 491 |
r = umount_all(&changed, last_try); |
| 492 |
if (r == 0) { |
| 493 |
need_umount = false; |
| 494 |
log_info("All filesystems unmounted."); |
| 495 |
} else if (r > 0) |
| 496 |
log_info("Not all file systems unmounted, %d left.", r); |
| 497 |
else |
| 498 |
log_error_errno(r, "Unable to unmount file systems: %m"); |
| 499 |
} |
| 500 |
|
| 501 |
if (need_swapoff) { |
| 502 |
log_info("Deactivating swaps."); |
| 503 |
r = swapoff_all(&changed); |
| 504 |
if (r == 0) { |
| 505 |
need_swapoff = false; |
| 506 |
log_info("All swaps deactivated."); |
| 507 |
} else if (r > 0) |
| 508 |
log_info("Not all swaps deactivated, %d left.", r); |
| 509 |
else |
| 510 |
log_error_errno(r, "Unable to deactivate swaps: %m"); |
| 511 |
} |
| 512 |
|
| 513 |
if (need_loop_detach) { |
| 514 |
log_info("Detaching loop devices."); |
| 515 |
r = loopback_detach_all(&changed, last_try); |
| 516 |
if (r == 0) { |
| 517 |
need_loop_detach = false; |
| 518 |
log_info("All loop devices detached."); |
| 519 |
} else if (r > 0) |
| 520 |
log_info("Not all loop devices detached, %d left.", r); |
| 521 |
else |
| 522 |
log_error_errno(r, "Unable to detach loop devices: %m"); |
| 523 |
} |
| 524 |
|
| 525 |
if (need_md_detach) { |
| 526 |
log_info("Stopping MD devices."); |
| 527 |
r = md_detach_all(&changed, last_try); |
| 528 |
if (r == 0) { |
| 529 |
need_md_detach = false; |
| 530 |
log_info("All MD devices stopped."); |
| 531 |
} else if (r > 0) |
| 532 |
log_info("Not all MD devices stopped, %d left.", r); |
| 533 |
else |
| 534 |
log_error_errno(r, "Unable to stop MD devices: %m"); |
| 535 |
} |
| 536 |
|
| 537 |
if (need_dm_detach) { |
| 538 |
log_info("Detaching DM devices."); |
| 539 |
r = dm_detach_all(&changed, last_try); |
| 540 |
if (r == 0) { |
| 541 |
need_dm_detach = false; |
| 542 |
log_info("All DM devices detached."); |
| 543 |
} else if (r > 0) |
| 544 |
log_info("Not all DM devices detached, %d left.", r); |
| 545 |
else |
| 546 |
log_error_errno(r, "Unable to detach DM devices: %m"); |
| 547 |
} |
| 548 |
|
| 549 |
if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach |
| 550 |
&& !need_md_detach) { |
| 551 |
log_info("All filesystems, swaps, loop devices, MD devices and DM devices detached."); |
| 552 |
/* Yay, done */ |
| 553 |
break; |
| 554 |
} |
| 555 |
|
| 556 |
if (!changed && !last_try && !can_exitrd) { |
| 557 |
/* There are things we cannot get rid of. Loop one more time in which we will log |
| 558 |
* with higher priority to inform the user. Note that we don't need to do this if |
| 559 |
* there is an exitrd to switch to, because that one is likely to get rid of the |
| 560 |
* remaining mounts. If not, it will log about them. */ |
| 561 |
last_try = true; |
| 562 |
continue; |
| 563 |
} |
| 564 |
|
| 565 |
/* If in this iteration we didn't manage to unmount/deactivate anything, we simply give up */ |
| 566 |
if (!changed) { |
| 567 |
log_info("Cannot finalize remaining%s%s%s%s%s continuing.", |
| 568 |
need_umount ? " file systems," : "", |
| 569 |
need_swapoff ? " swap devices," : "", |
| 570 |
need_loop_detach ? " loop devices," : "", |
| 571 |
need_dm_detach ? " DM devices," : "", |
| 572 |
need_md_detach ? " MD devices," : ""); |
| 573 |
break; |
| 574 |
} |
| 575 |
|
| 576 |
log_debug("Couldn't finalize remaining %s%s%s%s%s trying again.", |
| 577 |
need_umount ? " file systems," : "", |
| 578 |
need_swapoff ? " swap devices," : "", |
| 579 |
need_loop_detach ? " loop devices," : "", |
| 580 |
need_dm_detach ? " DM devices," : "", |
| 581 |
need_md_detach ? " MD devices," : ""); |
| 582 |
} |
| 583 |
|
| 584 |
/* We're done with the watchdog. Note that the watchdog is explicitly not stopped here. It remains |
| 585 |
* active to guard against any issues during the rest of the shutdown sequence. Note that we |
| 586 |
* explicitly close the device with disarm=false here, before releasing the rest of the watchdog |
| 587 |
* data. */ |
| 588 |
watchdog_close(/* disarm= */ false); |
| 589 |
watchdog_free_device(); |
| 590 |
|
| 591 |
const char *arguments[] = { |
| 592 |
NULL, /* Filled in by execute_directories(), when needed */ |
| 593 |
arg_verb, |
| 594 |
NULL, |
| 595 |
}; |
| 596 |
(void) execute_directories( |
| 597 |
"system-shutdown", |
| 598 |
dirs, |
| 599 |
DEFAULT_TIMEOUT_USEC, |
| 600 |
/* callbacks= */ NULL, |
| 601 |
/* callback_args= */ NULL, |
| 602 |
(char**) arguments, |
| 603 |
/* envp= */ NULL, |
| 604 |
EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS); |
| 605 |
|
| 606 |
(void) rlimit_nofile_safe(); |
| 607 |
|
| 608 |
if (can_exitrd) { |
| 609 |
r = switch_root_initramfs(); |
| 610 |
if (r >= 0) { |
| 611 |
argv[0] = (char*) "/shutdown"; |
| 612 |
|
| 613 |
(void) setsid(); |
| 614 |
(void) make_console_stdio(); |
| 615 |
|
| 616 |
log_info("Successfully changed into root pivot.\n" |
| 617 |
"Entering exitrd..."); |
| 618 |
|
| 619 |
execv("/shutdown", argv); |
| 620 |
log_error_errno(errno, "Failed to execute shutdown binary: %m"); |
| 621 |
} else |
| 622 |
log_error_errno(r, "Failed to switch root to \"/run/initramfs\": %m"); |
| 623 |
} |
| 624 |
|
| 625 |
if (need_umount || need_swapoff || need_loop_detach || need_dm_detach || need_md_detach) |
| 626 |
log_error("Unable to finalize remaining%s%s%s%s%s ignoring.", |
| 627 |
need_umount ? " file systems," : "", |
| 628 |
need_swapoff ? " swap devices," : "", |
| 629 |
need_loop_detach ? " loop devices," : "", |
| 630 |
need_dm_detach ? " DM devices," : "", |
| 631 |
need_md_detach ? " MD devices," : ""); |
| 632 |
|
| 633 |
/* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need |
| 634 |
* to be sync'ed explicitly in advance. So let's do this here, but not needlessly slow down |
| 635 |
* containers. Note that we sync'ed things already once above, but we did some more work since then |
| 636 |
* which might have caused IO, hence let's do it once more. Do not remove this sync, data corruption |
| 637 |
* will result. */ |
| 638 |
if (!in_container) |
| 639 |
(void) sync_with_progress(-EBADF); |
| 640 |
|
| 641 |
notify_supervisor(); |
| 642 |
|
| 643 |
sleep_until_minimum_uptime(); |
| 644 |
|
| 645 |
if (streq(arg_verb, "exit")) { |
| 646 |
if (in_container) { |
| 647 |
log_info("Exiting container."); |
| 648 |
return arg_exit_code; |
| 649 |
} |
| 650 |
|
| 651 |
cmd = RB_POWER_OFF; /* We cannot exit() on the host, fallback on another method. */ |
| 652 |
} |
| 653 |
|
| 654 |
switch (cmd) { |
| 655 |
|
| 656 |
case LINUX_REBOOT_CMD_KEXEC: |
| 657 |
|
| 658 |
if (!in_container) { |
| 659 |
/* Preserve fd stores via the kernel Live Update Orchestrator before kexec. |
| 660 |
* The session fd must stay open until the kexec syscall. */ |
| 661 |
(void) luo_preserve_fd_stores(luo_serialization, &luo_session_fd); |
| 662 |
|
| 663 |
log_info("Rebooting with kexec."); |
| 664 |
|
| 665 |
(void) kexec(); |
| 666 |
|
| 667 |
/* If we are still running, then the kexec can't have worked, let's fall through */ |
| 668 |
} |
| 669 |
|
| 670 |
cmd = RB_AUTOBOOT; |
| 671 |
_fallthrough_; |
| 672 |
|
| 673 |
case RB_AUTOBOOT: |
| 674 |
(void) reboot_with_parameter(REBOOT_LOG); |
| 675 |
log_info("Rebooting."); |
| 676 |
break; |
| 677 |
|
| 678 |
case RB_POWER_OFF: |
| 679 |
log_info("Powering off."); |
| 680 |
break; |
| 681 |
|
| 682 |
case RB_HALT_SYSTEM: |
| 683 |
log_info("Halting system."); |
| 684 |
break; |
| 685 |
|
| 686 |
default: |
| 687 |
assert_not_reached(); |
| 688 |
} |
| 689 |
|
| 690 |
(void) reboot(cmd); |
| 691 |
if (ERRNO_IS_PRIVILEGE(errno) && in_container) { |
| 692 |
/* If we are in a container, and we lacked CAP_SYS_BOOT just exit, this will kill our |
| 693 |
* container for good. */ |
| 694 |
log_info("Exiting container."); |
| 695 |
return EXIT_SUCCESS; |
| 696 |
} |
| 697 |
|
| 698 |
r = log_error_errno(errno, "Failed to invoke reboot(): %m"); |
| 699 |
|
| 700 |
error: |
| 701 |
log_struct_errno(LOG_EMERG, r, |
| 702 |
LOG_MESSAGE("Critical error while doing system shutdown: %m"), |
| 703 |
LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN_ERROR_STR)); |
| 704 |
freeze(); |
| 705 |
} |