dryang / rpms / systemd

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