chiark / gitweb /
bus: always show messages we send
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 060dcc14364745bba68809aef9d602cf471af50a..44ab071c4791656683c3eff8ad9500bfaba03a51 100644 (file)
 #include "bus-protocol.h"
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
+static int attach_io_events(sd_bus *b);
+static void detach_io_events(sd_bus *b);
 
 static void bus_close_fds(sd_bus *b) {
         assert(b);
 
+        detach_io_events(b);
+
         if (b->input_fd >= 0)
                 close_nointr_nofail(b->input_fd);
 
@@ -114,6 +118,7 @@ static void bus_reset_queues(sd_bus *b) {
         free(b->wqueue);
 
         b->rqueue = b->wqueue = NULL;
+        b->rqueue_allocated = b->wqueue_allocated = 0;
         b->rqueue_size = b->wqueue_size = 0;
 }
 
@@ -136,6 +141,8 @@ static void bus_free(sd_bus *b) {
         free(b->address);
         free(b->kernel);
         free(b->machine);
+        free(b->fake_label);
+        free(b->cgroup_root);
 
         free(b->exec_path);
         strv_free(b->exec_argv);
@@ -191,8 +198,7 @@ _public_ int sd_bus_new(sd_bus **ret) {
 
         /* We guarantee that wqueue always has space for at least one
          * entry */
-        r->wqueue = new(sd_bus_message*, 1);
-        if (!r->wqueue) {
+        if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) {
                 free(r);
                 return -ENOMEM;
         }
@@ -370,14 +376,14 @@ static int bus_send_hello(sd_bus *bus) {
         r = sd_bus_message_new_method_call(
                         bus,
                         "org.freedesktop.DBus",
-                        "/",
+                        "/org/freedesktop/DBus",
                         "org.freedesktop.DBus",
                         "Hello",
                         &m);
         if (r < 0)
                 return r;
 
-        return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
+        return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_cookie);
 }
 
 int bus_start_running(sd_bus *bus) {
@@ -393,7 +399,7 @@ int bus_start_running(sd_bus *bus) {
 }
 
 static int parse_address_key(const char **p, const char *key, char **value) {
-        size_t l, n = 0;
+        size_t l, n = 0, allocated = 0;
         const char *a;
         char *r = NULL;
 
@@ -417,7 +423,7 @@ static int parse_address_key(const char **p, const char *key, char **value) {
                 a = *p;
 
         while (*a != ';' && *a != ',' && *a != 0) {
-                char c, *t;
+                char c;
 
                 if (*a == '%') {
                         int x, y;
@@ -441,13 +447,9 @@ static int parse_address_key(const char **p, const char *key, char **value) {
                         a++;
                 }
 
-                t = realloc(r, n + 2);
-                if (!t) {
-                        free(r);
+                if (!GREEDY_REALLOC(r, allocated, n + 2))
                         return -ENOMEM;
-                }
 
-                r = t;
                 r[n++] = c;
         }
 
@@ -610,6 +612,7 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
         char *path = NULL;
         unsigned n_argv = 0, j;
         char **argv = NULL;
+        size_t allocated = 0;
         int r;
 
         assert(b);
@@ -643,17 +646,11 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
                         (*p) ++;
 
                         if (ul >= n_argv) {
-                                char **x;
-
-                                x = realloc(argv, sizeof(char*) * (ul + 2));
-                                if (!x) {
+                                if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
                                         r = -ENOMEM;
                                         goto fail;
                                 }
 
-                                memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
-
-                                argv = x;
                                 n_argv = ul + 1;
                         }
 
@@ -736,7 +733,7 @@ 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) {
+static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
         _cleanup_free_ char *machine = NULL;
         int r;
 
@@ -778,6 +775,49 @@ static int parse_container_address(sd_bus *b, const char **p, char **guid) {
         return 0;
 }
 
+static int parse_container_kernel_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;
+
+        if (!filename_is_safe(machine))
+                return -EINVAL;
+
+        free(b->machine);
+        b->machine = machine;
+        machine = NULL;
+
+        free(b->kernel);
+        b->kernel = strdup("/dev/kdbus/0-system/bus");
+        if (!b->kernel)
+                return -ENOMEM;
+
+        return 0;
+}
+
 static void bus_reset_parsed_address(sd_bus *b) {
         assert(b);
 
@@ -851,10 +891,18 @@ static int bus_parse_next_address(sd_bus *b) {
                                 return r;
 
                         break;
-                } else if (startswith(a, "x-container:")) {
+                } else if (startswith(a, "x-container-unix:")) {
+
+                        a += 17;
+                        r = parse_container_unix_address(b, &a, &guid);
+                        if (r < 0)
+                                return r;
+
+                        break;
+                } else if (startswith(a, "x-container-kernel:")) {
 
-                        a += 12;
-                        r = parse_container_address(b, &a, &guid);
+                        a += 19;
+                        r = parse_container_kernel_address(b, &a, &guid);
                         if (r < 0)
                                 return r;
 
@@ -882,36 +930,29 @@ static int bus_start_address(sd_bus *b) {
         assert(b);
 
         for (;;) {
-                sd_bus_close(b);
+                bool skipped = false;
 
-                if (b->exec_path) {
+                bus_close_fds(b);
 
+                if (b->exec_path)
                         r = bus_socket_exec(b);
-                        if (r >= 0)
-                                return r;
-
-                        b->last_connect_error = -r;
-                } else if (b->kernel) {
-
+                else if (b->machine && b->kernel)
+                        r = bus_container_connect_kernel(b);
+                else if (b->machine && b->sockaddr.sa.sa_family != AF_UNSPEC)
+                        r = bus_container_connect_socket(b);
+                else if (b->kernel)
                         r = bus_kernel_connect(b);
-                        if (r >= 0)
-                                return r;
-
-                        b->last_connect_error = -r;
-
-                } else if (b->machine) {
-
-                        r = bus_container_connect(b);
-                        if (r >= 0)
-                                return r;
-
-                        b->last_connect_error = -r;
-
-                } else if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
-
+                else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
                         r = bus_socket_connect(b);
-                        if (r >= 0)
-                                return r;
+                else
+                        skipped = true;
+
+                if (!skipped) {
+                        if (r >= 0) {
+                                r = attach_io_events(b);
+                                if (r >= 0)
+                                        return r;
+                        }
 
                         b->last_connect_error = -r;
                 }
@@ -1149,9 +1190,9 @@ _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
                 return -ENOMEM;
 
 #ifdef ENABLE_KDBUS
-        p = strjoin("kernel:path=/dev/kdbus/ns/machine-", e, "/0-system/bus;x-container:machine=", e, NULL);
+        p = strjoin("x-container-kernel:machine=", e, ";x-container-unix:machine=", e, NULL);
 #else
-        p = strjoin("x-container:machine=", e, NULL);
+        p = strjoin("x-container-unix:machine=", e, NULL);
 #endif
         if (!p)
                 return -ENOMEM;
@@ -1280,26 +1321,30 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
         assert(b);
         assert(m);
 
-        if (b->message_version != 0 &&
-            m->header->version != b->message_version)
-                return -EPERM;
-
-        if (b->message_endian != 0 &&
-            m->header->endian != b->message_endian)
-                return -EPERM;
-
         if (m->sealed) {
                 /* If we copy the same message to multiple
-                 * destinations, avoid using the same serial
+                 * destinations, avoid using the same cookie
                  * numbers. */
-                b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
+                b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m));
                 return 0;
         }
 
         if (timeout == 0)
                 timeout = BUS_DEFAULT_TIMEOUT;
 
-        return bus_message_seal(m, ++b->serial, timeout);
+        return bus_message_seal(m, ++b->cookie, timeout);
+}
+
+static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
+        assert(b);
+
+        /* Do packet version and endianess already match? */
+        if ((b->message_version == 0 || b->message_version == (*m)->header->version) &&
+            (b->message_endian == 0 || b->message_endian == (*m)->header->endian))
+                return 0;
+
+        /* No? Then remarshal! */
+        return bus_message_remarshal(b, m);
 }
 
 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
@@ -1317,14 +1362,33 @@ int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
         return bus_message_seal(m, 0xFFFFFFFFULL, 0);
 }
 
-static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
+static int bus_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
+        int r;
+
         assert(bus);
-        assert(message);
+        assert(m);
 
         if (bus->is_kernel)
-                return bus_kernel_write_message(bus, message);
+                r = bus_kernel_write_message(bus, m);
         else
-                return bus_socket_write_message(bus, message, idx);
+                r = bus_socket_write_message(bus, m, idx);
+
+        if (r <= 0)
+                return r;
+
+        if (bus->is_kernel || *idx >= BUS_MESSAGE_SIZE(m))
+                log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s",
+                          bus_message_type_to_string(m->header->type),
+                          strna(sd_bus_message_get_sender(m)),
+                          strna(sd_bus_message_get_destination(m)),
+                          strna(sd_bus_message_get_path(m)),
+                          strna(sd_bus_message_get_interface(m)),
+                          strna(sd_bus_message_get_member(m)),
+                          (unsigned long) BUS_MESSAGE_COOKIE(m),
+                          (unsigned long) m->reply_cookie,
+                          strna(m->error.message));
+
+        return r;
 }
 
 static int dispatch_wqueue(sd_bus *bus) {
@@ -1374,24 +1438,14 @@ static int bus_read_message(sd_bus *bus) {
 }
 
 int bus_rqueue_make_room(sd_bus *bus) {
-        sd_bus_message **q;
-        unsigned x;
-
-        x = bus->rqueue_size + 1;
-
-        if (bus->rqueue_allocated >= x)
-                return 0;
+        assert(bus);
 
-        if (x > BUS_RQUEUE_MAX)
+        if (bus->rqueue_size >= BUS_RQUEUE_MAX)
                 return -ENOBUFS;
 
-        q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
-        if (!q)
+        if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
                 return -ENOMEM;
 
-        bus->rqueue = q;
-        bus->rqueue_allocated = x;
-
         return 0;
 }
 
@@ -1423,7 +1477,8 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
         }
 }
 
-_public_ 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 *cookie) {
+        _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
         int r;
 
         assert_return(bus, -EINVAL);
@@ -1439,18 +1494,24 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
                         return -ENOTSUP;
         }
 
-        /* If the serial number isn't kept, then we know that no reply
+        /* If the cookie number isn't kept, then we know that no reply
          * is expected */
-        if (!serial && !m->sealed)
+        if (!cookie && !m->sealed)
                 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
 
         r = bus_seal_message(bus, m, 0);
         if (r < 0)
                 return r;
 
+        /* Remarshall if we have to. This will possible unref the
+         * message and place a replacement in m */
+        r = bus_remarshal_message(bus, &m);
+        if (r < 0)
+                return r;
+
         /* If this is a reply and no reply was requested, then let's
          * suppress this, if we can */
-        if (m->dont_send && !serial)
+        if (m->dont_send && !cookie)
                 return 1;
 
         if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
@@ -1473,28 +1534,24 @@ _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
                         bus->windex = idx;
                 }
         } else {
-                sd_bus_message **q;
-
                 /* Just append it to the queue. */
 
                 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
                         return -ENOBUFS;
 
-                q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
-                if (!q)
+                if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
                         return -ENOMEM;
 
-                bus->wqueue = q;
-                q[bus->wqueue_size ++] = sd_bus_message_ref(m);
+                bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m);
         }
 
-        if (serial)
-                *serial = BUS_MESSAGE_SERIAL(m);
+        if (cookie)
+                *cookie = BUS_MESSAGE_COOKIE(m);
 
         return 1;
 }
 
-_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
+_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
         int r;
 
         assert_return(bus, -EINVAL);
@@ -1512,7 +1569,7 @@ _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destinat
                         return r;
         }
 
-        return sd_bus_send(bus, m, serial);
+        return sd_bus_send(bus, m, cookie);
 }
 
 static usec_t calc_elapse(uint64_t usec) {
@@ -1542,12 +1599,13 @@ static int timeout_compare(const void *a, const void *b) {
 
 _public_ int sd_bus_call_async(
                 sd_bus *bus,
-                sd_bus_message *m,
+                sd_bus_message *_m,
                 sd_bus_message_handler_t callback,
                 void *userdata,
                 uint64_t usec,
-                uint64_t *serial) {
+                uint64_t *cookie) {
 
+        _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
         struct reply_callback *c;
         int r;
 
@@ -1571,16 +1629,20 @@ _public_ int sd_bus_call_async(
         if (r < 0)
                 return r;
 
+        r = bus_remarshal_message(bus, &m);
+        if (r < 0)
+                return r;
+
         c = new0(struct reply_callback, 1);
         if (!c)
                 return -ENOMEM;
 
         c->callback = callback;
         c->userdata = userdata;
-        c->serial = BUS_MESSAGE_SERIAL(m);
+        c->cookie = BUS_MESSAGE_COOKIE(m);
         c->timeout = calc_elapse(m->timeout);
 
-        r = hashmap_put(bus->reply_callbacks, &c->serial, c);
+        r = hashmap_put(bus->reply_callbacks, &c->cookie, c);
         if (r < 0) {
                 free(c);
                 return r;
@@ -1590,28 +1652,28 @@ _public_ int sd_bus_call_async(
                 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
                 if (r < 0) {
                         c->timeout = 0;
-                        sd_bus_call_async_cancel(bus, c->serial);
+                        sd_bus_call_async_cancel(bus, c->cookie);
                         return r;
                 }
         }
 
-        r = sd_bus_send(bus, m, serial);
+        r = sd_bus_send(bus, m, cookie);
         if (r < 0) {
-                sd_bus_call_async_cancel(bus, c->serial);
+                sd_bus_call_async_cancel(bus, c->cookie);
                 return r;
         }
 
         return r;
 }
 
-_public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
+_public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t cookie) {
         struct reply_callback *c;
 
         assert_return(bus, -EINVAL);
-        assert_return(serial != 0, -EINVAL);
+        assert_return(cookie != 0, -EINVAL);
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
-        c = hashmap_remove(bus->reply_callbacks, &serial);
+        c = hashmap_remove(bus->reply_callbacks, &cookie);
         if (!c)
                 return 0;
 
@@ -1649,13 +1711,14 @@ int bus_ensure_running(sd_bus *bus) {
 
 _public_ int sd_bus_call(
                 sd_bus *bus,
-                sd_bus_message *m,
+                sd_bus_message *_m,
                 uint64_t usec,
                 sd_bus_error *error,
                 sd_bus_message **reply) {
 
+        _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
         usec_t timeout;
-        uint64_t serial;
+        uint64_t cookie;
         unsigned i;
         int r;
 
@@ -1677,7 +1740,11 @@ _public_ int sd_bus_call(
         if (r < 0)
                 return r;
 
-        r = sd_bus_send(bus, m, &serial);
+        r = bus_remarshal_message(bus, &m);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_send(bus, m, &cookie);
         if (r < 0)
                 return r;
 
@@ -1691,7 +1758,7 @@ _public_ int sd_bus_call(
 
                         incoming = bus->rqueue[i];
 
-                        if (incoming->reply_serial == serial) {
+                        if (incoming->reply_cookie == cookie) {
                                 /* Found a match! */
 
                                 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
@@ -1713,7 +1780,7 @@ _public_ int sd_bus_call(
                                 sd_bus_message_unref(incoming);
                                 return r;
 
-                        } else if (incoming->header->serial == serial &&
+                        } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
                                    bus->unique_name &&
                                    incoming->sender &&
                                    streq(bus->unique_name, incoming->sender)) {
@@ -1863,7 +1930,7 @@ static int process_timeout(sd_bus *bus) {
 
         r = bus_message_new_synthetic_error(
                         bus,
-                        c->serial,
+                        c->cookie,
                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
                         &m);
         if (r < 0)
@@ -1876,7 +1943,7 @@ static int process_timeout(sd_bus *bus) {
                 return r;
 
         assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
-        hashmap_remove(bus->reply_callbacks, &c->serial);
+        hashmap_remove(bus->reply_callbacks, &c->cookie);
 
         bus->current = m;
         bus->iteration_counter ++;
@@ -1906,7 +1973,7 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) {
             m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
                 return -EIO;
 
-        if (m->reply_serial != bus->hello_serial)
+        if (m->reply_cookie != bus->hello_cookie)
                 return -EIO;
 
         return 0;
@@ -1924,7 +1991,7 @@ static int process_reply(sd_bus *bus, sd_bus_message *m) {
             m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
                 return 0;
 
-        c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
+        c = hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
         if (!c)
                 return 0;
 
@@ -2005,6 +2072,9 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
+        if (bus->manual_peer_interface)
+                return 0;
+
         if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
                 return 0;
 
@@ -2055,11 +2125,16 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
         bus->current = m;
         bus->iteration_counter++;
 
-        log_debug("Got message sender=%s object=%s interface=%s member=%s",
+        log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s",
+                  bus_message_type_to_string(m->header->type),
                   strna(sd_bus_message_get_sender(m)),
+                  strna(sd_bus_message_get_destination(m)),
                   strna(sd_bus_message_get_path(m)),
                   strna(sd_bus_message_get_interface(m)),
-                  strna(sd_bus_message_get_member(m)));
+                  strna(sd_bus_message_get_member(m)),
+                  (unsigned long) BUS_MESSAGE_COOKIE(m),
+                  (unsigned long) m->reply_cookie,
+                  strna(m->error.message));
 
         r = process_hello(bus, m);
         if (r != 0)
@@ -2125,6 +2200,12 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) {
 
         if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
 
+                log_debug("Unprocessed message call 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 = sd_bus_reply_method_errorf(
                                 m,
                                 SD_BUS_ERROR_UNKNOWN_OBJECT,
@@ -2157,7 +2238,7 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) {
                 /* First, fail all outstanding method calls */
                 r = bus_message_new_synthetic_error(
                                 bus,
-                                c->serial,
+                                c->cookie,
                                 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
                                 &m);
                 if (r < 0)
@@ -2170,7 +2251,7 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) {
                 if (c->timeout != 0)
                         prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
 
-                hashmap_remove(bus->reply_callbacks, &c->serial);
+                hashmap_remove(bus->reply_callbacks, &c->cookie);
 
                 bus->current = m;
                 bus->iteration_counter++;
@@ -2241,9 +2322,11 @@ _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
         switch (bus->state) {
 
         case BUS_UNSET:
-        case BUS_CLOSED:
                 return -ENOTCONN;
 
+        case BUS_CLOSED:
+                return -ECONNRESET;
+
         case BUS_OPENING:
                 r = bus_socket_process_opening(bus);
                 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
@@ -2598,6 +2681,66 @@ static int quit_callback(sd_event_source *event, void *userdata) {
         return 1;
 }
 
+static int attach_io_events(sd_bus *bus) {
+        int r;
+
+        assert(bus);
+
+        if (bus->input_fd < 0)
+                return 0;
+
+        if (!bus->event)
+                return 0;
+
+        if (!bus->input_io_event_source) {
+                r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
+                if (r < 0)
+                        return r;
+
+                r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
+                if (r < 0)
+                        return r;
+
+                r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
+        } else
+                r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
+
+        if (r < 0)
+                return r;
+
+        if (bus->output_fd != bus->input_fd) {
+                assert(bus->output_fd >= 0);
+
+                if (!bus->output_io_event_source) {
+                        r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
+                        if (r < 0)
+                                return r;
+
+                        r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
+                } else
+                        r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
+
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
+static void detach_io_events(sd_bus *bus) {
+        assert(bus);
+
+        if (bus->input_io_event_source) {
+                sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
+                bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
+        }
+
+        if (bus->output_io_event_source) {
+                sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
+                bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
+        }
+}
+
 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
         int r;
 
@@ -2616,37 +2759,21 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
                         return r;
         }
 
-        r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
-        if (r < 0)
-                goto fail;
-
-        r = sd_event_source_set_priority(bus->input_io_event_source, priority);
-        if (r < 0)
-                goto fail;
-
-        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;
-
-                r = sd_event_source_set_priority(bus->output_io_event_source, priority);
-                if (r < 0)
-                        goto fail;
-        }
+        bus->event_priority = priority;
 
-        r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
+        r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
         if (r < 0)
                 goto fail;
 
-        r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
+        r = sd_event_source_set_priority(bus->time_event_source, priority);
         if (r < 0)
                 goto fail;
 
-        r = sd_event_source_set_priority(bus->time_event_source, priority);
+        r = sd_event_add_exit(bus->event, quit_callback, bus, &bus->quit_event_source);
         if (r < 0)
                 goto fail;
 
-        r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
+        r = attach_io_events(bus);
         if (r < 0)
                 goto fail;
 
@@ -2663,15 +2790,7 @@ _public_ int sd_bus_detach_event(sd_bus *bus) {
         if (!bus->event)
                 return 0;
 
-        if (bus->input_io_event_source) {
-                sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
-                bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
-        }
-
-        if (bus->output_io_event_source) {
-                sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
-                bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
-        }
+        detach_io_events(bus);
 
         if (bus->time_event_source) {
                 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
@@ -2729,13 +2848,13 @@ static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus
 }
 
 _public_ int sd_bus_default_system(sd_bus **ret) {
-        static __thread sd_bus *default_system_bus = NULL;
+        static thread_local sd_bus *default_system_bus = NULL;
 
         return bus_default(sd_bus_open_system, &default_system_bus, ret);
 }
 
 _public_ int sd_bus_default_user(sd_bus **ret) {
-        static __thread sd_bus *default_user_bus = NULL;
+        static thread_local sd_bus *default_user_bus = NULL;
 
         return bus_default(sd_bus_open_user, &default_user_bus, ret);
 }
@@ -2872,3 +2991,25 @@ _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **re
         *ret = c;
         return 0;
 }
+
+_public_ int sd_bus_try_close(sd_bus *bus) {
+        int r;
+
+        assert_return(bus, -EINVAL);
+        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+        assert_return(bus->is_kernel, -ENOTSUP);
+
+        if (bus->rqueue_size > 0)
+                return -EBUSY;
+
+        if (bus->wqueue_size > 0)
+                return -EBUSY;
+
+        r = bus_kernel_try_close(bus);
+        if (r < 0)
+                return r;
+
+        sd_bus_close(bus);
+        return 0;
+}