b11b5f
From 797b00e6a6f33d2b74beba02f678bf4d12e2146b Mon Sep 17 00:00:00 2001
b11b5f
From: Ludwig Nussel <ludwig.nussel@suse.de>
b11b5f
Date: Tue, 14 Dec 2021 17:27:05 +0100
b11b5f
Subject: [PATCH] systemctl: simplify halt_main()
b11b5f
b11b5f
The code at this point is not able to tell whether it was called as
b11b5f
halt/poweroff/reboot or shutdown with time "now".
b11b5f
The code also takes a shortcut to skip logind if called as root.
b11b5f
That however means asking shutdown for immediate action won't trigger a
b11b5f
wall message.
b11b5f
As per https://github.com/systemd/systemd/issues/8424#issuecomment-374677315
b11b5f
all commands should trigger a wall message.
b11b5f
That simplifies the code as we can try logind first always.
b11b5f
b11b5f
(cherry picked from commit adefc8789b63225662e50ceaa282f9553b5c64eb)
b11b5f
b11b5f
Resolves: #2053273
b11b5f
---
b11b5f
 src/systemctl/systemctl.c | 44 ++++++++++++++++-----------------------
b11b5f
 1 file changed, 18 insertions(+), 26 deletions(-)
b11b5f
b11b5f
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
b11b5f
index b967550b97..4bedb52f2a 100644
b11b5f
--- a/src/systemctl/systemctl.c
b11b5f
+++ b/src/systemctl/systemctl.c
b11b5f
@@ -8658,34 +8658,23 @@ static int logind_schedule_shutdown(void) {
b11b5f
 static int halt_main(void) {
b11b5f
         int r;
b11b5f
 
b11b5f
-        r = logind_check_inhibitors(arg_action);
b11b5f
-        if (r < 0)
b11b5f
-                return r;
b11b5f
-
b11b5f
+        /* always try logind first */
b11b5f
         if (arg_when > 0)
b11b5f
-                return logind_schedule_shutdown();
b11b5f
-
b11b5f
-        if (geteuid() != 0) {
b11b5f
-                if (arg_dry_run || arg_force > 0) {
b11b5f
-                        (void) must_be_root();
b11b5f
-                        return -EPERM;
b11b5f
-                }
b11b5f
+                r = logind_schedule_shutdown();
b11b5f
+        else {
b11b5f
+                r = logind_check_inhibitors(arg_action);
b11b5f
+                if (r < 0)
b11b5f
+                        return r;
b11b5f
 
b11b5f
-                /* Try logind if we are a normal user and no special
b11b5f
-                 * mode applies. Maybe PolicyKit allows us to shutdown
b11b5f
-                 * the machine. */
b11b5f
-                if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) {
b11b5f
-                        r = logind_reboot(arg_action);
b11b5f
-                        if (r >= 0)
b11b5f
-                                return r;
b11b5f
-                        if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS))
b11b5f
-                                /* requested operation is not
b11b5f
-                                 * supported on the local system or
b11b5f
-                                 * already in progress */
b11b5f
-                                return r;
b11b5f
-                        /* on all other errors, try low-level operation */
b11b5f
-                }
b11b5f
+                r = logind_reboot(arg_action);
b11b5f
         }
b11b5f
+        if (r >= 0)
b11b5f
+                return r;
b11b5f
+        if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS))
b11b5f
+                /* Requested operation is not supported on the local system or already in
b11b5f
+                 * progress */
b11b5f
+                return r;
b11b5f
+        /* on all other errors, try low-level operation */
b11b5f
 
b11b5f
         /* In order to minimize the difference between operation with and
b11b5f
          * without logind, we explicitly enable non-blocking mode for this,
b11b5f
@@ -8695,7 +8684,10 @@ static int halt_main(void) {
b11b5f
         if (!arg_dry_run && !arg_force)
b11b5f
                 return start_with_fallback();
b11b5f
 
b11b5f
-        assert(geteuid() == 0);
b11b5f
+        if (geteuid() != 0) {
b11b5f
+                (void) must_be_root();
b11b5f
+                return -EPERM;
b11b5f
+        }
b11b5f
 
b11b5f
         if (!arg_no_wtmp) {
b11b5f
                 if (sd_booted() > 0)