chiark / gitweb /
bus: suppress reply messages to method calls with no_reply set
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Mar 2013 19:16:27 +0000 (20:16 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 20 Mar 2013 22:00:09 +0000 (23:00 +0100)
src/libsystemd-bus/bus-message.c
src/libsystemd-bus/bus-message.h
src/libsystemd-bus/sd-bus.c

index 4c5588cf8a6653f8d0f2167daad5ef747d07e3f2..ccea12055da4085a9fc5bb554913037076e713db 100644 (file)
@@ -245,6 +245,8 @@ int sd_bus_message_new_signal(
                 return -EINVAL;
         if (!member)
                 return -EINVAL;
+        if (!m)
+                return -EINVAL;
 
         t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL);
         if (!t)
@@ -283,6 +285,8 @@ int sd_bus_message_new_method_call(
                 return -EINVAL;
         if (!member)
                 return -EINVAL;
+        if (!m)
+                return -EINVAL;
 
         t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL);
         if (!t)
@@ -315,9 +319,10 @@ fail:
         return r;
 }
 
-int sd_bus_message_new_method_return(
+static int message_new_reply(
                 sd_bus *bus,
                 sd_bus_message *call,
+                uint8_t type,
                 sd_bus_message **m) {
 
         sd_bus_message *t;
@@ -327,12 +332,15 @@ int sd_bus_message_new_method_return(
                 return -EINVAL;
         if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                 return -EINVAL;
+        if (!m)
+                return -EINVAL;
 
-        t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_RETURN);
+        t = message_new(bus, type);
         if (!t)
                 return -ENOMEM;
 
         t->reply_serial = BUS_MESSAGE_SERIAL(call);
+
         r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
         if (r < 0)
                 goto fail;
@@ -343,14 +351,23 @@ int sd_bus_message_new_method_return(
                         goto fail;
         }
 
+        t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
+
         *m = t;
-        return 0;
 
 fail:
         message_free(t);
         return r;
 }
 
+int sd_bus_message_new_method_return(
+                sd_bus *bus,
+                sd_bus_message *call,
+                sd_bus_message **m) {
+
+        return message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_RETURN, m);
+}
+
 int sd_bus_message_new_method_error(
                 sd_bus *bus,
                 sd_bus_message *call,
@@ -360,29 +377,16 @@ int sd_bus_message_new_method_error(
         sd_bus_message *t;
         int r;
 
-        if (!call)
-                return -EINVAL;
-        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return -EINVAL;
         if (!e)
                 return -EINVAL;
         if (!e->name)
                 return -EINVAL;
+        if (!m)
+                return -EINVAL;
 
-        t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_ERROR);
-        if (!t)
-                return -ENOMEM;
-
-        t->reply_serial = BUS_MESSAGE_SERIAL(call);
-        r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
+        r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
         if (r < 0)
-                goto fail;
-
-        if (call->sender) {
-                r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
-                if (r < 0)
-                        goto fail;
-        }
+                return r;
 
         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
         if (r < 0)
index c62659e4248239ad85e2eb6a04881052df1ba352..bfd2b1228008ac008c54e645b291a56b1456ba47 100644 (file)
@@ -71,6 +71,7 @@ struct sd_bus_message {
         bool free_header:1;
         bool free_fields:1;
         bool free_body:1;
+        bool dont_send:1;
 
         struct bus_header *header;
         void *fields;
index d80f09793cdd5a92c635a9f2e5589fff01b8f3a1..f7d76444632ad9256a4f54d7620d96d5feb450d6 100644 (file)
@@ -996,6 +996,11 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         if (r < 0)
                 return r;
 
+        /* If this is a reply and no reply was requested, then let's
+         * suppress this, if we can */
+        if (m->dont_send && !serial)
+                return 0;
+
         if (bus->wqueue_size <= 0) {
                 size_t idx = 0;