chiark / gitweb /
bus: parse uid/gid/pid/tid meta data from kdbus messages
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 0964649e39d675c1df423591aca7913b4e0d66ed..2424ee14465bb69737f8b4281ec226c6839672fa 100644 (file)
@@ -37,6 +37,7 @@
 #include "bus-message.h"
 #include "bus-type.h"
 #include "bus-socket.h"
+#include "bus-kernel.h"
 #include "bus-control.h"
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
@@ -54,6 +55,7 @@ static void bus_free(sd_bus *b) {
         free(b->unique_name);
         free(b->auth_buffer);
         free(b->address);
+        free(b->kernel);
 
         free(b->exec_path);
         strv_free(b->exec_argv);
@@ -259,7 +261,7 @@ static int bus_send_hello(sd_bus *bus) {
 
         assert(bus);
 
-        if (!bus->bus_client)
+        if (!bus->bus_client || bus->is_kernel)
                 return 0;
 
         r = sd_bus_message_new_method_call(
@@ -436,8 +438,11 @@ static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
 
 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
         _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
-        struct addrinfo hints, *result;
         int r;
+        struct addrinfo *result, hints = {
+                .ai_socktype = SOCK_STREAM,
+                .ai_flags = AI_ADDRCONFIG,
+        };
 
         assert(b);
         assert(p);
@@ -475,10 +480,6 @@ static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
         if (!host || !port)
                 return -EINVAL;
 
-        zero(hints);
-        hints.ai_socktype = SOCK_STREAM;
-        hints.ai_flags = AI_ADDRCONFIG;
-
         if (family) {
                 if (streq(family, "ipv4"))
                         hints.ai_family = AF_INET;
@@ -597,6 +598,41 @@ fail:
         return r;
 }
 
+static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
+        _cleanup_free_ char *path = NULL;
+        int r;
+
+        assert(b);
+        assert(p);
+        assert(*p);
+        assert(guid);
+
+        while (**p != 0 && **p != ';') {
+                r = parse_address_key(p, "guid", guid);
+                if (r < 0)
+                        return r;
+                else if (r > 0)
+                        continue;
+
+                r = parse_address_key(p, "path", &path);
+                if (r < 0)
+                        return r;
+                else if (r > 0)
+                        continue;
+
+                skip_address_key(p);
+        }
+
+        if (!path)
+                return -EINVAL;
+
+        free(b->kernel);
+        b->kernel = path;
+        path = NULL;
+
+        return 0;
+}
+
 static void bus_reset_parsed_address(sd_bus *b) {
         assert(b);
 
@@ -607,6 +643,8 @@ static void bus_reset_parsed_address(sd_bus *b) {
         b->exec_path = NULL;
         b->exec_argv = NULL;
         b->server_id = SD_ID128_NULL;
+        free(b->kernel);
+        b->kernel = NULL;
 }
 
 static int bus_parse_next_address(sd_bus *b) {
@@ -658,6 +696,14 @@ static int bus_parse_next_address(sd_bus *b) {
 
                         break;
 
+                } else if (startswith(a, "kernel:")) {
+
+                        a += 7;
+                        r = parse_kernel_address(b, &a, &guid);
+                        if (r < 0)
+                                return r;
+
+                        break;
                 }
 
                 a = strchr(a, ';');
@@ -697,6 +743,13 @@ static int bus_start_address(sd_bus *b) {
                         if (r >= 0)
                                 return r;
 
+                        b->last_connect_error = -r;
+                } else if (b->kernel) {
+
+                        r = bus_kernel_connect(b);
+                        if (r >= 0)
+                                return r;
+
                         b->last_connect_error = -r;
                 }
 
@@ -716,6 +769,7 @@ int bus_next_address(sd_bus *b) {
 }
 
 static int bus_start_fd(sd_bus *b) {
+        struct stat st;
         int r;
 
         assert(b);
@@ -740,7 +794,13 @@ static int bus_start_fd(sd_bus *b) {
                         return r;
         }
 
-        return bus_socket_take_fd(b);
+        if (fstat(b->input_fd, &st) < 0)
+                return -errno;
+
+        if (S_ISCHR(b->input_fd))
+                return bus_kernel_take_fd(b);
+        else
+                return bus_socket_take_fd(b);
 }
 
 int sd_bus_start(sd_bus *bus) {
@@ -758,7 +818,7 @@ int sd_bus_start(sd_bus *bus) {
 
         if (bus->input_fd >= 0)
                 r = bus_start_fd(bus);
-        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path)
+        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel)
                 r = bus_start_address(bus);
         else
                 return -EINVAL;
@@ -959,14 +1019,18 @@ static int dispatch_wqueue(sd_bus *bus) {
 
         while (bus->wqueue_size > 0) {
 
-                r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
+                if (bus->is_kernel)
+                        r = bus_kernel_write_message(bus, bus->wqueue[0]);
+                else
+                        r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
+
                 if (r < 0) {
                         sd_bus_close(bus);
                         return r;
                 } else if (r == 0)
                         /* Didn't do anything this time */
                         return ret;
-                else if (bus->windex >= bus->wqueue[0]->size) {
+                else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
                         /* Fully written. Let's drop the entry from
                          * the queue.
                          *
@@ -1011,7 +1075,11 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
 
         /* Try to read a new message */
         do {
-                r = bus_socket_read_message(bus, &z);
+                if (bus->is_kernel)
+                        r = bus_kernel_read_message(bus, &z);
+                else
+                        r = bus_socket_read_message(bus, &z);
+
                 if (r < 0) {
                         sd_bus_close(bus);
                         return r;
@@ -1063,11 +1131,15 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
         if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
                 size_t idx = 0;
 
-                r = bus_socket_write_message(bus, m, &idx);
+                if (bus->is_kernel)
+                        r = bus_kernel_write_message(bus, m);
+                else
+                        r = bus_socket_write_message(bus, m, &idx);
+
                 if (r < 0) {
                         sd_bus_close(bus);
                         return r;
-                } else if (idx < m->size)  {
+                } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m))  {
                         /* Wasn't fully written. So let's remember how
                          * much was written. Note that the first entry
                          * of the wqueue array is always allocated so
@@ -1167,7 +1239,7 @@ int sd_bus_send_with_reply(
         if (r < 0)
                 return r;
 
-        c = new(struct reply_callback, 1);
+        c = new0(struct reply_callback, 1);
         if (!c)
                 return -ENOMEM;
 
@@ -1305,7 +1377,10 @@ int sd_bus_send_with_reply_and_block(
                         room = true;
                 }
 
-                r = bus_socket_read_message(bus, &incoming);
+                if (bus->is_kernel)
+                        r = bus_kernel_read_message(bus, &incoming);
+                else
+                        r = bus_socket_read_message(bus, &incoming);
                 if (r < 0)
                         return r;
                 if (incoming) {
@@ -1314,7 +1389,12 @@ int sd_bus_send_with_reply_and_block(
                                 /* Found a match! */
 
                                 if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
-                                        *reply = incoming;
+
+                                        if (reply)
+                                                *reply = incoming;
+                                        else
+                                                sd_bus_message_unref(incoming);
+
                                         return 0;
                                 }
 
@@ -1517,20 +1597,47 @@ static int process_filter(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
-        LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
-                r = l->callback(bus, 0, m, l->userdata);
-                if (r != 0)
-                        return r;
-        }
+        do {
+                bus->filter_callbacks_modified = false;
+
+                LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
+
+                        if (bus->filter_callbacks_modified)
+                                break;
+
+                        /* Don't run this more than once per iteration */
+                        if (l->last_iteration == bus->iteration_counter)
+                                continue;
+
+                        l->last_iteration = bus->iteration_counter;
+
+                        r = l->callback(bus, 0, m, l->userdata);
+                        if (r != 0)
+                                return r;
+
+                }
+
+        } while (bus->filter_callbacks_modified);
 
         return 0;
 }
 
 static int process_match(sd_bus *bus, sd_bus_message *m) {
+        int r;
+
         assert(bus);
         assert(m);
 
-        return bus_match_run(bus, &bus->match_callbacks, 0, m);
+        do {
+                bus->match_callbacks_modified = false;
+
+                r = bus_match_run(bus, &bus->match_callbacks, 0, m);
+                if (r != 0)
+                        return r;
+
+        } while (bus->match_callbacks_modified);
+
+        return 0;
 }
 
 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
@@ -1565,7 +1672,7 @@ 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_INIT;
+                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
 
                 sd_bus_error_set(&error,
                                  "org.freedesktop.DBus.Error.UnknownMethod",
@@ -1585,12 +1692,12 @@ static int process_builtin(sd_bus *bus, sd_bus_message *m) {
 }
 
 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_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         struct object_callback *c;
-        char *p;
         int r;
         bool found = false;
+        size_t pl;
 
         assert(bus);
         assert(m);
@@ -1601,35 +1708,53 @@ static int process_object(sd_bus *bus, sd_bus_message *m) {
         if (hashmap_isempty(bus->object_callbacks))
                 return 0;
 
-        c = hashmap_get(bus->object_callbacks, m->path);
-        if (c) {
-                r = c->callback(bus, 0, m, c->userdata);
-                if (r != 0)
-                        return r;
+        pl = strlen(m->path);
 
-                found = true;
-        }
+        do {
+                char p[pl+1];
 
-        /* Look for fallback prefixes */
-        p = strdupa(m->path);
-        for (;;) {
-                char *e;
+                bus->object_callbacks_modified = false;
 
-                e = strrchr(p, '/');
-                if (e == p || !e)
-                        break;
+                c = hashmap_get(bus->object_callbacks, m->path);
+                if (c && c->last_iteration != bus->iteration_counter) {
 
-                *e = 0;
+                        c->last_iteration = bus->iteration_counter;
 
-                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;
                 }
-        }
+
+                /* 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 = c->callback(bus, 0, 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
@@ -1750,6 +1875,8 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
+        bus->iteration_counter++;
+
         r = process_hello(bus, m);
         if (r != 0)
                 return r;
@@ -1810,7 +1937,7 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) {
 
         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_INIT;
+                _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);
 
@@ -1845,6 +1972,10 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
         if (bus->input_fd < 0)
                 return -ENOTCONN;
 
+        /* We don't allow recursively invoking sd_bus_process(). */
+        if (bus->processing)
+                return -EBUSY;
+
         switch (bus->state) {
 
         case BUS_UNSET:
@@ -1870,14 +2001,18 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
         case BUS_RUNNING:
         case BUS_HELLO:
 
-                return process_running(bus, ret);
+                bus->processing = true;
+                r = process_running(bus, ret);
+                bus->processing = false;
+
+                return r;
         }
 
         assert_not_reached("Unknown state");
 }
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
-        struct pollfd p[2];
+        struct pollfd p[2] = {};
         int r, e, n;
         struct timespec ts;
         usec_t until, m;
@@ -1908,9 +2043,7 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
         if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
                 m = timeout_usec;
 
-        zero(p);
         p[0].fd = bus->input_fd;
-
         if (bus->output_fd == bus->input_fd) {
                 p[0].events = e;
                 n = 1;
@@ -1981,12 +2114,13 @@ int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *user
         if (!callback)
                 return -EINVAL;
 
-        f = new(struct filter_callback, 1);
+        f = new0(struct filter_callback, 1);
         if (!f)
                 return -ENOMEM;
         f->callback = callback;
         f->userdata = userdata;
 
+        bus->filter_callbacks_modified = true;
         LIST_PREPEND(struct filter_callback, callbacks, bus->filter_callbacks, f);
         return 0;
 }
@@ -2001,6 +2135,7 @@ int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *u
 
         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);
                         free(f);
                         return 1;
@@ -2031,7 +2166,7 @@ static int bus_add_object(
         if (r < 0)
                 return r;
 
-        c = new(struct object_callback, 1);
+        c = new0(struct object_callback, 1);
         if (!c)
                 return -ENOMEM;
 
@@ -2045,6 +2180,7 @@ static int bus_add_object(
         c->userdata = userdata;
         c->is_fallback = fallback;
 
+        bus->object_callbacks_modified = true;
         r = hashmap_put(bus->object_callbacks, c->path, c);
         if (r < 0) {
                 free(c->path);
@@ -2078,6 +2214,7 @@ static int bus_remove_object(
         if (c->callback != callback || c->userdata != userdata || c->is_fallback != fallback)
                 return 0;
 
+        bus->object_callbacks_modified = true;
         assert_se(c == hashmap_remove(bus->object_callbacks, c->path));
 
         free(c->path);
@@ -2117,6 +2254,7 @@ int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t ca
         }
 
         if (callback) {
+                bus->match_callbacks_modified = true;
                 r = bus_match_add(&bus->match_callbacks, match, callback, userdata, NULL);
                 if (r < 0) {
 
@@ -2139,10 +2277,132 @@ int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t
         if (bus->bus_client)
                 r = bus_remove_match_internal(bus, match);
 
-        if (callback)
+        if (callback) {
+                bus->match_callbacks_modified = true;
                 q = bus_match_remove(&bus->match_callbacks, match, callback, userdata);
+        }
 
         if (r < 0)
                 return r;
         return q;
 }
+
+int sd_bus_emit_signal(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *member,
+                const char *types, ...) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        va_list ap;
+        int r;
+
+        if (!bus)
+                return -EINVAL;
+
+        r = sd_bus_message_new_signal(bus, path, interface, member, &m);
+        if (r < 0)
+                return r;
+
+        va_start(ap, types);
+        r = bus_message_append_ap(m, types, ap);
+        va_end(ap);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(bus, m, NULL);
+}
+
+int sd_bus_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, ...) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        va_list ap;
+        int r;
+
+        if (!bus)
+                return -EINVAL;
+
+        r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
+        if (r < 0)
+                return r;
+
+        va_start(ap, types);
+        r = bus_message_append_ap(m, types, ap);
+        va_end(ap);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
+}
+
+int sd_bus_reply_method_return(
+                sd_bus *bus,
+                sd_bus_message *call,
+                const char *types, ...) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        va_list ap;
+        int r;
+
+        if (!bus)
+                return -EINVAL;
+        if (!call)
+                return -EINVAL;
+        if (!call->sealed)
+                return -EPERM;
+        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+                return -EINVAL;
+
+        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
+
+        r = sd_bus_message_new_method_return(bus, call, &m);
+        if (r < 0)
+                return r;
+
+        va_start(ap, types);
+        r = bus_message_append_ap(m, types, ap);
+        va_end(ap);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(bus, m, NULL);
+}
+
+int sd_bus_reply_method_error(
+                sd_bus *bus,
+                sd_bus_message *call,
+                const sd_bus_error *e) {
+
+        _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+        int r;
+
+        if (!bus)
+                return -EINVAL;
+        if (!call)
+                return -EINVAL;
+        if (!call->sealed)
+                return -EPERM;
+        if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+                return -EINVAL;
+        if (!sd_bus_error_is_set(e))
+                return -EINVAL;
+
+        if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+                return 0;
+
+        r = sd_bus_message_new_method_error(bus, call, e, &m);
+        if (r < 0)
+                return r;
+
+        return sd_bus_send(bus, m, NULL);
+}