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
);
53 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(track_item_hash_ops
, char, string_hash_func
, string_compare_func
,
54 struct track_item
, track_item_free
);
56 static void bus_track_add_to_queue(sd_bus_track
*track
) {
59 /* Adds the bus track object to the queue of objects we should dispatch next, subject to a number of
62 /* Already in the queue? */
66 /* if we are currently in the process of adding a new name, then let's not enqueue this just yet, let's wait
67 * until the addition is complete. */
68 if (track
->n_adding
> 0)
71 /* still referenced? */
72 if (hashmap_size(track
->names
) > 0)
75 /* Nothing to call? */
83 LIST_PREPEND(queue
, track
->bus
->track_queue
, track
);
84 track
->in_queue
= true;
87 static void bus_track_remove_from_queue(sd_bus_track
*track
) {
93 LIST_REMOVE(queue
, track
->bus
->track_queue
, track
);
94 track
->in_queue
= false;
97 static int bus_track_remove_name_fully(sd_bus_track
*track
, const char *name
) {
103 i
= hashmap_remove(track
->names
, name
);
109 bus_track_add_to_queue(track
);
111 track
->modified
= true;
115 _public_
int sd_bus_track_new(
117 sd_bus_track
**track
,
118 sd_bus_track_handler_t handler
,
123 assert_return(bus
, -EINVAL
);
124 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
125 assert_return(track
, -EINVAL
);
127 if (!bus
->bus_client
)
130 t
= new0(sd_bus_track
, 1);
135 t
->handler
= handler
;
136 t
->userdata
= userdata
;
137 t
->bus
= sd_bus_ref(bus
);
139 LIST_PREPEND(tracks
, bus
->tracks
, t
);
142 bus_track_add_to_queue(t
);
148 static sd_bus_track
*track_free(sd_bus_track
*track
) {
152 LIST_REMOVE(tracks
, track
->bus
->tracks
, track
);
154 bus_track_remove_from_queue(track
);
155 track
->names
= hashmap_free(track
->names
);
156 track
->bus
= sd_bus_unref(track
->bus
);
158 if (track
->destroy_callback
)
159 track
->destroy_callback(track
->userdata
);
164 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_track
, sd_bus_track
, track_free
);
166 static int on_name_owner_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
167 sd_bus_track
*track
= userdata
;
168 const char *name
, *old
, *new;
174 r
= sd_bus_message_read(message
, "sss", &name
, &old
, &new);
178 bus_track_remove_name_fully(track
, name
);
182 _public_
int sd_bus_track_add_name(sd_bus_track
*track
, const char *name
) {
183 _cleanup_(track_item_freep
) struct track_item
*n
= NULL
;
184 struct track_item
*i
;
188 assert_return(track
, -EINVAL
);
189 assert_return(service_name_is_valid(name
), -EINVAL
);
191 i
= hashmap_get(track
->names
, name
);
193 if (track
->recursive
) {
194 unsigned k
= track
->n_ref
+ 1;
196 if (k
< track
->n_ref
) /* Check for overflow */
202 bus_track_remove_from_queue(track
);
206 r
= hashmap_ensure_allocated(&track
->names
, &track_item_hash_ops
);
210 n
= new0(struct track_item
, 1);
213 n
->name
= strdup(name
);
217 /* First, subscribe to this name */
218 match
= MATCH_FOR_NAME(name
);
220 bus_track_remove_from_queue(track
); /* don't dispatch this while we work in it */
222 r
= sd_bus_add_match_async(track
->bus
, &n
->slot
, match
, on_name_owner_changed
, NULL
, track
);
224 bus_track_add_to_queue(track
);
228 r
= hashmap_put(track
->names
, n
->name
, n
);
230 bus_track_add_to_queue(track
);
234 /* Second, check if it is currently existing, or maybe doesn't, or maybe disappeared already. */
235 track
->n_adding
++; /* again, make sure this isn't dispatch while we are working in it */
236 r
= sd_bus_get_name_creds(track
->bus
, name
, 0, NULL
);
239 hashmap_remove(track
->names
, name
);
240 bus_track_add_to_queue(track
);
247 bus_track_remove_from_queue(track
);
248 track
->modified
= true;
253 _public_
int sd_bus_track_remove_name(sd_bus_track
*track
, const char *name
) {
254 struct track_item
*i
;
256 assert_return(name
, -EINVAL
);
258 if (!track
) /* Treat a NULL track object as an empty track object */
261 if (!track
->recursive
)
262 return bus_track_remove_name_fully(track
, name
);
264 i
= hashmap_get(track
->names
, name
);
273 return bus_track_remove_name_fully(track
, name
);
278 _public_
unsigned sd_bus_track_count(sd_bus_track
*track
) {
280 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
283 /* This signature really should have returned an int, so that we can propagate errors. But well, ... Also, note
284 * that this returns the number of names being watched, and multiple references to the same name are not
287 return hashmap_size(track
->names
);
290 _public_
const char* sd_bus_track_contains(sd_bus_track
*track
, const char *name
) {
291 assert_return(name
, NULL
);
293 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
296 return hashmap_get(track
->names
, (void*) name
) ? name
: NULL
;
299 _public_
const char* sd_bus_track_first(sd_bus_track
*track
) {
300 const char *n
= NULL
;
305 track
->modified
= false;
306 track
->iterator
= ITERATOR_FIRST
;
308 hashmap_iterate(track
->names
, &track
->iterator
, NULL
, (const void**) &n
);
312 _public_
const char* sd_bus_track_next(sd_bus_track
*track
) {
313 const char *n
= NULL
;
321 hashmap_iterate(track
->names
, &track
->iterator
, NULL
, (const void**) &n
);
325 _public_
int sd_bus_track_add_sender(sd_bus_track
*track
, sd_bus_message
*m
) {
328 assert_return(track
, -EINVAL
);
329 assert_return(m
, -EINVAL
);
331 if (sd_bus_message_get_bus(m
) != track
->bus
)
334 sender
= sd_bus_message_get_sender(m
);
338 return sd_bus_track_add_name(track
, sender
);
341 _public_
int sd_bus_track_remove_sender(sd_bus_track
*track
, sd_bus_message
*m
) {
344 assert_return(m
, -EINVAL
);
346 if (!track
) /* Treat a NULL track object as an empty track object */
349 if (sd_bus_message_get_bus(m
) != track
->bus
)
352 sender
= sd_bus_message_get_sender(m
);
356 return sd_bus_track_remove_name(track
, sender
);
359 _public_ sd_bus
* sd_bus_track_get_bus(sd_bus_track
*track
) {
360 assert_return(track
, NULL
);
365 void bus_track_dispatch(sd_bus_track
*track
) {
369 assert(track
->handler
);
371 bus_track_remove_from_queue(track
);
373 sd_bus_track_ref(track
);
375 r
= track
->handler(track
, track
->userdata
);
377 log_debug_errno(r
, "Failed to process track handler: %m");
379 bus_track_add_to_queue(track
);
381 sd_bus_track_unref(track
);
384 void bus_track_close(sd_bus_track
*track
) {
387 /* Called whenever our bus connected is closed. If so, and our track object is non-empty, dispatch it
388 * immediately, as we are closing now, but first flush out all names. */
391 return; /* We already closed this one, don't close it again. */
393 /* Remember that this one is closed now */
394 LIST_REMOVE(tracks
, track
->bus
->tracks
, track
);
395 track
->in_list
= false;
397 /* If there's no name in this one anyway, we don't have to dispatch */
398 if (hashmap_isempty(track
->names
))
401 /* Let's flush out all names */
402 hashmap_clear(track
->names
);
406 bus_track_dispatch(track
);
409 _public_
void *sd_bus_track_get_userdata(sd_bus_track
*track
) {
410 assert_return(track
, NULL
);
412 return track
->userdata
;
415 _public_
void *sd_bus_track_set_userdata(sd_bus_track
*track
, void *userdata
) {
418 assert_return(track
, NULL
);
420 ret
= track
->userdata
;
421 track
->userdata
= userdata
;
426 _public_
int sd_bus_track_set_destroy_callback(sd_bus_track
*track
, sd_bus_destroy_t callback
) {
427 assert_return(track
, -EINVAL
);
429 track
->destroy_callback
= callback
;
433 _public_
int sd_bus_track_get_destroy_callback(sd_bus_track
*track
, sd_bus_destroy_t
*ret
) {
434 assert_return(track
, -EINVAL
);
437 *ret
= track
->destroy_callback
;
439 return !!track
->destroy_callback
;
442 _public_
int sd_bus_track_set_recursive(sd_bus_track
*track
, int b
) {
443 assert_return(track
, -EINVAL
);
445 if (track
->recursive
== !!b
)
448 if (!hashmap_isempty(track
->names
))
451 track
->recursive
= b
;
455 _public_
int sd_bus_track_get_recursive(sd_bus_track
*track
) {
456 assert_return(track
, -EINVAL
);
458 return track
->recursive
;
461 _public_
int sd_bus_track_count_sender(sd_bus_track
*track
, sd_bus_message
*m
) {
464 assert_return(m
, -EINVAL
);
466 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
469 if (sd_bus_message_get_bus(m
) != track
->bus
)
472 sender
= sd_bus_message_get_sender(m
);
476 return sd_bus_track_count_name(track
, sender
);
479 _public_
int sd_bus_track_count_name(sd_bus_track
*track
, const char *name
) {
480 struct track_item
*i
;
482 assert_return(service_name_is_valid(name
), -EINVAL
);
484 if (!track
) /* Let's consider a NULL object equivalent to an empty object */
487 i
= hashmap_get(track
->names
, name
);