2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
23 #include "proc-cmdline.h"
24 //#include "special.h"
25 #include "string-util.h"
30 static int parse_item(const char *key, const char *value, void *data) {
32 assert_se(data == &obj);
34 log_info("kernel cmdline option <%s> = <%s>", key, strna(value));
38 static void test_proc_cmdline_parse(void) {
39 assert_se(proc_cmdline_parse(parse_item, &obj, true) >= 0);
42 #if 0 /// UNNEEDED by elogind
43 static void test_runlevel_to_target(void) {
44 in_initrd_force(false);
45 assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
46 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
47 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
48 assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET));
49 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL));
51 in_initrd_force(true);
52 assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
53 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
54 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
55 assert_se(streq_ptr(runlevel_to_target("3"), NULL));
56 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET));
59 static void test_proc_cmdline_get_key(void) {
60 _cleanup_free_ char *value = NULL;
62 putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm");
64 assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL);
65 assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0);
66 assert_se(proc_cmdline_get_key("abc", 0, &value) == 0 && value == NULL);
67 assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL, &value) == 0 && value == NULL);
69 assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
71 assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
73 assert_se(proc_cmdline_get_key("foo-bar", 0, &value) > 0 && streq_ptr(value, "quux"));
75 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
77 assert_se(proc_cmdline_get_key("foo-bar", 0, NULL) == 0);
78 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
80 assert_se(proc_cmdline_get_key("wuff-piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
82 assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
84 assert_se(proc_cmdline_get_key("wuff_piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
86 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
88 assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0);
89 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
91 assert_se(proc_cmdline_get_key("zumm", 0, &value) == 0 && value == NULL);
92 assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL);
93 assert_se(proc_cmdline_get_key("zumm", 0, NULL) > 0);
96 static void test_proc_cmdline_get_bool(void) {
99 putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep");
101 assert_se(proc_cmdline_get_bool("", &value) == -EINVAL);
102 assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false);
103 assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true);
104 assert_se(proc_cmdline_get_bool("foo-bar", &value) > 0 && value == true);
105 assert_se(proc_cmdline_get_bool("bar-waldo", &value) > 0 && value == true);
106 assert_se(proc_cmdline_get_bool("bar_waldo", &value) > 0 && value == true);
107 assert_se(proc_cmdline_get_bool("x_y-z", &value) > 0 && value == false);
108 assert_se(proc_cmdline_get_bool("x-y-z", &value) > 0 && value == false);
109 assert_se(proc_cmdline_get_bool("x-y_z", &value) > 0 && value == false);
110 assert_se(proc_cmdline_get_bool("x_y_z", &value) > 0 && value == false);
111 assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false);
115 static void test_proc_cmdline_key_streq(void) {
117 assert_se(proc_cmdline_key_streq("", ""));
118 assert_se(proc_cmdline_key_streq("a", "a"));
119 assert_se(!proc_cmdline_key_streq("", "a"));
120 assert_se(!proc_cmdline_key_streq("a", ""));
121 assert_se(proc_cmdline_key_streq("a", "a"));
122 assert_se(!proc_cmdline_key_streq("a", "b"));
123 assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
124 assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
125 assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z"));
126 assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z"));
127 assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z"));
128 assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z"));
131 #if 0 /// UNNEEDED by elogind
132 static void test_proc_cmdline_key_startswith(void) {
134 assert_se(proc_cmdline_key_startswith("", ""));
135 assert_se(proc_cmdline_key_startswith("x", ""));
136 assert_se(!proc_cmdline_key_startswith("", "x"));
137 assert_se(proc_cmdline_key_startswith("x", "x"));
138 assert_se(!proc_cmdline_key_startswith("x", "y"));
139 assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
140 assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
141 assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
142 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
143 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
144 assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
149 log_parse_environment();
152 test_proc_cmdline_parse();
153 test_proc_cmdline_key_streq();
154 #if 0 /// UNNEEDED by elogind
155 test_proc_cmdline_key_startswith();
156 test_proc_cmdline_get_key();
157 test_proc_cmdline_get_bool();
158 test_runlevel_to_target();