1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
7 #include "proc-cmdline.h"
9 #include "string-util.h"
14 static int parse_item(const char *key
, const char *value
, void *data
) {
16 assert_se(data
== &obj
);
18 log_info("kernel cmdline option <%s> = <%s>", key
, strna(value
));
22 static void test_proc_cmdline_parse(void) {
23 log_info("/* %s */", __func__
);
25 assert_se(proc_cmdline_parse(parse_item
, &obj
, PROC_CMDLINE_STRIP_RD_PREFIX
) >= 0);
28 static void test_proc_cmdline_override(void) {
29 log_info("/* %s */", __func__
);
31 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 /* Test if the override works */
34 _cleanup_free_
char *line
= NULL
, *value
= NULL
;
35 assert_se(proc_cmdline(&line
) >= 0);
37 /* Test if parsing makes uses of the override */
38 assert_se(streq(line
, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\""));
39 assert_se(proc_cmdline_get_key("foo_bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
42 assert_se(proc_cmdline_get_key("some_arg_with_space", 0, &value
) > 0 && streq_ptr(value
, "foo bar"));
45 assert_se(proc_cmdline_get_key("and_one_more", 0, &value
) > 0 && streq_ptr(value
, "zzz aaa"));
49 static int parse_item_given(const char *key
, const char *value
, void *data
) {
55 log_info("%s: option <%s> = <%s>", __func__
, key
, strna(value
));
56 if (proc_cmdline_key_streq(key
, "foo_bar"))
57 assert_se(streq(value
, "quux"));
58 else if (proc_cmdline_key_streq(key
, "wuff-piep"))
59 assert_se(streq(value
, "tuet "));
60 else if (proc_cmdline_key_streq(key
, "space"))
61 assert_se(streq(value
, "x y z"));
62 else if (proc_cmdline_key_streq(key
, "miepf"))
63 assert_se(streq(value
, "uuu"));
64 else if (in_initrd() && *strip
&& proc_cmdline_key_streq(key
, "zumm"))
66 else if (in_initrd() && !*strip
&& proc_cmdline_key_streq(key
, "rd.zumm"))
69 assert_not_reached("Bad key!");
74 static void test_proc_cmdline_given(bool flip_initrd
) {
75 log_info("/* %s (flip: %s) */", __func__
, yes_no(flip_initrd
));
78 in_initrd_force(!in_initrd());
80 bool t
= true, f
= false;
81 assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"",
82 parse_item_given
, &t
, PROC_CMDLINE_STRIP_RD_PREFIX
) >= 0);
84 assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"",
85 parse_item_given
, &f
, 0) >= 0);
88 in_initrd_force(!in_initrd());
91 static void test_proc_cmdline_get_key(void) {
92 _cleanup_free_
char *value
= NULL
;
94 log_info("/* %s */", __func__
);
95 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0);
97 assert_se(proc_cmdline_get_key("", 0, &value
) == -EINVAL
);
98 assert_se(proc_cmdline_get_key("abc", 0, NULL
) == 0);
99 assert_se(proc_cmdline_get_key("abc", 0, &value
) == 0 && value
== NULL
);
100 assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) == 0 && value
== NULL
);
102 assert_se(proc_cmdline_get_key("foo_bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
103 value
= mfree(value
);
104 assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "quux"));
105 value
= mfree(value
);
106 assert_se(proc_cmdline_get_key("foo-bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
107 value
= mfree(value
);
108 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "quux"));
109 value
= mfree(value
);
110 assert_se(proc_cmdline_get_key("foo-bar", 0, NULL
) == 0);
111 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL
, NULL
) == -EINVAL
);
113 assert_se(proc_cmdline_get_key("wuff-piep", 0, &value
) > 0 && streq_ptr(value
, "tuet"));
114 value
= mfree(value
);
115 assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "tuet"));
116 value
= mfree(value
);
117 assert_se(proc_cmdline_get_key("wuff_piep", 0, &value
) > 0 && streq_ptr(value
, "tuet"));
118 value
= mfree(value
);
119 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "tuet"));
120 value
= mfree(value
);
121 assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL
) == 0);
122 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL
, NULL
) == -EINVAL
);
124 assert_se(proc_cmdline_get_key("zumm", 0, &value
) == 0 && value
== NULL
);
125 assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && value
== NULL
);
126 assert_se(proc_cmdline_get_key("zumm", 0, NULL
) > 0);
128 assert_se(proc_cmdline_get_key("spaaace", 0, &value
) > 0 && streq_ptr(value
, "ö ü ß"));
129 value
= mfree(value
);
131 assert_se(proc_cmdline_get_key("ticks", 0, &value
) > 0 && streq_ptr(value
, "''"));
132 value
= mfree(value
);
134 assert_se(proc_cmdline_get_key("kkk", 0, &value
) > 0 && streq_ptr(value
, "uuu"));
137 static void test_proc_cmdline_get_bool(void) {
140 log_info("/* %s */", __func__
);
141 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0);
143 assert_se(proc_cmdline_get_bool("", &value
) == -EINVAL
);
144 assert_se(proc_cmdline_get_bool("abc", &value
) == 0 && value
== false);
145 assert_se(proc_cmdline_get_bool("foo_bar", &value
) > 0 && value
== true);
146 assert_se(proc_cmdline_get_bool("foo-bar", &value
) > 0 && value
== true);
147 assert_se(proc_cmdline_get_bool("bar-waldo", &value
) > 0 && value
== true);
148 assert_se(proc_cmdline_get_bool("bar_waldo", &value
) > 0 && value
== true);
149 assert_se(proc_cmdline_get_bool("x_y-z", &value
) > 0 && value
== false);
150 assert_se(proc_cmdline_get_bool("x-y-z", &value
) > 0 && value
== false);
151 assert_se(proc_cmdline_get_bool("x-y_z", &value
) > 0 && value
== false);
152 assert_se(proc_cmdline_get_bool("x_y_z", &value
) > 0 && value
== false);
153 assert_se(proc_cmdline_get_bool("quux", &value
) == -EINVAL
&& value
== false);
154 assert_se(proc_cmdline_get_bool("da", &value
) > 0 && value
== true);
155 assert_se(proc_cmdline_get_bool("the", &value
) > 0 && value
== true);
158 static void test_proc_cmdline_get_key_many(void) {
159 _cleanup_free_
char *value1
= NULL
, *value2
= NULL
, *value3
= NULL
, *value4
= NULL
, *value5
= NULL
, *value6
= NULL
, *value7
= NULL
;
161 log_info("/* %s */", __func__
);
162 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);
164 assert_se(proc_cmdline_get_key_many(0,
165 "wuff-piep", &value3
,
167 "idontexist", &value2
,
170 "doubleticks", &value6
,
171 "zummm", &value7
) == 5);
174 assert_se(streq_ptr(value1
, "quux"));
176 assert_se(streq_ptr(value3
, "tuet"));
178 assert_se(streq_ptr(value5
, "one two"));
179 assert_se(streq_ptr(value6
, " aaa aaa "));
180 assert_se(streq_ptr(value7
, "\n"));
183 static void test_proc_cmdline_key_streq(void) {
184 log_info("/* %s */", __func__
);
186 assert_se(proc_cmdline_key_streq("", ""));
187 assert_se(proc_cmdline_key_streq("a", "a"));
188 assert_se(!proc_cmdline_key_streq("", "a"));
189 assert_se(!proc_cmdline_key_streq("a", ""));
190 assert_se(proc_cmdline_key_streq("a", "a"));
191 assert_se(!proc_cmdline_key_streq("a", "b"));
192 assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
193 assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
194 assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z"));
195 assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z"));
196 assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z"));
197 assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z"));
200 static void test_proc_cmdline_key_startswith(void) {
201 log_info("/* %s */", __func__
);
203 assert_se(proc_cmdline_key_startswith("", ""));
204 assert_se(proc_cmdline_key_startswith("x", ""));
205 assert_se(!proc_cmdline_key_startswith("", "x"));
206 assert_se(proc_cmdline_key_startswith("x", "x"));
207 assert_se(!proc_cmdline_key_startswith("x", "y"));
208 assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
209 assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
210 assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
211 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
212 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
213 assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
216 static void test_runlevel_to_target(void) {
217 log_info("/* %s */", __func__
);
219 in_initrd_force(false);
220 assert_se(streq_ptr(runlevel_to_target(NULL
), NULL
));
221 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL
));
222 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL
));
223 assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET
));
224 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL
));
226 in_initrd_force(true);
227 assert_se(streq_ptr(runlevel_to_target(NULL
), NULL
));
228 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL
));
229 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL
));
230 assert_se(streq_ptr(runlevel_to_target("3"), NULL
));
231 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET
));
235 log_parse_environment();
238 test_proc_cmdline_parse();
239 test_proc_cmdline_override();
240 test_proc_cmdline_given(false);
241 /* Repeat the same thing, but now flip our ininitrdness */
242 test_proc_cmdline_given(true);
243 test_proc_cmdline_key_streq();
244 test_proc_cmdline_key_startswith();
245 test_proc_cmdline_get_key();
246 test_proc_cmdline_get_bool();
247 test_proc_cmdline_get_key_many();
248 test_runlevel_to_target();