chiark / gitweb /
bus: properly handle termination of connections
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 73774ba308c67430160892ab17bf4f7931426a87..941b33ab695f62074f8f2a2e58fb6e0bf8524d78 100644 (file)
@@ -40,6 +40,7 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
 
 static void bus_free(sd_bus *b) {
         struct filter_callback *f;
+        struct object_callback *c;
         unsigned i;
 
         assert(b);
@@ -52,6 +53,9 @@ static void bus_free(sd_bus *b) {
         free(b->auth_uid);
         free(b->address);
 
+        close_many(b->fds, b->n_fds);
+        free(b->fds);
+
         for (i = 0; i < b->rqueue_size; i++)
                 sd_bus_message_unref(b->rqueue[i]);
         free(b->rqueue);
@@ -68,6 +72,13 @@ static void bus_free(sd_bus *b) {
                 free(f);
         }
 
+        while ((c = hashmap_steal_first(b->object_callbacks))) {
+                free(c->path);
+                free(c);
+        }
+
+        hashmap_free(b->object_callbacks);
+
         free(b);
 }
 
@@ -521,6 +532,8 @@ static int bus_read_auth(sd_bus *b) {
         k = recvmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
         if (k < 0)
                 return errno == EAGAIN ? 0 : -errno;
+        if (k == 0)
+                return -ECONNRESET;
 
         b->rbuffer_size += k;
 
@@ -881,6 +894,18 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
 
         if (*idx >= m->size)
                 return 0;
+        zero(mh);
+
+        if (m->n_fds > 0) {
+                struct cmsghdr *control;
+                control = alloca(CMSG_SPACE(sizeof(int) * m->n_fds));
+
+                mh.msg_control = control;
+                control->cmsg_level = SOL_SOCKET;
+                control->cmsg_type = SCM_RIGHTS;
+                mh.msg_controllen = control->cmsg_len = CMSG_LEN(sizeof(int) * m->n_fds);
+                memcpy(CMSG_DATA(control), m->fds, sizeof(int) * m->n_fds);
+        }
 
         n = m->n_iovec * sizeof(struct iovec);
         iov = alloca(n);
@@ -889,7 +914,6 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
         j = 0;
         iovec_advance(iov, &j, *idx);
 
-        zero(mh);
         mh.msg_iov = iov;
         mh.msg_iovlen = m->n_iovec;
 
@@ -955,7 +979,7 @@ static int message_read_need(sd_bus *bus, size_t *need) {
 
 static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
         sd_bus_message *t;
-        void *b = NULL;
+        void *b;
         int r;
 
         assert(bus);
@@ -964,16 +988,18 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
 
         if (bus->rbuffer_size > size) {
-                b = memdup((const uint8_t*) bus->rbuffer + size, bus->rbuffer_size - size);
-                if (!b) {
-                        free(t);
+                b = memdup((const uint8_t*) bus->rbuffer + size,
+                           bus->rbuffer_size - size);
+                if (!b)
                         return -ENOMEM;
-                }
-        }
+        } else
+                b = NULL;
 
         r = bus_message_from_malloc(bus->rbuffer, size,
+                                    bus->fds, bus->n_fds,
                                     bus->ucred_valid ? &bus->ucred : NULL,
-                                    bus->label[0] ? bus->label : NULL, &t);
+                                    bus->label[0] ? bus->label : NULL,
+                                    &t);
         if (r < 0) {
                 free(b);
                 return r;
@@ -982,6 +1008,9 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
         bus->rbuffer = b;
         bus->rbuffer_size -= size;
 
+        bus->fds = NULL;
+        bus->n_fds = 0;
+
         *m = t;
         return 1;
 }
@@ -995,7 +1024,8 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
         void *b;
         union {
                 struct cmsghdr cmsghdr;
-                uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
+                uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) +
+                            CMSG_SPACE(sizeof(struct ucred)) +
                             CMSG_SPACE(NAME_MAX)]; /*selinux label */
         } control;
         struct cmsghdr *cmsg;
@@ -1030,13 +1060,28 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
         k = recvmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
         if (k < 0)
                 return errno == EAGAIN ? 0 : -errno;
+        if (k == 0)
+                return -ECONNRESET;
 
         bus->rbuffer_size += k;
-        bus->ucred_valid = false;
-        bus->label[0] = 0;
 
         for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
                 if (cmsg->cmsg_level == SOL_SOCKET &&
+                    cmsg->cmsg_type == SCM_RIGHTS) {
+                        int n, *f;
+
+                        n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+
+                        f = realloc(bus->fds, sizeof(int) + (bus->n_fds + n));
+                        if (!f) {
+                                close_many((int*) CMSG_DATA(cmsg), n);
+                                return -ENOMEM;
+                        }
+
+                        memcpy(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int));
+                        bus->fds = f;
+                        bus->n_fds += n;
+                } else if (cmsg->cmsg_level == SOL_SOCKET &&
                     cmsg->cmsg_type == SCM_CREDENTIALS &&
                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
 
@@ -1150,6 +1195,8 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
                 return -ENOTCONN;
         if (!m)
                 return -EINVAL;
+        if (m->n_fds > 0 && !bus->can_fds)
+                return -ENOTSUP;
 
         /* If the serial number isn't kept, then we know that no reply
          * is expected */
@@ -1528,7 +1575,6 @@ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
 }
 
 static int process_timeout(sd_bus *bus) {
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         struct reply_callback *c;
         usec_t n;
         int r;
@@ -1552,6 +1598,43 @@ static int process_timeout(sd_bus *bus) {
         return r < 0 ? r : 1;
 }
 
+static int process_reply(sd_bus *bus, sd_bus_message *m) {
+        struct reply_callback *c;
+        int r;
+
+        assert(bus);
+        assert(m);
+
+        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
+            m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+                return 0;
+
+        c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
+        if (!c)
+                return 0;
+
+        if (c->timeout != 0)
+                prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
+
+        r = c->callback(bus, 0, m, c->userdata);
+        free(c);
+
+        return r;
+}
+
+static int process_filter(sd_bus *bus, sd_bus_message *m) {
+        struct filter_callback *l;
+        int r;
+
+        LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
+                r = l->callback(bus, 0, m, l->userdata);
+                if (r != 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         int r;
@@ -1603,36 +1686,90 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) {
         return 1;
 }
 
-static int process_message(sd_bus *bus, sd_bus_message *m) {
-        struct filter_callback *l;
+static int process_object(sd_bus *bus, sd_bus_message *m) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        struct object_callback *c;
+        char *p;
         int r;
+        bool found = false;
 
         assert(bus);
         assert(m);
 
-        if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN || m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) {
-                struct reply_callback *c;
+        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+                return 0;
 
-                c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
-                if (c) {
-                        if (c->timeout != 0)
-                                prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
+        if (hashmap_isempty(bus->object_callbacks))
+                return 0;
 
-                        r = c->callback(bus, 0, m, c->userdata);
-                        free(c);
+        c = hashmap_get(bus->object_callbacks, m->path);
+        if (c) {
+                r = c->callback(bus, 0, m, c->userdata);
+                if (r != 0)
+                        return r;
+
+                found = true;
+        }
+
+        /* Look for fallback prefixes */
+        p = strdupa(m->path);
+        for (;;) {
+                char *e;
 
+                e = strrchr(p, '/');
+                if (e == p || !e)
+                        break;
+
+                *e = 0;
+
+                c = hashmap_get(bus->object_callbacks, p);
+                if (c && c->is_fallback) {
+                        r = c->callback(bus, 0, m, c->userdata);
                         if (r != 0)
                                 return r;
+
+                        found = true;
                 }
         }
 
-        LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
-                r = l->callback(bus, 0, m, l->userdata);
-                if (r != 0)
-                        return r;
-        }
+        if (!found)
+                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_message(sd_bus *bus, sd_bus_message *m) {
+        int r;
+
+        assert(bus);
+        assert(m);
+
+        r = process_reply(bus, m);
+        if (r != 0)
+                return r;
+
+        r = process_filter(bus, m);
+        if (r != 0)
+                return r;
+
+        r = process_builtin(bus, m);
+        if (r != 0)
+                return r;
 
-        return process_builtin(bus, m);
+        return process_object(bus, m);
 }
 
 int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
@@ -1874,3 +2011,95 @@ int sd_bus_remove_filter(sd_bus *bus, sd_message_handler_t callback, void *userd
 
         return 0;
 }
+
+static int bus_add_object(
+                sd_bus *bus,
+                bool fallback,
+                const char *path,
+                sd_message_handler_t callback,
+                void *userdata) {
+
+        struct object_callback *c;
+        int r;
+
+        if (!bus)
+                return -EINVAL;
+        if (!path)
+                return -EINVAL;
+        if (!callback)
+                return -EINVAL;
+
+        r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func);
+        if (r < 0)
+                return r;
+
+        c = new(struct object_callback, 1);
+        if (!c)
+                return -ENOMEM;
+
+        c->path = strdup(path);
+        if (!path) {
+                free(c);
+                return -ENOMEM;
+        }
+
+        c->callback = callback;
+        c->userdata = userdata;
+        c->is_fallback = fallback;
+
+        r = hashmap_put(bus->object_callbacks, c->path, c);
+        if (r < 0) {
+                free(c->path);
+                free(c);
+                return r;
+        }
+
+        return 0;
+}
+
+static int bus_remove_object(
+                sd_bus *bus,
+                bool fallback,
+                const char *path,
+                sd_message_handler_t callback,
+                void *userdata) {
+
+        struct object_callback *c;
+
+        if (!bus)
+                return -EINVAL;
+        if (!path)
+                return -EINVAL;
+        if (!callback)
+                return -EINVAL;
+
+        c = hashmap_get(bus->object_callbacks, path);
+        if (!c)
+                return 0;
+
+        if (c->callback != callback || c->userdata != userdata || c->is_fallback != fallback)
+                return 0;
+
+        assert_se(c == hashmap_remove(bus->object_callbacks, c->path));
+
+        free(c->path);
+        free(c);
+
+        return 1;
+}
+
+int sd_bus_add_object(sd_bus *bus, const char *path, sd_message_handler_t callback, void *userdata) {
+        return bus_add_object(bus, false, path, callback, userdata);
+}
+
+int sd_bus_remove_object(sd_bus *bus, const char *path, sd_message_handler_t callback, void *userdata) {
+        return bus_remove_object(bus, false, path, callback, userdata);
+}
+
+int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_message_handler_t callback, void *userdata) {
+        return bus_add_object(bus, true, prefix, callback, userdata);
+}
+
+int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_message_handler_t callback, void *userdata) {
+        return bus_remove_object(bus, true, prefix, callback, userdata);
+}