chiark / gitweb /
bus: return ECHILD as soon as people try to reuse a bus connection across a fork()
[elogind.git] / src / libsystemd-bus / sd-bus.c
index fde39e0cac9dc12490d56d75987622200517a19b..fd19ff32b01b79d34cc900fa237833dddff149a2 100644 (file)
 #include <netdb.h>
 #include <sys/poll.h>
 #include <byteswap.h>
+#include <sys/mman.h>
 
 #include "util.h"
 #include "macro.h"
 #include "strv.h"
 #include "set.h"
+#include "missing.h"
 
 #include "sd-bus.h"
 #include "bus-internal.h"
 #include "bus-message.h"
 #include "bus-type.h"
 #include "bus-socket.h"
+#include "bus-kernel.h"
 #include "bus-control.h"
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
 
+static void bus_close_fds(sd_bus *b) {
+        assert(b);
+
+        if (b->input_fd >= 0)
+                close_nointr_nofail(b->input_fd);
+
+        if (b->output_fd >= 0 && b->output_fd != b->input_fd)
+                close_nointr_nofail(b->output_fd);
+
+        b->input_fd = b->output_fd = -1;
+}
+
 static void bus_free(sd_bus *b) {
         struct filter_callback *f;
         struct object_callback *c;
@@ -48,12 +63,16 @@ static void bus_free(sd_bus *b) {
 
         assert(b);
 
-        sd_bus_close(b);
+        bus_close_fds(b);
+
+        if (b->kdbus_buffer)
+                munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
 
         free(b->rbuffer);
         free(b->unique_name);
         free(b->auth_buffer);
         free(b->address);
+        free(b->kernel);
 
         free(b->exec_path);
         strv_free(b->exec_argv);
@@ -83,9 +102,10 @@ static void bus_free(sd_bus *b) {
         }
 
         hashmap_free(b->object_callbacks);
-
         bus_match_free(&b->match_callbacks);
 
+        bus_kernel_flush_memfd(b);
+
         free(b);
 }
 
@@ -99,10 +119,11 @@ int sd_bus_new(sd_bus **ret) {
         if (!r)
                 return -ENOMEM;
 
-        r->n_ref = 1;
+        r->n_ref = REFCNT_INIT;
         r->input_fd = r->output_fd = -1;
         r->message_version = 1;
         r->negotiate_fds = true;
+        r->original_pid = getpid();
 
         /* We guarantee that wqueue always has space for at least one
          * entry */
@@ -125,6 +146,8 @@ int sd_bus_set_address(sd_bus *bus, const char *address) {
                 return -EPERM;
         if (!address)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         a = strdup(address);
         if (!a)
@@ -145,6 +168,8 @@ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
                 return -EINVAL;
         if (output_fd < 0)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->input_fd = input_fd;
         bus->output_fd = output_fd;
@@ -162,6 +187,8 @@ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
                 return -EINVAL;
         if (strv_isempty(argv))
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         p = strdup(path);
         if (!p)
@@ -187,6 +214,8 @@ int sd_bus_set_bus_client(sd_bus *bus, int b) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->bus_client = !!b;
         return 0;
@@ -197,6 +226,8 @@ int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->negotiate_fds = !!b;
         return 0;
@@ -209,6 +240,8 @@ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->is_server = !!b;
         bus->server_id = server_id;
@@ -220,23 +253,25 @@ int sd_bus_set_anonymous(sd_bus *bus, int b) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->anonymous_auth = !!b;
         return 0;
 }
 
-static int hello_callback(sd_bus *bus, int error, sd_bus_message *reply, void *userdata) {
+static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
         const char *s;
         int r;
 
         assert(bus);
         assert(bus->state == BUS_HELLO);
-
-        if (error != 0)
-                return -error;
-
         assert(reply);
 
+        r = bus_message_to_errno(reply);
+        if (r < 0)
+                return r;
+
         r = sd_bus_message_read(reply, "s", &s);
         if (r < 0)
                 return r;
@@ -259,7 +294,7 @@ static int bus_send_hello(sd_bus *bus) {
 
         assert(bus);
 
-        if (!bus->bus_client)
+        if (!bus->bus_client || bus->is_kernel)
                 return 0;
 
         r = sd_bus_message_new_method_call(
@@ -278,7 +313,7 @@ static int bus_send_hello(sd_bus *bus) {
 int bus_start_running(sd_bus *bus) {
         assert(bus);
 
-        if (bus->bus_client) {
+        if (bus->bus_client && !bus->is_kernel) {
                 bus->state = BUS_HELLO;
                 return 1;
         }
@@ -436,8 +471,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 +513,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;
@@ -597,6 +631,41 @@ fail:
         return r;
 }
 
+static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
+        _cleanup_free_ char *path = NULL;
+        int r;
+
+        assert(b);
+        assert(p);
+        assert(*p);
+        assert(guid);
+
+        while (**p != 0 && **p != ';') {
+                r = parse_address_key(p, "guid", guid);
+                if (r < 0)
+                        return r;
+                else if (r > 0)
+                        continue;
+
+                r = parse_address_key(p, "path", &path);
+                if (r < 0)
+                        return r;
+                else if (r > 0)
+                        continue;
+
+                skip_address_key(p);
+        }
+
+        if (!path)
+                return -EINVAL;
+
+        free(b->kernel);
+        b->kernel = path;
+        path = NULL;
+
+        return 0;
+}
+
 static void bus_reset_parsed_address(sd_bus *b) {
         assert(b);
 
@@ -607,6 +676,8 @@ static void bus_reset_parsed_address(sd_bus *b) {
         b->exec_path = NULL;
         b->exec_argv = NULL;
         b->server_id = SD_ID128_NULL;
+        free(b->kernel);
+        b->kernel = NULL;
 }
 
 static int bus_parse_next_address(sd_bus *b) {
@@ -658,6 +729,14 @@ static int bus_parse_next_address(sd_bus *b) {
 
                         break;
 
+                } else if (startswith(a, "kernel:")) {
+
+                        a += 7;
+                        r = parse_kernel_address(b, &a, &guid);
+                        if (r < 0)
+                                return r;
+
+                        break;
                 }
 
                 a = strchr(a, ';');
@@ -697,6 +776,13 @@ static int bus_start_address(sd_bus *b) {
                         if (r >= 0)
                                 return r;
 
+                        b->last_connect_error = -r;
+                } else if (b->kernel) {
+
+                        r = bus_kernel_connect(b);
+                        if (r >= 0)
+                                return r;
+
                         b->last_connect_error = -r;
                 }
 
@@ -716,6 +802,7 @@ int bus_next_address(sd_bus *b) {
 }
 
 static int bus_start_fd(sd_bus *b) {
+        struct stat st;
         int r;
 
         assert(b);
@@ -740,7 +827,13 @@ static int bus_start_fd(sd_bus *b) {
                         return r;
         }
 
-        return bus_socket_take_fd(b);
+        if (fstat(b->input_fd, &st) < 0)
+                return -errno;
+
+        if (S_ISCHR(b->input_fd))
+                return bus_kernel_take_fd(b);
+        else
+                return bus_socket_take_fd(b);
 }
 
 int sd_bus_start(sd_bus *bus) {
@@ -750,6 +843,8 @@ int sd_bus_start(sd_bus *bus) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->state = BUS_OPENING;
 
@@ -758,7 +853,7 @@ int sd_bus_start(sd_bus *bus) {
 
         if (bus->input_fd >= 0)
                 r = bus_start_fd(bus);
-        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path)
+        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel)
                 r = bus_start_address(bus);
         else
                 return -EINVAL;
@@ -781,7 +876,7 @@ int sd_bus_open_system(sd_bus **ret) {
         if (r < 0)
                 return r;
 
-        e = getenv("DBUS_SYSTEM_BUS_ADDRESS");
+        e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
         if (e) {
                 r = sd_bus_set_address(b, e);
                 if (r < 0)
@@ -819,13 +914,13 @@ int sd_bus_open_user(sd_bus **ret) {
         if (r < 0)
                 return r;
 
-        e = getenv("DBUS_SESSION_BUS_ADDRESS");
+        e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
         if (e) {
                 r = sd_bus_set_address(b, e);
                 if (r < 0)
                         goto fail;
         } else {
-                e = getenv("XDG_RUNTIME_DIR");
+                e = secure_getenv("XDG_RUNTIME_DIR");
                 if (!e) {
                         r = -ENOENT;
                         goto fail;
@@ -859,22 +954,29 @@ fail:
 void sd_bus_close(sd_bus *bus) {
         if (!bus)
                 return;
+        if (bus->state == BUS_CLOSED)
+                return;
+        if (bus_pid_changed(bus))
+                return;
 
-        if (bus->input_fd >= 0)
-                close_nointr_nofail(bus->input_fd);
-        if (bus->output_fd >= 0 && bus->output_fd != bus->input_fd)
-                close_nointr_nofail(bus->output_fd);
+        bus->state = BUS_CLOSED;
+
+        if (!bus->is_kernel)
+                bus_close_fds(bus);
 
-        bus->input_fd = bus->output_fd = -1;
+        /* We'll leave the fd open in case this is a kernel bus, since
+         * there might still be memblocks around that reference this
+         * bus, and they might need to invoke the
+         * KDBUS_CMD_MSG_RELEASE ioctl on the fd when they are
+         * freed. */
 }
 
 sd_bus *sd_bus_ref(sd_bus *bus) {
         if (!bus)
                 return NULL;
 
-        assert(bus->n_ref > 0);
+        assert_se(REFCNT_INC(bus->n_ref) >= 2);
 
-        bus->n_ref++;
         return bus;
 }
 
@@ -882,10 +984,7 @@ sd_bus *sd_bus_unref(sd_bus *bus) {
         if (!bus)
                 return NULL;
 
-        assert(bus->n_ref > 0);
-        bus->n_ref--;
-
-        if (bus->n_ref <= 0)
+        if (REFCNT_DEC(bus->n_ref) <= 0)
                 bus_free(bus);
 
         return NULL;
@@ -894,8 +993,10 @@ sd_bus *sd_bus_unref(sd_bus *bus) {
 int sd_bus_is_open(sd_bus *bus) {
         if (!bus)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
-        return bus->state != BUS_UNSET && bus->input_fd >= 0;
+        return BUS_IS_OPEN(bus->state);
 }
 
 int sd_bus_can_send(sd_bus *bus, char type) {
@@ -903,8 +1004,10 @@ int sd_bus_can_send(sd_bus *bus, char type) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->output_fd < 0)
+        if (bus->state == BUS_UNSET)
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (type == SD_BUS_TYPE_UNIX_FD) {
                 if (!bus->negotiate_fds)
@@ -927,6 +1030,8 @@ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
                 return -EINVAL;
         if (!server_id)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -954,19 +1059,20 @@ static int dispatch_wqueue(sd_bus *bus) {
         assert(bus);
         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
-        if (bus->output_fd < 0)
-                return -ENOTCONN;
-
         while (bus->wqueue_size > 0) {
 
-                r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
+                if (bus->is_kernel)
+                        r = bus_kernel_write_message(bus, bus->wqueue[0]);
+                else
+                        r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
+
                 if (r < 0) {
                         sd_bus_close(bus);
                         return r;
                 } else if (r == 0)
                         /* Didn't do anything this time */
                         return ret;
-                else if (bus->windex >= bus->wqueue[0]->size) {
+                else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
                         /* Fully written. Let's drop the entry from
                          * the queue.
                          *
@@ -997,9 +1103,6 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
         assert(m);
         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
-        if (bus->input_fd < 0)
-                return -ENOTCONN;
-
         if (bus->rqueue_size > 0) {
                 /* Dispatch a queued message */
 
@@ -1011,7 +1114,11 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
 
         /* Try to read a new message */
         do {
-                r = bus_socket_read_message(bus, &z);
+                if (bus->is_kernel)
+                        r = bus_kernel_read_message(bus, &z);
+                else
+                        r = bus_socket_read_message(bus, &z);
+
                 if (r < 0) {
                         sd_bus_close(bus);
                         return r;
@@ -1031,12 +1138,12 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->output_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (m->n_fds > 0) {
                 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
@@ -1063,11 +1170,15 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
                 size_t idx = 0;
 
-                r = bus_socket_write_message(bus, m, &idx);
+                if (bus->is_kernel)
+                        r = bus_kernel_write_message(bus, m);
+                else
+                        r = bus_socket_write_message(bus, m, &idx);
+
                 if (r < 0) {
                         sd_bus_close(bus);
                         return r;
-                } else if (idx < m->size)  {
+                } else if (!bus->is_kernel && 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
@@ -1140,9 +1251,7 @@ int sd_bus_send_with_reply(
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->output_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
@@ -1152,6 +1261,8 @@ int sd_bus_send_with_reply(
                 return -EINVAL;
         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
         if (r < 0)
@@ -1207,6 +1318,8 @@ int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
                 return -EINVAL;
         if (serial == 0)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         c = hashmap_remove(bus->reply_callbacks, &serial);
         if (!c)
@@ -1224,11 +1337,8 @@ int bus_ensure_running(sd_bus *bus) {
 
         assert(bus);
 
-        if (bus->input_fd < 0)
+        if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED)
                 return -ENOTCONN;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-
         if (bus->state == BUS_RUNNING)
                 return 1;
 
@@ -1261,9 +1371,7 @@ int sd_bus_send_with_reply_and_block(
 
         if (!bus)
                 return -EINVAL;
-        if (bus->output_fd < 0)
-                return -ENOTCONN;
-        if (bus->state == BUS_UNSET)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
@@ -1273,6 +1381,8 @@ int sd_bus_send_with_reply_and_block(
                 return -EINVAL;
         if (bus_error_is_dirty(error))
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -1305,7 +1415,10 @@ int sd_bus_send_with_reply_and_block(
                         room = true;
                 }
 
-                r = bus_socket_read_message(bus, &incoming);
+                if (bus->is_kernel)
+                        r = bus_kernel_read_message(bus, &incoming);
+                else
+                        r = bus_socket_read_message(bus, &incoming);
                 if (r < 0)
                         return r;
                 if (incoming) {
@@ -1314,7 +1427,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;
                                 }
 
@@ -1371,10 +1489,12 @@ int sd_bus_send_with_reply_and_block(
 int sd_bus_get_fd(sd_bus *bus) {
         if (!bus)
                 return -EINVAL;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (bus->input_fd != bus->output_fd)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         return bus->input_fd;
 }
@@ -1384,10 +1504,10 @@ int sd_bus_get_events(sd_bus *bus) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->state == BUS_OPENING)
                 flags |= POLLOUT;
@@ -1415,10 +1535,10 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
                 return -EINVAL;
         if (!timeout_usec)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->state == BUS_AUTHENTICATING) {
                 *timeout_usec = bus->auth_timeout;
@@ -1441,6 +1561,7 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
 }
 
 static int process_timeout(sd_bus *bus) {
+        _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
         struct reply_callback *c;
         usec_t n;
         int r;
@@ -1455,10 +1576,18 @@ static int process_timeout(sd_bus *bus) {
         if (c->timeout > n)
                 return 0;
 
+        r = bus_message_new_synthetic_error(
+                        bus,
+                        c->serial,
+                        &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.Timeout", "Timed out"),
+                        &m);
+        if (r < 0)
+                return r;
+
         assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
         hashmap_remove(bus->reply_callbacks, &c->serial);
 
-        r = c->callback(bus, ETIMEDOUT, NULL, c->userdata);
+        r = c->callback(bus, m, c->userdata);
         free(c);
 
         return r < 0 ? r : 1;
@@ -1504,7 +1633,11 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) {
         if (c->timeout != 0)
                 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
 
-        r = c->callback(bus, 0, m, c->userdata);
+        r = sd_bus_message_rewind(m, true);
+        if (r < 0)
+                return r;
+
+        r = c->callback(bus, m, c->userdata);
         free(c);
 
         return r;
@@ -1531,7 +1664,11 @@ static int process_filter(sd_bus *bus, sd_bus_message *m) {
 
                         l->last_iteration = bus->iteration_counter;
 
-                        r = l->callback(bus, 0, m, l->userdata);
+                        r = sd_bus_message_rewind(m, true);
+                        if (r < 0)
+                                return r;
+
+                        r = l->callback(bus, m, l->userdata);
                         if (r != 0)
                                 return r;
 
@@ -1551,7 +1688,7 @@ static int process_match(sd_bus *bus, sd_bus_message *m) {
         do {
                 bus->match_callbacks_modified = false;
 
-                r = bus_match_run(bus, &bus->match_callbacks, 0, m);
+                r = bus_match_run(bus, &bus->match_callbacks, m);
                 if (r != 0)
                         return r;
 
@@ -1640,7 +1777,11 @@ static int process_object(sd_bus *bus, sd_bus_message *m) {
 
                         c->last_iteration = bus->iteration_counter;
 
-                        r = c->callback(bus, 0, m, c->userdata);
+                        r = sd_bus_message_rewind(m, true);
+                        if (r < 0)
+                                return r;
+
+                        r = c->callback(bus, m, c->userdata);
                         if (r != 0)
                                 return r;
 
@@ -1666,7 +1807,11 @@ static int process_object(sd_bus *bus, sd_bus_message *m) {
 
                                 c->last_iteration = bus->iteration_counter;
 
-                                r = c->callback(bus, 0, m, c->userdata);
+                                r = sd_bus_message_rewind(m, true);
+                                if (r < 0)
+                                        return r;
+
+                                r = c->callback(bus, m, c->userdata);
                                 if (r != 0)
                                         return r;
 
@@ -1744,13 +1889,9 @@ static int process_introspect(sd_bus *bus, sd_bus_message *m) {
                 if (p)
                         *p = 0;
 
-                r = set_put(s, a);
-                if (r < 0) {
-                        free(a);
-
-                        if (r != -EEXIST)
-                                return r;
-                }
+                r = set_consume(s, a);
+                if (r < 0 && r != -EEXIST)
+                        return r;
         }
 
         f = open_memstream(&introspection, &size);
@@ -1850,6 +1991,10 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) {
                 goto null_message;
 
         if (ret) {
+                r = sd_bus_message_rewind(m, true);
+                if (r < 0)
+                        return r;
+
                 *ret = m;
                 m = NULL;
                 return 1;
@@ -1889,8 +2034,8 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->input_fd < 0)
-                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         /* We don't allow recursively invoking sd_bus_process(). */
         if (bus->processing)
@@ -1899,6 +2044,7 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
         switch (bus->state) {
 
         case BUS_UNSET:
+        case BUS_CLOSED:
                 return -ENOTCONN;
 
         case BUS_OPENING:
@@ -1932,14 +2078,14 @@ 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;
 
         assert(bus);
 
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
 
         e = sd_bus_get_events(bus);
@@ -1963,9 +2109,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;
@@ -1987,10 +2131,11 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
         if (bus->rqueue_size > 0)
                 return 0;
 
@@ -2002,10 +2147,10 @@ int sd_bus_flush(sd_bus *bus) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->output_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -2035,6 +2180,8 @@ int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *user
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         f = new0(struct filter_callback, 1);
         if (!f)
@@ -2054,6 +2201,8 @@ int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *u
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
                 if (f->callback == callback && f->userdata == userdata) {
@@ -2083,6 +2232,8 @@ static int bus_add_object(
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func);
         if (r < 0)
@@ -2128,6 +2279,8 @@ static int bus_remove_object(
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         c = hashmap_get(bus->object_callbacks, path);
         if (!c)
@@ -2168,6 +2321,8 @@ int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t ca
                 return -EINVAL;
         if (!match)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->bus_client) {
                 r = bus_add_match_internal(bus, match);
@@ -2195,6 +2350,8 @@ int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t
                 return -EINVAL;
         if (!match)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->bus_client)
                 r = bus_remove_match_internal(bus, match);
@@ -2222,6 +2379,10 @@ int sd_bus_emit_signal(
 
         if (!bus)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_message_new_signal(bus, path, interface, member, &m);
         if (r < 0)
@@ -2252,6 +2413,10 @@ int sd_bus_call_method(
 
         if (!bus)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
         if (r < 0)
@@ -2265,3 +2430,83 @@ 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 (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        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 (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        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);
+}
+
+bool bus_pid_changed(sd_bus *bus) {
+        assert(bus);
+
+        /* We don't support people creating a bus connection and
+         * keeping it around over a fork(). Let's complain. */
+
+        return bus->original_pid != getpid();
+}