diff options
Diffstat (limited to 'src/core/shutdown.c')
-rw-r--r-- | src/core/shutdown.c | 58 |
1 files changed, 23 insertions, 35 deletions
diff --git a/src/core/shutdown.c b/src/core/shutdown.c index cc31b33f1c..58c9a9de79 100644 --- a/src/core/shutdown.c +++ b/src/core/shutdown.c @@ -42,6 +42,7 @@ #include "missing.h" #include "parse-util.h" #include "process-util.h" +#include "reboot-util.h" #include "signal-util.h" #include "string-util.h" #include "switch-root.h" @@ -93,14 +94,14 @@ static int parse_argv(int argc, char *argv[]) { case ARG_LOG_LEVEL: r = log_set_max_level_from_string(optarg); if (r < 0) - log_error("Failed to parse log level %s, ignoring.", optarg); + log_error_errno(r, "Failed to parse log level %s, ignoring.", optarg); break; case ARG_LOG_TARGET: r = log_set_target_from_string(optarg); if (r < 0) - log_error("Failed to parse log target %s, ignoring", optarg); + log_error_errno(r, "Failed to parse log target %s, ignoring", optarg); break; @@ -109,7 +110,7 @@ static int parse_argv(int argc, char *argv[]) { if (optarg) { r = log_show_color_from_string(optarg); if (r < 0) - log_error("Failed to parse log color setting %s, ignoring", optarg); + log_error_errno(r, "Failed to parse log color setting %s, ignoring", optarg); } else log_show_color(true); @@ -119,7 +120,7 @@ static int parse_argv(int argc, char *argv[]) { if (optarg) { r = log_show_location_from_string(optarg); if (r < 0) - log_error("Failed to parse log location setting %s, ignoring", optarg); + log_error_errno(r, "Failed to parse log location setting %s, ignoring", optarg); } else log_show_location(true); @@ -128,14 +129,14 @@ static int parse_argv(int argc, char *argv[]) { case ARG_EXIT_CODE: r = safe_atou8(optarg, &arg_exit_code); if (r < 0) - log_error("Failed to parse exit code %s, ignoring", optarg); + log_error_errno(r, "Failed to parse exit code %s, ignoring", optarg); break; case ARG_TIMEOUT: r = parse_sec(optarg, &arg_timeout); if (r < 0) - log_error("Failed to parse shutdown timeout %s, ignoring", optarg); + log_error_errno(r, "Failed to parse shutdown timeout %s, ignoring", optarg); break; @@ -276,15 +277,18 @@ int main(int argc, char *argv[]) { static const char* const dirs[] = {SYSTEM_SHUTDOWN_PATH, NULL}; char *watchdog_device; + /* The log target defaults to console, but the original systemd process will pass its log target in through a + * command line argument, which will override this default. Also, ensure we'll never log to the journal or + * syslog, as these logging daemons are either already dead or will die very soon. */ + + log_set_target(LOG_TARGET_CONSOLE); + log_set_prohibit_ipc(true); log_parse_environment(); + r = parse_argv(argc, argv); if (r < 0) goto error; - /* journald will die if not gone yet. The log target defaults - * to console, but may have been changed by command line options. */ - - log_set_prohibit_ipc(true); log_open(); umask(0022); @@ -306,8 +310,8 @@ int main(int argc, char *argv[]) { else if (streq(arg_verb, "exit")) cmd = 0; /* ignored, just checking that arg_verb is valid */ else { - r = -EINVAL; log_error("Unknown action '%s'.", arg_verb); + r = -EINVAL; goto error; } @@ -324,7 +328,7 @@ int main(int argc, char *argv[]) { } /* Lock us into memory */ - mlockall(MCL_CURRENT|MCL_FUTURE); + (void) mlockall(MCL_CURRENT|MCL_FUTURE); /* Synchronize everything that is not written to disk yet at this point already. This is a good idea so that * slow IO is processed here already and the final process killing spree is not impacted by processes @@ -481,12 +485,9 @@ int main(int argc, char *argv[]) { if (streq(arg_verb, "exit")) { if (in_container) - exit(arg_exit_code); - else { - /* We cannot exit() on the host, fallback on another - * method. */ - cmd = RB_POWER_OFF; - } + return arg_exit_code; + + cmd = RB_POWER_OFF; /* We cannot exit() on the host, fallback on another method. */ } switch (cmd) { @@ -514,22 +515,9 @@ int main(int argc, char *argv[]) { cmd = RB_AUTOBOOT; _fallthrough_; - case RB_AUTOBOOT: - - if (!in_container) { - _cleanup_free_ char *param = NULL; - - r = read_one_line_file("/run/systemd/reboot-param", ¶m); - if (r < 0 && r != -ENOENT) - log_warning_errno(r, "Failed to read reboot parameter file: %m"); - - if (!isempty(param)) { - log_info("Rebooting with argument '%s'.", param); - syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, param); - log_warning_errno(errno, "Failed to reboot with parameter, retrying without: %m"); - } - } + case RB_AUTOBOOT: + (void) reboot_with_parameter(REBOOT_LOG); log_info("Rebooting."); break; @@ -545,13 +533,13 @@ int main(int argc, char *argv[]) { assert_not_reached("Unknown magic"); } - reboot(cmd); + (void) reboot(cmd); if (errno == EPERM && in_container) { /* If we are in a container, and we lacked * CAP_SYS_BOOT just exit, this will kill our * container for good. */ log_info("Exiting container."); - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; } r = log_error_errno(errno, "Failed to invoke reboot(): %m"); |