From: Lennart Poettering Date: Fri, 9 Jun 2023 17:12:51 +0000 (+0200) Subject: bus-util: add bus_message_append_string_set() helper X-Git-Tag: v254-rc1~232^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9298af8dd30a8c4fcae93540956dc8784ae44049;p=thirdparty%2Fsystemd.git bus-util: add bus_message_append_string_set() helper This new helper adds all strings from a Set object as a string array to a message. Various places where we have similar code are then ported over to this. --- diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index 726035b00a3..59d7a3ba71f 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -7,6 +7,7 @@ #include "bpf-firewall.h" #include "bpf-foreign.h" #include "bus-get-properties.h" +#include "bus-util.h" #include "cgroup-util.h" #include "cgroup.h" #include "core-varlink.h" @@ -400,9 +401,9 @@ static int property_get_restrict_network_interfaces( sd_bus_message *reply, void *userdata, sd_bus_error *error) { - int r; + CGroupContext *c = ASSERT_PTR(userdata); - char *iface; + int r; assert(bus); assert(reply); @@ -415,17 +416,7 @@ static int property_get_restrict_network_interfaces( if (r < 0) return r; - r = sd_bus_message_open_container(reply, 'a', "s"); - if (r < 0) - return r; - - SET_FOREACH(iface, c->restrict_network_interfaces) { - r = sd_bus_message_append(reply, "s", iface); - if (r < 0) - return r; - } - - r = sd_bus_message_close_container(reply); + r = bus_message_append_string_set(reply, c->restrict_network_interfaces); if (r < 0) return r; diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 57e9eb82546..5581d2277aa 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -10,6 +10,7 @@ #include "af-list.h" #include "alloc-util.h" #include "bus-get-properties.h" +#include "bus-util.h" #include "cap-list.h" #include "capability-util.h" #include "cpu-set-util.h" @@ -939,24 +940,12 @@ static int property_get_import_credential( sd_bus_error *error) { ExecContext *c = ASSERT_PTR(userdata); - const char *s; - int r; assert(bus); assert(property); assert(reply); - r = sd_bus_message_open_container(reply, 'a', "s"); - if (r < 0) - return r; - - SET_FOREACH(s, c->import_credentials) { - r = sd_bus_message_append(reply, "s", s); - if (r < 0) - return r; - } - - return sd_bus_message_close_container(reply); + return bus_message_append_string_set(reply, c->import_credentials); } static int property_get_root_hash( diff --git a/src/resolve/resolved-bus.c b/src/resolve/resolved-bus.c index 12d0d28babb..3420ebe8c01 100644 --- a/src/resolve/resolved-bus.c +++ b/src/resolve/resolved-bus.c @@ -1687,22 +1687,10 @@ static int bus_property_get_ntas( sd_bus_error *error) { Manager *m = ASSERT_PTR(userdata); - const char *domain; - int r; assert(reply); - r = sd_bus_message_open_container(reply, 'a', "s"); - if (r < 0) - return r; - - SET_FOREACH(domain, m->trust_anchor.negative_by_name) { - r = sd_bus_message_append(reply, "s", domain); - if (r < 0) - return r; - } - - return sd_bus_message_close_container(reply); + return bus_message_append_string_set(reply, m->trust_anchor.negative_by_name); } static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode); diff --git a/src/resolve/resolved-link-bus.c b/src/resolve/resolved-link-bus.c index 9b6d14f20cc..06a4bd4291c 100644 --- a/src/resolve/resolved-link-bus.c +++ b/src/resolve/resolved-link-bus.c @@ -217,22 +217,10 @@ static int property_get_ntas( sd_bus_error *error) { Link *l = ASSERT_PTR(userdata); - const char *name; - int r; assert(reply); - r = sd_bus_message_open_container(reply, 'a', "s"); - if (r < 0) - return r; - - SET_FOREACH(name, l->dnssec_negative_trust_anchors) { - r = sd_bus_message_append(reply, "s", name); - if (r < 0) - return r; - } - - return sd_bus_message_close_container(reply); + return bus_message_append_string_set(reply, l->dnssec_negative_trust_anchors); } static int verify_unmanaged_link(Link *l, sd_bus_error *error) { diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 4eb1b1c3161..4a48200f7fc 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -678,3 +678,22 @@ const struct hash_ops bus_message_hash_ops = { .compare = trivial_compare_func, .free_value = bus_message_unref_wrapper, }; + +int bus_message_append_string_set(sd_bus_message *m, Set *set) { + const char *s; + int r; + + assert(m); + + r = sd_bus_message_open_container(m, 'a', "s"); + if (r < 0) + return r; + + SET_FOREACH(s, set) { + r = sd_bus_message_append(m, "s", s); + if (r < 0) + return r; + } + + return sd_bus_message_close_container(m); +} diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 81f5d0f1522..7acd105cbbc 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -12,6 +12,7 @@ #include "errno-util.h" #include "macro.h" #include "runtime-scope.h" +#include "set.h" #include "string-util.h" #include "time-util.h" @@ -68,3 +69,5 @@ int bus_reply_pair_array(sd_bus_message *m, char **l); int bus_register_malloc_status(sd_bus *bus, const char *destination); extern const struct hash_ops bus_message_hash_ops; + +int bus_message_append_string_set(sd_bus_message *m, Set *s);