chiark / gitweb /
bus: parse matches locally and allow registration of callbacks for them
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 314fa6b6bff4a6af8a95de61488c23103077918e..0a263ea68c1e19868eacbf2b2b97b50c1960eae9 100644 (file)
@@ -37,6 +37,7 @@
 #include "bus-message.h"
 #include "bus-type.h"
 #include "bus-socket.h"
+#include "bus-control.h"
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
 
@@ -52,7 +53,7 @@ static void bus_free(sd_bus *b) {
 
         free(b->rbuffer);
         free(b->unique_name);
-        free(b->auth_uid);
+        free(b->auth_buffer);
         free(b->address);
 
         free(b->exec_path);
@@ -84,6 +85,8 @@ static void bus_free(sd_bus *b) {
 
         hashmap_free(b->object_callbacks);
 
+        bus_match_free(&b->match_callbacks);
+
         free(b);
 }
 
@@ -197,6 +200,29 @@ int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
         return 0;
 }
 
+int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server) {
+        if (!bus)
+                return -EINVAL;
+        if (!b && !sd_id128_equal(server, SD_ID128_NULL))
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+
+        bus->is_server = !!b;
+        bus->peer = server;
+        return 0;
+}
+
+int sd_bus_set_anonymous(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+
+        bus->anonymous_auth = !!b;
+        return 0;
+}
+
 static int hello_callback(sd_bus *bus, int error, sd_bus_message *reply, void *userdata) {
         const char *s;
         int r;
@@ -716,9 +742,12 @@ int sd_bus_start(sd_bus *bus) {
 
         bus->state = BUS_OPENING;
 
+        if (bus->is_server && bus->bus_client)
+                return -EINVAL;
+
         if (bus->fd >= 0)
                 r = bus_start_fd(bus);
-        else if (bus->address)
+        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path)
                 r = bus_start_address(bus);
         else
                 return -EINVAL;
@@ -1350,7 +1379,7 @@ int sd_bus_get_events(sd_bus *bus) {
                 flags |= POLLOUT;
         else if (bus->state == BUS_AUTHENTICATING) {
 
-                if (bus->auth_index < ELEMENTSOF(bus->auth_iovec))
+                if (bus_socket_auth_needs_write(bus))
                         flags |= POLLOUT;
 
                 flags |= POLLIN;
@@ -1426,8 +1455,8 @@ static int process_hello(sd_bus *bus, sd_bus_message *m) {
 
         /* Let's make sure the first message on the bus is the HELLO
          * reply. But note that we don't actually parse the message
-         * here (we leave that to the usual reply handling), we just
-         * verify we don't let any earlier msg through. */
+         * here (we leave that to the usual handling), we just verify
+         * we don't let any earlier msg through. */
 
         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
             m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
@@ -1467,6 +1496,9 @@ static int process_filter(sd_bus *bus, sd_bus_message *m) {
         struct filter_callback *l;
         int r;
 
+        assert(bus);
+        assert(m);
+
         LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
                 r = l->callback(bus, 0, m, l->userdata);
                 if (r != 0)
@@ -1476,6 +1508,13 @@ static int process_filter(sd_bus *bus, sd_bus_message *m) {
         return 0;
 }
 
+static int process_match(sd_bus *bus, sd_bus_message *m) {
+        assert(bus);
+        assert(m);
+
+        return bus_match_run(bus, &bus->match_callbacks, 0, m);
+}
+
 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         int r;
@@ -1705,6 +1744,10 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
         if (r != 0)
                 return r;
 
+        r = process_match(bus, m);
+        if (r != 0)
+                return r;
+
         r = process_builtin(bus, m);
         if (r != 0)
                 return r;
@@ -2031,3 +2074,48 @@ int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_message_handler_t ca
 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);
 }
+
+int sd_bus_add_match(sd_bus *bus, const char *match, sd_message_handler_t callback, void *userdata) {
+        int r = 0;
+
+        if (!bus)
+                return -EINVAL;
+        if (!match)
+                return -EINVAL;
+
+        if (bus->bus_client) {
+                r = bus_add_match_internal(bus, match);
+                if (r < 0)
+                        return r;
+        }
+
+        if (callback) {
+                r = bus_match_add(&bus->match_callbacks, match, callback, userdata, NULL);
+                if (r < 0) {
+
+                        if (bus->bus_client)
+                                bus_remove_match_internal(bus, match);
+                }
+        }
+
+        return r;
+}
+
+int sd_bus_remove_match(sd_bus *bus, const char *match, sd_message_handler_t callback, void *userdata) {
+        int r = 0, q = 0;
+
+        if (!bus)
+                return -EINVAL;
+        if (!match)
+                return -EINVAL;
+
+        if (bus->bus_client)
+                r = bus_remove_match_internal(bus, match);
+
+        if (callback)
+                q = bus_match_remove(&bus->match_callbacks, match, callback, userdata);
+
+        if (r < 0)
+                return r;
+        return q;
+}