]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logind: add handle enum to struct
authorLudwig Nussel <ludwig.nussel@suse.de>
Thu, 3 Feb 2022 16:08:28 +0000 (17:08 +0100)
committerLudwig Nussel <ludwig.nussel@suse.de>
Mon, 14 Feb 2022 08:35:12 +0000 (09:35 +0100)
Avoid pointer arithmetic at the expense of repeating the enum value
in the struct.

src/login/logind-action.c
src/login/logind-action.h
src/login/logind-dbus.c
src/login/logind-utmp.c

index 45f77bc090949cd7ee6b7810d7374753a0a0bc08..a4f3f6c3f662fada13a7cb253f1efe4876b12725 100644 (file)
@@ -20,6 +20,7 @@
 
 static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
         [HANDLE_POWEROFF] = {
+                HANDLE_POWEROFF,
                 SPECIAL_POWEROFF_TARGET,
                 INHIBIT_SHUTDOWN,
                 "org.freedesktop.login1.power-off",
@@ -31,6 +32,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 "power-off",
                 },
         [HANDLE_REBOOT] = {
+                HANDLE_REBOOT,
                 SPECIAL_REBOOT_TARGET,
                 INHIBIT_SHUTDOWN,
                 "org.freedesktop.login1.reboot",
@@ -42,6 +44,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 "reboot",
                 },
         [HANDLE_HALT]  = {
+                HANDLE_HALT,
                 SPECIAL_HALT_TARGET,
                 INHIBIT_SHUTDOWN,
                 "org.freedesktop.login1.halt",
@@ -53,6 +56,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 "halt",
                 },
         [HANDLE_KEXEC]  = {
+                HANDLE_KEXEC,
                 SPECIAL_KEXEC_TARGET,
                 INHIBIT_SHUTDOWN,
                 "org.freedesktop.login1.reboot",
@@ -64,6 +68,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 "kexec",
                 },
         [HANDLE_SUSPEND]  = {
+                HANDLE_SUSPEND,
                 SPECIAL_SUSPEND_TARGET,
                 INHIBIT_SLEEP,
                 "org.freedesktop.login1.suspend",
@@ -72,6 +77,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 SLEEP_SUSPEND,
                 },
         [HANDLE_HIBERNATE]  = {
+                HANDLE_HIBERNATE,
                 SPECIAL_HIBERNATE_TARGET,
                 INHIBIT_SLEEP,
                 "org.freedesktop.login1.hibernate",
@@ -80,6 +86,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 SLEEP_HIBERNATE,
                 },
         [HANDLE_HYBRID_SLEEP]  = {
+                HANDLE_HYBRID_SLEEP,
                 SPECIAL_HYBRID_SLEEP_TARGET,
                 INHIBIT_SLEEP,
                 "org.freedesktop.login1.hibernate",
@@ -88,6 +95,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 SLEEP_HYBRID_SLEEP,
                 },
         [HANDLE_SUSPEND_THEN_HIBERNATE]  = {
+                HANDLE_SUSPEND_THEN_HIBERNATE,
                 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
                 INHIBIT_SLEEP,
                 "org.freedesktop.login1.hibernate",
@@ -96,6 +104,7 @@ static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
                 SLEEP_SUSPEND_THEN_HIBERNATE,
                 },
         [HANDLE_FACTORY_RESET]  = {
+                HANDLE_FACTORY_RESET,
                 SPECIAL_FACTORY_RESET_TARGET,
                 _INHIBIT_WHAT_INVALID,
                 NULL,
@@ -122,12 +131,6 @@ const ActionTableItem* manager_item_for_handle(HandleAction handle) {
         return &action_table[handle];
 }
 
-HandleAction manager_handle_for_item(const ActionTableItem* a) {
-        if (a && a < action_table + ELEMENTSOF(action_table))
-                return a - action_table;
-        return _HANDLE_ACTION_INVALID;
-}
-
 int manager_handle_action(
                 Manager *m,
                 InhibitWhat inhibit_key,
index e6d304774331a59bdd7ce14d056d61b55d9e2c94..710aa4931d72980e74923efa3358d1daebe512ce 100644 (file)
@@ -28,6 +28,7 @@ typedef struct ActionTableItem ActionTableItem;
 #include "sleep-config.h"
 
 struct ActionTableItem {
+        HandleAction handle;
         const char *target;
         InhibitWhat inhibit_what;
         const char *polkit_action;
index e801f2453207ada416cd896fd52a01b35ef46ea8..2cdd5b9084d2bb2d20dfd5b9ed68c1a4af9b012a 100644 (file)
@@ -349,7 +349,7 @@ static int property_get_scheduled_shutdown(
                 return r;
 
         r = sd_bus_message_append(reply, "st",
-                handle_action_to_string(manager_handle_for_item(m->scheduled_shutdown_type)),
+                m->scheduled_shutdown_type ? handle_action_to_string(m->scheduled_shutdown_type->handle) : NULL,
                 m->scheduled_shutdown_timeout);
         if (r < 0)
                 return r;
@@ -1884,7 +1884,7 @@ static int method_do_shutdown_or_sleep(
                         return r;
                 if ((flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC) != 0)
                         return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
-                if (manager_handle_for_item(a) != HANDLE_REBOOT && (flags & SD_LOGIND_REBOOT_VIA_KEXEC))
+                if (a->handle != HANDLE_REBOOT && (flags & SD_LOGIND_REBOOT_VIA_KEXEC))
                         return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Reboot via kexec is only applicable with reboot operations");
         } else {
                 /* Old style method: no flags parameter, but interactive bool passed as boolean in
@@ -2035,6 +2035,7 @@ static int update_schedule_file(Manager *m) {
         int r;
 
         assert(m);
+        assert(m->scheduled_shutdown_type);
 
         r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
         if (r < 0)
@@ -2052,7 +2053,7 @@ static int update_schedule_file(Manager *m) {
                 "MODE=%s\n",
                 m->scheduled_shutdown_timeout,
                 m->enable_wall_messages,
-                handle_action_to_string(manager_handle_for_item(m->scheduled_shutdown_type)));
+                handle_action_to_string(m->scheduled_shutdown_type->handle));
 
         if (!isempty(m->wall_message)) {
                 _cleanup_free_ char *t = NULL;
@@ -2240,7 +2241,8 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd
         assert(m);
         assert(message);
 
-        cancelled = !IN_SET(manager_handle_for_item(m->scheduled_shutdown_type), HANDLE_IGNORE, _HANDLE_ACTION_INVALID);
+        cancelled = m->scheduled_shutdown_type
+                && !IN_SET(m->scheduled_shutdown_type->handle, HANDLE_IGNORE, _HANDLE_ACTION_INVALID);
         if (!cancelled)
                 goto done;
 
index 03817f9c1a6c74fc4a77b029968054094e300187..5a9e8db79d339a67381befc3b2b9a743ac5ea02d 100644 (file)
@@ -64,7 +64,7 @@ static int warn_wall(Manager *m, usec_t n) {
 
         assert(m);
 
-        if (!m->enable_wall_messages)
+        if (!m->enable_wall_messages || !m->scheduled_shutdown_type)
                 return 0;
 
         left = m->scheduled_shutdown_timeout > n;
@@ -72,7 +72,7 @@ static int warn_wall(Manager *m, usec_t n) {
         r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
                      strempty(m->wall_message),
                      isempty(m->wall_message) ? "" : "\n",
-                     handle_action_to_string(manager_handle_for_item(m->scheduled_shutdown_type)),
+                     handle_action_to_string(m->scheduled_shutdown_type->handle),
                      left ? "at " : "NOW",
                      left ? FORMAT_TIMESTAMP(m->scheduled_shutdown_timeout) : "");
         if (r < 0) {