sd-bus: kdbus: decouple creating message from enqueuing
authorAdrian Szyndela <adrian.s@samsung.com>
Mon, 23 Mar 2020 15:18:03 +0000 (16:18 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 25 Jan 2024 12:22:10 +0000 (13:22 +0100)
This separates creating replies from enqueuing them. This way we can use
enqueueing function for other replies.

No functional changes.

src/libsystemd/sd-bus/bus-control-kernel.c

index 21f8c56..6242641 100644 (file)
@@ -130,16 +130,12 @@ static int prepare_for_kernel_request(sd_bus *bus)
     return 0;
 }
 
-static int enqueue_kernel_reply(
-                sd_bus *bus,
-                sd_bus_slot **ret_slot,
-                int reply,
-                sd_bus_message_handler_t callback,
-                void *userdata,
-                const char *error_failed_message)
+static int prepare_int_reply(sd_bus *bus,
+                             sd_bus_message **ret_m,
+                             int reply,
+                             const char *error_failed_message)
 {
     int ret;
-    _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
     _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
     uint64_t cookie;
 
@@ -162,6 +158,21 @@ static int enqueue_kernel_reply(
     if (ret < 0)
         return ret;
 
+    *ret_m = m;
+    m = NULL;
+    return 0;
+}
+
+static int enqueue_kernel_reply(
+                sd_bus *bus,
+                sd_bus_slot **ret_slot,
+                sd_bus_message *m,
+                sd_bus_message_handler_t callback,
+                void *userdata)
+{
+    int ret;
+    _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
+
     if (ret_slot || callback) {
         s = bus_slot_allocate(bus, !ret_slot, BUS_REPLY_CALLBACK, sizeof(struct reply_callback), userdata);
         if (!s)
@@ -169,7 +180,7 @@ static int enqueue_kernel_reply(
 
         s->reply_callback.callback = callback;
 
-        s->reply_callback.cookie = cookie;
+        s->reply_callback.cookie = bus->cookie;
         ret = ordered_hashmap_put(bus->reply_callbacks, &s->reply_callback.cookie, &s->reply_callback);
         if (ret < 0) {
             s->reply_callback.cookie = 0;
@@ -187,7 +198,6 @@ static int enqueue_kernel_reply(
     }
 
     bus->rqueue[bus->rqueue_size++] = m;
-    m = NULL;
 
     if (ret_slot)
         *ret_slot = s;
@@ -196,6 +206,28 @@ static int enqueue_kernel_reply(
     return 0;
 }
 
+static int enqueue_int_kernel_reply(
+                sd_bus *bus,
+                sd_bus_slot **ret_slot,
+                int reply,
+                const char *error_string,
+                sd_bus_message_handler_t callback,
+                void *userdata) {
+        int ret;
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+
+        ret = prepare_int_reply(bus, &m, reply, error_string);
+        if (ret < 0)
+            return ret;
+
+        ret = enqueue_kernel_reply(bus, ret_slot, m, callback, userdata);
+        if (ret < 0)
+            return ret;
+
+        m = NULL;
+        return 1;
+}
+
 int bus_request_name_kernel_async(sd_bus *bus,
         sd_bus_slot **ret_slot,
         const char *name,
@@ -208,9 +240,12 @@ int bus_request_name_kernel_async(sd_bus *bus,
     if (ret < 0)
         return ret;
 
-    ret = bus_request_name_kernel(bus, name, flags);
-
-    return enqueue_kernel_reply(bus, ret_slot, ret, callback, userdata, "Request name failed");
+    return enqueue_int_kernel_reply(bus,
+                                    ret_slot,
+                                    bus_request_name_kernel(bus, name, flags),
+                                    "Request name failed",
+                                    callback,
+                                    userdata);
 }
 
 int bus_release_name_kernel(sd_bus *bus, const char *name) {
@@ -252,12 +287,14 @@ int bus_release_name_kernel_async(
     if (ret < 0)
         return ret;
 
-    ret = bus_release_name_kernel(bus, name);
-
-    return enqueue_kernel_reply(bus, ret_slot, ret, callback, userdata, "Release name failed");
+    return enqueue_int_kernel_reply(bus,
+                                    ret_slot,
+                                    bus_release_name_kernel(bus, name),
+                                    "Release name failed",
+                                    callback,
+                                    userdata);
 }
 
-
 static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
         struct kdbus_cmd_list cmd = {
                 .size = sizeof(cmd),