]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-netlink: introduce netlink_queue_received_message() and friend
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 26 Nov 2022 01:06:05 +0000 (10:06 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 26 Nov 2022 02:28:27 +0000 (11:28 +0900)
No functional change, just refactoring.

src/libsystemd/sd-netlink/netlink-internal.h
src/libsystemd/sd-netlink/netlink-socket.c
src/libsystemd/sd-netlink/sd-netlink.c

index 514f22511c342dab8109cd7627736d5b0ca04a8b..964f7c0016b02888c79f100d06c200bb44a48d41 100644 (file)
@@ -148,8 +148,6 @@ void message_seal(sd_netlink_message *m);
 
 int netlink_open_family(sd_netlink **ret, int family);
 bool netlink_pid_changed(sd_netlink *nl);
-int netlink_rqueue_make_room(sd_netlink *nl);
-int netlink_rqueue_partial_make_room(sd_netlink *nl);
 
 int socket_bind(sd_netlink *nl);
 int socket_broadcast_group_ref(sd_netlink *nl, unsigned group);
index fcf971b8f67cd28e65599a1118d59e3b2ee17ed7..1480525c73f826d62b543ce390594307947ab06e 100644 (file)
@@ -241,6 +241,39 @@ static int socket_recv_message(int fd, void *buf, size_t buf_size, uint32_t *ret
         return (int) n;
 }
 
+static int netlink_queue_received_message(sd_netlink *nl, sd_netlink_message *m) {
+        assert(nl);
+        assert(m);
+
+        if (nl->rqueue_size >= NETLINK_RQUEUE_MAX)
+                return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
+                                       "sd-netlink: exhausted the read queue size (%d)",
+                                       NETLINK_RQUEUE_MAX);
+
+        if (!GREEDY_REALLOC(nl->rqueue, nl->rqueue_size + 1))
+                return -ENOMEM;
+
+        nl->rqueue[nl->rqueue_size++] = sd_netlink_message_ref(m);
+        return 0;
+}
+
+static int netlink_queue_partially_received_message(sd_netlink *nl, sd_netlink_message *m) {
+        assert(nl);
+        assert(m);
+        assert(m->hdr->nlmsg_flags & NLM_F_MULTI);
+
+        if (nl->rqueue_partial_size >= NETLINK_RQUEUE_MAX)
+                return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
+                                       "sd-netlink: exhausted the partial read queue size (%d)",
+                                       NETLINK_RQUEUE_MAX);
+
+        if (!GREEDY_REALLOC(nl->rqueue_partial, nl->rqueue_partial_size + 1))
+                return -ENOMEM;
+
+        nl->rqueue_partial[nl->rqueue_partial_size++] = sd_netlink_message_ref(m);
+        return 0;
+}
+
 static sd_netlink_message *netlink_take_partial_message(sd_netlink *nl, uint32_t seqnum) {
         assert(nl);
 
@@ -362,21 +395,15 @@ int socket_read_message(sd_netlink *nl) {
         if (!first)
                 return 0;
 
-        if (!multi_part || done) {
+        done = done || !multi_part;
+        if (done)
                 /* we got a complete message, push it on the read queue */
-                r = netlink_rqueue_make_room(nl);
-                if (r < 0)
-                        return r;
-
-                nl->rqueue[nl->rqueue_size++] = TAKE_PTR(first);
-                return 1;
-        } else {
+                r = netlink_queue_received_message(nl, first);
+        else
                 /* we only got a partial multi-part message, push it on the partial read queue. */
-                r = netlink_rqueue_partial_make_room(nl);
-                if (r < 0)
-                        return r;
+                r = netlink_queue_partially_received_message(nl, first);
+        if (r < 0)
+                return r;
 
-                nl->rqueue_partial[nl->rqueue_partial_size++] = TAKE_PTR(first);
-                return 0;
-        }
+        return done;
 }
index ec1035fdd10ab19595ab09e0bfd60420526ded2c..7818a179c8fea7952c80a2251049a3163925b54f 100644 (file)
@@ -175,34 +175,6 @@ int sd_netlink_send(
         return 1;
 }
 
-int netlink_rqueue_make_room(sd_netlink *nl) {
-        assert(nl);
-
-        if (nl->rqueue_size >= NETLINK_RQUEUE_MAX)
-                return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
-                                       "sd-netlink: exhausted the read queue size (%d)",
-                                       NETLINK_RQUEUE_MAX);
-
-        if (!GREEDY_REALLOC(nl->rqueue, nl->rqueue_size + 1))
-                return -ENOMEM;
-
-        return 0;
-}
-
-int netlink_rqueue_partial_make_room(sd_netlink *nl) {
-        assert(nl);
-
-        if (nl->rqueue_partial_size >= NETLINK_RQUEUE_MAX)
-                return log_debug_errno(SYNTHETIC_ERRNO(ENOBUFS),
-                                       "sd-netlink: exhausted the partial read queue size (%d)",
-                                       NETLINK_RQUEUE_MAX);
-
-        if (!GREEDY_REALLOC(nl->rqueue_partial, nl->rqueue_partial_size + 1))
-                return -ENOMEM;
-
-        return 0;
-}
-
 static int dispatch_rqueue(sd_netlink *nl, sd_netlink_message **message) {
         int r;