]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
proc-cmdline: introduce PROC_CMDLINE_TRUE_WHEN_MISSING
authorMike Yuan <me@yhndnzj.com>
Sat, 12 Aug 2023 07:20:59 +0000 (15:20 +0800)
committerMike Yuan <me@yhndnzj.com>
Sun, 13 Aug 2023 04:52:15 +0000 (12:52 +0800)
src/basic/proc-cmdline.c
src/basic/proc-cmdline.h
src/test/test-proc-cmdline.c

index 95130a36be33153dfb449cf718031c2076c1cd9e..c6667366274cde0797cd1b64ad8b0c42ae396a2f 100644 (file)
@@ -197,9 +197,9 @@ static int proc_cmdline_parse_strv(char **args, proc_cmdline_parse_t parse_item,
 
         assert(parse_item);
 
-        /* The PROC_CMDLINE_VALUE_OPTIONAL flag doesn't really make sense for proc_cmdline_parse(), let's
-         * make this clear. */
-        assert(!FLAGS_SET(flags, PROC_CMDLINE_VALUE_OPTIONAL));
+        /* The PROC_CMDLINE_VALUE_OPTIONAL and PROC_CMDLINE_TRUE_WHEN_MISSING flags don't really make sense
+         * for proc_cmdline_parse(), let's make this clear. */
+        assert(!(flags & (PROC_CMDLINE_VALUE_OPTIONAL|PROC_CMDLINE_TRUE_WHEN_MISSING)));
 
         STRV_FOREACH(word, args) {
                 char *key, *value;
@@ -351,6 +351,9 @@ int proc_cmdline_get_key(const char *key, ProcCmdlineFlags flags, char **ret_val
          *
          * In all three cases, > 0 is returned if the key is found, 0 if not. */
 
+        /* PROC_CMDLINE_TRUE_WHEN_MISSING doesn't really make sense for proc_cmdline_get_key(). */
+        assert(!FLAGS_SET(flags, PROC_CMDLINE_TRUE_WHEN_MISSING));
+
         if (isempty(key))
                 return -EINVAL;
 
@@ -398,11 +401,11 @@ int proc_cmdline_get_bool(const char *key, ProcCmdlineFlags flags, bool *ret) {
 
         assert(ret);
 
-        r = proc_cmdline_get_key(key, flags | PROC_CMDLINE_VALUE_OPTIONAL, &v);
+        r = proc_cmdline_get_key(key, (flags & ~PROC_CMDLINE_TRUE_WHEN_MISSING) | PROC_CMDLINE_VALUE_OPTIONAL, &v);
         if (r < 0)
                 return r;
         if (r == 0) { /* key not specified at all */
-                *ret = false;
+                *ret = FLAGS_SET(flags, PROC_CMDLINE_TRUE_WHEN_MISSING);
                 return 0;
         }
 
@@ -456,9 +459,9 @@ int proc_cmdline_get_key_many_internal(ProcCmdlineFlags flags, ...) {
         int r, ret = 0;
         va_list ap;
 
-        /* The PROC_CMDLINE_VALUE_OPTIONAL flag doesn't really make sense for proc_cmdline_get_key_many(), let's make
-         * this clear. */
-        assert(!FLAGS_SET(flags, PROC_CMDLINE_VALUE_OPTIONAL));
+        /* The PROC_CMDLINE_VALUE_OPTIONAL and PROC_CMDLINE_TRUE_WHEN_MISSING flags don't really make sense
+         * for proc_cmdline_get_key_many, let's make this clear. */
+        assert(!(flags & (PROC_CMDLINE_VALUE_OPTIONAL|PROC_CMDLINE_TRUE_WHEN_MISSING)));
 
         /* This call may clobber arguments on failure! */
 
index babdcbeeaac41381ba94b67ca2042249aaf0c3fc..9502fb8d4380dab0772a1733c81323fdd611e1d7 100644 (file)
@@ -10,6 +10,7 @@ typedef enum ProcCmdlineFlags {
         PROC_CMDLINE_VALUE_OPTIONAL     = 1 << 1, /* the value is optional (for boolean switches that can omit the value) */
         PROC_CMDLINE_RD_STRICT          = 1 << 2, /* ignore this in the initrd */
         PROC_CMDLINE_IGNORE_EFI_OPTIONS = 1 << 3, /* don't check systemd's private EFI variable */
+        PROC_CMDLINE_TRUE_WHEN_MISSING  = 1 << 4, /* default to true when the key is missing for bool */
 } ProcCmdlineFlags;
 
 typedef int (*proc_cmdline_parse_t)(const char *key, const char *value, void *data);
index 3614ebe6f4382553aca2e56a198df94ee1912ed3..9920462ad53e27d7a38e9c720216370b8bc46235 100644 (file)
@@ -180,6 +180,7 @@ TEST(proc_cmdline_get_bool) {
 
         assert_se(proc_cmdline_get_bool("", /* flags = */ 0, &value) == -EINVAL);
         assert_se(proc_cmdline_get_bool("abc", /* flags = */ 0, &value) == 0 && value == false);
+        assert_se(proc_cmdline_get_bool("unspecified", PROC_CMDLINE_TRUE_WHEN_MISSING, &value) == 0 && value == true);
         assert_se(proc_cmdline_get_bool("foo_bar", /* flags = */ 0, &value) > 0 && value == true);
         assert_se(proc_cmdline_get_bool("foo-bar", /* flags = */ 0, &value) > 0 && value == true);
         assert_se(proc_cmdline_get_bool("bar-waldo", /* flags = */ 0, &value) > 0 && value == true);