From a175b98171885c255127ac239da2a361be1f9832 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 Mar 2023 09:21:28 +0900 Subject: [PATCH 1/5] proc-cmdline: insert an empty line between variable declaration and assertion --- src/basic/proc-cmdline.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c index eea70d8606..0e48b24a86 100644 --- a/src/basic/proc-cmdline.c +++ b/src/basic/proc-cmdline.c @@ -18,6 +18,7 @@ int proc_cmdline(char **ret) { const char *e; + assert(ret); /* For testing purposes it is sometimes useful to be able to override what we consider /proc/cmdline to be */ From b3e9a267ce2b1bc76a0db9a9a2cff25128d824e7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 Mar 2023 04:32:23 +0900 Subject: [PATCH 2/5] proc-cmdline: rename variable --- src/basic/proc-cmdline.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c index 0e48b24a86..1def562e18 100644 --- a/src/basic/proc-cmdline.c +++ b/src/basic/proc-cmdline.c @@ -174,7 +174,7 @@ bool proc_cmdline_key_streq(const char *x, const char *y) { } static int cmdline_get_key(const char *line, const char *key, ProcCmdlineFlags flags, char **ret_value) { - _cleanup_free_ char *ret = NULL; + _cleanup_free_ char *v = NULL; bool found = false; const char *p; int r; @@ -200,7 +200,7 @@ static int cmdline_get_key(const char *line, const char *key, ProcCmdlineFlags f continue; if (*e == '=') { - r = free_and_strdup(&ret, e+1); + r = free_and_strdup(&v, e+1); if (r < 0) return r; @@ -218,7 +218,7 @@ static int cmdline_get_key(const char *line, const char *key, ProcCmdlineFlags f } if (ret_value) - *ret_value = TAKE_PTR(ret); + *ret_value = TAKE_PTR(v); return found; } From 1479799c4dc0c9cb18508fa784ea218c27f3bcc9 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 Mar 2023 03:06:16 +0900 Subject: [PATCH 3/5] proc-cmdline: use proc_cmdline_key_string() when we search for key --- src/basic/proc-cmdline.c | 2 +- src/test/test-proc-cmdline.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c index 1def562e18..4f4ea869bf 100644 --- a/src/basic/proc-cmdline.c +++ b/src/basic/proc-cmdline.c @@ -210,7 +210,7 @@ static int cmdline_get_key(const char *line, const char *key, ProcCmdlineFlags f found = true; } else { - if (streq(word, key)) { + if (proc_cmdline_key_streq(word, key)) { found = true; break; /* we found what we were looking for */ } diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index 1d54066ae6..b576c46c23 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -109,7 +109,7 @@ TEST(test_proc_cmdline_given) { TEST(proc_cmdline_get_key) { _cleanup_free_ char *value = NULL; - assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0); + assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm-ghh spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0); assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL); assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0); @@ -120,6 +120,7 @@ TEST(proc_cmdline_get_key) { value = mfree(value); assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux")); value = mfree(value); + assert_se(proc_cmdline_get_key("foo_bar", 0, NULL) == 0); assert_se(proc_cmdline_get_key("foo-bar", 0, &value) > 0 && streq_ptr(value, "quux")); value = mfree(value); assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux")); @@ -138,9 +139,12 @@ TEST(proc_cmdline_get_key) { assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0); assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL); - assert_se(proc_cmdline_get_key("zumm", 0, &value) == 0 && value == NULL); - assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL); - assert_se(proc_cmdline_get_key("zumm", 0, NULL) > 0); + assert_se(proc_cmdline_get_key("zumm-ghh", 0, &value) == 0 && value == NULL); + assert_se(proc_cmdline_get_key("zumm-ghh", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL); + assert_se(proc_cmdline_get_key("zumm-ghh", 0, NULL) > 0); + assert_se(proc_cmdline_get_key("zumm_ghh", 0, &value) == 0 && value == NULL); + assert_se(proc_cmdline_get_key("zumm_ghh", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL); + assert_se(proc_cmdline_get_key("zumm_ghh", 0, NULL) > 0); assert_se(proc_cmdline_get_key("spaaace", 0, &value) > 0 && streq_ptr(value, "ö ü ß")); value = mfree(value); From 2a6911bb1f8e2dbc0259957a8c9e79cc48399f88 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 Mar 2023 04:55:59 +0900 Subject: [PATCH 4/5] proc-cmdline: make proc_cmdline_parse_given() static It is used only in proc-cmdline.c and its test. And the test can be covered by proc_cmdline_parse(). --- src/basic/proc-cmdline.c | 2 +- src/basic/proc-cmdline.h | 1 - src/test/test-proc-cmdline.c | 10 +++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c index 4f4ea869bf..de1f66635a 100644 --- a/src/basic/proc-cmdline.c +++ b/src/basic/proc-cmdline.c @@ -79,7 +79,7 @@ static int proc_cmdline_extract_first(const char **p, char **ret_word, ProcCmdli return 0; } -int proc_cmdline_parse_given(const char *line, proc_cmdline_parse_t parse_item, void *data, ProcCmdlineFlags flags) { +static int proc_cmdline_parse_given(const char *line, proc_cmdline_parse_t parse_item, void *data, ProcCmdlineFlags flags) { const char *p; int r; diff --git a/src/basic/proc-cmdline.h b/src/basic/proc-cmdline.h index 45f3a27f27..8650e293ce 100644 --- a/src/basic/proc-cmdline.h +++ b/src/basic/proc-cmdline.h @@ -16,7 +16,6 @@ typedef int (*proc_cmdline_parse_t)(const char *key, const char *value, void *da int proc_cmdline(char **ret); -int proc_cmdline_parse_given(const char *line, proc_cmdline_parse_t parse_item, void *data, ProcCmdlineFlags flags); int proc_cmdline_parse(const proc_cmdline_parse_t parse, void *userdata, ProcCmdlineFlags flags); int proc_cmdline_get_key(const char *parameter, ProcCmdlineFlags flags, char **value); diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index b576c46c23..37076c8adb 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -90,17 +90,17 @@ static void test_proc_cmdline_given_one(bool flip_initrd) { in_initrd_force(!in_initrd()); bool t = true, f = false; - assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"", - parse_item_given, &t, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); - - assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"", - parse_item_given, &f, 0) >= 0); + assert_se(proc_cmdline_parse(parse_item_given, &t, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); + assert_se(proc_cmdline_parse(parse_item_given, &f, 0) >= 0); if (flip_initrd) in_initrd_force(!in_initrd()); } TEST(test_proc_cmdline_given) { + assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"") == 0); + assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=miepf=\"uuu\"") == 0); + test_proc_cmdline_given_one(false); /* Repeat the same thing, but now flip our ininitrdness */ test_proc_cmdline_given_one(true); From a3cb11f841ccaa456fdf017095d95947bc29af11 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 23 Mar 2023 05:52:32 +0900 Subject: [PATCH 5/5] test-proc-cmdline: test proc_cmdline_get_key() actually parses EFI options Follow-up for 53aa0d02add93d8c0afa2772609372a2040c162a. --- src/test/test-proc-cmdline.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index 37076c8adb..7f8330cc24 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -26,15 +26,17 @@ TEST(proc_cmdline_parse) { } TEST(proc_cmdline_override) { + _cleanup_free_ char *line = NULL, *value = NULL; + assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"") == 0); assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=different") == 0); /* First test if the overrides for /proc/cmdline still work */ - _cleanup_free_ char *line = NULL, *value = NULL; assert_se(proc_cmdline(&line) >= 0); + assert_se(streq(line, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"")); + line = mfree(line); /* Test if parsing makes uses of the override */ - assert_se(streq(line, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"")); assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux")); value = mfree(value); @@ -44,10 +46,13 @@ TEST(proc_cmdline_override) { assert_se(proc_cmdline_get_key("and_one_more", 0, &value) > 0 && streq_ptr(value, "zzz aaa")); value = mfree(value); - assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=") == 0); - assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"") == 0); + assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=hoge") == 0); + assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"") == 0); + + assert_se(proc_cmdline(&line) >= 0); + assert_se(streq(line, "hoge")); + line = mfree(line); - assert_se(streq(line, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"")); assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux")); value = mfree(value);