]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-objects.c
Merge pull request #11578 from keszybz/gcc-9-fixes
[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;
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' || !streq(strempty(signature), strempty(c->vtable->x.property.signature)))
642 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Incorrect parameters for property '%s', expected '%s', got '%s'.", c->member, strempty(c->vtable->x.property.signature), strempty(signature));
643
644 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
645 if (r < 0)
646 return r;
647
648 r = check_access(bus, m, c, &error);
649 if (r < 0)
650 return bus_maybe_reply_error(m, r, &error);
651
652 r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
653 if (r < 0)
654 return bus_maybe_reply_error(m, r, &error);
655
656 if (bus->nodes_modified)
657 return 0;
658
659 r = sd_bus_message_exit_container(m);
660 if (r < 0)
661 return r;
662 }
663
664 r = sd_bus_send(bus, reply, NULL);
665 if (r < 0)
666 return r;
667
668 return 1;
669 }
670
671 static int vtable_append_one_property(
672 sd_bus *bus,
673 sd_bus_message *reply,
674 const char *path,
675 struct node_vtable *c,
676 const sd_bus_vtable *v,
677 void *userdata,
678 sd_bus_error *error) {
679
680 sd_bus_slot *slot;
681 int r;
682
683 assert(bus);
684 assert(reply);
685 assert(path);
686 assert(c);
687 assert(v);
688
689 r = sd_bus_message_open_container(reply, 'e', "sv");
690 if (r < 0)
691 return r;
692
693 r = sd_bus_message_append(reply, "s", v->x.property.member);
694 if (r < 0)
695 return r;
696
697 r = sd_bus_message_open_container(reply, 'v', v->x.property.signature);
698 if (r < 0)
699 return r;
700
701 slot = container_of(c, sd_bus_slot, node_vtable);
702
703 r = invoke_property_get(bus, slot, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), error);
704 if (r < 0)
705 return r;
706 if (bus->nodes_modified)
707 return 0;
708
709 r = sd_bus_message_close_container(reply);
710 if (r < 0)
711 return r;
712
713 r = sd_bus_message_close_container(reply);
714 if (r < 0)
715 return r;
716
717 return 0;
718 }
719
720 static int vtable_append_all_properties(
721 sd_bus *bus,
722 sd_bus_message *reply,
723 const char *path,
724 struct node_vtable *c,
725 void *userdata,
726 sd_bus_error *error) {
727
728 const sd_bus_vtable *v;
729 int r;
730
731 assert(bus);
732 assert(reply);
733 assert(path);
734 assert(c);
735
736 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
737 return 1;
738
739 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
740 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
741 continue;
742
743 if (v->flags & SD_BUS_VTABLE_HIDDEN)
744 continue;
745
746 if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
747 continue;
748
749 r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
750 if (r < 0)
751 return r;
752 if (bus->nodes_modified)
753 return 0;
754 }
755
756 return 1;
757 }
758
759 static int property_get_all_callbacks_run(
760 sd_bus *bus,
761 sd_bus_message *m,
762 struct node_vtable *first,
763 bool require_fallback,
764 const char *iface,
765 bool *found_object) {
766
767 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
768 struct node_vtable *c;
769 bool found_interface;
770 int r;
771
772 assert(bus);
773 assert(m);
774 assert(found_object);
775
776 r = sd_bus_message_new_method_return(m, &reply);
777 if (r < 0)
778 return r;
779
780 r = sd_bus_message_open_container(reply, 'a', "{sv}");
781 if (r < 0)
782 return r;
783
784 found_interface = !iface ||
785 streq(iface, "org.freedesktop.DBus.Properties") ||
786 streq(iface, "org.freedesktop.DBus.Peer") ||
787 streq(iface, "org.freedesktop.DBus.Introspectable");
788
789 LIST_FOREACH(vtables, c, first) {
790 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
791 void *u;
792
793 if (require_fallback && !c->is_fallback)
794 continue;
795
796 r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
797 if (r < 0)
798 return bus_maybe_reply_error(m, r, &error);
799 if (bus->nodes_modified)
800 return 0;
801 if (r == 0)
802 continue;
803
804 *found_object = true;
805
806 if (iface && !streq(c->interface, iface))
807 continue;
808 found_interface = true;
809
810 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
811 if (r < 0)
812 return bus_maybe_reply_error(m, r, &error);
813 if (bus->nodes_modified)
814 return 0;
815 }
816
817 if (!*found_object)
818 return 0;
819
820 if (!found_interface) {
821 r = sd_bus_reply_method_errorf(
822 m,
823 SD_BUS_ERROR_UNKNOWN_INTERFACE,
824 "Unknown interface '%s'.", iface);
825 if (r < 0)
826 return r;
827
828 return 1;
829 }
830
831 r = sd_bus_message_close_container(reply);
832 if (r < 0)
833 return r;
834
835 r = sd_bus_send(bus, reply, NULL);
836 if (r < 0)
837 return r;
838
839 return 1;
840 }
841
842 static int bus_node_exists(
843 sd_bus *bus,
844 struct node *n,
845 const char *path,
846 bool require_fallback) {
847
848 struct node_vtable *c;
849 struct node_callback *k;
850 int r;
851
852 assert(bus);
853 assert(n);
854 assert(path);
855
856 /* Tests if there's anything attached directly to this node
857 * for the specified path */
858
859 if (!require_fallback && (n->enumerators || n->object_managers))
860 return true;
861
862 LIST_FOREACH(callbacks, k, n->callbacks) {
863 if (require_fallback && !k->is_fallback)
864 continue;
865
866 return 1;
867 }
868
869 LIST_FOREACH(vtables, c, n->vtables) {
870 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
871
872 if (require_fallback && !c->is_fallback)
873 continue;
874
875 r = node_vtable_get_userdata(bus, path, c, NULL, &error);
876 if (r != 0)
877 return r;
878 if (bus->nodes_modified)
879 return 0;
880 }
881
882 return 0;
883 }
884
885 static int process_introspect(
886 sd_bus *bus,
887 sd_bus_message *m,
888 struct node *n,
889 bool require_fallback,
890 bool *found_object) {
891
892 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
893 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
894 _cleanup_set_free_free_ Set *s = NULL;
895 const char *previous_interface = NULL;
896 struct introspect intro;
897 struct node_vtable *c;
898 bool empty;
899 int r;
900
901 assert(bus);
902 assert(m);
903 assert(n);
904 assert(found_object);
905
906 r = get_child_nodes(bus, m->path, n, 0, &s, &error);
907 if (r < 0)
908 return bus_maybe_reply_error(m, r, &error);
909 if (bus->nodes_modified)
910 return 0;
911
912 r = introspect_begin(&intro, bus->trusted);
913 if (r < 0)
914 return r;
915
916 r = introspect_write_default_interfaces(&intro, !require_fallback && n->object_managers);
917 if (r < 0)
918 return r;
919
920 empty = set_isempty(s);
921
922 LIST_FOREACH(vtables, c, n->vtables) {
923 if (require_fallback && !c->is_fallback)
924 continue;
925
926 r = node_vtable_get_userdata(bus, m->path, c, NULL, &error);
927 if (r < 0) {
928 r = bus_maybe_reply_error(m, r, &error);
929 goto finish;
930 }
931 if (bus->nodes_modified) {
932 r = 0;
933 goto finish;
934 }
935 if (r == 0)
936 continue;
937
938 empty = false;
939
940 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
941 continue;
942
943 if (!streq_ptr(previous_interface, c->interface)) {
944
945 if (previous_interface)
946 fputs(" </interface>\n", intro.f);
947
948 fprintf(intro.f, " <interface name=\"%s\">\n", c->interface);
949 }
950
951 r = introspect_write_interface(&intro, c->vtable);
952 if (r < 0)
953 goto finish;
954
955 previous_interface = c->interface;
956 }
957
958 if (previous_interface)
959 fputs(" </interface>\n", intro.f);
960
961 if (empty) {
962 /* Nothing?, let's see if we exist at all, and if not
963 * refuse to do anything */
964 r = bus_node_exists(bus, n, m->path, require_fallback);
965 if (r <= 0) {
966 r = bus_maybe_reply_error(m, r, &error);
967 goto finish;
968 }
969 if (bus->nodes_modified) {
970 r = 0;
971 goto finish;
972 }
973 }
974
975 *found_object = true;
976
977 r = introspect_write_child_nodes(&intro, s, m->path);
978 if (r < 0)
979 goto finish;
980
981 r = introspect_finish(&intro, bus, m, &reply);
982 if (r < 0)
983 goto finish;
984
985 r = sd_bus_send(bus, reply, NULL);
986 if (r < 0)
987 goto finish;
988
989 r = 1;
990
991 finish:
992 introspect_free(&intro);
993 return r;
994 }
995
996 static int object_manager_serialize_path(
997 sd_bus *bus,
998 sd_bus_message *reply,
999 const char *prefix,
1000 const char *path,
1001 bool require_fallback,
1002 sd_bus_error *error) {
1003
1004 const char *previous_interface = NULL;
1005 bool found_something = false;
1006 struct node_vtable *i;
1007 struct node *n;
1008 int r;
1009
1010 assert(bus);
1011 assert(reply);
1012 assert(prefix);
1013 assert(path);
1014 assert(error);
1015
1016 n = hashmap_get(bus->nodes, prefix);
1017 if (!n)
1018 return 0;
1019
1020 LIST_FOREACH(vtables, i, n->vtables) {
1021 void *u;
1022
1023 if (require_fallback && !i->is_fallback)
1024 continue;
1025
1026 r = node_vtable_get_userdata(bus, path, i, &u, error);
1027 if (r < 0)
1028 return r;
1029 if (bus->nodes_modified)
1030 return 0;
1031 if (r == 0)
1032 continue;
1033
1034 if (!found_something) {
1035
1036 /* Open the object part */
1037
1038 r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}");
1039 if (r < 0)
1040 return r;
1041
1042 r = sd_bus_message_append(reply, "o", path);
1043 if (r < 0)
1044 return r;
1045
1046 r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}");
1047 if (r < 0)
1048 return r;
1049
1050 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1051 if (r < 0)
1052 return r;
1053
1054 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1055 if (r < 0)
1056 return r;
1057
1058 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1059 if (r < 0)
1060 return r;
1061
1062 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1063 if (r < 0)
1064 return r;
1065
1066 found_something = true;
1067 }
1068
1069 if (!streq_ptr(previous_interface, i->interface)) {
1070
1071 /* Maybe close the previous interface part */
1072
1073 if (previous_interface) {
1074 r = sd_bus_message_close_container(reply);
1075 if (r < 0)
1076 return r;
1077
1078 r = sd_bus_message_close_container(reply);
1079 if (r < 0)
1080 return r;
1081 }
1082
1083 /* Open the new interface part */
1084
1085 r = sd_bus_message_open_container(reply, 'e', "sa{sv}");
1086 if (r < 0)
1087 return r;
1088
1089 r = sd_bus_message_append(reply, "s", i->interface);
1090 if (r < 0)
1091 return r;
1092
1093 r = sd_bus_message_open_container(reply, 'a', "{sv}");
1094 if (r < 0)
1095 return r;
1096 }
1097
1098 r = vtable_append_all_properties(bus, reply, path, i, u, error);
1099 if (r < 0)
1100 return r;
1101 if (bus->nodes_modified)
1102 return 0;
1103
1104 previous_interface = i->interface;
1105 }
1106
1107 if (previous_interface) {
1108 r = sd_bus_message_close_container(reply);
1109 if (r < 0)
1110 return r;
1111
1112 r = sd_bus_message_close_container(reply);
1113 if (r < 0)
1114 return r;
1115 }
1116
1117 if (found_something) {
1118 r = sd_bus_message_close_container(reply);
1119 if (r < 0)
1120 return r;
1121
1122 r = sd_bus_message_close_container(reply);
1123 if (r < 0)
1124 return r;
1125 }
1126
1127 return 1;
1128 }
1129
1130 static int object_manager_serialize_path_and_fallbacks(
1131 sd_bus *bus,
1132 sd_bus_message *reply,
1133 const char *path,
1134 sd_bus_error *error) {
1135
1136 char *prefix;
1137 int r;
1138
1139 assert(bus);
1140 assert(reply);
1141 assert(path);
1142 assert(error);
1143
1144 /* First, add all vtables registered for this path */
1145 r = object_manager_serialize_path(bus, reply, path, path, false, error);
1146 if (r < 0)
1147 return r;
1148 if (bus->nodes_modified)
1149 return 0;
1150
1151 /* Second, add fallback vtables registered for any of the prefixes */
1152 prefix = newa(char, strlen(path) + 1);
1153 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1154 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
1155 if (r < 0)
1156 return r;
1157 if (bus->nodes_modified)
1158 return 0;
1159 }
1160
1161 return 0;
1162 }
1163
1164 static int process_get_managed_objects(
1165 sd_bus *bus,
1166 sd_bus_message *m,
1167 struct node *n,
1168 bool require_fallback,
1169 bool *found_object) {
1170
1171 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1172 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1173 _cleanup_set_free_free_ Set *s = NULL;
1174 Iterator i;
1175 char *path;
1176 int r;
1177
1178 assert(bus);
1179 assert(m);
1180 assert(n);
1181 assert(found_object);
1182
1183 /* Spec says, GetManagedObjects() is only implemented on the root of a
1184 * sub-tree. Therefore, we require a registered object-manager on
1185 * exactly the queried path, otherwise, we refuse to respond. */
1186
1187 if (require_fallback || !n->object_managers)
1188 return 0;
1189
1190 r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
1191 if (r < 0)
1192 return bus_maybe_reply_error(m, r, &error);
1193 if (bus->nodes_modified)
1194 return 0;
1195
1196 r = sd_bus_message_new_method_return(m, &reply);
1197 if (r < 0)
1198 return r;
1199
1200 r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}");
1201 if (r < 0)
1202 return r;
1203
1204 SET_FOREACH(path, s, i) {
1205 r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
1206 if (r < 0)
1207 return bus_maybe_reply_error(m, r, &error);
1208
1209 if (bus->nodes_modified)
1210 return 0;
1211 }
1212
1213 r = sd_bus_message_close_container(reply);
1214 if (r < 0)
1215 return r;
1216
1217 r = sd_bus_send(bus, reply, NULL);
1218 if (r < 0)
1219 return r;
1220
1221 return 1;
1222 }
1223
1224 static int object_find_and_run(
1225 sd_bus *bus,
1226 sd_bus_message *m,
1227 const char *p,
1228 bool require_fallback,
1229 bool *found_object) {
1230
1231 struct node *n;
1232 struct vtable_member vtable_key, *v;
1233 int r;
1234
1235 assert(bus);
1236 assert(m);
1237 assert(p);
1238 assert(found_object);
1239
1240 n = hashmap_get(bus->nodes, p);
1241 if (!n)
1242 return 0;
1243
1244 /* First, try object callbacks */
1245 r = node_callbacks_run(bus, m, n->callbacks, require_fallback, found_object);
1246 if (r != 0)
1247 return r;
1248 if (bus->nodes_modified)
1249 return 0;
1250
1251 if (!m->interface || !m->member)
1252 return 0;
1253
1254 /* Then, look for a known method */
1255 vtable_key.path = (char*) p;
1256 vtable_key.interface = m->interface;
1257 vtable_key.member = m->member;
1258
1259 v = hashmap_get(bus->vtable_methods, &vtable_key);
1260 if (v) {
1261 r = method_callbacks_run(bus, m, v, require_fallback, found_object);
1262 if (r != 0)
1263 return r;
1264 if (bus->nodes_modified)
1265 return 0;
1266 }
1267
1268 /* Then, look for a known property */
1269 if (streq(m->interface, "org.freedesktop.DBus.Properties")) {
1270 bool get = false;
1271
1272 get = streq(m->member, "Get");
1273
1274 if (get || streq(m->member, "Set")) {
1275
1276 r = sd_bus_message_rewind(m, true);
1277 if (r < 0)
1278 return r;
1279
1280 vtable_key.path = (char*) p;
1281
1282 r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member);
1283 if (r < 0)
1284 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters");
1285
1286 v = hashmap_get(bus->vtable_properties, &vtable_key);
1287 if (v) {
1288 r = property_get_set_callbacks_run(bus, m, v, require_fallback, get, found_object);
1289 if (r != 0)
1290 return r;
1291 }
1292
1293 } else if (streq(m->member, "GetAll")) {
1294 const char *iface;
1295
1296 r = sd_bus_message_rewind(m, true);
1297 if (r < 0)
1298 return r;
1299
1300 r = sd_bus_message_read(m, "s", &iface);
1301 if (r < 0)
1302 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter");
1303
1304 if (iface[0] == 0)
1305 iface = NULL;
1306
1307 r = property_get_all_callbacks_run(bus, m, n->vtables, require_fallback, iface, found_object);
1308 if (r != 0)
1309 return r;
1310 }
1311
1312 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1313
1314 if (!isempty(sd_bus_message_get_signature(m, true)))
1315 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1316
1317 r = process_introspect(bus, m, n, require_fallback, found_object);
1318 if (r != 0)
1319 return r;
1320
1321 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1322
1323 if (!isempty(sd_bus_message_get_signature(m, true)))
1324 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1325
1326 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
1327 if (r != 0)
1328 return r;
1329 }
1330
1331 if (bus->nodes_modified)
1332 return 0;
1333
1334 if (!*found_object) {
1335 r = bus_node_exists(bus, n, m->path, require_fallback);
1336 if (r < 0)
1337 return bus_maybe_reply_error(m, r, NULL);
1338 if (bus->nodes_modified)
1339 return 0;
1340 if (r > 0)
1341 *found_object = true;
1342 }
1343
1344 return 0;
1345 }
1346
1347 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
1348 int r;
1349 size_t pl;
1350 bool found_object = false;
1351
1352 assert(bus);
1353 assert(m);
1354
1355 if (bus->is_monitor)
1356 return 0;
1357
1358 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1359 return 0;
1360
1361 if (hashmap_isempty(bus->nodes))
1362 return 0;
1363
1364 /* Never respond to broadcast messages */
1365 if (bus->bus_client && !m->destination)
1366 return 0;
1367
1368 assert(m->path);
1369 assert(m->member);
1370
1371 pl = strlen(m->path);
1372 do {
1373 char prefix[pl+1];
1374
1375 bus->nodes_modified = false;
1376
1377 r = object_find_and_run(bus, m, m->path, false, &found_object);
1378 if (r != 0)
1379 return r;
1380
1381 /* Look for fallback prefixes */
1382 OBJECT_PATH_FOREACH_PREFIX(prefix, m->path) {
1383
1384 if (bus->nodes_modified)
1385 break;
1386
1387 r = object_find_and_run(bus, m, prefix, true, &found_object);
1388 if (r != 0)
1389 return r;
1390 }
1391
1392 } while (bus->nodes_modified);
1393
1394 if (!found_object)
1395 return 0;
1396
1397 if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get") ||
1398 sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set"))
1399 r = sd_bus_reply_method_errorf(
1400 m,
1401 SD_BUS_ERROR_UNKNOWN_PROPERTY,
1402 "Unknown property or interface.");
1403 else
1404 r = sd_bus_reply_method_errorf(
1405 m,
1406 SD_BUS_ERROR_UNKNOWN_METHOD,
1407 "Unknown method '%s' or interface '%s'.", m->member, m->interface);
1408
1409 if (r < 0)
1410 return r;
1411
1412 return 1;
1413 }
1414
1415 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
1416 struct node *n, *parent;
1417 const char *e;
1418 _cleanup_free_ char *s = NULL;
1419 char *p;
1420 int r;
1421
1422 assert(bus);
1423 assert(path);
1424 assert(path[0] == '/');
1425
1426 n = hashmap_get(bus->nodes, path);
1427 if (n)
1428 return n;
1429
1430 r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops);
1431 if (r < 0)
1432 return NULL;
1433
1434 s = strdup(path);
1435 if (!s)
1436 return NULL;
1437
1438 if (streq(path, "/"))
1439 parent = NULL;
1440 else {
1441 e = strrchr(path, '/');
1442 assert(e);
1443
1444 p = strndupa(path, MAX(1, e - path));
1445
1446 parent = bus_node_allocate(bus, p);
1447 if (!parent)
1448 return NULL;
1449 }
1450
1451 n = new0(struct node, 1);
1452 if (!n)
1453 return NULL;
1454
1455 n->parent = parent;
1456 n->path = TAKE_PTR(s);
1457
1458 r = hashmap_put(bus->nodes, n->path, n);
1459 if (r < 0) {
1460 free(n->path);
1461 return mfree(n);
1462 }
1463
1464 if (parent)
1465 LIST_PREPEND(siblings, parent->child, n);
1466
1467 return n;
1468 }
1469
1470 void bus_node_gc(sd_bus *b, struct node *n) {
1471 assert(b);
1472
1473 if (!n)
1474 return;
1475
1476 if (n->child ||
1477 n->callbacks ||
1478 n->vtables ||
1479 n->enumerators ||
1480 n->object_managers)
1481 return;
1482
1483 assert_se(hashmap_remove(b->nodes, n->path) == n);
1484
1485 if (n->parent)
1486 LIST_REMOVE(siblings, n->parent->child, n);
1487
1488 free(n->path);
1489 bus_node_gc(b, n->parent);
1490 free(n);
1491 }
1492
1493 static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const char *path) {
1494 struct node *n;
1495
1496 assert(bus);
1497 assert(path);
1498
1499 n = hashmap_get(bus->nodes, path);
1500 if (!n) {
1501 char *prefix;
1502
1503 prefix = newa(char, strlen(path) + 1);
1504 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1505 n = hashmap_get(bus->nodes, prefix);
1506 if (n)
1507 break;
1508 }
1509 }
1510
1511 while (n && !n->object_managers)
1512 n = n->parent;
1513
1514 if (out)
1515 *out = n;
1516 return !!n;
1517 }
1518
1519 static int bus_add_object(
1520 sd_bus *bus,
1521 sd_bus_slot **slot,
1522 bool fallback,
1523 const char *path,
1524 sd_bus_message_handler_t callback,
1525 void *userdata) {
1526
1527 sd_bus_slot *s;
1528 struct node *n;
1529 int r;
1530
1531 assert_return(bus, -EINVAL);
1532 assert_return(bus = bus_resolve(bus), -ENOPKG);
1533 assert_return(object_path_is_valid(path), -EINVAL);
1534 assert_return(callback, -EINVAL);
1535 assert_return(!bus_pid_changed(bus), -ECHILD);
1536
1537 n = bus_node_allocate(bus, path);
1538 if (!n)
1539 return -ENOMEM;
1540
1541 s = bus_slot_allocate(bus, !slot, BUS_NODE_CALLBACK, sizeof(struct node_callback), userdata);
1542 if (!s) {
1543 r = -ENOMEM;
1544 goto fail;
1545 }
1546
1547 s->node_callback.callback = callback;
1548 s->node_callback.is_fallback = fallback;
1549
1550 s->node_callback.node = n;
1551 LIST_PREPEND(callbacks, n->callbacks, &s->node_callback);
1552 bus->nodes_modified = true;
1553
1554 if (slot)
1555 *slot = s;
1556
1557 return 0;
1558
1559 fail:
1560 sd_bus_slot_unref(s);
1561 bus_node_gc(bus, n);
1562
1563 return r;
1564 }
1565
1566 _public_ int sd_bus_add_object(
1567 sd_bus *bus,
1568 sd_bus_slot **slot,
1569 const char *path,
1570 sd_bus_message_handler_t callback,
1571 void *userdata) {
1572
1573 return bus_add_object(bus, slot, false, path, callback, userdata);
1574 }
1575
1576 _public_ int sd_bus_add_fallback(
1577 sd_bus *bus,
1578 sd_bus_slot **slot,
1579 const char *prefix,
1580 sd_bus_message_handler_t callback,
1581 void *userdata) {
1582
1583 return bus_add_object(bus, slot, true, prefix, callback, userdata);
1584 }
1585
1586 static void vtable_member_hash_func(const struct vtable_member *m, struct siphash *state) {
1587 assert(m);
1588
1589 string_hash_func(m->path, state);
1590 string_hash_func(m->interface, state);
1591 string_hash_func(m->member, state);
1592 }
1593
1594 static int vtable_member_compare_func(const struct vtable_member *x, const struct vtable_member *y) {
1595 int r;
1596
1597 assert(x);
1598 assert(y);
1599
1600 r = strcmp(x->path, y->path);
1601 if (r != 0)
1602 return r;
1603
1604 r = strcmp(x->interface, y->interface);
1605 if (r != 0)
1606 return r;
1607
1608 return strcmp(x->member, y->member);
1609 }
1610
1611 DEFINE_PRIVATE_HASH_OPS(vtable_member_hash_ops, struct vtable_member, vtable_member_hash_func, vtable_member_compare_func);
1612
1613 static int add_object_vtable_internal(
1614 sd_bus *bus,
1615 sd_bus_slot **slot,
1616 const char *path,
1617 const char *interface,
1618 const sd_bus_vtable *vtable,
1619 bool fallback,
1620 sd_bus_object_find_t find,
1621 void *userdata) {
1622
1623 sd_bus_slot *s = NULL;
1624 struct node_vtable *i, *existing = NULL;
1625 const sd_bus_vtable *v;
1626 struct node *n;
1627 int r;
1628
1629 assert_return(bus, -EINVAL);
1630 assert_return(bus = bus_resolve(bus), -ENOPKG);
1631 assert_return(object_path_is_valid(path), -EINVAL);
1632 assert_return(interface_name_is_valid(interface), -EINVAL);
1633 assert_return(vtable, -EINVAL);
1634 assert_return(vtable[0].type == _SD_BUS_VTABLE_START, -EINVAL);
1635 assert_return(vtable[0].x.start.element_size == sizeof(struct sd_bus_vtable), -EINVAL);
1636 assert_return(!bus_pid_changed(bus), -ECHILD);
1637 assert_return(!streq(interface, "org.freedesktop.DBus.Properties") &&
1638 !streq(interface, "org.freedesktop.DBus.Introspectable") &&
1639 !streq(interface, "org.freedesktop.DBus.Peer") &&
1640 !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL);
1641
1642 r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops);
1643 if (r < 0)
1644 return r;
1645
1646 r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops);
1647 if (r < 0)
1648 return r;
1649
1650 n = bus_node_allocate(bus, path);
1651 if (!n)
1652 return -ENOMEM;
1653
1654 LIST_FOREACH(vtables, i, n->vtables) {
1655 if (i->is_fallback != fallback) {
1656 r = -EPROTOTYPE;
1657 goto fail;
1658 }
1659
1660 if (streq(i->interface, interface)) {
1661
1662 if (i->vtable == vtable) {
1663 r = -EEXIST;
1664 goto fail;
1665 }
1666
1667 existing = i;
1668 }
1669 }
1670
1671 s = bus_slot_allocate(bus, !slot, BUS_NODE_VTABLE, sizeof(struct node_vtable), userdata);
1672 if (!s) {
1673 r = -ENOMEM;
1674 goto fail;
1675 }
1676
1677 s->node_vtable.is_fallback = fallback;
1678 s->node_vtable.vtable = vtable;
1679 s->node_vtable.find = find;
1680
1681 s->node_vtable.interface = strdup(interface);
1682 if (!s->node_vtable.interface) {
1683 r = -ENOMEM;
1684 goto fail;
1685 }
1686
1687 for (v = s->node_vtable.vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1688
1689 switch (v->type) {
1690
1691 case _SD_BUS_VTABLE_METHOD: {
1692 struct vtable_member *m;
1693
1694 if (!member_name_is_valid(v->x.method.member) ||
1695 !signature_is_valid(strempty(v->x.method.signature), false) ||
1696 !signature_is_valid(strempty(v->x.method.result), false) ||
1697 !(v->x.method.handler || (isempty(v->x.method.signature) && isempty(v->x.method.result))) ||
1698 v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) {
1699 r = -EINVAL;
1700 goto fail;
1701 }
1702
1703 m = new0(struct vtable_member, 1);
1704 if (!m) {
1705 r = -ENOMEM;
1706 goto fail;
1707 }
1708
1709 m->parent = &s->node_vtable;
1710 m->path = n->path;
1711 m->interface = s->node_vtable.interface;
1712 m->member = v->x.method.member;
1713 m->vtable = v;
1714
1715 r = hashmap_put(bus->vtable_methods, m, m);
1716 if (r < 0) {
1717 free(m);
1718 goto fail;
1719 }
1720
1721 break;
1722 }
1723
1724 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
1725
1726 if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
1727 r = -EINVAL;
1728 goto fail;
1729 }
1730
1731 if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
1732 r = -EINVAL;
1733 goto fail;
1734 }
1735
1736 _fallthrough_;
1737 case _SD_BUS_VTABLE_PROPERTY: {
1738 struct vtable_member *m;
1739
1740 if (!member_name_is_valid(v->x.property.member) ||
1741 !signature_is_single(v->x.property.signature, false) ||
1742 !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
1743 (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
1744 (!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
1745 ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
1746 (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
1747 r = -EINVAL;
1748 goto fail;
1749 }
1750
1751 m = new0(struct vtable_member, 1);
1752 if (!m) {
1753 r = -ENOMEM;
1754 goto fail;
1755 }
1756
1757 m->parent = &s->node_vtable;
1758 m->path = n->path;
1759 m->interface = s->node_vtable.interface;
1760 m->member = v->x.property.member;
1761 m->vtable = v;
1762
1763 r = hashmap_put(bus->vtable_properties, m, m);
1764 if (r < 0) {
1765 free(m);
1766 goto fail;
1767 }
1768
1769 break;
1770 }
1771
1772 case _SD_BUS_VTABLE_SIGNAL:
1773
1774 if (!member_name_is_valid(v->x.signal.member) ||
1775 !signature_is_valid(strempty(v->x.signal.signature), false) ||
1776 v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
1777 r = -EINVAL;
1778 goto fail;
1779 }
1780
1781 break;
1782
1783 default:
1784 r = -EINVAL;
1785 goto fail;
1786 }
1787 }
1788
1789 s->node_vtable.node = n;
1790 LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
1791 bus->nodes_modified = true;
1792
1793 if (slot)
1794 *slot = s;
1795
1796 return 0;
1797
1798 fail:
1799 sd_bus_slot_unref(s);
1800 bus_node_gc(bus, n);
1801
1802 return r;
1803 }
1804
1805 _public_ int sd_bus_add_object_vtable(
1806 sd_bus *bus,
1807 sd_bus_slot **slot,
1808 const char *path,
1809 const char *interface,
1810 const sd_bus_vtable *vtable,
1811 void *userdata) {
1812
1813 return add_object_vtable_internal(bus, slot, path, interface, vtable, false, NULL, userdata);
1814 }
1815
1816 _public_ int sd_bus_add_fallback_vtable(
1817 sd_bus *bus,
1818 sd_bus_slot **slot,
1819 const char *prefix,
1820 const char *interface,
1821 const sd_bus_vtable *vtable,
1822 sd_bus_object_find_t find,
1823 void *userdata) {
1824
1825 return add_object_vtable_internal(bus, slot, prefix, interface, vtable, true, find, userdata);
1826 }
1827
1828 _public_ int sd_bus_add_node_enumerator(
1829 sd_bus *bus,
1830 sd_bus_slot **slot,
1831 const char *path,
1832 sd_bus_node_enumerator_t callback,
1833 void *userdata) {
1834
1835 sd_bus_slot *s;
1836 struct node *n;
1837 int r;
1838
1839 assert_return(bus, -EINVAL);
1840 assert_return(bus = bus_resolve(bus), -ENOPKG);
1841 assert_return(object_path_is_valid(path), -EINVAL);
1842 assert_return(callback, -EINVAL);
1843 assert_return(!bus_pid_changed(bus), -ECHILD);
1844
1845 n = bus_node_allocate(bus, path);
1846 if (!n)
1847 return -ENOMEM;
1848
1849 s = bus_slot_allocate(bus, !slot, BUS_NODE_ENUMERATOR, sizeof(struct node_enumerator), userdata);
1850 if (!s) {
1851 r = -ENOMEM;
1852 goto fail;
1853 }
1854
1855 s->node_enumerator.callback = callback;
1856
1857 s->node_enumerator.node = n;
1858 LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
1859 bus->nodes_modified = true;
1860
1861 if (slot)
1862 *slot = s;
1863
1864 return 0;
1865
1866 fail:
1867 sd_bus_slot_unref(s);
1868 bus_node_gc(bus, n);
1869
1870 return r;
1871 }
1872
1873 static int emit_properties_changed_on_interface(
1874 sd_bus *bus,
1875 const char *prefix,
1876 const char *path,
1877 const char *interface,
1878 bool require_fallback,
1879 bool *found_interface,
1880 char **names) {
1881
1882 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1883 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1884 bool has_invalidating = false, has_changing = false;
1885 struct vtable_member key = {};
1886 struct node_vtable *c;
1887 struct node *n;
1888 char **property;
1889 void *u = NULL;
1890 int r;
1891
1892 assert(bus);
1893 assert(prefix);
1894 assert(path);
1895 assert(interface);
1896 assert(found_interface);
1897
1898 n = hashmap_get(bus->nodes, prefix);
1899 if (!n)
1900 return 0;
1901
1902 r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
1903 if (r < 0)
1904 return r;
1905
1906 r = sd_bus_message_append(m, "s", interface);
1907 if (r < 0)
1908 return r;
1909
1910 r = sd_bus_message_open_container(m, 'a', "{sv}");
1911 if (r < 0)
1912 return r;
1913
1914 key.path = prefix;
1915 key.interface = interface;
1916
1917 LIST_FOREACH(vtables, c, n->vtables) {
1918 if (require_fallback && !c->is_fallback)
1919 continue;
1920
1921 if (!streq(c->interface, interface))
1922 continue;
1923
1924 r = node_vtable_get_userdata(bus, path, c, &u, &error);
1925 if (r < 0)
1926 return r;
1927 if (bus->nodes_modified)
1928 return 0;
1929 if (r == 0)
1930 continue;
1931
1932 *found_interface = true;
1933
1934 if (names) {
1935 /* If the caller specified a list of
1936 * properties we include exactly those in the
1937 * PropertiesChanged message */
1938
1939 STRV_FOREACH(property, names) {
1940 struct vtable_member *v;
1941
1942 assert_return(member_name_is_valid(*property), -EINVAL);
1943
1944 key.member = *property;
1945 v = hashmap_get(bus->vtable_properties, &key);
1946 if (!v)
1947 return -ENOENT;
1948
1949 /* If there are two vtables for the same
1950 * interface, let's handle this property when
1951 * we come to that vtable. */
1952 if (c != v->parent)
1953 continue;
1954
1955 assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ||
1956 v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
1957
1958 assert_return(!(v->vtable->flags & SD_BUS_VTABLE_HIDDEN), -EDOM);
1959
1960 if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1961 has_invalidating = true;
1962 continue;
1963 }
1964
1965 has_changing = true;
1966
1967 r = vtable_append_one_property(bus, m, m->path, c, v->vtable, u, &error);
1968 if (r < 0)
1969 return r;
1970 if (bus->nodes_modified)
1971 return 0;
1972 }
1973 } else {
1974 const sd_bus_vtable *v;
1975
1976 /* If the caller specified no properties list
1977 * we include all properties that are marked
1978 * as changing in the message. */
1979
1980 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1981 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
1982 continue;
1983
1984 if (v->flags & SD_BUS_VTABLE_HIDDEN)
1985 continue;
1986
1987 if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1988 has_invalidating = true;
1989 continue;
1990 }
1991
1992 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
1993 continue;
1994
1995 has_changing = true;
1996
1997 r = vtable_append_one_property(bus, m, m->path, c, v, u, &error);
1998 if (r < 0)
1999 return r;
2000 if (bus->nodes_modified)
2001 return 0;
2002 }
2003 }
2004 }
2005
2006 if (!has_invalidating && !has_changing)
2007 return 0;
2008
2009 r = sd_bus_message_close_container(m);
2010 if (r < 0)
2011 return r;
2012
2013 r = sd_bus_message_open_container(m, 'a', "s");
2014 if (r < 0)
2015 return r;
2016
2017 if (has_invalidating) {
2018 LIST_FOREACH(vtables, c, n->vtables) {
2019 if (require_fallback && !c->is_fallback)
2020 continue;
2021
2022 if (!streq(c->interface, interface))
2023 continue;
2024
2025 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2026 if (r < 0)
2027 return r;
2028 if (bus->nodes_modified)
2029 return 0;
2030 if (r == 0)
2031 continue;
2032
2033 if (names) {
2034 STRV_FOREACH(property, names) {
2035 struct vtable_member *v;
2036
2037 key.member = *property;
2038 assert_se(v = hashmap_get(bus->vtable_properties, &key));
2039 assert(c == v->parent);
2040
2041 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2042 continue;
2043
2044 r = sd_bus_message_append(m, "s", *property);
2045 if (r < 0)
2046 return r;
2047 }
2048 } else {
2049 const sd_bus_vtable *v;
2050
2051 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
2052 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2053 continue;
2054
2055 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2056 continue;
2057
2058 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2059 continue;
2060
2061 r = sd_bus_message_append(m, "s", v->x.property.member);
2062 if (r < 0)
2063 return r;
2064 }
2065 }
2066 }
2067 }
2068
2069 r = sd_bus_message_close_container(m);
2070 if (r < 0)
2071 return r;
2072
2073 r = sd_bus_send(bus, m, NULL);
2074 if (r < 0)
2075 return r;
2076
2077 return 1;
2078 }
2079
2080 _public_ int sd_bus_emit_properties_changed_strv(
2081 sd_bus *bus,
2082 const char *path,
2083 const char *interface,
2084 char **names) {
2085
2086 bool found_interface = false;
2087 char *prefix;
2088 int r;
2089
2090 assert_return(bus, -EINVAL);
2091 assert_return(bus = bus_resolve(bus), -ENOPKG);
2092 assert_return(object_path_is_valid(path), -EINVAL);
2093 assert_return(interface_name_is_valid(interface), -EINVAL);
2094 assert_return(!bus_pid_changed(bus), -ECHILD);
2095
2096 if (!BUS_IS_OPEN(bus->state))
2097 return -ENOTCONN;
2098
2099 /* A non-NULL but empty names list means nothing needs to be
2100 generated. A NULL list OTOH indicates that all properties
2101 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2102 included in the PropertiesChanged message. */
2103 if (names && names[0] == NULL)
2104 return 0;
2105
2106 BUS_DONT_DESTROY(bus);
2107
2108 do {
2109 bus->nodes_modified = false;
2110
2111 r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
2112 if (r != 0)
2113 return r;
2114 if (bus->nodes_modified)
2115 continue;
2116
2117 prefix = newa(char, strlen(path) + 1);
2118 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2119 r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
2120 if (r != 0)
2121 return r;
2122 if (bus->nodes_modified)
2123 break;
2124 }
2125
2126 } while (bus->nodes_modified);
2127
2128 return found_interface ? 0 : -ENOENT;
2129 }
2130
2131 _public_ int sd_bus_emit_properties_changed(
2132 sd_bus *bus,
2133 const char *path,
2134 const char *interface,
2135 const char *name, ...) {
2136
2137 char **names;
2138
2139 assert_return(bus, -EINVAL);
2140 assert_return(bus = bus_resolve(bus), -ENOPKG);
2141 assert_return(object_path_is_valid(path), -EINVAL);
2142 assert_return(interface_name_is_valid(interface), -EINVAL);
2143 assert_return(!bus_pid_changed(bus), -ECHILD);
2144
2145 if (!BUS_IS_OPEN(bus->state))
2146 return -ENOTCONN;
2147
2148 if (!name)
2149 return 0;
2150
2151 names = strv_from_stdarg_alloca(name);
2152
2153 return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
2154 }
2155
2156 static int object_added_append_all_prefix(
2157 sd_bus *bus,
2158 sd_bus_message *m,
2159 Set *s,
2160 const char *prefix,
2161 const char *path,
2162 bool require_fallback) {
2163
2164 const char *previous_interface = NULL;
2165 struct node_vtable *c;
2166 struct node *n;
2167 int r;
2168
2169 assert(bus);
2170 assert(m);
2171 assert(s);
2172 assert(prefix);
2173 assert(path);
2174
2175 n = hashmap_get(bus->nodes, prefix);
2176 if (!n)
2177 return 0;
2178
2179 LIST_FOREACH(vtables, c, n->vtables) {
2180 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2181 void *u = NULL;
2182
2183 if (require_fallback && !c->is_fallback)
2184 continue;
2185
2186 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2187 if (r < 0)
2188 return r;
2189 if (bus->nodes_modified)
2190 return 0;
2191 if (r == 0)
2192 continue;
2193
2194 if (!streq_ptr(c->interface, previous_interface)) {
2195 /* If a child-node already handled this interface, we
2196 * skip it on any of its parents. The child vtables
2197 * always fully override any conflicting vtables of
2198 * any parent node. */
2199 if (set_get(s, c->interface))
2200 continue;
2201
2202 r = set_put(s, c->interface);
2203 if (r < 0)
2204 return r;
2205
2206 if (previous_interface) {
2207 r = sd_bus_message_close_container(m);
2208 if (r < 0)
2209 return r;
2210 r = sd_bus_message_close_container(m);
2211 if (r < 0)
2212 return r;
2213 }
2214
2215 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2216 if (r < 0)
2217 return r;
2218 r = sd_bus_message_append(m, "s", c->interface);
2219 if (r < 0)
2220 return r;
2221 r = sd_bus_message_open_container(m, 'a', "{sv}");
2222 if (r < 0)
2223 return r;
2224
2225 previous_interface = c->interface;
2226 }
2227
2228 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2229 if (r < 0)
2230 return r;
2231 if (bus->nodes_modified)
2232 return 0;
2233 }
2234
2235 if (previous_interface) {
2236 r = sd_bus_message_close_container(m);
2237 if (r < 0)
2238 return r;
2239 r = sd_bus_message_close_container(m);
2240 if (r < 0)
2241 return r;
2242 }
2243
2244 return 0;
2245 }
2246
2247 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2248 _cleanup_set_free_ Set *s = NULL;
2249 char *prefix;
2250 int r;
2251
2252 assert(bus);
2253 assert(m);
2254 assert(path);
2255
2256 /*
2257 * This appends all interfaces registered on path @path. We first add
2258 * the builtin interfaces, which are always available and handled by
2259 * sd-bus. Then, we add all interfaces registered on the exact node,
2260 * followed by all fallback interfaces registered on any parent prefix.
2261 *
2262 * If an interface is registered multiple times on the same node with
2263 * different vtables, we merge all the properties across all vtables.
2264 * However, if a child node has the same interface registered as one of
2265 * its parent nodes has as fallback, we make the child overwrite the
2266 * parent instead of extending it. Therefore, we keep a "Set" of all
2267 * handled interfaces during parent traversal, so we skip interfaces on
2268 * a parent that were overwritten by a child.
2269 */
2270
2271 s = set_new(&string_hash_ops);
2272 if (!s)
2273 return -ENOMEM;
2274
2275 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2276 if (r < 0)
2277 return r;
2278 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2279 if (r < 0)
2280 return r;
2281 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2282 if (r < 0)
2283 return r;
2284 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2285 if (r < 0)
2286 return r;
2287
2288 r = object_added_append_all_prefix(bus, m, s, path, path, false);
2289 if (r < 0)
2290 return r;
2291 if (bus->nodes_modified)
2292 return 0;
2293
2294 prefix = newa(char, strlen(path) + 1);
2295 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2296 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
2297 if (r < 0)
2298 return r;
2299 if (bus->nodes_modified)
2300 return 0;
2301 }
2302
2303 return 0;
2304 }
2305
2306 _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
2307 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2308 struct node *object_manager;
2309 int r;
2310
2311 /*
2312 * This emits an InterfacesAdded signal on the given path, by iterating
2313 * all registered vtables and fallback vtables on the path. All
2314 * properties are queried and included in the signal.
2315 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2316 * explicit list of registered interfaces. However, unlike
2317 * interfaces_added(), this call can figure out the list of supported
2318 * interfaces itself. Furthermore, it properly adds the builtin
2319 * org.freedesktop.DBus.* interfaces.
2320 */
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(!bus_pid_changed(bus), -ECHILD);
2326
2327 if (!BUS_IS_OPEN(bus->state))
2328 return -ENOTCONN;
2329
2330 r = bus_find_parent_object_manager(bus, &object_manager, path);
2331 if (r < 0)
2332 return r;
2333 if (r == 0)
2334 return -ESRCH;
2335
2336 BUS_DONT_DESTROY(bus);
2337
2338 do {
2339 bus->nodes_modified = false;
2340 m = sd_bus_message_unref(m);
2341
2342 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2343 if (r < 0)
2344 return r;
2345
2346 r = sd_bus_message_append_basic(m, 'o', path);
2347 if (r < 0)
2348 return r;
2349
2350 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2351 if (r < 0)
2352 return r;
2353
2354 r = object_added_append_all(bus, m, path);
2355 if (r < 0)
2356 return r;
2357
2358 if (bus->nodes_modified)
2359 continue;
2360
2361 r = sd_bus_message_close_container(m);
2362 if (r < 0)
2363 return r;
2364
2365 } while (bus->nodes_modified);
2366
2367 return sd_bus_send(bus, m, NULL);
2368 }
2369
2370 static int object_removed_append_all_prefix(
2371 sd_bus *bus,
2372 sd_bus_message *m,
2373 Set *s,
2374 const char *prefix,
2375 const char *path,
2376 bool require_fallback) {
2377
2378 const char *previous_interface = NULL;
2379 struct node_vtable *c;
2380 struct node *n;
2381 int r;
2382
2383 assert(bus);
2384 assert(m);
2385 assert(s);
2386 assert(prefix);
2387 assert(path);
2388
2389 n = hashmap_get(bus->nodes, prefix);
2390 if (!n)
2391 return 0;
2392
2393 LIST_FOREACH(vtables, c, n->vtables) {
2394 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2395 void *u = NULL;
2396
2397 if (require_fallback && !c->is_fallback)
2398 continue;
2399 if (streq_ptr(c->interface, previous_interface))
2400 continue;
2401
2402 /* If a child-node already handled this interface, we
2403 * skip it on any of its parents. The child vtables
2404 * always fully override any conflicting vtables of
2405 * any parent node. */
2406 if (set_get(s, c->interface))
2407 continue;
2408
2409 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2410 if (r < 0)
2411 return r;
2412 if (bus->nodes_modified)
2413 return 0;
2414 if (r == 0)
2415 continue;
2416
2417 r = set_put(s, c->interface);
2418 if (r < 0)
2419 return r;
2420
2421 r = sd_bus_message_append(m, "s", c->interface);
2422 if (r < 0)
2423 return r;
2424
2425 previous_interface = c->interface;
2426 }
2427
2428 return 0;
2429 }
2430
2431 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2432 _cleanup_set_free_ Set *s = NULL;
2433 char *prefix;
2434 int r;
2435
2436 assert(bus);
2437 assert(m);
2438 assert(path);
2439
2440 /* see sd_bus_emit_object_added() for details */
2441
2442 s = set_new(&string_hash_ops);
2443 if (!s)
2444 return -ENOMEM;
2445
2446 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Peer");
2447 if (r < 0)
2448 return r;
2449 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Introspectable");
2450 if (r < 0)
2451 return r;
2452 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Properties");
2453 if (r < 0)
2454 return r;
2455 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.ObjectManager");
2456 if (r < 0)
2457 return r;
2458
2459 r = object_removed_append_all_prefix(bus, m, s, path, path, false);
2460 if (r < 0)
2461 return r;
2462 if (bus->nodes_modified)
2463 return 0;
2464
2465 prefix = newa(char, strlen(path) + 1);
2466 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2467 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
2468 if (r < 0)
2469 return r;
2470 if (bus->nodes_modified)
2471 return 0;
2472 }
2473
2474 return 0;
2475 }
2476
2477 _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
2478 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2479 struct node *object_manager;
2480 int r;
2481
2482 /*
2483 * This is like sd_bus_emit_object_added(), but emits an
2484 * InterfacesRemoved signal on the given path. This only includes any
2485 * registered interfaces but skips the properties. Note that this will
2486 * call into the find() callbacks of any registered vtable. Therefore,
2487 * you must call this function before destroying/unlinking your object.
2488 * Otherwise, the list of interfaces will be incomplete. However, note
2489 * that this will *NOT* call into any property callback. Therefore, the
2490 * object might be in an "destructed" state, as long as we can find it.
2491 */
2492
2493 assert_return(bus, -EINVAL);
2494 assert_return(bus = bus_resolve(bus), -ENOPKG);
2495 assert_return(object_path_is_valid(path), -EINVAL);
2496 assert_return(!bus_pid_changed(bus), -ECHILD);
2497
2498 if (!BUS_IS_OPEN(bus->state))
2499 return -ENOTCONN;
2500
2501 r = bus_find_parent_object_manager(bus, &object_manager, path);
2502 if (r < 0)
2503 return r;
2504 if (r == 0)
2505 return -ESRCH;
2506
2507 BUS_DONT_DESTROY(bus);
2508
2509 do {
2510 bus->nodes_modified = false;
2511 m = sd_bus_message_unref(m);
2512
2513 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2514 if (r < 0)
2515 return r;
2516
2517 r = sd_bus_message_append_basic(m, 'o', path);
2518 if (r < 0)
2519 return r;
2520
2521 r = sd_bus_message_open_container(m, 'a', "s");
2522 if (r < 0)
2523 return r;
2524
2525 r = object_removed_append_all(bus, m, path);
2526 if (r < 0)
2527 return r;
2528
2529 if (bus->nodes_modified)
2530 continue;
2531
2532 r = sd_bus_message_close_container(m);
2533 if (r < 0)
2534 return r;
2535
2536 } while (bus->nodes_modified);
2537
2538 return sd_bus_send(bus, m, NULL);
2539 }
2540
2541 static int interfaces_added_append_one_prefix(
2542 sd_bus *bus,
2543 sd_bus_message *m,
2544 const char *prefix,
2545 const char *path,
2546 const char *interface,
2547 bool require_fallback) {
2548
2549 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2550 bool found_interface = false;
2551 struct node_vtable *c;
2552 struct node *n;
2553 void *u = NULL;
2554 int r;
2555
2556 assert(bus);
2557 assert(m);
2558 assert(prefix);
2559 assert(path);
2560 assert(interface);
2561
2562 n = hashmap_get(bus->nodes, prefix);
2563 if (!n)
2564 return 0;
2565
2566 LIST_FOREACH(vtables, c, n->vtables) {
2567 if (require_fallback && !c->is_fallback)
2568 continue;
2569
2570 if (!streq(c->interface, interface))
2571 continue;
2572
2573 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2574 if (r < 0)
2575 return r;
2576 if (bus->nodes_modified)
2577 return 0;
2578 if (r == 0)
2579 continue;
2580
2581 if (!found_interface) {
2582 r = sd_bus_message_append_basic(m, 's', interface);
2583 if (r < 0)
2584 return r;
2585
2586 r = sd_bus_message_open_container(m, 'a', "{sv}");
2587 if (r < 0)
2588 return r;
2589
2590 found_interface = true;
2591 }
2592
2593 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2594 if (r < 0)
2595 return r;
2596 if (bus->nodes_modified)
2597 return 0;
2598 }
2599
2600 if (found_interface) {
2601 r = sd_bus_message_close_container(m);
2602 if (r < 0)
2603 return r;
2604 }
2605
2606 return found_interface;
2607 }
2608
2609 static int interfaces_added_append_one(
2610 sd_bus *bus,
2611 sd_bus_message *m,
2612 const char *path,
2613 const char *interface) {
2614
2615 char *prefix;
2616 int r;
2617
2618 assert(bus);
2619 assert(m);
2620 assert(path);
2621 assert(interface);
2622
2623 r = interfaces_added_append_one_prefix(bus, m, path, path, interface, false);
2624 if (r != 0)
2625 return r;
2626 if (bus->nodes_modified)
2627 return 0;
2628
2629 prefix = newa(char, strlen(path) + 1);
2630 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2631 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
2632 if (r != 0)
2633 return r;
2634 if (bus->nodes_modified)
2635 return 0;
2636 }
2637
2638 return -ENOENT;
2639 }
2640
2641 _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) {
2642 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2643 struct node *object_manager;
2644 char **i;
2645 int r;
2646
2647 assert_return(bus, -EINVAL);
2648 assert_return(bus = bus_resolve(bus), -ENOPKG);
2649 assert_return(object_path_is_valid(path), -EINVAL);
2650 assert_return(!bus_pid_changed(bus), -ECHILD);
2651
2652 if (!BUS_IS_OPEN(bus->state))
2653 return -ENOTCONN;
2654
2655 if (strv_isempty(interfaces))
2656 return 0;
2657
2658 r = bus_find_parent_object_manager(bus, &object_manager, path);
2659 if (r < 0)
2660 return r;
2661 if (r == 0)
2662 return -ESRCH;
2663
2664 BUS_DONT_DESTROY(bus);
2665
2666 do {
2667 bus->nodes_modified = false;
2668 m = sd_bus_message_unref(m);
2669
2670 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2671 if (r < 0)
2672 return r;
2673
2674 r = sd_bus_message_append_basic(m, 'o', path);
2675 if (r < 0)
2676 return r;
2677
2678 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2679 if (r < 0)
2680 return r;
2681
2682 STRV_FOREACH(i, interfaces) {
2683 assert_return(interface_name_is_valid(*i), -EINVAL);
2684
2685 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2686 if (r < 0)
2687 return r;
2688
2689 r = interfaces_added_append_one(bus, m, path, *i);
2690 if (r < 0)
2691 return r;
2692
2693 if (bus->nodes_modified)
2694 break;
2695
2696 r = sd_bus_message_close_container(m);
2697 if (r < 0)
2698 return r;
2699 }
2700
2701 if (bus->nodes_modified)
2702 continue;
2703
2704 r = sd_bus_message_close_container(m);
2705 if (r < 0)
2706 return r;
2707
2708 } while (bus->nodes_modified);
2709
2710 return sd_bus_send(bus, m, NULL);
2711 }
2712
2713 _public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) {
2714 char **interfaces;
2715
2716 assert_return(bus, -EINVAL);
2717 assert_return(bus = bus_resolve(bus), -ENOPKG);
2718 assert_return(object_path_is_valid(path), -EINVAL);
2719 assert_return(!bus_pid_changed(bus), -ECHILD);
2720
2721 if (!BUS_IS_OPEN(bus->state))
2722 return -ENOTCONN;
2723
2724 interfaces = strv_from_stdarg_alloca(interface);
2725
2726 return sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
2727 }
2728
2729 _public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
2730 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2731 struct node *object_manager;
2732 int r;
2733
2734 assert_return(bus, -EINVAL);
2735 assert_return(bus = bus_resolve(bus), -ENOPKG);
2736 assert_return(object_path_is_valid(path), -EINVAL);
2737 assert_return(!bus_pid_changed(bus), -ECHILD);
2738
2739 if (!BUS_IS_OPEN(bus->state))
2740 return -ENOTCONN;
2741
2742 if (strv_isempty(interfaces))
2743 return 0;
2744
2745 r = bus_find_parent_object_manager(bus, &object_manager, path);
2746 if (r < 0)
2747 return r;
2748 if (r == 0)
2749 return -ESRCH;
2750
2751 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2752 if (r < 0)
2753 return r;
2754
2755 r = sd_bus_message_append_basic(m, 'o', path);
2756 if (r < 0)
2757 return r;
2758
2759 r = sd_bus_message_append_strv(m, interfaces);
2760 if (r < 0)
2761 return r;
2762
2763 return sd_bus_send(bus, m, NULL);
2764 }
2765
2766 _public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) {
2767 char **interfaces;
2768
2769 assert_return(bus, -EINVAL);
2770 assert_return(bus = bus_resolve(bus), -ENOPKG);
2771 assert_return(object_path_is_valid(path), -EINVAL);
2772 assert_return(!bus_pid_changed(bus), -ECHILD);
2773
2774 if (!BUS_IS_OPEN(bus->state))
2775 return -ENOTCONN;
2776
2777 interfaces = strv_from_stdarg_alloca(interface);
2778
2779 return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
2780 }
2781
2782 _public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
2783 sd_bus_slot *s;
2784 struct node *n;
2785 int r;
2786
2787 assert_return(bus, -EINVAL);
2788 assert_return(bus = bus_resolve(bus), -ENOPKG);
2789 assert_return(object_path_is_valid(path), -EINVAL);
2790 assert_return(!bus_pid_changed(bus), -ECHILD);
2791
2792 n = bus_node_allocate(bus, path);
2793 if (!n)
2794 return -ENOMEM;
2795
2796 s = bus_slot_allocate(bus, !slot, BUS_NODE_OBJECT_MANAGER, sizeof(struct node_object_manager), NULL);
2797 if (!s) {
2798 r = -ENOMEM;
2799 goto fail;
2800 }
2801
2802 s->node_object_manager.node = n;
2803 LIST_PREPEND(object_managers, n->object_managers, &s->node_object_manager);
2804 bus->nodes_modified = true;
2805
2806 if (slot)
2807 *slot = s;
2808
2809 return 0;
2810
2811 fail:
2812 sd_bus_slot_unref(s);
2813 bus_node_gc(bus, n);
2814
2815 return r;
2816 }