1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-15 09:57:39 +03:00

parse-util: add parse_tristate() and use it everywhere

We parse tristates all the time, let's add an explicit parser for them.
This commit is contained in:
Lennart Poettering 2023-11-01 16:48:42 +01:00 committed by Yu Watanabe
parent 6a4d0efa00
commit b71a721fbc
11 changed files with 47 additions and 84 deletions

View File

@ -44,6 +44,24 @@ int parse_boolean(const char *v) {
return -EINVAL;
}
int parse_tristate_full(const char *v, const char *third, int *ret) {
int r;
if (isempty(v) || streq_ptr(v, third)) { /* Empty string is always taken as the third/invalid/auto state */
if (ret)
*ret = -1;
} else {
r = parse_boolean(v);
if (r < 0)
return r;
if (ret)
*ret = r;
}
return 0;
}
int parse_pid(const char *s, pid_t* ret_pid) {
unsigned long ul = 0;
pid_t pid;

View File

@ -12,6 +12,10 @@
typedef unsigned long loadavg_t;
int parse_boolean(const char *v) _pure_;
int parse_tristate_full(const char *v, const char *third, int *ret);
static inline int parse_tristate(const char *v, int *ret) {
return parse_tristate_full(v, NULL, ret);
}
int parse_pid(const char *s, pid_t* ret_pid);
int parse_mode(const char *s, mode_t *ret);
int parse_ifindex(const char *s);

View File

@ -859,8 +859,7 @@ int config_parse_macsec_sa_activate(
_cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
_cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
MACsec *s = userdata;
int *dest;
int r;
int *dest, r;
assert(filename);
assert(section);
@ -877,21 +876,16 @@ int config_parse_macsec_sa_activate(
dest = a ? &a->sa.activate : &b->sa.activate;
if (isempty(rvalue))
r = -1;
else {
r = parse_boolean(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse activation mode of %s security association. "
"Ignoring assignment: %s",
streq(section, "MACsecTransmitAssociation") ? "transmit" : "receive",
rvalue);
return 0;
}
r = parse_tristate(rvalue, dest);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse activation mode of %s security association. "
"Ignoring assignment: %s",
streq(section, "MACsecTransmitAssociation") ? "transmit" : "receive",
rvalue);
return 0;
}
*dest = r;
TAKE_PTR(a);
TAKE_PTR(b);
@ -930,7 +924,7 @@ int config_parse_macsec_use_for_encoding(
return 0;
}
r = parse_boolean(rvalue);
r = parse_tristate(rvalue, &a->sa.use_for_encoding);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse %s= setting. Ignoring assignment: %s",
@ -938,7 +932,6 @@ int config_parse_macsec_use_for_encoding(
return 0;
}
a->sa.use_for_encoding = r;
if (a->sa.use_for_encoding > 0)
a->sa.activate = true;

View File

@ -1229,21 +1229,13 @@ int config_parse_nexthop_onlink(
if (r < 0)
return log_oom();
if (isempty(rvalue)) {
n->onlink = -1;
TAKE_PTR(n);
return 0;
}
r = parse_boolean(rvalue);
r = parse_tristate(rvalue, &n->onlink);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
return 0;
}
n->onlink = r;
TAKE_PTR(n);
return 0;
}

View File

@ -354,13 +354,7 @@ int config_parse_cake_tristate(
else
assert_not_reached();
if (isempty(rvalue)) {
*dest = -1;
TAKE_PTR(qdisc);
return 0;
}
r = parse_boolean(rvalue);
r = parse_tristate(rvalue, dest);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
@ -368,7 +362,6 @@ int config_parse_cake_tristate(
return 0;
}
*dest = r;
TAKE_PTR(qdisc);
return 0;
}

View File

@ -223,14 +223,7 @@ int config_parse_controlled_delay_bool(
cd = CODEL(qdisc);
if (isempty(rvalue)) {
cd->ecn = -1;
qdisc = NULL;
return 0;
}
r = parse_boolean(rvalue);
r = parse_tristate(rvalue, &cd->ecn);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
@ -238,8 +231,7 @@ int config_parse_controlled_delay_bool(
return 0;
}
cd->ecn = r;
qdisc = NULL;
TAKE_PTR(qdisc);
return 0;
}

View File

@ -251,14 +251,7 @@ int config_parse_fair_queueing_controlled_delay_bool(
fqcd = FQ_CODEL(qdisc);
if (isempty(rvalue)) {
fqcd->ecn = -1;
TAKE_PTR(qdisc);
return 0;
}
r = parse_boolean(rvalue);
r = parse_tristate(rvalue, &fqcd->ecn);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
@ -266,7 +259,6 @@ int config_parse_fair_queueing_controlled_delay_bool(
return 0;
}
fqcd->ecn = r;
TAKE_PTR(qdisc);
return 0;

View File

@ -267,14 +267,7 @@ int config_parse_fair_queueing_bool(
fq = FQ(qdisc);
if (isempty(rvalue)) {
fq->pacing = -1;
qdisc = NULL;
return 0;
}
r = parse_boolean(rvalue);
r = parse_tristate(rvalue, &fq->pacing);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
@ -283,7 +276,7 @@ int config_parse_fair_queueing_bool(
}
fq->pacing = r;
qdisc = NULL;
TAKE_PTR(qdisc);
return 0;
}

View File

@ -163,14 +163,7 @@ int config_parse_generic_random_early_detection_bool(
gred = GRED(qdisc);
if (isempty(rvalue)) {
gred->grio = -1;
TAKE_PTR(qdisc);
return 0;
}
r = parse_boolean(rvalue);
r = parse_tristate(rvalue, &gred->grio);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=', ignoring assignment: %s",
@ -178,7 +171,6 @@ int config_parse_generic_random_early_detection_bool(
return 0;
}
gred->grio = r;
TAKE_PTR(qdisc);
return 0;

View File

@ -1017,7 +1017,7 @@ int config_parse_tristate(
void *data,
void *userdata) {
int k, *t = ASSERT_PTR(data);
int r, *t = ASSERT_PTR(data);
assert(filename);
assert(lvalue);
@ -1031,14 +1031,13 @@ int config_parse_tristate(
return 0;
}
k = parse_boolean(rvalue);
if (k < 0) {
log_syntax(unit, LOG_WARNING, filename, line, k,
r = parse_tristate(rvalue, t);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse boolean value for %s=, ignoring: %s", lvalue, rvalue);
return 0;
}
*t = k;
return 0;
}

View File

@ -830,14 +830,9 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
break;
case ARG_CHECK_INHIBITORS:
if (streq(optarg, "auto"))
arg_check_inhibitors = -1;
else {
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
arg_check_inhibitors = r;
}
r = parse_tristate_full(optarg, "auto", &arg_check_inhibitors);
if (r < 0)
return log_error_errno(r, "Failed to parse --check-inhibitors= argument: %s", optarg);
break;
case ARG_PLAIN: