1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-10 05:18:17 +03:00

sd-network: modernize parse_operational_state_range()

- rename 'out' -> 'ret',
- introduce LINK_OPERSTATE_RANGE_INVALID,
- constify LINK_OPERSTATE_RANGE_DEFAULT,
- drop spurious const specifier for allocated string,
- etc,.
This commit is contained in:
Yu Watanabe 2024-01-16 23:54:39 +09:00
parent 15f624f80f
commit 2db2979505
2 changed files with 42 additions and 34 deletions

View File

@ -90,49 +90,48 @@ static const char *const link_online_state_table[_LINK_ONLINE_STATE_MAX] = {
DEFINE_STRING_TABLE_LOOKUP(link_online_state, LinkOnlineState);
int parse_operational_state_range(const char *str, LinkOperationalStateRange *out) {
LinkOperationalStateRange range = { _LINK_OPERSTATE_INVALID, _LINK_OPERSTATE_INVALID };
_cleanup_free_ const char *min = NULL;
int parse_operational_state_range(const char *s, LinkOperationalStateRange *ret) {
LinkOperationalStateRange range = LINK_OPERSTATE_RANGE_INVALID;
_cleanup_free_ char *buf = NULL;
const char *p;
assert(str);
assert(out);
assert(s);
assert(ret);
/* allowed formats: "min", "min:", "min:max", ":max" */
if (isempty(s) || streq(s, ":"))
return -EINVAL;
p = strchr(s, ':');
if (!p || isempty(p + 1))
range.max = LINK_OPERSTATE_ROUTABLE;
else {
range.max = link_operstate_from_string(p + 1);
if (range.max < 0)
return -EINVAL;
}
p = strchr(str, ':');
if (p) {
min = strndup(str, p - str);
buf = strndup(s, p - s);
if (!buf)
return -ENOMEM;
if (!isempty(p + 1)) {
range.max = link_operstate_from_string(p + 1);
if (range.max < 0)
return -EINVAL;
}
} else
min = strdup(str);
s = buf;
}
if (!min)
return -ENOMEM;
if (!isempty(min)) {
range.min = link_operstate_from_string(min);
if (isempty(s))
range.min = LINK_OPERSTATE_MISSING;
else {
range.min = link_operstate_from_string(s);
if (range.min < 0)
return -EINVAL;
}
/* Fail on empty strings. */
if (range.min == _LINK_OPERSTATE_INVALID && range.max == _LINK_OPERSTATE_INVALID)
if (!operational_state_range_is_valid(&range))
return -EINVAL;
if (range.min == _LINK_OPERSTATE_INVALID)
range.min = LINK_OPERSTATE_MISSING;
if (range.max == _LINK_OPERSTATE_INVALID)
range.max = LINK_OPERSTATE_ROUTABLE;
if (range.min > range.max)
return -EINVAL;
*out = range;
*ret = range;
return 0;
}

View File

@ -79,10 +79,19 @@ typedef struct LinkOperationalStateRange {
LinkOperationalState max;
} LinkOperationalStateRange;
#define LINK_OPERSTATE_RANGE_DEFAULT (LinkOperationalStateRange) { LINK_OPERSTATE_DEGRADED, \
LINK_OPERSTATE_ROUTABLE }
#define LINK_OPERSTATE_RANGE_DEFAULT \
(const LinkOperationalStateRange) { \
.min = LINK_OPERSTATE_DEGRADED, \
.max = LINK_OPERSTATE_ROUTABLE, \
}
int parse_operational_state_range(const char *str, LinkOperationalStateRange *out);
#define LINK_OPERSTATE_RANGE_INVALID \
(const LinkOperationalStateRange) { \
.min = _LINK_OPERSTATE_INVALID, \
.max = _LINK_OPERSTATE_INVALID, \
}
int parse_operational_state_range(const char *s, LinkOperationalStateRange *ret);
int network_link_get_operational_state(int ifindex, LinkOperationalState *ret);
static inline bool operational_state_is_valid(LinkOperationalState s) {