+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;