Debian Codegraph @

src/libsystemd/sd-bus/bus-track.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-error.h"
7 #include "bus-internal.h"
8 #include "bus-track.h"
9 #include "hashmap.h"
10 #include "log.h"
11 #include "string-util.h"
12
13 typedef struct BusTrackItem {
14 unsigned n_ref;
15 char *name;
16 sd_bus_slot *slot;
17 sd_bus_track *track;
18 } BusTrackItem;
19
20 struct sd_bus_track {
21 unsigned n_ref;
22 unsigned n_adding; /* are we in the process of adding a new name? */
23 sd_bus *bus;
24 sd_bus_track_handler_t handler;
25 void *userdata;
26 Hashmap *names;
27 LIST_FIELDS(sd_bus_track, queue);
28 Iterator iterator;
29 bool in_list:1; /* In bus->tracks? */
30 bool in_queue:1; /* In bus->track_queue? */
31 bool modified:1;
32 bool recursive:1;
33 sd_bus_destroy_t destroy_callback;
34
35 LIST_FIELDS(sd_bus_track, tracks);
36 };
37
38 #define MATCH_FOR_NAME(name) \
39 strjoina("type='signal'," \
40 "sender='org.freedesktop.DBus'," \
41 "path='/org/freedesktop/DBus'," \
42 "interface='org.freedesktop.DBus'," \
43 "member='NameOwnerChanged'," \
44 "arg0='", name, "'")
45
46 static BusTrackItem* track_item_free(BusTrackItem *i) {
47 if (!i)
48 return NULL;
49
50 sd_bus_slot_unref(i->slot);
51 free(i->name);
52 return mfree(i);
53 }
54
55 DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(BusTrackItem, track_item, track_item_free);
56 DEFINE_TRIVIAL_CLEANUP_FUNC(BusTrackItem*, track_item_unref);
57 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(track_item_hash_ops, char, string_hash_func, string_compare_func,
58 BusTrackItem, track_item_free);
59
60 static void bus_track_add_to_queue(sd_bus_track *track) {
61 assert(track);
62
63 /* Adds the bus track object to the queue of objects we should dispatch next, subject to a number of
64 * conditions. */
65
66 /* Already in the queue? */
67 if (track->in_queue)
68 return;
69
70 /* if we are currently in the process of adding a new name, then let's not enqueue this just yet, let's wait
71 * until the addition is complete. */
72 if (track->n_adding > 0)
73 return;
74
75 /* still referenced? */
76 if (!hashmap_isempty(track->names))
77 return;
78
79 /* Nothing to call? */
80 if (!track->handler)
81 return;
82
83 /* Already closed? */
84 if (!track->in_list)
85 return;
86
87 LIST_PREPEND(queue, track->bus->track_queue, track);
88 track->in_queue = true;
89 }
90
91 static void bus_track_remove_from_queue(sd_bus_track *track) {
92 assert(track);
93
94 if (!track->in_queue)
95 return;
96
97 LIST_REMOVE(queue, track->bus->track_queue, track);
98 track->in_queue = false;
99 }
100
101 static int bus_track_remove_name_fully(sd_bus_track *track, const char *name) {
102 BusTrackItem *i;
103
104 assert(track);
105 assert(name);
106
107 i = hashmap_remove(track->names, name);
108 if (!i)
109 return 0;
110
111 track_item_free(i);
112
113 bus_track_add_to_queue(track);
114
115 track->modified = true;
116 return 1;
117 }
118
119 _public_ int sd_bus_track_new(
120 sd_bus *bus,
121 sd_bus_track **ret,
122 sd_bus_track_handler_t handler,
123 void *userdata) {
124
125 sd_bus_track *t;
126
127 assert_return(bus, -EINVAL);
128 assert_return(bus = bus_resolve(bus), -ENOPKG);
129 assert_return(ret, -EINVAL);
130
131 if (!bus->bus_client)
132 return -EINVAL;
133
134 t = new0(sd_bus_track, 1);
135 if (!t)
136 return -ENOMEM;
137
138 t->n_ref = 1;
139 t->handler = handler;
140 t->userdata = userdata;
141 t->bus = sd_bus_ref(bus);
142
143 LIST_PREPEND(tracks, bus->tracks, t);
144 t->in_list = true;
145
146 bus_track_add_to_queue(t);
147
148 *ret = t;
149 return 0;
150 }
151
152 static sd_bus_track* track_free(sd_bus_track *track) {
153 assert(track);
154
155 if (track->in_list)
156 LIST_REMOVE(tracks, track->bus->tracks, track);
157
158 bus_track_remove_from_queue(track);
159 track->names = hashmap_free(track->names);
160 track->bus = sd_bus_unref(track->bus);
161
162 if (track->destroy_callback)
163 track->destroy_callback(track->userdata);
164
165 return mfree(track);
166 }
167
168 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_track, sd_bus_track, track_free);
169
170 static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
171 BusTrackItem *item = ASSERT_PTR(userdata);
172 const char *name;
173 int r;
174
175 assert(message);
176 assert(item->track);
177
178 r = sd_bus_message_read(message, "sss", &name, NULL, NULL);
179 if (r < 0)
180 return 0;
181
182 bus_track_remove_name_fully(item->track, name);
183 return 0;
184 }
185
186 static int name_owner_changed_install_callback(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
187 BusTrackItem *item = ASSERT_PTR(userdata);
188 const sd_bus_error *e;
189 int r;
190
191 assert(message);
192 assert(item->track);
193 assert(item->name);
194
195 e = sd_bus_message_get_error(message);
196 if (!e)
197 return 0;
198
199 r = sd_bus_error_get_errno(e);
200 log_debug_errno(r, "Failed to install match for tracking name '%s': %s", item->name, bus_error_message(e, r));
201
202 bus_track_remove_name_fully(item->track, item->name);
203 return 0;
204 }
205
206 _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
207 _cleanup_(track_item_unrefp) BusTrackItem *n = NULL;
208 BusTrackItem *i;
209 const char *match;
210 int r;
211
212 assert_return(track, -EINVAL);
213 assert_return(service_name_is_valid(name), -EINVAL);
214
215 i = hashmap_get(track->names, name);
216 if (i) {
217 if (track->recursive) {
218 assert(i->n_ref > 0);
219
220 /* Manual overflow check (instead of a DEFINE_TRIVIAL_REF_FUNC() helper or so), so
221 * that we can return a proper error, given this is almost always called in a
222 * directly client controllable way, and thus better should never hit an assertion
223 * here. */
224 if (i->n_ref >= UINT_MAX)
225 return -EOVERFLOW;
226
227 i->n_ref++;
228 }
229
230 bus_track_remove_from_queue(track);
231 return 0;
232 }
233
234 r = hashmap_ensure_allocated(&track->names, &track_item_hash_ops);
235 if (r < 0)
236 return r;
237
238 n = new(BusTrackItem, 1);
239 if (!n)
240 return -ENOMEM;
241
242 *n = (BusTrackItem) {
243 .n_ref = 1,
244 .track = track,
245 };
246
247 n->name = strdup(name);
248 if (!n->name)
249 return -ENOMEM;
250
251 /* First, subscribe to this name */
252 match = MATCH_FOR_NAME(name);
253
254 bus_track_remove_from_queue(track); /* don't dispatch this while we work in it */
255
256 r = sd_bus_add_match_async(track->bus, &n->slot, match, on_name_owner_changed, name_owner_changed_install_callback, n);
257 if (r < 0) {
258 bus_track_add_to_queue(track);
259 return r;
260 }
261
262 r = hashmap_put(track->names, n->name, n);
263 if (r < 0) {
264 bus_track_add_to_queue(track);
265 return r;
266 }
267
268 /* Second, check if it is currently existing, or maybe doesn't, or maybe disappeared already. */
269 track->n_adding++; /* again, make sure this isn't dispatch while we are working in it */
270 r = sd_bus_get_name_creds(track->bus, name, 0, NULL);
271 track->n_adding--;
272 if (r < 0) {
273 hashmap_remove(track->names, name);
274 bus_track_add_to_queue(track);
275 return r;
276 }
277
278 TAKE_PTR(n);
279
280 bus_track_remove_from_queue(track);
281 track->modified = true;
282
283 return 1;
284 }
285
286 _public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) {
287 BusTrackItem *i;
288
289 assert_return(name, -EINVAL);
290
291 if (!track) /* Treat a NULL track object as an empty track object */
292 return 0;
293
294 i = hashmap_get(track->names, name);
295 if (!i)
296 return 0;
297
298 assert(i->n_ref >= 1);
299 if (i->n_ref <= 1)
300 return bus_track_remove_name_fully(track, name);
301
302 track_item_unref(i);
303
304 return 1;
305 }
306
307 _public_ unsigned sd_bus_track_count(sd_bus_track *track) {
308
309 if (!track) /* Let's consider a NULL object equivalent to an empty object */
310 return 0;
311
312 /* This signature really should have returned an int, so that we can propagate errors. But well, ... Also, note
313 * that this returns the number of names being watched, and multiple references to the same name are not
314 * counted. */
315
316 return hashmap_size(track->names);
317 }
318
319 _public_ const char* sd_bus_track_contains(sd_bus_track *track, const char *name) {
320 assert_return(name, NULL);
321
322 if (!track) /* Let's consider a NULL object equivalent to an empty object */
323 return NULL;
324
325 return hashmap_contains(track->names, name) ? name : NULL;
326 }
327
328 _public_ const char* sd_bus_track_first(sd_bus_track *track) {
329 const char *n = NULL;
330
331 if (!track)
332 return NULL;
333
334 track->modified = false;
335 track->iterator = ITERATOR_FIRST;
336
337 (void) hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
338 return n;
339 }
340
341 _public_ const char* sd_bus_track_next(sd_bus_track *track) {
342 const char *n = NULL;
343
344 if (!track)
345 return NULL;
346
347 if (track->modified)
348 return NULL;
349
350 (void) hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
351 return n;
352 }
353
354 _public_ int sd_bus_track_add_sender(sd_bus_track *track, sd_bus_message *m) {
355 const char *sender;
356
357 assert_return(track, -EINVAL);
358 assert_return(m, -EINVAL);
359
360 if (sd_bus_message_get_bus(m) != track->bus)
361 return -EINVAL;
362
363 sender = sd_bus_message_get_sender(m);
364 if (!sender)
365 return -EINVAL;
366
367 return sd_bus_track_add_name(track, sender);
368 }
369
370 _public_ int sd_bus_track_remove_sender(sd_bus_track *track, sd_bus_message *m) {
371 const char *sender;
372
373 assert_return(m, -EINVAL);
374
375 if (!track) /* Treat a NULL track object as an empty track object */
376 return 0;
377
378 if (sd_bus_message_get_bus(m) != track->bus)
379 return -EINVAL;
380
381 sender = sd_bus_message_get_sender(m);
382 if (!sender)
383 return -EINVAL;
384
385 return sd_bus_track_remove_name(track, sender);
386 }
387
388 _public_ sd_bus* sd_bus_track_get_bus(sd_bus_track *track) {
389 assert_return(track, NULL);
390
391 return track->bus;
392 }
393
394 void bus_track_dispatch(sd_bus_track *track) {
395 int r;
396
397 assert(track);
398 assert(track->handler);
399
400 bus_track_remove_from_queue(track);
401
402 sd_bus_track_ref(track);
403
404 r = track->handler(track, track->userdata);
405 if (r < 0)
406 log_debug_errno(r, "Failed to process track handler: %m");
407 else if (r == 0)
408 bus_track_add_to_queue(track);
409
410 sd_bus_track_unref(track);
411 }
412
413 void bus_track_close(sd_bus_track *track) {
414 assert(track);
415
416 /* Called whenever our bus connected is closed. If so, and our track object is non-empty, dispatch it
417 * immediately, as we are closing now, but first flush out all names. */
418
419 if (!track->in_list)
420 return; /* We already closed this one, don't close it again. */
421
422 /* Remember that this one is closed now */
423 LIST_REMOVE(tracks, track->bus->tracks, track);
424 track->in_list = false;
425
426 /* If there's no name in this one anyway, we don't have to dispatch */
427 if (hashmap_isempty(track->names))
428 return;
429
430 /* Let's flush out all names */
431 hashmap_clear(track->names);
432
433 /* Invoke handler */
434 if (track->handler)
435 bus_track_dispatch(track);
436 }
437
438 _public_ void* sd_bus_track_get_userdata(sd_bus_track *track) {
439 assert_return(track, NULL);
440
441 return track->userdata;
442 }
443
444 _public_ void* sd_bus_track_set_userdata(sd_bus_track *track, void *userdata) {
445 void *ret;
446
447 assert_return(track, NULL);
448
449 ret = track->userdata;
450 track->userdata = userdata;
451
452 return ret;
453 }
454
455 _public_ int sd_bus_track_set_destroy_callback(sd_bus_track *track, sd_bus_destroy_t callback) {
456 assert_return(track, -EINVAL);
457
458 track->destroy_callback = callback;
459 return 0;
460 }
461
462 _public_ int sd_bus_track_get_destroy_callback(sd_bus_track *track, sd_bus_destroy_t *ret) {
463 assert_return(track, -EINVAL);
464
465 if (ret)
466 *ret = track->destroy_callback;
467
468 return !!track->destroy_callback;
469 }
470
471 _public_ int sd_bus_track_set_recursive(sd_bus_track *track, int b) {
472 assert_return(track, -EINVAL);
473
474 if (track->recursive == !!b)
475 return 0;
476
477 if (!hashmap_isempty(track->names))
478 return -EBUSY;
479
480 track->recursive = b;
481 return 0;
482 }
483
484 _public_ int sd_bus_track_get_recursive(sd_bus_track *track) {
485 assert_return(track, -EINVAL);
486
487 return track->recursive;
488 }
489
490 _public_ int sd_bus_track_count_sender(sd_bus_track *track, sd_bus_message *m) {
491 const char *sender;
492
493 assert_return(m, -EINVAL);
494
495 if (!track) /* Let's consider a NULL object equivalent to an empty object */
496 return 0;
497
498 if (sd_bus_message_get_bus(m) != track->bus)
499 return -EINVAL;
500
501 sender = sd_bus_message_get_sender(m);
502 if (!sender)
503 return -EINVAL;
504
505 return sd_bus_track_count_name(track, sender);
506 }
507
508 _public_ int sd_bus_track_count_name(sd_bus_track *track, const char *name) {
509 BusTrackItem *i;
510
511 assert_return(service_name_is_valid(name), -EINVAL);
512
513 if (!track) /* Let's consider a NULL object equivalent to an empty object */
514 return 0;
515
516 i = hashmap_get(track->names, name);
517 if (!i)
518 return 0;
519
520 return i->n_ref;
521 }