#include "umount.h"
#include "util.h"
-#define TIMEOUT_USEC (5 * USEC_PER_SEC)
+#define TIMEOUT_USEC (5 * USEC_PER_SEC)
#define FINALIZE_ATTEMPTS 50
#define FINALIZE_CRITICAL_ATTEMPTS 10
return r;
}
-
int main(int argc, char *argv[]) {
int cmd, r, retries;
- bool need_umount = true, need_swapoff = true, need_loop_detach = true;
+ bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
log_parse_environment();
- log_set_target(LOG_TARGET_KMSG); /* syslog will die if not gone yet */
+ log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */
log_open();
if (getpid() != 1) {
- log_error("Not executed by init (pid-1).");
+ log_error("Not executed by init (pid 1).");
r = -EPERM;
goto error;
}
if (r < 0)
log_warning("Cannot send SIGKILL to all process: %s", strerror(r));
-
- /* preventing that we won't block umounts */
- if (chdir("/") != 0)
- log_warning("Cannot chdir(\"/\"): %m. Unmounts likely to fail.");
-
- /* umount all mountpoints, swaps, and loopback devices */
+ /* Unmount all mountpoints, swaps, and loopback devices */
retries = FINALIZE_ATTEMPTS;
- while (need_umount || need_swapoff || need_loop_detach) {
+ for (;;) {
if (need_umount) {
log_info("Unmounting filesystems.");
r = umount_all();
log_warning("Not all loop devices detached, %d left.", r);
else
log_error("Error detaching loop devices: %s", strerror(-r));
+ }
+ if (need_dm_detach) {
+ log_info("Detaching DM devices.");
+ r = dm_detach_all();
+ if (r == 0)
+ need_dm_detach = false;
+ else if (r > 0)
+ log_warning("Not all dm devices detached, %d left.", r);
+ else
+ log_error("Error detaching dm devices: %s", strerror(-r));
}
- if (need_umount || need_swapoff || need_loop_detach) {
+ if (need_umount || need_swapoff || need_loop_detach || need_dm_detach) {
retries--;
- if (retries <= FINALIZE_CRITICAL_ATTEMPTS) {
+ if (retries == FINALIZE_CRITICAL_ATTEMPTS) {
log_warning("Approaching critical level to finalize filesystem and devices, try to kill all processes.");
rescue_send_signal(SIGTERM);
rescue_send_signal(SIGKILL);
}
if (retries > 0)
- log_info("Action still required, %d tries left", retries);
+ log_info("Action still required, %d tries left.", retries);
else {
- log_error("Tried enough but still action required need_umount=%d, need_swapoff=%d, need_loop_detach=%d", need_umount, need_swapoff, need_loop_detach);
- r = -EBUSY;
- goto error;
+ log_error("Giving up. Actions left: Umount=%s, Swap off=%s, Loop detach=%s, dm detach=%s",
+ yes_no(need_umount), yes_no(need_swapoff), yes_no(need_loop_detach), yes_no(need_dm_detach));
+ break;
}
- }
+ } else
+ break;
}
sync();
if (cmd == LINUX_REBOOT_CMD_KEXEC) {
/* we cheat and exec kexec to avoid doing all its work */
pid_t pid = fork();
- if (pid < 0) {
- log_error("Could not fork: %m. Falling back to reboot.");
- cmd = RB_AUTOBOOT;
- } else if (pid > 0) {
- waitpid(pid, NULL, 0);
- log_warning("Failed %s -e -x -f. Falling back to reboot", KEXEC_BINARY_PATH);
- cmd = RB_AUTOBOOT;
+ if (pid < 0)
+ log_error("Could not fork: %m. Falling back to normal reboot.");
+ else if (pid > 0) {
+ wait_for_terminate_and_warn("kexec", pid);
+ log_warning("kexec failed. Falling back to normal reboot.");
} else {
- const char *args[5] = {KEXEC_BINARY_PATH, "-e", "-f", "-x", NULL};
+ /* Child */
+ const char *args[5] = { KEXEC_BINARY_PATH, "-e", "-f", "-x", NULL };
execv(args[0], (char * const *) args);
return EXIT_FAILURE;
}
+
+ cmd = RB_AUTOBOOT;
}
reboot(cmd);
- r = errno;
+ log_error("Failed to invoke reboot(): %m");
+ r = -errno;
error:
sync();
- if (r < 0)
- r = -r;
- log_error("Critical error while doing system shutdown: %s", strerror(r));
+ log_error("Critical error while doing system shutdown: %s", strerror(-r));
+
freeze();
return EXIT_FAILURE;
}