chiark / gitweb /
bus: bump serial number counter when sending already sealed messages
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 08ab202baf8301b1eca4c280d2fa52a1e5ba802a..0be5a29f367fbe4e53bd2ef6a849feb55a8a1169 100644 (file)
@@ -26,6 +26,8 @@
 #include <netdb.h>
 #include <sys/poll.h>
 #include <byteswap.h>
+#include <sys/mman.h>
+#include <pthread.h>
 
 #include "util.h"
 #include "macro.h"
 #include "bus-socket.h"
 #include "bus-kernel.h"
 #include "bus-control.h"
+#include "bus-introspect.h"
+#include "bus-signature.h"
+#include "bus-objects.h"
+#include "bus-util.h"
+#include "bus-container.h"
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
 
@@ -55,20 +62,63 @@ static void bus_close_fds(sd_bus *b) {
         b->input_fd = b->output_fd = -1;
 }
 
+static void bus_node_destroy(sd_bus *b, struct node *n) {
+        struct node_callback *c;
+        struct node_vtable *v;
+        struct node_enumerator *e;
+
+        assert(b);
+
+        if (!n)
+                return;
+
+        while (n->child)
+                bus_node_destroy(b, n->child);
+
+        while ((c = n->callbacks)) {
+                LIST_REMOVE(callbacks, n->callbacks, c);
+                free(c);
+        }
+
+        while ((v = n->vtables)) {
+                LIST_REMOVE(vtables, n->vtables, v);
+                free(v->interface);
+                free(v);
+        }
+
+        while ((e = n->enumerators)) {
+                LIST_REMOVE(enumerators, n->enumerators, e);
+                free(e);
+        }
+
+        if (n->parent)
+                LIST_REMOVE(siblings, n->parent->child, n);
+
+        assert_se(hashmap_remove(b->nodes, n->path) == n);
+        free(n->path);
+        free(n);
+}
+
 static void bus_free(sd_bus *b) {
         struct filter_callback *f;
-        struct object_callback *c;
+        struct node *n;
         unsigned i;
 
         assert(b);
 
+        sd_bus_detach_event(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->machine);
 
         free(b->exec_path);
         strv_free(b->exec_argv);
@@ -88,28 +138,31 @@ static void bus_free(sd_bus *b) {
         prioq_free(b->reply_callbacks_prioq);
 
         while ((f = b->filter_callbacks)) {
-                LIST_REMOVE(struct filter_callback, callbacks, b->filter_callbacks, f);
+                LIST_REMOVE(callbacks, b->filter_callbacks, f);
                 free(f);
         }
 
-        while ((c = hashmap_steal_first(b->object_callbacks))) {
-                free(c->path);
-                free(c);
-        }
-
-        hashmap_free(b->object_callbacks);
         bus_match_free(&b->match_callbacks);
 
+        hashmap_free_free(b->vtable_methods);
+        hashmap_free_free(b->vtable_properties);
+
+        while ((n = hashmap_first(b->nodes)))
+                bus_node_destroy(b, n);
+
+        hashmap_free(b->nodes);
+
         bus_kernel_flush_memfd(b);
 
+        assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
+
         free(b);
 }
 
-int sd_bus_new(sd_bus **ret) {
+_public_ int sd_bus_new(sd_bus **ret) {
         sd_bus *r;
 
-        if (!ret)
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
 
         r = new0(sd_bus, 1);
         if (!r)
@@ -118,7 +171,10 @@ int sd_bus_new(sd_bus **ret) {
         r->n_ref = REFCNT_INIT;
         r->input_fd = r->output_fd = -1;
         r->message_version = 1;
-        r->negotiate_fds = true;
+        r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
+        r->original_pid = getpid();
+
+        assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
 
         /* We guarantee that wqueue always has space for at least one
          * entry */
@@ -132,15 +188,13 @@ int sd_bus_new(sd_bus **ret) {
         return 0;
 }
 
-int sd_bus_set_address(sd_bus *bus, const char *address) {
+_public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
         char *a;
 
-        if (!bus)
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
-        if (!address)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(address, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         a = strdup(address);
         if (!a)
@@ -152,32 +206,26 @@ int sd_bus_set_address(sd_bus *bus, const char *address) {
         return 0;
 }
 
-int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
-        if (!bus)
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
-        if (input_fd < 0)
-                return -EINVAL;
-        if (output_fd < 0)
-                return -EINVAL;
+_public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(input_fd >= 0, -EINVAL);
+        assert_return(output_fd >= 0, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         bus->input_fd = input_fd;
         bus->output_fd = output_fd;
         return 0;
 }
 
-int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
+_public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
         char *p, **a;
 
-        if (!bus)
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
-        if (!path)
-                return -EINVAL;
-        if (strv_isempty(argv))
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(path, -EINVAL);
+        assert_return(!strv_isempty(argv), -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         p = strdup(path);
         if (!p)
@@ -198,44 +246,120 @@ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
         return 0;
 }
 
-int sd_bus_set_bus_client(sd_bus *bus, int b) {
-        if (!bus)
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
+_public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         bus->bus_client = !!b;
         return 0;
 }
 
-int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
-        if (!bus)
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
+_public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        bus->negotiate_fds = !!b;
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
         return 0;
 }
 
-int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
-        if (!bus)
-                return -EINVAL;
-        if (!b && !sd_id128_equal(server_id, SD_ID128_NULL))
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
+_public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_TIMESTAMP, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CREDS, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_comm(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_COMM, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_exe(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_EXE, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_cmdline(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CMDLINE, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_cgroup(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CGROUP, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_caps(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CAPS, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_selinux_context(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_SECLABEL, b);
+        return 0;
+}
+
+_public_ int sd_bus_negotiate_attach_audit(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_AUDIT, b);
+        return 0;
+}
+
+_public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
+        assert_return(bus, -EINVAL);
+        assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         bus->is_server = !!b;
         bus->server_id = server_id;
         return 0;
 }
 
-int sd_bus_set_anonymous(sd_bus *bus, int b) {
-        if (!bus)
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
+_public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         bus->anonymous_auth = !!b;
         return 0;
@@ -249,9 +373,11 @@ static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
         assert(bus->state == BUS_HELLO);
         assert(reply);
 
-        r = bus_message_to_errno(reply);
+        r = sd_bus_message_get_errno(reply);
         if (r < 0)
                 return r;
+        if (r > 0)
+                return -r;
 
         r = sd_bus_message_read(reply, "s", &s);
         if (r < 0)
@@ -288,7 +414,7 @@ static int bus_send_hello(sd_bus *bus) {
         if (r < 0)
                 return r;
 
-        return sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
+        return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
 }
 
 int bus_start_running(sd_bus *bus) {
@@ -647,6 +773,45 @@ static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
         return 0;
 }
 
+static int parse_container_address(sd_bus *b, const char **p, char **guid) {
+        _cleanup_free_ char *machine = 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, "machine", &machine);
+                if (r < 0)
+                        return r;
+                else if (r > 0)
+                        continue;
+
+                skip_address_key(p);
+        }
+
+        if (!machine)
+                return -EINVAL;
+
+        free(b->machine);
+        b->machine = machine;
+        machine = NULL;
+
+        b->sockaddr.un.sun_family = AF_UNIX;
+        strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
+        b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
+
+        return 0;
+}
+
 static void bus_reset_parsed_address(sd_bus *b) {
         assert(b);
 
@@ -659,6 +824,8 @@ static void bus_reset_parsed_address(sd_bus *b) {
         b->server_id = SD_ID128_NULL;
         free(b->kernel);
         b->kernel = NULL;
+        free(b->machine);
+        b->machine = NULL;
 }
 
 static int bus_parse_next_address(sd_bus *b) {
@@ -717,6 +884,14 @@ static int bus_parse_next_address(sd_bus *b) {
                         if (r < 0)
                                 return r;
 
+                        break;
+                } else if (startswith(a, "x-container:")) {
+
+                        a += 12;
+                        r = parse_container_address(b, &a, &guid);
+                        if (r < 0)
+                                return r;
+
                         break;
                 }
 
@@ -743,24 +918,32 @@ static int bus_start_address(sd_bus *b) {
         for (;;) {
                 sd_bus_close(b);
 
-                if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
+                if (b->exec_path) {
 
-                        r = bus_socket_connect(b);
+                        r = bus_socket_exec(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;
 
-                } else if (b->exec_path) {
+                } else if (b->machine) {
 
-                        r = bus_socket_exec(b);
+                        r = bus_container_connect(b);
                         if (r >= 0)
                                 return r;
 
                         b->last_connect_error = -r;
-                } else if (b->kernel) {
 
-                        r = bus_kernel_connect(b);
+                } else if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
+
+                        r = bus_socket_connect(b);
                         if (r >= 0)
                                 return r;
 
@@ -817,13 +1000,12 @@ static int bus_start_fd(sd_bus *b) {
                 return bus_socket_take_fd(b);
 }
 
-int sd_bus_start(sd_bus *bus) {
+_public_ int sd_bus_start(sd_bus *bus) {
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (bus->state != BUS_UNSET)
-                return -EPERM;
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state == BUS_UNSET, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         bus->state = BUS_OPENING;
 
@@ -832,7 +1014,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 || bus->kernel)
+        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
                 r = bus_start_address(bus);
         else
                 return -EINVAL;
@@ -843,13 +1025,12 @@ int sd_bus_start(sd_bus *bus) {
         return bus_send_hello(bus);
 }
 
-int sd_bus_open_system(sd_bus **ret) {
+_public_ int sd_bus_open_system(sd_bus **ret) {
         const char *e;
         sd_bus *b;
         int r;
 
-        if (!ret)
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
 
         r = sd_bus_new(&b);
         if (r < 0)
@@ -880,14 +1061,13 @@ fail:
         return r;
 }
 
-int sd_bus_open_user(sd_bus **ret) {
+_public_ int sd_bus_open_user(sd_bus **ret) {
         const char *e;
         sd_bus *b;
         size_t l;
         int r;
 
-        if (!ret)
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
 
         r = sd_bus_new(&b);
         if (r < 0)
@@ -930,15 +1110,90 @@ fail:
         return r;
 }
 
-void sd_bus_close(sd_bus *bus) {
+_public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
+        _cleanup_free_ char *e = NULL;
+        char *p = NULL;
+        sd_bus *bus;
+        int r;
+
+        assert_return(host, -EINVAL);
+        assert_return(ret, -EINVAL);
+
+        e = bus_address_escape(host);
+        if (!e)
+                return -ENOMEM;
+
+        p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
+        if (!p)
+                return -ENOMEM;
+
+        r = sd_bus_new(&bus);
+        if (r < 0) {
+                free(p);
+                return r;
+        }
+
+        bus->address = p;
+        bus->bus_client = true;
+
+        r = sd_bus_start(bus);
+        if (r < 0) {
+                bus_free(bus);
+                return r;
+        }
+
+        *ret = bus;
+        return 0;
+}
+
+_public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
+        _cleanup_free_ char *e = NULL;
+        sd_bus *bus;
+        char *p;
+        int r;
+
+        assert_return(machine, -EINVAL);
+        assert_return(ret, -EINVAL);
+
+        e = bus_address_escape(machine);
+        if (!e)
+                return -ENOMEM;
+
+        p = strjoin("x-container:machine=", e, NULL);
+        if (!p)
+                return -ENOMEM;
+
+        r = sd_bus_new(&bus);
+        if (r < 0) {
+                free(p);
+                return r;
+        }
+
+        bus->address = p;
+        bus->bus_client = true;
+
+        r = sd_bus_start(bus);
+        if (r < 0) {
+                bus_free(bus);
+                return r;
+        }
+
+        *ret = bus;
+        return 0;
+}
+
+_public_ void sd_bus_close(sd_bus *bus) {
         if (!bus)
                 return;
-
-        if (bus->state != BUS_CLOSED)
+        if (bus->state == BUS_CLOSED)
+                return;
+        if (bus_pid_changed(bus))
                 return;
 
         bus->state = BUS_CLOSED;
 
+        sd_bus_detach_event(bus);
+
         if (!bus->is_kernel)
                 bus_close_fds(bus);
 
@@ -949,18 +1204,16 @@ void sd_bus_close(sd_bus *bus) {
          * freed. */
 }
 
-sd_bus *sd_bus_ref(sd_bus *bus) {
-        if (!bus)
-                return NULL;
+_public_ sd_bus *sd_bus_ref(sd_bus *bus) {
+        assert_return(bus, NULL);
 
         assert_se(REFCNT_INC(bus->n_ref) >= 2);
 
         return bus;
 }
 
-sd_bus *sd_bus_unref(sd_bus *bus) {
-        if (!bus)
-                return NULL;
+_public_ sd_bus *sd_bus_unref(sd_bus *bus) {
+        assert_return(bus, NULL);
 
         if (REFCNT_DEC(bus->n_ref) <= 0)
                 bus_free(bus);
@@ -968,23 +1221,23 @@ sd_bus *sd_bus_unref(sd_bus *bus) {
         return NULL;
 }
 
-int sd_bus_is_open(sd_bus *bus) {
-        if (!bus)
-                return -EINVAL;
+_public_ int sd_bus_is_open(sd_bus *bus) {
+
+        assert_return(bus, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         return BUS_IS_OPEN(bus->state);
 }
 
-int sd_bus_can_send(sd_bus *bus, char type) {
+_public_ int sd_bus_can_send(sd_bus *bus, char type) {
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
+        assert_return(bus, -EINVAL);
+        assert_return(bus->state != BUS_UNSET, -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (type == SD_BUS_TYPE_UNIX_FD) {
-                if (!bus->negotiate_fds)
+                if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
                         return 0;
 
                 r = bus_ensure_running(bus);
@@ -997,13 +1250,12 @@ int sd_bus_can_send(sd_bus *bus, char type) {
         return bus_type_is_valid(type);
 }
 
-int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
+_public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (!server_id)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(server_id, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -1019,8 +1271,13 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
         if (m->header->version > b->message_version)
                 return -EPERM;
 
-        if (m->sealed)
+        if (m->sealed) {
+                /* If we copy the same message to multiple
+                 * destinations, avoid using the same serial
+                 * numbers. */
+                b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
                 return 0;
+        }
 
         return bus_message_seal(m, ++b->serial);
 }
@@ -1098,22 +1355,20 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
                 if (r == 0)
                         return ret;
 
-                r = 1;
+                ret = 1;
         } while (!z);
 
         *m = z;
-        return 1;
+        return ret;
 }
 
-int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
+_public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (!m)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(m, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (m->n_fds > 0) {
                 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
@@ -1135,7 +1390,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         /* 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;
+                return 1;
 
         if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
                 size_t idx = 0;
@@ -1177,7 +1432,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         if (serial)
                 *serial = BUS_MESSAGE_SERIAL(m);
 
-        return 0;
+        return 1;
 }
 
 static usec_t calc_elapse(uint64_t usec) {
@@ -1208,7 +1463,7 @@ static int timeout_compare(const void *a, const void *b) {
         return 0;
 }
 
-int sd_bus_send_with_reply(
+_public_ int sd_bus_call_async(
                 sd_bus *bus,
                 sd_bus_message *m,
                 sd_bus_message_handler_t callback,
@@ -1219,18 +1474,13 @@ int sd_bus_send_with_reply(
         struct reply_callback *c;
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (!m)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return -EINVAL;
-        if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(m, -EINVAL);
+        assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
         if (r < 0)
@@ -1265,27 +1515,26 @@ int sd_bus_send_with_reply(
                 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
                 if (r < 0) {
                         c->timeout = 0;
-                        sd_bus_send_with_reply_cancel(bus, c->serial);
+                        sd_bus_call_async_cancel(bus, c->serial);
                         return r;
                 }
         }
 
         r = sd_bus_send(bus, m, serial);
         if (r < 0) {
-                sd_bus_send_with_reply_cancel(bus, c->serial);
+                sd_bus_call_async_cancel(bus, c->serial);
                 return r;
         }
 
         return r;
 }
 
-int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
+_public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
         struct reply_callback *c;
 
-        if (!bus)
-                return -EINVAL;
-        if (serial == 0)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(serial != 0, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         c = hashmap_remove(bus->reply_callbacks, &serial);
         if (!c)
@@ -1323,7 +1572,7 @@ int bus_ensure_running(sd_bus *bus) {
         }
 }
 
-int sd_bus_send_with_reply_and_block(
+_public_ int sd_bus_call(
                 sd_bus *bus,
                 sd_bus_message *m,
                 uint64_t usec,
@@ -1335,18 +1584,13 @@ int sd_bus_send_with_reply_and_block(
         uint64_t serial;
         bool room = false;
 
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (!m)
-                return -EINVAL;
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return -EINVAL;
-        if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
-                return -EINVAL;
-        if (bus_error_is_dirty(error))
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(m, -EINVAL);
+        assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
+        assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
+        assert_return(!bus_error_is_dirty(error), -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -1390,17 +1634,17 @@ int sd_bus_send_with_reply_and_block(
                         if (incoming->reply_serial == serial) {
                                 /* Found a match! */
 
-                                if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
+                                if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
 
                                         if (reply)
                                                 *reply = incoming;
                                         else
                                                 sd_bus_message_unref(incoming);
 
-                                        return 0;
+                                        return 1;
                                 }
 
-                                if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) {
+                                if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
                                         int k;
 
                                         r = sd_bus_error_copy(error, &incoming->error);
@@ -1409,13 +1653,26 @@ int sd_bus_send_with_reply_and_block(
                                                 return r;
                                         }
 
-                                        k = bus_error_to_errno(&incoming->error);
+                                        k = sd_bus_error_get_errno(&incoming->error);
                                         sd_bus_message_unref(incoming);
-                                        return k;
+                                        return -k;
                                 }
 
                                 sd_bus_message_unref(incoming);
                                 return -EIO;
+
+                        } else if (incoming->header->serial == serial &&
+                                   bus->unique_name &&
+                                   incoming->sender &&
+                                   streq(bus->unique_name, incoming->sender)) {
+
+                                /* Our own message? Somebody is trying
+                                 * to send its own client a message,
+                                 * let's not dead-lock, let's fail
+                                 * immediately. */
+
+                                sd_bus_message_unref(incoming);
+                                return -ELOOP;
                         }
 
                         /* There's already guaranteed to be room for
@@ -1450,24 +1707,22 @@ int sd_bus_send_with_reply_and_block(
         }
 }
 
-int sd_bus_get_fd(sd_bus *bus) {
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
-        if (bus->input_fd != bus->output_fd)
-                return -EPERM;
+_public_ int sd_bus_get_fd(sd_bus *bus) {
+
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(bus->input_fd == bus->output_fd, -EPERM);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         return bus->input_fd;
 }
 
-int sd_bus_get_events(sd_bus *bus) {
+_public_ int sd_bus_get_events(sd_bus *bus) {
         int flags = 0;
 
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (bus->state == BUS_OPENING)
                 flags |= POLLOUT;
@@ -1488,15 +1743,13 @@ int sd_bus_get_events(sd_bus *bus) {
         return flags;
 }
 
-int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
+_public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
         struct reply_callback *c;
 
-        if (!bus)
-                return -EINVAL;
-        if (!timeout_usec)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
+        assert_return(bus, -EINVAL);
+        assert_return(timeout_usec, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         if (bus->state == BUS_AUTHENTICATING) {
                 *timeout_usec = bus->auth_timeout;
@@ -1508,8 +1761,13 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
                 return 0;
         }
 
-        c = prioq_peek(bus->reply_callbacks_prioq);
-        if (!c) {
+        if (bus->rqueue_size > 0) {
+                *timeout_usec = 0;
+                return 1;
+        }
+
+        c = prioq_peek(bus->reply_callbacks_prioq);
+        if (!c) {
                 *timeout_usec = (uint64_t) -1;
                 return 0;
         }
@@ -1537,7 +1795,7 @@ static int process_timeout(sd_bus *bus) {
         r = bus_message_new_synthetic_error(
                         bus,
                         c->serial,
-                        &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.Timeout", "Timed out"),
+                        &SD_BUS_ERROR_MAKE(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
                         &m);
         if (r < 0)
                 return r;
@@ -1563,8 +1821,8 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) {
          * here (we leave that to the usual handling), we just verify
          * we don't let any earlier msg through. */
 
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
-            m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+        if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
+            m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
                 return -EIO;
 
         if (m->reply_serial != bus->hello_serial)
@@ -1580,8 +1838,8 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
-            m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+        if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
+            m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
                 return 0;
 
         c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
@@ -1662,7 +1920,7 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+        if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
                 return 0;
 
         if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
@@ -1687,197 +1945,12 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) {
 
                 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
         } else {
-                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-
-                sd_bus_error_set(&error,
-                                 "org.freedesktop.DBus.Error.UnknownMethod",
+                r = sd_bus_message_new_method_errorf(
+                                bus, m, &reply,
+                                SD_BUS_ERROR_UNKNOWN_METHOD,
                                  "Unknown method '%s' on interface '%s'.", m->member, m->interface);
-
-                r = sd_bus_message_new_method_error(bus, m, &error, &reply);
-        }
-
-        if (r < 0)
-                return r;
-
-        r = sd_bus_send(bus, reply, NULL);
-        if (r < 0)
-                return r;
-
-        return 1;
-}
-
-static int process_object(sd_bus *bus, sd_bus_message *m) {
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-        struct object_callback *c;
-        int r;
-        bool found = false;
-        size_t pl;
-
-        assert(bus);
-        assert(m);
-
-        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
-                return 0;
-
-        if (hashmap_isempty(bus->object_callbacks))
-                return 0;
-
-        pl = strlen(m->path);
-
-        do {
-                char p[pl+1];
-
-                bus->object_callbacks_modified = false;
-
-                c = hashmap_get(bus->object_callbacks, m->path);
-                if (c && c->last_iteration != bus->iteration_counter) {
-
-                        c->last_iteration = bus->iteration_counter;
-
-                        r = sd_bus_message_rewind(m, true);
-                        if (r < 0)
-                                return r;
-
-                        r = c->callback(bus, m, c->userdata);
-                        if (r != 0)
-                                return r;
-
-                        found = true;
-                }
-
-                /* Look for fallback prefixes */
-                strcpy(p, m->path);
-                for (;;) {
-                        char *e;
-
-                        if (bus->object_callbacks_modified)
-                                break;
-
-                        e = strrchr(p, '/');
-                        if (e == p || !e)
-                                break;
-
-                        *e = 0;
-
-                        c = hashmap_get(bus->object_callbacks, p);
-                        if (c && c->last_iteration != bus->iteration_counter && c->is_fallback) {
-
-                                c->last_iteration = bus->iteration_counter;
-
-                                r = sd_bus_message_rewind(m, true);
-                                if (r < 0)
-                                        return r;
-
-                                r = c->callback(bus, m, c->userdata);
-                                if (r != 0)
-                                        return r;
-
-                                found = true;
-                        }
-                }
-
-        } while (bus->object_callbacks_modified);
-
-        /* We found some handlers but none wanted to take this, then
-         * return this -- with one exception, we can handle
-         * introspection minimally ourselves */
-        if (!found || sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect"))
-                return 0;
-
-        sd_bus_error_set(&error,
-                         "org.freedesktop.DBus.Error.UnknownMethod",
-                         "Unknown method '%s' or interface '%s'.", m->member, m->interface);
-
-        r = sd_bus_message_new_method_error(bus, m, &error, &reply);
-        if (r < 0)
-                return r;
-
-        r = sd_bus_send(bus, reply, NULL);
-        if (r < 0)
-                return r;
-
-        return 1;
-}
-
-static int process_introspect(sd_bus *bus, sd_bus_message *m) {
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-        _cleanup_free_ char *introspection = NULL;
-        _cleanup_set_free_free_ Set *s = NULL;
-        _cleanup_fclose_ FILE *f = NULL;
-        struct object_callback *c;
-        Iterator i;
-        size_t size = 0;
-        char *node;
-        int r;
-
-        assert(bus);
-        assert(m);
-
-        if (!sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect"))
-                return 0;
-
-        if (!m->path)
-                return 0;
-
-        s = set_new(string_hash_func, string_compare_func);
-        if (!s)
-                return -ENOMEM;
-
-        HASHMAP_FOREACH(c, bus->object_callbacks, i) {
-                const char *e;
-                char *a, *p;
-
-                if (streq(c->path, "/"))
-                        continue;
-
-                if (streq(m->path, "/"))
-                        e = c->path;
-                else {
-                        e = startswith(c->path, m->path);
-                        if (!e || *e != '/')
-                                continue;
-                }
-
-                a = strdup(e+1);
-                if (!a)
-                        return -ENOMEM;
-
-                p = strchr(a, '/');
-                if (p)
-                        *p = 0;
-
-                r = set_consume(s, a);
-                if (r < 0 && r != -EEXIST)
-                        return r;
-        }
-
-        f = open_memstream(&introspection, &size);
-        if (!f)
-                return -ENOMEM;
-
-        fputs(SD_BUS_INTROSPECT_DOCTYPE, f);
-        fputs("<node>\n", f);
-        fputs(SD_BUS_INTROSPECT_INTERFACE_PEER, f);
-        fputs(SD_BUS_INTROSPECT_INTERFACE_INTROSPECTABLE, f);
-
-        while ((node = set_steal_first(s))) {
-                fprintf(f, " <node name=\"%s\"/>\n", node);
-                free(node);
         }
 
-        fputs("</node>\n", f);
-
-        fflush(f);
-
-        if (ferror(f))
-                return -ENOMEM;
-
-        r = sd_bus_message_new_method_return(bus, m, &reply);
-        if (r < 0)
-                return r;
-
-        r = sd_bus_message_append(reply, "s", introspection);
         if (r < 0)
                 return r;
 
@@ -1894,33 +1967,40 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
+        bus->current = m;
         bus->iteration_counter++;
 
+        log_debug("Got message sender=%s object=%s interface=%s member=%s",
+                  strna(sd_bus_message_get_sender(m)),
+                  strna(sd_bus_message_get_path(m)),
+                  strna(sd_bus_message_get_interface(m)),
+                  strna(sd_bus_message_get_member(m)));
+
         r = process_hello(bus, m);
         if (r != 0)
-                return r;
+                goto finish;
 
         r = process_reply(bus, m);
         if (r != 0)
-                return r;
+                goto finish;
 
         r = process_filter(bus, m);
         if (r != 0)
-                return r;
+                goto finish;
 
         r = process_match(bus, m);
         if (r != 0)
-                return r;
+                goto finish;
 
         r = process_builtin(bus, m);
         if (r != 0)
-                return r;
+                goto finish;
 
-        r = process_object(bus, m);
-        if (r != 0)
-                return r;
+        r = bus_process_object(bus, m);
 
-        return process_introspect(bus, m);
+finish:
+        bus->current = NULL;
+        return r;
 }
 
 static int process_running(sd_bus *bus, sd_bus_message **ret) {
@@ -1958,17 +2038,12 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) {
                 return 1;
         }
 
-        if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) {
-                _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-
-                sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path);
-
-                r = sd_bus_message_new_method_error(bus, m, &error, &reply);
-                if (r < 0)
-                        return r;
+        if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
 
-                r = sd_bus_send(bus, reply, NULL);
+                r = sd_bus_reply_method_errorf(
+                                bus, m,
+                                SD_BUS_ERROR_UNKNOWN_OBJECT,
+                                "Unknown object '%s'.", m->path);
                 if (r < 0)
                         return r;
         }
@@ -1982,7 +2057,8 @@ null_message:
         return r;
 }
 
-int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
+_public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
+        BUS_DONT_DESTROY(bus);
         int r;
 
         /* Returns 0 when we didn't do anything. This should cause the
@@ -1990,12 +2066,11 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
          * time. Returns > 0 when we did something, which possibly
          * means *ret is filled in with an unprocessed message. */
 
-        if (!bus)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         /* We don't allow recursively invoking sd_bus_process(). */
-        if (bus->processing)
-                return -EBUSY;
+        assert_return(!bus->processing, -EBUSY);
 
         switch (bus->state) {
 
@@ -2037,29 +2112,33 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
         struct pollfd p[2] = {};
         int r, e, n;
         struct timespec ts;
-        usec_t until, m;
+        usec_t m = (usec_t) -1;
 
         assert(bus);
-
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
 
         e = sd_bus_get_events(bus);
         if (e < 0)
                 return e;
 
         if (need_more)
+                /* The caller really needs some more data, he doesn't
+                 * care about what's already read, or any timeouts
+                 * except its own.*/
                 e |= POLLIN;
-
-        r = sd_bus_get_timeout(bus, &until);
-        if (r < 0)
-                return r;
-        if (r == 0)
-                m = (uint64_t) -1;
         else {
-                usec_t nw;
-                nw = now(CLOCK_MONOTONIC);
-                m = until > nw ? until - nw : 0;
+                usec_t until;
+                /* The caller wants to process if there's something to
+                 * process, but doesn't care otherwise */
+
+                r = sd_bus_get_timeout(bus, &until);
+                if (r < 0)
+                        return r;
+                if (r > 0) {
+                        usec_t nw;
+                        nw = now(CLOCK_MONOTONIC);
+                        m = until > nw ? until - nw : 0;
+                }
         }
 
         if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
@@ -2083,25 +2162,24 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
         return r > 0 ? 1 : 0;
 }
 
-int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
+_public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
+
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
         if (bus->rqueue_size > 0)
                 return 0;
 
         return bus_poll(bus, false, timeout_usec);
 }
 
-int sd_bus_flush(sd_bus *bus) {
+_public_ int sd_bus_flush(sd_bus *bus) {
         int r;
 
-        if (!bus)
-                return -EINVAL;
-        if (!BUS_IS_OPEN(bus->state))
-                return -ENOTCONN;
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -2124,13 +2202,15 @@ int sd_bus_flush(sd_bus *bus) {
         }
 }
 
-int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) {
+_public_ int sd_bus_add_filter(sd_bus *bus,
+                               sd_bus_message_handler_t callback,
+                               void *userdata) {
+
         struct filter_callback *f;
 
-        if (!bus)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         f = new0(struct filter_callback, 1);
         if (!f)
@@ -2139,22 +2219,24 @@ int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *user
         f->userdata = userdata;
 
         bus->filter_callbacks_modified = true;
-        LIST_PREPEND(struct filter_callback, callbacks, bus->filter_callbacks, f);
+        LIST_PREPEND(callbacks, bus->filter_callbacks, f);
         return 0;
 }
 
-int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) {
+_public_ int sd_bus_remove_filter(sd_bus *bus,
+                                  sd_bus_message_handler_t callback,
+                                  void *userdata) {
+
         struct filter_callback *f;
 
-        if (!bus)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
         LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
                 if (f->callback == callback && f->userdata == userdata) {
                         bus->filter_callbacks_modified = true;
-                        LIST_REMOVE(struct filter_callback, callbacks, bus->filter_callbacks, f);
+                        LIST_REMOVE(callbacks, bus->filter_callbacks, f);
                         free(f);
                         return 1;
                 }
@@ -2163,264 +2245,301 @@ int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *u
         return 0;
 }
 
-static int bus_add_object(
-                sd_bus *bus,
-                bool fallback,
-                const char *path,
-                sd_bus_message_handler_t callback,
-                void *userdata) {
+_public_ int sd_bus_add_match(sd_bus *bus,
+                              const char *match,
+                              sd_bus_message_handler_t callback,
+                              void *userdata) {
 
-        struct object_callback *c;
-        int r;
+        struct bus_match_component *components = NULL;
+        unsigned n_components = 0;
+        uint64_t cookie = 0;
+        int r = 0;
 
-        if (!bus)
-                return -EINVAL;
-        if (!path)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(match, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func);
+        r = bus_match_parse(match, &components, &n_components);
         if (r < 0)
-                return r;
+                goto finish;
 
-        c = new0(struct object_callback, 1);
-        if (!c)
-                return -ENOMEM;
+        if (bus->bus_client) {
+                cookie = ++bus->match_cookie;
 
-        c->path = strdup(path);
-        if (!c->path) {
-                free(c);
-                return -ENOMEM;
+                r = bus_add_match_internal(bus, match, components, n_components, cookie);
+                if (r < 0)
+                        goto finish;
         }
 
-        c->callback = callback;
-        c->userdata = userdata;
-        c->is_fallback = fallback;
-
-        bus->object_callbacks_modified = true;
-        r = hashmap_put(bus->object_callbacks, c->path, c);
+        bus->match_callbacks_modified = true;
+        r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
         if (r < 0) {
-                free(c->path);
-                free(c);
-                return r;
+                if (bus->bus_client)
+                        bus_remove_match_internal(bus, match, cookie);
         }
 
-        return 0;
+finish:
+        bus_match_parse_free(components, n_components);
+        return r;
 }
 
-static int bus_remove_object(
-                sd_bus *bus,
-                bool fallback,
-                const char *path,
-                sd_bus_message_handler_t callback,
-                void *userdata) {
+_public_ int sd_bus_remove_match(sd_bus *bus,
+                                 const char *match,
+                                 sd_bus_message_handler_t callback,
+                                 void *userdata) {
 
-        struct object_callback *c;
+        struct bus_match_component *components = NULL;
+        unsigned n_components = 0;
+        int r = 0, q = 0;
+        uint64_t cookie = 0;
 
-        if (!bus)
-                return -EINVAL;
-        if (!path)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
+        assert_return(bus, -EINVAL);
+        assert_return(match, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        c = hashmap_get(bus->object_callbacks, path);
-        if (!c)
-                return 0;
+        r = bus_match_parse(match, &components, &n_components);
+        if (r < 0)
+                return r;
 
-        if (c->callback != callback || c->userdata != userdata || c->is_fallback != fallback)
-                return 0;
+        bus->match_callbacks_modified = true;
+        r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
 
-        bus->object_callbacks_modified = true;
-        assert_se(c == hashmap_remove(bus->object_callbacks, c->path));
+        if (bus->bus_client)
+                q = bus_remove_match_internal(bus, match, cookie);
 
-        free(c->path);
-        free(c);
+        bus_match_parse_free(components, n_components);
 
-        return 1;
+        return r < 0 ? r : q;
 }
 
-int sd_bus_add_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
-        return bus_add_object(bus, false, path, callback, userdata);
-}
+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. */
 
-int sd_bus_remove_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
-        return bus_remove_object(bus, false, path, callback, userdata);
+        return bus->original_pid != getpid();
 }
 
-int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
-        return bus_add_object(bus, true, prefix, callback, userdata);
+static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+        sd_bus *bus = userdata;
+        int r;
+
+        assert(bus);
+
+        r = sd_bus_process(bus, NULL);
+        if (r < 0)
+                return r;
+
+        return 1;
 }
 
-int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
-        return bus_remove_object(bus, true, prefix, callback, userdata);
+static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
+        sd_bus *bus = userdata;
+        int r;
+
+        assert(bus);
+
+        r = sd_bus_process(bus, NULL);
+        if (r < 0)
+                return r;
+
+        return 1;
 }
 
-int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
-        int r = 0;
+static int prepare_callback(sd_event_source *s, void *userdata) {
+        sd_bus *bus = userdata;
+        int r, e;
+        usec_t until;
 
-        if (!bus)
-                return -EINVAL;
-        if (!match)
-                return -EINVAL;
+        assert(s);
+        assert(bus);
 
-        if (bus->bus_client) {
-                r = bus_add_match_internal(bus, match);
+        e = sd_bus_get_events(bus);
+        if (e < 0)
+                return e;
+
+        if (bus->output_fd != bus->input_fd) {
+
+                r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
+                if (r < 0)
+                        return r;
+
+                r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
+                if (r < 0)
+                        return r;
+        } else {
+                r = sd_event_source_set_io_events(bus->input_io_event_source, e);
                 if (r < 0)
                         return r;
         }
 
-        if (callback) {
-                bus->match_callbacks_modified = true;
-                r = bus_match_add(&bus->match_callbacks, match, callback, userdata, NULL);
-                if (r < 0) {
+        r = sd_bus_get_timeout(bus, &until);
+        if (r < 0)
+                return r;
+        if (r > 0) {
+                int j;
 
-                        if (bus->bus_client)
-                                bus_remove_match_internal(bus, match);
-                }
+                j = sd_event_source_set_time(bus->time_event_source, until);
+                if (j < 0)
+                        return j;
         }
 
-        return r;
+        r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
+        if (r < 0)
+                return r;
+
+        return 1;
 }
 
-int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
-        int r = 0, q = 0;
+static int quit_callback(sd_event_source *event, void *userdata) {
+        sd_bus *bus = userdata;
 
-        if (!bus)
-                return -EINVAL;
-        if (!match)
-                return -EINVAL;
+        assert(event);
 
-        if (bus->bus_client)
-                r = bus_remove_match_internal(bus, match);
+        sd_bus_flush(bus);
+
+        return 1;
+}
+
+_public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
+        int r;
+
+        assert_return(bus, -EINVAL);
+        assert_return(!bus->event, -EBUSY);
+
+        assert(!bus->input_io_event_source);
+        assert(!bus->output_io_event_source);
+        assert(!bus->time_event_source);
 
-        if (callback) {
-                bus->match_callbacks_modified = true;
-                q = bus_match_remove(&bus->match_callbacks, match, callback, userdata);
+        if (event)
+                bus->event = sd_event_ref(event);
+        else  {
+                r = sd_event_default(&bus->event);
+                if (r < 0)
+                        return r;
         }
 
+        r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
         if (r < 0)
-                return r;
-        return q;
-}
+                goto fail;
 
-int sd_bus_emit_signal(
-                sd_bus *bus,
-                const char *path,
-                const char *interface,
-                const char *member,
-                const char *types, ...) {
+        r = sd_event_source_set_priority(bus->input_io_event_source, priority);
+        if (r < 0)
+                goto fail;
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        va_list ap;
-        int r;
+        if (bus->output_fd != bus->input_fd) {
+                r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
+                if (r < 0)
+                        goto fail;
 
-        if (!bus)
-                return -EINVAL;
+                r = sd_event_source_set_priority(bus->output_io_event_source, priority);
+                if (r < 0)
+                        goto fail;
+        }
 
-        r = sd_bus_message_new_signal(bus, path, interface, member, &m);
+        r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
         if (r < 0)
-                return r;
+                goto fail;
 
-        va_start(ap, types);
-        r = bus_message_append_ap(m, types, ap);
-        va_end(ap);
+        r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
         if (r < 0)
-                return r;
+                goto fail;
+
+        r = sd_event_source_set_priority(bus->time_event_source, priority);
+        if (r < 0)
+                goto fail;
+
+        r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
+        if (r < 0)
+                goto fail;
 
-        return sd_bus_send(bus, m, NULL);
+        return 0;
+
+fail:
+        sd_bus_detach_event(bus);
+        return r;
 }
 
-int sd_bus_call_method(
-                sd_bus *bus,
-                const char *destination,
-                const char *path,
-                const char *interface,
-                const char *member,
-                sd_bus_error *error,
-                sd_bus_message **reply,
-                const char *types, ...) {
+_public_ int sd_bus_detach_event(sd_bus *bus) {
+        assert_return(bus, -EINVAL);
+        assert_return(bus->event, -ENXIO);
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        va_list ap;
-        int r;
+        if (bus->input_io_event_source)
+                bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
 
-        if (!bus)
-                return -EINVAL;
+        if (bus->output_io_event_source)
+                bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
 
-        r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
-        if (r < 0)
-                return r;
+        if (bus->time_event_source)
+                bus->time_event_source = sd_event_source_unref(bus->time_event_source);
 
-        va_start(ap, types);
-        r = bus_message_append_ap(m, types, ap);
-        va_end(ap);
-        if (r < 0)
-                return r;
+        if (bus->quit_event_source)
+                bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
+
+        if (bus->event)
+                bus->event = sd_event_unref(bus->event);
 
-        return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
+        return 0;
 }
 
-int sd_bus_reply_method_return(
-                sd_bus *bus,
-                sd_bus_message *call,
-                const char *types, ...) {
+_public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
+        assert_return(bus, NULL);
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        va_list ap;
+        return bus->current;
+}
+
+static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
+        sd_bus *b = 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;
+        assert(bus_open);
+        assert(default_bus);
 
-        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+        if (!ret)
+                return !!*default_bus;
+
+        if (*default_bus) {
+                *ret = sd_bus_ref(*default_bus);
                 return 0;
+        }
 
-        r = sd_bus_message_new_method_return(bus, call, &m);
+        r = bus_open(&b);
         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;
+        b->default_bus_ptr = default_bus;
+        b->tid = gettid();
+        *default_bus = b;
 
-        return sd_bus_send(bus, m, NULL);
+        *ret = b;
+        return 1;
 }
 
-int sd_bus_reply_method_error(
-                sd_bus *bus,
-                sd_bus_message *call,
-                const sd_bus_error *e) {
+_public_ int sd_bus_default_system(sd_bus **ret) {
+        static __thread sd_bus *default_system_bus = NULL;
 
-        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
-        int r;
+        return bus_default(sd_bus_open_system, &default_system_bus, ret);
+}
 
-        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;
+_public_ int sd_bus_default_user(sd_bus **ret) {
+        static __thread sd_bus *default_user_bus = NULL;
+
+        return bus_default(sd_bus_open_user, &default_user_bus, ret);
+}
+
+_public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
+        assert_return(b, -EINVAL);
+        assert_return(tid, -EINVAL);
+        assert_return(!bus_pid_changed(b), -ECHILD);
 
-        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+        if (b->tid != 0) {
+                *tid = b->tid;
                 return 0;
+        }
 
-        r = sd_bus_message_new_method_error(bus, call, e, &m);
-        if (r < 0)
-                return r;
+        if (b->event)
+                return sd_event_get_tid(b->event, tid);
 
-        return sd_bus_send(bus, m, NULL);
+        return -ENXIO;
 }