From: Lennart Poettering Date: Wed, 1 Nov 2023 15:48:42 +0000 (+0100) Subject: parse-util: add parse_tristate() and use it everywhere X-Git-Tag: v255-rc1~53 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b71a721fbc5bd4e7d45fd39c0af79e985c9c6b22;p=thirdparty%2Fsystemd.git parse-util: add parse_tristate() and use it everywhere We parse tristates all the time, let's add an explicit parser for them. --- diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index d39082ed155..dc868c9b8e9 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -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; diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h index e586077efb5..1845f0a876f 100644 --- a/src/basic/parse-util.h +++ b/src/basic/parse-util.h @@ -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); diff --git a/src/network/netdev/macsec.c b/src/network/netdev/macsec.c index 98927b168da..17d6acefb66 100644 --- a/src/network/netdev/macsec.c +++ b/src/network/netdev/macsec.c @@ -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; diff --git a/src/network/networkd-nexthop.c b/src/network/networkd-nexthop.c index 2af65ec260e..e2ded28197b 100644 --- a/src/network/networkd-nexthop.c +++ b/src/network/networkd-nexthop.c @@ -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; } diff --git a/src/network/tc/cake.c b/src/network/tc/cake.c index a22a11cce3b..c495fafda4c 100644 --- a/src/network/tc/cake.c +++ b/src/network/tc/cake.c @@ -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; } diff --git a/src/network/tc/codel.c b/src/network/tc/codel.c index b5f95f7ed2a..e21252394c0 100644 --- a/src/network/tc/codel.c +++ b/src/network/tc/codel.c @@ -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; } diff --git a/src/network/tc/fq-codel.c b/src/network/tc/fq-codel.c index 50ec203e5bd..124faf73e72 100644 --- a/src/network/tc/fq-codel.c +++ b/src/network/tc/fq-codel.c @@ -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; diff --git a/src/network/tc/fq.c b/src/network/tc/fq.c index 149a72e37c4..74785c980ae 100644 --- a/src/network/tc/fq.c +++ b/src/network/tc/fq.c @@ -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; } diff --git a/src/network/tc/gred.c b/src/network/tc/gred.c index 476122c844d..2efb02c345f 100644 --- a/src/network/tc/gred.c +++ b/src/network/tc/gred.c @@ -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; diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index a60588ad9ee..1b7d2fdb4d4 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -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; } diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 70b8c162494..dd6f6c9873e 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -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: