[systemd-commits] 5 commits - Makefile.am TODO src/libsystemd-bus src/shared src/stdio-bridge src/systemd

Lennart Poettering lennart at kemper.freedesktop.org
Thu May 16 19:26:41 PDT 2013


 Makefile.am                          |    4 
 TODO                                 |    7 
 src/libsystemd-bus/bus-control.c     |   30 +++
 src/libsystemd-bus/bus-internal.h    |   22 ++
 src/libsystemd-bus/bus-kernel.c      |   49 +++---
 src/libsystemd-bus/bus-kernel.h      |    4 
 src/libsystemd-bus/bus-message.c     |    6 
 src/libsystemd-bus/bus-socket.c      |   12 -
 src/libsystemd-bus/sd-bus.c          |  264 ++++++++++++++++++++++++++++-------
 src/libsystemd-bus/test-bus-kernel.c |   16 ++
 src/libsystemd-bus/test-bus-server.c |    4 
 src/shared/macro.h                   |    3 
 src/stdio-bridge/stdio-bridge.c      |    4 
 src/systemd/sd-bus.h                 |    9 +
 14 files changed, 349 insertions(+), 85 deletions(-)

New commits:
commit 264ad849a4a0acf1ca392da62b7018d4fe7b66b3
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri May 17 04:25:56 2013 +0200

    bus: add APIs for negotiating what is attached to messages

diff --git a/TODO b/TODO
index 390106a..fb5f0c3 100644
--- a/TODO
+++ b/TODO
@@ -35,14 +35,12 @@ Features:
 * libsystemd-bus:
   - default policy (allow uid == 0 and our own uid)
   - enforce alignment of pointers passed in
-  - negotiation for attach attributes
   - when kdbus doesn't take our message without memfds, try again with memfds
   - kdbus: generate correct bloom filter for matches
   - implement translator service
   - port systemd to new library
   - implement busname unit type in systemd
   - move to gvariant
-  - keep the connection fds around as long as the bus is open
   - merge busctl into systemctl or so?
   - synthesize sd_bus_message objects from kernel messages
 
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 8f87bea..e775b09 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -99,7 +99,6 @@ struct sd_bus {
         int message_version;
 
         bool is_kernel:1;
-        bool negotiate_fds:1;
         bool can_fds:1;
         bool bus_client:1;
         bool ucred_valid:1;
@@ -181,6 +180,8 @@ struct sd_bus {
         unsigned n_memfd_cache;
 
         pid_t original_pid;
+
+        uint64_t hello_flags;
 };
 
 static inline void bus_unrefp(sd_bus **b) {
diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 699d241..107b2bd 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -345,15 +345,7 @@ int bus_kernel_take_fd(sd_bus *b) {
         }
 
         hello->size = sizeof(h);
-        hello->conn_flags =
-                KDBUS_HELLO_ACCEPT_FD|
-                KDBUS_HELLO_ATTACH_COMM|
-                KDBUS_HELLO_ATTACH_EXE|
-                KDBUS_HELLO_ATTACH_CMDLINE|
-                KDBUS_HELLO_ATTACH_CGROUP|
-                KDBUS_HELLO_ATTACH_CAPS|
-                KDBUS_HELLO_ATTACH_SECLABEL|
-                KDBUS_HELLO_ATTACH_AUDIT;
+        hello->conn_flags = b->hello_flags;
 
         hello->items[0].type = KDBUS_HELLO_POOL;
         hello->items[0].size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec);
@@ -378,7 +370,7 @@ int bus_kernel_take_fd(sd_bus *b) {
 
         b->is_kernel = true;
         b->bus_client = true;
-        b->can_fds = true;
+        b->can_fds = !!(hello->conn_flags & KDBUS_HELLO_ACCEPT_FD);
 
         r = bus_start_running(b);
         if (r < 0)
diff --git a/src/libsystemd-bus/bus-socket.c b/src/libsystemd-bus/bus-socket.c
index befded7..b60facb 100644
--- a/src/libsystemd-bus/bus-socket.c
+++ b/src/libsystemd-bus/bus-socket.c
@@ -181,7 +181,7 @@ static int bus_socket_auth_verify_client(sd_bus *b) {
         if (!e)
                 return 0;
 
-        if (b->negotiate_fds) {
+        if (b->hello_flags & KDBUS_HELLO_ACCEPT_FD) {
                 f = memmem(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2);
                 if (!f)
                         return 0;
@@ -464,7 +464,7 @@ static int bus_socket_auth_verify_server(sd_bus *b) {
                                         r = bus_socket_auth_write_ok(b);
                         }
                 } else if (line_equals(line, l, "NEGOTIATE_UNIX_FD")) {
-                        if (b->auth == _BUS_AUTH_INVALID || !b->negotiate_fds)
+                        if (b->auth == _BUS_AUTH_INVALID || !(b->hello_flags & KDBUS_HELLO_ACCEPT_FD))
                                 r = bus_socket_auth_write(b, "ERROR\r\n");
                         else {
                                 b->can_fds = true;
@@ -610,6 +610,8 @@ static int bus_socket_setup(sd_bus *b) {
          * socket, just in case. */
         enable = !b->bus_client;
         setsockopt(b->input_fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable));
+
+        enable = !b->bus_client && (b->hello_flags & KDBUS_HELLO_ATTACH_SECLABEL);
         setsockopt(b->input_fd, SOL_SOCKET, SO_PASSSEC, &enable, sizeof(enable));
 
         /* Increase the buffers to a MB */
@@ -651,7 +653,7 @@ static int bus_socket_start_auth_client(sd_bus *b) {
         if (!b->auth_buffer)
                 return -ENOMEM;
 
-        if (b->negotiate_fds)
+        if (b->hello_flags & KDBUS_HELLO_ACCEPT_FD)
                 auth_suffix = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
         else
                 auth_suffix = "\r\nBEGIN\r\n";
@@ -673,11 +675,11 @@ static int bus_socket_start_auth(sd_bus *b) {
         b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_DEFAULT_TIMEOUT;
 
         if (sd_is_socket(b->input_fd, AF_UNIX, 0, 0) <= 0)
-                b->negotiate_fds = false;
+                b->hello_flags &= ~KDBUS_HELLO_ACCEPT_FD;
 
         if (b->output_fd != b->input_fd)
                 if (sd_is_socket(b->output_fd, AF_UNIX, 0, 0) <= 0)
-                        b->negotiate_fds = false;
+                        b->hello_flags &= ~KDBUS_HELLO_ACCEPT_FD;
 
         if (b->is_server)
                 return bus_socket_read_auth(b);
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 5e66a31..6033aa7 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -125,7 +125,7 @@ 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);
@@ -226,7 +226,7 @@ int sd_bus_set_bus_client(sd_bus *bus, int b) {
         return 0;
 }
 
-int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
+int sd_bus_negotiate_fds(sd_bus *bus, int b) {
         if (!bus)
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
@@ -234,7 +234,91 @@ int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
         if (bus_pid_changed(bus))
                 return -ECHILD;
 
-        bus->negotiate_fds = !!b;
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
+        return 0;
+}
+
+int sd_bus_negotiate_attach_comm(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_COMM, b);
+        return 0;
+}
+
+int sd_bus_negotiate_attach_exe(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_EXE, b);
+        return 0;
+}
+
+int sd_bus_negotiate_attach_cmdline(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CMDLINE, b);
+        return 0;
+}
+
+int sd_bus_negotiate_attach_cgroup(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CGROUP, b);
+        return 0;
+}
+
+int sd_bus_negotiate_attach_caps(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CAPS, b);
+        return 0;
+}
+
+int sd_bus_negotiate_attach_selinux_context(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_SECLABEL, b);
+        return 0;
+}
+
+int sd_bus_negotiate_attach_audit(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
+        SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_AUDIT, b);
         return 0;
 }
 
@@ -1015,7 +1099,7 @@ int sd_bus_can_send(sd_bus *bus, char type) {
                 return -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);
diff --git a/src/libsystemd-bus/test-bus-kernel.c b/src/libsystemd-bus/test-bus-kernel.c
index 72514a3..b8e89e4 100644
--- a/src/libsystemd-bus/test-bus-kernel.c
+++ b/src/libsystemd-bus/test-bus-kernel.c
@@ -60,6 +60,22 @@ int main(int argc, char *argv[]) {
         r = sd_bus_set_address(b, address);
         assert_se(r >= 0);
 
+        assert_se(sd_bus_negotiate_attach_comm(a, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_exe(a, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_cmdline(a, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_cgroup(a, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_caps(a, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_selinux_context(a, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_audit(a, 1) >= 0);
+
+        assert_se(sd_bus_negotiate_attach_comm(b, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_exe(b, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_cmdline(b, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_cgroup(b, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_caps(b, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_selinux_context(b, 1) >= 0);
+        assert_se(sd_bus_negotiate_attach_audit(b, 1) >= 0);
+
         r = sd_bus_start(a);
         assert_se(r >= 0);
 
diff --git a/src/libsystemd-bus/test-bus-server.c b/src/libsystemd-bus/test-bus-server.c
index a977262..ef26a65 100644
--- a/src/libsystemd-bus/test-bus-server.c
+++ b/src/libsystemd-bus/test-bus-server.c
@@ -55,8 +55,8 @@ static void *server(void *p) {
         assert_se(sd_bus_new(&bus) >= 0);
         assert_se(sd_bus_set_fd(bus, c->fds[0], c->fds[0]) >= 0);
         assert_se(sd_bus_set_server(bus, 1, id) >= 0);
-        assert_se(sd_bus_set_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
         assert_se(sd_bus_set_anonymous(bus, c->server_anonymous_auth) >= 0);
+        assert_se(sd_bus_negotiate_fds(bus, c->server_negotiate_unix_fds) >= 0);
         assert_se(sd_bus_start(bus) >= 0);
 
         while (!quit) {
@@ -134,7 +134,7 @@ static int client(struct context *c) {
 
         assert_se(sd_bus_new(&bus) >= 0);
         assert_se(sd_bus_set_fd(bus, c->fds[1], c->fds[1]) >= 0);
-        assert_se(sd_bus_set_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
+        assert_se(sd_bus_negotiate_fds(bus, c->client_negotiate_unix_fds) >= 0);
         assert_se(sd_bus_set_anonymous(bus, c->client_anonymous_auth) >= 0);
         assert_se(sd_bus_start(bus) >= 0);
 
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 2f151bc..bfe03f2 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -284,4 +284,7 @@ do {                                                                    \
             sizeof(type) <= 4 ? 10 :                                    \
             sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
 
+#define SET_FLAG(v, flag, b) \
+        (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
+
 #include "log.h"
diff --git a/src/stdio-bridge/stdio-bridge.c b/src/stdio-bridge/stdio-bridge.c
index a5bdb03..ab1a43a 100644
--- a/src/stdio-bridge/stdio-bridge.c
+++ b/src/stdio-bridge/stdio-bridge.c
@@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        r = sd_bus_set_negotiate_fds(a, is_unix);
+        r = sd_bus_negotiate_fds(a, is_unix);
         if (r < 0) {
                 log_error("Failed to set FD negotiation: %s", strerror(-r));
                 goto finish;
@@ -104,7 +104,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        r = sd_bus_set_negotiate_fds(b, is_unix);
+        r = sd_bus_negotiate_fds(b, is_unix);
         if (r < 0) {
                 log_error("Failed to set FD negotiation: %s", strerror(-r));
                 goto finish;
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index 85deacf..eeca69d 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -64,7 +64,14 @@ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]);
 int sd_bus_set_bus_client(sd_bus *bus, int b);
 int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id);
 int sd_bus_set_anonymous(sd_bus *bus, int b);
-int sd_bus_set_negotiate_fds(sd_bus *bus, int b);
+int sd_bus_negotiate_fds(sd_bus *bus, int b);
+int sd_bus_negotiate_attach_comm(sd_bus *bus, int b);
+int sd_bus_negotiate_attach_exe(sd_bus *bus, int b);
+int sd_bus_negotiate_attach_cmdline(sd_bus *bus, int b);
+int sd_bus_negotiate_attach_cgroup(sd_bus *bus, int b);
+int sd_bus_negotiate_attach_caps(sd_bus *bus, int b);
+int sd_bus_negotiate_attach_selinux_context(sd_bus *bus, int b);
+int sd_bus_negotiate_attach_audit(sd_bus *bus, int b);
 int sd_bus_start(sd_bus *ret);
 
 void sd_bus_close(sd_bus *bus);

commit 45fbe937d7ca8d0da9ea276d57bc70ebd41c285e
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri May 17 03:13:58 2013 +0200

    bus: add minimal locking around the memfd cache
    
    We want to allow clients to process an sd_bus_message on a different
    thread than it was received on. Since unreffing a bus message might
    readd some of its memfds to the memfd cache add some minimal locking
    around the cache.

diff --git a/Makefile.am b/Makefile.am
index 5265982..66f12e9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1749,6 +1749,10 @@ libsystemd_bus_la_LIBADD =  \
 	libsystemd-shared.la \
 	libsystemd-daemon.la
 
+libsystemd_bus_la_CFLAGS = \
+	$(AM_CFLAGS) \
+	-pthread
+
 noinst_LTLIBRARIES += \
 	libsystemd-bus.la
 
diff --git a/TODO b/TODO
index f609b4b..390106a 100644
--- a/TODO
+++ b/TODO
@@ -42,7 +42,6 @@ Features:
   - port systemd to new library
   - implement busname unit type in systemd
   - move to gvariant
-  - minimal locking around the memfd cache
   - keep the connection fds around as long as the bus is open
   - merge busctl into systemctl or so?
   - synthesize sd_bus_message objects from kernel messages
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index b6975c5..8f87bea 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -24,6 +24,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/in.h>
+#include <pthread.h>
 
 #include "hashmap.h"
 #include "prioq.h"
@@ -169,6 +170,13 @@ struct sd_bus {
 
         void *kdbus_buffer;
 
+        /* We do locking around the memfd cache, since we want to
+         * allow people to process a sd_bus_message in a different
+         * thread then it was generated on and free it there. Since
+         * adding something to the memfd cache might happen when a
+         * message is released, we hence need to protect this bit with
+         * a mutex. */
+        pthread_mutex_t memfd_cache_mutex;
         struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
         unsigned n_memfd_cache;
 
diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index ede78d7..699d241 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -721,6 +721,7 @@ int bus_kernel_create(const char *name, char **s) {
 
 int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *size) {
         struct memfd_cache *c;
+        int fd;
 
         assert(address);
         assert(size);
@@ -728,8 +729,12 @@ int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *size) {
         if (!bus || !bus->is_kernel)
                 return -ENOTSUP;
 
+        assert_se(pthread_mutex_lock(&bus->memfd_cache_mutex) == 0);
+
         if (bus->n_memfd_cache <= 0) {
-                int fd, r;
+                int r;
+
+                assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) == 0);
 
                 r = ioctl(bus->input_fd, KDBUS_CMD_MEMFD_NEW, &fd);
                 if (r < 0)
@@ -747,8 +752,18 @@ int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *size) {
 
         *address = c->address;
         *size = c->size;
+        fd = c->fd;
+
+        assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) == 0);
 
-        return c->fd;
+        return fd;
+}
+
+static void close_and_munmap(int fd, void *address, size_t size) {
+        if (size > 0)
+                assert_se(munmap(address, PAGE_ALIGN(size)) == 0);
+
+        close_nointr_nofail(fd);
 }
 
 void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t size) {
@@ -757,13 +772,17 @@ void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t size) {
         assert(fd >= 0);
         assert(size == 0 || address);
 
-        if (!bus || !bus->is_kernel ||
-            bus->n_memfd_cache >= ELEMENTSOF(bus->memfd_cache)) {
+        if (!bus || !bus->is_kernel) {
+                close_and_munmap(fd, address, size);
+                return;
+        }
+
+        assert_se(pthread_mutex_lock(&bus->memfd_cache_mutex) == 0);
 
-                if (size > 0)
-                        assert_se(munmap(address, PAGE_ALIGN(size)) == 0);
+        if (bus->n_memfd_cache >= ELEMENTSOF(bus->memfd_cache)) {
+                assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) == 0);
 
-                close_nointr_nofail(fd);
+                close_and_munmap(fd, address, size);
                 return;
         }
 
@@ -780,6 +799,8 @@ void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t size) {
                 c->size = MEMFD_CACHE_ITEM_SIZE_MAX;
         } else
                 c->size = size;
+
+        assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) == 0);
 }
 
 void bus_kernel_flush_memfd(sd_bus *b) {
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index fd19ff3..5e66a31 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -27,6 +27,7 @@
 #include <sys/poll.h>
 #include <byteswap.h>
 #include <sys/mman.h>
+#include <pthread.h>
 
 #include "util.h"
 #include "macro.h"
@@ -106,6 +107,8 @@ static void bus_free(sd_bus *b) {
 
         bus_kernel_flush_memfd(b);
 
+        assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
+
         free(b);
 }
 
@@ -125,6 +128,8 @@ int sd_bus_new(sd_bus **ret) {
         r->negotiate_fds = true;
         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 */
         r->wqueue = new(sd_bus_message*, 1);

commit d5a2b9a6f455468a0f29483303657ab4fd7013d8
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri May 17 02:50:00 2013 +0200

    bus: return ECHILD as soon as people try to reuse a bus connection across a fork()

diff --git a/TODO b/TODO
index 6ca98a3..f609b4b 100644
--- a/TODO
+++ b/TODO
@@ -29,11 +29,13 @@ Fedora 19:
 
 Features:
 
+* libsystemd-journal:
+  - return ECHILD as soon as somebody tries to reuse a journal object across a fork()
+
 * libsystemd-bus:
   - default policy (allow uid == 0 and our own uid)
   - enforce alignment of pointers passed in
   - negotiation for attach attributes
-  - verify that the PID doesn't change for existing busses
   - when kdbus doesn't take our message without memfds, try again with memfds
   - kdbus: generate correct bloom filter for matches
   - implement translator service
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index a4dc9bf..177bd88 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -40,6 +40,8 @@ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
                 return -EINVAL;
         if (!unique)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -60,6 +62,10 @@ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
                 return -EINVAL;
         if (!bus->bus_client)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->is_kernel) {
                 struct kdbus_cmd_name *n;
@@ -114,6 +120,10 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
                 return -EINVAL;
         if (!bus->bus_client)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->is_kernel) {
                 struct kdbus_cmd_name *n;
@@ -163,6 +173,10 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
                 return -EINVAL;
         if (!l)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_call_method(
                         bus,
@@ -213,6 +227,10 @@ int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
                 return -EINVAL;
         if (!name)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_call_method(
                         bus,
@@ -255,6 +273,10 @@ int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
                 return -EINVAL;
         if (!uid)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_call_method(
                         bus,
@@ -288,6 +310,10 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
                 return -EINVAL;
         if (!pid)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_call_method(
                         bus,
@@ -354,6 +380,10 @@ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machi
                 return -EINVAL;
         if (!name)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (streq_ptr(name, bus->unique_name))
                 return sd_id128_get_machine(machine);
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 5ba32ad..b6975c5 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -171,6 +171,8 @@ struct sd_bus {
 
         struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
         unsigned n_memfd_cache;
+
+        pid_t original_pid;
 };
 
 static inline void bus_unrefp(sd_bus **b) {
@@ -217,3 +219,5 @@ const char *bus_message_type_to_string(uint8_t u);
 int bus_ensure_running(sd_bus *bus);
 int bus_start_running(sd_bus *bus);
 int bus_next_address(sd_bus *bus);
+
+bool bus_pid_changed(sd_bus *bus);
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index b0730d4..fd19ff3 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -123,6 +123,7 @@ int sd_bus_new(sd_bus **ret) {
         r->input_fd = r->output_fd = -1;
         r->message_version = 1;
         r->negotiate_fds = true;
+        r->original_pid = getpid();
 
         /* We guarantee that wqueue always has space for at least one
          * entry */
@@ -145,6 +146,8 @@ int sd_bus_set_address(sd_bus *bus, const char *address) {
                 return -EPERM;
         if (!address)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         a = strdup(address);
         if (!a)
@@ -165,6 +168,8 @@ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
                 return -EINVAL;
         if (output_fd < 0)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->input_fd = input_fd;
         bus->output_fd = output_fd;
@@ -182,6 +187,8 @@ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
                 return -EINVAL;
         if (strv_isempty(argv))
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         p = strdup(path);
         if (!p)
@@ -207,6 +214,8 @@ int sd_bus_set_bus_client(sd_bus *bus, int b) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->bus_client = !!b;
         return 0;
@@ -217,6 +226,8 @@ int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->negotiate_fds = !!b;
         return 0;
@@ -229,6 +240,8 @@ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->is_server = !!b;
         bus->server_id = server_id;
@@ -240,6 +253,8 @@ int sd_bus_set_anonymous(sd_bus *bus, int b) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->anonymous_auth = !!b;
         return 0;
@@ -828,6 +843,8 @@ int sd_bus_start(sd_bus *bus) {
                 return -EINVAL;
         if (bus->state != BUS_UNSET)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         bus->state = BUS_OPENING;
 
@@ -937,8 +954,9 @@ fail:
 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;
@@ -975,6 +993,8 @@ sd_bus *sd_bus_unref(sd_bus *bus) {
 int sd_bus_is_open(sd_bus *bus) {
         if (!bus)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         return BUS_IS_OPEN(bus->state);
 }
@@ -986,6 +1006,8 @@ int sd_bus_can_send(sd_bus *bus, char type) {
                 return -EINVAL;
         if (bus->state == BUS_UNSET)
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (type == SD_BUS_TYPE_UNIX_FD) {
                 if (!bus->negotiate_fds)
@@ -1008,6 +1030,8 @@ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
                 return -EINVAL;
         if (!server_id)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -1118,6 +1142,8 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (m->n_fds > 0) {
                 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
@@ -1235,6 +1261,8 @@ int sd_bus_send_with_reply(
                 return -EINVAL;
         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
         if (r < 0)
@@ -1290,6 +1318,8 @@ int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
                 return -EINVAL;
         if (serial == 0)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         c = hashmap_remove(bus->reply_callbacks, &serial);
         if (!c)
@@ -1351,6 +1381,8 @@ int sd_bus_send_with_reply_and_block(
                 return -EINVAL;
         if (bus_error_is_dirty(error))
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -1461,6 +1493,8 @@ int sd_bus_get_fd(sd_bus *bus) {
                 return -ENOTCONN;
         if (bus->input_fd != bus->output_fd)
                 return -EPERM;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         return bus->input_fd;
 }
@@ -1472,6 +1506,8 @@ int sd_bus_get_events(sd_bus *bus) {
                 return -EINVAL;
         if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->state == BUS_OPENING)
                 flags |= POLLOUT;
@@ -1501,6 +1537,8 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
                 return -EINVAL;
         if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->state == BUS_AUTHENTICATING) {
                 *timeout_usec = bus->auth_timeout;
@@ -1996,6 +2034,8 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
 
         if (!bus)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         /* We don't allow recursively invoking sd_bus_process(). */
         if (bus->processing)
@@ -2093,6 +2133,9 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
                 return -EINVAL;
         if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
+
         if (bus->rqueue_size > 0)
                 return 0;
 
@@ -2106,6 +2149,8 @@ int sd_bus_flush(sd_bus *bus) {
                 return -EINVAL;
         if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = bus_ensure_running(bus);
         if (r < 0)
@@ -2135,6 +2180,8 @@ int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *user
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         f = new0(struct filter_callback, 1);
         if (!f)
@@ -2154,6 +2201,8 @@ int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *u
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
                 if (f->callback == callback && f->userdata == userdata) {
@@ -2183,6 +2232,8 @@ static int bus_add_object(
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func);
         if (r < 0)
@@ -2228,6 +2279,8 @@ static int bus_remove_object(
                 return -EINVAL;
         if (!callback)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         c = hashmap_get(bus->object_callbacks, path);
         if (!c)
@@ -2268,6 +2321,8 @@ int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t ca
                 return -EINVAL;
         if (!match)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->bus_client) {
                 r = bus_add_match_internal(bus, match);
@@ -2295,6 +2350,8 @@ int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t
                 return -EINVAL;
         if (!match)
                 return -EINVAL;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (bus->bus_client)
                 r = bus_remove_match_internal(bus, match);
@@ -2322,6 +2379,10 @@ int sd_bus_emit_signal(
 
         if (!bus)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_message_new_signal(bus, path, interface, member, &m);
         if (r < 0)
@@ -2352,6 +2413,10 @@ int sd_bus_call_method(
 
         if (!bus)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
         if (r < 0)
@@ -2383,6 +2448,10 @@ int sd_bus_reply_method_return(
                 return -EPERM;
         if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
                 return 0;
@@ -2418,6 +2487,10 @@ int sd_bus_reply_method_error(
                 return -EINVAL;
         if (!sd_bus_error_is_set(e))
                 return -EINVAL;
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+        if (bus_pid_changed(bus))
+                return -ECHILD;
 
         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
                 return 0;
@@ -2428,3 +2501,12 @@ int sd_bus_reply_method_error(
 
         return sd_bus_send(bus, m, NULL);
 }
+
+bool bus_pid_changed(sd_bus *bus) {
+        assert(bus);
+
+        /* We don't support people creating a bus connection and
+         * keeping it around over a fork(). Let's complain. */
+
+        return bus->original_pid != getpid();
+}

commit 63edf05ed9c1d4cb5cf9364e734b2a96f84622d0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri May 17 02:32:32 2013 +0200

    bus: actually unmap kdbus pool after use

diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
index 8ef5752..ede78d7 100644
--- a/src/libsystemd-bus/bus-kernel.c
+++ b/src/libsystemd-bus/bus-kernel.c
@@ -45,8 +45,6 @@
 #define KDBUS_ITEM_HEADER_SIZE offsetof(struct kdbus_item, data)
 #define KDBUS_ITEM_SIZE(s) ALIGN8((s) + KDBUS_ITEM_HEADER_SIZE)
 
-#define KDBUS_POOL_SIZE (4*1024*1024)
-
 static int parse_unique_name(const char *s, uint64_t *id) {
         int r;
 
diff --git a/src/libsystemd-bus/bus-kernel.h b/src/libsystemd-bus/bus-kernel.h
index 1651c1e..8cf153a 100644
--- a/src/libsystemd-bus/bus-kernel.h
+++ b/src/libsystemd-bus/bus-kernel.h
@@ -33,6 +33,10 @@
  * sending vectors */
 #define MEMFD_MIN_SIZE (32*1024)
 
+/* The size of the per-connection memory pool that we set up and where
+ * the kernel places our incoming messages */
+#define KDBUS_POOL_SIZE (16*1024*1024)
+
 struct memfd_cache {
         int fd;
         void *address;
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 08ab202..b0730d4 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -26,6 +26,7 @@
 #include <netdb.h>
 #include <sys/poll.h>
 #include <byteswap.h>
+#include <sys/mman.h>
 
 #include "util.h"
 #include "macro.h"
@@ -64,6 +65,9 @@ static void bus_free(sd_bus *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);

commit f54514f3542db9b1f1a6f7546472718ce0d02aae
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri May 17 02:22:37 2013 +0200

    bus: keep kernel bus fd around during entire life-time of bus
    
    We need this since we might need to invoke the release ioctl for
    messages. Since we don't want to add any locking for that we simply keep
    a reference to the bus and then rely that the fd stays valid all the
    time.

diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 0edb097..5ba32ad 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -68,9 +68,14 @@ enum bus_state {
         BUS_OPENING,
         BUS_AUTHENTICATING,
         BUS_HELLO,
-        BUS_RUNNING
+        BUS_RUNNING,
+        BUS_CLOSED
 };
 
+static inline bool BUS_IS_OPEN(enum bus_state state) {
+        return state > BUS_UNSET && state < BUS_CLOSED;
+}
+
 enum bus_auth {
         _BUS_AUTH_INVALID,
         BUS_AUTH_EXTERNAL,
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index c72b52d..e531dec 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -128,14 +128,14 @@ static void message_free(sd_bus_message *m) {
         if (m->release_kdbus)
                 ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, m->kdbus);
 
+        if (m->bus)
+                sd_bus_unref(m->bus);
+
         if (m->free_fds) {
                 close_many(m->fds, m->n_fds);
                 free(m->fds);
         }
 
-        if (m->bus)
-                sd_bus_unref(m->bus);
-
         if (m->iovec != m->iovec_fixed)
                 free(m->iovec);
 
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 4a08177..08ab202 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -43,6 +43,18 @@
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
 
+static void bus_close_fds(sd_bus *b) {
+        assert(b);
+
+        if (b->input_fd >= 0)
+                close_nointr_nofail(b->input_fd);
+
+        if (b->output_fd >= 0 && b->output_fd != b->input_fd)
+                close_nointr_nofail(b->output_fd);
+
+        b->input_fd = b->output_fd = -1;
+}
+
 static void bus_free(sd_bus *b) {
         struct filter_callback *f;
         struct object_callback *c;
@@ -50,7 +62,7 @@ static void bus_free(sd_bus *b) {
 
         assert(b);
 
-        sd_bus_close(b);
+        bus_close_fds(b);
 
         free(b->rbuffer);
         free(b->unique_name);
@@ -922,12 +934,19 @@ void sd_bus_close(sd_bus *bus) {
         if (!bus)
                 return;
 
-        if (bus->input_fd >= 0)
-                close_nointr_nofail(bus->input_fd);
-        if (bus->output_fd >= 0 && bus->output_fd != bus->input_fd)
-                close_nointr_nofail(bus->output_fd);
+        if (bus->state != BUS_CLOSED)
+                return;
+
+        bus->state = BUS_CLOSED;
 
-        bus->input_fd = bus->output_fd = -1;
+        if (!bus->is_kernel)
+                bus_close_fds(bus);
+
+        /* We'll leave the fd open in case this is a kernel bus, since
+         * there might still be memblocks around that reference this
+         * bus, and they might need to invoke the
+         * KDBUS_CMD_MSG_RELEASE ioctl on the fd when they are
+         * freed. */
 }
 
 sd_bus *sd_bus_ref(sd_bus *bus) {
@@ -953,7 +972,7 @@ int sd_bus_is_open(sd_bus *bus) {
         if (!bus)
                 return -EINVAL;
 
-        return bus->state != BUS_UNSET && bus->input_fd >= 0;
+        return BUS_IS_OPEN(bus->state);
 }
 
 int sd_bus_can_send(sd_bus *bus, char type) {
@@ -961,7 +980,7 @@ int sd_bus_can_send(sd_bus *bus, char type) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->output_fd < 0)
+        if (bus->state == BUS_UNSET)
                 return -ENOTCONN;
 
         if (type == SD_BUS_TYPE_UNIX_FD) {
@@ -1012,9 +1031,6 @@ static int dispatch_wqueue(sd_bus *bus) {
         assert(bus);
         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
-        if (bus->output_fd < 0)
-                return -ENOTCONN;
-
         while (bus->wqueue_size > 0) {
 
                 if (bus->is_kernel)
@@ -1059,9 +1075,6 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
         assert(m);
         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
-        if (bus->input_fd < 0)
-                return -ENOTCONN;
-
         if (bus->rqueue_size > 0) {
                 /* Dispatch a queued message */
 
@@ -1097,9 +1110,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->output_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
@@ -1210,9 +1221,7 @@ int sd_bus_send_with_reply(
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->output_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
@@ -1294,11 +1303,8 @@ int bus_ensure_running(sd_bus *bus) {
 
         assert(bus);
 
-        if (bus->input_fd < 0)
-                return -ENOTCONN;
-        if (bus->state == BUS_UNSET)
+        if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED)
                 return -ENOTCONN;
-
         if (bus->state == BUS_RUNNING)
                 return 1;
 
@@ -1331,9 +1337,7 @@ int sd_bus_send_with_reply_and_block(
 
         if (!bus)
                 return -EINVAL;
-        if (bus->output_fd < 0)
-                return -ENOTCONN;
-        if (bus->state == BUS_UNSET)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
@@ -1449,7 +1453,7 @@ int sd_bus_send_with_reply_and_block(
 int sd_bus_get_fd(sd_bus *bus) {
         if (!bus)
                 return -EINVAL;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (bus->input_fd != bus->output_fd)
                 return -EPERM;
@@ -1462,9 +1466,7 @@ int sd_bus_get_events(sd_bus *bus) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
 
         if (bus->state == BUS_OPENING)
@@ -1493,9 +1495,7 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
                 return -EINVAL;
         if (!timeout_usec)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
 
         if (bus->state == BUS_AUTHENTICATING) {
@@ -1992,8 +1992,6 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->input_fd < 0)
-                return -ENOTCONN;
 
         /* We don't allow recursively invoking sd_bus_process(). */
         if (bus->processing)
@@ -2002,6 +2000,7 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
         switch (bus->state) {
 
         case BUS_UNSET:
+        case BUS_CLOSED:
                 return -ENOTCONN;
 
         case BUS_OPENING:
@@ -2042,7 +2041,7 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
 
         assert(bus);
 
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
 
         e = sd_bus_get_events(bus);
@@ -2088,9 +2087,7 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->input_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
         if (bus->rqueue_size > 0)
                 return 0;
@@ -2103,9 +2100,7 @@ int sd_bus_flush(sd_bus *bus) {
 
         if (!bus)
                 return -EINVAL;
-        if (bus->state == BUS_UNSET)
-                return -ENOTCONN;
-        if (bus->output_fd < 0)
+        if (!BUS_IS_OPEN(bus->state))
                 return -ENOTCONN;
 
         r = bus_ensure_running(bus);



More information about the systemd-commits mailing list