1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2010 ProFUSION embedded systems
8 #include <linux/reboot.h>
12 #include <sys/mount.h>
13 #include <sys/reboot.h>
17 #include "sd-daemon.h"
18 #include "sd-messages.h"
20 #include "alloc-util.h"
22 #include "binfmt-util.h"
23 #include "cgroup-setup.h"
24 #include "cgroup-util.h"
25 #include "constants.h"
26 #include "coredump-util.h"
27 #include "detach-dm.h"
28 #include "detach-loopback.h"
29 #include "detach-md.h"
30 #include "detach-swap.h"
31 #include "errno-util.h"
32 #include "exec-util.h"
35 #include "getopt-defs.h"
36 #include "initrd-util.h"
39 #include "parse-util.h"
40 #include "process-util.h"
41 #include "reboot-util.h"
42 #include "rlimit-util.h"
43 #include "signal-util.h"
44 #include "string-util.h"
45 #include "switch-root.h"
46 #include "sysctl-util.h"
47 #include "terminal-util.h"
52 #define SYNC_PROGRESS_ATTEMPTS 3
53 #define SYNC_TIMEOUT_USEC (10*USEC_PER_SEC)
55 static char* arg_verb
;
56 static uint8_t arg_exit_code
;
57 static usec_t arg_timeout
= DEFAULT_TIMEOUT_USEC
;
59 static int parse_argv(int argc
, char *argv
[]) {
65 static const struct option options
[] = {
66 COMMON_GETOPT_OPTIONS
,
67 SHUTDOWN_GETOPT_OPTIONS
,
76 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
77 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
80 /* "-" prevents getopt from permuting argv[] and moving the verb away
81 * from argv[1]. Our interface to initrd promises it'll be there. */
82 while ((c
= getopt_long(argc
, argv
, "-", options
, NULL
)) >= 0)
86 r
= log_set_max_level_from_string(optarg
);
88 log_error_errno(r
, "Failed to parse log level %s, ignoring: %m", optarg
);
93 r
= log_set_target_from_string(optarg
);
95 log_error_errno(r
, "Failed to parse log target %s, ignoring: %m", optarg
);
102 r
= log_show_color_from_string(optarg
);
104 log_error_errno(r
, "Failed to parse log color setting %s, ignoring: %m", optarg
);
106 log_show_color(true);
110 case ARG_LOG_LOCATION
:
112 r
= log_show_location_from_string(optarg
);
114 log_error_errno(r
, "Failed to parse log location setting %s, ignoring: %m", optarg
);
116 log_show_location(true);
123 r
= log_show_time_from_string(optarg
);
125 log_error_errno(r
, "Failed to parse log time setting %s, ignoring: %m", optarg
);
132 r
= safe_atou8(optarg
, &arg_exit_code
);
134 log_error_errno(r
, "Failed to parse exit code %s, ignoring: %m", optarg
);
139 r
= parse_sec(optarg
, &arg_timeout
);
141 log_error_errno(r
, "Failed to parse shutdown timeout %s, ignoring: %m", optarg
);
149 log_error("Excess arguments, ignoring");
156 assert_not_reached();
160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
161 "Verb argument missing.");
166 static int switch_root_initramfs(void) {
167 /* Do not detach the old root, because /run/initramfs/shutdown needs to access it.
169 * Disable sync() during switch-root, we after all sync'ed here plenty, and a dumb sync (as opposed
170 * to the "smart" sync() we did here that looks at progress parameters) would defeat much of our
171 * efforts here. As the new root will be /run/initramfs/, it is not necessary to mount /run/
174 /* new_root= */ "/run/initramfs",
175 /* old_root_after= */ "/oldroot",
176 /* flags= */ SWITCH_ROOT_DONT_SYNC
);
179 /* Read the following fields from /proc/meminfo:
185 * Return true if the sum of these fields is greater than the previous
186 * value input. For all other issues, report the failure and indicate that
187 * the sync is not making progress.
189 static int sync_making_progress(unsigned long long *prev_dirty
) {
190 _cleanup_fclose_
FILE *f
= NULL
;
191 unsigned long long val
= 0;
194 f
= fopen("/proc/meminfo", "re");
196 return log_warning_errno(errno
, "Failed to open /proc/meminfo: %m");
199 _cleanup_free_
char *line
= NULL
;
200 unsigned long long ull
= 0;
203 q
= read_line(f
, LONG_LINE_MAX
, &line
);
205 return log_warning_errno(q
, "Failed to parse /proc/meminfo: %m");
209 if (!first_word(line
, "NFS_Unstable:") && !first_word(line
, "Writeback:") && !first_word(line
, "Dirty:"))
213 if (sscanf(line
, "%*s %llu %*s", &ull
) != 1) {
215 log_warning_errno(errno
, "Failed to parse /proc/meminfo: %m");
217 log_warning("Failed to parse /proc/meminfo");
225 ret
= *prev_dirty
> val
;
230 static void sync_with_progress(void) {
231 unsigned long long dirty
= ULLONG_MAX
;
236 BLOCK_SIGNALS(SIGCHLD
);
238 /* Due to the possibility of the sync operation hanging, we fork a child process and monitor
239 * the progress. If the timeout lapses, the assumption is that the particular sync stalled. */
241 r
= asynchronous_sync(&pid
);
243 log_error_errno(r
, "Failed to fork sync(): %m");
247 log_info("Syncing filesystems and block devices.");
249 /* Start monitoring the sync operation. If more than
250 * SYNC_PROGRESS_ATTEMPTS lapse without progress being made,
251 * we assume that the sync is stalled */
252 for (checks
= 0; checks
< SYNC_PROGRESS_ATTEMPTS
; checks
++) {
253 r
= wait_for_terminate_with_timeout(pid
, SYNC_TIMEOUT_USEC
);
255 /* Sync finished without error.
256 * (The sync itself does not return an error code) */
258 else if (r
== -ETIMEDOUT
) {
259 /* Reset the check counter if the "Dirty" value is
261 if (sync_making_progress(&dirty
) > 0)
264 log_error_errno(r
, "Failed to sync filesystems and block devices: %m");
269 /* Only reached in the event of a timeout. We should issue a kill
270 * to the stray process. */
271 log_error("Syncing filesystems and block devices - timed out, issuing SIGKILL to PID "PID_FMT
".", pid
);
272 (void) kill(pid
, SIGKILL
);
275 static int read_current_sysctl_printk_log_level(void) {
276 _cleanup_free_
char *sysctl_printk_vals
= NULL
, *sysctl_printk_curr
= NULL
;
281 r
= sysctl_read("kernel/printk", &sysctl_printk_vals
);
283 return log_debug_errno(r
, "Cannot read sysctl kernel.printk: %m");
285 p
= sysctl_printk_vals
;
286 r
= extract_first_word(&p
, &sysctl_printk_curr
, NULL
, 0);
288 return log_debug_errno(r
, "Failed to split out kernel printk priority: %m");
290 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Short read while reading kernel.printk sysctl");
292 r
= safe_atoi(sysctl_printk_curr
, ¤t_lvl
);
294 return log_debug_errno(r
, "Failed to parse kernel.printk sysctl: %s", sysctl_printk_vals
);
299 static void bump_sysctl_printk_log_level(int min_level
) {
302 /* Set the logging level to be able to see messages with log level smaller or equal to min_level */
304 current_lvl
= read_current_sysctl_printk_log_level();
305 if (current_lvl
< 0 || current_lvl
>= min_level
+ 1)
308 r
= sysctl_writef("kernel/printk", "%i", min_level
+ 1);
310 log_debug_errno(r
, "Failed to bump kernel.printk to %i: %m", min_level
+ 1);
313 static void init_watchdog(void) {
317 s
= getenv("WATCHDOG_DEVICE");
319 r
= watchdog_set_device(s
);
321 log_warning_errno(r
, "Failed to set watchdog device to %s, ignoring: %m", s
);
324 s
= getenv("WATCHDOG_USEC");
328 r
= safe_atou64(s
, &usec
);
330 log_warning_errno(r
, "Failed to parse watchdog timeout '%s', ignoring: %m", s
);
332 (void) watchdog_setup(usec
);
336 static void notify_supervisor(void) {
337 /* Notify VMM/container manager of the desired mode of reboot and the boot parameter */
338 _cleanup_free_
char *reboot_parameter
= NULL
;
341 r
= read_reboot_parameter(&reboot_parameter
);
342 if (r
< 0 && r
!= -ENOENT
)
343 log_debug_errno(r
, "Failed to read reboot parameter, ignoring: %m");
345 if (reboot_parameter
)
346 (void) sd_notifyf(/* unset_environment= */ false,
347 "X_SYSTEMD_SHUTDOWN=%s\n"
348 "X_SYSTEMD_REBOOT_PARAMETER=%s",
349 arg_verb
, reboot_parameter
);
351 (void) sd_notifyf(/* unset_environment= */ false,
352 "X_SYSTEMD_SHUTDOWN=%s",
356 int main(int argc
, char *argv
[]) {
357 static const char* const dirs
[] = {
358 SYSTEM_SHUTDOWN_PATH
,
361 _cleanup_free_
char *cgroup
= NULL
;
365 /* Close random fds we might have get passed, just for paranoia, before we open any new fds, for
366 * example for logging. After all this tool's purpose is about detaching any pinned resources, and
367 * open file descriptors are the primary way to pin resources. Note that we don't really expect any
368 * fds to be passed here. */
369 (void) close_all_fds(NULL
, 0);
371 /* The log target defaults to console, but the original systemd process will pass its log target in through a
372 * command line argument, which will override this default. Also, ensure we'll never log to the journal or
373 * syslog, as these logging daemons are either already dead or will die very soon. */
375 log_set_target(LOG_TARGET_CONSOLE
);
376 log_set_prohibit_ipc(true);
377 log_parse_environment();
379 if (getpid_cached() == 1)
380 log_set_always_reopen_console(true);
382 r
= parse_argv(argc
, argv
);
390 if (getpid_cached() != 1) {
391 r
= log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Not executed by init (PID 1).");
395 if (streq(arg_verb
, "reboot"))
397 else if (streq(arg_verb
, "poweroff"))
399 else if (streq(arg_verb
, "halt"))
400 cmd
= RB_HALT_SYSTEM
;
401 else if (streq(arg_verb
, "kexec"))
402 cmd
= LINUX_REBOOT_CMD_KEXEC
;
403 else if (streq(arg_verb
, "exit"))
404 cmd
= 0; /* ignored, just checking that arg_verb is valid */
406 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown action '%s'.", arg_verb
);
410 /* This is primarily useful when running systemd in a VM, as it provides the user running the VM with
411 * a mechanism to pick up systemd's exit status in the VM. Note that we execute this as early as
412 * possible since otherwise we might shut down the VM before the AF_VSOCK buffers have been flushed.
413 * While this doesn't guarantee the message will arrive, in practice we do enough work after this
414 * that the message should always arrive on the host */
415 (void) sd_notifyf(0, "EXIT_STATUS=%i", arg_exit_code
);
417 (void) cg_get_root_path(&cgroup
);
418 bool in_container
= detect_container() > 0;
420 /* If the logging messages are going to KMSG, and if we are not running from a container, then try to
421 * update the sysctl kernel.printk current value in order to see "info" messages; This current log
422 * level is not updated if already big enough.
425 IN_SET(log_get_target(),
427 LOG_TARGET_JOURNAL_OR_KMSG
,
428 LOG_TARGET_SYSLOG_OR_KMSG
,
430 bump_sysctl_printk_log_level(LOG_WARNING
);
434 /* Lock us into memory */
435 (void) mlockall(MCL_CURRENT
|MCL_FUTURE
);
437 /* We need to make mounts private so that we can MS_MOVE in unmount_all(). Kernel does not allow
438 * MS_MOVE when parent mountpoints have shared propagation. */
439 if (mount(NULL
, "/", NULL
, MS_REC
|MS_PRIVATE
, NULL
) < 0)
440 log_warning_errno(errno
, "Failed to make mounts private, ignoring: %m");
442 /* Synchronize everything that is not written to disk yet at this point already. This is a good idea so that
443 * slow IO is processed here already and the final process killing spree is not impacted by processes
444 * desperately trying to sync IO to disk within their timeout. Do not remove this sync, data corruption will
447 sync_with_progress();
452 log_info("Sending SIGTERM to remaining processes...");
453 broadcast_signal(SIGTERM
, true, true, arg_timeout
);
455 log_info("Sending SIGKILL to remaining processes...");
456 broadcast_signal(SIGKILL
, true, false, arg_timeout
);
458 bool need_umount
= !in_container
, need_swapoff
= !in_container
, need_loop_detach
= !in_container
,
459 need_dm_detach
= !in_container
, need_md_detach
= !in_container
, can_initrd
, last_try
= false;
460 can_initrd
= !in_container
&& !in_initrd() && access("/run/initramfs/shutdown", X_OK
) == 0;
462 /* Unmount all mountpoints, swaps, and loopback devices */
464 bool changed
= false;
466 (void) watchdog_ping();
468 /* Let's trim the cgroup tree on each iteration so that we leave an empty cgroup tree around,
469 * so that container managers get a nice notify event when we are down */
471 (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, false);
474 log_info("Unmounting file systems.");
475 r
= umount_all(&changed
, last_try
);
478 log_info("All filesystems unmounted.");
480 log_info("Not all file systems unmounted, %d left.", r
);
482 log_error_errno(r
, "Unable to unmount file systems: %m");
486 log_info("Deactivating swaps.");
487 r
= swapoff_all(&changed
);
489 need_swapoff
= false;
490 log_info("All swaps deactivated.");
492 log_info("Not all swaps deactivated, %d left.", r
);
494 log_error_errno(r
, "Unable to deactivate swaps: %m");
497 if (need_loop_detach
) {
498 log_info("Detaching loop devices.");
499 r
= loopback_detach_all(&changed
, last_try
);
501 need_loop_detach
= false;
502 log_info("All loop devices detached.");
504 log_info("Not all loop devices detached, %d left.", r
);
506 log_error_errno(r
, "Unable to detach loop devices: %m");
509 if (need_md_detach
) {
510 log_info("Stopping MD devices.");
511 r
= md_detach_all(&changed
, last_try
);
513 need_md_detach
= false;
514 log_info("All MD devices stopped.");
516 log_info("Not all MD devices stopped, %d left.", r
);
518 log_error_errno(r
, "Unable to stop MD devices: %m");
521 if (need_dm_detach
) {
522 log_info("Detaching DM devices.");
523 r
= dm_detach_all(&changed
, last_try
);
525 need_dm_detach
= false;
526 log_info("All DM devices detached.");
528 log_info("Not all DM devices detached, %d left.", r
);
530 log_error_errno(r
, "Unable to detach DM devices: %m");
533 if (!need_umount
&& !need_swapoff
&& !need_loop_detach
&& !need_dm_detach
534 && !need_md_detach
) {
535 log_info("All filesystems, swaps, loop devices, MD devices and DM devices detached.");
540 if (!changed
&& !last_try
&& !can_initrd
) {
541 /* There are things we cannot get rid of. Loop one more time in which we will log
542 * with higher priority to inform the user. Note that we don't need to do this if
543 * there is an initrd to switch to, because that one is likely to get rid of the
544 * remaining mounts. If not, it will log about them. */
549 /* If in this iteration we didn't manage to unmount/deactivate anything, we simply give up */
551 log_info("Cannot finalize remaining%s%s%s%s%s continuing.",
552 need_umount
? " file systems," : "",
553 need_swapoff
? " swap devices," : "",
554 need_loop_detach
? " loop devices," : "",
555 need_dm_detach
? " DM devices," : "",
556 need_md_detach
? " MD devices," : "");
560 log_debug("Couldn't finalize remaining %s%s%s%s%s trying again.",
561 need_umount
? " file systems," : "",
562 need_swapoff
? " swap devices," : "",
563 need_loop_detach
? " loop devices," : "",
564 need_dm_detach
? " DM devices," : "",
565 need_md_detach
? " MD devices," : "");
568 /* We're done with the watchdog. Note that the watchdog is explicitly not stopped here. It remains
569 * active to guard against any issues during the rest of the shutdown sequence. */
570 watchdog_free_device();
572 arguments
[0] = NULL
; /* Filled in by execute_directories(), when needed */
573 arguments
[1] = arg_verb
;
575 (void) execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
, EXEC_DIR_PARALLEL
| EXEC_DIR_IGNORE_ERRORS
);
577 (void) rlimit_nofile_safe();
580 r
= switch_root_initramfs();
582 argv
[0] = (char*) "/shutdown";
585 (void) make_console_stdio();
587 log_info("Successfully changed into root pivot.\n"
588 "Returning to initrd...");
590 execv("/shutdown", argv
);
591 log_error_errno(errno
, "Failed to execute shutdown binary: %m");
593 log_error_errno(r
, "Failed to switch root to \"/run/initramfs\": %m");
596 if (need_umount
|| need_swapoff
|| need_loop_detach
|| need_dm_detach
|| need_md_detach
)
597 log_error("Unable to finalize remaining%s%s%s%s%s ignoring.",
598 need_umount
? " file systems," : "",
599 need_swapoff
? " swap devices," : "",
600 need_loop_detach
? " loop devices," : "",
601 need_dm_detach
? " DM devices," : "",
602 need_md_detach
? " MD devices," : "");
604 /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need
605 * to be sync'ed explicitly in advance. So let's do this here, but not needlessly slow down
606 * containers. Note that we sync'ed things already once above, but we did some more work since then
607 * which might have caused IO, hence let's do it once more. Do not remove this sync, data corruption
610 sync_with_progress();
614 if (streq(arg_verb
, "exit")) {
616 log_info("Exiting container.");
617 return arg_exit_code
;
620 cmd
= RB_POWER_OFF
; /* We cannot exit() on the host, fallback on another method. */
625 case LINUX_REBOOT_CMD_KEXEC
:
628 /* We cheat and exec kexec to avoid doing all its work */
629 log_info("Rebooting with kexec.");
631 r
= safe_fork("(sd-kexec)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
|FORK_WAIT
, NULL
);
633 const char * const args
[] = {
639 execv(args
[0], (char * const *) args
);
640 log_debug_errno(errno
, "Failed to execute '" KEXEC
"' binary, proceeding with reboot(RB_KEXEC): %m");
642 /* execv failed (kexec binary missing?), so try simply reboot(RB_KEXEC) */
647 /* If we are still running, then the kexec can't have worked, let's fall through */
654 (void) reboot_with_parameter(REBOOT_LOG
);
655 log_info("Rebooting.");
659 log_info("Powering off.");
663 log_info("Halting system.");
667 assert_not_reached();
671 if (ERRNO_IS_PRIVILEGE(errno
) && in_container
) {
672 /* If we are in a container, and we lacked CAP_SYS_BOOT just exit, this will kill our
673 * container for good. */
674 log_info("Exiting container.");
678 r
= log_error_errno(errno
, "Failed to invoke reboot(): %m");
681 log_struct_errno(LOG_EMERG
, r
,
682 LOG_MESSAGE("Critical error while doing system shutdown: %m"),
683 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_ERROR_STR
);