]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-objects.c
strv: make iterator in STRV_FOREACH() declaread in the loop
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-objects.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "bus-internal.h"
5 #include "bus-introspect.h"
6 #include "bus-message.h"
7 #include "bus-objects.h"
8 #include "bus-signature.h"
9 #include "bus-slot.h"
10 #include "bus-type.h"
11 #include "missing_capability.h"
12 #include "string-util.h"
13 #include "strv.h"
14
15 static int node_vtable_get_userdata(
16 sd_bus *bus,
17 const char *path,
18 struct node_vtable *c,
19 void **userdata,
20 sd_bus_error *error) {
21
22 sd_bus_slot *s;
23 void *u, *found_u = NULL;
24 int r;
25
26 assert(bus);
27 assert(path);
28 assert(c);
29
30 s = container_of(c, sd_bus_slot, node_vtable);
31 u = s->userdata;
32 if (c->find) {
33 bus->current_slot = sd_bus_slot_ref(s);
34 bus->current_userdata = u;
35 r = c->find(bus, path, c->interface, u, &found_u, error);
36 bus->current_userdata = NULL;
37 bus->current_slot = sd_bus_slot_unref(s);
38
39 if (r < 0)
40 return r;
41 if (sd_bus_error_is_set(error))
42 return -sd_bus_error_get_errno(error);
43 if (r == 0)
44 return r;
45 } else
46 found_u = u;
47
48 if (userdata)
49 *userdata = found_u;
50
51 return 1;
52 }
53
54 static void *vtable_method_convert_userdata(const sd_bus_vtable *p, void *u) {
55 assert(p);
56
57 if (!u || FLAGS_SET(p->flags, SD_BUS_VTABLE_ABSOLUTE_OFFSET))
58 return SIZE_TO_PTR(p->x.method.offset); /* don't add offset on NULL, to make ubsan happy */
59
60 return (uint8_t*) u + p->x.method.offset;
61 }
62
63 static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
64 assert(p);
65
66 if (!u || FLAGS_SET(p->flags, SD_BUS_VTABLE_ABSOLUTE_OFFSET))
67 return SIZE_TO_PTR(p->x.property.offset); /* as above */
68
69 return (uint8_t*) u + p->x.property.offset;
70 }
71
72 static int vtable_property_get_userdata(
73 sd_bus *bus,
74 const char *path,
75 struct vtable_member *p,
76 void **userdata,
77 sd_bus_error *error) {
78
79 void *u;
80 int r;
81
82 assert(bus);
83 assert(path);
84 assert(p);
85 assert(userdata);
86
87 r = node_vtable_get_userdata(bus, path, p->parent, &u, error);
88 if (r <= 0)
89 return r;
90 if (bus->nodes_modified)
91 return 0;
92
93 *userdata = vtable_property_convert_userdata(p->vtable, u);
94 return 1;
95 }
96
97 static int add_enumerated_to_set(
98 sd_bus *bus,
99 const char *prefix,
100 struct node_enumerator *first,
101 OrderedSet *s,
102 sd_bus_error *error) {
103
104 int r;
105
106 assert(bus);
107 assert(prefix);
108 assert(s);
109
110 LIST_FOREACH(enumerators, c, first) {
111 char **children = NULL;
112 sd_bus_slot *slot;
113
114 if (bus->nodes_modified)
115 return 0;
116
117 slot = container_of(c, sd_bus_slot, node_enumerator);
118
119 bus->current_slot = sd_bus_slot_ref(slot);
120 bus->current_userdata = slot->userdata;
121 r = c->callback(bus, prefix, slot->userdata, &children, error);
122 bus->current_userdata = NULL;
123 bus->current_slot = sd_bus_slot_unref(slot);
124
125 if (r < 0)
126 return r;
127 if (sd_bus_error_is_set(error))
128 return -sd_bus_error_get_errno(error);
129
130 STRV_FOREACH(k, children) {
131 if (r < 0) {
132 free(*k);
133 continue;
134 }
135
136 if (!object_path_is_valid(*k)) {
137 free(*k);
138 r = -EINVAL;
139 continue;
140 }
141
142 if (!object_path_startswith(*k, prefix)) {
143 free(*k);
144 continue;
145 }
146
147 r = ordered_set_consume(s, *k);
148 if (r == -EEXIST)
149 r = 0;
150 }
151
152 free(children);
153 if (r < 0)
154 return r;
155 }
156
157 return 0;
158 }
159
160 enum {
161 /* if set, add_subtree() works recursively */
162 CHILDREN_RECURSIVE = 1 << 0,
163 /* if set, add_subtree() scans object-manager hierarchies recursively */
164 CHILDREN_SUBHIERARCHIES = 1 << 1,
165 };
166
167 static int add_subtree_to_set(
168 sd_bus *bus,
169 const char *prefix,
170 struct node *n,
171 unsigned flags,
172 OrderedSet *s,
173 sd_bus_error *error) {
174
175 int r;
176
177 assert(bus);
178 assert(prefix);
179 assert(n);
180 assert(s);
181
182 r = add_enumerated_to_set(bus, prefix, n->enumerators, s, error);
183 if (r < 0)
184 return r;
185 if (bus->nodes_modified)
186 return 0;
187
188 LIST_FOREACH(siblings, i, n->child) {
189 char *t;
190
191 if (!object_path_startswith(i->path, prefix))
192 continue;
193
194 t = strdup(i->path);
195 if (!t)
196 return -ENOMEM;
197
198 r = ordered_set_consume(s, t);
199 if (r < 0 && r != -EEXIST)
200 return r;
201
202 if ((flags & CHILDREN_RECURSIVE) &&
203 ((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
204 r = add_subtree_to_set(bus, prefix, i, flags, s, error);
205 if (r < 0)
206 return r;
207 if (bus->nodes_modified)
208 return 0;
209 }
210 }
211
212 return 0;
213 }
214
215 static int get_child_nodes(
216 sd_bus *bus,
217 const char *prefix,
218 struct node *n,
219 unsigned flags,
220 OrderedSet **_s,
221 sd_bus_error *error) {
222
223 OrderedSet *s = NULL;
224 int r;
225
226 assert(bus);
227 assert(prefix);
228 assert(n);
229 assert(_s);
230
231 s = ordered_set_new(&string_hash_ops);
232 if (!s)
233 return -ENOMEM;
234
235 r = add_subtree_to_set(bus, prefix, n, flags, s, error);
236 if (r < 0) {
237 ordered_set_free_free(s);
238 return r;
239 }
240
241 *_s = s;
242 return 0;
243 }
244
245 static int node_callbacks_run(
246 sd_bus *bus,
247 sd_bus_message *m,
248 struct node_callback *first,
249 bool require_fallback,
250 bool *found_object) {
251
252 int r;
253
254 assert(bus);
255 assert(m);
256 assert(found_object);
257
258 LIST_FOREACH(callbacks, c, first) {
259 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
260 sd_bus_slot *slot;
261
262 if (bus->nodes_modified)
263 return 0;
264
265 if (require_fallback && !c->is_fallback)
266 continue;
267
268 *found_object = true;
269
270 if (c->last_iteration == bus->iteration_counter)
271 continue;
272
273 c->last_iteration = bus->iteration_counter;
274
275 r = sd_bus_message_rewind(m, true);
276 if (r < 0)
277 return r;
278
279 slot = container_of(c, sd_bus_slot, node_callback);
280
281 bus->current_slot = sd_bus_slot_ref(slot);
282 bus->current_handler = c->callback;
283 bus->current_userdata = slot->userdata;
284 r = c->callback(m, slot->userdata, &error_buffer);
285 bus->current_userdata = NULL;
286 bus->current_handler = NULL;
287 bus->current_slot = sd_bus_slot_unref(slot);
288
289 r = bus_maybe_reply_error(m, r, &error_buffer);
290 if (r != 0)
291 return r;
292 }
293
294 return 0;
295 }
296
297 #define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
298
299 static int check_access(sd_bus *bus, sd_bus_message *m, struct vtable_member *c, sd_bus_error *error) {
300 uint64_t cap;
301 int r;
302
303 assert(bus);
304 assert(m);
305 assert(c);
306
307 /* If the entire bus is trusted let's grant access */
308 if (bus->trusted)
309 return 0;
310
311 /* If the member is marked UNPRIVILEGED let's grant access */
312 if (c->vtable->flags & SD_BUS_VTABLE_UNPRIVILEGED)
313 return 0;
314
315 /* Check that the caller has the requested capability set. Note that the flags value contains the
316 * capability number plus one, which we need to subtract here. We do this so that we have 0 as
317 * special value for the default. */
318 cap = CAPABILITY_SHIFT(c->vtable->flags);
319 if (cap == 0)
320 cap = CAPABILITY_SHIFT(c->parent->vtable[0].flags);
321 if (cap == 0)
322 cap = CAP_SYS_ADMIN;
323 else
324 cap--;
325
326 r = sd_bus_query_sender_privilege(m, cap);
327 if (r < 0)
328 return r;
329 if (r > 0)
330 return 0;
331
332 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access to %s.%s() not permitted.", c->interface, c->member);
333 }
334
335 static int method_callbacks_run(
336 sd_bus *bus,
337 sd_bus_message *m,
338 struct vtable_member *c,
339 bool require_fallback,
340 bool *found_object) {
341
342 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
343 const char *signature;
344 void *u;
345 int r;
346
347 assert(bus);
348 assert(m);
349 assert(c);
350 assert(found_object);
351
352 if (require_fallback && !c->parent->is_fallback)
353 return 0;
354
355 if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
356 r = sd_bus_message_sensitive(m);
357 if (r < 0)
358 return r;
359 }
360
361 r = check_access(bus, m, c, &error);
362 if (r < 0)
363 return bus_maybe_reply_error(m, r, &error);
364
365 r = node_vtable_get_userdata(bus, m->path, c->parent, &u, &error);
366 if (r <= 0)
367 return bus_maybe_reply_error(m, r, &error);
368 if (bus->nodes_modified)
369 return 0;
370
371 u = vtable_method_convert_userdata(c->vtable, u);
372
373 *found_object = true;
374
375 if (c->last_iteration == bus->iteration_counter)
376 return 0;
377
378 c->last_iteration = bus->iteration_counter;
379
380 r = sd_bus_message_rewind(m, true);
381 if (r < 0)
382 return r;
383
384 signature = sd_bus_message_get_signature(m, true);
385 if (!signature)
386 return -EINVAL;
387
388 if (!streq(strempty(c->vtable->x.method.signature), signature))
389 return sd_bus_reply_method_errorf(
390 m,
391 SD_BUS_ERROR_INVALID_ARGS,
392 "Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
393 signature, c->interface, c->member, strempty(c->vtable->x.method.signature));
394
395 /* Keep track what the signature of the reply to this message
396 * should be, so that this can be enforced when sealing the
397 * reply. */
398 m->enforced_reply_signature = strempty(c->vtable->x.method.result);
399
400 if (c->vtable->x.method.handler) {
401 sd_bus_slot *slot;
402
403 slot = container_of(c->parent, sd_bus_slot, node_vtable);
404
405 bus->current_slot = sd_bus_slot_ref(slot);
406 bus->current_handler = c->vtable->x.method.handler;
407 bus->current_userdata = u;
408 r = c->vtable->x.method.handler(m, u, &error);
409 bus->current_userdata = NULL;
410 bus->current_handler = NULL;
411 bus->current_slot = sd_bus_slot_unref(slot);
412
413 return bus_maybe_reply_error(m, r, &error);
414 }
415
416 /* If the method callback is NULL, make this a successful NOP */
417 r = sd_bus_reply_method_return(m, NULL);
418 if (r < 0)
419 return r;
420
421 return 1;
422 }
423
424 static int invoke_property_get(
425 sd_bus *bus,
426 sd_bus_slot *slot,
427 const sd_bus_vtable *v,
428 const char *path,
429 const char *interface,
430 const char *property,
431 sd_bus_message *reply,
432 void *userdata,
433 sd_bus_error *error) {
434
435 const void *p;
436 int r;
437
438 assert(bus);
439 assert(slot);
440 assert(v);
441 assert(path);
442 assert(interface);
443 assert(property);
444 assert(reply);
445
446 if (v->x.property.get) {
447
448 bus->current_slot = sd_bus_slot_ref(slot);
449 bus->current_userdata = userdata;
450 r = v->x.property.get(bus, path, interface, property, reply, userdata, error);
451 bus->current_userdata = NULL;
452 bus->current_slot = sd_bus_slot_unref(slot);
453
454 if (r < 0)
455 return r;
456 if (sd_bus_error_is_set(error))
457 return -sd_bus_error_get_errno(error);
458 return r;
459 }
460
461 /* Automatic handling if no callback is defined. */
462
463 if (streq(v->x.property.signature, "as"))
464 return sd_bus_message_append_strv(reply, *(char***) userdata);
465
466 assert(signature_is_single(v->x.property.signature, false));
467 assert(bus_type_is_basic(v->x.property.signature[0]));
468
469 switch (v->x.property.signature[0]) {
470
471 case SD_BUS_TYPE_STRING:
472 case SD_BUS_TYPE_SIGNATURE:
473 p = strempty(*(char**) userdata);
474 break;
475
476 case SD_BUS_TYPE_OBJECT_PATH:
477 p = *(char**) userdata;
478 assert(p);
479 break;
480
481 default:
482 p = userdata;
483 break;
484 }
485
486 return sd_bus_message_append_basic(reply, v->x.property.signature[0], p);
487 }
488
489 static int invoke_property_set(
490 sd_bus *bus,
491 sd_bus_slot *slot,
492 const sd_bus_vtable *v,
493 const char *path,
494 const char *interface,
495 const char *property,
496 sd_bus_message *value,
497 void *userdata,
498 sd_bus_error *error) {
499
500 int r;
501
502 assert(bus);
503 assert(slot);
504 assert(v);
505 assert(path);
506 assert(interface);
507 assert(property);
508 assert(value);
509
510 if (v->x.property.set) {
511
512 bus->current_slot = sd_bus_slot_ref(slot);
513 bus->current_userdata = userdata;
514 r = v->x.property.set(bus, path, interface, property, value, userdata, error);
515 bus->current_userdata = NULL;
516 bus->current_slot = sd_bus_slot_unref(slot);
517
518 if (r < 0)
519 return r;
520 if (sd_bus_error_is_set(error))
521 return -sd_bus_error_get_errno(error);
522 return r;
523 }
524
525 /* Automatic handling if no callback is defined. */
526
527 assert(signature_is_single(v->x.property.signature, false));
528 assert(bus_type_is_basic(v->x.property.signature[0]));
529
530 switch (v->x.property.signature[0]) {
531
532 case SD_BUS_TYPE_STRING:
533 case SD_BUS_TYPE_OBJECT_PATH:
534 case SD_BUS_TYPE_SIGNATURE: {
535 const char *p;
536 char *n;
537
538 r = sd_bus_message_read_basic(value, v->x.property.signature[0], &p);
539 if (r < 0)
540 return r;
541
542 n = strdup(p);
543 if (!n)
544 return -ENOMEM;
545
546 free(*(char**) userdata);
547 *(char**) userdata = n;
548
549 break;
550 }
551
552 default:
553 r = sd_bus_message_read_basic(value, v->x.property.signature[0], userdata);
554 if (r < 0)
555 return r;
556
557 break;
558 }
559
560 return 1;
561 }
562
563 static int property_get_set_callbacks_run(
564 sd_bus *bus,
565 sd_bus_message *m,
566 struct vtable_member *c,
567 bool require_fallback,
568 bool is_get,
569 bool *found_object) {
570
571 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
572 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
573 sd_bus_slot *slot;
574 void *u = NULL;
575 int r;
576
577 assert(bus);
578 assert(m);
579 assert(c);
580 assert(found_object);
581
582 if (require_fallback && !c->parent->is_fallback)
583 return 0;
584
585 if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
586 r = sd_bus_message_sensitive(m);
587 if (r < 0)
588 return r;
589 }
590
591 r = vtable_property_get_userdata(bus, m->path, c, &u, &error);
592 if (r <= 0)
593 return bus_maybe_reply_error(m, r, &error);
594 if (bus->nodes_modified)
595 return 0;
596
597 slot = container_of(c->parent, sd_bus_slot, node_vtable);
598
599 *found_object = true;
600
601 r = sd_bus_message_new_method_return(m, &reply);
602 if (r < 0)
603 return r;
604
605 if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
606 r = sd_bus_message_sensitive(reply);
607 if (r < 0)
608 return r;
609 }
610
611 if (is_get) {
612 /* Note that we do not protect against reexecution
613 * here (using the last_iteration check, see below),
614 * should the node tree have changed and we got called
615 * again. We assume that property Get() calls are
616 * ultimately without side-effects or if they aren't
617 * then at least idempotent. */
618
619 r = sd_bus_message_open_container(reply, 'v', c->vtable->x.property.signature);
620 if (r < 0)
621 return r;
622
623 /* Note that we do not do an access check here. Read
624 * access to properties is always unrestricted, since
625 * PropertiesChanged signals broadcast contents
626 * anyway. */
627
628 r = invoke_property_get(bus, slot, c->vtable, m->path, c->interface, c->member, reply, u, &error);
629 if (r < 0)
630 return bus_maybe_reply_error(m, r, &error);
631
632 if (bus->nodes_modified)
633 return 0;
634
635 r = sd_bus_message_close_container(reply);
636 if (r < 0)
637 return r;
638
639 } else {
640 const char *signature = NULL;
641 char type = 0;
642
643 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
644 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
645
646 /* Avoid that we call the set routine more than once
647 * if the processing of this message got restarted
648 * because the node tree changed. */
649 if (c->last_iteration == bus->iteration_counter)
650 return 0;
651
652 c->last_iteration = bus->iteration_counter;
653
654 r = sd_bus_message_peek_type(m, &type, &signature);
655 if (r < 0)
656 return r;
657
658 if (type != 'v')
659 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_SIGNATURE,
660 "Incorrect signature when setting property '%s', expected 'v', got '%c'.",
661 c->member, type);
662 if (!streq(strempty(signature), strempty(c->vtable->x.property.signature)))
663 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS,
664 "Incorrect parameters for property '%s', expected '%s', got '%s'.",
665 c->member, strempty(c->vtable->x.property.signature), strempty(signature));
666
667 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
668 if (r < 0)
669 return r;
670
671 r = check_access(bus, m, c, &error);
672 if (r < 0)
673 return bus_maybe_reply_error(m, r, &error);
674
675 r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
676 if (r < 0)
677 return bus_maybe_reply_error(m, r, &error);
678
679 if (bus->nodes_modified)
680 return 0;
681
682 r = sd_bus_message_exit_container(m);
683 if (r < 0)
684 return r;
685 }
686
687 r = sd_bus_send(bus, reply, NULL);
688 if (r < 0)
689 return r;
690
691 return 1;
692 }
693
694 static int vtable_append_one_property(
695 sd_bus *bus,
696 sd_bus_message *reply,
697 const char *path,
698 struct node_vtable *c,
699 const sd_bus_vtable *v,
700 void *userdata,
701 sd_bus_error *error) {
702
703 sd_bus_slot *slot;
704 int r;
705
706 assert(bus);
707 assert(reply);
708 assert(path);
709 assert(c);
710 assert(v);
711
712 if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
713 r = sd_bus_message_sensitive(reply);
714 if (r < 0)
715 return r;
716 }
717
718 r = sd_bus_message_open_container(reply, 'e', "sv");
719 if (r < 0)
720 return r;
721
722 r = sd_bus_message_append(reply, "s", v->x.property.member);
723 if (r < 0)
724 return r;
725
726 r = sd_bus_message_open_container(reply, 'v', v->x.property.signature);
727 if (r < 0)
728 return r;
729
730 slot = container_of(c, sd_bus_slot, node_vtable);
731
732 r = invoke_property_get(bus, slot, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), error);
733 if (r < 0)
734 return r;
735 if (bus->nodes_modified)
736 return 0;
737
738 r = sd_bus_message_close_container(reply);
739 if (r < 0)
740 return r;
741
742 r = sd_bus_message_close_container(reply);
743 if (r < 0)
744 return r;
745
746 return 0;
747 }
748
749 static int vtable_append_all_properties(
750 sd_bus *bus,
751 sd_bus_message *reply,
752 const char *path,
753 struct node_vtable *c,
754 void *userdata,
755 sd_bus_error *error) {
756
757 const sd_bus_vtable *v;
758 int r;
759
760 assert(bus);
761 assert(reply);
762 assert(path);
763 assert(c);
764
765 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
766 return 1;
767
768 v = c->vtable;
769 for (v = bus_vtable_next(c->vtable, v); v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(c->vtable, v)) {
770 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
771 continue;
772
773 if (v->flags & SD_BUS_VTABLE_HIDDEN)
774 continue;
775
776 /* Let's not include properties marked as "explicit" in any message that contains a generic
777 * dump of properties, but only in those generated as a response to an explicit request. */
778 if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
779 continue;
780
781 /* Let's not include properties marked only for invalidation on change (i.e. in contrast to
782 * those whose new values are included in PropertiesChanges message) in any signals. This is
783 * useful to ensure they aren't included in InterfacesAdded messages. */
784 if (reply->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
785 FLAGS_SET(v->flags, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
786 continue;
787
788 r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
789 if (r < 0)
790 return r;
791 if (bus->nodes_modified)
792 return 0;
793 }
794
795 return 1;
796 }
797
798 static int property_get_all_callbacks_run(
799 sd_bus *bus,
800 sd_bus_message *m,
801 struct node_vtable *first,
802 bool require_fallback,
803 const char *iface,
804 bool *found_object) {
805
806 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
807 bool found_interface;
808 int r;
809
810 assert(bus);
811 assert(m);
812 assert(found_object);
813
814 r = sd_bus_message_new_method_return(m, &reply);
815 if (r < 0)
816 return r;
817
818 r = sd_bus_message_open_container(reply, 'a', "{sv}");
819 if (r < 0)
820 return r;
821
822 found_interface = !iface || STR_IN_SET(iface,
823 "org.freedesktop.DBus.Properties",
824 "org.freedesktop.DBus.Peer",
825 "org.freedesktop.DBus.Introspectable");
826
827 LIST_FOREACH(vtables, c, first) {
828 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
829 void *u;
830
831 if (require_fallback && !c->is_fallback)
832 continue;
833
834 r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
835 if (r < 0)
836 return bus_maybe_reply_error(m, r, &error);
837 if (bus->nodes_modified)
838 return 0;
839 if (r == 0)
840 continue;
841
842 *found_object = true;
843
844 if (iface && !streq(c->interface, iface))
845 continue;
846 found_interface = true;
847
848 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
849 if (r < 0)
850 return bus_maybe_reply_error(m, r, &error);
851 if (bus->nodes_modified)
852 return 0;
853 }
854
855 if (!*found_object)
856 return 0;
857
858 if (!found_interface) {
859 r = sd_bus_reply_method_errorf(
860 m,
861 SD_BUS_ERROR_UNKNOWN_INTERFACE,
862 "Unknown interface '%s'.", iface);
863 if (r < 0)
864 return r;
865
866 return 1;
867 }
868
869 r = sd_bus_message_close_container(reply);
870 if (r < 0)
871 return r;
872
873 r = sd_bus_send(bus, reply, NULL);
874 if (r < 0)
875 return r;
876
877 return 1;
878 }
879
880 static int bus_node_exists(
881 sd_bus *bus,
882 struct node *n,
883 const char *path,
884 bool require_fallback) {
885
886 int r;
887
888 assert(bus);
889 assert(n);
890 assert(path);
891
892 /* Tests if there's anything attached directly to this node
893 * for the specified path */
894
895 if (!require_fallback && (n->enumerators || n->object_managers))
896 return true;
897
898 LIST_FOREACH(callbacks, k, n->callbacks) {
899 if (require_fallback && !k->is_fallback)
900 continue;
901
902 return 1;
903 }
904
905 LIST_FOREACH(vtables, c, n->vtables) {
906 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
907
908 if (require_fallback && !c->is_fallback)
909 continue;
910
911 r = node_vtable_get_userdata(bus, path, c, NULL, &error);
912 if (r != 0)
913 return r;
914 if (bus->nodes_modified)
915 return 0;
916 }
917
918 return 0;
919 }
920
921 int introspect_path(
922 sd_bus *bus,
923 const char *path,
924 struct node *n,
925 bool require_fallback,
926 bool ignore_nodes_modified,
927 bool *found_object,
928 char **ret,
929 sd_bus_error *error) {
930
931 _cleanup_ordered_set_free_ OrderedSet *s = NULL;
932 _cleanup_(introspect_free) struct introspect intro = {};
933 bool empty;
934 int r;
935
936 if (!n) {
937 n = hashmap_get(bus->nodes, path);
938 if (!n)
939 return -ENOENT;
940 }
941
942 r = get_child_nodes(bus, path, n, 0, &s, error);
943 if (r < 0)
944 return r;
945 if (bus->nodes_modified && !ignore_nodes_modified)
946 return 0;
947
948 r = introspect_begin(&intro, bus->trusted);
949 if (r < 0)
950 return r;
951
952 r = introspect_write_default_interfaces(&intro, !require_fallback && n->object_managers);
953 if (r < 0)
954 return r;
955
956 empty = ordered_set_isempty(s);
957
958 LIST_FOREACH(vtables, c, n->vtables) {
959 if (require_fallback && !c->is_fallback)
960 continue;
961
962 r = node_vtable_get_userdata(bus, path, c, NULL, error);
963 if (r < 0)
964 return r;
965 if (bus->nodes_modified && !ignore_nodes_modified)
966 return 0;
967 if (r == 0)
968 continue;
969
970 empty = false;
971
972 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
973 continue;
974
975 r = introspect_write_interface(&intro, c->interface, c->vtable);
976 if (r < 0)
977 return r;
978 }
979
980 if (empty) {
981 /* Nothing?, let's see if we exist at all, and if not
982 * refuse to do anything */
983 r = bus_node_exists(bus, n, path, require_fallback);
984 if (r <= 0)
985 return r;
986 if (bus->nodes_modified && !ignore_nodes_modified)
987 return 0;
988 }
989
990 if (found_object)
991 *found_object = true;
992
993 r = introspect_write_child_nodes(&intro, s, path);
994 if (r < 0)
995 return r;
996
997 r = introspect_finish(&intro, ret);
998 if (r < 0)
999 return r;
1000
1001 return 1;
1002 }
1003
1004 static int process_introspect(
1005 sd_bus *bus,
1006 sd_bus_message *m,
1007 struct node *n,
1008 bool require_fallback,
1009 bool *found_object) {
1010
1011 _cleanup_free_ char *s = NULL;
1012 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1013 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1014 int r;
1015
1016 assert(bus);
1017 assert(m);
1018 assert(n);
1019 assert(found_object);
1020
1021 r = introspect_path(bus, m->path, n, require_fallback, false, found_object, &s, &error);
1022 if (r < 0)
1023 return bus_maybe_reply_error(m, r, &error);
1024 if (r == 0)
1025 /* nodes_modified == true */
1026 return 0;
1027
1028 r = sd_bus_message_new_method_return(m, &reply);
1029 if (r < 0)
1030 return r;
1031
1032 r = sd_bus_message_append(reply, "s", s);
1033 if (r < 0)
1034 return r;
1035
1036 r = sd_bus_send(bus, reply, NULL);
1037 if (r < 0)
1038 return r;
1039
1040 return 1;
1041 }
1042
1043 static int object_manager_serialize_path(
1044 sd_bus *bus,
1045 sd_bus_message *reply,
1046 const char *prefix,
1047 const char *path,
1048 bool require_fallback,
1049 sd_bus_error *error) {
1050
1051 const char *previous_interface = NULL;
1052 bool found_something = false;
1053 struct node *n;
1054 int r;
1055
1056 assert(bus);
1057 assert(reply);
1058 assert(prefix);
1059 assert(path);
1060 assert(error);
1061
1062 n = hashmap_get(bus->nodes, prefix);
1063 if (!n)
1064 return 0;
1065
1066 LIST_FOREACH(vtables, i, n->vtables) {
1067 void *u;
1068
1069 if (require_fallback && !i->is_fallback)
1070 continue;
1071
1072 r = node_vtable_get_userdata(bus, path, i, &u, error);
1073 if (r < 0)
1074 return r;
1075 if (bus->nodes_modified)
1076 return 0;
1077 if (r == 0)
1078 continue;
1079
1080 if (!found_something) {
1081
1082 /* Open the object part */
1083
1084 r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}");
1085 if (r < 0)
1086 return r;
1087
1088 r = sd_bus_message_append(reply, "o", path);
1089 if (r < 0)
1090 return r;
1091
1092 r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}");
1093 if (r < 0)
1094 return r;
1095
1096 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1097 if (r < 0)
1098 return r;
1099
1100 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1101 if (r < 0)
1102 return r;
1103
1104 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1105 if (r < 0)
1106 return r;
1107
1108 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1109 if (r < 0)
1110 return r;
1111
1112 found_something = true;
1113 }
1114
1115 if (!streq_ptr(previous_interface, i->interface)) {
1116
1117 /* Maybe close the previous interface part */
1118
1119 if (previous_interface) {
1120 r = sd_bus_message_close_container(reply);
1121 if (r < 0)
1122 return r;
1123
1124 r = sd_bus_message_close_container(reply);
1125 if (r < 0)
1126 return r;
1127 }
1128
1129 /* Open the new interface part */
1130
1131 r = sd_bus_message_open_container(reply, 'e', "sa{sv}");
1132 if (r < 0)
1133 return r;
1134
1135 r = sd_bus_message_append(reply, "s", i->interface);
1136 if (r < 0)
1137 return r;
1138
1139 r = sd_bus_message_open_container(reply, 'a', "{sv}");
1140 if (r < 0)
1141 return r;
1142 }
1143
1144 r = vtable_append_all_properties(bus, reply, path, i, u, error);
1145 if (r < 0)
1146 return r;
1147 if (bus->nodes_modified)
1148 return 0;
1149
1150 previous_interface = i->interface;
1151 }
1152
1153 if (previous_interface) {
1154 r = sd_bus_message_close_container(reply);
1155 if (r < 0)
1156 return r;
1157
1158 r = sd_bus_message_close_container(reply);
1159 if (r < 0)
1160 return r;
1161 }
1162
1163 if (found_something) {
1164 r = sd_bus_message_close_container(reply);
1165 if (r < 0)
1166 return r;
1167
1168 r = sd_bus_message_close_container(reply);
1169 if (r < 0)
1170 return r;
1171 }
1172
1173 return 1;
1174 }
1175
1176 static int object_manager_serialize_path_and_fallbacks(
1177 sd_bus *bus,
1178 sd_bus_message *reply,
1179 const char *path,
1180 sd_bus_error *error) {
1181
1182 _cleanup_free_ char *prefix = NULL;
1183 size_t pl;
1184 int r;
1185
1186 assert(bus);
1187 assert(reply);
1188 assert(path);
1189 assert(error);
1190
1191 /* First, add all vtables registered for this path */
1192 r = object_manager_serialize_path(bus, reply, path, path, false, error);
1193 if (r < 0)
1194 return r;
1195 if (bus->nodes_modified)
1196 return 0;
1197
1198 /* Second, add fallback vtables registered for any of the prefixes */
1199 pl = strlen(path);
1200 assert(pl <= BUS_PATH_SIZE_MAX);
1201 prefix = new(char, pl + 1);
1202 if (!prefix)
1203 return -ENOMEM;
1204
1205 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1206 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
1207 if (r < 0)
1208 return r;
1209 if (bus->nodes_modified)
1210 return 0;
1211 }
1212
1213 return 0;
1214 }
1215
1216 static int process_get_managed_objects(
1217 sd_bus *bus,
1218 sd_bus_message *m,
1219 struct node *n,
1220 bool require_fallback,
1221 bool *found_object) {
1222
1223 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1224 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1225 _cleanup_ordered_set_free_free_ OrderedSet *s = NULL;
1226 char *path;
1227 int r;
1228
1229 assert(bus);
1230 assert(m);
1231 assert(n);
1232 assert(found_object);
1233
1234 /* Spec says, GetManagedObjects() is only implemented on the root of a
1235 * sub-tree. Therefore, we require a registered object-manager on
1236 * exactly the queried path, otherwise, we refuse to respond. */
1237
1238 if (require_fallback || !n->object_managers)
1239 return 0;
1240
1241 r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
1242 if (r < 0)
1243 return bus_maybe_reply_error(m, r, &error);
1244 if (bus->nodes_modified)
1245 return 0;
1246
1247 r = sd_bus_message_new_method_return(m, &reply);
1248 if (r < 0)
1249 return r;
1250
1251 r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}");
1252 if (r < 0)
1253 return r;
1254
1255 ORDERED_SET_FOREACH(path, s) {
1256 r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
1257 if (r < 0)
1258 return bus_maybe_reply_error(m, r, &error);
1259
1260 if (bus->nodes_modified)
1261 return 0;
1262 }
1263
1264 r = sd_bus_message_close_container(reply);
1265 if (r < 0)
1266 return r;
1267
1268 r = sd_bus_send(bus, reply, NULL);
1269 if (r < 0)
1270 return r;
1271
1272 return 1;
1273 }
1274
1275 static int object_find_and_run(
1276 sd_bus *bus,
1277 sd_bus_message *m,
1278 const char *p,
1279 bool require_fallback,
1280 bool *found_object) {
1281
1282 struct node *n;
1283 struct vtable_member vtable_key, *v;
1284 int r;
1285
1286 assert(bus);
1287 assert(m);
1288 assert(p);
1289 assert(found_object);
1290
1291 n = hashmap_get(bus->nodes, p);
1292 if (!n)
1293 return 0;
1294
1295 /* First, try object callbacks */
1296 r = node_callbacks_run(bus, m, n->callbacks, require_fallback, found_object);
1297 if (r != 0)
1298 return r;
1299 if (bus->nodes_modified)
1300 return 0;
1301
1302 if (!m->interface || !m->member)
1303 return 0;
1304
1305 /* Then, look for a known method */
1306 vtable_key.path = (char*) p;
1307 vtable_key.interface = m->interface;
1308 vtable_key.member = m->member;
1309
1310 v = hashmap_get(bus->vtable_methods, &vtable_key);
1311 if (v) {
1312 r = method_callbacks_run(bus, m, v, require_fallback, found_object);
1313 if (r != 0)
1314 return r;
1315 if (bus->nodes_modified)
1316 return 0;
1317 }
1318
1319 /* Then, look for a known property */
1320 if (streq(m->interface, "org.freedesktop.DBus.Properties")) {
1321 bool get = false;
1322
1323 get = streq(m->member, "Get");
1324
1325 if (get || streq(m->member, "Set")) {
1326
1327 r = sd_bus_message_rewind(m, true);
1328 if (r < 0)
1329 return r;
1330
1331 vtable_key.path = (char*) p;
1332
1333 r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member);
1334 if (r < 0)
1335 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters");
1336
1337 v = hashmap_get(bus->vtable_properties, &vtable_key);
1338 if (v) {
1339 r = property_get_set_callbacks_run(bus, m, v, require_fallback, get, found_object);
1340 if (r != 0)
1341 return r;
1342 }
1343
1344 } else if (streq(m->member, "GetAll")) {
1345 const char *iface;
1346
1347 r = sd_bus_message_rewind(m, true);
1348 if (r < 0)
1349 return r;
1350
1351 r = sd_bus_message_read(m, "s", &iface);
1352 if (r < 0)
1353 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter");
1354
1355 if (iface[0] == 0)
1356 iface = NULL;
1357
1358 r = property_get_all_callbacks_run(bus, m, n->vtables, require_fallback, iface, found_object);
1359 if (r != 0)
1360 return r;
1361 }
1362
1363 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1364
1365 if (!isempty(sd_bus_message_get_signature(m, true)))
1366 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1367
1368 r = process_introspect(bus, m, n, require_fallback, found_object);
1369 if (r != 0)
1370 return r;
1371
1372 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1373
1374 if (!isempty(sd_bus_message_get_signature(m, true)))
1375 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1376
1377 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
1378 if (r != 0)
1379 return r;
1380 }
1381
1382 if (bus->nodes_modified)
1383 return 0;
1384
1385 if (!*found_object) {
1386 r = bus_node_exists(bus, n, m->path, require_fallback);
1387 if (r < 0)
1388 return bus_maybe_reply_error(m, r, NULL);
1389 if (bus->nodes_modified)
1390 return 0;
1391 if (r > 0)
1392 *found_object = true;
1393 }
1394
1395 return 0;
1396 }
1397
1398 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
1399 _cleanup_free_ char *prefix = NULL;
1400 int r;
1401 size_t pl;
1402 bool found_object = false;
1403
1404 assert(bus);
1405 assert(m);
1406
1407 if (bus->is_monitor)
1408 return 0;
1409
1410 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1411 return 0;
1412
1413 if (hashmap_isempty(bus->nodes))
1414 return 0;
1415
1416 /* Never respond to broadcast messages */
1417 if (bus->bus_client && !m->destination)
1418 return 0;
1419
1420 assert(m->path);
1421 assert(m->member);
1422
1423 pl = strlen(m->path);
1424 assert(pl <= BUS_PATH_SIZE_MAX);
1425 prefix = new(char, pl + 1);
1426 if (!prefix)
1427 return -ENOMEM;
1428
1429 do {
1430 bus->nodes_modified = false;
1431
1432 r = object_find_and_run(bus, m, m->path, false, &found_object);
1433 if (r != 0)
1434 return r;
1435
1436 /* Look for fallback prefixes */
1437 OBJECT_PATH_FOREACH_PREFIX(prefix, m->path) {
1438
1439 if (bus->nodes_modified)
1440 break;
1441
1442 r = object_find_and_run(bus, m, prefix, true, &found_object);
1443 if (r != 0)
1444 return r;
1445 }
1446
1447 } while (bus->nodes_modified);
1448
1449 if (!found_object)
1450 return 0;
1451
1452 if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get") ||
1453 sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set")) {
1454 const char *interface = NULL, *property = NULL;
1455
1456 (void) sd_bus_message_rewind(m, true);
1457 (void) sd_bus_message_read_basic(m, 's', &interface);
1458 (void) sd_bus_message_read_basic(m, 's', &property);
1459
1460 r = sd_bus_reply_method_errorf(
1461 m,
1462 SD_BUS_ERROR_UNKNOWN_PROPERTY,
1463 "Unknown interface %s or property %s.", strnull(interface), strnull(property));
1464 } else
1465 r = sd_bus_reply_method_errorf(
1466 m,
1467 SD_BUS_ERROR_UNKNOWN_METHOD,
1468 "Unknown method %s or interface %s.", m->member, m->interface);
1469
1470 if (r < 0)
1471 return r;
1472
1473 return 1;
1474 }
1475
1476 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
1477 struct node *n, *parent;
1478 const char *e;
1479 _cleanup_free_ char *s = NULL;
1480 char *p;
1481 int r;
1482
1483 assert(bus);
1484 assert(path);
1485 assert(path[0] == '/');
1486
1487 n = hashmap_get(bus->nodes, path);
1488 if (n)
1489 return n;
1490
1491 r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops);
1492 if (r < 0)
1493 return NULL;
1494
1495 s = strdup(path);
1496 if (!s)
1497 return NULL;
1498
1499 if (streq(path, "/"))
1500 parent = NULL;
1501 else {
1502 e = strrchr(path, '/');
1503 assert(e);
1504
1505 p = strndupa_safe(path, MAX(1, e - path));
1506
1507 parent = bus_node_allocate(bus, p);
1508 if (!parent)
1509 return NULL;
1510 }
1511
1512 n = new0(struct node, 1);
1513 if (!n)
1514 return NULL;
1515
1516 n->parent = parent;
1517 n->path = TAKE_PTR(s);
1518
1519 r = hashmap_put(bus->nodes, n->path, n);
1520 if (r < 0) {
1521 free(n->path);
1522 return mfree(n);
1523 }
1524
1525 if (parent)
1526 LIST_PREPEND(siblings, parent->child, n);
1527
1528 return n;
1529 }
1530
1531 void bus_node_gc(sd_bus *b, struct node *n) {
1532 assert(b);
1533
1534 if (!n)
1535 return;
1536
1537 if (n->child ||
1538 n->callbacks ||
1539 n->vtables ||
1540 n->enumerators ||
1541 n->object_managers)
1542 return;
1543
1544 assert_se(hashmap_remove(b->nodes, n->path) == n);
1545
1546 if (n->parent)
1547 LIST_REMOVE(siblings, n->parent->child, n);
1548
1549 free(n->path);
1550 bus_node_gc(b, n->parent);
1551 free(n);
1552 }
1553
1554 static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const char *path) {
1555 struct node *n;
1556
1557 assert(bus);
1558 assert(path);
1559
1560 n = hashmap_get(bus->nodes, path);
1561 if (!n) {
1562 _cleanup_free_ char *prefix = NULL;
1563 size_t pl;
1564
1565 pl = strlen(path);
1566 assert(pl <= BUS_PATH_SIZE_MAX);
1567 prefix = new(char, pl + 1);
1568 if (!prefix)
1569 return -ENOMEM;
1570
1571 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1572 n = hashmap_get(bus->nodes, prefix);
1573 if (n)
1574 break;
1575 }
1576 }
1577
1578 while (n && !n->object_managers)
1579 n = n->parent;
1580
1581 if (out)
1582 *out = n;
1583 return !!n;
1584 }
1585
1586 static int bus_add_object(
1587 sd_bus *bus,
1588 sd_bus_slot **slot,
1589 bool fallback,
1590 const char *path,
1591 sd_bus_message_handler_t callback,
1592 void *userdata) {
1593
1594 sd_bus_slot *s;
1595 struct node *n;
1596 int r;
1597
1598 assert_return(bus, -EINVAL);
1599 assert_return(bus = bus_resolve(bus), -ENOPKG);
1600 assert_return(object_path_is_valid(path), -EINVAL);
1601 assert_return(callback, -EINVAL);
1602 assert_return(!bus_pid_changed(bus), -ECHILD);
1603
1604 n = bus_node_allocate(bus, path);
1605 if (!n)
1606 return -ENOMEM;
1607
1608 s = bus_slot_allocate(bus, !slot, BUS_NODE_CALLBACK, sizeof(struct node_callback), userdata);
1609 if (!s) {
1610 r = -ENOMEM;
1611 goto fail;
1612 }
1613
1614 s->node_callback.callback = callback;
1615 s->node_callback.is_fallback = fallback;
1616
1617 s->node_callback.node = n;
1618 LIST_PREPEND(callbacks, n->callbacks, &s->node_callback);
1619 bus->nodes_modified = true;
1620
1621 if (slot)
1622 *slot = s;
1623
1624 return 0;
1625
1626 fail:
1627 sd_bus_slot_unref(s);
1628 bus_node_gc(bus, n);
1629
1630 return r;
1631 }
1632
1633 _public_ int sd_bus_add_object(
1634 sd_bus *bus,
1635 sd_bus_slot **slot,
1636 const char *path,
1637 sd_bus_message_handler_t callback,
1638 void *userdata) {
1639
1640 return bus_add_object(bus, slot, false, path, callback, userdata);
1641 }
1642
1643 _public_ int sd_bus_add_fallback(
1644 sd_bus *bus,
1645 sd_bus_slot **slot,
1646 const char *prefix,
1647 sd_bus_message_handler_t callback,
1648 void *userdata) {
1649
1650 return bus_add_object(bus, slot, true, prefix, callback, userdata);
1651 }
1652
1653 static void vtable_member_hash_func(const struct vtable_member *m, struct siphash *state) {
1654 assert(m);
1655
1656 string_hash_func(m->path, state);
1657 string_hash_func(m->interface, state);
1658 string_hash_func(m->member, state);
1659 }
1660
1661 static int vtable_member_compare_func(const struct vtable_member *x, const struct vtable_member *y) {
1662 int r;
1663
1664 assert(x);
1665 assert(y);
1666
1667 r = strcmp(x->path, y->path);
1668 if (r != 0)
1669 return r;
1670
1671 r = strcmp(x->interface, y->interface);
1672 if (r != 0)
1673 return r;
1674
1675 return strcmp(x->member, y->member);
1676 }
1677
1678 DEFINE_PRIVATE_HASH_OPS(vtable_member_hash_ops, struct vtable_member, vtable_member_hash_func, vtable_member_compare_func);
1679
1680 typedef enum {
1681 NAMES_FIRST_PART = 1 << 0, /* first part of argument name list (input names). It is reset by names_are_valid() */
1682 NAMES_PRESENT = 1 << 1, /* at least one argument name is present, so the names will checked.
1683 This flag is set and used internally by names_are_valid(), but needs to be stored across calls for 2-parts list */
1684 NAMES_SINGLE_PART = 1 << 2, /* argument name list consisting of a single part */
1685 } names_flags;
1686
1687 static bool names_are_valid(const char *signature, const char **names, names_flags *flags) {
1688 int r;
1689
1690 if ((*flags & NAMES_FIRST_PART || *flags & NAMES_SINGLE_PART) && **names != '\0')
1691 *flags |= NAMES_PRESENT;
1692
1693 for (;*flags & NAMES_PRESENT;) {
1694 size_t l;
1695
1696 if (!*signature)
1697 break;
1698
1699 r = signature_element_length(signature, &l);
1700 if (r < 0)
1701 return false;
1702
1703 if (**names != '\0') {
1704 if (!member_name_is_valid(*names))
1705 return false;
1706 *names += strlen(*names) + 1;
1707 } else if (*flags & NAMES_PRESENT)
1708 return false;
1709
1710 signature += l;
1711 }
1712 /* let's check if there are more argument names specified than the signature allows */
1713 if (*flags & NAMES_PRESENT && **names != '\0' && !(*flags & NAMES_FIRST_PART))
1714 return false;
1715 *flags &= ~NAMES_FIRST_PART;
1716 return true;
1717 }
1718
1719 /* the current version of this struct is defined in sd-bus-vtable.h, but we need to list here the historical versions
1720 to make sure the calling code is compatible with one of these */
1721 struct sd_bus_vtable_221 {
1722 uint8_t type:8;
1723 uint64_t flags:56;
1724 union {
1725 struct {
1726 size_t element_size;
1727 } start;
1728 struct {
1729 const char *member;
1730 const char *signature;
1731 const char *result;
1732 sd_bus_message_handler_t handler;
1733 size_t offset;
1734 } method;
1735 struct {
1736 const char *member;
1737 const char *signature;
1738 } signal;
1739 struct {
1740 const char *member;
1741 const char *signature;
1742 sd_bus_property_get_t get;
1743 sd_bus_property_set_t set;
1744 size_t offset;
1745 } property;
1746 } x;
1747 };
1748 /* Structure size up to v241 */
1749 #define VTABLE_ELEMENT_SIZE_221 sizeof(struct sd_bus_vtable_221)
1750
1751 /* Size of the structure when "features" field was added. If the structure definition is augmented, a copy of
1752 * the structure definition will need to be made (similarly to the sd_bus_vtable_221 above), and this
1753 * definition updated to refer to it. */
1754 #define VTABLE_ELEMENT_SIZE_242 sizeof(struct sd_bus_vtable)
1755
1756 static int vtable_features(const sd_bus_vtable *vtable) {
1757 if (vtable[0].x.start.element_size < VTABLE_ELEMENT_SIZE_242 ||
1758 !vtable[0].x.start.vtable_format_reference)
1759 return 0;
1760 return vtable[0].x.start.features;
1761 }
1762
1763 bool bus_vtable_has_names(const sd_bus_vtable *vtable) {
1764 return vtable_features(vtable) & _SD_BUS_VTABLE_PARAM_NAMES;
1765 }
1766
1767 const sd_bus_vtable* bus_vtable_next(const sd_bus_vtable *vtable, const sd_bus_vtable *v) {
1768 return (const sd_bus_vtable*) ((char*) v + vtable[0].x.start.element_size);
1769 }
1770
1771 static int add_object_vtable_internal(
1772 sd_bus *bus,
1773 sd_bus_slot **slot,
1774 const char *path,
1775 const char *interface,
1776 const sd_bus_vtable *vtable,
1777 bool fallback,
1778 sd_bus_object_find_t find,
1779 void *userdata) {
1780
1781 sd_bus_slot *s = NULL;
1782 struct node_vtable *existing = NULL;
1783 const sd_bus_vtable *v;
1784 struct node *n;
1785 int r;
1786 const char *names = "";
1787 names_flags nf;
1788
1789 assert_return(bus, -EINVAL);
1790 assert_return(bus = bus_resolve(bus), -ENOPKG);
1791 assert_return(object_path_is_valid(path), -EINVAL);
1792 assert_return(interface_name_is_valid(interface), -EINVAL);
1793 assert_return(vtable, -EINVAL);
1794 assert_return(vtable[0].type == _SD_BUS_VTABLE_START, -EINVAL);
1795 assert_return(vtable[0].x.start.element_size == VTABLE_ELEMENT_SIZE_221 ||
1796 vtable[0].x.start.element_size >= VTABLE_ELEMENT_SIZE_242,
1797 -EINVAL);
1798 assert_return(!bus_pid_changed(bus), -ECHILD);
1799 assert_return(!streq(interface, "org.freedesktop.DBus.Properties") &&
1800 !streq(interface, "org.freedesktop.DBus.Introspectable") &&
1801 !streq(interface, "org.freedesktop.DBus.Peer") &&
1802 !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL);
1803
1804 r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops);
1805 if (r < 0)
1806 return r;
1807
1808 r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops);
1809 if (r < 0)
1810 return r;
1811
1812 n = bus_node_allocate(bus, path);
1813 if (!n)
1814 return -ENOMEM;
1815
1816 LIST_FOREACH(vtables, i, n->vtables) {
1817 if (i->is_fallback != fallback) {
1818 r = -EPROTOTYPE;
1819 goto fail;
1820 }
1821
1822 if (streq(i->interface, interface)) {
1823
1824 if (i->vtable == vtable) {
1825 r = -EEXIST;
1826 goto fail;
1827 }
1828
1829 existing = i;
1830 }
1831 }
1832
1833 s = bus_slot_allocate(bus, !slot, BUS_NODE_VTABLE, sizeof(struct node_vtable), userdata);
1834 if (!s) {
1835 r = -ENOMEM;
1836 goto fail;
1837 }
1838
1839 s->node_vtable.is_fallback = fallback;
1840 s->node_vtable.vtable = vtable;
1841 s->node_vtable.find = find;
1842
1843 s->node_vtable.interface = strdup(interface);
1844 if (!s->node_vtable.interface) {
1845 r = -ENOMEM;
1846 goto fail;
1847 }
1848
1849 v = s->node_vtable.vtable;
1850 for (v = bus_vtable_next(vtable, v); v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(vtable, v)) {
1851
1852 switch (v->type) {
1853
1854 case _SD_BUS_VTABLE_METHOD: {
1855 struct vtable_member *m;
1856 nf = NAMES_FIRST_PART;
1857
1858 if (bus_vtable_has_names(vtable))
1859 names = strempty(v->x.method.names);
1860
1861 if (!member_name_is_valid(v->x.method.member) ||
1862 !signature_is_valid(strempty(v->x.method.signature), false) ||
1863 !signature_is_valid(strempty(v->x.method.result), false) ||
1864 !names_are_valid(strempty(v->x.method.signature), &names, &nf) ||
1865 !names_are_valid(strempty(v->x.method.result), &names, &nf) ||
1866 !(v->x.method.handler || (isempty(v->x.method.signature) && isempty(v->x.method.result))) ||
1867 v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) {
1868 r = -EINVAL;
1869 goto fail;
1870 }
1871
1872 m = new0(struct vtable_member, 1);
1873 if (!m) {
1874 r = -ENOMEM;
1875 goto fail;
1876 }
1877
1878 m->parent = &s->node_vtable;
1879 m->path = n->path;
1880 m->interface = s->node_vtable.interface;
1881 m->member = v->x.method.member;
1882 m->vtable = v;
1883
1884 r = hashmap_put(bus->vtable_methods, m, m);
1885 if (r < 0) {
1886 free(m);
1887 goto fail;
1888 }
1889
1890 break;
1891 }
1892
1893 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
1894
1895 if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
1896 r = -EINVAL;
1897 goto fail;
1898 }
1899
1900 if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
1901 r = -EINVAL;
1902 goto fail;
1903 }
1904
1905 _fallthrough_;
1906 case _SD_BUS_VTABLE_PROPERTY: {
1907 struct vtable_member *m;
1908
1909 if (!member_name_is_valid(v->x.property.member) ||
1910 !signature_is_single(v->x.property.signature, false) ||
1911 !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
1912 (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
1913 (!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
1914 ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
1915 (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
1916 r = -EINVAL;
1917 goto fail;
1918 }
1919
1920 m = new0(struct vtable_member, 1);
1921 if (!m) {
1922 r = -ENOMEM;
1923 goto fail;
1924 }
1925
1926 m->parent = &s->node_vtable;
1927 m->path = n->path;
1928 m->interface = s->node_vtable.interface;
1929 m->member = v->x.property.member;
1930 m->vtable = v;
1931
1932 r = hashmap_put(bus->vtable_properties, m, m);
1933 if (r < 0) {
1934 free(m);
1935 goto fail;
1936 }
1937
1938 break;
1939 }
1940
1941 case _SD_BUS_VTABLE_SIGNAL:
1942 nf = NAMES_SINGLE_PART;
1943
1944 if (bus_vtable_has_names(vtable))
1945 names = strempty(v->x.signal.names);
1946
1947 if (!member_name_is_valid(v->x.signal.member) ||
1948 !signature_is_valid(strempty(v->x.signal.signature), false) ||
1949 !names_are_valid(strempty(v->x.signal.signature), &names, &nf) ||
1950 v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
1951 r = -EINVAL;
1952 goto fail;
1953 }
1954
1955 break;
1956
1957 default:
1958 r = -EINVAL;
1959 goto fail;
1960 }
1961 }
1962
1963 s->node_vtable.node = n;
1964 LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
1965 bus->nodes_modified = true;
1966
1967 if (slot)
1968 *slot = s;
1969
1970 return 0;
1971
1972 fail:
1973 sd_bus_slot_unref(s);
1974 bus_node_gc(bus, n);
1975
1976 return r;
1977 }
1978
1979 /* This symbol exists solely to tell the linker that the "new" vtable format is used. */
1980 _public_ const unsigned sd_bus_object_vtable_format = 242;
1981
1982 _public_ int sd_bus_add_object_vtable(
1983 sd_bus *bus,
1984 sd_bus_slot **slot,
1985 const char *path,
1986 const char *interface,
1987 const sd_bus_vtable *vtable,
1988 void *userdata) {
1989
1990 return add_object_vtable_internal(bus, slot, path, interface, vtable, false, NULL, userdata);
1991 }
1992
1993 _public_ int sd_bus_add_fallback_vtable(
1994 sd_bus *bus,
1995 sd_bus_slot **slot,
1996 const char *prefix,
1997 const char *interface,
1998 const sd_bus_vtable *vtable,
1999 sd_bus_object_find_t find,
2000 void *userdata) {
2001
2002 return add_object_vtable_internal(bus, slot, prefix, interface, vtable, true, find, userdata);
2003 }
2004
2005 _public_ int sd_bus_add_node_enumerator(
2006 sd_bus *bus,
2007 sd_bus_slot **slot,
2008 const char *path,
2009 sd_bus_node_enumerator_t callback,
2010 void *userdata) {
2011
2012 sd_bus_slot *s;
2013 struct node *n;
2014 int r;
2015
2016 assert_return(bus, -EINVAL);
2017 assert_return(bus = bus_resolve(bus), -ENOPKG);
2018 assert_return(object_path_is_valid(path), -EINVAL);
2019 assert_return(callback, -EINVAL);
2020 assert_return(!bus_pid_changed(bus), -ECHILD);
2021
2022 n = bus_node_allocate(bus, path);
2023 if (!n)
2024 return -ENOMEM;
2025
2026 s = bus_slot_allocate(bus, !slot, BUS_NODE_ENUMERATOR, sizeof(struct node_enumerator), userdata);
2027 if (!s) {
2028 r = -ENOMEM;
2029 goto fail;
2030 }
2031
2032 s->node_enumerator.callback = callback;
2033
2034 s->node_enumerator.node = n;
2035 LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
2036 bus->nodes_modified = true;
2037
2038 if (slot)
2039 *slot = s;
2040
2041 return 0;
2042
2043 fail:
2044 sd_bus_slot_unref(s);
2045 bus_node_gc(bus, n);
2046
2047 return r;
2048 }
2049
2050 static int emit_properties_changed_on_interface(
2051 sd_bus *bus,
2052 const char *prefix,
2053 const char *path,
2054 const char *interface,
2055 bool require_fallback,
2056 bool *found_interface,
2057 char **names) {
2058
2059 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2060 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2061 bool has_invalidating = false, has_changing = false;
2062 struct vtable_member key = {};
2063 struct node *n;
2064 void *u = NULL;
2065 int r;
2066
2067 assert(bus);
2068 assert(prefix);
2069 assert(path);
2070 assert(interface);
2071 assert(found_interface);
2072
2073 n = hashmap_get(bus->nodes, prefix);
2074 if (!n)
2075 return 0;
2076
2077 r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
2078 if (r < 0)
2079 return r;
2080
2081 r = sd_bus_message_append(m, "s", interface);
2082 if (r < 0)
2083 return r;
2084
2085 r = sd_bus_message_open_container(m, 'a', "{sv}");
2086 if (r < 0)
2087 return r;
2088
2089 key.path = prefix;
2090 key.interface = interface;
2091
2092 LIST_FOREACH(vtables, c, n->vtables) {
2093 if (require_fallback && !c->is_fallback)
2094 continue;
2095
2096 if (!streq(c->interface, interface))
2097 continue;
2098
2099 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2100 if (r < 0)
2101 return r;
2102 if (bus->nodes_modified)
2103 return 0;
2104 if (r == 0)
2105 continue;
2106
2107 *found_interface = true;
2108
2109 if (names) {
2110 /* If the caller specified a list of
2111 * properties we include exactly those in the
2112 * PropertiesChanged message */
2113
2114 STRV_FOREACH(property, names) {
2115 struct vtable_member *v;
2116
2117 assert_return(member_name_is_valid(*property), -EINVAL);
2118
2119 key.member = *property;
2120 v = hashmap_get(bus->vtable_properties, &key);
2121 if (!v)
2122 return -ENOENT;
2123
2124 /* If there are two vtables for the same
2125 * interface, let's handle this property when
2126 * we come to that vtable. */
2127 if (c != v->parent)
2128 continue;
2129
2130 assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ||
2131 v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
2132
2133 assert_return(!(v->vtable->flags & SD_BUS_VTABLE_HIDDEN), -EDOM);
2134
2135 if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
2136 has_invalidating = true;
2137 continue;
2138 }
2139
2140 has_changing = true;
2141
2142 r = vtable_append_one_property(bus, m, m->path, c, v->vtable, u, &error);
2143 if (r < 0)
2144 return r;
2145 if (bus->nodes_modified)
2146 return 0;
2147 }
2148 } else {
2149 const sd_bus_vtable *v;
2150
2151 /* If the caller specified no properties list
2152 * we include all properties that are marked
2153 * as changing in the message. */
2154
2155 v = c->vtable;
2156 for (v = bus_vtable_next(c->vtable, v); v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(c->vtable, v)) {
2157 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2158 continue;
2159
2160 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2161 continue;
2162
2163 if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
2164 has_invalidating = true;
2165 continue;
2166 }
2167
2168 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
2169 continue;
2170
2171 has_changing = true;
2172
2173 r = vtable_append_one_property(bus, m, m->path, c, v, u, &error);
2174 if (r < 0)
2175 return r;
2176 if (bus->nodes_modified)
2177 return 0;
2178 }
2179 }
2180 }
2181
2182 if (!has_invalidating && !has_changing)
2183 return 0;
2184
2185 r = sd_bus_message_close_container(m);
2186 if (r < 0)
2187 return r;
2188
2189 r = sd_bus_message_open_container(m, 'a', "s");
2190 if (r < 0)
2191 return r;
2192
2193 if (has_invalidating) {
2194 LIST_FOREACH(vtables, c, n->vtables) {
2195 if (require_fallback && !c->is_fallback)
2196 continue;
2197
2198 if (!streq(c->interface, interface))
2199 continue;
2200
2201 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2202 if (r < 0)
2203 return r;
2204 if (bus->nodes_modified)
2205 return 0;
2206 if (r == 0)
2207 continue;
2208
2209 if (names) {
2210 STRV_FOREACH(property, names) {
2211 struct vtable_member *v;
2212
2213 key.member = *property;
2214 assert_se(v = hashmap_get(bus->vtable_properties, &key));
2215 assert(c == v->parent);
2216
2217 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2218 continue;
2219
2220 r = sd_bus_message_append(m, "s", *property);
2221 if (r < 0)
2222 return r;
2223 }
2224 } else {
2225 const sd_bus_vtable *v;
2226
2227 v = c->vtable;
2228 for (v = bus_vtable_next(c->vtable, v); v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(c->vtable, v)) {
2229 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2230 continue;
2231
2232 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2233 continue;
2234
2235 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2236 continue;
2237
2238 r = sd_bus_message_append(m, "s", v->x.property.member);
2239 if (r < 0)
2240 return r;
2241 }
2242 }
2243 }
2244 }
2245
2246 r = sd_bus_message_close_container(m);
2247 if (r < 0)
2248 return r;
2249
2250 r = sd_bus_send(bus, m, NULL);
2251 if (r < 0)
2252 return r;
2253
2254 return 1;
2255 }
2256
2257 _public_ int sd_bus_emit_properties_changed_strv(
2258 sd_bus *bus,
2259 const char *path,
2260 const char *interface,
2261 char **names) {
2262
2263 _cleanup_free_ char *prefix = NULL;
2264 bool found_interface = false;
2265 size_t pl;
2266 int r;
2267
2268 assert_return(bus, -EINVAL);
2269 assert_return(bus = bus_resolve(bus), -ENOPKG);
2270 assert_return(object_path_is_valid(path), -EINVAL);
2271 assert_return(interface_name_is_valid(interface), -EINVAL);
2272 assert_return(!bus_pid_changed(bus), -ECHILD);
2273
2274 if (!BUS_IS_OPEN(bus->state))
2275 return -ENOTCONN;
2276
2277 /* A non-NULL but empty names list means nothing needs to be
2278 generated. A NULL list OTOH indicates that all properties
2279 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2280 included in the PropertiesChanged message. */
2281 if (names && names[0] == NULL)
2282 return 0;
2283
2284 BUS_DONT_DESTROY(bus);
2285
2286 pl = strlen(path);
2287 assert(pl <= BUS_PATH_SIZE_MAX);
2288 prefix = new(char, pl + 1);
2289 if (!prefix)
2290 return -ENOMEM;
2291
2292 do {
2293 bus->nodes_modified = false;
2294
2295 r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
2296 if (r != 0)
2297 return r;
2298 if (bus->nodes_modified)
2299 continue;
2300
2301 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2302 r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
2303 if (r != 0)
2304 return r;
2305 if (bus->nodes_modified)
2306 break;
2307 }
2308
2309 } while (bus->nodes_modified);
2310
2311 return found_interface ? 0 : -ENOENT;
2312 }
2313
2314 _public_ int sd_bus_emit_properties_changed(
2315 sd_bus *bus,
2316 const char *path,
2317 const char *interface,
2318 const char *name, ...) {
2319
2320 char **names;
2321
2322 assert_return(bus, -EINVAL);
2323 assert_return(bus = bus_resolve(bus), -ENOPKG);
2324 assert_return(object_path_is_valid(path), -EINVAL);
2325 assert_return(interface_name_is_valid(interface), -EINVAL);
2326 assert_return(!bus_pid_changed(bus), -ECHILD);
2327
2328 if (!BUS_IS_OPEN(bus->state))
2329 return -ENOTCONN;
2330
2331 if (!name)
2332 return 0;
2333
2334 names = strv_from_stdarg_alloca(name);
2335
2336 return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
2337 }
2338
2339 static int object_added_append_all_prefix(
2340 sd_bus *bus,
2341 sd_bus_message *m,
2342 OrderedSet *s,
2343 const char *prefix,
2344 const char *path,
2345 bool require_fallback) {
2346
2347 const char *previous_interface = NULL;
2348 struct node *n;
2349 int r;
2350
2351 assert(bus);
2352 assert(m);
2353 assert(s);
2354 assert(prefix);
2355 assert(path);
2356
2357 n = hashmap_get(bus->nodes, prefix);
2358 if (!n)
2359 return 0;
2360
2361 LIST_FOREACH(vtables, c, n->vtables) {
2362 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2363 void *u = NULL;
2364
2365 if (require_fallback && !c->is_fallback)
2366 continue;
2367
2368 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2369 if (r < 0)
2370 return r;
2371 if (bus->nodes_modified)
2372 return 0;
2373 if (r == 0)
2374 continue;
2375
2376 if (!streq_ptr(c->interface, previous_interface)) {
2377 /* If a child-node already handled this interface, we
2378 * skip it on any of its parents. The child vtables
2379 * always fully override any conflicting vtables of
2380 * any parent node. */
2381 if (ordered_set_get(s, c->interface))
2382 continue;
2383
2384 r = ordered_set_put(s, c->interface);
2385 if (r < 0)
2386 return r;
2387
2388 if (previous_interface) {
2389 r = sd_bus_message_close_container(m);
2390 if (r < 0)
2391 return r;
2392 r = sd_bus_message_close_container(m);
2393 if (r < 0)
2394 return r;
2395 }
2396
2397 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2398 if (r < 0)
2399 return r;
2400 r = sd_bus_message_append(m, "s", c->interface);
2401 if (r < 0)
2402 return r;
2403 r = sd_bus_message_open_container(m, 'a', "{sv}");
2404 if (r < 0)
2405 return r;
2406
2407 previous_interface = c->interface;
2408 }
2409
2410 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2411 if (r < 0)
2412 return r;
2413 if (bus->nodes_modified)
2414 return 0;
2415 }
2416
2417 if (previous_interface) {
2418 r = sd_bus_message_close_container(m);
2419 if (r < 0)
2420 return r;
2421 r = sd_bus_message_close_container(m);
2422 if (r < 0)
2423 return r;
2424 }
2425
2426 return 0;
2427 }
2428
2429 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2430 _cleanup_ordered_set_free_ OrderedSet *s = NULL;
2431 _cleanup_free_ char *prefix = NULL;
2432 size_t pl;
2433 int r;
2434
2435 assert(bus);
2436 assert(m);
2437 assert(path);
2438
2439 /*
2440 * This appends all interfaces registered on path @path. We first add
2441 * the builtin interfaces, which are always available and handled by
2442 * sd-bus. Then, we add all interfaces registered on the exact node,
2443 * followed by all fallback interfaces registered on any parent prefix.
2444 *
2445 * If an interface is registered multiple times on the same node with
2446 * different vtables, we merge all the properties across all vtables.
2447 * However, if a child node has the same interface registered as one of
2448 * its parent nodes has as fallback, we make the child overwrite the
2449 * parent instead of extending it. Therefore, we keep a "Set" of all
2450 * handled interfaces during parent traversal, so we skip interfaces on
2451 * a parent that were overwritten by a child.
2452 */
2453
2454 s = ordered_set_new(&string_hash_ops);
2455 if (!s)
2456 return -ENOMEM;
2457
2458 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2459 if (r < 0)
2460 return r;
2461 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2462 if (r < 0)
2463 return r;
2464 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2465 if (r < 0)
2466 return r;
2467 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2468 if (r < 0)
2469 return r;
2470
2471 r = object_added_append_all_prefix(bus, m, s, path, path, false);
2472 if (r < 0)
2473 return r;
2474 if (bus->nodes_modified)
2475 return 0;
2476
2477 pl = strlen(path);
2478 assert(pl <= BUS_PATH_SIZE_MAX);
2479 prefix = new(char, pl + 1);
2480 if (!prefix)
2481 return -ENOMEM;
2482
2483 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2484 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
2485 if (r < 0)
2486 return r;
2487 if (bus->nodes_modified)
2488 return 0;
2489 }
2490
2491 return 0;
2492 }
2493
2494 _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
2495 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2496 struct node *object_manager;
2497 int r;
2498
2499 /*
2500 * This emits an InterfacesAdded signal on the given path, by iterating
2501 * all registered vtables and fallback vtables on the path. All
2502 * properties are queried and included in the signal.
2503 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2504 * explicit list of registered interfaces. However, unlike
2505 * interfaces_added(), this call can figure out the list of supported
2506 * interfaces itself. Furthermore, it properly adds the builtin
2507 * org.freedesktop.DBus.* interfaces.
2508 */
2509
2510 assert_return(bus, -EINVAL);
2511 assert_return(bus = bus_resolve(bus), -ENOPKG);
2512 assert_return(object_path_is_valid(path), -EINVAL);
2513 assert_return(!bus_pid_changed(bus), -ECHILD);
2514
2515 if (!BUS_IS_OPEN(bus->state))
2516 return -ENOTCONN;
2517
2518 r = bus_find_parent_object_manager(bus, &object_manager, path);
2519 if (r < 0)
2520 return r;
2521 if (r == 0)
2522 return -ESRCH;
2523
2524 BUS_DONT_DESTROY(bus);
2525
2526 do {
2527 bus->nodes_modified = false;
2528 m = sd_bus_message_unref(m);
2529
2530 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2531 if (r < 0)
2532 return r;
2533
2534 r = sd_bus_message_append_basic(m, 'o', path);
2535 if (r < 0)
2536 return r;
2537
2538 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2539 if (r < 0)
2540 return r;
2541
2542 r = object_added_append_all(bus, m, path);
2543 if (r < 0)
2544 return r;
2545
2546 if (bus->nodes_modified)
2547 continue;
2548
2549 r = sd_bus_message_close_container(m);
2550 if (r < 0)
2551 return r;
2552
2553 } while (bus->nodes_modified);
2554
2555 return sd_bus_send(bus, m, NULL);
2556 }
2557
2558 static int object_removed_append_all_prefix(
2559 sd_bus *bus,
2560 sd_bus_message *m,
2561 OrderedSet *s,
2562 const char *prefix,
2563 const char *path,
2564 bool require_fallback) {
2565
2566 const char *previous_interface = NULL;
2567 struct node *n;
2568 int r;
2569
2570 assert(bus);
2571 assert(m);
2572 assert(s);
2573 assert(prefix);
2574 assert(path);
2575
2576 n = hashmap_get(bus->nodes, prefix);
2577 if (!n)
2578 return 0;
2579
2580 LIST_FOREACH(vtables, c, n->vtables) {
2581 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2582 void *u = NULL;
2583
2584 if (require_fallback && !c->is_fallback)
2585 continue;
2586 if (streq_ptr(c->interface, previous_interface))
2587 continue;
2588
2589 /* If a child-node already handled this interface, we
2590 * skip it on any of its parents. The child vtables
2591 * always fully override any conflicting vtables of
2592 * any parent node. */
2593 if (ordered_set_get(s, c->interface))
2594 continue;
2595
2596 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2597 if (r < 0)
2598 return r;
2599 if (bus->nodes_modified)
2600 return 0;
2601 if (r == 0)
2602 continue;
2603
2604 r = ordered_set_put(s, c->interface);
2605 if (r < 0)
2606 return r;
2607
2608 r = sd_bus_message_append(m, "s", c->interface);
2609 if (r < 0)
2610 return r;
2611
2612 previous_interface = c->interface;
2613 }
2614
2615 return 0;
2616 }
2617
2618 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2619 _cleanup_ordered_set_free_ OrderedSet *s = NULL;
2620 _cleanup_free_ char *prefix = NULL;
2621 size_t pl;
2622 int r;
2623
2624 assert(bus);
2625 assert(m);
2626 assert(path);
2627
2628 /* see sd_bus_emit_object_added() for details */
2629
2630 s = ordered_set_new(&string_hash_ops);
2631 if (!s)
2632 return -ENOMEM;
2633
2634 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Peer");
2635 if (r < 0)
2636 return r;
2637 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Introspectable");
2638 if (r < 0)
2639 return r;
2640 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Properties");
2641 if (r < 0)
2642 return r;
2643 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.ObjectManager");
2644 if (r < 0)
2645 return r;
2646
2647 r = object_removed_append_all_prefix(bus, m, s, path, path, false);
2648 if (r < 0)
2649 return r;
2650 if (bus->nodes_modified)
2651 return 0;
2652
2653 pl = strlen(path);
2654 assert(pl <= BUS_PATH_SIZE_MAX);
2655 prefix = new(char, pl + 1);
2656 if (!prefix)
2657 return -ENOMEM;
2658
2659 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2660 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
2661 if (r < 0)
2662 return r;
2663 if (bus->nodes_modified)
2664 return 0;
2665 }
2666
2667 return 0;
2668 }
2669
2670 _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
2671 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2672 struct node *object_manager;
2673 int r;
2674
2675 /*
2676 * This is like sd_bus_emit_object_added(), but emits an
2677 * InterfacesRemoved signal on the given path. This only includes any
2678 * registered interfaces but skips the properties. Note that this will
2679 * call into the find() callbacks of any registered vtable. Therefore,
2680 * you must call this function before destroying/unlinking your object.
2681 * Otherwise, the list of interfaces will be incomplete. However, note
2682 * that this will *NOT* call into any property callback. Therefore, the
2683 * object might be in an "destructed" state, as long as we can find it.
2684 */
2685
2686 assert_return(bus, -EINVAL);
2687 assert_return(bus = bus_resolve(bus), -ENOPKG);
2688 assert_return(object_path_is_valid(path), -EINVAL);
2689 assert_return(!bus_pid_changed(bus), -ECHILD);
2690
2691 if (!BUS_IS_OPEN(bus->state))
2692 return -ENOTCONN;
2693
2694 r = bus_find_parent_object_manager(bus, &object_manager, path);
2695 if (r < 0)
2696 return r;
2697 if (r == 0)
2698 return -ESRCH;
2699
2700 BUS_DONT_DESTROY(bus);
2701
2702 do {
2703 bus->nodes_modified = false;
2704 m = sd_bus_message_unref(m);
2705
2706 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2707 if (r < 0)
2708 return r;
2709
2710 r = sd_bus_message_append_basic(m, 'o', path);
2711 if (r < 0)
2712 return r;
2713
2714 r = sd_bus_message_open_container(m, 'a', "s");
2715 if (r < 0)
2716 return r;
2717
2718 r = object_removed_append_all(bus, m, path);
2719 if (r < 0)
2720 return r;
2721
2722 if (bus->nodes_modified)
2723 continue;
2724
2725 r = sd_bus_message_close_container(m);
2726 if (r < 0)
2727 return r;
2728
2729 } while (bus->nodes_modified);
2730
2731 return sd_bus_send(bus, m, NULL);
2732 }
2733
2734 static int interfaces_added_append_one_prefix(
2735 sd_bus *bus,
2736 sd_bus_message *m,
2737 const char *prefix,
2738 const char *path,
2739 const char *interface,
2740 bool require_fallback) {
2741
2742 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2743 bool found_interface = false;
2744 struct node *n;
2745 void *u = NULL;
2746 int r;
2747
2748 assert(bus);
2749 assert(m);
2750 assert(prefix);
2751 assert(path);
2752 assert(interface);
2753
2754 n = hashmap_get(bus->nodes, prefix);
2755 if (!n)
2756 return 0;
2757
2758 LIST_FOREACH(vtables, c, n->vtables) {
2759 if (require_fallback && !c->is_fallback)
2760 continue;
2761
2762 if (!streq(c->interface, interface))
2763 continue;
2764
2765 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2766 if (r < 0)
2767 return r;
2768 if (bus->nodes_modified)
2769 return 0;
2770 if (r == 0)
2771 continue;
2772
2773 if (!found_interface) {
2774 r = sd_bus_message_append_basic(m, 's', interface);
2775 if (r < 0)
2776 return r;
2777
2778 r = sd_bus_message_open_container(m, 'a', "{sv}");
2779 if (r < 0)
2780 return r;
2781
2782 found_interface = true;
2783 }
2784
2785 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2786 if (r < 0)
2787 return r;
2788 if (bus->nodes_modified)
2789 return 0;
2790 }
2791
2792 if (found_interface) {
2793 r = sd_bus_message_close_container(m);
2794 if (r < 0)
2795 return r;
2796 }
2797
2798 return found_interface;
2799 }
2800
2801 static int interfaces_added_append_one(
2802 sd_bus *bus,
2803 sd_bus_message *m,
2804 const char *path,
2805 const char *interface) {
2806
2807 _cleanup_free_ char *prefix = NULL;
2808 size_t pl;
2809 int r;
2810
2811 assert(bus);
2812 assert(m);
2813 assert(path);
2814 assert(interface);
2815
2816 r = interfaces_added_append_one_prefix(bus, m, path, path, interface, false);
2817 if (r != 0)
2818 return r;
2819 if (bus->nodes_modified)
2820 return 0;
2821
2822 pl = strlen(path);
2823 assert(pl <= BUS_PATH_SIZE_MAX);
2824 prefix = new(char, pl + 1);
2825 if (!prefix)
2826 return -ENOMEM;
2827
2828 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2829 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
2830 if (r != 0)
2831 return r;
2832 if (bus->nodes_modified)
2833 return 0;
2834 }
2835
2836 return -ENOENT;
2837 }
2838
2839 _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) {
2840 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2841 struct node *object_manager;
2842 int r;
2843
2844 assert_return(bus, -EINVAL);
2845 assert_return(bus = bus_resolve(bus), -ENOPKG);
2846 assert_return(object_path_is_valid(path), -EINVAL);
2847 assert_return(!bus_pid_changed(bus), -ECHILD);
2848
2849 if (!BUS_IS_OPEN(bus->state))
2850 return -ENOTCONN;
2851
2852 if (strv_isempty(interfaces))
2853 return 0;
2854
2855 r = bus_find_parent_object_manager(bus, &object_manager, path);
2856 if (r < 0)
2857 return r;
2858 if (r == 0)
2859 return -ESRCH;
2860
2861 BUS_DONT_DESTROY(bus);
2862
2863 do {
2864 bus->nodes_modified = false;
2865 m = sd_bus_message_unref(m);
2866
2867 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2868 if (r < 0)
2869 return r;
2870
2871 r = sd_bus_message_append_basic(m, 'o', path);
2872 if (r < 0)
2873 return r;
2874
2875 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2876 if (r < 0)
2877 return r;
2878
2879 STRV_FOREACH(i, interfaces) {
2880 assert_return(interface_name_is_valid(*i), -EINVAL);
2881
2882 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2883 if (r < 0)
2884 return r;
2885
2886 r = interfaces_added_append_one(bus, m, path, *i);
2887 if (r < 0)
2888 return r;
2889
2890 if (bus->nodes_modified)
2891 break;
2892
2893 r = sd_bus_message_close_container(m);
2894 if (r < 0)
2895 return r;
2896 }
2897
2898 if (bus->nodes_modified)
2899 continue;
2900
2901 r = sd_bus_message_close_container(m);
2902 if (r < 0)
2903 return r;
2904
2905 } while (bus->nodes_modified);
2906
2907 return sd_bus_send(bus, m, NULL);
2908 }
2909
2910 _public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) {
2911 char **interfaces;
2912
2913 assert_return(bus, -EINVAL);
2914 assert_return(bus = bus_resolve(bus), -ENOPKG);
2915 assert_return(object_path_is_valid(path), -EINVAL);
2916 assert_return(!bus_pid_changed(bus), -ECHILD);
2917
2918 if (!BUS_IS_OPEN(bus->state))
2919 return -ENOTCONN;
2920
2921 interfaces = strv_from_stdarg_alloca(interface);
2922
2923 return sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
2924 }
2925
2926 _public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
2927 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2928 struct node *object_manager;
2929 int r;
2930
2931 assert_return(bus, -EINVAL);
2932 assert_return(bus = bus_resolve(bus), -ENOPKG);
2933 assert_return(object_path_is_valid(path), -EINVAL);
2934 assert_return(!bus_pid_changed(bus), -ECHILD);
2935
2936 if (!BUS_IS_OPEN(bus->state))
2937 return -ENOTCONN;
2938
2939 if (strv_isempty(interfaces))
2940 return 0;
2941
2942 r = bus_find_parent_object_manager(bus, &object_manager, path);
2943 if (r < 0)
2944 return r;
2945 if (r == 0)
2946 return -ESRCH;
2947
2948 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2949 if (r < 0)
2950 return r;
2951
2952 r = sd_bus_message_append_basic(m, 'o', path);
2953 if (r < 0)
2954 return r;
2955
2956 r = sd_bus_message_append_strv(m, interfaces);
2957 if (r < 0)
2958 return r;
2959
2960 return sd_bus_send(bus, m, NULL);
2961 }
2962
2963 _public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) {
2964 char **interfaces;
2965
2966 assert_return(bus, -EINVAL);
2967 assert_return(bus = bus_resolve(bus), -ENOPKG);
2968 assert_return(object_path_is_valid(path), -EINVAL);
2969 assert_return(!bus_pid_changed(bus), -ECHILD);
2970
2971 if (!BUS_IS_OPEN(bus->state))
2972 return -ENOTCONN;
2973
2974 interfaces = strv_from_stdarg_alloca(interface);
2975
2976 return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
2977 }
2978
2979 _public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
2980 sd_bus_slot *s;
2981 struct node *n;
2982 int r;
2983
2984 assert_return(bus, -EINVAL);
2985 assert_return(bus = bus_resolve(bus), -ENOPKG);
2986 assert_return(object_path_is_valid(path), -EINVAL);
2987 assert_return(!bus_pid_changed(bus), -ECHILD);
2988
2989 n = bus_node_allocate(bus, path);
2990 if (!n)
2991 return -ENOMEM;
2992
2993 s = bus_slot_allocate(bus, !slot, BUS_NODE_OBJECT_MANAGER, sizeof(struct node_object_manager), NULL);
2994 if (!s) {
2995 r = -ENOMEM;
2996 goto fail;
2997 }
2998
2999 s->node_object_manager.node = n;
3000 LIST_PREPEND(object_managers, n->object_managers, &s->node_object_manager);
3001 bus->nodes_modified = true;
3002
3003 if (slot)
3004 *slot = s;
3005
3006 return 0;
3007
3008 fail:
3009 sd_bus_slot_unref(s);
3010 bus_node_gc(bus, n);
3011
3012 return r;
3013 }