richardphibel / rpms / systemd

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