diff options
author | Ludwig Nussel <ludwig.nussel@suse.de> | 2021-12-14 17:27:05 +0100 |
---|---|---|
committer | Ludwig Nussel <ludwig.nussel@suse.de> | 2022-01-25 14:52:09 +0100 |
commit | adefc8789b63225662e50ceaa282f9553b5c64eb (patch) | |
tree | 93a01a33e851b199d9af8319bf743227b02d21ff | |
parent | logind: fix wall message for immediate shutdowns (diff) | |
download | systemd-adefc8789b63225662e50ceaa282f9553b5c64eb.tar.xz systemd-adefc8789b63225662e50ceaa282f9553b5c64eb.zip |
systemctl: simplify halt_main()
The code at this point is not able to tell whether it was called as
halt/poweroff/reboot or shutdown with time "now".
The code also takes a shortcut to skip logind if called as root.
That however means asking shutdown for immediate action won't trigger a
wall message.
As per https://github.com/systemd/systemd/issues/8424#issuecomment-374677315
all commands should trigger a wall message.
That simplifies the code as we can try logind first always.
-rw-r--r-- | src/systemctl/systemctl-compat-halt.c | 47 |
1 files changed, 19 insertions, 28 deletions
diff --git a/src/systemctl/systemctl-compat-halt.c b/src/systemctl/systemctl-compat-halt.c index 760758322f5..7d95d34d542 100644 --- a/src/systemctl/systemctl-compat-halt.c +++ b/src/systemctl/systemctl-compat-halt.c @@ -144,35 +144,23 @@ int halt_parse_argv(int argc, char *argv[]) { int halt_main(void) { int r; - r = logind_check_inhibitors(arg_action); - if (r < 0) - return r; - - /* Delayed shutdown requested, and was successful */ - if (arg_when > 0 && logind_schedule_shutdown() == 0) - return 0; - - /* No delay, or logind failed or is not at all available */ - if (geteuid() != 0) { - if (arg_dry_run || arg_force > 0) { - (void) must_be_root(); - return -EPERM; - } + /* always try logind first */ + if (arg_when > 0) + r = logind_schedule_shutdown(); + else { + r = logind_check_inhibitors(arg_action); + if (r < 0) + return r; - /* Try logind if we are a normal user and no special mode applies. Maybe polkit allows us to - * shutdown the machine. */ - if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_KEXEC, ACTION_HALT)) { - r = logind_reboot(arg_action); - if (r >= 0) - return r; - if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) - /* Requested operation is not supported on the local system or already in - * progress */ - return r; - - /* on all other errors, try low-level operation */ - } + r = logind_reboot(arg_action); } + if (r >= 0) + return r; + if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) + /* Requested operation is not supported on the local system or already in + * progress */ + return r; + /* on all other errors, try low-level operation */ /* In order to minimize the difference between operation with and without logind, we explicitly * enable non-blocking mode for this, as logind's shutdown operations are always non-blocking. */ @@ -181,7 +169,10 @@ int halt_main(void) { if (!arg_dry_run && !arg_force) return start_with_fallback(); - assert(geteuid() == 0); + if (geteuid() != 0) { + (void) must_be_root(); + return -EPERM; + } if (!arg_no_wtmp) { if (sd_booted() > 0) |