2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2015-06-13 21:10:39 +02:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-netlink.h"
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2018-02-09 17:53:28 +01:00
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2018-11-27 10:34:32 +01:00
|
|
|
#include "io-util.h"
|
2015-06-13 21:10:39 +02:00
|
|
|
#include "netlink-internal.h"
|
|
|
|
#include "netlink-types.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "netlink-util.h"
|
|
|
|
#include "socket-util.h"
|
|
|
|
#include "util.h"
|
2015-06-13 21:10:39 +02:00
|
|
|
|
2015-06-13 21:25:05 +02:00
|
|
|
int socket_open(int family) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, family);
|
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2018-02-09 17:53:28 +01:00
|
|
|
return fd_move_above_stdio(fd);
|
2015-06-13 21:25:05 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 17:59:10 +02:00
|
|
|
static int broadcast_groups_get(sd_netlink *nl) {
|
|
|
|
_cleanup_free_ uint32_t *groups = NULL;
|
|
|
|
socklen_t len = 0, old_len;
|
|
|
|
unsigned i, j;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(nl);
|
2016-01-10 15:36:03 +01:00
|
|
|
assert(nl->fd >= 0);
|
2015-10-15 17:59:10 +02:00
|
|
|
|
|
|
|
r = getsockopt(nl->fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &len);
|
|
|
|
if (r < 0) {
|
|
|
|
if (errno == ENOPROTOOPT) {
|
|
|
|
nl->broadcast_group_dont_leave = true;
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
groups = new0(uint32_t, len);
|
|
|
|
if (!groups)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
old_len = len;
|
|
|
|
|
|
|
|
r = getsockopt(nl->fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, groups, &len);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (old_len != len)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
r = hashmap_ensure_allocated(&nl->broadcast_group_refs, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2016-02-23 05:32:04 +01:00
|
|
|
for (j = 0; j < sizeof(uint32_t) * 8; j++) {
|
2015-10-15 17:59:10 +02:00
|
|
|
uint32_t offset;
|
|
|
|
unsigned group;
|
|
|
|
|
|
|
|
offset = 1U << j;
|
|
|
|
|
|
|
|
if (!(groups[i] & offset))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
group = i * sizeof(uint32_t) * 8 + j + 1;
|
|
|
|
|
|
|
|
r = hashmap_put(nl->broadcast_group_refs, UINT_TO_PTR(group), UINT_TO_PTR(1));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-13 21:25:05 +02:00
|
|
|
int socket_bind(sd_netlink *nl) {
|
|
|
|
socklen_t addrlen;
|
2018-10-15 19:21:37 +02:00
|
|
|
int r;
|
2015-06-13 21:25:05 +02:00
|
|
|
|
2018-10-18 19:48:18 +02:00
|
|
|
r = setsockopt_int(nl->fd, SOL_NETLINK, NETLINK_PKTINFO, true);
|
2015-06-13 21:25:05 +02:00
|
|
|
if (r < 0)
|
2018-10-18 19:48:18 +02:00
|
|
|
return r;
|
2015-06-13 21:25:05 +02:00
|
|
|
|
|
|
|
addrlen = sizeof(nl->sockaddr);
|
|
|
|
|
|
|
|
r = bind(nl->fd, &nl->sockaddr.sa, addrlen);
|
|
|
|
/* ignore EINVAL to allow opening an already bound socket */
|
|
|
|
if (r < 0 && errno != EINVAL)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
r = getsockname(nl->fd, &nl->sockaddr.sa, &addrlen);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2015-10-15 17:59:10 +02:00
|
|
|
r = broadcast_groups_get(nl);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-13 21:25:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-15 17:59:10 +02:00
|
|
|
static unsigned broadcast_group_get_ref(sd_netlink *nl, unsigned group) {
|
|
|
|
assert(nl);
|
|
|
|
|
|
|
|
return PTR_TO_UINT(hashmap_get(nl->broadcast_group_refs, UINT_TO_PTR(group)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int broadcast_group_set_ref(sd_netlink *nl, unsigned group, unsigned n_ref) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(nl);
|
|
|
|
|
|
|
|
r = hashmap_replace(nl->broadcast_group_refs, UINT_TO_PTR(group), UINT_TO_PTR(n_ref));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-13 21:25:05 +02:00
|
|
|
|
2015-10-15 17:59:10 +02:00
|
|
|
static int broadcast_group_join(sd_netlink *nl, unsigned group) {
|
2015-06-13 21:25:05 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(nl);
|
|
|
|
assert(nl->fd >= 0);
|
|
|
|
assert(group > 0);
|
|
|
|
|
|
|
|
r = setsockopt(nl->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-15 17:59:10 +02:00
|
|
|
int socket_broadcast_group_ref(sd_netlink *nl, unsigned group) {
|
|
|
|
unsigned n_ref;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(nl);
|
|
|
|
|
|
|
|
n_ref = broadcast_group_get_ref(nl, group);
|
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
n_ref++;
|
2015-10-15 17:59:10 +02:00
|
|
|
|
|
|
|
r = hashmap_ensure_allocated(&nl->broadcast_group_refs, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = broadcast_group_set_ref(nl, group, n_ref);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (n_ref > 1)
|
|
|
|
/* not yet in the group */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = broadcast_group_join(nl, group);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int broadcast_group_leave(sd_netlink *nl, unsigned group) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(nl);
|
|
|
|
assert(nl->fd >= 0);
|
|
|
|
assert(group > 0);
|
|
|
|
|
|
|
|
if (nl->broadcast_group_dont_leave)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = setsockopt(nl->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, &group, sizeof(group));
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int socket_broadcast_group_unref(sd_netlink *nl, unsigned group) {
|
|
|
|
unsigned n_ref;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(nl);
|
|
|
|
|
|
|
|
n_ref = broadcast_group_get_ref(nl, group);
|
|
|
|
|
|
|
|
assert(n_ref > 0);
|
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
n_ref--;
|
2015-10-15 17:59:10 +02:00
|
|
|
|
|
|
|
r = broadcast_group_set_ref(nl, group, n_ref);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (n_ref > 0)
|
|
|
|
/* still refs left */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = broadcast_group_leave(nl, group);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-13 21:10:39 +02:00
|
|
|
/* returns the number of bytes sent, or a negative error code */
|
|
|
|
int socket_write_message(sd_netlink *nl, sd_netlink_message *m) {
|
|
|
|
union {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_nl nl;
|
|
|
|
} addr = {
|
|
|
|
.nl.nl_family = AF_NETLINK,
|
|
|
|
};
|
|
|
|
ssize_t k;
|
|
|
|
|
|
|
|
assert(nl);
|
|
|
|
assert(m);
|
|
|
|
assert(m->hdr);
|
|
|
|
|
|
|
|
k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len,
|
|
|
|
0, &addr.sa, sizeof(addr));
|
|
|
|
if (k < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2020-12-18 05:11:06 +01:00
|
|
|
int socket_writev_message(sd_netlink *nl, sd_netlink_message **m, size_t msgcount) {
|
2020-06-19 14:58:41 +02:00
|
|
|
_cleanup_free_ struct iovec *iovs = NULL;
|
|
|
|
ssize_t k;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
assert(nl);
|
2020-12-18 05:12:09 +01:00
|
|
|
assert(m);
|
|
|
|
assert(msgcount > 0);
|
2020-06-19 14:58:41 +02:00
|
|
|
|
|
|
|
iovs = new0(struct iovec, msgcount);
|
|
|
|
if (!iovs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < msgcount; i++) {
|
|
|
|
assert(m[i]->hdr != NULL);
|
|
|
|
assert(m[i]->hdr->nlmsg_len > 0);
|
|
|
|
iovs[i] = IOVEC_MAKE(m[i]->hdr, m[i]->hdr->nlmsg_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
k = writev(nl->fd, iovs, msgcount);
|
|
|
|
if (k < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2020-04-17 11:52:05 +02:00
|
|
|
static int socket_recv_message(int fd, struct iovec *iov, uint32_t *ret_mcast_group, bool peek) {
|
2015-06-13 21:10:39 +02:00
|
|
|
union sockaddr_union sender;
|
2020-05-27 19:37:19 +02:00
|
|
|
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct nl_pktinfo))) control;
|
2015-06-13 21:10:39 +02:00
|
|
|
struct msghdr msg = {
|
|
|
|
.msg_iov = iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_name = &sender,
|
|
|
|
.msg_namelen = sizeof(sender),
|
2020-04-23 19:47:38 +02:00
|
|
|
.msg_control = &control,
|
|
|
|
.msg_controllen = sizeof(control),
|
2015-06-13 21:10:39 +02:00
|
|
|
};
|
2018-01-23 15:47:18 +01:00
|
|
|
ssize_t n;
|
2015-06-13 21:10:39 +02:00
|
|
|
|
|
|
|
assert(fd >= 0);
|
|
|
|
assert(iov);
|
|
|
|
|
2020-04-23 19:47:38 +02:00
|
|
|
n = recvmsg_safe(fd, &msg, MSG_TRUNC | (peek ? MSG_PEEK : 0));
|
|
|
|
if (n == -ENOBUFS)
|
|
|
|
return log_debug_errno(n, "rtnl: kernel receive buffer overrun");
|
|
|
|
if (IN_SET(n, -EAGAIN, -EINTR))
|
|
|
|
return 0;
|
|
|
|
if (n < 0)
|
|
|
|
return (int) n;
|
2015-06-13 21:10:39 +02:00
|
|
|
|
|
|
|
if (sender.nl.nl_pid != 0) {
|
|
|
|
/* not from the kernel, ignore */
|
|
|
|
log_debug("rtnl: ignoring message from portid %"PRIu32, sender.nl.nl_pid);
|
|
|
|
|
|
|
|
if (peek) {
|
|
|
|
/* drop the message */
|
2020-04-23 19:47:38 +02:00
|
|
|
n = recvmsg_safe(fd, &msg, 0);
|
2018-01-23 15:47:18 +01:00
|
|
|
if (n < 0)
|
2020-04-23 19:47:38 +02:00
|
|
|
return (int) n;
|
2015-06-13 21:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-17 11:52:05 +02:00
|
|
|
if (ret_mcast_group) {
|
2020-04-23 19:47:38 +02:00
|
|
|
struct nl_pktinfo *pi;
|
2015-06-13 21:10:39 +02:00
|
|
|
|
2020-04-23 19:47:38 +02:00
|
|
|
pi = CMSG_FIND_DATA(&msg, SOL_NETLINK, NETLINK_PKTINFO, struct nl_pktinfo);
|
|
|
|
if (pi)
|
|
|
|
*ret_mcast_group = pi->group;
|
2020-04-17 11:52:05 +02:00
|
|
|
else
|
|
|
|
*ret_mcast_group = 0;
|
2015-06-13 21:10:39 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 15:47:18 +01:00
|
|
|
return (int) n;
|
2015-06-13 21:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* On success, the number of bytes received is returned and *ret points to the received message
|
|
|
|
* which has a valid header and the correct size.
|
|
|
|
* If nothing useful was received 0 is returned.
|
|
|
|
* On failure, a negative error code is returned.
|
|
|
|
*/
|
|
|
|
int socket_read_message(sd_netlink *rtnl) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *first = NULL;
|
2015-06-13 21:10:39 +02:00
|
|
|
struct iovec iov = {};
|
|
|
|
uint32_t group = 0;
|
|
|
|
bool multi_part = false, done = false;
|
|
|
|
struct nlmsghdr *new_msg;
|
|
|
|
size_t len;
|
|
|
|
int r;
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
assert(rtnl->rbuffer);
|
|
|
|
assert(rtnl->rbuffer_allocated >= sizeof(struct nlmsghdr));
|
|
|
|
|
|
|
|
/* read nothing, just get the pending message size */
|
|
|
|
r = socket_recv_message(rtnl->fd, &iov, NULL, true);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
else
|
2018-01-23 15:47:18 +01:00
|
|
|
len = (size_t) r;
|
2015-06-13 21:10:39 +02:00
|
|
|
|
|
|
|
/* make room for the pending message */
|
|
|
|
if (!greedy_realloc((void **)&rtnl->rbuffer,
|
|
|
|
&rtnl->rbuffer_allocated,
|
|
|
|
len, sizeof(uint8_t)))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-11-27 10:34:32 +01:00
|
|
|
iov = IOVEC_MAKE(rtnl->rbuffer, rtnl->rbuffer_allocated);
|
2015-06-13 21:10:39 +02:00
|
|
|
|
|
|
|
/* read the pending message */
|
|
|
|
r = socket_recv_message(rtnl->fd, &iov, &group, false);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
else
|
2018-01-23 15:47:18 +01:00
|
|
|
len = (size_t) r;
|
2015-06-13 21:10:39 +02:00
|
|
|
|
|
|
|
if (len > rtnl->rbuffer_allocated)
|
|
|
|
/* message did not fit in read buffer */
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
if (NLMSG_OK(rtnl->rbuffer, len) && rtnl->rbuffer->nlmsg_flags & NLM_F_MULTI) {
|
|
|
|
multi_part = true;
|
|
|
|
|
|
|
|
for (i = 0; i < rtnl->rqueue_partial_size; i++) {
|
|
|
|
if (rtnl_message_get_serial(rtnl->rqueue_partial[i]) ==
|
|
|
|
rtnl->rbuffer->nlmsg_seq) {
|
|
|
|
first = rtnl->rqueue_partial[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (new_msg = rtnl->rbuffer; NLMSG_OK(new_msg, len) && !done; new_msg = NLMSG_NEXT(new_msg, len)) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
|
2015-06-13 21:10:39 +02:00
|
|
|
const NLType *nl_type;
|
|
|
|
|
|
|
|
if (!group && new_msg->nlmsg_pid != rtnl->sockaddr.nl.nl_pid)
|
|
|
|
/* not broadcast and not for us */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (new_msg->nlmsg_type == NLMSG_NOOP)
|
|
|
|
/* silently drop noop messages */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (new_msg->nlmsg_type == NLMSG_DONE) {
|
|
|
|
/* finished reading multi-part message */
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
/* if first is not defined, put NLMSG_DONE into the receive queue. */
|
|
|
|
if (first)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that we support this message type */
|
2019-07-23 08:54:06 +02:00
|
|
|
r = type_system_root_get_type(rtnl, &nl_type, new_msg->nlmsg_type);
|
2015-06-13 21:10:39 +02:00
|
|
|
if (r < 0) {
|
|
|
|
if (r == -EOPNOTSUPP)
|
|
|
|
log_debug("sd-netlink: ignored message with unknown type: %i",
|
|
|
|
new_msg->nlmsg_type);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that the size matches the message type */
|
2015-06-23 11:03:10 +02:00
|
|
|
if (new_msg->nlmsg_len < NLMSG_LENGTH(type_get_size(nl_type))) {
|
2019-06-28 20:27:24 +02:00
|
|
|
log_debug("sd-netlink: message is shorter than expected, dropping");
|
2015-06-13 21:10:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = message_new_empty(rtnl, &m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
m->broadcast = !!group;
|
|
|
|
|
|
|
|
m->hdr = memdup(new_msg, new_msg->nlmsg_len);
|
|
|
|
if (!m->hdr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* seal and parse the top-level message */
|
2019-07-23 08:54:06 +02:00
|
|
|
r = sd_netlink_message_rewind(m, rtnl);
|
2015-06-13 21:10:39 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* push the message onto the multi-part message stack */
|
|
|
|
if (first)
|
|
|
|
m->next = first;
|
2018-04-05 07:26:26 +02:00
|
|
|
first = TAKE_PTR(m);
|
2015-06-13 21:10:39 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 15:47:18 +01:00
|
|
|
if (len > 0)
|
2015-06-13 21:10:39 +02:00
|
|
|
log_debug("sd-netlink: discarding %zu bytes of incoming message", len);
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!multi_part || done) {
|
|
|
|
/* we got a complete message, push it on the read queue */
|
|
|
|
r = rtnl_rqueue_make_room(rtnl);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
rtnl->rqueue[rtnl->rqueue_size++] = TAKE_PTR(first);
|
2015-06-13 21:10:39 +02:00
|
|
|
|
|
|
|
if (multi_part && (i < rtnl->rqueue_partial_size)) {
|
|
|
|
/* remove the message form the partial read queue */
|
|
|
|
memmove(rtnl->rqueue_partial + i,rtnl->rqueue_partial + i + 1,
|
|
|
|
sizeof(sd_netlink_message*) * (rtnl->rqueue_partial_size - i - 1));
|
2016-02-23 05:32:04 +01:00
|
|
|
rtnl->rqueue_partial_size--;
|
2015-06-13 21:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* we only got a partial multi-part message, push it on the
|
|
|
|
partial read queue */
|
2018-01-23 15:47:18 +01:00
|
|
|
if (i < rtnl->rqueue_partial_size)
|
2018-04-05 07:26:26 +02:00
|
|
|
rtnl->rqueue_partial[i] = TAKE_PTR(first);
|
2018-01-23 15:47:18 +01:00
|
|
|
else {
|
2015-06-13 21:10:39 +02:00
|
|
|
r = rtnl_rqueue_partial_make_room(rtnl);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
rtnl->rqueue_partial[rtnl->rqueue_partial_size++] = TAKE_PTR(first);
|
2015-06-13 21:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|