1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-11 05:17:44 +03:00

sd-netlink: shorten things a bit

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-03-09 17:26:01 +01:00
parent 4fa024683c
commit c7209bcfe1
2 changed files with 19 additions and 38 deletions

View File

@ -144,7 +144,6 @@ static int add_rtattr(sd_netlink_message *m, unsigned short type, const void *da
size_t message_length;
struct nlmsghdr *new_hdr;
struct rtattr *rta;
unsigned i;
int offset;
assert(m);
@ -172,7 +171,7 @@ static int add_rtattr(sd_netlink_message *m, unsigned short type, const void *da
rtattr_append_attribute_internal(rta, type, data, data_length);
/* if we are inside containers, extend them */
for (i = 0; i < m->n_containers; i++)
for (unsigned i = 0; i < m->n_containers; i++)
GET_CONTAINER(m, i)->rta_len += RTA_SPACE(data_length);
/* update message size */
@ -643,7 +642,6 @@ int sd_netlink_message_open_array(sd_netlink_message *m, uint16_t type) {
}
int sd_netlink_message_cancel_array(sd_netlink_message *m) {
unsigned i;
uint32_t rta_len;
assert_return(m, -EINVAL);
@ -652,7 +650,7 @@ int sd_netlink_message_cancel_array(sd_netlink_message *m) {
rta_len = GET_CONTAINER(m, (m->n_containers - 1))->rta_len;
for (i = 0; i < m->n_containers; i++)
for (unsigned i = 0; i < m->n_containers; i++)
GET_CONTAINER(m, i)->rta_len -= rta_len;
m->hdr->nlmsg_len -= rta_len;
@ -1283,7 +1281,6 @@ int sd_netlink_message_rewind(sd_netlink_message *m, sd_netlink *genl) {
const NLType *nl_type;
uint16_t type;
size_t size;
unsigned i;
int r;
assert_return(m, -EINVAL);
@ -1293,7 +1290,7 @@ int sd_netlink_message_rewind(sd_netlink_message *m, sd_netlink *genl) {
if (!m->sealed)
rtnl_message_seal(m);
for (i = 1; i <= m->n_containers; i++)
for (unsigned i = 1; i <= m->n_containers; i++)
m->containers[i].attributes = mfree(m->containers[i].attributes);
m->n_containers = 0;

View File

@ -29,7 +29,6 @@ int socket_open(int family) {
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);
@ -37,11 +36,11 @@ static int broadcast_groups_get(sd_netlink *nl) {
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
if (errno != ENOPROTOOPT)
return -errno;
nl->broadcast_group_dont_leave = true;
return 0;
}
if (len == 0)
@ -64,23 +63,15 @@ static int broadcast_groups_get(sd_netlink *nl) {
if (r < 0)
return r;
for (i = 0; i < len; i++) {
for (j = 0; j < sizeof(uint32_t) * 8; j++) {
uint32_t offset;
unsigned group;
for (unsigned i = 0; i < len; i++)
for (unsigned j = 0; j < sizeof(uint32_t) * 8; j++)
if (groups[i] & (1U << j)) {
unsigned group = i * sizeof(uint32_t) * 8 + j + 1;
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;
}
}
r = hashmap_put(nl->broadcast_group_refs, UINT_TO_PTR(group), UINT_TO_PTR(1));
if (r < 0)
return r;
}
return 0;
}
@ -104,11 +95,7 @@ int socket_bind(sd_netlink *nl) {
if (r < 0)
return -errno;
r = broadcast_groups_get(nl);
if (r < 0)
return r;
return 0;
return broadcast_groups_get(nl);
}
static unsigned broadcast_group_get_ref(sd_netlink *nl, unsigned group) {
@ -230,8 +217,6 @@ int socket_write_message(sd_netlink *nl, sd_netlink_message *m) {
int socket_writev_message(sd_netlink *nl, sd_netlink_message **m, size_t msgcount) {
_cleanup_free_ struct iovec *iovs = NULL;
ssize_t k;
size_t i;
assert(nl);
assert(m);
@ -241,13 +226,13 @@ int socket_writev_message(sd_netlink *nl, sd_netlink_message **m, size_t msgcoun
if (!iovs)
return -ENOMEM;
for (i = 0; i < msgcount; i++) {
for (size_t 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);
ssize_t k = writev(nl->fd, iovs, msgcount);
if (k < 0)
return -errno;
@ -315,7 +300,6 @@ int socket_read_message(sd_netlink *rtnl) {
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;
@ -362,7 +346,7 @@ int socket_read_message(sd_netlink *rtnl) {
}
}
for (new_msg = rtnl->rbuffer; NLMSG_OK(new_msg, len) && !done; new_msg = NLMSG_NEXT(new_msg, len)) {
for (struct nlmsghdr *new_msg = rtnl->rbuffer; NLMSG_OK(new_msg, len) && !done; new_msg = NLMSG_NEXT(new_msg, len)) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
const NLType *nl_type;