chiark / gitweb /
driverd: implement AddMatch/RemoveMatch logic
[elogind.git] / src / bus-driverd / bus-driverd.c
index ccb1fb3..d4126a9 100644 (file)
@@ -34,7 +34,6 @@
 #include "kdbus.h"
 #include "sd-bus.h"
 #include "bus-internal.h"
-
 #include "sd-daemon.h"
 #include "sd-event.h"
 #include "event-util.h"
 #include "async.h"
 #include "hashmap.h"
 #include "def.h"
+#include "unit-name.h"
+#include "bus-control.h"
+
+#define CLIENTS_MAX 1024
+#define MATCHES_MAX 1024
+
+typedef struct Match Match;
+typedef struct Client Client;
+typedef struct Context Context;
+
+struct Match {
+        Client *client;
+        char *match;
+        uint64_t cookie;
+        LIST_FIELDS(Match, matches);
+};
+
+struct Client {
+        Context *context;
+        uint64_t id;
+        uint64_t next_cookie;
+        Hashmap *matches;
+        unsigned n_matches;
+        char *watch;
+};
+
+struct Context {
+        sd_bus *bus;
+        sd_event *event;
+        Hashmap *clients;
+};
+
+static void match_free(Match *m) {
 
-/*
- * TODO:
- *
- * AddMatch / RemoveMatch
- * ListActivatableNames
- * StartServiceByName
- */
+        if (!m)
+                return;
 
-static int driver_name_info_error(sd_bus *bus, sd_bus_message *m, const char *name, int error_code) {
+        if (m->client) {
+                Match *first;
 
-        if (error_code == -ENXIO || error_code == -ENOENT)
-                return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_NAME_HAS_NO_OWNER,
-                                                  "Could not get owner of name '%s': no such name",
-                                                  name);
+                first = hashmap_get(m->client->matches, m->match);
+                LIST_REMOVE(matches, first, m);
+                if (first)
+                        assert_se(hashmap_replace(m->client->matches, m->match, first) >= 0);
+                else
+                        hashmap_remove(m->client->matches, m->match);
+
+                m->client->n_matches--;
+        }
 
-        return sd_bus_reply_method_errno(m, error_code, NULL);
+        free(m->match);
+        free(m);
 }
 
-static int driver_add_match(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+static int match_new(Client *c, struct bus_match_component *components, unsigned n_components, Match **_m) {
+        Match *m, *first;
+        int r;
+
+        assert(c);
+        assert(_m);
+
+        r = hashmap_ensure_allocated(&c->matches, string_hash_func, string_compare_func);
+        if (r < 0)
+                return r;
+
+        m = new0(Match, 1);
+        if (!m)
+                return -ENOMEM;
+
+        m->match = bus_match_to_string(components, n_components);
+        if (!m->match) {
+                r = -ENOMEM;
+                goto fail;
+        }
+
+        m->cookie = ++c->next_cookie;
+
+        first = hashmap_get(c->matches, m->match);
+        LIST_PREPEND(matches, first, m);
+        r = hashmap_replace(c->matches, m->match, first);
+        if (r < 0) {
+                LIST_REMOVE(matches, first, m);
+                goto fail;
+        }
+
+        m->client = c;
+        c->n_matches++;
 
-        char *arg0, *match;
+        *_m = m;
+        m = NULL;
+
+        return 0;
+
+fail:
+        match_free(m);
+        return r;
+}
+
+static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error);
+
+static void client_free(Client *c) {
+        Match *m;
+
+        if (!c)
+                return;
+
+        if (c->context) {
+                if (c->watch)
+                        sd_bus_remove_match(c->context->bus, c->watch, on_name_owner_changed, c);
+
+                assert_se(hashmap_remove(c->context->clients, &c->id) == c);
+        }
+
+        while ((m = hashmap_first(c->matches)))
+                match_free(m);
+
+        hashmap_free(c->matches);
+        free(c->watch);
+
+        free(c);
+}
+
+static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+        Client *c = userdata;
+
+        assert(bus);
+        assert(m);
+
+        client_free(c);
+        return 0;
+}
+
+static int client_acquire(Context *context, uint64_t id, Client **_c) {
+        char *watch = NULL;
+        Client *c;
         int r;
 
-        r = sd_bus_message_read(m, "s", &arg0);
+        assert(context);
+        assert(_c);
+
+        c = hashmap_get(context->clients, &id);
+        if (c) {
+                *_c = c;
+                return 0;
+        }
+
+        if (hashmap_size(context->clients) >= CLIENTS_MAX)
+                return -ENOBUFS;
+
+        r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func);
         if (r < 0)
                 return r;
 
-        match = strdup(arg0);
-        if (!match)
+        c = new0(Client, 1);
+        if (!c)
                 return -ENOMEM;
 
-        /* FIXME */
+        c->id = id;
 
-        return sd_bus_reply_method_return(m, NULL);
+        r = hashmap_put(context->clients, &c->id, c);
+        if (r < 0)
+                goto fail;
+
+        c->context = context;
+
+        if (asprintf(&watch,
+                     "type='signal',"
+                     "sender='org.freedesktop.DBus',"
+                     "path='/org/freedesktop/DBus',"
+                     "interface='org.freedesktop.DBus',"
+                     "member='NameOwnerChanged',"
+                     "arg0=':1.%llu'", (unsigned long long) id) < 0) {
+                r = -ENOMEM;
+                goto fail;
+        }
+
+        r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c);
+        if (r < 0) {
+                free(watch);
+                goto fail;
+        }
+
+        c->watch = watch;
+
+        *_c = c;
+        return 0;
+
+fail:
+        client_free(c);
+        return r;
 }
 
-static int driver_remove_match(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+static int driver_add_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
 
+        struct bus_match_component *components = NULL;
+        Context *context = userdata;
+        unsigned n_components = 0;
+        Match *m = NULL;
+        Client *c = NULL;
         char *arg0;
+        uint64_t id;
         int r;
 
-        r = sd_bus_message_read(m, "s", &arg0);
+        assert(bus);
+        assert(message);
+        assert(context);
+
+        r = sd_bus_message_read(message, "s", &arg0);
         if (r < 0)
                 return r;
 
-        /* FIXME */
+        r = bus_kernel_parse_unique_name(message->sender, &id);
+        if (r < 0)
+                return r;
+
+        r = client_acquire(context, id, &c);
+        if (r == -ENOBUFS)
+                return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u clients", CLIENTS_MAX);
+        if (r < 0)
+                return r;
+
+        if (c->n_matches >= MATCHES_MAX) {
+                r = sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u matches per client", MATCHES_MAX);
+                goto fail;
+        }
+
+        r = bus_match_parse(arg0, &components, &n_components);
+        if (r < 0) {
+                r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
+                goto fail;
+        }
+
+        r = match_new(c, components, n_components, &m);
+        if (r < 0)
+                goto fail;
+
+        r = bus_add_match_internal_kernel(bus, id, components, n_components, m->cookie);
+        if (r < 0)
+                goto fail;
+
+        bus_match_parse_free(components, n_components);
+
+        return sd_bus_reply_method_return(message, NULL);
+
+fail:
+        bus_match_parse_free(components, n_components);
 
-        return sd_bus_reply_method_return(m, NULL);
+        match_free(m);
+
+        if (c->n_matches <= 0)
+                client_free(c);
+
+        return r;
 }
 
-static int driver_get_security_ctx(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+static int driver_remove_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+
+        struct bus_match_component *components = NULL;
+        _cleanup_free_ char *normalized = NULL;
+        Context *context = userdata;
+        unsigned n_components = 0;
+        Client *c = NULL;
+        Match *m = NULL;
+        char *arg0;
+        uint64_t id;
+        int r;
+
+        assert(bus);
+        assert(message);
+        assert(context);
+
+        r = sd_bus_message_read(message, "s", &arg0);
+        if (r < 0)
+                return r;
+
+        r = bus_kernel_parse_unique_name(message->sender, &id);
+        if (r < 0)
+                return r;
+
+        c = hashmap_get(context->clients, &id);
+        if (!c)
+                return sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "You have not registered any matches.");
+
+        r = bus_match_parse(arg0, &components, &n_components);
+        if (r < 0) {
+                r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
+                goto finish;
+        }
+
+        normalized = bus_match_to_string(components, n_components);
+        if (!normalized) {
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        m = hashmap_get(c->matches, normalized);
+        if (!m) {
+                r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "Match rule \"%s\" not found.");
+                goto finish;
+        }
+
+        bus_remove_match_internal_kernel(bus, id, m->cookie);
+        match_free(m);
+
+        r = sd_bus_reply_method_return(message, NULL);
 
+finish:
+        bus_match_parse_free(components, n_components);
+
+        if (c->n_matches <= 0)
+                client_free(c);
+
+        return r;
+}
+
+static int driver_get_security_ctx(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         char *arg0;
@@ -112,13 +374,11 @@ static int driver_get_security_ctx(sd_bus *bus, sd_bus_message *m, void *userdat
         if (r < 0)
                 return r;
 
+        assert_return(service_name_is_valid(arg0), -EINVAL);
+
         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_SELINUX_CONTEXT, &creds);
-        if (r < 0) {
-                if (r == -ENOENT)
-                        return driver_name_info_error(bus, m, arg0, r);
-                else
-                        return r;
-        }
+        if (r < 0)
+                return r;
 
         r = sd_bus_message_new_method_return(m, &reply);
         if (r < 0)
@@ -132,7 +392,6 @@ static int driver_get_security_ctx(sd_bus *bus, sd_bus_message *m, void *userdat
 }
 
 static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
         char *arg0;
         int r;
@@ -141,6 +400,8 @@ static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus
         if (r < 0)
                 return r;
 
+        assert_return(service_name_is_valid(arg0), -EINVAL);
+
         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_PID, &creds);
         if (r < 0)
                 return r;
@@ -149,7 +410,6 @@ static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus
 }
 
 static int driver_get_user(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
         char *arg0;
         int r;
@@ -158,19 +418,16 @@ static int driver_get_user(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
         if (r < 0)
                 return r;
 
+        assert_return(service_name_is_valid(arg0), -EINVAL);
+
         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_UID, &creds);
-        if (r < 0) {
-                if (r == -ENOENT)
-                        return driver_name_info_error(bus, m, arg0, r);
-                else
-                        return r;
-        }
+        if (r < 0)
+                return r;
 
         return sd_bus_reply_method_return(m, "u", creds->uid);
 }
 
 static int driver_get_id(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
         sd_id128_t server_id;
         char buf[SD_ID128_STRING_MAX];
         int r;
@@ -183,7 +440,6 @@ static int driver_get_id(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_
 }
 
 static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
         char *arg0;
         int r;
@@ -192,67 +448,57 @@ static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata,
         if (r < 0)
                 return r;
 
+        assert_return(service_name_is_valid(arg0), -EINVAL);
+
         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_UNIQUE_NAME, &creds);
-        if (r < 0) {
-                if (r == -ENOENT)
-                        return driver_name_info_error(bus, m, arg0, r);
-                else
-                        return r;
-        }
+        if (r < 0)
+                return r;
 
         return sd_bus_reply_method_return(m, "s", creds->unique_name);
 }
 
 static int driver_hello(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
         return sd_bus_reply_method_return(m, "s", m->sender);
 }
 
-static int driver_list_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
+static int return_strv(sd_bus *bus, sd_bus_message *m, char **l) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-        _cleanup_strv_free_ char **names = NULL;
         int r;
 
-        r = sd_bus_list_names(bus, &names, NULL);
-        if (r < 0)
-                return r;
-
         r = sd_bus_message_new_method_return(m, &reply);
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_append_strv(reply, names);
+        r = sd_bus_message_append_strv(reply, l);
         if (r < 0)
                 return r;
 
         return sd_bus_send(bus, reply, NULL);
 }
 
-static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+static int driver_list_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
         _cleanup_strv_free_ char **names = NULL;
         int r;
 
-        r = sd_bus_list_names(bus, NULL, &names);
+        r = sd_bus_list_names(bus, &names, NULL);
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_new_method_return(m, &reply);
-        if (r < 0)
-                return r;
+        return return_strv(bus, m, names);
+}
 
-        r = sd_bus_message_append_strv(reply, names);
+static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+        _cleanup_strv_free_ char **names = NULL;
+        int r;
+
+        r = sd_bus_list_names(bus, NULL, &names);
         if (r < 0)
                 return r;
 
-        return sd_bus_send(bus, reply, NULL);
+        return return_strv(bus, m, names);
 }
 
 static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         struct kdbus_cmd_name_list cmd = {};
         struct kdbus_name_list *name_list;
         struct kdbus_cmd_name *name;
@@ -264,6 +510,8 @@ static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userd
         if (r < 0)
                 return r;
 
+        assert_return(service_name_is_valid(arg0), -EINVAL);
+
         cmd.flags = KDBUS_NAME_LIST_QUEUED;
 
         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
@@ -273,37 +521,32 @@ static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userd
         name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
 
         KDBUS_ITEM_FOREACH(name, name_list, names) {
-                if (name->size > sizeof(*name) && !streq(name->name, arg0)) {
-                        char *n;
+                char *n;
+
+                if (name->size <= sizeof(*name))
+                        continue;
 
-                        if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0)
-                                return -ENOMEM;
+                if (!streq(name->name, arg0))
+                        continue;
 
-                        r = strv_push(&owners, n);
-                        if (r < 0) {
-                                free(n);
-                                return -ENOMEM;
-                        }
+                if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0)
+                        return -ENOMEM;
+
+                r = strv_push(&owners, n);
+                if (r < 0) {
+                        free(n);
+                        return -ENOMEM;
                 }
         }
 
-        r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_FREE, &cmd.offset);
+        r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
         if (r < 0)
                 return -errno;
 
-        r = sd_bus_message_new_method_return(m, &reply);
-        if (r < 0)
-                return r;
-
-        r = sd_bus_message_append_strv(reply, owners);
-        if (r < 0)
-                return r;
-
-        return sd_bus_send(bus, reply, NULL);
+        return return_strv(bus, m, owners);
 }
 
 static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
         char *arg0;
         int r;
 
@@ -311,32 +554,36 @@ static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata,
         if (r < 0)
                 return r;
 
+        assert_return(service_name_is_valid(arg0), -EINVAL);
+
         r = sd_bus_get_owner(bus, arg0, 0, NULL);
         if (r < 0 && r != -ENOENT)
                 return r;
 
-        return sd_bus_reply_method_return(m, "b", r == 0);
+        return sd_bus_reply_method_return(m, "b", r >= 0);
 }
 
 static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
-
-        struct kdbus_cmd_name *cmd_name;
+        struct kdbus_cmd_name *n;
         uint32_t flags;
-        size_t size;
+        size_t size, l;
         uint64_t id;
-        char *name;
+        const char *name;
         int r;
 
         r = sd_bus_message_read(m, "su", &name, &flags);
         if (r < 0)
                 return r;
 
-        size = sizeof(*cmd_name) + strlen(name) + 1;
+        assert_return(service_name_is_valid(name), -EINVAL);
+        assert_return((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) == 0, -EINVAL);
 
-        cmd_name = alloca0(size);
-        strcpy(cmd_name->name, name);
-        cmd_name->size = size;
-        kdbus_translate_request_name_flags(flags, (uint64_t *) &cmd_name->conn_flags);
+        l = strlen(name);
+        size = offsetof(struct kdbus_cmd_name, name) + l + 1;
+        n = alloca0(size);
+        n->size = size;
+        memcpy(n->name, name, l+1);
+        kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
 
         /* This function is open-coded because we request the name 'on behalf'
          * of the requesting connection */
@@ -344,39 +591,119 @@ static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, s
         if (r < 0)
                 return r;
 
-        cmd_name->id = id;
+        n->id = id;
 
-        r = ioctl(sd_bus_get_fd(bus), KDBUS_CMD_NAME_ACQUIRE, cmd_name);
+        r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
         if (r < 0) {
                 if (errno == EEXIST)
                         return sd_bus_reply_method_return(m, "u", BUS_NAME_EXISTS);
-                else if (errno == EALREADY)
+                if (errno == EALREADY)
                         return sd_bus_reply_method_return(m, "u", BUS_NAME_ALREADY_OWNER);
 
                 return -errno;
         }
 
-        if (cmd_name->flags & KDBUS_NAME_IN_QUEUE)
+        if (n->flags & KDBUS_NAME_IN_QUEUE)
                 return sd_bus_reply_method_return(m, "u", BUS_NAME_IN_QUEUE);
 
         return sd_bus_reply_method_return(m, "u", BUS_NAME_PRIMARY_OWNER);
 }
 
-static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+static int driver_release_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+        struct kdbus_cmd_name *n;
+        const char *name;
+        size_t l, size;
+        uint64_t id;
+        int r;
+
+        r = sd_bus_message_read(m, "s", &name);
+        if (r < 0)
+                return r;
+
+        assert_return(service_name_is_valid(name), -EINVAL);
+
+        l = strlen(name);
+        size = offsetof(struct kdbus_cmd_name, name) + l + 1;
+        n = alloca0(size);
+        n->size = size;
+        memcpy(n->name, name, l+1);
+
+        /* This function is open-coded because we request the name 'on behalf'
+         * of the requesting connection */
+        r = bus_kernel_parse_unique_name(m->sender, &id);
+        if (r < 0)
+                return r;
+
+        n->id = id;
+
+        r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
+        if (r < 0) {
+                if (errno == ESRCH)
+                        return sd_bus_reply_method_return(m, "u", BUS_NAME_NON_EXISTENT);
+                if (errno == EADDRINUSE)
+                        return sd_bus_reply_method_return(m, "u", BUS_NAME_NOT_OWNER);
+                return -errno;
+        }
+
+        return sd_bus_reply_method_return(m, "u", BUS_NAME_RELEASED);
+}
 
+static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
-        char *s;
+        _cleanup_strv_free_ char **t = NULL;
+        _cleanup_free_ char *path = NULL;
+        uint32_t flags;
+        char *name, *u;
         int r;
 
-        return sd_bus_reply_method_return(m, "u", 2);
+        r = sd_bus_message_read(m, "su", &name, &flags);
+        if (r < 0)
+                return r;
+
+        assert_return(service_name_is_valid(name), -EINVAL);
+        assert_return(flags == 0, -ENOTSUP);
 
-        r = sd_bus_message_read(m, "s", &s);
+        r = sd_bus_get_owner(bus, name, 0, NULL);
+        if (r >= 0)
+                return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_ALREADY_RUNNING);
+        if (r != -ENOENT)
+                return r;
+
+        u = strappenda(name, ".busname");
+
+        path = unit_dbus_path_from_name(u);
+        if (!path)
+                return -ENOMEM;
+
+        r = sd_bus_get_property_strv(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        path,
+                        "org.freedesktop.systemd1.Unit",
+                        "Triggers",
+                        error,
+                        &t);
         if (r < 0)
                 return r;
 
-        /* FIXME */
+        if (!t[0] || t[1])
+                return -EIO;
+
+        r = sd_bus_call_method(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        "/org/freedesktop/systemd1",
+                        "org.freedesktop.systemd1.Manager",
+                        "StartUnit",
+                        error,
+                        &reply,
+                        "ss",
+                        t[0],
+                        "replace");
+        if (r < 0)
+                return r;
 
-        return sd_bus_send(bus, reply, NULL);
+        return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS);
 }
 
 static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
@@ -386,67 +713,68 @@ static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd
 static const sd_bus_vtable driver_vtable[] = {
         SD_BUS_VTABLE_START(0),
         SD_BUS_METHOD("AddMatch", "s", NULL, driver_add_match, 0),
-        SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_ctx, 0),
-        SD_BUS_METHOD("GetConnectionUnixProcessID", "s", "u", driver_get_pid, 0),
-        SD_BUS_METHOD("GetConnectionUnixUser", "s", "u", driver_get_user, 0),
-        SD_BUS_METHOD("GetId", NULL, "s", driver_get_id, 0),
-        SD_BUS_METHOD("GetNameOwner", "s", "s", driver_get_name_owner, 0),
-        SD_BUS_METHOD("Hello", NULL, "s", driver_hello, 0),
-        SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, 0),
-        SD_BUS_METHOD("ListNames", NULL, "as", driver_list_names, 0),
-        SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, 0),
-        SD_BUS_METHOD("NameHasOwner", "s", "b", driver_name_has_owner, 0),
-        SD_BUS_METHOD("ReloadConfig", NULL, NULL, driver_unsupported, 0),
-        SD_BUS_METHOD("RemoveMatch", "s", NULL, driver_remove_match, 0),
-        SD_BUS_METHOD("RequestName", "su", "u", driver_request_name, 0),
-        SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, 0),
-        SD_BUS_METHOD("UpdateActivationEnvironment", "a{ss}", NULL, driver_unsupported, 0),
-        SD_BUS_SIGNAL("NameAcquired", "s", 0),
-        SD_BUS_SIGNAL("NameLost", "s", 0),
+        SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_ctx, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("GetConnectionUnixProcessID", "s", "u", driver_get_pid, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("GetConnectionUnixUser", "s", "u", driver_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("GetId", NULL, "s", driver_get_id, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("GetNameOwner", "s", "s", driver_get_name_owner, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("Hello", NULL, "s", driver_hello, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ListNames", NULL, "as", driver_list_names, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("NameHasOwner", "s", "b", driver_name_has_owner, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ReleaseName", "s", "u", driver_release_name, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ReloadConfig", NULL, NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
+        SD_BUS_METHOD("RemoveMatch", "s", NULL, driver_remove_match, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("RequestName", "su", "u", driver_request_name, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("UpdateActivationEnvironment", "a{ss}", NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
+        SD_BUS_SIGNAL("NameAcquired", "s", SD_BUS_VTABLE_DEPRECATED),
+        SD_BUS_SIGNAL("NameLost", "s", SD_BUS_VTABLE_DEPRECATED),
         SD_BUS_SIGNAL("NameOwnerChanged", "sss", 0),
         SD_BUS_VTABLE_END
 };
 
-static int connect_bus(sd_event *event, sd_bus **_bus) {
-        _cleanup_bus_unref_ sd_bus *bus = NULL;
+static int connect_bus(Context *c) {
         int r;
 
-        assert(event);
-        assert(_bus);
+        assert(c);
 
-        r = sd_bus_default_system(&bus);
+        r = sd_bus_default_system(&c->bus);
         if (r < 0) {
                 log_error("Failed to create bus: %s", strerror(-r));
                 return r;
         }
 
-        r = sd_bus_add_object_vtable(bus, "/org/freedesktop/DBus", "org.freedesktop.DBus", driver_vtable, NULL);
+        if (!c->bus->is_kernel) {
+                log_error("Not running on kdbus");
+                return -EPERM;
+        }
+
+        r = sd_bus_add_object_vtable(c->bus, "/org/freedesktop/DBus", "org.freedesktop.DBus", driver_vtable, c);
         if (r < 0) {
                 log_error("Failed to add manager object vtable: %s", strerror(-r));
                 return r;
         }
 
-        r = sd_bus_request_name(bus, "org.freedesktop.DBus", 0);
+        r = sd_bus_request_name(c->bus, "org.freedesktop.DBus", 0);
         if (r < 0) {
                 log_error("Unable to request name: %s\n", strerror(-r));
                 return r;
         }
 
-        r = sd_bus_attach_event(bus, event, 0);
+        r = sd_bus_attach_event(c->bus, c->event, 0);
         if (r < 0) {
-                log_error("Error %d while adding bus to even: %s", r, strerror(-r));
+                log_error("Error while adding bus to event loop: %s", strerror(-r));
                 return r;
         }
 
-        *_bus = bus;
-        bus = NULL;
-
         return 0;
 }
 
 int main(int argc, char *argv[]) {
-        _cleanup_event_unref_ sd_event *event = NULL;
-        _cleanup_bus_unref_ sd_bus *bus = NULL;
+        Context context = {};
+        Client *c;
         int r;
 
         log_set_target(LOG_TARGET_AUTO);
@@ -459,25 +787,31 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        r = sd_event_default(&event);
+        r = sd_event_default(&context.event);
         if (r < 0) {
                 log_error("Failed to allocate event loop: %s", strerror(-r));
                 goto finish;
         }
 
-        sd_event_set_watchdog(event, true);
+        sd_event_set_watchdog(context.event, true);
 
-        r = connect_bus(event, &bus);
+        r = connect_bus(&context);
         if (r < 0)
                 goto finish;
 
-        r = bus_event_loop_with_idle(event, bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC);
+        r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC);
         if (r < 0) {
                 log_error("Failed to run event loop: %s", strerror(-r));
                 goto finish;
         }
 
 finish:
+        while ((c = hashmap_first(context.clients)))
+                client_free(c);
+
+        sd_bus_unref(context.bus);
+        sd_event_unref(context.event);
+
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 
 }