1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bus-internal.h"
18 unsigned n_adding
; /* are we in the process of adding a new name? */
20 sd_bus_track_handler_t handler
;
23 LIST_FIELDS(sd_bus_track
, queue
);
25 bool in_list
:1; /* In bus->tracks? */
26 bool in_queue
:1; /* In bus->track_queue? */
29 sd_bus_destroy_t destroy_callback
;
31 LIST_FIELDS(sd_bus_track
, tracks
);
34 #define MATCH_FOR_NAME(name) \
35 strjoina("type='signal'," \
36 "sender='org.freedesktop.DBus'," \
37 "path='/org/freedesktop/DBus'," \
38 "interface='org.freedesktop.DBus'," \
39 "member='NameOwnerChanged'," \
42 static struct track_item
* track_item_free(struct track_item
*i
) {
47 sd_bus_slot_unref(i
->slot
);
52 DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item
*, track_item_free
);
54 static void bus_track_add_to_queue(sd_bus_track
*track
) {
57 /* Adds the bus track object to the queue of objects we should dispatch next, subject to a number of
60 /* Already in the queue? */
64 /* if we are currently in the process of adding a new name, then let's not enqueue this just yet, let's wait
65 * until the addition is complete. */
66 if (track
->n_adding
> 0)
69 /* still referenced? */
70 if (hashmap_size(track
->names
) > 0)
73 /* Nothing to call? */
81 LIST_PREPEND(queue
, track
->bus
->track_queue
, track
);
82 track
->in_queue
= true;
85 static void bus_track_remove_from_queue(sd_bus_track
*track
) {
91 LIST_REMOVE(queue
, track
->bus
->track_queue
, track
);
92 track
->in_queue
= false;
95 static int bus_track_remove_name_fully(sd_bus_track
*track
, const char *name
) {
101 i
= hashmap_remove(track
->names
, name
);
107 bus_track_add_to_queue(track
);
109 track
->modified
= true;
113 _public_
int sd_bus_track_new(
115 sd_bus_track
**track
,
116 sd_bus_track_handler_t handler
,
121 assert_return(bus
, -EINVAL
);
122 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
123 assert_return(track
, -EINVAL
);
125 if (!bus
->bus_client
)
128 t
= new0(sd_bus_track
, 1);
133 t
->handler
= handler
;
134 t
->userdata
= userdata
;
135 t
->bus
= sd_bus_ref(bus
);
137 LIST_PREPEND(tracks
, bus
->tracks
, t
);
140 bus_track_add_to_queue(t
);
146 static sd_bus_track
*track_free(sd_bus_track
*track
) {
150 LIST_REMOVE(tracks
, track
->bus
->tracks
, track
);
152 bus_track_remove_from_queue(track
);
153 track
->names
= hashmap_free_with_destructor(track
->names
, track_item_free
);
154 track
->bus
= sd_bus_unref(track
->bus
);
156 if (track
->destroy_callback
)
157 track
->destroy_callback(track
->userdata
);
162 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_track
, sd_bus_track
, track_free
);
164 static int on_name_owner_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
165 sd_bus_track
*track
= userdata
;
166 const char *name
, *old
, *new;
172 r
= sd_bus_message_read(message
, "sss", &name
, &old
, &new);
176 bus_track_remove_name_fully(track
, name
);
180 _public_
int sd_bus_track_add_name(sd_bus_track
*track
, const char *name
) {
181 _cleanup_(track_item_freep
) struct track_item
*n
= NULL
;
182 struct track_item
*i
;
186 assert_return(track
, -EINVAL
);
187 assert_return(service_name_is_valid(name
), -EINVAL
);
189 i
= hashmap_get(track
->names
, name
);
191 if (track
->recursive
) {
192 unsigned k
= track
->n_ref
+ 1;
194 if (k
< track
->n_ref
) /* Check for overflow */
200 bus_track_remove_from_queue(track
);
204 r
= hashmap_ensure_allocated(&track
->names
, &string_hash_ops
);
208 n
= new0(struct track_item
, 1);
211 n
->name
= strdup(name
);
215 /* First, subscribe to this name */
216 match
= MATCH_FOR_NAME(name
);
218 bus_track_remove_from_queue(track
); /* don't dispatch this while we work in it */
220 r
= sd_bus_add_match_async(track
->bus
, &n
->slot
, match
, on_name_owner_changed
, NULL
, track
);
222 bus_track_add_to_queue(track
);
226 r
= hashmap_put(track
->names
, n
->name
, n
);
228 bus_track_add_to_queue(track
);
232 /* Second, check if it is currently existing, or maybe doesn't, or maybe disappeared already. */
233 track
->n_adding
++; /* again, make sure this isn't dispatch while we are working in it */
234 r
= sd_bus_get_name_creds(track
->bus
, name
, 0, NULL
);
237 hashmap_remove(track
->names
, name
);
238 bus_track_add_to_queue(track
);
245 bus_track_remove_from_queue(track
);
246 track
->modified
= true;
251 _public_
int sd_bus_track_remove_name(sd_bus_track
*track
, const char *name
) {
252 struct track_item
*i
;
254 assert_return(name
, -EINVAL
);
256 if (!track
) /* Treat a NULL track object as an empty track object */
259 if (!track
->recursive
)
260 return bus_track_remove_name_fully(track
, name
);
262 i
= hashmap_get(track
->names
, name
);
271 return bus_track_remove_name_fully(track
, name
);
276 _public_
unsigned sd_bus_track_count(sd_bus_track
*track
) {
278 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
281 /* This signature really should have returned an int, so that we can propagate errors. But well, ... Also, note
282 * that this returns the number of names being watched, and multiple references to the same name are not
285 return hashmap_size(track
->names
);
288 _public_
const char* sd_bus_track_contains(sd_bus_track
*track
, const char *name
) {
289 assert_return(name
, NULL
);
291 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
294 return hashmap_get(track
->names
, (void*) name
) ? name
: NULL
;
297 _public_
const char* sd_bus_track_first(sd_bus_track
*track
) {
298 const char *n
= NULL
;
303 track
->modified
= false;
304 track
->iterator
= ITERATOR_FIRST
;
306 hashmap_iterate(track
->names
, &track
->iterator
, NULL
, (const void**) &n
);
310 _public_
const char* sd_bus_track_next(sd_bus_track
*track
) {
311 const char *n
= NULL
;
319 hashmap_iterate(track
->names
, &track
->iterator
, NULL
, (const void**) &n
);
323 _public_
int sd_bus_track_add_sender(sd_bus_track
*track
, sd_bus_message
*m
) {
326 assert_return(track
, -EINVAL
);
327 assert_return(m
, -EINVAL
);
329 if (sd_bus_message_get_bus(m
) != track
->bus
)
332 sender
= sd_bus_message_get_sender(m
);
336 return sd_bus_track_add_name(track
, sender
);
339 _public_
int sd_bus_track_remove_sender(sd_bus_track
*track
, sd_bus_message
*m
) {
342 assert_return(m
, -EINVAL
);
344 if (!track
) /* Treat a NULL track object as an empty track object */
347 if (sd_bus_message_get_bus(m
) != track
->bus
)
350 sender
= sd_bus_message_get_sender(m
);
354 return sd_bus_track_remove_name(track
, sender
);
357 _public_ sd_bus
* sd_bus_track_get_bus(sd_bus_track
*track
) {
358 assert_return(track
, NULL
);
363 void bus_track_dispatch(sd_bus_track
*track
) {
367 assert(track
->handler
);
369 bus_track_remove_from_queue(track
);
371 sd_bus_track_ref(track
);
373 r
= track
->handler(track
, track
->userdata
);
375 log_debug_errno(r
, "Failed to process track handler: %m");
377 bus_track_add_to_queue(track
);
379 sd_bus_track_unref(track
);
382 void bus_track_close(sd_bus_track
*track
) {
385 /* Called whenever our bus connected is closed. If so, and our track object is non-empty, dispatch it
386 * immediately, as we are closing now, but first flush out all names. */
389 return; /* We already closed this one, don't close it again. */
391 /* Remember that this one is closed now */
392 LIST_REMOVE(tracks
, track
->bus
->tracks
, track
);
393 track
->in_list
= false;
395 /* If there's no name in this one anyway, we don't have to dispatch */
396 if (hashmap_isempty(track
->names
))
399 /* Let's flush out all names */
400 hashmap_clear_with_destructor(track
->names
, track_item_free
);
404 bus_track_dispatch(track
);
407 _public_
void *sd_bus_track_get_userdata(sd_bus_track
*track
) {
408 assert_return(track
, NULL
);
410 return track
->userdata
;
413 _public_
void *sd_bus_track_set_userdata(sd_bus_track
*track
, void *userdata
) {
416 assert_return(track
, NULL
);
418 ret
= track
->userdata
;
419 track
->userdata
= userdata
;
424 _public_
int sd_bus_track_set_destroy_callback(sd_bus_track
*track
, sd_bus_destroy_t callback
) {
425 assert_return(track
, -EINVAL
);
427 track
->destroy_callback
= callback
;
431 _public_
int sd_bus_track_get_destroy_callback(sd_bus_track
*track
, sd_bus_destroy_t
*ret
) {
432 assert_return(track
, -EINVAL
);
435 *ret
= track
->destroy_callback
;
437 return !!track
->destroy_callback
;
440 _public_
int sd_bus_track_set_recursive(sd_bus_track
*track
, int b
) {
441 assert_return(track
, -EINVAL
);
443 if (track
->recursive
== !!b
)
446 if (!hashmap_isempty(track
->names
))
449 track
->recursive
= b
;
453 _public_
int sd_bus_track_get_recursive(sd_bus_track
*track
) {
454 assert_return(track
, -EINVAL
);
456 return track
->recursive
;
459 _public_
int sd_bus_track_count_sender(sd_bus_track
*track
, sd_bus_message
*m
) {
462 assert_return(m
, -EINVAL
);
464 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
467 if (sd_bus_message_get_bus(m
) != track
->bus
)
470 sender
= sd_bus_message_get_sender(m
);
474 return sd_bus_track_count_name(track
, sender
);
477 _public_
int sd_bus_track_count_name(sd_bus_track
*track
, const char *name
) {
478 struct track_item
*i
;
480 assert_return(service_name_is_valid(name
), -EINVAL
);
482 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
485 i
= hashmap_get(track
->names
, name
);