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