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