]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bus-util: add bus_message_append_string_set() helper
authorLennart Poettering <lennart@poettering.net>
Fri, 9 Jun 2023 17:12:51 +0000 (19:12 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 12 Jun 2023 09:34:26 +0000 (11:34 +0200)
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.

src/core/dbus-cgroup.c
src/core/dbus-execute.c
src/resolve/resolved-bus.c
src/resolve/resolved-link-bus.c
src/shared/bus-util.c
src/shared/bus-util.h

index 726035b00a3aebf25eb554a88961cda785aa9066..59d7a3ba71fe0a0896921162db930d5fee46adf9 100644 (file)
@@ -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;
 
index 57e9eb825464f89ff0ae02d50543ee3febce0327..5581d2277aafc717c8126a72f976c64946ce03cc 100644 (file)
@@ -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(
index 12d0d28babb9a896c0cc6b18fc771f22833a4e63..3420ebe8c0173175607371c6c1f1ca38cabd047c 100644 (file)
@@ -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);
index 9b6d14f20cc92d197566d48cfcfa386e62fedb00..06a4bd4291cd57383858070589bcf65689b90bd4 100644 (file)
@@ -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) {
index 4eb1b1c316194bda1daa3e715f9687da3c304d17..4a48200f7fc6a0e9775879665e1778bfdd445dc5 100644 (file)
@@ -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);
+}
index 81f5d0f15221a97696e76f96f1dad70401dcd4fd..7acd105cbbc6c6a29fc75523ebc766bd328e2f67 100644 (file)
@@ -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);