698723
From 6751217a032dd1a8e8ee324332f29786265f0ebe Mon Sep 17 00:00:00 2001
698723
From: Lennart Poettering <lennart@poettering.net>
698723
Date: Tue, 2 Feb 2021 15:27:30 +0100
698723
Subject: [PATCH] logind: simplify flags handling a bit
698723
698723
Let's split out the two codepaths a bit, and emphasize which ones it the
698723
new-style and which the old-style codepath, and let's clearly convert
698723
the params of the old-stye into the new style for further processing, so
698723
that the old style path is brief and isolated.
698723
698723
No change in behaviour.
698723
698723
Follow-up for: 8885fed4e3a52cf1bf105e42043203c485ed9d92
698723
698723
(cherry picked from commit d3e99bc0c7f785dcf4e73cfed12f74002e73be5f)
698723
698723
Related: #1269726
698723
---
698723
 src/login/logind-dbus.c | 30 ++++++++++++++++++------------
698723
 1 file changed, 18 insertions(+), 12 deletions(-)
698723
698723
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
698723
index 0c43fbb3e0..ae9abc9bce 100644
698723
--- a/src/login/logind-dbus.c
698723
+++ b/src/login/logind-dbus.c
698723
@@ -1776,8 +1776,8 @@ static int method_do_shutdown_or_sleep(
698723
                 bool with_flags,
698723
                 sd_bus_error *error) {
698723
 
698723
-        int interactive = false, r;
698723
-        uint64_t flags = 0;
698723
+        uint64_t flags;
698723
+        int r;
698723
 
698723
         assert(m);
698723
         assert(message);
698723
@@ -1785,19 +1785,25 @@ static int method_do_shutdown_or_sleep(
698723
         assert(w >= 0);
698723
         assert(w <= _INHIBIT_WHAT_MAX);
698723
 
698723
-        if (with_flags)
698723
+        if (with_flags) {
698723
+                /* New style method: with flags parameter (and interactive bool in the bus message header) */
698723
                 r = sd_bus_message_read(message, "t", &flags);
698723
-        else
698723
-                r = sd_bus_message_read(message, "b", &interactive);
698723
-
698723
-        if (r < 0)
698723
-                return r;
698723
+                if (r < 0)
698723
+                        return r;
698723
+                if ((flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC) != 0)
698723
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
698723
+        } else {
698723
+                /* Old style method: no flags parameter, but interactive bool passed as boolean in
698723
+                 * payload. Let's convert this argument to the new-style flags parameter for our internal
698723
+                 * use. */
698723
+                int interactive;
698723
 
698723
-        if (with_flags && (flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC))
698723
-                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
698723
-                                         "Invalid flags parameter");
698723
+                r = sd_bus_message_read(message, "b", &interactive);
698723
+                if (r < 0)
698723
+                        return r;
698723
 
698723
-        SET_FLAG(flags, SD_LOGIND_INTERACTIVE, interactive);
698723
+                flags = interactive ? SD_LOGIND_INTERACTIVE : 0;
698723
+        }
698723
 
698723
         /* Don't allow multiple jobs being executed at the same time */
698723
         if (m->action_what)