1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

sd-netlink: make type_system_get_*() and friends return value directly

This commit is contained in:
Yu Watanabe 2021-08-29 17:59:17 +09:00
parent e1578f608b
commit 98be429243
7 changed files with 121 additions and 145 deletions

View File

@ -191,16 +191,13 @@ static int genl_family_new(
return 0;
}
static int genl_family_get_type_system(const GenericNetlinkFamily *family, const NLTypeSystem **ret) {
static const NLTypeSystem *genl_family_get_type_system(const GenericNetlinkFamily *family) {
assert(family);
assert(ret);
if (family->type_system) {
*ret = family->type_system;
return 0;
}
if (family->type_system)
return family->type_system;
return genl_get_type_system_by_name(family->name, ret);
return genl_get_type_system_by_name(family->name);
}
static int genl_message_new(
@ -218,9 +215,9 @@ static int genl_message_new(
assert(family);
assert(ret);
r = genl_family_get_type_system(family, &type_system);
if (r < 0)
return r;
type_system = genl_family_get_type_system(family);
if (!type_system)
return -EOPNOTSUPP;
r = message_new_full(nl, family->id, type_system,
sizeof(struct genlmsghdr) + family->additional_header_size, &m);
@ -252,9 +249,9 @@ static int genl_family_get_by_name_internal(
assert(name);
assert(ret);
r = genl_get_type_system_by_name(name, &type_system);
if (r < 0)
return r;
type_system = genl_get_type_system_by_name(name);
if (!type_system)
return -EOPNOTSUPP;
r = genl_message_new(nl, ctrl, CTRL_CMD_GETFAMILY, &req);
if (r < 0)
@ -340,9 +337,13 @@ int genl_get_type_system_and_header_size(
return r;
if (ret_type_system) {
r = genl_family_get_type_system(f, ret_type_system);
if (r < 0)
return r;
const NLTypeSystem *t;
t = genl_family_get_type_system(f);
if (!t)
return -EOPNOTSUPP;
*ret_type_system = t;
}
if (ret_header_size)
*ret_header_size = sizeof(struct genlmsghdr) + f->additional_header_size;

View File

@ -215,13 +215,12 @@ static int add_rtattr(sd_netlink_message *m, unsigned short type, const void *da
static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, uint16_t attribute_type, uint16_t data_type) {
const NLType *type;
int r;
assert(m);
r = type_system_get_type(m->containers[m->n_containers].type_system, &type, attribute_type);
if (r < 0)
return r;
type = type_system_get_type(m->containers[m->n_containers].type_system, attribute_type);
if (!type)
return -EOPNOTSUPP;
if (type_get_type(type) != data_type)
return -EINVAL;
@ -584,23 +583,23 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type
if (r < 0)
return r;
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
if (r < 0)
return r;
type_system_union = type_system_get_type_system_union(
m->containers[m->n_containers].type_system,
type);
if (!type_system_union)
return -EOPNOTSUPP;
r = type_system_union_get_type_system_by_protocol(
m->containers[m->n_containers + 1].type_system =
type_system_union_get_type_system_by_protocol(
type_system_union,
&m->containers[m->n_containers + 1].type_system,
family);
if (r < 0)
return r;
} else {
r = type_system_get_type_system(m->containers[m->n_containers].type_system,
&m->containers[m->n_containers + 1].type_system,
type);
if (r < 0)
return r;
}
} else
m->containers[m->n_containers + 1].type_system =
type_system_get_type_system(
m->containers[m->n_containers].type_system,
type);
if (!m->containers[m->n_containers + 1].type_system)
return -EOPNOTSUPP;
r = add_rtattr(m, type | NLA_F_NESTED, NULL, size);
if (r < 0)
@ -619,16 +618,18 @@ int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned shor
assert_return(!m->sealed, -EPERM);
assert_return(m->n_containers < (NETLINK_CONTAINER_DEPTH - 1), -ERANGE);
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
if (r < 0)
return r;
type_system_union = type_system_get_type_system_union(
m->containers[m->n_containers].type_system,
type);
if (!type_system_union)
return -EOPNOTSUPP;
r = type_system_union_get_type_system_by_string(
m->containers[m->n_containers + 1].type_system =
type_system_union_get_type_system_by_string(
type_system_union,
&m->containers[m->n_containers + 1].type_system,
key);
if (r < 0)
return r;
if (!m->containers[m->n_containers + 1].type_system)
return -EOPNOTSUPP;
r = sd_netlink_message_append_string(m, type_system_union_get_match_attribute(type_system_union), key);
if (r < 0)
@ -1043,24 +1044,24 @@ int sd_netlink_message_read_strv(sd_netlink_message *m, unsigned short container
assert_return(m, -EINVAL);
assert_return(m->n_containers < NETLINK_CONTAINER_DEPTH, -EINVAL);
r = type_system_get_type(m->containers[m->n_containers].type_system,
&nl_type,
container_type);
if (r < 0)
return r;
nl_type = type_system_get_type(
m->containers[m->n_containers].type_system,
container_type);
if (!nl_type)
return -EOPNOTSUPP;
if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
return -EINVAL;
r = type_system_get_type_system(m->containers[m->n_containers].type_system,
&type_system,
container_type);
if (r < 0)
return r;
type_system = type_system_get_type_system(
m->containers[m->n_containers].type_system,
container_type);
if (!type_system)
return -EOPNOTSUPP;
r = type_system_get_type(type_system, &nl_type, type_id);
if (r < 0)
return r;
nl_type = type_system_get_type(type_system, type_id);
if (!nl_type)
return -EOPNOTSUPP;
if (type_get_type(nl_type) != NETLINK_TYPE_STRING)
return -EINVAL;
@ -1139,28 +1140,28 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
assert_return(m, -EINVAL);
assert_return(m->n_containers < (NETLINK_CONTAINER_DEPTH - 1), -EINVAL);
r = type_system_get_type(m->containers[m->n_containers].type_system,
&nl_type,
type_id);
if (r < 0)
return r;
nl_type = type_system_get_type(
m->containers[m->n_containers].type_system,
type_id);
if (!nl_type)
return -EOPNOTSUPP;
type = type_get_type(nl_type);
if (type == NETLINK_TYPE_NESTED) {
r = type_system_get_type_system(m->containers[m->n_containers].type_system,
&type_system,
type_id);
if (r < 0)
return r;
type_system = type_system_get_type_system(
m->containers[m->n_containers].type_system,
type_id);
if (!type_system)
return -EOPNOTSUPP;
} else if (type == NETLINK_TYPE_UNION) {
const NLTypeSystemUnion *type_system_union;
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system,
&type_system_union,
type_id);
if (r < 0)
return r;
type_system_union = type_system_get_type_system_union(
m->containers[m->n_containers].type_system,
type_id);
if (!type_system_union)
return -EOPNOTSUPP;
switch (type_system_union_get_match_type(type_system_union)) {
case NL_MATCH_SIBLING: {
@ -1173,12 +1174,11 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
if (r < 0)
return r;
r = type_system_union_get_type_system_by_string(
type_system = type_system_union_get_type_system_by_string(
type_system_union,
&type_system,
key);
if (r < 0)
return r;
if (!type_system)
return -EOPNOTSUPP;
break;
}
@ -1189,12 +1189,11 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
if (r < 0)
return r;
r = type_system_union_get_type_system_by_protocol(
type_system = type_system_union_get_type_system_by_protocol(
type_system_union,
&type_system,
family);
if (r < 0)
return r;
if (!type_system)
return -EOPNOTSUPP;
break;
}

View File

@ -229,6 +229,6 @@ static const NLTypeSystemUnionElement genl_type_systems[] = {
/* This is the root type system union, so match_attribute is not necessary. */
DEFINE_TYPE_SYSTEM_UNION_MATCH_SIBLING(genl, 0);
int genl_get_type_system_by_name(const char *name, const NLTypeSystem **ret) {
return type_system_union_get_type_system_by_string(&genl_type_system_union, ret, name);
const NLTypeSystem *genl_get_type_system_by_name(const char *name) {
return type_system_union_get_type_system_by_string(&genl_type_system_union, name);
}

View File

@ -186,13 +186,12 @@ static const NLType nfnl_types[] = {
DEFINE_TYPE_SYSTEM(nfnl);
int nfnl_get_type(uint16_t nlmsg_type, const NLType **ret) {
const NLType *nfnl_get_type(uint16_t nlmsg_type) {
const NLTypeSystem *subsys;
int r;
r = type_system_get_type_system(&nfnl_type_system, &subsys, nlmsg_type >> 8);
if (r < 0)
return r;
subsys = type_system_get_type_system(&nfnl_type_system, nlmsg_type >> 8);
if (!subsys)
return NULL;
return type_system_get_type(subsys, ret, nlmsg_type & ((1U << 8) - 1));
return type_system_get_type(subsys, nlmsg_type & ((1U << 8) - 1));
}

View File

@ -867,6 +867,6 @@ static const NLType rtnl_types[] = {
DEFINE_TYPE_SYSTEM(rtnl);
int rtnl_get_type(uint16_t nlmsg_type, const NLType **ret) {
return type_system_get_type(&rtnl_type_system, ret, nlmsg_type);
const NLType *rtnl_get_type(uint16_t nlmsg_type) {
return type_system_get_type(&rtnl_type_system, nlmsg_type);
}

View File

@ -50,11 +50,6 @@ const NLTypeSystemUnion *type_get_type_system_union(const NLType *nl_type) {
return nl_type->type_system_union;
}
uint16_t type_system_get_count(const NLTypeSystem *type_system) {
assert(type_system);
return type_system->count;
}
int type_system_root_get_type_system_and_header_size(
sd_netlink *nl,
uint16_t type,
@ -62,27 +57,26 @@ int type_system_root_get_type_system_and_header_size(
size_t *ret_header_size) {
const NLType *nl_type;
int r;
assert(nl);
if (IN_SET(type, NLMSG_DONE, NLMSG_ERROR))
r = type_system_get_type(&basic_type_system, &nl_type, type);
nl_type = type_system_get_type(&basic_type_system, type);
else
switch(nl->protocol) {
case NETLINK_ROUTE:
r = rtnl_get_type(type, &nl_type);
nl_type = rtnl_get_type(type);
break;
case NETLINK_NETFILTER:
r = nfnl_get_type(type, &nl_type);
nl_type = nfnl_get_type(type);
break;
case NETLINK_GENERIC:
return genl_get_type_system_and_header_size(nl, type, ret_type_system, ret_header_size);
default:
return -EOPNOTSUPP;
}
if (r < 0)
return r;
if (!nl_type)
return -EOPNOTSUPP;
if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
return -EOPNOTSUPP;
@ -94,51 +88,41 @@ int type_system_root_get_type_system_and_header_size(
return 0;
}
int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
const NLType *type_system_get_type(const NLTypeSystem *type_system, uint16_t type) {
const NLType *nl_type;
assert(ret);
assert(type_system);
assert(type_system->types);
if (type >= type_system->count)
return -EOPNOTSUPP;
return NULL;
nl_type = &type_system->types[type];
if (nl_type->type == NETLINK_TYPE_UNSPEC)
return -EOPNOTSUPP;
return NULL;
*ret = nl_type;
return 0;
return nl_type;
}
int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type) {
const NLTypeSystem *type_system_get_type_system(const NLTypeSystem *type_system, uint16_t type) {
const NLType *nl_type;
int r;
assert(ret);
nl_type = type_system_get_type(type_system, type);
if (!nl_type)
return NULL;
r = type_system_get_type(type_system, &nl_type, type);
if (r < 0)
return r;
*ret = type_get_type_system(nl_type);
return 0;
return type_get_type_system(nl_type);
}
int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type) {
const NLTypeSystemUnion *type_system_get_type_system_union(const NLTypeSystem *type_system, uint16_t type) {
const NLType *nl_type;
int r;
assert(ret);
nl_type = type_system_get_type(type_system, type);
if (!nl_type)
return NULL;
r = type_system_get_type(type_system, &nl_type, type);
if (r < 0)
return r;
*ret = type_get_type_system_union(nl_type);
return 0;
return type_get_type_system_union(nl_type);
}
NLMatchType type_system_union_get_match_type(const NLTypeSystemUnion *type_system_union) {
@ -152,33 +136,27 @@ uint16_t type_system_union_get_match_attribute(const NLTypeSystemUnion *type_sys
return type_system_union->match_attribute;
}
int type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key) {
const NLTypeSystem *type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const char *key) {
assert(type_system_union);
assert(type_system_union->elements);
assert(type_system_union->match_type == NL_MATCH_SIBLING);
assert(ret);
assert(key);
for (size_t i = 0; i < type_system_union->count; i++)
if (streq(type_system_union->elements[i].name, key)) {
*ret = &type_system_union->elements[i].type_system;
return 0;
}
if (streq(type_system_union->elements[i].name, key))
return &type_system_union->elements[i].type_system;
return -EOPNOTSUPP;
return NULL;
}
int type_system_union_get_type_system_by_protocol(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol) {
const NLTypeSystem *type_system_union_get_type_system_by_protocol(const NLTypeSystemUnion *type_system_union, uint16_t protocol) {
assert(type_system_union);
assert(type_system_union->elements);
assert(type_system_union->match_type == NL_MATCH_PROTOCOL);
assert(ret);
for (size_t i = 0; i < type_system_union->count; i++)
if (type_system_union->elements[i].protocol == protocol) {
*ret = &type_system_union->elements[i].type_system;
return 0;
}
if (type_system_union->elements[i].protocol == protocol)
return &type_system_union->elements[i].type_system;
return -EOPNOTSUPP;
return NULL;
}

View File

@ -35,9 +35,9 @@ typedef struct NLTypeSystemUnion NLTypeSystemUnion;
typedef struct NLTypeSystem NLTypeSystem;
typedef struct NLType NLType;
int rtnl_get_type(uint16_t nlmsg_type, const NLType **ret);
int nfnl_get_type(uint16_t nlmsg_type, const NLType **ret);
int genl_get_type_system_by_name(const char *name, const NLTypeSystem **ret);
const NLType *rtnl_get_type(uint16_t nlmsg_type);
const NLType *nfnl_get_type(uint16_t nlmsg_type);
const NLTypeSystem *genl_get_type_system_by_name(const char *name);
int genl_get_type_system_and_header_size(
sd_netlink *nl,
uint16_t id,
@ -55,11 +55,10 @@ int type_system_root_get_type_system_and_header_size(
const NLTypeSystem **ret_type_system,
size_t *ret_header_size);
uint16_t type_system_get_count(const NLTypeSystem *type_system);
int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type);
int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type);
int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type);
const NLType *type_system_get_type(const NLTypeSystem *type_system, uint16_t type);
const NLTypeSystem *type_system_get_type_system(const NLTypeSystem *type_system, uint16_t type);
const NLTypeSystemUnion *type_system_get_type_system_union(const NLTypeSystem *type_system, uint16_t type);
NLMatchType type_system_union_get_match_type(const NLTypeSystemUnion *type_system_union);
uint16_t type_system_union_get_match_attribute(const NLTypeSystemUnion *type_system_union);
int type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, const char *key);
int type_system_union_get_type_system_by_protocol(const NLTypeSystemUnion *type_system_union, const NLTypeSystem **ret, uint16_t protocol);
const NLTypeSystem *type_system_union_get_type_system_by_string(const NLTypeSystemUnion *type_system_union, const char *key);
const NLTypeSystem *type_system_union_get_type_system_by_protocol(const NLTypeSystemUnion *type_system_union, uint16_t protocol);