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