X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fbus-objects.c;h=a6e8b2de867a38d5ea1e4a67026585a08bd83676;hb=718db96199e;hp=31d761e2154a98b18ef3e7432fb57b5481f15bc6;hpb=250a918dc4c8a15d927deecc3b3f6a0604657ae4;p=elogind.git diff --git a/src/libsystemd-bus/bus-objects.c b/src/libsystemd-bus/bus-objects.c index 31d761e21..a6e8b2de8 100644 --- a/src/libsystemd-bus/bus-objects.c +++ b/src/libsystemd-bus/bus-objects.c @@ -114,12 +114,17 @@ static int add_enumerated_to_set( continue; } - if (!object_path_is_valid(*k) && object_path_startswith(*k, prefix)) { + if (!object_path_is_valid(*k)){ free(*k); r = -EINVAL; continue; } + if (!object_path_startswith(*k, prefix)) { + free(*k); + continue; + } + r = set_consume(s, *k); } @@ -154,6 +159,9 @@ static int add_subtree_to_set( LIST_FOREACH(siblings, i, n->child) { char *t; + if (!object_path_startswith(i->path, prefix)) + continue; + t = strdup(i->path); if (!t) return -ENOMEM; @@ -312,7 +320,6 @@ static int invoke_property_get( sd_bus_error *error, void *userdata) { - int r; const void *p; assert(bus); @@ -327,6 +334,9 @@ static int invoke_property_get( /* Automatic handling if no callback is defined. */ + if (streq(v->x.property.signature, "as")) + return sd_bus_message_append_strv(m, *(char***) userdata); + assert(signature_is_single(v->x.property.signature, false)); assert(bus_type_is_basic(v->x.property.signature[0])); @@ -347,11 +357,7 @@ static int invoke_property_get( break; } - r = sd_bus_message_append_basic(m, v->x.property.signature[0], p); - if (r < 0) - return r; - - return 1; + return sd_bus_message_append_basic(m, v->x.property.signature[0], p); } static int invoke_property_set( @@ -585,7 +591,7 @@ static int property_get_all_callbacks_run( _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; struct node_vtable *c; - bool found_interface = false; + bool found_interface; int r; assert(bus); @@ -600,6 +606,11 @@ static int property_get_all_callbacks_run( if (r < 0) return r; + found_interface = !iface || + streq(iface, "org.freedesktop.DBus.Properties") || + streq(iface, "org.freedesktop.DBus.Peer") || + streq(iface, "org.freedesktop.DBus.Introspectable"); + LIST_FOREACH(vtables, c, first) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; void *u; @@ -717,6 +728,7 @@ static int process_introspect( _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_set_free_free_ Set *s = NULL; + const char *previous_interface = NULL; struct introspect intro; struct node_vtable *c; bool empty; @@ -757,11 +769,24 @@ static int process_introspect( empty = false; - r = introspect_write_interface(&intro, c->interface, c->vtable); + if (!streq_ptr(previous_interface, c->interface)) { + + if (previous_interface) + fputs(" \n", intro.f); + + fprintf(intro.f, " \n", c->interface); + } + + r = introspect_write_interface(&intro, c->vtable); if (r < 0) goto finish; + + previous_interface = c->interface; } + if (previous_interface) + fputs(" \n", intro.f); + if (empty) { /* Nothing?, let's see if we exist at all, and if not * refuse to do anything */ @@ -795,51 +820,6 @@ finish: return r; } -static int object_manager_serialize_vtable( - sd_bus *bus, - sd_bus_message *reply, - const char *path, - struct node_vtable *c, - sd_bus_error *error, - void *userdata) { - - int r; - - assert(bus); - assert(reply); - assert(path); - assert(c); - assert(error); - - r = sd_bus_message_open_container(reply, 'e', "sa{sv}"); - if (r < 0) - return r; - - r = sd_bus_message_append(reply, "s", c->interface); - if (r < 0) - return r; - - r = sd_bus_message_open_container(reply, 'a', "{sv}"); - if (r < 0) - return r; - - r = vtable_append_all_properties(bus, reply, path, c, userdata, error); - if (r < 0) - return r; - if (bus->nodes_modified) - return 0; - - r = sd_bus_message_close_container(reply); - if (r < 0) - return r; - - r = sd_bus_message_close_container(reply); - if (r < 0) - return r; - - return 0; -} - static int object_manager_serialize_path( sd_bus *bus, sd_bus_message *reply, @@ -848,9 +828,10 @@ static int object_manager_serialize_path( bool require_fallback, sd_bus_error *error) { + const char *previous_interface = NULL; + bool found_something = false; struct node_vtable *i; struct node *n; - bool found_something = false; int r; assert(bus); @@ -878,6 +859,9 @@ static int object_manager_serialize_path( continue; if (!found_something) { + + /* Open the object part */ + r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}"); if (r < 0) return r; @@ -893,13 +877,54 @@ static int object_manager_serialize_path( found_something = true; } - r = object_manager_serialize_vtable(bus, reply, path, i, error, u); + if (!streq_ptr(previous_interface, i->interface)) { + + /* Maybe close the previous interface part */ + + if (previous_interface) { + r = sd_bus_message_close_container(reply); + if (r < 0) + return r; + + r = sd_bus_message_close_container(reply); + if (r < 0) + return r; + } + + /* Open the new interface part */ + + r = sd_bus_message_open_container(reply, 'e', "sa{sv}"); + if (r < 0) + return r; + + r = sd_bus_message_append(reply, "s", i->interface); + if (r < 0) + return r; + + r = sd_bus_message_open_container(reply, 'a', "{sv}"); + if (r < 0) + return r; + } + + r = vtable_append_all_properties(bus, reply, path, i, u, error); if (r < 0) return r; if (sd_bus_error_is_set(error)) return 0; if (bus->nodes_modified) return 0; + + previous_interface = i->interface; + } + + if (previous_interface) { + r = sd_bus_message_close_container(reply); + if (r < 0) + return r; + + r = sd_bus_message_close_container(reply); + if (r < 0) + return r; } if (found_something) { @@ -1378,19 +1403,35 @@ static int bus_remove_object( return 1; } -int sd_bus_add_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) { +_public_ int sd_bus_add_object(sd_bus *bus, + const char *path, + sd_bus_message_handler_t callback, + void *userdata) { + return bus_add_object(bus, false, path, callback, userdata); } -int sd_bus_remove_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) { +_public_ int sd_bus_remove_object(sd_bus *bus, + const char *path, + sd_bus_message_handler_t callback, + void *userdata) { + return bus_remove_object(bus, false, path, callback, userdata); } -int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) { +_public_ int sd_bus_add_fallback(sd_bus *bus, + const char *prefix, + sd_bus_message_handler_t callback, + void *userdata) { + return bus_add_object(bus, true, prefix, callback, userdata); } -int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) { +_public_ int sd_bus_remove_fallback(sd_bus *bus, + const char *prefix, + sd_bus_message_handler_t callback, + void *userdata) { + return bus_remove_object(bus, true, prefix, callback, userdata); } @@ -1476,7 +1517,7 @@ static int add_object_vtable_internal( sd_bus_object_find_t find, void *userdata) { - struct node_vtable *c = NULL, *i; + struct node_vtable *c = NULL, *i, *existing = NULL; const sd_bus_vtable *v; struct node *n; int r; @@ -1488,6 +1529,10 @@ static int add_object_vtable_internal( assert_return(vtable[0].type == _SD_BUS_VTABLE_START, -EINVAL); assert_return(vtable[0].x.start.element_size == sizeof(struct sd_bus_vtable), -EINVAL); assert_return(!bus_pid_changed(bus), -ECHILD); + assert_return(!streq(interface, "org.freedesktop.DBus.Properties") && + !streq(interface, "org.freedesktop.DBus.Introspectable") && + !streq(interface, "org.freedesktop.DBus.Peer") && + !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL); r = hashmap_ensure_allocated(&bus->vtable_methods, vtable_member_hash_func, vtable_member_compare_func); if (r < 0) @@ -1502,15 +1547,20 @@ static int add_object_vtable_internal( return -ENOMEM; LIST_FOREACH(vtables, i, n->vtables) { - if (streq(i->interface, interface)) { - r = -EEXIST; - goto fail; - } - if (i->is_fallback != fallback) { r = -EPROTOTYPE; goto fail; } + + if (streq(i->interface, interface)) { + + if (i->vtable == vtable) { + r = -EEXIST; + goto fail; + } + + existing = i; + } } c = new0(struct node_vtable, 1); @@ -1582,7 +1632,7 @@ static int add_object_vtable_internal( if (!member_name_is_valid(v->x.property.member) || !signature_is_single(v->x.property.signature, false) || - !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0])) || + !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) || v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY || (v->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY && !(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))) { r = -EINVAL; @@ -1614,7 +1664,7 @@ static int add_object_vtable_internal( case _SD_BUS_VTABLE_SIGNAL: if (!member_name_is_valid(v->x.signal.member) || - !signature_is_single(strempty(v->x.signal.signature), false)) { + !signature_is_valid(strempty(v->x.signal.signature), false)) { r = -EINVAL; goto fail; } @@ -1627,7 +1677,7 @@ static int add_object_vtable_internal( } } - LIST_PREPEND(vtables, n->vtables, c); + LIST_INSERT_AFTER(vtables, n->vtables, existing, c); bus->nodes_modified = true; return 0; @@ -1644,7 +1694,10 @@ static int remove_object_vtable_internal( sd_bus *bus, const char *path, const char *interface, - bool fallback) { + const sd_bus_vtable *vtable, + bool fallback, + sd_bus_object_find_t find, + void *userdata) { struct node_vtable *c; struct node *n; @@ -1659,7 +1712,11 @@ static int remove_object_vtable_internal( return 0; LIST_FOREACH(vtables, c, n->vtables) - if (streq(c->interface, interface) && c->is_fallback == fallback) + if (streq(c->interface, interface) && + c->is_fallback == fallback && + c->vtable == vtable && + c->find == find && + c->userdata == userdata) break; if (!c) @@ -1675,7 +1732,7 @@ static int remove_object_vtable_internal( return 1; } -int sd_bus_add_object_vtable( +_public_ int sd_bus_add_object_vtable( sd_bus *bus, const char *path, const char *interface, @@ -1685,15 +1742,17 @@ int sd_bus_add_object_vtable( return add_object_vtable_internal(bus, path, interface, vtable, false, NULL, userdata); } -int sd_bus_remove_object_vtable( +_public_ int sd_bus_remove_object_vtable( sd_bus *bus, const char *path, - const char *interface) { + const char *interface, + const sd_bus_vtable *vtable, + void *userdata) { - return remove_object_vtable_internal(bus, path, interface, false); + return remove_object_vtable_internal(bus, path, interface, vtable, false, NULL, userdata); } -int sd_bus_add_fallback_vtable( +_public_ int sd_bus_add_fallback_vtable( sd_bus *bus, const char *path, const char *interface, @@ -1704,15 +1763,18 @@ int sd_bus_add_fallback_vtable( return add_object_vtable_internal(bus, path, interface, vtable, true, find, userdata); } -int sd_bus_remove_fallback_vtable( +_public_ int sd_bus_remove_fallback_vtable( sd_bus *bus, const char *path, - const char *interface) { + const char *interface, + const sd_bus_vtable *vtable, + sd_bus_object_find_t find, + void *userdata) { - return remove_object_vtable_internal(bus, path, interface, true); + return remove_object_vtable_internal(bus, path, interface, vtable, true, find, userdata); } -int sd_bus_add_node_enumerator( +_public_ int sd_bus_add_node_enumerator( sd_bus *bus, const char *path, sd_bus_node_enumerator_t callback, @@ -1753,7 +1815,7 @@ fail: return r; } -int sd_bus_remove_node_enumerator( +_public_ int sd_bus_remove_node_enumerator( sd_bus *bus, const char *path, sd_bus_node_enumerator_t callback, @@ -1797,8 +1859,8 @@ static int emit_properties_changed_on_interface( char **names) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; - bool has_invalidating = false; - struct vtable_member key; + bool has_invalidating = false, has_changing = false; + struct vtable_member key = {}; struct node_vtable *c; struct node *n; char **property; @@ -1814,23 +1876,6 @@ static int emit_properties_changed_on_interface( if (!n) return 0; - LIST_FOREACH(vtables, c, n->vtables) { - if (require_fallback && !c->is_fallback) - continue; - - if (streq(c->interface, interface)) - break; - } - - if (!c) - return 0; - - r = node_vtable_get_userdata(bus, path, c, &u); - if (r <= 0) - return r; - if (bus->nodes_modified) - return 0; - r = sd_bus_message_new_signal(bus, path, "org.freedesktop.DBus.Properties", "PropertiesChanged", &m); if (r < 0) return r; @@ -1846,52 +1891,78 @@ static int emit_properties_changed_on_interface( key.path = prefix; key.interface = interface; - STRV_FOREACH(property, names) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - struct vtable_member *v; - - assert_return(member_name_is_valid(*property), -EINVAL); - - key.member = *property; - v = hashmap_get(bus->vtable_properties, &key); - if (!v) - return -ENOENT; - - assert(c == v->parent); - assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE, -EDOM); + LIST_FOREACH(vtables, c, n->vtables) { + if (require_fallback && !c->is_fallback) + continue; - if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY) { - has_invalidating = true; + if (!streq(c->interface, interface)) continue; - } - r = sd_bus_message_open_container(m, 'e', "sv"); + r = node_vtable_get_userdata(bus, path, c, &u); if (r < 0) return r; + if (bus->nodes_modified) + return 0; + if (r == 0) + continue; - r = sd_bus_message_append(m, "s", *property); - if (r < 0) - return r; + STRV_FOREACH(property, names) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + struct vtable_member *v; - r = sd_bus_message_open_container(m, 'v', v->vtable->x.property.signature); - if (r < 0) - return r; + assert_return(member_name_is_valid(*property), -EINVAL); - r = invoke_property_get(bus, v->vtable, m->path, interface, *property, m, &error, vtable_property_convert_userdata(v->vtable, u)); - if (r < 0) - return r; - if (bus->nodes_modified) - return 0; + key.member = *property; + v = hashmap_get(bus->vtable_properties, &key); + if (!v) + return -ENOENT; + + /* If there are two vtables for the same + * interface, let's handle this property when + * we come to that vtable. */ + if (c != v->parent) + continue; - r = sd_bus_message_close_container(m); - if (r < 0) - return r; + assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE, -EDOM); - r = sd_bus_message_close_container(m); - if (r < 0) - return r; + if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY) { + has_invalidating = true; + continue; + } + + has_changing = true; + + r = sd_bus_message_open_container(m, 'e', "sv"); + if (r < 0) + return r; + + r = sd_bus_message_append(m, "s", *property); + if (r < 0) + return r; + + r = sd_bus_message_open_container(m, 'v', v->vtable->x.property.signature); + if (r < 0) + return r; + + r = invoke_property_get(bus, v->vtable, m->path, interface, *property, m, &error, vtable_property_convert_userdata(v->vtable, u)); + if (r < 0) + return r; + if (bus->nodes_modified) + return 0; + + r = sd_bus_message_close_container(m); + if (r < 0) + return r; + + r = sd_bus_message_close_container(m); + if (r < 0) + return r; + } } + if (!has_invalidating && !has_changing) + return 0; + r = sd_bus_message_close_container(m); if (r < 0) return r; @@ -1901,19 +1972,35 @@ static int emit_properties_changed_on_interface( return r; if (has_invalidating) { - STRV_FOREACH(property, names) { - struct vtable_member *v; - - key.member = *property; - assert_se(v = hashmap_get(bus->vtable_properties, &key)); - assert(c == v->parent); + LIST_FOREACH(vtables, c, n->vtables) { + if (require_fallback && !c->is_fallback) + continue; - if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY)) + if (!streq(c->interface, interface)) continue; - r = sd_bus_message_append(m, "s", *property); + r = node_vtable_get_userdata(bus, path, c, &u); if (r < 0) return r; + if (bus->nodes_modified) + return 0; + if (r == 0) + continue; + + STRV_FOREACH(property, names) { + struct vtable_member *v; + + key.member = *property; + assert_se(v = hashmap_get(bus->vtable_properties, &key)); + assert(c == v->parent); + + if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY)) + continue; + + r = sd_bus_message_append(m, "s", *property); + if (r < 0) + return r; + } } } @@ -1928,7 +2015,7 @@ static int emit_properties_changed_on_interface( return 1; } -int sd_bus_emit_properties_changed_strv( +_public_ int sd_bus_emit_properties_changed_strv( sd_bus *bus, const char *path, const char *interface, @@ -1970,7 +2057,7 @@ int sd_bus_emit_properties_changed_strv( return -ENOENT; } -int sd_bus_emit_properties_changed( +_public_ int sd_bus_emit_properties_changed( sd_bus *bus, const char *path, const char *interface, @@ -2001,6 +2088,7 @@ static int interfaces_added_append_one_prefix( bool require_fallback) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + bool found_interface = false; struct node_vtable *c; struct node *n; void *u = NULL; @@ -2020,38 +2108,43 @@ static int interfaces_added_append_one_prefix( if (require_fallback && !c->is_fallback) continue; - if (streq(c->interface, interface)) - break; - } + if (!streq(c->interface, interface)) + continue; - if (!c) - return 0; + r = node_vtable_get_userdata(bus, path, c, &u); + if (r < 0) + return r; + if (bus->nodes_modified) + return 0; + if (r == 0) + continue; - r = node_vtable_get_userdata(bus, path, c, &u); - if (r <= 0) - return r; - if (bus->nodes_modified) - return 0; + if (!found_interface) { + r = sd_bus_message_append_basic(m, 's', interface); + if (r < 0) + return r; - r = sd_bus_message_append_basic(m, 's', interface); - if (r < 0) - return r; + r = sd_bus_message_open_container(m, 'a', "{sv}"); + if (r < 0) + return r; - r = sd_bus_message_open_container(m, 'a', "{sv}"); - if (r < 0) - return r; + found_interface = true; + } - r = vtable_append_all_properties(bus, m,path, c, u, &error); - if (r < 0) - return r; - if (bus->nodes_modified) - return 0; + r = vtable_append_all_properties(bus, m, path, c, u, &error); + if (r < 0) + return r; + if (bus->nodes_modified) + return 0; + } - r = sd_bus_message_close_container(m); - if (r < 0) - return r; + if (found_interface) { + r = sd_bus_message_close_container(m); + if (r < 0) + return r; + } - return 1; + return found_interface; } static int interfaces_added_append_one( @@ -2086,7 +2179,7 @@ static int interfaces_added_append_one( return -ENOENT; } -int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) { +_public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) { BUS_DONT_DESTROY(bus); _cleanup_bus_message_unref_ sd_bus_message *m = NULL; @@ -2150,7 +2243,7 @@ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **inte return sd_bus_send(bus, m, NULL); } -int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) { +_public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) { char **interfaces; assert_return(bus, -EINVAL); @@ -2163,7 +2256,7 @@ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *inte return sd_bus_emit_interfaces_added_strv(bus, path, interfaces); } -int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) { +_public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; int r; @@ -2190,7 +2283,7 @@ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **in return sd_bus_send(bus, m, NULL); } -int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) { +_public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) { char **interfaces; assert_return(bus, -EINVAL); @@ -2203,7 +2296,7 @@ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *in return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces); } -int sd_bus_add_object_manager(sd_bus *bus, const char *path) { +_public_ int sd_bus_add_object_manager(sd_bus *bus, const char *path) { struct node *n; assert_return(bus, -EINVAL); @@ -2219,7 +2312,7 @@ int sd_bus_add_object_manager(sd_bus *bus, const char *path) { return 0; } -int sd_bus_remove_object_manager(sd_bus *bus, const char *path) { +_public_ int sd_bus_remove_object_manager(sd_bus *bus, const char *path) { struct node *n; assert_return(bus, -EINVAL);