1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-30 14:55:37 +03:00

Merge pull request #24356 from keszybz/sd-netlink-api

Small improvements to the sd-netlink api
This commit is contained in:
Yu Watanabe 2022-08-20 19:11:02 +09:00 committed by GitHub
commit 0f27758ae9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 81 additions and 99 deletions

View File

@ -792,7 +792,6 @@ global:
sd_hwdb_new_from_path;
sd_netlink_new_from_fd;
sd_netlink_open;
sd_netlink_open_fd;
sd_netlink_increase_rxbuf;
@ -855,7 +854,7 @@ global:
sd_netlink_message_next;
sd_netlink_message_ref;
sd_netlink_message_unref;
sd_netlink_message_request_dump;
sd_netlink_message_set_request_dump;
sd_netlink_message_is_error;
sd_netlink_message_get_errno;
sd_netlink_message_get_type;

View File

@ -633,77 +633,77 @@ _public_ int sd_rtnl_message_addr_set_scope(sd_netlink_message *m, unsigned char
return 0;
}
_public_ int sd_rtnl_message_addr_get_family(sd_netlink_message *m, int *family) {
_public_ int sd_rtnl_message_addr_get_family(sd_netlink_message *m, int *ret_family) {
struct ifaddrmsg *ifa;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
assert_return(family, -EINVAL);
assert_return(ret_family, -EINVAL);
ifa = NLMSG_DATA(m->hdr);
*family = ifa->ifa_family;
*ret_family = ifa->ifa_family;
return 0;
}
_public_ int sd_rtnl_message_addr_get_prefixlen(sd_netlink_message *m, unsigned char *prefixlen) {
_public_ int sd_rtnl_message_addr_get_prefixlen(sd_netlink_message *m, unsigned char *ret_prefixlen) {
struct ifaddrmsg *ifa;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
assert_return(prefixlen, -EINVAL);
assert_return(ret_prefixlen, -EINVAL);
ifa = NLMSG_DATA(m->hdr);
*prefixlen = ifa->ifa_prefixlen;
*ret_prefixlen = ifa->ifa_prefixlen;
return 0;
}
_public_ int sd_rtnl_message_addr_get_scope(sd_netlink_message *m, unsigned char *scope) {
_public_ int sd_rtnl_message_addr_get_scope(sd_netlink_message *m, unsigned char *ret_scope) {
struct ifaddrmsg *ifa;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
assert_return(scope, -EINVAL);
assert_return(ret_scope, -EINVAL);
ifa = NLMSG_DATA(m->hdr);
*scope = ifa->ifa_scope;
*ret_scope = ifa->ifa_scope;
return 0;
}
_public_ int sd_rtnl_message_addr_get_flags(sd_netlink_message *m, unsigned char *flags) {
_public_ int sd_rtnl_message_addr_get_flags(sd_netlink_message *m, unsigned char *ret_flags) {
struct ifaddrmsg *ifa;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
assert_return(flags, -EINVAL);
assert_return(ret_flags, -EINVAL);
ifa = NLMSG_DATA(m->hdr);
*flags = ifa->ifa_flags;
*ret_flags = ifa->ifa_flags;
return 0;
}
_public_ int sd_rtnl_message_addr_get_ifindex(sd_netlink_message *m, int *ifindex) {
_public_ int sd_rtnl_message_addr_get_ifindex(sd_netlink_message *m, int *ret_ifindex) {
struct ifaddrmsg *ifa;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
assert_return(ifindex, -EINVAL);
assert_return(ret_ifindex, -EINVAL);
ifa = NLMSG_DATA(m->hdr);
*ifindex = ifa->ifa_index;
*ret_ifindex = ifa->ifa_index;
return 0;
}

View File

@ -113,7 +113,7 @@ int message_new_synthetic_error(sd_netlink *nl, int error, uint32_t serial, sd_n
return 0;
}
_public_ int sd_netlink_message_request_dump(sd_netlink_message *m, int dump) {
_public_ int sd_netlink_message_set_request_dump(sd_netlink_message *m, int dump) {
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(m->protocol != NETLINK_ROUTE ||
@ -256,7 +256,7 @@ _public_ int sd_netlink_message_append_string(sd_netlink_message *m, uint16_t at
return 0;
}
_public_ int sd_netlink_message_append_strv(sd_netlink_message *m, uint16_t attr_type, char * const *data) {
_public_ int sd_netlink_message_append_strv(sd_netlink_message *m, uint16_t attr_type, const char* const *data) {
size_t length, size;
int r;

View File

@ -187,7 +187,12 @@ int rtnl_get_link_alternative_names(sd_netlink **rtnl, int ifindex, char ***ret)
return 0;
}
static int rtnl_update_link_alternative_names(sd_netlink **rtnl, uint16_t nlmsg_type, int ifindex, char * const *alternative_names) {
static int rtnl_update_link_alternative_names(
sd_netlink **rtnl,
uint16_t nlmsg_type,
int ifindex,
char* const *alternative_names) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
int r;
@ -212,7 +217,7 @@ static int rtnl_update_link_alternative_names(sd_netlink **rtnl, uint16_t nlmsg_
if (r < 0)
return r;
r = sd_netlink_message_append_strv(message, IFLA_ALT_IFNAME, alternative_names);
r = sd_netlink_message_append_strv(message, IFLA_ALT_IFNAME, (const char**) alternative_names);
if (r < 0)
return r;
@ -227,15 +232,19 @@ static int rtnl_update_link_alternative_names(sd_netlink **rtnl, uint16_t nlmsg_
return 0;
}
int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names) {
int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char* const *alternative_names) {
return rtnl_update_link_alternative_names(rtnl, RTM_NEWLINKPROP, ifindex, alternative_names);
}
int rtnl_delete_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names) {
int rtnl_delete_link_alternative_names(sd_netlink **rtnl, int ifindex, char* const *alternative_names) {
return rtnl_update_link_alternative_names(rtnl, RTM_DELLINKPROP, ifindex, alternative_names);
}
int rtnl_set_link_alternative_names_by_ifname(sd_netlink **rtnl, const char *ifname, char * const *alternative_names) {
int rtnl_set_link_alternative_names_by_ifname(
sd_netlink **rtnl,
const char *ifname,
char* const *alternative_names) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
int r;
@ -263,7 +272,7 @@ int rtnl_set_link_alternative_names_by_ifname(sd_netlink **rtnl, const char *ifn
if (r < 0)
return r;
r = sd_netlink_message_append_strv(message, IFLA_ALT_IFNAME, alternative_names);
r = sd_netlink_message_append_strv(message, IFLA_ALT_IFNAME, (const char**) alternative_names);
if (r < 0)
return r;

View File

@ -42,9 +42,9 @@ int rtnl_set_link_properties(
uint32_t gso_max_size,
size_t gso_max_segments);
int rtnl_get_link_alternative_names(sd_netlink **rtnl, int ifindex, char ***ret);
int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names);
int rtnl_set_link_alternative_names_by_ifname(sd_netlink **rtnl, const char *ifname, char * const *alternative_names);
int rtnl_delete_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names);
int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char* const *alternative_names);
int rtnl_set_link_alternative_names_by_ifname(sd_netlink **rtnl, const char *ifname, char* const *alternative_names);
int rtnl_delete_link_alternative_names(sd_netlink **rtnl, int ifindex, char* const *alternative_names);
int rtnl_resolve_link_alternative_name(sd_netlink **rtnl, const char *name, char **ret);
int rtnl_resolve_ifname(sd_netlink **rtnl, const char *name);
int rtnl_resolve_interface(sd_netlink **rtnl, const char *name);

View File

@ -69,31 +69,6 @@ static int netlink_new(sd_netlink **ret) {
return 0;
}
_public_ int sd_netlink_new_from_fd(sd_netlink **ret, int fd) {
_cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL;
socklen_t addrlen;
int r;
assert_return(ret, -EINVAL);
r = netlink_new(&nl);
if (r < 0)
return r;
addrlen = sizeof(nl->sockaddr);
if (getsockname(fd, &nl->sockaddr.sa, &addrlen) < 0)
return -errno;
if (nl->sockaddr.nl.nl_family != AF_NETLINK)
return -EINVAL;
nl->fd = fd;
*ret = TAKE_PTR(nl);
return 0;
}
_public_ int sd_netlink_open_fd(sd_netlink **ret, int fd) {
_cleanup_(sd_netlink_unrefp) sd_netlink *nl = NULL;
int r, protocol;
@ -564,7 +539,7 @@ _public_ int sd_netlink_call_async(
}
}
/* Set this at last. Otherwise, some failures in above call the destroy callback but some do not. */
/* Set this at last. Otherwise, some failures in above would call destroy_callback but some would not. */
slot->destroy_callback = destroy_callback;
if (ret_slot)
@ -730,32 +705,31 @@ static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
static int prepare_callback(sd_event_source *s, void *userdata) {
sd_netlink *nl = userdata;
int r, e;
int r, enabled;
usec_t until;
assert(s);
assert(nl);
e = sd_netlink_get_events(nl);
if (e < 0)
return e;
r = sd_event_source_set_io_events(nl->io_event_source, e);
r = sd_netlink_get_events(nl);
if (r < 0)
return r;
r = sd_netlink_get_timeout(nl, &until);
r = sd_event_source_set_io_events(nl->io_event_source, r);
if (r < 0)
return r;
if (r > 0) {
int j;
j = sd_event_source_set_time(nl->time_event_source, until);
if (j < 0)
return j;
enabled = sd_netlink_get_timeout(nl, &until);
if (enabled < 0)
return enabled;
if (enabled > 0) {
r = sd_event_source_set_time(nl->time_event_source, until);
if (r < 0)
return r;
}
r = sd_event_source_set_enabled(nl->time_event_source, r > 0);
r = sd_event_source_set_enabled(nl->time_event_source,
enabled > 0 ? SD_EVENT_ONESHOT : SD_EVENT_OFF);
if (r < 0)
return r;
@ -947,7 +921,7 @@ _public_ int sd_netlink_add_match(
destroy_callback, userdata, description);
}
_public_ int sd_netlink_attach_filter(sd_netlink *nl, size_t len, struct sock_filter *filter) {
_public_ int sd_netlink_attach_filter(sd_netlink *nl, size_t len, const struct sock_filter *filter) {
assert_return(nl, -EINVAL);
assert_return(len == 0 || filter, -EINVAL);
@ -955,7 +929,7 @@ _public_ int sd_netlink_attach_filter(sd_netlink *nl, size_t len, struct sock_fi
len == 0 ? SO_DETACH_FILTER : SO_ATTACH_FILTER,
&(struct sock_fprog) {
.len = len,
.filter = filter,
.filter = (struct sock_filter*) filter,
}, sizeof(struct sock_fprog)) < 0)
return -errno;

View File

@ -99,7 +99,7 @@ static void test_address_get(sd_netlink *rtnl, int ifindex) {
assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
assert_se(m);
assert_se(sd_netlink_message_request_dump(m, true) >= 0);
assert_se(sd_netlink_message_set_request_dump(m, true) >= 0);
assert_se(sd_netlink_call(rtnl, m, -1, &r) == 1);
assert_se(sd_netlink_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
@ -480,7 +480,7 @@ static void test_get_addresses(sd_netlink *rtnl) {
log_debug("/* %s */", __func__);
assert_se(sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC) >= 0);
assert_se(sd_netlink_message_request_dump(req, true) >= 0);
assert_se(sd_netlink_message_set_request_dump(req, true) >= 0);
assert_se(sd_netlink_call(rtnl, req, 0, &reply) >= 0);
for (m = reply; m; m = sd_netlink_message_next(m)) {
@ -572,7 +572,7 @@ static void test_strv(sd_netlink *rtnl) {
}
assert_se(sd_netlink_message_open_container(m, IFLA_PROP_LIST) >= 0);
assert_se(sd_netlink_message_append_strv(m, IFLA_ALT_IFNAME, names_in) >= 0);
assert_se(sd_netlink_message_append_strv(m, IFLA_ALT_IFNAME, (const char**) names_in) >= 0);
assert_se(sd_netlink_message_close_container(m) >= 0);
message_seal(m);

View File

@ -721,7 +721,7 @@ static int acquire_link_info(sd_bus *bus, sd_netlink *rtnl, char **patterns, Lin
if (r < 0)
return rtnl_log_create_error(r);
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return rtnl_log_create_error(r);
@ -922,7 +922,7 @@ static int get_gateway_description(
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;
@ -1115,7 +1115,7 @@ static int dump_address_labels(sd_netlink *rtnl) {
if (r < 0)
return log_error_errno(r, "Could not allocate RTM_GETADDRLABEL message: %m");
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;

View File

@ -809,7 +809,7 @@ int link_drop_ipv6ll_addresses(Link *link) {
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;

View File

@ -739,7 +739,7 @@ static int manager_enumerate_internal(
assert(req);
assert(process);
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;

View File

@ -71,7 +71,7 @@ static int speed_meter_handler(sd_event_source *s, uint64_t usec, void *userdata
return 0;
}
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0) {
log_warning_errno(r, "Failed to set dump flag, ignoring: %m");
return 0;

View File

@ -284,7 +284,7 @@ static int manager_rtnl_listen(Manager *m) {
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;

View File

@ -1964,7 +1964,7 @@ static int status_all(sd_bus *bus, StatusMode mode) {
if (r < 0)
return rtnl_log_create_error(r);
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return rtnl_log_create_error(r);

View File

@ -226,7 +226,7 @@ static int manager_rtnl_listen(Manager *m) {
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;
@ -248,7 +248,7 @@ static int manager_rtnl_listen(Manager *m) {
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;

View File

@ -83,7 +83,7 @@ int local_addresses(
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;
@ -236,7 +236,7 @@ int local_gateways(
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
r = sd_netlink_message_set_request_dump(req, true);
if (r < 0)
return r;

View File

@ -40,9 +40,8 @@ typedef int (*sd_netlink_message_handler_t)(sd_netlink *nl, sd_netlink_message *
typedef _sd_destroy_t sd_netlink_destroy_t;
/* bus */
int sd_netlink_new_from_fd(sd_netlink **nl, int fd);
int sd_netlink_open(sd_netlink **nl);
int sd_netlink_open_fd(sd_netlink **nl, int fd);
int sd_netlink_open(sd_netlink **ret);
int sd_netlink_open_fd(sd_netlink **ret, int fd);
int sd_netlink_increase_rxbuf(sd_netlink *nl, const size_t size);
sd_netlink *sd_netlink_ref(sd_netlink *nl);
@ -68,11 +67,11 @@ int sd_netlink_add_match(sd_netlink *nl, sd_netlink_slot **ret_slot, uint16_t ma
int sd_netlink_attach_event(sd_netlink *nl, sd_event *e, int64_t priority);
int sd_netlink_detach_event(sd_netlink *nl);
int sd_netlink_attach_filter(sd_netlink *nl, size_t len, struct sock_filter *filter);
int sd_netlink_attach_filter(sd_netlink *nl, size_t len, const struct sock_filter *filter);
/* message */
/* Message construction */
int sd_netlink_message_append_string(sd_netlink_message *m, uint16_t attr_type, const char *data);
int sd_netlink_message_append_strv(sd_netlink_message *m, uint16_t attr_type, char * const *data);
int sd_netlink_message_append_strv(sd_netlink_message *m, uint16_t attr_type, const char* const *data);
int sd_netlink_message_append_flag(sd_netlink_message *m, uint16_t attr_type);
int sd_netlink_message_append_u8(sd_netlink_message *m, uint16_t attr_type, uint8_t data);
int sd_netlink_message_append_u16(sd_netlink_message *m, uint16_t attr_type, uint16_t data);
@ -100,6 +99,10 @@ int sd_netlink_message_open_container(sd_netlink_message *m, uint16_t attr_type)
int sd_netlink_message_open_container_union(sd_netlink_message *m, uint16_t attr_type, const char *key);
int sd_netlink_message_close_container(sd_netlink_message *m);
int sd_netlink_message_open_array(sd_netlink_message *m, uint16_t type);
int sd_netlink_message_cancel_array(sd_netlink_message *m);
/* Reading messages */
int sd_netlink_message_read(sd_netlink_message *m, uint16_t attr_type, size_t size, void *data);
int sd_netlink_message_read_data(sd_netlink_message *m, uint16_t attr_type, size_t *ret_size, void **ret_data);
int sd_netlink_message_read_data_suffix0(sd_netlink_message *m, uint16_t attr_type, size_t *ret_size, void **ret_data);
@ -118,9 +121,6 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, uint16_t attr_type
int sd_netlink_message_enter_array(sd_netlink_message *m, uint16_t attr_type);
int sd_netlink_message_exit_container(sd_netlink_message *m);
int sd_netlink_message_open_array(sd_netlink_message *m, uint16_t type);
int sd_netlink_message_cancel_array(sd_netlink_message *m);
int sd_netlink_message_rewind(sd_netlink_message *m, sd_netlink *nl);
sd_netlink_message *sd_netlink_message_next(sd_netlink_message *m);
@ -128,7 +128,7 @@ sd_netlink_message *sd_netlink_message_next(sd_netlink_message *m);
sd_netlink_message *sd_netlink_message_ref(sd_netlink_message *m);
sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m);
int sd_netlink_message_request_dump(sd_netlink_message *m, int dump);
int sd_netlink_message_set_request_dump(sd_netlink_message *m, int dump);
int sd_netlink_message_is_error(sd_netlink_message *m);
int sd_netlink_message_get_errno(sd_netlink_message *m);
int sd_netlink_message_get_type(sd_netlink_message *m, uint16_t *type);
@ -145,10 +145,10 @@ int sd_rtnl_message_addr_set_prefixlen(sd_netlink_message *m, unsigned char pref
int sd_rtnl_message_addr_set_scope(sd_netlink_message *m, unsigned char scope);
int sd_rtnl_message_addr_set_flags(sd_netlink_message *m, unsigned char flags);
int sd_rtnl_message_addr_get_family(sd_netlink_message *m, int *family);
int sd_rtnl_message_addr_get_prefixlen(sd_netlink_message *m, unsigned char *prefixlen);
int sd_rtnl_message_addr_get_scope(sd_netlink_message *m, unsigned char *scope);
int sd_rtnl_message_addr_get_flags(sd_netlink_message *m, unsigned char *flags);
int sd_rtnl_message_addr_get_ifindex(sd_netlink_message *m, int *ifindex);
int sd_rtnl_message_addr_get_prefixlen(sd_netlink_message *m, unsigned char *ret_prefixlen);
int sd_rtnl_message_addr_get_scope(sd_netlink_message *m, unsigned char *ret_scope);
int sd_rtnl_message_addr_get_flags(sd_netlink_message *m, unsigned char *ret_flags);
int sd_rtnl_message_addr_get_ifindex(sd_netlink_message *m, int *ret_ifindex);
int sd_rtnl_message_new_link(sd_netlink *nl, sd_netlink_message **ret, uint16_t msg_type, int index);
int sd_rtnl_message_link_set_flags(sd_netlink_message *m, unsigned flags, unsigned change);

View File

@ -139,7 +139,7 @@ TEST(link_info_get) {
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0) >= 0);
assert_se(sd_netlink_message_request_dump(req, true) >= 0);
assert_se(sd_netlink_message_set_request_dump(req, true) >= 0);
assert_se(sd_netlink_call(rtnl, req, 0, &reply) >= 0);
for (sd_netlink_message *reply_one = reply; reply_one; reply_one = sd_netlink_message_next(reply_one)) {