| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "initrd-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "nulstr-util.h" |
| 11 |
#include "proc-cmdline.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "strv.h" |
| 14 |
#include "tests.h" |
| 15 |
|
| 16 |
static int obj; |
| 17 |
|
| 18 |
static int parse_item(const char *key, const char *value, void *data) { |
| 19 |
assert_se(key); |
| 20 |
assert_se(data == &obj); |
| 21 |
|
| 22 |
log_info("kernel cmdline option <%s> = <%s>", key, strna(value)); |
| 23 |
return 0; |
| 24 |
} |
| 25 |
|
| 26 |
TEST(proc_cmdline_parse) { |
| 27 |
assert_se(proc_cmdline_parse(parse_item, &obj, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); |
| 28 |
} |
| 29 |
|
| 30 |
TEST(proc_cmdline_override) { |
| 31 |
_cleanup_free_ char *line = NULL, *value = NULL; |
| 32 |
_cleanup_strv_free_ char **args = NULL; |
| 33 |
|
| 34 |
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); |
| 35 |
|
| 36 |
/* First test if the overrides for /proc/cmdline still work */ |
| 37 |
assert_se(proc_cmdline(&line) >= 0); |
| 38 |
ASSERT_STREQ(line, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\""); |
| 39 |
line = mfree(line); |
| 40 |
assert_se(proc_cmdline_strv(&args) >= 0); |
| 41 |
assert_se(strv_equal(args, STRV_MAKE("foo_bar=quux", "wuff-piep=tuet", "zumm", "some_arg_with_space=foo bar", "and_one_more=zzz aaa"))); |
| 42 |
args = strv_free(args); |
| 43 |
|
| 44 |
/* Test if parsing makes uses of the override */ |
| 45 |
assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux")); |
| 46 |
value = mfree(value); |
| 47 |
|
| 48 |
assert_se(proc_cmdline_get_key("some_arg_with_space", 0, &value) > 0 && streq_ptr(value, "foo bar")); |
| 49 |
value = mfree(value); |
| 50 |
|
| 51 |
assert_se(proc_cmdline_get_key("and_one_more", 0, &value) > 0 && streq_ptr(value, "zzz aaa")); |
| 52 |
value = mfree(value); |
| 53 |
|
| 54 |
assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=hoge") == 0); |
| 55 |
|
| 56 |
assert_se(proc_cmdline(&line) >= 0); |
| 57 |
ASSERT_STREQ(line, "hoge"); |
| 58 |
line = mfree(line); |
| 59 |
assert_se(proc_cmdline_strv(&args) >= 0); |
| 60 |
assert_se(strv_equal(args, STRV_MAKE("hoge"))); |
| 61 |
args = strv_free(args); |
| 62 |
} |
| 63 |
|
| 64 |
static int parse_item_given(const char *key, const char *value, void *data) { |
| 65 |
assert_se(key); |
| 66 |
assert_se(data); |
| 67 |
|
| 68 |
bool *strip = data; |
| 69 |
|
| 70 |
log_info("%s: option <%s> = <%s>", __func__, key, strna(value)); |
| 71 |
if (proc_cmdline_key_streq(key, "foo_bar")) |
| 72 |
ASSERT_STREQ(value, "quux"); |
| 73 |
else if (proc_cmdline_key_streq(key, "wuff-piep")) |
| 74 |
ASSERT_STREQ(value, "tuet "); |
| 75 |
else if (proc_cmdline_key_streq(key, "space")) |
| 76 |
ASSERT_STREQ(value, "x y z"); |
| 77 |
else if (proc_cmdline_key_streq(key, "miepf")) |
| 78 |
ASSERT_STREQ(value, "uuu"); |
| 79 |
else if (in_initrd() && *strip && proc_cmdline_key_streq(key, "zumm")) |
| 80 |
assert_se(!value); |
| 81 |
else if (in_initrd() && !*strip && proc_cmdline_key_streq(key, "rd.zumm")) |
| 82 |
assert_se(!value); |
| 83 |
else |
| 84 |
assert_not_reached(); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static void test_proc_cmdline_given_one(bool flip_initrd) { |
| 90 |
log_info("/* %s (flip: %s) */", __func__, yes_no(flip_initrd)); |
| 91 |
|
| 92 |
if (flip_initrd) |
| 93 |
in_initrd_force(!in_initrd()); |
| 94 |
|
| 95 |
bool t = true, f = false; |
| 96 |
assert_se(proc_cmdline_parse(parse_item_given, &t, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); |
| 97 |
assert_se(proc_cmdline_parse(parse_item_given, &f, 0) >= 0); |
| 98 |
|
| 99 |
if (flip_initrd) |
| 100 |
in_initrd_force(!in_initrd()); |
| 101 |
} |
| 102 |
|
| 103 |
TEST(proc_cmdline_given) { |
| 104 |
assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"") == 0); |
| 105 |
|
| 106 |
test_proc_cmdline_given_one(false); |
| 107 |
/* Repeat the same thing, but now flip our ininitrdness */ |
| 108 |
test_proc_cmdline_given_one(true); |
| 109 |
} |
| 110 |
|
| 111 |
TEST(proc_cmdline_get_key) { |
| 112 |
_cleanup_free_ char *value = NULL; |
| 113 |
|
| 114 |
assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm-ghh spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0); |
| 115 |
|
| 116 |
assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL); |
| 117 |
assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0); |
| 118 |
assert_se(proc_cmdline_get_key("abc", 0, &value) == 0 && value == NULL); |
| 119 |
assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL, &value) == 0 && value == NULL); |
| 120 |
|
| 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", 0, &value) > 0 && streq_ptr(value, "quux")); |
| 127 |
value = mfree(value); |
| 128 |
assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux")); |
| 129 |
value = mfree(value); |
| 130 |
assert_se(proc_cmdline_get_key("foo-bar", 0, NULL) == 0); |
| 131 |
assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL); |
| 132 |
|
| 133 |
assert_se(proc_cmdline_get_key("wuff-piep", 0, &value) > 0 && streq_ptr(value, "tuet")); |
| 134 |
value = mfree(value); |
| 135 |
assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet")); |
| 136 |
value = mfree(value); |
| 137 |
assert_se(proc_cmdline_get_key("wuff_piep", 0, &value) > 0 && streq_ptr(value, "tuet")); |
| 138 |
value = mfree(value); |
| 139 |
assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet")); |
| 140 |
value = mfree(value); |
| 141 |
assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0); |
| 142 |
assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL); |
| 143 |
|
| 144 |
assert_se(proc_cmdline_get_key("zumm-ghh", 0, &value) == 0 && value == NULL); |
| 145 |
assert_se(proc_cmdline_get_key("zumm-ghh", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL); |
| 146 |
assert_se(proc_cmdline_get_key("zumm-ghh", 0, NULL) > 0); |
| 147 |
assert_se(proc_cmdline_get_key("zumm_ghh", 0, &value) == 0 && value == NULL); |
| 148 |
assert_se(proc_cmdline_get_key("zumm_ghh", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL); |
| 149 |
assert_se(proc_cmdline_get_key("zumm_ghh", 0, NULL) > 0); |
| 150 |
|
| 151 |
assert_se(proc_cmdline_get_key("spaaace", 0, &value) > 0 && streq_ptr(value, "ö ü ß")); |
| 152 |
value = mfree(value); |
| 153 |
|
| 154 |
assert_se(proc_cmdline_get_key("ticks", 0, &value) > 0 && streq_ptr(value, "''")); |
| 155 |
value = mfree(value); |
| 156 |
|
| 157 |
assert_se(proc_cmdline_get_key("kkk", 0, &value) > 0 && streq_ptr(value, "uuu")); |
| 158 |
} |
| 159 |
|
| 160 |
TEST(proc_cmdline_get_bool) { |
| 161 |
bool value = false; |
| 162 |
|
| 163 |
assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0); |
| 164 |
|
| 165 |
assert_se(proc_cmdline_get_bool("", /* flags= */ 0, &value) == -EINVAL); |
| 166 |
assert_se(proc_cmdline_get_bool("abc", /* flags= */ 0, &value) == 0 && value == false); |
| 167 |
assert_se(proc_cmdline_get_bool("unspecified", PROC_CMDLINE_TRUE_WHEN_MISSING, &value) == 0 && value == true); |
| 168 |
assert_se(proc_cmdline_get_bool("foo_bar", /* flags= */ 0, &value) > 0 && value == true); |
| 169 |
assert_se(proc_cmdline_get_bool("foo-bar", /* flags= */ 0, &value) > 0 && value == true); |
| 170 |
assert_se(proc_cmdline_get_bool("bar-waldo", /* flags= */ 0, &value) > 0 && value == true); |
| 171 |
assert_se(proc_cmdline_get_bool("bar_waldo", /* flags= */ 0, &value) > 0 && value == true); |
| 172 |
assert_se(proc_cmdline_get_bool("x_y-z", /* flags= */ 0, &value) > 0 && value == false); |
| 173 |
assert_se(proc_cmdline_get_bool("x-y-z", /* flags= */ 0, &value) > 0 && value == false); |
| 174 |
assert_se(proc_cmdline_get_bool("x-y_z", /* flags= */ 0, &value) > 0 && value == false); |
| 175 |
assert_se(proc_cmdline_get_bool("x_y_z", /* flags= */ 0, &value) > 0 && value == false); |
| 176 |
assert_se(proc_cmdline_get_bool("quux", /* flags= */ 0, &value) == -EINVAL && value == false); |
| 177 |
assert_se(proc_cmdline_get_bool("da", /* flags= */ 0, &value) > 0 && value == true); |
| 178 |
assert_se(proc_cmdline_get_bool("the", /* flags= */ 0, &value) > 0 && value == true); |
| 179 |
} |
| 180 |
|
| 181 |
TEST(proc_cmdline_get_key_many) { |
| 182 |
_cleanup_free_ char *value1 = NULL, *value2 = NULL, *value3 = NULL, *value4 = NULL, *value5 = NULL, *value6 = NULL, *value7 = NULL; |
| 183 |
|
| 184 |
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); |
| 185 |
|
| 186 |
assert_se(proc_cmdline_get_key_many(0, |
| 187 |
"wuff-piep", &value3, |
| 188 |
"foo_bar", &value1, |
| 189 |
"idontexist", &value2, |
| 190 |
"zumm", &value4, |
| 191 |
"SPACE", &value5, |
| 192 |
"doubleticks", &value6, |
| 193 |
"zummm", &value7) == 5); |
| 194 |
|
| 195 |
ASSERT_STREQ(value1, "quux"); |
| 196 |
assert_se(!value2); |
| 197 |
ASSERT_STREQ(value3, "tuet"); |
| 198 |
assert_se(!value4); |
| 199 |
ASSERT_STREQ(value5, "one two"); |
| 200 |
ASSERT_STREQ(value6, " aaa aaa "); |
| 201 |
ASSERT_STREQ(value7, "\n"); |
| 202 |
} |
| 203 |
|
| 204 |
TEST(proc_cmdline_key_streq) { |
| 205 |
assert_se(proc_cmdline_key_streq("", "")); |
| 206 |
assert_se(proc_cmdline_key_streq("a", "a")); |
| 207 |
assert_se(!proc_cmdline_key_streq("", "a")); |
| 208 |
assert_se(!proc_cmdline_key_streq("a", "")); |
| 209 |
assert_se(proc_cmdline_key_streq("a", "a")); |
| 210 |
assert_se(!proc_cmdline_key_streq("a", "b")); |
| 211 |
assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z")); |
| 212 |
assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z")); |
| 213 |
assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z")); |
| 214 |
assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z")); |
| 215 |
assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z")); |
| 216 |
assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z")); |
| 217 |
} |
| 218 |
|
| 219 |
TEST(proc_cmdline_key_startswith) { |
| 220 |
assert_se(proc_cmdline_key_startswith("", "")); |
| 221 |
assert_se(proc_cmdline_key_startswith("x", "")); |
| 222 |
assert_se(!proc_cmdline_key_startswith("", "x")); |
| 223 |
assert_se(proc_cmdline_key_startswith("x", "x")); |
| 224 |
assert_se(!proc_cmdline_key_startswith("x", "y")); |
| 225 |
assert_se(!proc_cmdline_key_startswith("foo-bar", "quux")); |
| 226 |
assert_se(proc_cmdline_key_startswith("foo-bar", "foo")); |
| 227 |
assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar")); |
| 228 |
assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar")); |
| 229 |
assert_se(proc_cmdline_key_startswith("foo-bar", "foo_")); |
| 230 |
assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx")); |
| 231 |
} |
| 232 |
|
| 233 |
#define test_proc_cmdline_filter_pid1_args_one(nulstr, expected) \ |
| 234 |
({ \ |
| 235 |
_cleanup_strv_free_ char **a = NULL, **b = NULL; \ |
| 236 |
const char s[] = (nulstr); \ |
| 237 |
\ |
| 238 |
/* This emulates pid_get_cmdline_strv(). */ \ |
| 239 |
assert_se(a = strv_parse_nulstr_full(s, ELEMENTSOF(s), \ |
| 240 |
/* drop_trailing_nuls= */ true)); \ |
| 241 |
assert_se(proc_cmdline_filter_pid1_args(a, &b) >= 0); \ |
| 242 |
assert_se(strv_equal(b, expected)); \ |
| 243 |
}) |
| 244 |
|
| 245 |
TEST(proc_cmdline_filter_pid1_args) { |
| 246 |
test_proc_cmdline_filter_pid1_args_one("systemd\0", |
| 247 |
STRV_EMPTY); |
| 248 |
|
| 249 |
/* short option */ |
| 250 |
test_proc_cmdline_filter_pid1_args_one("systemd\0" |
| 251 |
"-a\0" /* unknown option */ |
| 252 |
"-abc\0" /* unknown options */ |
| 253 |
"-h\0" /* known option */ |
| 254 |
"-hDbs\0" /* known options */ |
| 255 |
"-hsx\0" /* mixed (known and unknown) options */ |
| 256 |
"-z\0drop1\0" /* option with argument */ |
| 257 |
"-z\0-z\0accept1\0" /* the second -z is handled as argument */ |
| 258 |
"-az\0drop2\0" /* options with argument */ |
| 259 |
"-za\0accept2\0" /* options with argument */ |
| 260 |
"-z\0--\0-x\0", /* "--" is handled as argument */ |
| 261 |
STRV_MAKE("accept1", "accept2")); |
| 262 |
|
| 263 |
/* long option */ |
| 264 |
test_proc_cmdline_filter_pid1_args_one("systemd\0" |
| 265 |
"--unknown\0accept1\0" /* unknown option */ |
| 266 |
"--system\0accept2\0" /* no argument */ |
| 267 |
"--log-level\0drop1\0" /* required argument (separated with space) */ |
| 268 |
"--log-level=drop2\0accept3\0" /* required argument (concatenated with '=') */ |
| 269 |
"--log-level\0--log-level\0accept4\0" /* the second "--log-level" is handled as argument */ |
| 270 |
"--log-level\0--\0-x\0" /* "--" is handled as argument */ |
| 271 |
"--log-color\0--log-level\0drop3\0" /* optional argument ("--log-level" is handled as another option) */ |
| 272 |
"--log-color\0accept5\0" /* optional argument (separated with space) */ |
| 273 |
"--log-color=drop4\0accept6\0" /* optional argument (concatenated with '=') */ |
| 274 |
"--log-color\0--\0" /* "--" is _not_ handled as argument, and remaining strings are accepted */ |
| 275 |
"remaining\0-x\0--foo\0", |
| 276 |
STRV_MAKE("accept1", "accept2", "accept3", "accept4", "accept5", "accept6", "remaining", "-x", "--foo")); |
| 277 |
|
| 278 |
/* test for "--" */ |
| 279 |
test_proc_cmdline_filter_pid1_args_one("systemd\0" |
| 280 |
"-a\0" |
| 281 |
"--dropped\0" |
| 282 |
"--\0" /* remaining strings are accepted */ |
| 283 |
"-x\0" |
| 284 |
"-abc\0" |
| 285 |
"--hoge\0" |
| 286 |
"accepted\0", |
| 287 |
STRV_MAKE("-x", "-abc", "--hoge", "accepted")); |
| 288 |
|
| 289 |
/* test for space */ |
| 290 |
test_proc_cmdline_filter_pid1_args_one("/usr/lib/systemd/systemd\0" |
| 291 |
"--switched-root\0" |
| 292 |
"--system\0" |
| 293 |
"--deserialize\030\0" /* followed with space */ |
| 294 |
"--deserialize=31\0" /* followed with '=' */ |
| 295 |
"--exit-code=42\0" |
| 296 |
"\0\0\0" |
| 297 |
"systemd.log_level=debug\0" |
| 298 |
"--unit\0foo.target\0" |
| 299 |
" ' quoted '\0" |
| 300 |
"systemd.log_target=console\0" |
| 301 |
"\t\0" |
| 302 |
" arg with space \0" |
| 303 |
"3\0" |
| 304 |
"\0\0\0", |
| 305 |
STRV_MAKE("", "", "", "systemd.log_level=debug", " ' quoted '", "systemd.log_target=console", "\t", " arg with space ", "3")); |
| 306 |
} |
| 307 |
|
| 308 |
static int intro(void) { |
| 309 |
if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno)) |
| 310 |
return log_tests_skipped("can't read /proc/cmdline"); |
| 311 |
|
| 312 |
return EXIT_SUCCESS; |
| 313 |
} |
| 314 |
|
| 315 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |