chiark / gitweb /
bus: calculate iovec for messages only when we need it
[elogind.git] / src / libsystemd-bus / sd-bus.c
index fde39e0cac9dc12490d56d75987622200517a19b..89172e6369c3dd5ea8d69fbdfc366c4028faf2c0 100644 (file)
@@ -436,8 +436,11 @@ static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
 
 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
         _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
-        struct addrinfo hints, *result;
         int r;
+        struct addrinfo *result, hints = {
+                .ai_socktype = SOCK_STREAM,
+                .ai_flags = AI_ADDRCONFIG,
+        };
 
         assert(b);
         assert(p);
@@ -475,10 +478,6 @@ static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
         if (!host || !port)
                 return -EINVAL;
 
-        zero(hints);
-        hints.ai_socktype = SOCK_STREAM;
-        hints.ai_flags = AI_ADDRCONFIG;
-
         if (family) {
                 if (streq(family, "ipv4"))
                         hints.ai_family = AF_INET;
@@ -966,7 +965,7 @@ static int dispatch_wqueue(sd_bus *bus) {
                 } else if (r == 0)
                         /* Didn't do anything this time */
                         return ret;
-                else if (bus->windex >= bus->wqueue[0]->size) {
+                else if (bus->windex >= bus_message_size(bus->wqueue[0])) {
                         /* Fully written. Let's drop the entry from
                          * the queue.
                          *
@@ -1067,7 +1066,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
                 if (r < 0) {
                         sd_bus_close(bus);
                         return r;
-                } else if (idx < m->size)  {
+                } else if (idx < bus_message_size(m))  {
                         /* Wasn't fully written. So let's remember how
                          * much was written. Note that the first entry
                          * of the wqueue array is always allocated so
@@ -1314,7 +1313,12 @@ int sd_bus_send_with_reply_and_block(
                                 /* Found a match! */
 
                                 if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
-                                        *reply = incoming;
+
+                                        if (reply)
+                                                *reply = incoming;
+                                        else
+                                                sd_bus_message_unref(incoming);
+
                                         return 0;
                                 }
 
@@ -1932,7 +1936,7 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
 }
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
-        struct pollfd p[2];
+        struct pollfd p[2] = {};
         int r, e, n;
         struct timespec ts;
         usec_t until, m;
@@ -1963,9 +1967,7 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
         if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
                 m = timeout_usec;
 
-        zero(p);
         p[0].fd = bus->input_fd;
-
         if (bus->output_fd == bus->input_fd) {
                 p[0].events = e;
                 n = 1;
@@ -2265,3 +2267,66 @@ int sd_bus_call_method(
 
         return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
 }
+
+int sd_bus_reply_method_return(
+                sd_bus *bus,
+                sd_bus_message *call,
+                const char *types, ...) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        va_list ap;
+        int r;
+
+        if (!bus)
+                return -EINVAL;
+        if (!call)
+                return -EINVAL;
+        if (!call->sealed)
+                return -EPERM;
+        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+                return -EINVAL;
+
+        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
+
+        r = sd_bus_message_new_method_return(bus, call, &m);
+        if (r < 0)
+                return r;
+
+        va_start(ap, types);
+        r = bus_message_append_ap(m, types, ap);
+        va_end(ap);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(bus, m, NULL);
+}
+
+int sd_bus_reply_method_error(
+                sd_bus *bus,
+                sd_bus_message *call,
+                const sd_bus_error *e) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        int r;
+
+        if (!bus)
+                return -EINVAL;
+        if (!call)
+                return -EINVAL;
+        if (!call->sealed)
+                return -EPERM;
+        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+                return -EINVAL;
+        if (!sd_bus_error_is_set(e))
+                return -EINVAL;
+
+        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
+
+        r = sd_bus_message_new_method_error(bus, call, e, &m);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(bus, m, NULL);
+}