]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/proc-cmdline.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / basic / proc-cmdline.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <string.h>
11
12 #include "alloc-util.h"
13 #include "extract-word.h"
14 #include "fileio.h"
15 #include "macro.h"
16 #include "parse-util.h"
17 #include "proc-cmdline.h"
18 #include "process-util.h"
19 #include "special.h"
20 #include "string-util.h"
21 #include "util.h"
22 #include "virt.h"
23
24 int proc_cmdline(char **ret) {
25 const char *e;
26 assert(ret);
27
28 /* For testing purposes it is sometimes useful to be able to override what we consider /proc/cmdline to be */
29 e = secure_getenv("SYSTEMD_PROC_CMDLINE");
30 if (e) {
31 char *m;
32
33 m = strdup(e);
34 if (!m)
35 return -ENOMEM;
36
37 *ret = m;
38 return 0;
39 }
40
41 if (detect_container() > 0)
42 return get_process_cmdline(1, 0, false, ret);
43 else
44 return read_one_line_file("/proc/cmdline", ret);
45 }
46
47 int proc_cmdline_parse(proc_cmdline_parse_t parse_item, void *data, unsigned flags) {
48
49 _cleanup_free_ char *line = NULL;
50 const char *p;
51 int r;
52
53 assert(parse_item);
54
55 r = proc_cmdline(&line);
56 if (r < 0)
57 return r;
58
59 p = line;
60 for (;;) {
61 _cleanup_free_ char *word = NULL;
62 char *value, *key, *q;
63
64 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
65 if (r < 0)
66 return r;
67 if (r == 0)
68 break;
69
70 key = word;
71
72 /* Filter out arguments that are intended only for the initrd */
73 q = startswith(word, "rd.");
74 if (q) {
75 if (!in_initrd())
76 continue;
77
78 if (flags & PROC_CMDLINE_STRIP_RD_PREFIX)
79 key = q;
80 }
81
82 value = strchr(key, '=');
83 if (value)
84 *(value++) = 0;
85
86 r = parse_item(key, value, data);
87 if (r < 0)
88 return r;
89 }
90
91 return 0;
92 }
93
94 static bool relaxed_equal_char(char a, char b) {
95
96 return a == b ||
97 (a == '_' && b == '-') ||
98 (a == '-' && b == '_');
99 }
100
101 char *proc_cmdline_key_startswith(const char *s, const char *prefix) {
102
103 assert(s);
104 assert(prefix);
105
106 /* Much like startswith(), but considers "-" and "_" the same */
107
108 for (; *prefix != 0; s++, prefix++)
109 if (!relaxed_equal_char(*s, *prefix))
110 return NULL;
111
112 return (char*) s;
113 }
114
115 bool proc_cmdline_key_streq(const char *x, const char *y) {
116 assert(x);
117 assert(y);
118
119 /* Much like streq(), but considers "-" and "_" the same */
120
121 for (; *x != 0 || *y != 0; x++, y++)
122 if (!relaxed_equal_char(*x, *y))
123 return false;
124
125 return true;
126 }
127
128 int proc_cmdline_get_key(const char *key, unsigned flags, char **value) {
129 _cleanup_free_ char *line = NULL, *ret = NULL;
130 bool found = false;
131 const char *p;
132 int r;
133
134 /* Looks for a specific key on the kernel command line. Supports two modes:
135 *
136 * a) The "value" parameter is used. In this case a parameter beginning with the "key" string followed by "="
137 * is searched, and the value following this is returned in "value".
138 *
139 * b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a
140 * separate word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then
141 * this is also accepted, and "value" is returned as NULL.
142 *
143 * c) The "value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
144 *
145 * In all three cases, > 0 is returned if the key is found, 0 if not. */
146
147 if (isempty(key))
148 return -EINVAL;
149
150 if ((flags & PROC_CMDLINE_VALUE_OPTIONAL) && !value)
151 return -EINVAL;
152
153 r = proc_cmdline(&line);
154 if (r < 0)
155 return r;
156
157 p = line;
158 for (;;) {
159 _cleanup_free_ char *word = NULL;
160 const char *e;
161
162 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
163 if (r < 0)
164 return r;
165 if (r == 0)
166 break;
167
168 /* Automatically filter out arguments that are intended only for the initrd, if we are not in the
169 * initrd. */
170 if (!in_initrd() && startswith(word, "rd."))
171 continue;
172
173 if (value) {
174 e = proc_cmdline_key_startswith(word, key);
175 if (!e)
176 continue;
177
178 if (*e == '=') {
179 r = free_and_strdup(&ret, e+1);
180 if (r < 0)
181 return r;
182
183 found = true;
184
185 } else if (*e == 0 && (flags & PROC_CMDLINE_VALUE_OPTIONAL))
186 found = true;
187
188 } else {
189 if (streq(word, key))
190 found = true;
191 }
192 }
193
194 if (value)
195 *value = TAKE_PTR(ret);
196
197 return found;
198 }
199
200 int proc_cmdline_get_bool(const char *key, bool *ret) {
201 _cleanup_free_ char *v = NULL;
202 int r;
203
204 assert(ret);
205
206 r = proc_cmdline_get_key(key, PROC_CMDLINE_VALUE_OPTIONAL, &v);
207 if (r < 0)
208 return r;
209 if (r == 0) {
210 *ret = false;
211 return 0;
212 }
213
214 if (v) { /* parameter passed */
215 r = parse_boolean(v);
216 if (r < 0)
217 return r;
218 *ret = r;
219 } else /* no parameter passed */
220 *ret = true;
221
222 return 1;
223 }
224
225 int shall_restore_state(void) {
226 bool ret;
227 int r;
228
229 r = proc_cmdline_get_bool("systemd.restore_state", &ret);
230 if (r < 0)
231 return r;
232
233 return r > 0 ? ret : true;
234 }
235
236 static const char * const rlmap[] = {
237 "emergency", SPECIAL_EMERGENCY_TARGET,
238 "-b", SPECIAL_EMERGENCY_TARGET,
239 "rescue", SPECIAL_RESCUE_TARGET,
240 "single", SPECIAL_RESCUE_TARGET,
241 "-s", SPECIAL_RESCUE_TARGET,
242 "s", SPECIAL_RESCUE_TARGET,
243 "S", SPECIAL_RESCUE_TARGET,
244 "1", SPECIAL_RESCUE_TARGET,
245 "2", SPECIAL_MULTI_USER_TARGET,
246 "3", SPECIAL_MULTI_USER_TARGET,
247 "4", SPECIAL_MULTI_USER_TARGET,
248 "5", SPECIAL_GRAPHICAL_TARGET,
249 NULL
250 };
251
252 static const char * const rlmap_initrd[] = {
253 "emergency", SPECIAL_EMERGENCY_TARGET,
254 "rescue", SPECIAL_RESCUE_TARGET,
255 NULL
256 };
257
258 const char* runlevel_to_target(const char *word) {
259 const char * const *rlmap_ptr;
260 size_t i;
261
262 if (!word)
263 return NULL;
264
265 if (in_initrd()) {
266 word = startswith(word, "rd.");
267 if (!word)
268 return NULL;
269 }
270
271 rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap;
272
273 for (i = 0; rlmap_ptr[i]; i += 2)
274 if (streq(word, rlmap_ptr[i]))
275 return rlmap_ptr[i+1];
276
277 return NULL;
278 }