1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-08 08:58:27 +03:00

Merge pull request #26957 from yuwata/proc-cmdline-cleanups

proc-cmdline: several cleanups
This commit is contained in:
Daan De Meyer 2023-03-24 11:29:54 +01:00 committed by GitHub
commit d258aacdfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 20 deletions

View File

@ -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 */
@ -78,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;
@ -173,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;
@ -199,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;
@ -209,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 */
}
@ -217,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;
}

View File

@ -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);

View File

@ -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);
@ -90,17 +95,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);
@ -109,7 +114,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 +125,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 +144,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);