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