diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-11-26 02:06:05 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-11-26 03:28:27 +0100 |
commit | 2ea465ef6246b200f94645d42322c914d5e13082 (patch) | |
tree | 207b242e316b32e737bd05330385cbed7eddf270 /src/libsystemd | |
parent | sd-netlink: fix segfault (diff) | |
download | systemd-2ea465ef6246b200f94645d42322c914d5e13082.tar.xz systemd-2ea465ef6246b200f94645d42322c914d5e13082.zip |
sd-netlink: introduce netlink_queue_received_message() and friend
No functional change, just refactoring.
Diffstat (limited to 'src/libsystemd')
-rw-r--r-- | src/libsystemd/sd-netlink/netlink-internal.h | 2 | ||||
-rw-r--r-- | src/libsystemd/sd-netlink/netlink-socket.c | 55 | ||||
-rw-r--r-- | src/libsystemd/sd-netlink/sd-netlink.c | 28 |
3 files changed, 41 insertions, 44 deletions
diff --git a/src/libsystemd/sd-netlink/netlink-internal.h b/src/libsystemd/sd-netlink/netlink-internal.h index 514f22511c..964f7c0016 100644 --- a/src/libsystemd/sd-netlink/netlink-internal.h +++ b/src/libsystemd/sd-netlink/netlink-internal.h @@ -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); diff --git a/src/libsystemd/sd-netlink/netlink-socket.c b/src/libsystemd/sd-netlink/netlink-socket.c index fcf971b8f6..1480525c73 100644 --- a/src/libsystemd/sd-netlink/netlink-socket.c +++ b/src/libsystemd/sd-netlink/netlink-socket.c @@ -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; } diff --git a/src/libsystemd/sd-netlink/sd-netlink.c b/src/libsystemd/sd-netlink/sd-netlink.c index ec1035fdd1..7818a179c8 100644 --- a/src/libsystemd/sd-netlink/sd-netlink.c +++ b/src/libsystemd/sd-netlink/sd-netlink.c @@ -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; |