shutdown: unify handling of reboot() syscall a bit

This commit is contained in:
Lennart Poettering 2013-11-08 19:32:45 +01:00
parent 6d6d40c910
commit 477def8097
4 changed files with 74 additions and 44 deletions

4
TODO
View File

@ -46,9 +46,11 @@ CGroup Rework Completion:
Features: Features:
* increase journal files by a few MB each time, instead of piecemeal
* add field to transient units that indicate whether systemd or somebody else saves/restores its settings, for integration with libvirt * add field to transient units that indicate whether systemd or somebody else saves/restores its settings, for integration with libvirt
* wait filter, reboot() filter unification, unify dispatch table in systemctl_main() and friends, convert all to bus_log_create_error() * wait filter, unify dispatch table in systemctl_main() and friends, convert all to bus_log_create_error()
* bus: access policy as vtable flag * bus: access policy as vtable flag

View File

@ -135,7 +135,7 @@ static int pivot_to_new_root(void) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true; bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
bool in_container, use_watchdog = false; bool in_container, use_watchdog = false;
_cleanup_free_ char *line = NULL, *cgroup = NULL, *param = NULL; _cleanup_free_ char *line = NULL, *cgroup = NULL;
char *arguments[3]; char *arguments[3];
unsigned retries; unsigned retries;
int cmd, r; int cmd, r;
@ -174,11 +174,9 @@ int main(int argc, char *argv[]) {
in_container = detect_container(NULL) > 0; in_container = detect_container(NULL) > 0;
if (streq(argv[1], "reboot")) { if (streq(argv[1], "reboot"))
cmd = RB_AUTOBOOT; cmd = RB_AUTOBOOT;
/* if this fails, that's OK */ else if (streq(argv[1], "poweroff"))
read_one_line_file(REBOOT_PARAM_FILE, &param);
} else if (streq(argv[1], "poweroff"))
cmd = RB_POWER_OFF; cmd = RB_POWER_OFF;
else if (streq(argv[1], "halt")) else if (streq(argv[1], "halt"))
cmd = RB_HALT_SYSTEM; cmd = RB_HALT_SYSTEM;
@ -318,39 +316,69 @@ int main(int argc, char *argv[]) {
if (!in_container) if (!in_container)
sync(); sync();
if (cmd == LINUX_REBOOT_CMD_KEXEC) { switch (cmd) {
case LINUX_REBOOT_CMD_KEXEC:
if (!in_container) { if (!in_container) {
/* We cheat and exec kexec to avoid doing all its work */ /* We cheat and exec kexec to avoid doing all its work */
pid_t pid = fork(); pid_t pid;
log_info("Rebooting with kexec.");
pid = fork();
if (pid < 0) if (pid < 0)
log_error("Could not fork: %m. Falling back to normal reboot."); log_error("Failed to fork: %m");
else if (pid > 0) { else if (pid == 0) {
wait_for_terminate_and_warn("kexec", pid);
log_warning("kexec failed. Falling back to normal reboot."); const char * const args[] = {
} else { KEXEC, "-e", NULL
};
/* Child */ /* Child */
const char *args[3] = { KEXEC, "-e", NULL };
execv(args[0], (char * const *) args); execv(args[0], (char * const *) args);
return EXIT_FAILURE; _exit(EXIT_FAILURE);
} } else
wait_for_terminate_and_warn("kexec", pid);
} }
cmd = RB_AUTOBOOT; cmd = RB_AUTOBOOT;
/* Fall through */
case RB_AUTOBOOT:
if (!in_container) {
_cleanup_free_ char *param = NULL;
if (read_one_line_file(REBOOT_PARAM_FILE, &param) >= 0) {
log_info("Rebooting with argument '%s'.", param);
syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, param);
}
}
log_info("Rebooting.");
break;
case RB_POWER_OFF:
log_info("Powering off.");
break;
case RB_HALT_SYSTEM:
log_info("Halting system.");
break;
default:
assert_not_reached("Unknown magic");
} }
if (param) reboot(cmd);
syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, param);
else
reboot(cmd);
if (errno == EPERM && in_container) { if (errno == EPERM && in_container) {
/* If we are in a container, and we lacked /* If we are in a container, and we lacked
* CAP_SYS_BOOT just exit, this will kill our * CAP_SYS_BOOT just exit, this will kill our
* container for good. */ * container for good. */
log_error("Exiting container."); log_info("Exiting container.");
exit(0); exit(0);
} }

View File

@ -77,6 +77,7 @@
#include "utf8.h" #include "utf8.h"
#include "gunicode.h" #include "gunicode.h"
#include "virt.h" #include "virt.h"
#include "def.h"
int saved_argc = 0; int saved_argc = 0;
char **saved_argv = NULL; char **saved_argv = NULL;

View File

@ -135,7 +135,7 @@ static OutputMode arg_output = OUTPUT_SHORT;
static bool arg_plain = false; static bool arg_plain = false;
static int daemon_reload(sd_bus *bus, char **args); static int daemon_reload(sd_bus *bus, char **args);
static void halt_now(enum action a); static int halt_now(enum action a);
static void pager_open_if_enabled(void) { static void pager_open_if_enabled(void) {
@ -2033,7 +2033,7 @@ static int start_special(sd_bus *bus, char **args) {
(a == ACTION_HALT || (a == ACTION_HALT ||
a == ACTION_POWEROFF || a == ACTION_POWEROFF ||
a == ACTION_REBOOT)) a == ACTION_REBOOT))
halt_now(a); return halt_now(a);
if (arg_force >= 1 && if (arg_force >= 1 &&
(a == ACTION_HALT || (a == ACTION_HALT ||
@ -5609,11 +5609,9 @@ done:
return 0; return 0;
} }
static _noreturn_ void halt_now(enum action a) { static int halt_now(enum action a) {
_cleanup_free_ char *param = NULL; /* Make sure C-A-D is handled by the kernel from this
/* Make sure C-A-D is handled by the kernel from this
* point on... */ * point on... */
reboot(RB_ENABLE_CAD); reboot(RB_ENABLE_CAD);
@ -5622,30 +5620,30 @@ static _noreturn_ void halt_now(enum action a) {
case ACTION_HALT: case ACTION_HALT:
log_info("Halting."); log_info("Halting.");
reboot(RB_HALT_SYSTEM); reboot(RB_HALT_SYSTEM);
break; return -errno;
case ACTION_POWEROFF: case ACTION_POWEROFF:
log_info("Powering off."); log_info("Powering off.");
reboot(RB_POWER_OFF); reboot(RB_POWER_OFF);
break; return -errno;
case ACTION_REBOOT: case ACTION_REBOOT: {
_cleanup_free_ char *param = NULL;
if (read_one_line_file(REBOOT_PARAM_FILE, &param) == 0) { if (read_one_line_file(REBOOT_PARAM_FILE, &param) >= 0) {
log_info("Rebooting with arg '%s'.", param); log_info("Rebooting with argument '%s'.", param);
syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, param); LINUX_REBOOT_CMD_RESTART2, param);
} else {
log_info("Rebooting.");
reboot(RB_AUTOBOOT);
} }
break;
default: log_info("Rebooting.");
assert_not_reached("Unknown halt action."); reboot(RB_AUTOBOOT);
return -errno;
} }
assert_not_reached("Uh? This shouldn't happen."); default:
assert_not_reached("Unknown action.");
}
} }
static int halt_main(sd_bus *bus) { static int halt_main(sd_bus *bus) {
@ -5717,9 +5715,10 @@ static int halt_main(sd_bus *bus) {
if (arg_dry) if (arg_dry)
return 0; return 0;
halt_now(arg_action); r = halt_now(arg_action);
/* We should never reach this. */ log_error("Failed to reboot: %s", strerror(-r));
return -ENOSYS;
return r;
} }
static int runlevel_main(void) { static int runlevel_main(void) {