1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
7 #include "proc-cmdline.h"
9 #include "string-util.h"
15 static int parse_item(const char *key
, const char *value
, void *data
) {
17 assert_se(data
== &obj
);
19 log_info("kernel cmdline option <%s> = <%s>", key
, strna(value
));
23 static void test_proc_cmdline_parse(void) {
24 log_info("/* %s */", __func__
);
26 assert_se(proc_cmdline_parse(parse_item
, &obj
, PROC_CMDLINE_STRIP_RD_PREFIX
) >= 0);
29 static void test_proc_cmdline_override(void) {
30 log_info("/* %s */", __func__
);
32 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);
33 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=different") == 0);
35 /* First test if the overrides for /proc/cmdline still work */
36 _cleanup_free_
char *line
= NULL
, *value
= NULL
;
37 assert_se(proc_cmdline(&line
) >= 0);
39 /* Test if parsing makes uses of the override */
40 assert_se(streq(line
, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\""));
41 assert_se(proc_cmdline_get_key("foo_bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
44 assert_se(proc_cmdline_get_key("some_arg_with_space", 0, &value
) > 0 && streq_ptr(value
, "foo bar"));
47 assert_se(proc_cmdline_get_key("and_one_more", 0, &value
) > 0 && streq_ptr(value
, "zzz aaa"));
50 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=") == 0);
51 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);
53 assert_se(streq(line
, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\""));
54 assert_se(proc_cmdline_get_key("foo_bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
57 assert_se(proc_cmdline_get_key("some_arg_with_space", 0, &value
) > 0 && streq_ptr(value
, "foo bar"));
60 assert_se(proc_cmdline_get_key("and_one_more", 0, &value
) > 0 && streq_ptr(value
, "zzz aaa"));
64 static int parse_item_given(const char *key
, const char *value
, void *data
) {
70 log_info("%s: option <%s> = <%s>", __func__
, key
, strna(value
));
71 if (proc_cmdline_key_streq(key
, "foo_bar"))
72 assert_se(streq(value
, "quux"));
73 else if (proc_cmdline_key_streq(key
, "wuff-piep"))
74 assert_se(streq(value
, "tuet "));
75 else if (proc_cmdline_key_streq(key
, "space"))
76 assert_se(streq(value
, "x y z"));
77 else if (proc_cmdline_key_streq(key
, "miepf"))
78 assert_se(streq(value
, "uuu"));
79 else if (in_initrd() && *strip
&& proc_cmdline_key_streq(key
, "zumm"))
81 else if (in_initrd() && !*strip
&& proc_cmdline_key_streq(key
, "rd.zumm"))
84 assert_not_reached("Bad key!");
89 static void test_proc_cmdline_given(bool flip_initrd
) {
90 log_info("/* %s (flip: %s) */", __func__
, yes_no(flip_initrd
));
93 in_initrd_force(!in_initrd());
95 bool t
= true, f
= false;
96 assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"",
97 parse_item_given
, &t
, PROC_CMDLINE_STRIP_RD_PREFIX
) >= 0);
99 assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"",
100 parse_item_given
, &f
, 0) >= 0);
103 in_initrd_force(!in_initrd());
106 static void test_proc_cmdline_get_key(void) {
107 _cleanup_free_
char *value
= NULL
;
109 log_info("/* %s */", __func__
);
110 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0);
112 assert_se(proc_cmdline_get_key("", 0, &value
) == -EINVAL
);
113 assert_se(proc_cmdline_get_key("abc", 0, NULL
) == 0);
114 assert_se(proc_cmdline_get_key("abc", 0, &value
) == 0 && value
== NULL
);
115 assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) == 0 && value
== NULL
);
117 assert_se(proc_cmdline_get_key("foo_bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
118 value
= mfree(value
);
119 assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "quux"));
120 value
= mfree(value
);
121 assert_se(proc_cmdline_get_key("foo-bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
122 value
= mfree(value
);
123 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "quux"));
124 value
= mfree(value
);
125 assert_se(proc_cmdline_get_key("foo-bar", 0, NULL
) == 0);
126 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL
, NULL
) == -EINVAL
);
128 assert_se(proc_cmdline_get_key("wuff-piep", 0, &value
) > 0 && streq_ptr(value
, "tuet"));
129 value
= mfree(value
);
130 assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "tuet"));
131 value
= mfree(value
);
132 assert_se(proc_cmdline_get_key("wuff_piep", 0, &value
) > 0 && streq_ptr(value
, "tuet"));
133 value
= mfree(value
);
134 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "tuet"));
135 value
= mfree(value
);
136 assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL
) == 0);
137 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL
, NULL
) == -EINVAL
);
139 assert_se(proc_cmdline_get_key("zumm", 0, &value
) == 0 && value
== NULL
);
140 assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && value
== NULL
);
141 assert_se(proc_cmdline_get_key("zumm", 0, NULL
) > 0);
143 assert_se(proc_cmdline_get_key("spaaace", 0, &value
) > 0 && streq_ptr(value
, "ö ü ß"));
144 value
= mfree(value
);
146 assert_se(proc_cmdline_get_key("ticks", 0, &value
) > 0 && streq_ptr(value
, "''"));
147 value
= mfree(value
);
149 assert_se(proc_cmdline_get_key("kkk", 0, &value
) > 0 && streq_ptr(value
, "uuu"));
152 static void test_proc_cmdline_get_bool(void) {
155 log_info("/* %s */", __func__
);
156 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0);
157 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=") == 0);
159 assert_se(proc_cmdline_get_bool("", &value
) == -EINVAL
);
160 assert_se(proc_cmdline_get_bool("abc", &value
) == 0 && value
== false);
161 assert_se(proc_cmdline_get_bool("foo_bar", &value
) > 0 && value
== true);
162 assert_se(proc_cmdline_get_bool("foo-bar", &value
) > 0 && value
== true);
163 assert_se(proc_cmdline_get_bool("bar-waldo", &value
) > 0 && value
== true);
164 assert_se(proc_cmdline_get_bool("bar_waldo", &value
) > 0 && value
== true);
165 assert_se(proc_cmdline_get_bool("x_y-z", &value
) > 0 && value
== false);
166 assert_se(proc_cmdline_get_bool("x-y-z", &value
) > 0 && value
== false);
167 assert_se(proc_cmdline_get_bool("x-y_z", &value
) > 0 && value
== false);
168 assert_se(proc_cmdline_get_bool("x_y_z", &value
) > 0 && value
== false);
169 assert_se(proc_cmdline_get_bool("quux", &value
) == -EINVAL
&& value
== false);
170 assert_se(proc_cmdline_get_bool("da", &value
) > 0 && value
== true);
171 assert_se(proc_cmdline_get_bool("the", &value
) > 0 && value
== true);
173 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=") == 0);
174 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0);
177 assert_se(proc_cmdline_get_bool("", &value
) == -EINVAL
);
178 assert_se(proc_cmdline_get_bool("abc", &value
) == 0 && value
== false);
179 assert_se(proc_cmdline_get_bool("foo_bar", &value
) > 0 && value
== true);
180 assert_se(proc_cmdline_get_bool("foo-bar", &value
) > 0 && value
== true);
181 assert_se(proc_cmdline_get_bool("bar-waldo", &value
) > 0 && value
== true);
182 assert_se(proc_cmdline_get_bool("bar_waldo", &value
) > 0 && value
== true);
183 assert_se(proc_cmdline_get_bool("x_y-z", &value
) > 0 && value
== false);
184 assert_se(proc_cmdline_get_bool("x-y-z", &value
) > 0 && value
== false);
185 assert_se(proc_cmdline_get_bool("x-y_z", &value
) > 0 && value
== false);
186 assert_se(proc_cmdline_get_bool("x_y_z", &value
) > 0 && value
== false);
187 assert_se(proc_cmdline_get_bool("quux", &value
) == -EINVAL
&& value
== false);
188 assert_se(proc_cmdline_get_bool("da", &value
) > 0 && value
== true);
189 assert_se(proc_cmdline_get_bool("the", &value
) > 0 && value
== true);
193 static void test_proc_cmdline_get_key_many(void) {
194 _cleanup_free_
char *value1
= NULL
, *value2
= NULL
, *value3
= NULL
, *value4
= NULL
, *value5
= NULL
, *value6
= NULL
, *value7
= NULL
;
196 log_info("/* %s */", __func__
);
197 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm SPACE='one two' doubleticks=\" aaa aaa \"\n\nzummm='\n'\n") == 0);
199 assert_se(proc_cmdline_get_key_many(0,
200 "wuff-piep", &value3
,
202 "idontexist", &value2
,
205 "doubleticks", &value6
,
206 "zummm", &value7
) == 5);
208 assert_se(streq_ptr(value1
, "quux"));
210 assert_se(streq_ptr(value3
, "tuet"));
212 assert_se(streq_ptr(value5
, "one two"));
213 assert_se(streq_ptr(value6
, " aaa aaa "));
214 assert_se(streq_ptr(value7
, "\n"));
217 static void test_proc_cmdline_key_streq(void) {
218 log_info("/* %s */", __func__
);
220 assert_se(proc_cmdline_key_streq("", ""));
221 assert_se(proc_cmdline_key_streq("a", "a"));
222 assert_se(!proc_cmdline_key_streq("", "a"));
223 assert_se(!proc_cmdline_key_streq("a", ""));
224 assert_se(proc_cmdline_key_streq("a", "a"));
225 assert_se(!proc_cmdline_key_streq("a", "b"));
226 assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
227 assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
228 assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z"));
229 assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z"));
230 assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z"));
231 assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z"));
234 static void test_proc_cmdline_key_startswith(void) {
235 log_info("/* %s */", __func__
);
237 assert_se(proc_cmdline_key_startswith("", ""));
238 assert_se(proc_cmdline_key_startswith("x", ""));
239 assert_se(!proc_cmdline_key_startswith("", "x"));
240 assert_se(proc_cmdline_key_startswith("x", "x"));
241 assert_se(!proc_cmdline_key_startswith("x", "y"));
242 assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
243 assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
244 assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
245 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
246 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
247 assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
251 test_setup_logging(LOG_INFO
);
253 test_proc_cmdline_parse();
254 test_proc_cmdline_override();
255 test_proc_cmdline_given(false);
256 /* Repeat the same thing, but now flip our ininitrdness */
257 test_proc_cmdline_given(true);
258 test_proc_cmdline_key_streq();
259 test_proc_cmdline_key_startswith();
260 test_proc_cmdline_get_key();
261 test_proc_cmdline_get_bool();
262 test_proc_cmdline_get_key_many();