From 07636114b131741fe9e385cb86a721f32169e5ca Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 20 Mar 2018 00:37:00 +0900 Subject: bus-util: introduce bus_message_print_all_properties() Then, use it where applicable. --- src/shared/bus-util.c | 121 ++++++++++++++++++++++++++++++++------------------ src/shared/bus-util.h | 5 ++- 2 files changed, 83 insertions(+), 43 deletions(-) (limited to 'src/shared') diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 2336338102..8061af1865 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -656,15 +656,15 @@ int bus_connect_user_systemd(sd_bus **_bus) { printf("%s=" fmt "\n", name, __VA_ARGS__); \ } while (0) -int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) { +int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all) { char type; const char *contents; int r; assert(name); - assert(property); + assert(m); - r = sd_bus_message_peek_type(property, &type, &contents); + r = sd_bus_message_peek_type(m, &type, &contents); if (r < 0) return r; @@ -673,7 +673,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b case SD_BUS_TYPE_STRING: { const char *s; - r = sd_bus_message_read_basic(property, type, &s); + r = sd_bus_message_read_basic(m, type, &s); if (r < 0) return r; @@ -692,7 +692,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b case SD_BUS_TYPE_BOOLEAN: { int b; - r = sd_bus_message_read_basic(property, type, &b); + r = sd_bus_message_read_basic(m, type, &b); if (r < 0) return r; @@ -704,7 +704,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b case SD_BUS_TYPE_UINT64: { uint64_t u; - r = sd_bus_message_read_basic(property, type, &u); + r = sd_bus_message_read_basic(m, type, &u); if (r < 0) return r; @@ -781,7 +781,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b case SD_BUS_TYPE_INT64: { int64_t i; - r = sd_bus_message_read_basic(property, type, &i); + r = sd_bus_message_read_basic(m, type, &i); if (r < 0) return r; @@ -793,7 +793,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b case SD_BUS_TYPE_UINT32: { uint32_t u; - r = sd_bus_message_read_basic(property, type, &u); + r = sd_bus_message_read_basic(m, type, &u); if (r < 0) return r; @@ -818,7 +818,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b case SD_BUS_TYPE_INT32: { int32_t i; - r = sd_bus_message_read_basic(property, type, &i); + r = sd_bus_message_read_basic(m, type, &i); if (r < 0) return r; @@ -829,7 +829,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b case SD_BUS_TYPE_DOUBLE: { double d; - r = sd_bus_message_read_basic(property, type, &d); + r = sd_bus_message_read_basic(m, type, &d); if (r < 0) return r; @@ -842,11 +842,11 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b bool first = true; const char *str; - r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents); + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents); if (r < 0) return r; - while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) { + while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) { bool good; if (first && !value) @@ -868,7 +868,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b if (!first || all) puts(""); - r = sd_bus_message_exit_container(property); + r = sd_bus_message_exit_container(m); if (r < 0) return r; @@ -878,7 +878,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b const uint8_t *u; size_t n; - r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n); + r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n); if (r < 0) return r; @@ -900,7 +900,7 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b uint32_t *u; size_t n; - r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n); + r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n); if (r < 0) return r; @@ -925,81 +925,118 @@ int bus_print_property(const char *name, sd_bus_message *property, bool value, b return 0; } -int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) { - _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - int r; +int bus_message_print_all_properties( + sd_bus_message *m, + bus_message_print_t func, + char **filter, + bool value, + bool all, + Set **found_properties) { - assert(bus); - assert(path); + int r; - r = sd_bus_call_method(bus, - dest, - path, - "org.freedesktop.DBus.Properties", - "GetAll", - &error, - &reply, - "s", ""); - if (r < 0) - return r; + assert(m); - r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}"); + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); if (r < 0) return r; - while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { + while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { const char *name; const char *contents; - r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name); + r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name); if (r < 0) return r; + if (found_properties) { + r = set_ensure_allocated(found_properties, &string_hash_ops); + if (r < 0) + return log_oom(); + + r = set_put(*found_properties, name); + if (r < 0 && r != EEXIST) + return log_oom(); + } + if (!filter || strv_find(filter, name)) { - r = sd_bus_message_peek_type(reply, NULL, &contents); + r = sd_bus_message_peek_type(m, NULL, &contents); if (r < 0) return r; - r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents); + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); if (r < 0) return r; - r = bus_print_property(name, reply, value, all); + if (func) + r = func(name, m, value, all); + if (!func || r == 0) + r = bus_print_property(name, m, value, all); if (r < 0) return r; if (r == 0) { if (all) printf("%s=[unprintable]\n", name); /* skip what we didn't read */ - r = sd_bus_message_skip(reply, contents); + r = sd_bus_message_skip(m, contents); if (r < 0) return r; } - r = sd_bus_message_exit_container(reply); + r = sd_bus_message_exit_container(m); if (r < 0) return r; } else { - r = sd_bus_message_skip(reply, "v"); + r = sd_bus_message_skip(m, "v"); if (r < 0) return r; } - r = sd_bus_message_exit_container(reply); + r = sd_bus_message_exit_container(m); if (r < 0) return r; } if (r < 0) return r; - r = sd_bus_message_exit_container(reply); + r = sd_bus_message_exit_container(m); if (r < 0) return r; return 0; } +int bus_print_all_properties( + sd_bus *bus, + const char *dest, + const char *path, + bus_message_print_t func, + char **filter, + bool value, + bool all, + Set **found_properties) { + + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + int r; + + assert(bus); + assert(path); + + r = sd_bus_call_method(bus, + dest, + path, + "org.freedesktop.DBus.Properties", + "GetAll", + &error, + &reply, + "s", ""); + if (r < 0) + return r; + + return bus_message_print_all_properties(reply, func, filter, value, all, found_properties); +} + int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { sd_id128_t *p = userdata; const void *v; diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 5e1fe893f9..e42198ff1c 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -76,8 +76,11 @@ int bus_connect_user_systemd(sd_bus **_bus); int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **bus); int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus); +typedef int (*bus_message_print_t) (const char *name, sd_bus_message *m, bool value, bool all); + int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all); -int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all); +int bus_message_print_all_properties(sd_bus_message *m, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties); +int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties); int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); int bus_property_set_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error); -- cgit v1.2.1