teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0445-systemctl-replace-switch-statement-by-table-of-struc.patch

21255d
From 8b34041ee97069bee8bf03ae5ba651b34b1b8460 Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Tue, 26 Mar 2019 15:20:26 +0100
21255d
Subject: [PATCH] systemctl: replace switch statement by table of structures
21255d
21255d
(cherry picked from commit c45e5fb877033c9e3f9b79121644ed71032af379)
21255d
21255d
Related: #846319
21255d
---
21255d
 src/systemctl/systemctl.c | 68 ++++++++++++---------------------------
21255d
 1 file changed, 21 insertions(+), 47 deletions(-)
21255d
21255d
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
21255d
index e963f19b0a..04e24691d8 100644
21255d
--- a/src/systemctl/systemctl.c
21255d
+++ b/src/systemctl/systemctl.c
21255d
@@ -3179,64 +3179,38 @@ static int logind_set_wall_message(void) {
21255d
 }
21255d
 #endif
21255d
 
21255d
-/* Ask systemd-logind, which might grant access to unprivileged users
21255d
- * through PolicyKit */
21255d
+/* Ask systemd-logind, which might grant access to unprivileged users through polkit */
21255d
 static int logind_reboot(enum action a) {
21255d
 #if ENABLE_LOGIND
21255d
+        static const struct {
21255d
+                const char *method;
21255d
+                const char *description;
21255d
+        } actions[_ACTION_MAX] = {
21255d
+                [ACTION_POWEROFF]               = { "PowerOff",             "power off system"                },
21255d
+                [ACTION_REBOOT]                 = { "Reboot",               "reboot system"                   },
21255d
+                [ACTION_HALT]                   = { "Halt",                 "halt system"                     },
21255d
+                [ACTION_SUSPEND]                = { "Suspend",              "suspend system"                  },
21255d
+                [ACTION_HIBERNATE]              = { "Hibernate",            "hibernate system"                },
21255d
+                [ACTION_HYBRID_SLEEP]           = { "HybridSleep",          "put system into hybrid sleep"    },
21255d
+                [ACTION_SUSPEND_THEN_HIBERNATE] = { "SuspendThenHibernate", "suspend system, hibernate later" },
21255d
+        };
21255d
+
21255d
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
21255d
-        const char *method, *description;
21255d
         sd_bus *bus;
21255d
         int r;
21255d
 
21255d
+        if (a < 0 || a >= _ACTION_MAX || !actions[a].method)
21255d
+                return -EINVAL;
21255d
+
21255d
         r = acquire_bus(BUS_FULL, &bus;;
21255d
         if (r < 0)
21255d
                 return r;
21255d
 
21255d
-        switch (a) {
21255d
-
21255d
-        case ACTION_POWEROFF:
21255d
-                method = "PowerOff";
21255d
-                description = "power off system";
21255d
-                break;
21255d
-
21255d
-        case ACTION_REBOOT:
21255d
-                method = "Reboot";
21255d
-                description = "reboot system";
21255d
-                break;
21255d
-
21255d
-        case ACTION_HALT:
21255d
-                method = "Halt";
21255d
-                description = "halt system";
21255d
-                break;
21255d
-
21255d
-        case ACTION_SUSPEND:
21255d
-                method = "Suspend";
21255d
-                description = "suspend system";
21255d
-                break;
21255d
-
21255d
-        case ACTION_HIBERNATE:
21255d
-                method = "Hibernate";
21255d
-                description = "hibernate system";
21255d
-                break;
21255d
-
21255d
-        case ACTION_HYBRID_SLEEP:
21255d
-                method = "HybridSleep";
21255d
-                description = "put system into hybrid sleep";
21255d
-                break;
21255d
-
21255d
-        case ACTION_SUSPEND_THEN_HIBERNATE:
21255d
-                method = "SuspendThenHibernate";
21255d
-                description = "put system into suspend followed by hibernate";
21255d
-                break;
21255d
-
21255d
-        default:
21255d
-                return -EINVAL;
21255d
-        }
21255d
-
21255d
         polkit_agent_open_maybe();
21255d
         (void) logind_set_wall_message();
21255d
 
21255d
-        log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", method);
21255d
+        log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", actions[a].method);
21255d
+
21255d
         if (arg_dry_run)
21255d
                 return 0;
21255d
 
21255d
@@ -3245,12 +3219,12 @@ static int logind_reboot(enum action a) {
21255d
                         "org.freedesktop.login1",
21255d
                         "/org/freedesktop/login1",
21255d
                         "org.freedesktop.login1.Manager",
21255d
-                        method,
21255d
+                        actions[a].method,
21255d
                         &error,
21255d
                         NULL,
21255d
                         "b", arg_ask_password);
21255d
         if (r < 0)
21255d
-                return log_error_errno(r, "Failed to %s via logind: %s", description, bus_error_message(&error, r));
21255d
+                return log_error_errno(r, "Failed to %s via logind: %s", actions[a].description, bus_error_message(&error, r));
21255d
 
21255d
         return 0;
21255d
 #else