X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshutdown.c;h=2c14371df2692bf0b9105ccbc133cf7a7bc68309;hp=023648c4cf9d5e5c32b1405b1b56a0140e953ce7;hb=d48141ba0bed2866e05261cf06f0a6adfb11f349;hpb=b1b2a107d15a370d40b200172837bdd82ff3c3b2 diff --git a/src/shutdown.c b/src/shutdown.c index 023648c4c..2c14371df 100644 --- a/src/shutdown.c +++ b/src/shutdown.c @@ -36,15 +36,10 @@ #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 -_noreturn_ static void freeze(void) { - for (;;) - pause(); -} - static bool ignore_proc(pid_t pid) { if (pid == 1) return true; @@ -203,17 +198,16 @@ finish: 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; } @@ -252,14 +246,9 @@ int main(int argc, char *argv[]) { 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(); @@ -291,26 +280,37 @@ int main(int argc, char *argv[]) { 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(); @@ -318,28 +318,29 @@ int main(int argc, char *argv[]) { 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 0; + return EXIT_FAILURE; }