1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
8 #include "alloc-util.h"
9 #include "bus-internal.h"
10 #include "bus-track.h"
21 unsigned n_adding; /* are we in the process of adding a new name? */
23 sd_bus_track_handler_t handler;
26 LIST_FIELDS(sd_bus_track, queue);
28 bool in_list:1; /* In bus->tracks? */
29 bool in_queue:1; /* In bus->track_queue? */
32 sd_bus_destroy_t destroy_callback;
34 LIST_FIELDS(sd_bus_track, tracks);
37 #define MATCH_FOR_NAME(name) \
38 strjoina("type='signal'," \
39 "sender='org.freedesktop.DBus'," \
40 "path='/org/freedesktop/DBus'," \
41 "interface='org.freedesktop.DBus'," \
42 "member='NameOwnerChanged'," \
45 static struct track_item* track_item_free(struct track_item *i) {
50 sd_bus_slot_unref(i->slot);
55 DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_free);
57 static void bus_track_add_to_queue(sd_bus_track *track) {
60 /* Adds the bus track object to the queue of objects we should dispatch next, subject to a number of
63 /* Already in the queue? */
67 /* if we are currently in the process of adding a new name, then let's not enqueue this just yet, let's wait
68 * until the addition is complete. */
69 if (track->n_adding > 0)
72 /* still referenced? */
73 if (hashmap_size(track->names) > 0)
76 /* Nothing to call? */
84 LIST_PREPEND(queue, track->bus->track_queue, track);
85 track->in_queue = true;
88 static void bus_track_remove_from_queue(sd_bus_track *track) {
94 LIST_REMOVE(queue, track->bus->track_queue, track);
95 track->in_queue = false;
98 static int bus_track_remove_name_fully(sd_bus_track *track, const char *name) {
104 i = hashmap_remove(track->names, name);
110 bus_track_add_to_queue(track);
112 track->modified = true;
116 _public_ int sd_bus_track_new(
118 sd_bus_track **track,
119 sd_bus_track_handler_t handler,
124 assert_return(bus, -EINVAL);
125 assert_return(bus = bus_resolve(bus), -ENOPKG);
126 assert_return(track, -EINVAL);
128 if (!bus->bus_client)
131 t = new0(sd_bus_track, 1);
136 t->handler = handler;
137 t->userdata = userdata;
138 t->bus = sd_bus_ref(bus);
140 LIST_PREPEND(tracks, bus->tracks, t);
143 bus_track_add_to_queue(t);
149 _public_ sd_bus_track* sd_bus_track_ref(sd_bus_track *track) {
154 assert(track->n_ref > 0);
161 _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) {
165 assert(track->n_ref > 0);
168 if (track->n_ref > 0)
172 LIST_REMOVE(tracks, track->bus->tracks, track);
174 bus_track_remove_from_queue(track);
175 track->names = hashmap_free_with_destructor(track->names, track_item_free);
176 track->bus = sd_bus_unref(track->bus);
178 if (track->destroy_callback)
179 track->destroy_callback(track->userdata);
184 static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
185 sd_bus_track *track = userdata;
186 const char *name, *old, *new;
192 r = sd_bus_message_read(message, "sss", &name, &old, &new);
196 bus_track_remove_name_fully(track, name);
200 _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
201 _cleanup_(track_item_freep) struct track_item *n = NULL;
202 struct track_item *i;
206 assert_return(track, -EINVAL);
207 assert_return(service_name_is_valid(name), -EINVAL);
209 i = hashmap_get(track->names, name);
211 if (track->recursive) {
212 unsigned k = track->n_ref + 1;
214 if (k < track->n_ref) /* Check for overflow */
220 bus_track_remove_from_queue(track);
224 r = hashmap_ensure_allocated(&track->names, &string_hash_ops);
228 n = new0(struct track_item, 1);
231 n->name = strdup(name);
235 /* First, subscribe to this name */
236 match = MATCH_FOR_NAME(name);
238 bus_track_remove_from_queue(track); /* don't dispatch this while we work in it */
240 r = sd_bus_add_match_async(track->bus, &n->slot, match, on_name_owner_changed, NULL, track);
242 bus_track_add_to_queue(track);
246 r = hashmap_put(track->names, n->name, n);
248 bus_track_add_to_queue(track);
252 /* Second, check if it is currently existing, or maybe doesn't, or maybe disappeared already. */
253 track->n_adding++; /* again, make sure this isn't dispatch while we are working in it */
254 r = sd_bus_get_name_creds(track->bus, name, 0, NULL);
257 hashmap_remove(track->names, name);
258 bus_track_add_to_queue(track);
265 bus_track_remove_from_queue(track);
266 track->modified = true;
271 _public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) {
272 struct track_item *i;
274 assert_return(name, -EINVAL);
276 if (!track) /* Treat a NULL track object as an empty track object */
279 if (!track->recursive)
280 return bus_track_remove_name_fully(track, name);
282 i = hashmap_get(track->names, name);
291 return bus_track_remove_name_fully(track, name);
296 _public_ unsigned sd_bus_track_count(sd_bus_track *track) {
298 if (!track) /* Let's consider a NULL object equivalent to an empty object */
301 /* This signature really should have returned an int, so that we can propagate errors. But well, ... Also, note
302 * that this returns the number of names being watched, and multiple references to the same name are not
305 return hashmap_size(track->names);
308 _public_ const char* sd_bus_track_contains(sd_bus_track *track, const char *name) {
309 assert_return(name, NULL);
311 if (!track) /* Let's consider a NULL object equivalent to an empty object */
314 return hashmap_get(track->names, (void*) name) ? name : NULL;
317 _public_ const char* sd_bus_track_first(sd_bus_track *track) {
318 const char *n = NULL;
323 track->modified = false;
324 track->iterator = ITERATOR_FIRST;
326 hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
330 _public_ const char* sd_bus_track_next(sd_bus_track *track) {
331 const char *n = NULL;
339 hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
343 _public_ int sd_bus_track_add_sender(sd_bus_track *track, sd_bus_message *m) {
346 assert_return(track, -EINVAL);
347 assert_return(m, -EINVAL);
349 if (sd_bus_message_get_bus(m) != track->bus)
352 sender = sd_bus_message_get_sender(m);
356 return sd_bus_track_add_name(track, sender);
359 _public_ int sd_bus_track_remove_sender(sd_bus_track *track, sd_bus_message *m) {
362 assert_return(m, -EINVAL);
364 if (!track) /* Treat a NULL track object as an empty track object */
367 if (sd_bus_message_get_bus(m) != track->bus)
370 sender = sd_bus_message_get_sender(m);
374 return sd_bus_track_remove_name(track, sender);
377 _public_ sd_bus* sd_bus_track_get_bus(sd_bus_track *track) {
378 assert_return(track, NULL);
383 void bus_track_dispatch(sd_bus_track *track) {
387 assert(track->handler);
389 bus_track_remove_from_queue(track);
391 sd_bus_track_ref(track);
393 r = track->handler(track, track->userdata);
395 log_debug_errno(r, "Failed to process track handler: %m");
397 bus_track_add_to_queue(track);
399 sd_bus_track_unref(track);
402 void bus_track_close(sd_bus_track *track) {
405 /* Called whenever our bus connected is closed. If so, and our track object is non-empty, dispatch it
406 * immediately, as we are closing now, but first flush out all names. */
409 return; /* We already closed this one, don't close it again. */
411 /* Remember that this one is closed now */
412 LIST_REMOVE(tracks, track->bus->tracks, track);
413 track->in_list = false;
415 /* If there's no name in this one anyway, we don't have to dispatch */
416 if (hashmap_isempty(track->names))
419 /* Let's flush out all names */
420 hashmap_clear_with_destructor(track->names, track_item_free);
424 bus_track_dispatch(track);
427 _public_ void *sd_bus_track_get_userdata(sd_bus_track *track) {
428 assert_return(track, NULL);
430 return track->userdata;
433 _public_ void *sd_bus_track_set_userdata(sd_bus_track *track, void *userdata) {
436 assert_return(track, NULL);
438 ret = track->userdata;
439 track->userdata = userdata;
444 _public_ int sd_bus_track_set_destroy_callback(sd_bus_track *track, sd_bus_destroy_t callback) {
445 assert_return(track, -EINVAL);
447 track->destroy_callback = callback;
451 _public_ int sd_bus_track_get_destroy_callback(sd_bus_track *track, sd_bus_destroy_t *ret) {
452 assert_return(track, -EINVAL);
455 *ret = track->destroy_callback;
457 return !!track->destroy_callback;
460 _public_ int sd_bus_track_set_recursive(sd_bus_track *track, int b) {
461 assert_return(track, -EINVAL);
463 if (track->recursive == !!b)
466 if (!hashmap_isempty(track->names))
469 track->recursive = b;
473 _public_ int sd_bus_track_get_recursive(sd_bus_track *track) {
474 assert_return(track, -EINVAL);
476 return track->recursive;
479 _public_ int sd_bus_track_count_sender(sd_bus_track *track, sd_bus_message *m) {
482 assert_return(m, -EINVAL);
484 if (!track) /* Let's consider a NULL object equivalent to an empty object */
487 if (sd_bus_message_get_bus(m) != track->bus)
490 sender = sd_bus_message_get_sender(m);
494 return sd_bus_track_count_name(track, sender);
497 _public_ int sd_bus_track_count_name(sd_bus_track *track, const char *name) {
498 struct track_item *i;
500 assert_return(service_name_is_valid(name), -EINVAL);
502 if (!track) /* Let's consider a NULL object equivalent to an empty object */
505 i = hashmap_get(track->names, name);