2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "bus-internal.h"
24 #include "bus-track.h"
35 unsigned n_adding; /* are we in the process of adding a new name? */
37 sd_bus_track_handler_t handler;
40 LIST_FIELDS(sd_bus_track, queue);
47 #define MATCH_PREFIX \
49 "sender='org.freedesktop.DBus'," \
50 "path='/org/freedesktop/DBus'," \
51 "interface='org.freedesktop.DBus'," \
52 "member='NameOwnerChanged'," \
55 #define MATCH_SUFFIX \
58 #define MATCH_FOR_NAME(name) \
61 size_t _l = strlen(name); \
62 _x = alloca(strlen(MATCH_PREFIX)+_l+strlen(MATCH_SUFFIX)+1); \
63 strcpy(stpcpy(stpcpy(_x, MATCH_PREFIX), name), MATCH_SUFFIX); \
67 static struct track_item* track_item_free(struct track_item *i) {
72 sd_bus_slot_unref(i->slot);
79 DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_free);
81 static void bus_track_add_to_queue(sd_bus_track *track) {
84 /* Adds the bus track object to the queue of objects we should dispatch next, subject to a number of
87 /* Already in the queue? */
91 /* if we are currently in the process of adding a new name, then let's not enqueue this just yet, let's wait
92 * until the addition is complete. */
93 if (track->n_adding > 0)
96 /* still referenced? */
97 if (hashmap_size(track->names) > 0)
100 /* Nothing to call? */
104 LIST_PREPEND(queue, track->bus->track_queue, track);
105 track->in_queue = true;
108 static void bus_track_remove_from_queue(sd_bus_track *track) {
111 if (!track->in_queue)
114 LIST_REMOVE(queue, track->bus->track_queue, track);
115 track->in_queue = false;
118 static int bus_track_remove_name_fully(sd_bus_track *track, const char *name) {
119 struct track_item *i;
124 i = hashmap_remove(track->names, name);
130 bus_track_add_to_queue(track);
132 track->modified = true;
136 _public_ int sd_bus_track_new(
138 sd_bus_track **track,
139 sd_bus_track_handler_t handler,
144 assert_return(bus, -EINVAL);
145 assert_return(track, -EINVAL);
147 if (!bus->bus_client)
150 t = new0(sd_bus_track, 1);
155 t->handler = handler;
156 t->userdata = userdata;
157 t->bus = sd_bus_ref(bus);
159 bus_track_add_to_queue(t);
165 _public_ sd_bus_track* sd_bus_track_ref(sd_bus_track *track) {
170 assert(track->n_ref > 0);
177 _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) {
178 struct track_item *i;
183 assert(track->n_ref > 0);
185 if (track->n_ref > 1) {
190 while ((i = hashmap_steal_first(track->names)))
193 bus_track_remove_from_queue(track);
194 hashmap_free(track->names);
195 sd_bus_unref(track->bus);
201 static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
202 sd_bus_track *track = userdata;
203 const char *name, *old, *new;
209 r = sd_bus_message_read(message, "sss", &name, &old, &new);
213 bus_track_remove_name_fully(track, name);
217 _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
218 _cleanup_(track_item_freep) struct track_item *n = NULL;
219 struct track_item *i;
223 assert_return(track, -EINVAL);
224 assert_return(service_name_is_valid(name), -EINVAL);
226 i = hashmap_get(track->names, name);
228 if (track->recursive) {
229 unsigned k = track->n_ref + 1;
231 if (k < track->n_ref) /* Check for overflow */
237 bus_track_remove_from_queue(track);
241 r = hashmap_ensure_allocated(&track->names, &string_hash_ops);
245 n = new0(struct track_item, 1);
248 n->name = strdup(name);
252 /* First, subscribe to this name */
253 match = MATCH_FOR_NAME(name);
255 bus_track_remove_from_queue(track); /* don't dispatch this while we work in it */
257 track->n_adding++; /* make sure we aren't dispatched while we synchronously add this match */
258 r = sd_bus_add_match(track->bus, &n->slot, match, on_name_owner_changed, track);
261 bus_track_add_to_queue(track);
265 r = hashmap_put(track->names, n->name, n);
267 bus_track_add_to_queue(track);
271 /* Second, check if it is currently existing, or maybe doesn't, or maybe disappeared already. */
272 track->n_adding++; /* again, make sure this isn't dispatch while we are working in it */
273 r = sd_bus_get_name_creds(track->bus, name, 0, NULL);
276 hashmap_remove(track->names, name);
277 bus_track_add_to_queue(track);
284 bus_track_remove_from_queue(track);
285 track->modified = true;
290 _public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) {
291 struct track_item *i;
293 assert_return(name, -EINVAL);
295 if (!track) /* Treat a NULL track object as an empty track object */
298 if (!track->recursive)
299 return bus_track_remove_name_fully(track, name);
301 i = hashmap_get(track->names, name);
310 return bus_track_remove_name_fully(track, name);
315 _public_ unsigned sd_bus_track_count(sd_bus_track *track) {
317 if (!track) /* Let's consider a NULL object equivalent to an empty object */
320 /* This signature really should have returned an int, so that we can propagate errors. But well, ... Also, note
321 * that this returns the number of names being watched, and multiple references to the same name are not
324 return hashmap_size(track->names);
327 _public_ const char* sd_bus_track_contains(sd_bus_track *track, const char *name) {
328 assert_return(name, NULL);
330 if (!track) /* Let's consider a NULL object equivalent to an empty object */
333 return hashmap_get(track->names, (void*) name) ? name : NULL;
336 _public_ const char* sd_bus_track_first(sd_bus_track *track) {
337 const char *n = NULL;
342 track->modified = false;
343 track->iterator = ITERATOR_FIRST;
345 hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
349 _public_ const char* sd_bus_track_next(sd_bus_track *track) {
350 const char *n = NULL;
358 hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
362 _public_ int sd_bus_track_add_sender(sd_bus_track *track, sd_bus_message *m) {
365 assert_return(track, -EINVAL);
366 assert_return(m, -EINVAL);
368 if (sd_bus_message_get_bus(m) != track->bus)
371 sender = sd_bus_message_get_sender(m);
375 return sd_bus_track_add_name(track, sender);
378 _public_ int sd_bus_track_remove_sender(sd_bus_track *track, sd_bus_message *m) {
381 assert_return(m, -EINVAL);
383 if (!track) /* Treat a NULL track object as an empty track object */
386 if (sd_bus_message_get_bus(m) != track->bus)
389 sender = sd_bus_message_get_sender(m);
393 return sd_bus_track_remove_name(track, sender);
396 _public_ sd_bus* sd_bus_track_get_bus(sd_bus_track *track) {
397 assert_return(track, NULL);
402 void bus_track_dispatch(sd_bus_track *track) {
406 assert(track->in_queue);
407 assert(track->handler);
409 bus_track_remove_from_queue(track);
411 sd_bus_track_ref(track);
413 r = track->handler(track, track->userdata);
415 log_debug_errno(r, "Failed to process track handler: %m");
417 bus_track_add_to_queue(track);
419 sd_bus_track_unref(track);
422 #if 0 /// UNNEEDED by elogind
423 _public_ void *sd_bus_track_get_userdata(sd_bus_track *track) {
424 assert_return(track, NULL);
426 return track->userdata;
429 _public_ void *sd_bus_track_set_userdata(sd_bus_track *track, void *userdata) {
432 assert_return(track, NULL);
434 ret = track->userdata;
435 track->userdata = userdata;
441 _public_ int sd_bus_track_set_recursive(sd_bus_track *track, int b) {
442 assert_return(track, -EINVAL);
444 if (track->recursive == !!b)
447 if (!hashmap_isempty(track->names))
450 track->recursive = b;
454 _public_ int sd_bus_track_get_recursive(sd_bus_track *track) {
455 assert_return(track, -EINVAL);
457 return track->recursive;
460 _public_ int sd_bus_track_count_sender(sd_bus_track *track, sd_bus_message *m) {
463 assert_return(m, -EINVAL);
465 if (!track) /* Let's consider a NULL object equivalent to an empty object */
468 if (sd_bus_message_get_bus(m) != track->bus)
471 sender = sd_bus_message_get_sender(m);
475 return sd_bus_track_count_name(track, sender);
478 _public_ int sd_bus_track_count_name(sd_bus_track *track, const char *name) {
479 struct track_item *i;
481 assert_return(service_name_is_valid(name), -EINVAL);
483 if (!track) /* Let's consider a NULL object equivalent to an empty object */
486 i = hashmap_get(track->names, name);