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