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