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