787d6d
From d36295d7c1b110d150b7af6e3354c28af4c4884d Mon Sep 17 00:00:00 2001
787d6d
From: David Tardon <dtardon@redhat.com>
787d6d
Date: Mon, 30 Jan 2023 14:27:24 +0100
787d6d
Subject: [PATCH] systemctl: reintroduce the original halt_main()
787d6d
787d6d
RHEL-only
787d6d
787d6d
Related: #2053273
787d6d
---
787d6d
 src/systemctl/systemctl.c | 59 +++++++++++++++++++++++++++++++++++++++
787d6d
 1 file changed, 59 insertions(+)
787d6d
787d6d
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
787d6d
index 199f736f7f..a26e4a913a 100644
787d6d
--- a/src/systemctl/systemctl.c
787d6d
+++ b/src/systemctl/systemctl.c
787d6d
@@ -8655,6 +8655,65 @@ static int logind_schedule_shutdown(void) {
787d6d
 #endif
787d6d
 }
787d6d
 
787d6d
+static int halt_main_old(void) {
787d6d
+        int r;
787d6d
+
787d6d
+        r = logind_check_inhibitors(arg_action);
787d6d
+        if (r < 0)
787d6d
+                return r;
787d6d
+
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
+
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
+        }
787d6d
+
787d6d
+        /* In order to minimize the difference between operation with and
787d6d
+         * without logind, we explicitly enable non-blocking mode for this,
787d6d
+         * as logind's shutdown operations are always non-blocking. */
787d6d
+        arg_no_block = true;
787d6d
+
787d6d
+        if (!arg_dry_run && !arg_force)
787d6d
+                return start_with_fallback();
787d6d
+
787d6d
+        assert(geteuid() == 0);
787d6d
+
787d6d
+        if (!arg_no_wtmp) {
787d6d
+                if (sd_booted() > 0)
787d6d
+                        log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
787d6d
+                else {
787d6d
+                        r = utmp_put_shutdown();
787d6d
+                        if (r < 0)
787d6d
+                                log_warning_errno(r, "Failed to write utmp record: %m");
787d6d
+                }
787d6d
+        }
787d6d
+
787d6d
+        if (arg_dry_run)
787d6d
+                return 0;
787d6d
+
787d6d
+        r = halt_now(arg_action);
787d6d
+        return log_error_errno(r, "Failed to reboot: %m");
787d6d
+}
787d6d
+
787d6d
 static int halt_main(void) {
787d6d
         int r;
787d6d