dcavalca / rpms / systemd

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