util: make *_from_name() returns negative errno on error

This commit is contained in:
Yu Watanabe 2018-11-28 17:54:04 +09:00
parent 7b5e750d2a
commit acf4d15893
9 changed files with 24 additions and 25 deletions

View File

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
@ -29,7 +30,7 @@ int af_from_name(const char *name) {
sc = lookup_af(name, strlen(name));
if (!sc)
return AF_UNSPEC;
return -EINVAL;
return sc->id;
}

View File

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <net/if_arp.h>
#include <string.h>
@ -29,7 +30,7 @@ int arphrd_from_name(const char *name) {
sc = lookup_arphrd(name, strlen(name));
if (!sc)
return 0;
return -EINVAL;
return sc->id;
}

View File

@ -1517,8 +1517,8 @@ int bus_exec_context_set_transient_property(
int af;
af = af_from_name(*s);
if (af <= 0)
return -EINVAL;
if (af < 0)
return af;
if (!invert == c->address_families_whitelist) {
r = set_put(c->address_families, INT_TO_PTR(af));

View File

@ -65,7 +65,7 @@ static int supported_socket_protocol_from_string(const char *s) {
r = socket_protocol_from_name(s);
if (r < 0)
return -EINVAL;
return r;
if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP))
return -EPROTONOSUPPORT;
@ -2871,8 +2871,8 @@ int config_parse_address_families(
}
af = af_from_name(word);
if (af <= 0) {
log_syntax(unit, LOG_ERR, filename, line, 0,
if (af < 0) {
log_syntax(unit, LOG_ERR, filename, line, af,
"Failed to parse address family, ignoring: %s", word);
continue;
}

View File

@ -177,7 +177,6 @@ int config_parse_capability(
for (;;) {
_cleanup_free_ char *word = NULL;
int cap;
r = extract_first_word(&rvalue, &word, NULL, 0);
if (r < 0) {
@ -187,13 +186,13 @@ int config_parse_capability(
if (r == 0)
break;
cap = capability_from_name(word);
if (cap < 0) {
log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse capability, ignoring: %s", word);
r = capability_from_name(word);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse capability, ignoring: %s", word);
continue;
}
u |= UINT64_C(1) << cap;
u |= UINT64_C(1) << r;
}
if (u == 0)

View File

@ -774,17 +774,14 @@ static int parse_argv(int argc, char *argv[]) {
else
minus = (uint64_t) -1;
} else {
int cap;
cap = capability_from_name(t);
if (cap < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to parse capability %s.", t);
r = capability_from_name(t);
if (r < 0)
return log_error_errno(r, "Failed to parse capability %s.", t);
if (c == ARG_CAPABILITY)
plus |= 1ULL << (uint64_t) cap;
plus |= 1ULL << r;
else
minus |= 1ULL << (uint64_t) cap;
minus |= 1ULL << r;
}
}

View File

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
@ -29,7 +30,7 @@ int socket_protocol_from_name(const char *name) {
sc = lookup_socket_protocol(name, strlen(name));
if (!sc)
return 0;
return -EINVAL;
return sc->id;
}

View File

@ -27,8 +27,8 @@ int main(int argc, const char *argv[]) {
assert_se(af_to_name(af_max()) == NULL);
assert_se(af_to_name(-1) == NULL);
assert_se(af_from_name("huddlduddl") == AF_UNSPEC);
assert_se(af_from_name("") == AF_UNSPEC);
assert_se(af_from_name("huddlduddl") == -EINVAL);
assert_se(af_from_name("") == -EINVAL);
return 0;
}

View File

@ -27,8 +27,8 @@ int main(int argc, const char *argv[]) {
assert_se(arphrd_to_name(arphrd_max()) == NULL);
assert_se(arphrd_to_name(0) == NULL);
assert_se(arphrd_from_name("huddlduddl") == 0);
assert_se(arphrd_from_name("") == 0);
assert_se(arphrd_from_name("huddlduddl") == -EINVAL);
assert_se(arphrd_from_name("") == -EINVAL);
return 0;
}