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; 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(family);
assert(ret);
if (family->type_system) { if (family->type_system)
*ret = family->type_system; return family->type_system;
return 0;
}
return genl_get_type_system_by_name(family->name, ret); return genl_get_type_system_by_name(family->name);
} }
static int genl_message_new( static int genl_message_new(
@ -218,9 +215,9 @@ static int genl_message_new(
assert(family); assert(family);
assert(ret); assert(ret);
r = genl_family_get_type_system(family, &type_system); type_system = genl_family_get_type_system(family);
if (r < 0) if (!type_system)
return r; return -EOPNOTSUPP;
r = message_new_full(nl, family->id, type_system, r = message_new_full(nl, family->id, type_system,
sizeof(struct genlmsghdr) + family->additional_header_size, &m); sizeof(struct genlmsghdr) + family->additional_header_size, &m);
@ -252,9 +249,9 @@ static int genl_family_get_by_name_internal(
assert(name); assert(name);
assert(ret); assert(ret);
r = genl_get_type_system_by_name(name, &type_system); type_system = genl_get_type_system_by_name(name);
if (r < 0) if (!type_system)
return r; return -EOPNOTSUPP;
r = genl_message_new(nl, ctrl, CTRL_CMD_GETFAMILY, &req); r = genl_message_new(nl, ctrl, CTRL_CMD_GETFAMILY, &req);
if (r < 0) if (r < 0)
@ -340,9 +337,13 @@ int genl_get_type_system_and_header_size(
return r; return r;
if (ret_type_system) { if (ret_type_system) {
r = genl_family_get_type_system(f, ret_type_system); const NLTypeSystem *t;
if (r < 0)
return r; t = genl_family_get_type_system(f);
if (!t)
return -EOPNOTSUPP;
*ret_type_system = t;
} }
if (ret_header_size) if (ret_header_size)
*ret_header_size = sizeof(struct genlmsghdr) + f->additional_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) { 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; const NLType *type;
int r;
assert(m); assert(m);
r = type_system_get_type(m->containers[m->n_containers].type_system, &type, attribute_type); type = type_system_get_type(m->containers[m->n_containers].type_system, attribute_type);
if (r < 0) if (!type)
return r; return -EOPNOTSUPP;
if (type_get_type(type) != data_type) if (type_get_type(type) != data_type)
return -EINVAL; return -EINVAL;
@ -584,23 +583,23 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type
if (r < 0) if (r < 0)
return r; return r;
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type); type_system_union = type_system_get_type_system_union(
if (r < 0) m->containers[m->n_containers].type_system,
return r;
r = 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); type);
if (r < 0) if (!type_system_union)
return r; return -EOPNOTSUPP;
}
m->containers[m->n_containers + 1].type_system =
type_system_union_get_type_system_by_protocol(
type_system_union,
family);
} 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); r = add_rtattr(m, type | NLA_F_NESTED, NULL, size);
if (r < 0) 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->sealed, -EPERM);
assert_return(m->n_containers < (NETLINK_CONTAINER_DEPTH - 1), -ERANGE); 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); type_system_union = type_system_get_type_system_union(
if (r < 0) m->containers[m->n_containers].type_system,
return r; 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, type_system_union,
&m->containers[m->n_containers + 1].type_system,
key); key);
if (r < 0) if (!m->containers[m->n_containers + 1].type_system)
return r; return -EOPNOTSUPP;
r = sd_netlink_message_append_string(m, type_system_union_get_match_attribute(type_system_union), key); r = sd_netlink_message_append_string(m, type_system_union_get_match_attribute(type_system_union), key);
if (r < 0) 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, -EINVAL);
assert_return(m->n_containers < NETLINK_CONTAINER_DEPTH, -EINVAL); assert_return(m->n_containers < NETLINK_CONTAINER_DEPTH, -EINVAL);
r = type_system_get_type(m->containers[m->n_containers].type_system, nl_type = type_system_get_type(
&nl_type, m->containers[m->n_containers].type_system,
container_type); container_type);
if (r < 0) if (!nl_type)
return r; return -EOPNOTSUPP;
if (type_get_type(nl_type) != NETLINK_TYPE_NESTED) if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
return -EINVAL; return -EINVAL;
r = type_system_get_type_system(m->containers[m->n_containers].type_system, type_system = type_system_get_type_system(
&type_system, m->containers[m->n_containers].type_system,
container_type); container_type);
if (r < 0) if (!type_system)
return r; return -EOPNOTSUPP;
r = type_system_get_type(type_system, &nl_type, type_id); nl_type = type_system_get_type(type_system, type_id);
if (r < 0) if (!nl_type)
return r; return -EOPNOTSUPP;
if (type_get_type(nl_type) != NETLINK_TYPE_STRING) if (type_get_type(nl_type) != NETLINK_TYPE_STRING)
return -EINVAL; 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, -EINVAL);
assert_return(m->n_containers < (NETLINK_CONTAINER_DEPTH - 1), -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_system_get_type(
&nl_type, m->containers[m->n_containers].type_system,
type_id); type_id);
if (r < 0) if (!nl_type)
return r; return -EOPNOTSUPP;
type = type_get_type(nl_type); type = type_get_type(nl_type);
if (type == NETLINK_TYPE_NESTED) { if (type == NETLINK_TYPE_NESTED) {
r = type_system_get_type_system(m->containers[m->n_containers].type_system, type_system = type_system_get_type_system(
&type_system, m->containers[m->n_containers].type_system,
type_id); type_id);
if (r < 0) if (!type_system)
return r; return -EOPNOTSUPP;
} else if (type == NETLINK_TYPE_UNION) { } else if (type == NETLINK_TYPE_UNION) {
const NLTypeSystemUnion *type_system_union; const NLTypeSystemUnion *type_system_union;
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, type_system_union = type_system_get_type_system_union(
&type_system_union, m->containers[m->n_containers].type_system,
type_id); type_id);
if (r < 0) if (!type_system_union)
return r; return -EOPNOTSUPP;
switch (type_system_union_get_match_type(type_system_union)) { switch (type_system_union_get_match_type(type_system_union)) {
case NL_MATCH_SIBLING: { case NL_MATCH_SIBLING: {
@ -1173,12 +1174,11 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
if (r < 0) if (r < 0)
return r; 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_union,
&type_system,
key); key);
if (r < 0) if (!type_system)
return r; return -EOPNOTSUPP;
break; break;
} }
@ -1189,12 +1189,11 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
if (r < 0) if (r < 0)
return r; 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_union,
&type_system,
family); family);
if (r < 0) if (!type_system)
return r; return -EOPNOTSUPP;
break; 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. */ /* This is the root type system union, so match_attribute is not necessary. */
DEFINE_TYPE_SYSTEM_UNION_MATCH_SIBLING(genl, 0); DEFINE_TYPE_SYSTEM_UNION_MATCH_SIBLING(genl, 0);
int genl_get_type_system_by_name(const char *name, const NLTypeSystem **ret) { const NLTypeSystem *genl_get_type_system_by_name(const char *name) {
return type_system_union_get_type_system_by_string(&genl_type_system_union, ret, 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); 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; const NLTypeSystem *subsys;
int r;
r = type_system_get_type_system(&nfnl_type_system, &subsys, nlmsg_type >> 8); subsys = type_system_get_type_system(&nfnl_type_system, nlmsg_type >> 8);
if (r < 0) if (!subsys)
return r; 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); DEFINE_TYPE_SYSTEM(rtnl);
int rtnl_get_type(uint16_t nlmsg_type, const NLType **ret) { const NLType *rtnl_get_type(uint16_t nlmsg_type) {
return type_system_get_type(&rtnl_type_system, ret, 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; 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( int type_system_root_get_type_system_and_header_size(
sd_netlink *nl, sd_netlink *nl,
uint16_t type, uint16_t type,
@ -62,27 +57,26 @@ int type_system_root_get_type_system_and_header_size(
size_t *ret_header_size) { size_t *ret_header_size) {
const NLType *nl_type; const NLType *nl_type;
int r;
assert(nl); assert(nl);
if (IN_SET(type, NLMSG_DONE, NLMSG_ERROR)) 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 else
switch(nl->protocol) { switch(nl->protocol) {
case NETLINK_ROUTE: case NETLINK_ROUTE:
r = rtnl_get_type(type, &nl_type); nl_type = rtnl_get_type(type);
break; break;
case NETLINK_NETFILTER: case NETLINK_NETFILTER:
r = nfnl_get_type(type, &nl_type); nl_type = nfnl_get_type(type);
break; break;
case NETLINK_GENERIC: case NETLINK_GENERIC:
return genl_get_type_system_and_header_size(nl, type, ret_type_system, ret_header_size); return genl_get_type_system_and_header_size(nl, type, ret_type_system, ret_header_size);
default: default:
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
if (r < 0) if (!nl_type)
return r; return -EOPNOTSUPP;
if (type_get_type(nl_type) != NETLINK_TYPE_NESTED) if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
return -EOPNOTSUPP; return -EOPNOTSUPP;
@ -94,51 +88,41 @@ int type_system_root_get_type_system_and_header_size(
return 0; 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; const NLType *nl_type;
assert(ret);
assert(type_system); assert(type_system);
assert(type_system->types); assert(type_system->types);
if (type >= type_system->count) if (type >= type_system->count)
return -EOPNOTSUPP; return NULL;
nl_type = &type_system->types[type]; nl_type = &type_system->types[type];
if (nl_type->type == NETLINK_TYPE_UNSPEC) if (nl_type->type == NETLINK_TYPE_UNSPEC)
return -EOPNOTSUPP; return NULL;
*ret = nl_type; return nl_type;
return 0;
} }
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; 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); return type_get_type_system(nl_type);
if (r < 0)
return r;
*ret = type_get_type_system(nl_type);
return 0;
} }
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; 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); return type_get_type_system_union(nl_type);
if (r < 0)
return r;
*ret = type_get_type_system_union(nl_type);
return 0;
} }
NLMatchType type_system_union_get_match_type(const NLTypeSystemUnion *type_system_union) { 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; 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);
assert(type_system_union->elements); assert(type_system_union->elements);
assert(type_system_union->match_type == NL_MATCH_SIBLING); assert(type_system_union->match_type == NL_MATCH_SIBLING);
assert(ret);
assert(key); assert(key);
for (size_t i = 0; i < type_system_union->count; i++) for (size_t i = 0; i < type_system_union->count; i++)
if (streq(type_system_union->elements[i].name, key)) { if (streq(type_system_union->elements[i].name, key))
*ret = &type_system_union->elements[i].type_system; return &type_system_union->elements[i].type_system;
return 0;
}
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);
assert(type_system_union->elements); assert(type_system_union->elements);
assert(type_system_union->match_type == NL_MATCH_PROTOCOL); assert(type_system_union->match_type == NL_MATCH_PROTOCOL);
assert(ret);
for (size_t i = 0; i < type_system_union->count; i++) for (size_t i = 0; i < type_system_union->count; i++)
if (type_system_union->elements[i].protocol == protocol) { if (type_system_union->elements[i].protocol == protocol)
*ret = &type_system_union->elements[i].type_system; return &type_system_union->elements[i].type_system;
return 0;
}
return -EOPNOTSUPP; return NULL;
} }

View File

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