summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-12-19 03:02:45 +0100
committerLennart Poettering <lennart@poettering.net>2013-12-19 04:40:56 +0100
commit53461b74df0576ec091275d1a5dbee00611df1ee (patch)
treef81491e1f628a7848b9096c12006d5d3b79d7686
parentb43b8f7a7e088c31416374340f576887968f7cad (diff)
driverd: implement AddMatch/RemoveMatch logic
-rw-r--r--TODO4
-rw-r--r--src/bus-driverd/bus-driverd.c345
-rw-r--r--src/libsystemd-bus/bus-control.c18
-rw-r--r--src/libsystemd-bus/bus-control.h3
-rw-r--r--src/libsystemd-bus/bus-match.c40
-rw-r--r--src/libsystemd-bus/bus-match.h1
-rw-r--r--src/systemd/sd-bus-protocol.h2
7 files changed, 369 insertions, 44 deletions
diff --git a/TODO b/TODO
index 8025169033..2e3becc3df 100644
--- a/TODO
+++ b/TODO
@@ -113,17 +113,15 @@ Features:
* libsystemd-bus:
- when kdbus doesn't take our message without memfds, try again with memfds
- - implement translator service
- implement monitor logic
+ - implement support for byebye ioctl
- properly map matches with well-known names against messages with unique names
- when triggering property change events, allow a NULL strv indicate that all properties listed as such are send out as changed
- see if we can drop more message validation on the sending side
- support "const" properties as flag
- add API to clone sd_bus_message objects
- SD_BUS_COMMENT() macro for inclusion in vtables, syntax inspired by gdbus
- - sort out error codes for sd_bus_release_name(), distuingish: successful removal from foreign name, from non-existing name
- kdbus: matches against source or destination pids for an "strace -p"-like feel. Problem: The PID info needs to be available in userspace too...
- - kdbus: we need a way to distuingish messages we got due to monitoring from normal messages, since we want to bind methods only to the latter
- kdbus: when we do "systemctl daemon-reexec" the call doesn't get properly cancelled
- longer term:
* priority queues
diff --git a/src/bus-driverd/bus-driverd.c b/src/bus-driverd/bus-driverd.c
index f28dc5738e..d4126a99b6 100644
--- a/src/bus-driverd/bus-driverd.c
+++ b/src/bus-driverd/bus-driverd.c
@@ -50,38 +50,318 @@
#include "hashmap.h"
#include "def.h"
#include "unit-name.h"
+#include "bus-control.h"
-/*
- * TODO:
- *
- * AddMatch / RemoveMatch
- */
+#define CLIENTS_MAX 1024
+#define MATCHES_MAX 1024
-static int driver_add_match(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
+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) {
+
+ if (!m)
+ return;
+
+ if (m->client) {
+ Match *first;
+
+ 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--;
+ }
+
+ free(m->match);
+ free(m);
+}
+
+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++;
+
+ *_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;
+
+ 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;
+
+ c = new0(Client, 1);
+ if (!c)
+ return -ENOMEM;
+
+ c->id = id;
+
+ 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_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;
+
+ r = bus_kernel_parse_unique_name(message->sender, &id);
if (r < 0)
return r;
- /* FIXME */
+ 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);
+
+ match_free(m);
- return sd_bus_reply_method_return(m, NULL);
+ if (c->n_matches <= 0)
+ 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_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;
- 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;
+
+ r = bus_kernel_parse_unique_name(message->sender, &id);
if (r < 0)
return r;
- /* FIXME */
+ 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);
- return sd_bus_reply_method_return(m, NULL);
+ 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) {
@@ -455,51 +735,46 @@ static const sd_bus_vtable driver_vtable[] = {
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;
}
- if (!bus->is_kernel) {
+ if (!c->bus->is_kernel) {
log_error("Not running on kdbus");
return -EPERM;
}
- r = sd_bus_add_object_vtable(bus, "/org/freedesktop/DBus", "org.freedesktop.DBus", driver_vtable, NULL);
+ 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);
@@ -512,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;
}
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index b51007d1e2..b8f6360fb7 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -851,7 +851,7 @@ static int add_name_change_match(sd_bus *bus,
item = m->items;
item->size = offsetof(struct kdbus_item, id_change) + sizeof(struct kdbus_notify_id_change);
- item->id_change.id = name_id;
+ /* item->id_change.id = name_id; */
/* If the old name is unset or empty, then this can
* match against added ids */
@@ -877,9 +877,9 @@ static int add_name_change_match(sd_bus *bus,
return 0;
}
-static int bus_add_match_internal_kernel(
+int bus_add_match_internal_kernel(
sd_bus *bus,
- const char *match,
+ uint64_t id,
struct bus_match_component *components,
unsigned n_components,
uint64_t cookie) {
@@ -898,7 +898,6 @@ static int bus_add_match_internal_kernel(
int r;
assert(bus);
- assert(match);
zero(bloom);
@@ -1018,6 +1017,7 @@ static int bus_add_match_internal_kernel(
m->size = sz;
m->cookie = cookie;
m->src_id = src_id;
+ m->id = id;
item = m->items;
@@ -1084,25 +1084,25 @@ int bus_add_match_internal(
assert(match);
if (bus->is_kernel)
- return bus_add_match_internal_kernel(bus, match, components, n_components, cookie);
+ return bus_add_match_internal_kernel(bus, 0, components, n_components, cookie);
else
return bus_add_match_internal_dbus1(bus, match);
}
-static int bus_remove_match_internal_kernel(
+int bus_remove_match_internal_kernel(
sd_bus *bus,
- const char *match,
+ uint64_t id,
uint64_t cookie) {
struct kdbus_cmd_match m;
int r;
assert(bus);
- assert(match);
zero(m);
m.size = offsetof(struct kdbus_cmd_match, items);
m.cookie = cookie;
+ m.id = id;
r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
if (r < 0)
@@ -1139,7 +1139,7 @@ int bus_remove_match_internal(
assert(match);
if (bus->is_kernel)
- return bus_remove_match_internal_kernel(bus, match, cookie);
+ return bus_remove_match_internal_kernel(bus, 0, cookie);
else
return bus_remove_match_internal_dbus1(bus, match);
}
diff --git a/src/libsystemd-bus/bus-control.h b/src/libsystemd-bus/bus-control.h
index 2cac5d83ae..b610bef842 100644
--- a/src/libsystemd-bus/bus-control.h
+++ b/src/libsystemd-bus/bus-control.h
@@ -25,3 +25,6 @@
int bus_add_match_internal(sd_bus *bus, const char *match, struct bus_match_component *components, unsigned n_components, uint64_t cookie);
int bus_remove_match_internal(sd_bus *bus, const char *match, uint64_t cookie);
+
+int bus_add_match_internal_kernel(sd_bus *bus, uint64_t id, struct bus_match_component *components, unsigned n_components, uint64_t cookie);
+int bus_remove_match_internal_kernel(sd_bus *bus, uint64_t id, uint64_t cookie);
diff --git a/src/libsystemd-bus/bus-match.c b/src/libsystemd-bus/bus-match.c
index 342819d9c0..7638f2038b 100644
--- a/src/libsystemd-bus/bus-match.c
+++ b/src/libsystemd-bus/bus-match.c
@@ -816,6 +816,46 @@ fail:
return r;
}
+char *bus_match_to_string(struct bus_match_component *components, unsigned n_components) {
+ _cleanup_free_ FILE *f = NULL;
+ char *buffer = NULL;
+ size_t size = 0;
+ unsigned i;
+
+ if (n_components <= 0)
+ return strdup("");
+
+ assert(components);
+
+ f = open_memstream(&buffer, &size);
+ if (!f)
+ return NULL;
+
+ for (i = 0; i < n_components; i++) {
+ char buf[32];
+
+ if (i != 0)
+ fputc(',', f);
+
+ fputs(bus_match_node_type_to_string(components[i].type, buf, sizeof(buf)), f);
+ fputc('=', f);
+ fputc('\'', f);
+
+ if (components[i].type == BUS_MATCH_MESSAGE_TYPE)
+ fputs(bus_message_type_to_string(components[i].value_u8), f);
+ else
+ fputs(components[i].value_str, f);
+
+ fputc('\'', f);
+ }
+
+ fflush(f);
+ if (ferror(f))
+ return NULL;
+
+ return buffer;
+}
+
int bus_match_add(
struct bus_match_node *root,
struct bus_match_component *components,
diff --git a/src/libsystemd-bus/bus-match.h b/src/libsystemd-bus/bus-match.h
index 1d38126600..056082b908 100644
--- a/src/libsystemd-bus/bus-match.h
+++ b/src/libsystemd-bus/bus-match.h
@@ -90,3 +90,4 @@ enum bus_match_node_type bus_match_node_type_from_string(const char *k, size_t n
int bus_match_parse(const char *match, struct bus_match_component **_components, unsigned *_n_components);
void bus_match_parse_free(struct bus_match_component *components, unsigned n_components);
+char *bus_match_to_string(struct bus_match_component *components, unsigned n_components);
diff --git a/src/systemd/sd-bus-protocol.h b/src/systemd/sd-bus-protocol.h
index 1967fa8f04..439a77961e 100644
--- a/src/systemd/sd-bus-protocol.h
+++ b/src/systemd/sd-bus-protocol.h
@@ -94,6 +94,8 @@ enum {
#define SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN "org.freedesktop.DBus.Error.UnixProcessIdUnknown"
#define SD_BUS_ERROR_INVALID_SIGNATURE "org.freedesktop.DBus.Error.InvalidSignature"
#define SD_BUS_ERROR_INCONSISTENT_MESSAGE "org.freedesktop.DBus.Error.InconsistentMessage"
+#define SD_BUS_ERROR_MATCH_RULE_NOT_FOUND "org.freedesktop.DBus.Error.MatchRuleNotFound"
+#define SD_BUS_ERROR_MATCH_RULE_INVALID "org.freedesktop.DBus.Error.MatchRuleInvalid"
_SD_END_DECLARATIONS;