ryantimwilson / rpms / systemd

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