1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
10 #include "alloc-util.h"
11 #include "bus-internal.h"
12 #include "bus-track.h"
23 unsigned n_adding; /* are we in the process of adding a new name? */
25 sd_bus_track_handler_t handler;
28 LIST_FIELDS(sd_bus_track, queue);
30 bool in_list:1; /* In bus->tracks? */
31 bool in_queue:1; /* In bus->track_queue? */
34 sd_bus_destroy_t destroy_callback;
36 LIST_FIELDS(sd_bus_track, tracks);
39 #define MATCH_FOR_NAME(name) \
40 strjoina("type='signal'," \
41 "sender='org.freedesktop.DBus'," \
42 "path='/org/freedesktop/DBus'," \
43 "interface='org.freedesktop.DBus'," \
44 "member='NameOwnerChanged'," \
47 static struct track_item* track_item_free(struct track_item *i) {
52 sd_bus_slot_unref(i->slot);
57 DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_free);
59 static void bus_track_add_to_queue(sd_bus_track *track) {
62 /* Adds the bus track object to the queue of objects we should dispatch next, subject to a number of
65 /* Already in the queue? */
69 /* if we are currently in the process of adding a new name, then let's not enqueue this just yet, let's wait
70 * until the addition is complete. */
71 if (track->n_adding > 0)
74 /* still referenced? */
75 if (hashmap_size(track->names) > 0)
78 /* Nothing to call? */
86 LIST_PREPEND(queue, track->bus->track_queue, track);
87 track->in_queue = true;
90 static void bus_track_remove_from_queue(sd_bus_track *track) {
96 LIST_REMOVE(queue, track->bus->track_queue, track);
97 track->in_queue = false;
100 static int bus_track_remove_name_fully(sd_bus_track *track, const char *name) {
101 struct track_item *i;
106 i = hashmap_remove(track->names, name);
112 bus_track_add_to_queue(track);
114 track->modified = true;
118 _public_ int sd_bus_track_new(
120 sd_bus_track **track,
121 sd_bus_track_handler_t handler,
126 assert_return(bus, -EINVAL);
127 assert_return(bus = bus_resolve(bus), -ENOPKG);
128 assert_return(track, -EINVAL);
130 if (!bus->bus_client)
133 t = new0(sd_bus_track, 1);
138 t->handler = handler;
139 t->userdata = userdata;
140 t->bus = sd_bus_ref(bus);
142 LIST_PREPEND(tracks, bus->tracks, t);
145 bus_track_add_to_queue(t);
151 _public_ sd_bus_track* sd_bus_track_ref(sd_bus_track *track) {
156 assert(track->n_ref > 0);
163 _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) {
167 assert(track->n_ref > 0);
170 if (track->n_ref > 0)
174 LIST_REMOVE(tracks, track->bus->tracks, track);
176 bus_track_remove_from_queue(track);
177 track->names = hashmap_free_with_destructor(track->names, track_item_free);
178 track->bus = sd_bus_unref(track->bus);
180 if (track->destroy_callback)
181 track->destroy_callback(track->userdata);
186 static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
187 sd_bus_track *track = userdata;
188 const char *name, *old, *new;
194 r = sd_bus_message_read(message, "sss", &name, &old, &new);
198 bus_track_remove_name_fully(track, name);
202 _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
203 _cleanup_(track_item_freep) struct track_item *n = NULL;
204 struct track_item *i;
208 assert_return(track, -EINVAL);
209 assert_return(service_name_is_valid(name), -EINVAL);
211 i = hashmap_get(track->names, name);
213 if (track->recursive) {
214 unsigned k = track->n_ref + 1;
216 if (k < track->n_ref) /* Check for overflow */
222 bus_track_remove_from_queue(track);
226 r = hashmap_ensure_allocated(&track->names, &string_hash_ops);
230 n = new0(struct track_item, 1);
233 n->name = strdup(name);
237 /* First, subscribe to this name */
238 match = MATCH_FOR_NAME(name);
240 bus_track_remove_from_queue(track); /* don't dispatch this while we work in it */
242 r = sd_bus_add_match_async(track->bus, &n->slot, match, on_name_owner_changed, NULL, track);
244 bus_track_add_to_queue(track);
248 r = hashmap_put(track->names, n->name, n);
250 bus_track_add_to_queue(track);
254 /* Second, check if it is currently existing, or maybe doesn't, or maybe disappeared already. */
255 track->n_adding++; /* again, make sure this isn't dispatch while we are working in it */
256 r = sd_bus_get_name_creds(track->bus, name, 0, NULL);
259 hashmap_remove(track->names, name);
260 bus_track_add_to_queue(track);
267 bus_track_remove_from_queue(track);
268 track->modified = true;
273 _public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) {
274 struct track_item *i;
276 assert_return(name, -EINVAL);
278 if (!track) /* Treat a NULL track object as an empty track object */
281 if (!track->recursive)
282 return bus_track_remove_name_fully(track, name);
284 i = hashmap_get(track->names, name);
293 return bus_track_remove_name_fully(track, name);
298 _public_ unsigned sd_bus_track_count(sd_bus_track *track) {
300 if (!track) /* Let's consider a NULL object equivalent to an empty object */
303 /* This signature really should have returned an int, so that we can propagate errors. But well, ... Also, note
304 * that this returns the number of names being watched, and multiple references to the same name are not
307 return hashmap_size(track->names);
310 _public_ const char* sd_bus_track_contains(sd_bus_track *track, const char *name) {
311 assert_return(name, NULL);
313 if (!track) /* Let's consider a NULL object equivalent to an empty object */
316 return hashmap_get(track->names, (void*) name) ? name : NULL;
319 _public_ const char* sd_bus_track_first(sd_bus_track *track) {
320 const char *n = NULL;
325 track->modified = false;
326 track->iterator = ITERATOR_FIRST;
328 hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
332 _public_ const char* sd_bus_track_next(sd_bus_track *track) {
333 const char *n = NULL;
341 hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
345 _public_ int sd_bus_track_add_sender(sd_bus_track *track, sd_bus_message *m) {
348 assert_return(track, -EINVAL);
349 assert_return(m, -EINVAL);
351 if (sd_bus_message_get_bus(m) != track->bus)
354 sender = sd_bus_message_get_sender(m);
358 return sd_bus_track_add_name(track, sender);
361 _public_ int sd_bus_track_remove_sender(sd_bus_track *track, sd_bus_message *m) {
364 assert_return(m, -EINVAL);
366 if (!track) /* Treat a NULL track object as an empty track object */
369 if (sd_bus_message_get_bus(m) != track->bus)
372 sender = sd_bus_message_get_sender(m);
376 return sd_bus_track_remove_name(track, sender);
379 _public_ sd_bus* sd_bus_track_get_bus(sd_bus_track *track) {
380 assert_return(track, NULL);
385 void bus_track_dispatch(sd_bus_track *track) {
389 assert(track->handler);
391 bus_track_remove_from_queue(track);
393 sd_bus_track_ref(track);
395 r = track->handler(track, track->userdata);
397 log_debug_errno(r, "Failed to process track handler: %m");
399 bus_track_add_to_queue(track);
401 sd_bus_track_unref(track);
404 void bus_track_close(sd_bus_track *track) {
407 /* Called whenever our bus connected is closed. If so, and our track object is non-empty, dispatch it
408 * immediately, as we are closing now, but first flush out all names. */
411 return; /* We already closed this one, don't close it again. */
413 /* Remember that this one is closed now */
414 LIST_REMOVE(tracks, track->bus->tracks, track);
415 track->in_list = false;
417 /* If there's no name in this one anyway, we don't have to dispatch */
418 if (hashmap_isempty(track->names))
421 /* Let's flush out all names */
422 hashmap_clear_with_destructor(track->names, track_item_free);
426 bus_track_dispatch(track);
429 _public_ void *sd_bus_track_get_userdata(sd_bus_track *track) {
430 assert_return(track, NULL);
432 return track->userdata;
435 _public_ void *sd_bus_track_set_userdata(sd_bus_track *track, void *userdata) {
438 assert_return(track, NULL);
440 ret = track->userdata;
441 track->userdata = userdata;
446 _public_ int sd_bus_track_set_destroy_callback(sd_bus_track *track, sd_bus_destroy_t callback) {
447 assert_return(track, -EINVAL);
449 track->destroy_callback = callback;
453 _public_ int sd_bus_track_get_destroy_callback(sd_bus_track *track, sd_bus_destroy_t *ret) {
454 assert_return(track, -EINVAL);
457 *ret = track->destroy_callback;
459 return !!track->destroy_callback;
462 _public_ int sd_bus_track_set_recursive(sd_bus_track *track, int b) {
463 assert_return(track, -EINVAL);
465 if (track->recursive == !!b)
468 if (!hashmap_isempty(track->names))
471 track->recursive = b;
475 _public_ int sd_bus_track_get_recursive(sd_bus_track *track) {
476 assert_return(track, -EINVAL);
478 return track->recursive;
481 _public_ int sd_bus_track_count_sender(sd_bus_track *track, sd_bus_message *m) {
484 assert_return(m, -EINVAL);
486 if (!track) /* Let's consider a NULL object equivalent to an empty object */
489 if (sd_bus_message_get_bus(m) != track->bus)
492 sender = sd_bus_message_get_sender(m);
496 return sd_bus_track_count_name(track, sender);
499 _public_ int sd_bus_track_count_name(sd_bus_track *track, const char *name) {
500 struct track_item *i;
502 assert_return(service_name_is_valid(name), -EINVAL);
504 if (!track) /* Let's consider a NULL object equivalent to an empty object */
507 i = hashmap_get(track->names, name);