]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
parse-util: add parse_tristate() and use it everywhere
authorLennart Poettering <lennart@poettering.net>
Wed, 1 Nov 2023 15:48:42 +0000 (16:48 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 1 Nov 2023 19:52:16 +0000 (04:52 +0900)
We parse tristates all the time, let's add an explicit parser for them.

src/basic/parse-util.c
src/basic/parse-util.h
src/network/netdev/macsec.c
src/network/networkd-nexthop.c
src/network/tc/cake.c
src/network/tc/codel.c
src/network/tc/fq-codel.c
src/network/tc/fq.c
src/network/tc/gred.c
src/shared/conf-parser.c
src/systemctl/systemctl.c

index d39082ed15520e36e189824d8b40aa49dc8d643d..dc868c9b8e9acfff9f7f7c0d94deb3ea584e6dd5 100644 (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;
index e586077efb59afb1f0dea49dedc51a93e4ea67ff..1845f0a876f1aa00ef92aff9eac5b2b13e198d4c 100644 (file)
 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);
index 98927b168dab3db1bd3fe83da05df70e659e2fe9..17d6acefb6662f3a8b0b5daeb6cdd0f60180e583 100644 (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;
 
index 2af65ec260e539aae748669ab66de173ff9e3a20..e2ded28197bc81b2fabe75b69efda24e04781942 100644 (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;
 }
index a22a11cce3be61b7dfd2436ac418fc71cc5fe5f8..c495fafda4cc85678416d7b80a75a5f113d648fb 100644 (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;
 }
index b5f95f7ed2a4885e7b1cc1ed6db733609178c592..e21252394c03e0b936871c7d4c98c089c589dc1d 100644 (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;
 }
index 50ec203e5bd75364b6aabc5565c9c89b37a33426..124faf73e72a4d9afae90a596c0501259293349a 100644 (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;
index 149a72e37c4156dd7b0a31179d0f83af023a72e4..74785c980ae4b932aa0ccd63b7f2b7ba1c0c19aa 100644 (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;
 }
index 476122c844dc9f8a1f1b87424ad8148708f65471..2efb02c345f044e27434a7746f0badeb4d0036ab 100644 (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;
index a60588ad9ee74b704dc98e79fa41b0696118b1f6..1b7d2fdb4d4cc9fa78abc2076f1910853f780406 100644 (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;
 }
 
index 70b8c16249413ebcdf512b5a0271f74d8f77b9a8..dd6f6c9873e25e611ab703928c7ea7c79fb48c96 100644 (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: