]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-bus/bus-objects.c
Refuse dbus message paths longer than BUS_PATH_SIZE_MAX limit.
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-objects.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
992c052c 2
b5efdb8a 3#include "alloc-util.h"
992c052c 4#include "bus-internal.h"
07630cea 5#include "bus-introspect.h"
992c052c 6#include "bus-message.h"
cf0fbc49 7#include "bus-objects.h"
992c052c 8#include "bus-signature.h"
19befb2d 9#include "bus-slot.h"
07630cea
LP
10#include "bus-type.h"
11#include "bus-util.h"
ef118d00 12#include "missing_capability.h"
07630cea
LP
13#include "set.h"
14#include "string-util.h"
15#include "strv.h"
992c052c
LP
16
17static int node_vtable_get_userdata(
18 sd_bus *bus,
19 const char *path,
20 struct node_vtable *c,
f00c3121
LP
21 void **userdata,
22 sd_bus_error *error) {
992c052c 23
19befb2d 24 sd_bus_slot *s;
00590f82 25 void *u, *found_u;
992c052c
LP
26 int r;
27
28 assert(bus);
29 assert(path);
30 assert(c);
31
19befb2d
LP
32 s = container_of(c, sd_bus_slot, node_vtable);
33 u = s->userdata;
992c052c 34 if (c->find) {
1b64f838 35 bus->current_slot = sd_bus_slot_ref(s);
caa82984 36 bus->current_userdata = u;
00590f82 37 r = c->find(bus, path, c->interface, u, &found_u, error);
caa82984 38 bus->current_userdata = NULL;
1b64f838 39 bus->current_slot = sd_bus_slot_unref(s);
19befb2d 40
f00c3121
LP
41 if (r < 0)
42 return r;
43 if (sd_bus_error_is_set(error))
5958d089 44 return -sd_bus_error_get_errno(error);
f00c3121 45 if (r == 0)
992c052c 46 return r;
00590f82
AJ
47 } else
48 found_u = u;
992c052c
LP
49
50 if (userdata)
00590f82 51 *userdata = found_u;
992c052c
LP
52
53 return 1;
54}
55
09c8a7c6 56static 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
992c052c
LP
62static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
63 assert(p);
64
77a874a3 65 return (uint8_t*) u + p->x.property.offset;
992c052c
LP
66}
67
68static int vtable_property_get_userdata(
69 sd_bus *bus,
70 const char *path,
71 struct vtable_member *p,
f00c3121
LP
72 void **userdata,
73 sd_bus_error *error) {
992c052c
LP
74
75 void *u;
76 int r;
77
78 assert(bus);
79 assert(path);
80 assert(p);
81 assert(userdata);
82
f00c3121 83 r = node_vtable_get_userdata(bus, path, p->parent, &u, error);
992c052c
LP
84 if (r <= 0)
85 return r;
68313d3d
LP
86 if (bus->nodes_modified)
87 return 0;
992c052c
LP
88
89 *userdata = vtable_property_convert_userdata(p->vtable, u);
90 return 1;
91}
92
dfa92725
LP
93static int add_enumerated_to_set(
94 sd_bus *bus,
95 const char *prefix,
96 struct node_enumerator *first,
f00c3121
LP
97 Set *s,
98 sd_bus_error *error) {
dfa92725 99
992c052c
LP
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;
1b64f838 109 sd_bus_slot *slot;
992c052c 110
68313d3d
LP
111 if (bus->nodes_modified)
112 return 0;
113
1b64f838
LP
114 slot = container_of(c, sd_bus_slot, node_enumerator);
115
116 bus->current_slot = sd_bus_slot_ref(slot);
caa82984 117 bus->current_userdata = slot->userdata;
1b64f838 118 r = c->callback(bus, prefix, slot->userdata, &children, error);
caa82984 119 bus->current_userdata = NULL;
1b64f838 120 bus->current_slot = sd_bus_slot_unref(slot);
19befb2d 121
992c052c
LP
122 if (r < 0)
123 return r;
f00c3121 124 if (sd_bus_error_is_set(error))
5958d089 125 return -sd_bus_error_get_errno(error);
992c052c
LP
126
127 STRV_FOREACH(k, children) {
128 if (r < 0) {
129 free(*k);
130 continue;
131 }
132
9ed794a3 133 if (!object_path_is_valid(*k)) {
992c052c
LP
134 free(*k);
135 r = -EINVAL;
136 continue;
137 }
138
5d66866d
LP
139 if (!object_path_startswith(*k, prefix)) {
140 free(*k);
141 continue;
142 }
143
992c052c 144 r = set_consume(s, *k);
52c7f2b2
LP
145 if (r == -EEXIST)
146 r = 0;
992c052c
LP
147 }
148
149 free(children);
150 if (r < 0)
151 return r;
152 }
153
154 return 0;
155}
156
44eb1add
DH
157enum {
158 /* if set, add_subtree() works recursively */
ef31828d 159 CHILDREN_RECURSIVE = 1 << 0,
44eb1add 160 /* if set, add_subtree() scans object-manager hierarchies recursively */
ef31828d 161 CHILDREN_SUBHIERARCHIES = 1 << 1,
44eb1add
DH
162};
163
dfa92725
LP
164static int add_subtree_to_set(
165 sd_bus *bus,
166 const char *prefix,
167 struct node *n,
14cb109d 168 unsigned flags,
f00c3121
LP
169 Set *s,
170 sd_bus_error *error) {
dfa92725 171
992c052c
LP
172 struct node *i;
173 int r;
174
175 assert(bus);
176 assert(prefix);
177 assert(n);
178 assert(s);
179
f00c3121 180 r = add_enumerated_to_set(bus, prefix, n->enumerators, s, error);
992c052c
LP
181 if (r < 0)
182 return r;
68313d3d
LP
183 if (bus->nodes_modified)
184 return 0;
992c052c
LP
185
186 LIST_FOREACH(siblings, i, n->child) {
187 char *t;
188
5d66866d
LP
189 if (!object_path_startswith(i->path, prefix))
190 continue;
191
992c052c
LP
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
44eb1add
DH
200 if ((flags & CHILDREN_RECURSIVE) &&
201 ((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
202 r = add_subtree_to_set(bus, prefix, i, flags, s, error);
2d5c8a27
DH
203 if (r < 0)
204 return r;
205 if (bus->nodes_modified)
206 return 0;
207 }
992c052c
LP
208 }
209
210 return 0;
211}
212
dfa92725
LP
213static int get_child_nodes(
214 sd_bus *bus,
215 const char *prefix,
216 struct node *n,
14cb109d 217 unsigned flags,
f00c3121
LP
218 Set **_s,
219 sd_bus_error *error) {
dfa92725 220
992c052c
LP
221 Set *s = NULL;
222 int r;
223
224 assert(bus);
dfa92725 225 assert(prefix);
992c052c
LP
226 assert(n);
227 assert(_s);
228
d5099efc 229 s = set_new(&string_hash_ops);
992c052c
LP
230 if (!s)
231 return -ENOMEM;
232
44eb1add 233 r = add_subtree_to_set(bus, prefix, n, flags, s, error);
992c052c
LP
234 if (r < 0) {
235 set_free_free(s);
236 return r;
237 }
238
239 *_s = s;
240 return 0;
241}
242
243static 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) {
4afd3348 258 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1b64f838 259 sd_bus_slot *slot;
ebcf1f97 260
68313d3d
LP
261 if (bus->nodes_modified)
262 return 0;
263
992c052c
LP
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
68313d3d
LP
272 c->last_iteration = bus->iteration_counter;
273
992c052c
LP
274 r = sd_bus_message_rewind(m, true);
275 if (r < 0)
276 return r;
277
1b64f838
LP
278 slot = container_of(c, sd_bus_slot, node_callback);
279
280 bus->current_slot = sd_bus_slot_ref(slot);
caa82984
LP
281 bus->current_handler = c->callback;
282 bus->current_userdata = slot->userdata;
19070062 283 r = c->callback(m, slot->userdata, &error_buffer);
caa82984
LP
284 bus->current_userdata = NULL;
285 bus->current_handler = NULL;
1b64f838 286 bus->current_slot = sd_bus_slot_unref(slot);
19befb2d 287
ebcf1f97 288 r = bus_maybe_reply_error(m, r, &error_buffer);
992c052c
LP
289 if (r != 0)
290 return r;
291 }
292
293 return 0;
294}
295
adacb957
LP
296#define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
297
298static int check_access(sd_bus *bus, sd_bus_message *m, struct vtable_member *c, sd_bus_error *error) {
adacb957 299 uint64_t cap;
adacb957
LP
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
adacb957
LP
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
313cefa1 325 cap--;
adacb957 326
def9a7aa
LP
327 r = sd_bus_query_sender_privilege(m, cap);
328 if (r < 0)
329 return r;
adacb957 330 if (r > 0)
def9a7aa 331 return 0;
adacb957
LP
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
992c052c
LP
336static 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
4afd3348 343 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
992c052c
LP
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
adacb957
LP
356 r = check_access(bus, m, c, &error);
357 if (r < 0)
358 return bus_maybe_reply_error(m, r, &error);
359
f00c3121 360 r = node_vtable_get_userdata(bus, m->path, c->parent, &u, &error);
992c052c 361 if (r <= 0)
f00c3121 362 return bus_maybe_reply_error(m, r, &error);
68313d3d
LP
363 if (bus->nodes_modified)
364 return 0;
992c052c 365
09c8a7c6 366 u = vtable_method_convert_userdata(c->vtable, u);
367
992c052c
LP
368 *found_object = true;
369
68313d3d
LP
370 if (c->last_iteration == bus->iteration_counter)
371 return 0;
372
373 c->last_iteration = bus->iteration_counter;
374
992c052c
LP
375 r = sd_bus_message_rewind(m, true);
376 if (r < 0)
377 return r;
378
40ca29a1
LP
379 signature = sd_bus_message_get_signature(m, true);
380 if (!signature)
381 return -EINVAL;
992c052c 382
f00c3121 383 if (!streq(strempty(c->vtable->x.method.signature), signature))
86b8d289
LP
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));
992c052c 389
6717d473
LP
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
ebcf1f97 395 if (c->vtable->x.method.handler) {
1b64f838
LP
396 sd_bus_slot *slot;
397
398 slot = container_of(c->parent, sd_bus_slot, node_vtable);
19befb2d 399
1b64f838 400 bus->current_slot = sd_bus_slot_ref(slot);
caa82984
LP
401 bus->current_handler = c->vtable->x.method.handler;
402 bus->current_userdata = u;
19070062 403 r = c->vtable->x.method.handler(m, u, &error);
caa82984
LP
404 bus->current_userdata = NULL;
405 bus->current_handler = NULL;
1b64f838 406 bus->current_slot = sd_bus_slot_unref(slot);
19befb2d 407
f00c3121 408 return bus_maybe_reply_error(m, r, &error);
ebcf1f97 409 }
992c052c
LP
410
411 /* If the method callback is NULL, make this a successful NOP */
df2d202e 412 r = sd_bus_reply_method_return(m, NULL);
992c052c
LP
413 if (r < 0)
414 return r;
415
416 return 1;
417}
418
419static int invoke_property_get(
420 sd_bus *bus,
19befb2d 421 sd_bus_slot *slot,
992c052c
LP
422 const sd_bus_vtable *v,
423 const char *path,
424 const char *interface,
425 const char *property,
ebcf1f97
LP
426 sd_bus_message *reply,
427 void *userdata,
428 sd_bus_error *error) {
992c052c 429
af8601fa 430 const void *p;
f00c3121 431 int r;
992c052c
LP
432
433 assert(bus);
19befb2d 434 assert(slot);
992c052c 435 assert(v);
dfa92725
LP
436 assert(path);
437 assert(interface);
438 assert(property);
ebcf1f97 439 assert(reply);
992c052c 440
f00c3121 441 if (v->x.property.get) {
19befb2d 442
1b64f838 443 bus->current_slot = sd_bus_slot_ref(slot);
caa82984 444 bus->current_userdata = userdata;
f00c3121 445 r = v->x.property.get(bus, path, interface, property, reply, userdata, error);
caa82984 446 bus->current_userdata = NULL;
1b64f838 447 bus->current_slot = sd_bus_slot_unref(slot);
19befb2d 448
f00c3121
LP
449 if (r < 0)
450 return r;
451 if (sd_bus_error_is_set(error))
5958d089 452 return -sd_bus_error_get_errno(error);
f00c3121
LP
453 return r;
454 }
992c052c
LP
455
456 /* Automatic handling if no callback is defined. */
457
c13c7de3 458 if (streq(v->x.property.signature, "as"))
ebcf1f97 459 return sd_bus_message_append_strv(reply, *(char***) userdata);
c13c7de3 460
77a874a3
LP
461 assert(signature_is_single(v->x.property.signature, false));
462 assert(bus_type_is_basic(v->x.property.signature[0]));
992c052c 463
77a874a3 464 switch (v->x.property.signature[0]) {
992c052c
LP
465
466 case SD_BUS_TYPE_STRING:
28d6633a
LP
467 case SD_BUS_TYPE_SIGNATURE:
468 p = strempty(*(char**) userdata);
af8601fa
KS
469 break;
470
992c052c 471 case SD_BUS_TYPE_OBJECT_PATH:
992c052c 472 p = *(char**) userdata;
28d6633a 473 assert(p);
992c052c
LP
474 break;
475
476 default:
477 p = userdata;
478 break;
479 }
480
ebcf1f97 481 return sd_bus_message_append_basic(reply, v->x.property.signature[0], p);
992c052c
LP
482}
483
484static int invoke_property_set(
485 sd_bus *bus,
19befb2d 486 sd_bus_slot *slot,
992c052c
LP
487 const sd_bus_vtable *v,
488 const char *path,
489 const char *interface,
490 const char *property,
491 sd_bus_message *value,
ebcf1f97
LP
492 void *userdata,
493 sd_bus_error *error) {
992c052c
LP
494
495 int r;
496
497 assert(bus);
19befb2d 498 assert(slot);
992c052c 499 assert(v);
dfa92725
LP
500 assert(path);
501 assert(interface);
502 assert(property);
503 assert(value);
992c052c 504
f00c3121 505 if (v->x.property.set) {
19befb2d 506
1b64f838 507 bus->current_slot = sd_bus_slot_ref(slot);
caa82984 508 bus->current_userdata = userdata;
f00c3121 509 r = v->x.property.set(bus, path, interface, property, value, userdata, error);
caa82984 510 bus->current_userdata = NULL;
1b64f838 511 bus->current_slot = sd_bus_slot_unref(slot);
19befb2d 512
f00c3121
LP
513 if (r < 0)
514 return r;
515 if (sd_bus_error_is_set(error))
5958d089 516 return -sd_bus_error_get_errno(error);
f00c3121
LP
517 return r;
518 }
992c052c
LP
519
520 /* Automatic handling if no callback is defined. */
521
77a874a3
LP
522 assert(signature_is_single(v->x.property.signature, false));
523 assert(bus_type_is_basic(v->x.property.signature[0]));
992c052c 524
77a874a3 525 switch (v->x.property.signature[0]) {
992c052c
LP
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
77a874a3 533 r = sd_bus_message_read_basic(value, v->x.property.signature[0], &p);
992c052c
LP
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:
77a874a3 548 r = sd_bus_message_read_basic(value, v->x.property.signature[0], userdata);
992c052c
LP
549 if (r < 0)
550 return r;
551
552 break;
553 }
554
555 return 1;
556}
557
558static 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
4afd3348
LP
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;
19befb2d 568 sd_bus_slot *slot;
c94d7fc3 569 void *u = NULL;
992c052c
LP
570 int r;
571
572 assert(bus);
573 assert(m);
dfa92725 574 assert(c);
992c052c
LP
575 assert(found_object);
576
577 if (require_fallback && !c->parent->is_fallback)
578 return 0;
579
f00c3121 580 r = vtable_property_get_userdata(bus, m->path, c, &u, &error);
992c052c 581 if (r <= 0)
f00c3121 582 return bus_maybe_reply_error(m, r, &error);
68313d3d
LP
583 if (bus->nodes_modified)
584 return 0;
992c052c 585
19befb2d
LP
586 slot = container_of(c->parent, sd_bus_slot, node_vtable);
587
992c052c
LP
588 *found_object = true;
589
df2d202e 590 r = sd_bus_message_new_method_return(m, &reply);
992c052c
LP
591 if (r < 0)
592 return r;
593
992c052c 594 if (is_get) {
68313d3d
LP
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
77a874a3 602 r = sd_bus_message_open_container(reply, 'v', c->vtable->x.property.signature);
992c052c
LP
603 if (r < 0)
604 return r;
605
adacb957
LP
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
19befb2d 611 r = invoke_property_get(bus, slot, c->vtable, m->path, c->interface, c->member, reply, u, &error);
992c052c 612 if (r < 0)
f00c3121 613 return bus_maybe_reply_error(m, r, &error);
992c052c 614
68313d3d
LP
615 if (bus->nodes_modified)
616 return 0;
617
992c052c
LP
618 r = sd_bus_message_close_container(reply);
619 if (r < 0)
620 return r;
621
622 } else {
0ca454d4
LP
623 const char *signature = NULL;
624 char type = 0;
625
992c052c 626 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
ebcf1f97 627 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
68313d3d 628
ebcf1f97
LP
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;
992c052c 634
ebcf1f97 635 c->last_iteration = bus->iteration_counter;
992c052c 636
0ca454d4
LP
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
ebcf1f97
LP
644 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
645 if (r < 0)
646 return r;
992c052c 647
adacb957
LP
648 r = check_access(bus, m, c, &error);
649 if (r < 0)
650 return bus_maybe_reply_error(m, r, &error);
651
19befb2d 652 r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
ebcf1f97 653 if (r < 0)
f00c3121 654 return bus_maybe_reply_error(m, r, &error);
992c052c 655
68313d3d
LP
656 if (bus->nodes_modified)
657 return 0;
658
992c052c
LP
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
a03e4337
LP
671static 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
19befb2d 680 sd_bus_slot *slot;
a03e4337
LP
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
19befb2d
LP
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);
a03e4337
LP
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
992c052c
LP
720static 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);
dfa92725 733 assert(path);
992c052c
LP
734 assert(c);
735
6e8df5f0
LP
736 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
737 return 1;
738
992c052c 739 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
945c2931 740 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
992c052c
LP
741 continue;
742
6e8df5f0
LP
743 if (v->flags & SD_BUS_VTABLE_HIDDEN)
744 continue;
745
33702051
LP
746 if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
747 continue;
748
a03e4337 749 r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
f00c3121
LP
750 if (r < 0)
751 return r;
68313d3d
LP
752 if (bus->nodes_modified)
753 return 0;
992c052c
LP
754 }
755
756 return 1;
757}
758
759static 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
4afd3348 767 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
992c052c 768 struct node_vtable *c;
bd037038 769 bool found_interface;
992c052c
LP
770 int r;
771
772 assert(bus);
773 assert(m);
774 assert(found_object);
775
df2d202e 776 r = sd_bus_message_new_method_return(m, &reply);
992c052c
LP
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
38911893 784 found_interface = !iface ||
bd037038
LP
785 streq(iface, "org.freedesktop.DBus.Properties") ||
786 streq(iface, "org.freedesktop.DBus.Peer") ||
787 streq(iface, "org.freedesktop.DBus.Introspectable");
788
992c052c 789 LIST_FOREACH(vtables, c, first) {
4afd3348 790 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
992c052c
LP
791 void *u;
792
793 if (require_fallback && !c->is_fallback)
794 continue;
795
f00c3121 796 r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
992c052c 797 if (r < 0)
f00c3121 798 return bus_maybe_reply_error(m, r, &error);
68313d3d
LP
799 if (bus->nodes_modified)
800 return 0;
992c052c
LP
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
992c052c
LP
810 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
811 if (r < 0)
f00c3121 812 return bus_maybe_reply_error(m, r, &error);
68313d3d
LP
813 if (bus->nodes_modified)
814 return 0;
992c052c
LP
815 }
816
733e792b
AJ
817 if (!*found_object)
818 return 0;
819
992c052c
LP
820 if (!found_interface) {
821 r = sd_bus_reply_method_errorf(
df2d202e 822 m,
40ca29a1 823 SD_BUS_ERROR_UNKNOWN_INTERFACE,
992c052c
LP
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
ff02f101 842static int bus_node_exists(
dfa92725
LP
843 sd_bus *bus,
844 struct node *n,
845 const char *path,
846 bool require_fallback) {
847
992c052c
LP
848 struct node_vtable *c;
849 struct node_callback *k;
ff02f101 850 int r;
992c052c
LP
851
852 assert(bus);
853 assert(n);
dfa92725 854 assert(path);
992c052c
LP
855
856 /* Tests if there's anything attached directly to this node
857 * for the specified path */
858
ff02f101
DH
859 if (!require_fallback && (n->enumerators || n->object_managers))
860 return true;
861
992c052c
LP
862 LIST_FOREACH(callbacks, k, n->callbacks) {
863 if (require_fallback && !k->is_fallback)
864 continue;
865
ff02f101 866 return 1;
992c052c
LP
867 }
868
869 LIST_FOREACH(vtables, c, n->vtables) {
4afd3348 870 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
992c052c
LP
871
872 if (require_fallback && !c->is_fallback)
873 continue;
874
ff02f101
DH
875 r = node_vtable_get_userdata(bus, path, c, NULL, &error);
876 if (r != 0)
877 return r;
68313d3d 878 if (bus->nodes_modified)
ff02f101 879 return 0;
992c052c
LP
880 }
881
ff02f101 882 return 0;
992c052c
LP
883}
884
885static 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
4afd3348
LP
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;
992c052c 894 _cleanup_set_free_free_ Set *s = NULL;
718db961 895 const char *previous_interface = NULL;
992c052c
LP
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
44eb1add 906 r = get_child_nodes(bus, m->path, n, 0, &s, &error);
992c052c 907 if (r < 0)
f00c3121 908 return bus_maybe_reply_error(m, r, &error);
68313d3d
LP
909 if (bus->nodes_modified)
910 return 0;
992c052c 911
7fb411f0 912 r = introspect_begin(&intro, bus->trusted);
992c052c
LP
913 if (r < 0)
914 return r;
915
943c3f94 916 r = introspect_write_default_interfaces(&intro, !require_fallback && n->object_managers);
992c052c
LP
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
f00c3121
LP
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 }
992c052c
LP
935 if (r == 0)
936 continue;
937
938 empty = false;
939
6e8df5f0
LP
940 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
941 continue;
942
718db961
LP
943 if (!streq_ptr(previous_interface, c->interface)) {
944
945 if (previous_interface)
0d536673 946 fputs(" </interface>\n", intro.f);
718db961
LP
947
948 fprintf(intro.f, " <interface name=\"%s\">\n", c->interface);
949 }
950
951 r = introspect_write_interface(&intro, c->vtable);
992c052c
LP
952 if (r < 0)
953 goto finish;
718db961
LP
954
955 previous_interface = c->interface;
992c052c
LP
956 }
957
718db961 958 if (previous_interface)
0d536673 959 fputs(" </interface>\n", intro.f);
718db961 960
992c052c
LP
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);
f2bfc6ba
LP
965 if (r <= 0) {
966 r = bus_maybe_reply_error(m, r, &error);
ff02f101 967 goto finish;
f2bfc6ba 968 }
ff02f101
DH
969 if (bus->nodes_modified) {
970 r = 0;
992c052c 971 goto finish;
ff02f101 972 }
992c052c
LP
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
991finish:
992 introspect_free(&intro);
993 return r;
994}
995
992c052c
LP
996static 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
718db961
LP
1004 const char *previous_interface = NULL;
1005 bool found_something = false;
992c052c
LP
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
992c052c 1020 LIST_FOREACH(vtables, i, n->vtables) {
92db139e 1021 void *u;
992c052c
LP
1022
1023 if (require_fallback && !i->is_fallback)
1024 continue;
1025
f00c3121 1026 r = node_vtable_get_userdata(bus, path, i, &u, error);
92db139e
LP
1027 if (r < 0)
1028 return r;
68313d3d
LP
1029 if (bus->nodes_modified)
1030 return 0;
92db139e
LP
1031 if (r == 0)
1032 continue;
1033
1034 if (!found_something) {
718db961
LP
1035
1036 /* Open the object part */
1037
92db139e
LP
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
c0e7906d
DH
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
92db139e
LP
1066 found_something = true;
1067 }
1068
718db961
LP
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);
992c052c
LP
1099 if (r < 0)
1100 return r;
68313d3d
LP
1101 if (bus->nodes_modified)
1102 return 0;
718db961
LP
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;
992c052c
LP
1115 }
1116
92db139e
LP
1117 if (found_something) {
1118 r = sd_bus_message_close_container(reply);
1119 if (r < 0)
1120 return r;
992c052c 1121
92db139e
LP
1122 r = sd_bus_message_close_container(reply);
1123 if (r < 0)
1124 return r;
1125 }
992c052c
LP
1126
1127 return 1;
1128}
1129
1130static 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
92e189e5 1136 char *prefix;
992c052c
LP
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;
68313d3d
LP
1148 if (bus->nodes_modified)
1149 return 0;
992c052c
LP
1150
1151 /* Second, add fallback vtables registered for any of the prefixes */
6e9417f5 1152 prefix = newa(char, strlen(path) + 1);
92e189e5
LP
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;
68313d3d
LP
1157 if (bus->nodes_modified)
1158 return 0;
992c052c
LP
1159 }
1160
1161 return 0;
1162}
1163
1164static 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
4afd3348
LP
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;
992c052c 1173 _cleanup_set_free_free_ Set *s = NULL;
943c3f94
DH
1174 Iterator i;
1175 char *path;
992c052c
LP
1176 int r;
1177
1178 assert(bus);
1179 assert(m);
1180 assert(n);
1181 assert(found_object);
1182
943c3f94
DH
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)
992c052c
LP
1188 return 0;
1189
44eb1add 1190 r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
992c052c 1191 if (r < 0)
f2bfc6ba 1192 return bus_maybe_reply_error(m, r, &error);
68313d3d
LP
1193 if (bus->nodes_modified)
1194 return 0;
992c052c 1195
df2d202e 1196 r = sd_bus_message_new_method_return(m, &reply);
992c052c
LP
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
943c3f94
DH
1204 SET_FOREACH(path, s, i) {
1205 r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
1206 if (r < 0)
f2bfc6ba 1207 return bus_maybe_reply_error(m, r, &error);
992c052c 1208
943c3f94 1209 if (bus->nodes_modified)
992c052c 1210 return 0;
992c052c
LP
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
1224static 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;
68313d3d
LP
1248 if (bus->nodes_modified)
1249 return 0;
992c052c
LP
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;
68313d3d
LP
1264 if (bus->nodes_modified)
1265 return 0;
992c052c
LP
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)
0fc5ab90 1284 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters");
992c052c
LP
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)
0fc5ab90 1302 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter");
992c052c
LP
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
0fc5ab90
LP
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
992c052c
LP
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
0fc5ab90
LP
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
992c052c
LP
1326 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
1327 if (r != 0)
1328 return r;
1329 }
1330
68313d3d
LP
1331 if (bus->nodes_modified)
1332 return 0;
1333
992c052c
LP
1334 if (!*found_object) {
1335 r = bus_node_exists(bus, n, m->path, require_fallback);
1336 if (r < 0)
f2bfc6ba 1337 return bus_maybe_reply_error(m, r, NULL);
ff02f101
DH
1338 if (bus->nodes_modified)
1339 return 0;
992c052c
LP
1340 if (r > 0)
1341 *found_object = true;
1342 }
1343
1344 return 0;
1345}
1346
1347int 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
c7db1984 1355 if (bus->is_monitor)
09365592
LP
1356 return 0;
1357
40ca29a1 1358 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
992c052c
LP
1359 return 0;
1360
992c052c
LP
1361 if (hashmap_isempty(bus->nodes))
1362 return 0;
1363
f820cf99
LP
1364 /* Never respond to broadcast messages */
1365 if (bus->bus_client && !m->destination)
1366 return 0;
1367
adacb957
LP
1368 assert(m->path);
1369 assert(m->member);
1370
992c052c
LP
1371 pl = strlen(m->path);
1372 do {
92e189e5 1373 char prefix[pl+1];
992c052c
LP
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 */
92e189e5 1382 OBJECT_PATH_FOREACH_PREFIX(prefix, m->path) {
992c052c
LP
1383
1384 if (bus->nodes_modified)
1385 break;
1386
92e189e5 1387 r = object_find_and_run(bus, m, prefix, true, &found_object);
992c052c
LP
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(
df2d202e 1400 m,
40ca29a1 1401 SD_BUS_ERROR_UNKNOWN_PROPERTY,
992c052c
LP
1402 "Unknown property or interface.");
1403 else
1404 r = sd_bus_reply_method_errorf(
df2d202e 1405 m,
40ca29a1 1406 SD_BUS_ERROR_UNKNOWN_METHOD,
992c052c
LP
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
1415static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
1416 struct node *n, *parent;
1417 const char *e;
2fd069b1
ZJS
1418 _cleanup_free_ char *s = NULL;
1419 char *p;
992c052c
LP
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
d5099efc 1430 r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops);
992c052c
LP
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
7d67077f 1444 p = strndupa(path, MAX(1, e - path));
992c052c
LP
1445
1446 parent = bus_node_allocate(bus, p);
2fd069b1 1447 if (!parent)
992c052c 1448 return NULL;
992c052c
LP
1449 }
1450
1451 n = new0(struct node, 1);
1452 if (!n)
1453 return NULL;
1454
1455 n->parent = parent;
ae2a15bc 1456 n->path = TAKE_PTR(s);
992c052c 1457
8e050193 1458 r = hashmap_put(bus->nodes, n->path, n);
992c052c 1459 if (r < 0) {
2fd069b1 1460 free(n->path);
5fecf46d 1461 return mfree(n);
992c052c
LP
1462 }
1463
1464 if (parent)
71fda00f 1465 LIST_PREPEND(siblings, parent->child, n);
992c052c
LP
1466
1467 return n;
1468}
1469
19befb2d 1470void bus_node_gc(sd_bus *b, struct node *n) {
992c052c
LP
1471 assert(b);
1472
1473 if (!n)
1474 return;
1475
1476 if (n->child ||
1477 n->callbacks ||
1478 n->vtables ||
1479 n->enumerators ||
19befb2d 1480 n->object_managers)
992c052c
LP
1481 return;
1482
85e55d14 1483 assert_se(hashmap_remove(b->nodes, n->path) == n);
992c052c
LP
1484
1485 if (n->parent)
71fda00f 1486 LIST_REMOVE(siblings, n->parent->child, n);
992c052c
LP
1487
1488 free(n->path);
1489 bus_node_gc(b, n->parent);
1490 free(n);
1491}
1492
2d5c8a27
DH
1493static 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
6e9417f5 1503 prefix = newa(char, strlen(path) + 1);
2d5c8a27
DH
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
992c052c 1519static int bus_add_object(
dfa92725 1520 sd_bus *bus,
19befb2d 1521 sd_bus_slot **slot,
992c052c
LP
1522 bool fallback,
1523 const char *path,
1524 sd_bus_message_handler_t callback,
1525 void *userdata) {
1526
19befb2d 1527 sd_bus_slot *s;
992c052c
LP
1528 struct node *n;
1529 int r;
1530
dfa92725 1531 assert_return(bus, -EINVAL);
45b1f410 1532 assert_return(bus = bus_resolve(bus), -ENOPKG);
dfa92725
LP
1533 assert_return(object_path_is_valid(path), -EINVAL);
1534 assert_return(callback, -EINVAL);
1535 assert_return(!bus_pid_changed(bus), -ECHILD);
992c052c 1536
dfa92725 1537 n = bus_node_allocate(bus, path);
992c052c
LP
1538 if (!n)
1539 return -ENOMEM;
1540
19befb2d
LP
1541 s = bus_slot_allocate(bus, !slot, BUS_NODE_CALLBACK, sizeof(struct node_callback), userdata);
1542 if (!s) {
992c052c
LP
1543 r = -ENOMEM;
1544 goto fail;
1545 }
1546
19befb2d
LP
1547 s->node_callback.callback = callback;
1548 s->node_callback.is_fallback = fallback;
992c052c 1549
19befb2d
LP
1550 s->node_callback.node = n;
1551 LIST_PREPEND(callbacks, n->callbacks, &s->node_callback);
68313d3d
LP
1552 bus->nodes_modified = true;
1553
19befb2d
LP
1554 if (slot)
1555 *slot = s;
1556
992c052c
LP
1557 return 0;
1558
1559fail:
19befb2d 1560 sd_bus_slot_unref(s);
dfa92725 1561 bus_node_gc(bus, n);
19befb2d 1562
992c052c
LP
1563 return r;
1564}
1565
19befb2d 1566_public_ int sd_bus_add_object(
992c052c 1567 sd_bus *bus,
19befb2d 1568 sd_bus_slot **slot,
992c052c
LP
1569 const char *path,
1570 sd_bus_message_handler_t callback,
1571 void *userdata) {
1572
19befb2d 1573 return bus_add_object(bus, slot, false, path, callback, userdata);
992c052c
LP
1574}
1575
19befb2d
LP
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) {
992c052c 1582
19befb2d 1583 return bus_add_object(bus, slot, true, prefix, callback, userdata);
992c052c
LP
1584}
1585
7a08d314 1586static void vtable_member_hash_func(const struct vtable_member *m, struct siphash *state) {
dfa92725
LP
1587 assert(m);
1588
b826ab58
TG
1589 string_hash_func(m->path, state);
1590 string_hash_func(m->interface, state);
1591 string_hash_func(m->member, state);
992c052c
LP
1592}
1593
7a08d314 1594static int vtable_member_compare_func(const struct vtable_member *x, const struct vtable_member *y) {
992c052c
LP
1595 int r;
1596
dfa92725
LP
1597 assert(x);
1598 assert(y);
1599
992c052c
LP
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
7a08d314 1611DEFINE_PRIVATE_HASH_OPS(vtable_member_hash_ops, struct vtable_member, vtable_member_hash_func, vtable_member_compare_func);
d5099efc 1612
992c052c
LP
1613static int add_object_vtable_internal(
1614 sd_bus *bus,
19befb2d 1615 sd_bus_slot **slot,
992c052c
LP
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
2915234d 1623 sd_bus_slot *s = NULL;
19befb2d 1624 struct node_vtable *i, *existing = NULL;
992c052c
LP
1625 const sd_bus_vtable *v;
1626 struct node *n;
1627 int r;
1628
dfa92725 1629 assert_return(bus, -EINVAL);
45b1f410 1630 assert_return(bus = bus_resolve(bus), -ENOPKG);
dfa92725
LP
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);
718db961
LP
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);
992c052c 1641
d5099efc 1642 r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops);
992c052c
LP
1643 if (r < 0)
1644 return r;
1645
d5099efc 1646 r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops);
992c052c
LP
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) {
992c052c
LP
1655 if (i->is_fallback != fallback) {
1656 r = -EPROTOTYPE;
1657 goto fail;
1658 }
718db961
LP
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 }
992c052c
LP
1669 }
1670
19befb2d
LP
1671 s = bus_slot_allocate(bus, !slot, BUS_NODE_VTABLE, sizeof(struct node_vtable), userdata);
1672 if (!s) {
992c052c
LP
1673 r = -ENOMEM;
1674 goto fail;
1675 }
1676
19befb2d
LP
1677 s->node_vtable.is_fallback = fallback;
1678 s->node_vtable.vtable = vtable;
1679 s->node_vtable.find = find;
992c052c 1680
19befb2d
LP
1681 s->node_vtable.interface = strdup(interface);
1682 if (!s->node_vtable.interface) {
992c052c
LP
1683 r = -ENOMEM;
1684 goto fail;
1685 }
1686
19befb2d 1687 for (v = s->node_vtable.vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
992c052c
LP
1688
1689 switch (v->type) {
1690
1691 case _SD_BUS_VTABLE_METHOD: {
1692 struct vtable_member *m;
1693
77a874a3
LP
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))) ||
df98a87b 1698 v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) {
992c052c
LP
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
19befb2d 1709 m->parent = &s->node_vtable;
992c052c 1710 m->path = n->path;
19befb2d 1711 m->interface = s->node_vtable.interface;
77a874a3 1712 m->member = v->x.method.member;
992c052c
LP
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
77a874a3 1726 if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
992c052c
LP
1727 r = -EINVAL;
1728 goto fail;
1729 }
9b772efb
LP
1730
1731 if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
1732 r = -EINVAL;
1733 goto fail;
1734 }
992c052c 1735
4831981d 1736 _fallthrough_;
992c052c
LP
1737 case _SD_BUS_VTABLE_PROPERTY: {
1738 struct vtable_member *m;
1739
77a874a3
LP
1740 if (!member_name_is_valid(v->x.property.member) ||
1741 !signature_is_single(v->x.property.signature, false) ||
c13c7de3 1742 !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
33702051 1743 (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
df98a87b 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 ||
33702051 1745 ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
adacb957 1746 (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
992c052c
LP
1747 r = -EINVAL;
1748 goto fail;
1749 }
1750
992c052c
LP
1751 m = new0(struct vtable_member, 1);
1752 if (!m) {
1753 r = -ENOMEM;
1754 goto fail;
1755 }
1756
19befb2d 1757 m->parent = &s->node_vtable;
992c052c 1758 m->path = n->path;
19befb2d 1759 m->interface = s->node_vtable.interface;
77a874a3 1760 m->member = v->x.property.member;
992c052c
LP
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
77a874a3 1774 if (!member_name_is_valid(v->x.signal.member) ||
adacb957
LP
1775 !signature_is_valid(strempty(v->x.signal.signature), false) ||
1776 v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
992c052c
LP
1777 r = -EINVAL;
1778 goto fail;
1779 }
1780
1781 break;
1782
1783 default:
1784 r = -EINVAL;
1785 goto fail;
1786 }
1787 }
1788
19befb2d
LP
1789 s->node_vtable.node = n;
1790 LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
68313d3d
LP
1791 bus->nodes_modified = true;
1792
19befb2d
LP
1793 if (slot)
1794 *slot = s;
1795
992c052c
LP
1796 return 0;
1797
1798fail:
19befb2d 1799 sd_bus_slot_unref(s);
dfa92725
LP
1800 bus_node_gc(bus, n);
1801
19befb2d 1802 return r;
992c052c
LP
1803}
1804
d9f644e2 1805_public_ int sd_bus_add_object_vtable(
992c052c 1806 sd_bus *bus,
19befb2d 1807 sd_bus_slot **slot,
992c052c
LP
1808 const char *path,
1809 const char *interface,
1810 const sd_bus_vtable *vtable,
1811 void *userdata) {
1812
19befb2d 1813 return add_object_vtable_internal(bus, slot, path, interface, vtable, false, NULL, userdata);
992c052c
LP
1814}
1815
d9f644e2 1816_public_ int sd_bus_add_fallback_vtable(
992c052c 1817 sd_bus *bus,
19befb2d
LP
1818 sd_bus_slot **slot,
1819 const char *prefix,
718db961
LP
1820 const char *interface,
1821 const sd_bus_vtable *vtable,
1822 sd_bus_object_find_t find,
1823 void *userdata) {
992c052c 1824
19befb2d 1825 return add_object_vtable_internal(bus, slot, prefix, interface, vtable, true, find, userdata);
992c052c
LP
1826}
1827
d9f644e2 1828_public_ int sd_bus_add_node_enumerator(
992c052c 1829 sd_bus *bus,
19befb2d 1830 sd_bus_slot **slot,
992c052c
LP
1831 const char *path,
1832 sd_bus_node_enumerator_t callback,
1833 void *userdata) {
1834
19befb2d 1835 sd_bus_slot *s;
992c052c
LP
1836 struct node *n;
1837 int r;
1838
dfa92725 1839 assert_return(bus, -EINVAL);
45b1f410 1840 assert_return(bus = bus_resolve(bus), -ENOPKG);
dfa92725
LP
1841 assert_return(object_path_is_valid(path), -EINVAL);
1842 assert_return(callback, -EINVAL);
1843 assert_return(!bus_pid_changed(bus), -ECHILD);
992c052c
LP
1844
1845 n = bus_node_allocate(bus, path);
1846 if (!n)
1847 return -ENOMEM;
1848
19befb2d
LP
1849 s = bus_slot_allocate(bus, !slot, BUS_NODE_ENUMERATOR, sizeof(struct node_enumerator), userdata);
1850 if (!s) {
992c052c
LP
1851 r = -ENOMEM;
1852 goto fail;
1853 }
1854
19befb2d 1855 s->node_enumerator.callback = callback;
68313d3d 1856
19befb2d
LP
1857 s->node_enumerator.node = n;
1858 LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
68313d3d
LP
1859 bus->nodes_modified = true;
1860
19befb2d
LP
1861 if (slot)
1862 *slot = s;
1863
992c052c
LP
1864 return 0;
1865
1866fail:
19befb2d 1867 sd_bus_slot_unref(s);
992c052c
LP
1868 bus_node_gc(bus, n);
1869
19befb2d 1870 return r;
992c052c
LP
1871}
1872
1873static 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,
46525bfc 1879 bool *found_interface,
992c052c
LP
1880 char **names) {
1881
4afd3348
LP
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;
718db961
LP
1884 bool has_invalidating = false, has_changing = false;
1885 struct vtable_member key = {};
992c052c
LP
1886 struct node_vtable *c;
1887 struct node *n;
1888 char **property;
1889 void *u = NULL;
1890 int r;
1891
1892 assert(bus);
dfa92725 1893 assert(prefix);
992c052c
LP
1894 assert(path);
1895 assert(interface);
46525bfc 1896 assert(found_interface);
992c052c
LP
1897
1898 n = hashmap_get(bus->nodes, prefix);
1899 if (!n)
1900 return 0;
1901
151b9b96 1902 r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
992c052c
LP
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
718db961
LP
1917 LIST_FOREACH(vtables, c, n->vtables) {
1918 if (require_fallback && !c->is_fallback)
1919 continue;
992c052c 1920
718db961 1921 if (!streq(c->interface, interface))
992c052c 1922 continue;
992c052c 1923
f00c3121 1924 r = node_vtable_get_userdata(bus, path, c, &u, &error);
992c052c
LP
1925 if (r < 0)
1926 return r;
718db961
LP
1927 if (bus->nodes_modified)
1928 return 0;
1929 if (r == 0)
1930 continue;
992c052c 1931
46525bfc
LP
1932 *found_interface = true;
1933
a03e4337
LP
1934 if (names) {
1935 /* If the caller specified a list of
1936 * properties we include exactly those in the
1937 * PropertiesChanged message */
992c052c 1938
a03e4337
LP
1939 STRV_FOREACH(property, names) {
1940 struct vtable_member *v;
992c052c 1941
a03e4337 1942 assert_return(member_name_is_valid(*property), -EINVAL);
718db961 1943
a03e4337
LP
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;
992c052c 1954
a03e4337
LP
1955 assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ||
1956 v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
992c052c 1957
a03e4337
LP
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;
718db961 1972 }
a03e4337
LP
1973 } else {
1974 const sd_bus_vtable *v;
718db961 1975
a03e4337
LP
1976 /* If the caller specified no properties list
1977 * we include all properties that are marked
1978 * as changing in the message. */
718db961 1979
a03e4337 1980 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
945c2931 1981 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
a03e4337 1982 continue;
718db961 1983
a03e4337
LP
1984 if (v->flags & SD_BUS_VTABLE_HIDDEN)
1985 continue;
718db961 1986
a03e4337
LP
1987 if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1988 has_invalidating = true;
1989 continue;
1990 }
718db961 1991
a03e4337
LP
1992 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
1993 continue;
718db961 1994
a03e4337 1995 has_changing = true;
718db961 1996
a03e4337
LP
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 }
718db961 2003 }
992c052c
LP
2004 }
2005
718db961
LP
2006 if (!has_invalidating && !has_changing)
2007 return 0;
2008
992c052c
LP
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) {
718db961
LP
2018 LIST_FOREACH(vtables, c, n->vtables) {
2019 if (require_fallback && !c->is_fallback)
2020 continue;
992c052c 2021
718db961 2022 if (!streq(c->interface, interface))
992c052c
LP
2023 continue;
2024
f00c3121 2025 r = node_vtable_get_userdata(bus, path, c, &u, &error);
992c052c
LP
2026 if (r < 0)
2027 return r;
718db961
LP
2028 if (bus->nodes_modified)
2029 return 0;
2030 if (r == 0)
2031 continue;
2032
a03e4337
LP
2033 if (names) {
2034 STRV_FOREACH(property, names) {
2035 struct vtable_member *v;
718db961 2036
a03e4337
LP
2037 key.member = *property;
2038 assert_se(v = hashmap_get(bus->vtable_properties, &key));
2039 assert(c == v->parent);
718db961 2040
a03e4337
LP
2041 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2042 continue;
718db961 2043
a03e4337
LP
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++) {
945c2931 2052 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
a03e4337
LP
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 }
718db961 2065 }
992c052c
LP
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
d9f644e2 2080_public_ int sd_bus_emit_properties_changed_strv(
dfa92725
LP
2081 sd_bus *bus,
2082 const char *path,
2083 const char *interface,
2084 char **names) {
2085
46525bfc 2086 bool found_interface = false;
92e189e5 2087 char *prefix;
992c052c
LP
2088 int r;
2089
dfa92725 2090 assert_return(bus, -EINVAL);
45b1f410 2091 assert_return(bus = bus_resolve(bus), -ENOPKG);
dfa92725
LP
2092 assert_return(object_path_is_valid(path), -EINVAL);
2093 assert_return(interface_name_is_valid(interface), -EINVAL);
dfa92725
LP
2094 assert_return(!bus_pid_changed(bus), -ECHILD);
2095
a3d59cd1
LP
2096 if (!BUS_IS_OPEN(bus->state))
2097 return -ENOTCONN;
a03e4337
LP
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)
dfa92725 2104 return 0;
992c052c 2105
7ae8edcd
ZJS
2106 BUS_DONT_DESTROY(bus);
2107
68313d3d
LP
2108 do {
2109 bus->nodes_modified = false;
992c052c 2110
46525bfc 2111 r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
92e189e5
LP
2112 if (r != 0)
2113 return r;
68313d3d
LP
2114 if (bus->nodes_modified)
2115 continue;
2116
6e9417f5 2117 prefix = newa(char, strlen(path) + 1);
68313d3d 2118 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
46525bfc 2119 r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
68313d3d
LP
2120 if (r != 0)
2121 return r;
2122 if (bus->nodes_modified)
2123 break;
2124 }
2125
2126 } while (bus->nodes_modified);
992c052c 2127
46525bfc 2128 return found_interface ? 0 : -ENOENT;
992c052c
LP
2129}
2130
d9f644e2 2131_public_ int sd_bus_emit_properties_changed(
dfa92725
LP
2132 sd_bus *bus,
2133 const char *path,
2134 const char *interface,
2135 const char *name, ...) {
2136
250a918d 2137 char **names;
992c052c 2138
dfa92725 2139 assert_return(bus, -EINVAL);
45b1f410 2140 assert_return(bus = bus_resolve(bus), -ENOPKG);
dfa92725
LP
2141 assert_return(object_path_is_valid(path), -EINVAL);
2142 assert_return(interface_name_is_valid(interface), -EINVAL);
dfa92725
LP
2143 assert_return(!bus_pid_changed(bus), -ECHILD);
2144
a3d59cd1
LP
2145 if (!BUS_IS_OPEN(bus->state))
2146 return -ENOTCONN;
2147
dfa92725
LP
2148 if (!name)
2149 return 0;
2150
250a918d 2151 names = strv_from_stdarg_alloca(name);
992c052c
LP
2152
2153 return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
2154}
2155
d95eb43e
DH
2156static 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) {
4afd3348 2180 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
d95eb43e
DH
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
2247static 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
6e9417f5 2294 prefix = newa(char, strlen(path) + 1);
d95eb43e
DH
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
969a9685 2306_public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
4afd3348 2307 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2d5c8a27 2308 struct node *object_manager;
d95eb43e
DH
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);
45b1f410 2323 assert_return(bus = bus_resolve(bus), -ENOPKG);
d95eb43e
DH
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
2d5c8a27
DH
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
7ae8edcd
ZJS
2336 BUS_DONT_DESTROY(bus);
2337
d95eb43e
DH
2338 do {
2339 bus->nodes_modified = false;
2340 m = sd_bus_message_unref(m);
2341
2d5c8a27 2342 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
d95eb43e
DH
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
2370static 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) {
4afd3348 2394 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
d95eb43e
DH
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
2431static 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
6e9417f5 2465 prefix = newa(char, strlen(path) + 1);
d95eb43e
DH
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
969a9685 2477_public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
4afd3348 2478 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2d5c8a27 2479 struct node *object_manager;
d95eb43e
DH
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);
45b1f410 2494 assert_return(bus = bus_resolve(bus), -ENOPKG);
d95eb43e
DH
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
2d5c8a27
DH
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
7ae8edcd
ZJS
2507 BUS_DONT_DESTROY(bus);
2508
d95eb43e
DH
2509 do {
2510 bus->nodes_modified = false;
2511 m = sd_bus_message_unref(m);
2512
2d5c8a27 2513 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
d95eb43e
DH
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
4be39163
LP
2541static 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
4afd3348 2549 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
718db961 2550 bool found_interface = false;
4be39163
LP
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
718db961
LP
2570 if (!streq(c->interface, interface))
2571 continue;
4be39163 2572
f00c3121 2573 r = node_vtable_get_userdata(bus, path, c, &u, &error);
718db961
LP
2574 if (r < 0)
2575 return r;
2576 if (bus->nodes_modified)
2577 return 0;
2578 if (r == 0)
2579 continue;
4be39163 2580
718db961
LP
2581 if (!found_interface) {
2582 r = sd_bus_message_append_basic(m, 's', interface);
2583 if (r < 0)
2584 return r;
4be39163 2585
718db961
LP
2586 r = sd_bus_message_open_container(m, 'a', "{sv}");
2587 if (r < 0)
2588 return r;
4be39163 2589
718db961
LP
2590 found_interface = true;
2591 }
4be39163 2592
718db961
LP
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 }
4be39163 2599
718db961
LP
2600 if (found_interface) {
2601 r = sd_bus_message_close_container(m);
2602 if (r < 0)
2603 return r;
2604 }
4be39163 2605
718db961 2606 return found_interface;
4be39163
LP
2607}
2608
2609static 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;
68313d3d
LP
2626 if (bus->nodes_modified)
2627 return 0;
4be39163 2628
6e9417f5 2629 prefix = newa(char, strlen(path) + 1);
4be39163
LP
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;
68313d3d
LP
2634 if (bus->nodes_modified)
2635 return 0;
4be39163
LP
2636 }
2637
2638 return -ENOENT;
992c052c
LP
2639}
2640
d9f644e2 2641_public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) {
4afd3348 2642 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2d5c8a27 2643 struct node *object_manager;
4be39163
LP
2644 char **i;
2645 int r;
2646
2647 assert_return(bus, -EINVAL);
45b1f410 2648 assert_return(bus = bus_resolve(bus), -ENOPKG);
4be39163 2649 assert_return(object_path_is_valid(path), -EINVAL);
4be39163
LP
2650 assert_return(!bus_pid_changed(bus), -ECHILD);
2651
a3d59cd1
LP
2652 if (!BUS_IS_OPEN(bus->state))
2653 return -ENOTCONN;
2654
4be39163
LP
2655 if (strv_isempty(interfaces))
2656 return 0;
2657
2d5c8a27
DH
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
7ae8edcd
ZJS
2664 BUS_DONT_DESTROY(bus);
2665
68313d3d
LP
2666 do {
2667 bus->nodes_modified = false;
1b02f301 2668 m = sd_bus_message_unref(m);
4be39163 2669
2d5c8a27 2670 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
68313d3d
LP
2671 if (r < 0)
2672 return r;
4be39163 2673
68313d3d 2674 r = sd_bus_message_append_basic(m, 'o', path);
4be39163
LP
2675 if (r < 0)
2676 return r;
2677
68313d3d 2678 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
4be39163
LP
2679 if (r < 0)
2680 return r;
2681
68313d3d
LP
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
4be39163
LP
2704 r = sd_bus_message_close_container(m);
2705 if (r < 0)
2706 return r;
4be39163 2707
68313d3d 2708 } while (bus->nodes_modified);
4be39163
LP
2709
2710 return sd_bus_send(bus, m, NULL);
2711}
2712
d9f644e2 2713_public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) {
250a918d 2714 char **interfaces;
4be39163
LP
2715
2716 assert_return(bus, -EINVAL);
45b1f410 2717 assert_return(bus = bus_resolve(bus), -ENOPKG);
4be39163 2718 assert_return(object_path_is_valid(path), -EINVAL);
4be39163
LP
2719 assert_return(!bus_pid_changed(bus), -ECHILD);
2720
a3d59cd1
LP
2721 if (!BUS_IS_OPEN(bus->state))
2722 return -ENOTCONN;
2723
250a918d 2724 interfaces = strv_from_stdarg_alloca(interface);
4be39163
LP
2725
2726 return sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
2727}
2728
d9f644e2 2729_public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
4afd3348 2730 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2d5c8a27 2731 struct node *object_manager;
4be39163
LP
2732 int r;
2733
2734 assert_return(bus, -EINVAL);
45b1f410 2735 assert_return(bus = bus_resolve(bus), -ENOPKG);
4be39163 2736 assert_return(object_path_is_valid(path), -EINVAL);
4be39163
LP
2737 assert_return(!bus_pid_changed(bus), -ECHILD);
2738
a3d59cd1
LP
2739 if (!BUS_IS_OPEN(bus->state))
2740 return -ENOTCONN;
2741
4be39163
LP
2742 if (strv_isempty(interfaces))
2743 return 0;
2744
2d5c8a27
DH
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");
4be39163
LP
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
d9f644e2 2766_public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) {
250a918d 2767 char **interfaces;
4be39163
LP
2768
2769 assert_return(bus, -EINVAL);
45b1f410 2770 assert_return(bus = bus_resolve(bus), -ENOPKG);
4be39163 2771 assert_return(object_path_is_valid(path), -EINVAL);
4be39163
LP
2772 assert_return(!bus_pid_changed(bus), -ECHILD);
2773
a3d59cd1
LP
2774 if (!BUS_IS_OPEN(bus->state))
2775 return -ENOTCONN;
2776
250a918d 2777 interfaces = strv_from_stdarg_alloca(interface);
4be39163
LP
2778
2779 return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
992c052c
LP
2780}
2781
19befb2d
LP
2782_public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
2783 sd_bus_slot *s;
992c052c 2784 struct node *n;
19befb2d 2785 int r;
992c052c 2786
dfa92725 2787 assert_return(bus, -EINVAL);
45b1f410 2788 assert_return(bus = bus_resolve(bus), -ENOPKG);
dfa92725
LP
2789 assert_return(object_path_is_valid(path), -EINVAL);
2790 assert_return(!bus_pid_changed(bus), -ECHILD);
992c052c
LP
2791
2792 n = bus_node_allocate(bus, path);
2793 if (!n)
2794 return -ENOMEM;
2795
19befb2d
LP
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 }
992c052c 2801
19befb2d
LP
2802 s->node_object_manager.node = n;
2803 LIST_PREPEND(object_managers, n->object_managers, &s->node_object_manager);
2804 bus->nodes_modified = true;
992c052c 2805
19befb2d
LP
2806 if (slot)
2807 *slot = s;
992c052c 2808
19befb2d 2809 return 0;
992c052c 2810
19befb2d
LP
2811fail:
2812 sd_bus_slot_unref(s);
992c052c 2813 bus_node_gc(bus, n);
dfa92725 2814
19befb2d 2815 return r;
992c052c 2816}