daandemeyer / rpms / systemd

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