daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
a8c905
From 3c31ef05ba732e3ab5d23761c5f84768ca8de68e Mon Sep 17 00:00:00 2001
a8c905
From: Yu Watanabe <watanabe.yu+github@gmail.com>
a8c905
Date: Sun, 15 Dec 2019 21:32:25 +0900
a8c905
Subject: [PATCH] sd-netlink: introduce sd_netlink_message_read_strv()
a8c905
a8c905
The combination of sd_netlink_message_enter_container() and
a8c905
sd_netlink_message_read_string() only reads the last element if the attribute is
a8c905
duplicated, such a situation easily happens for IFLA_ALT_IFNAME.
a8c905
The function introduced here reads all matched attributes.
a8c905
a8c905
(cherry picked from commit 8f3c1859669230c2c8458675f41de13e369b47e7)
a8c905
a8c905
Related: #1850986
a8c905
---
a8c905
 src/libsystemd/sd-netlink/netlink-message.c | 58 +++++++++++++++++++++
a8c905
 src/systemd/sd-netlink.h                    |  1 +
a8c905
 2 files changed, 59 insertions(+)
a8c905
a8c905
diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c
a8c905
index db9101c163..5723e1d21c 100644
a8c905
--- a/src/libsystemd/sd-netlink/netlink-message.c
a8c905
+++ b/src/libsystemd/sd-netlink/netlink-message.c
a8c905
@@ -14,6 +14,7 @@
a8c905
 #include "netlink-util.h"
a8c905
 #include "refcnt.h"
a8c905
 #include "socket-util.h"
a8c905
+#include "strv.h"
a8c905
 #include "util.h"
a8c905
 
a8c905
 #define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->containers[i].offset) : NULL)
a8c905
@@ -754,6 +755,63 @@ int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type,
a8c905
         return 0;
a8c905
 }
a8c905
 
a8c905
+int sd_netlink_message_read_strv(sd_netlink_message *m, unsigned short container_type, unsigned short type_id, char ***ret) {
a8c905
+        _cleanup_strv_free_ char **s = NULL;
a8c905
+        const NLTypeSystem *type_system;
a8c905
+        const NLType *nl_type;
a8c905
+        struct rtattr *rta;
a8c905
+        void *container;
a8c905
+        unsigned short rt_len;
a8c905
+        int r;
a8c905
+
a8c905
+        assert_return(m, -EINVAL);
a8c905
+        assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -EINVAL);
a8c905
+
a8c905
+        r = type_system_get_type(m->containers[m->n_containers].type_system,
a8c905
+                                 &nl_type,
a8c905
+                                 container_type);
a8c905
+        if (r < 0)
a8c905
+                return r;
a8c905
+
a8c905
+        if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
a8c905
+                return -EINVAL;
a8c905
+
a8c905
+        r = type_system_get_type_system(m->containers[m->n_containers].type_system,
a8c905
+                                        &type_system,
a8c905
+                                        container_type);
a8c905
+        if (r < 0)
a8c905
+                return r;
a8c905
+
a8c905
+        r = type_system_get_type(type_system, &nl_type, type_id);
a8c905
+        if (r < 0)
a8c905
+                return r;
a8c905
+
a8c905
+        if (type_get_type(nl_type) != NETLINK_TYPE_STRING)
a8c905
+                return -EINVAL;
a8c905
+
a8c905
+        r = netlink_message_read_internal(m, container_type, &container, NULL);
a8c905
+        if (r < 0)
a8c905
+                return r;
a8c905
+
a8c905
+        rt_len = (unsigned short) r;
a8c905
+        rta = container;
a8c905
+
a8c905
+        for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
a8c905
+                unsigned short type;
a8c905
+
a8c905
+                type = RTA_TYPE(rta);
a8c905
+                if (type != type_id)
a8c905
+                        continue;
a8c905
+
a8c905
+                r = strv_extend(&s, RTA_DATA(rta));
a8c905
+                if (r < 0)
a8c905
+                        return r;
a8c905
+        }
a8c905
+
a8c905
+        *ret = TAKE_PTR(s);
a8c905
+        return 0;
a8c905
+}
a8c905
+
a8c905
 static int netlink_container_parse(sd_netlink_message *m,
a8c905
                                    struct netlink_container *container,
a8c905
                                    int count,
a8c905
diff --git a/src/systemd/sd-netlink.h b/src/systemd/sd-netlink.h
a8c905
index 51f0fa16b4..1f5c093f11 100644
a8c905
--- a/src/systemd/sd-netlink.h
a8c905
+++ b/src/systemd/sd-netlink.h
a8c905
@@ -82,6 +82,7 @@ int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned shor
a8c905
 int sd_netlink_message_close_container(sd_netlink_message *m);
a8c905
 
a8c905
 int sd_netlink_message_read_string(sd_netlink_message *m, unsigned short type, const char **data);
a8c905
+int sd_netlink_message_read_strv(sd_netlink_message *m, unsigned short container_type, unsigned short type_id, char ***ret);
a8c905
 int sd_netlink_message_read_u8(sd_netlink_message *m, unsigned short type, uint8_t *data);
a8c905
 int sd_netlink_message_read_u16(sd_netlink_message *m, unsigned short type, uint16_t *data);
a8c905
 int sd_netlink_message_read_u32(sd_netlink_message *m, unsigned short type, uint32_t *data);