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