dryang / rpms / systemd

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