1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "extract-word.h"
11 #include "parse-util.h"
12 #include "proc-cmdline.h"
13 #include "process-util.h"
15 #include "string-util.h"
19 int proc_cmdline(char **ret
) {
23 /* For testing purposes it is sometimes useful to be able to override what we consider /proc/cmdline to be */
24 e
= secure_getenv("SYSTEMD_PROC_CMDLINE");
36 if (detect_container() > 0)
37 return get_process_cmdline(1, 0, false, ret
);
39 return read_one_line_file("/proc/cmdline", ret
);
42 int proc_cmdline_parse(proc_cmdline_parse_t parse_item
, void *data
, unsigned flags
) {
44 _cleanup_free_
char *line
= NULL
;
50 r
= proc_cmdline(&line
);
56 _cleanup_free_
char *word
= NULL
;
57 char *value
, *key
, *q
;
59 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
67 /* Filter out arguments that are intended only for the initrd */
68 q
= startswith(word
, "rd.");
73 if (flags
& PROC_CMDLINE_STRIP_RD_PREFIX
)
77 value
= strchr(key
, '=');
81 r
= parse_item(key
, value
, data
);
89 static bool relaxed_equal_char(char a
, char b
) {
92 (a
== '_' && b
== '-') ||
93 (a
== '-' && b
== '_');
96 char *proc_cmdline_key_startswith(const char *s
, const char *prefix
) {
101 /* Much like startswith(), but considers "-" and "_" the same */
103 for (; *prefix
!= 0; s
++, prefix
++)
104 if (!relaxed_equal_char(*s
, *prefix
))
110 bool proc_cmdline_key_streq(const char *x
, const char *y
) {
114 /* Much like streq(), but considers "-" and "_" the same */
116 for (; *x
!= 0 || *y
!= 0; x
++, y
++)
117 if (!relaxed_equal_char(*x
, *y
))
123 int proc_cmdline_get_key(const char *key
, unsigned flags
, char **value
) {
124 _cleanup_free_
char *line
= NULL
, *ret
= NULL
;
129 /* Looks for a specific key on the kernel command line. Supports two modes:
131 * a) The "value" parameter is used. In this case a parameter beginning with the "key" string followed by "="
132 * is searched, and the value following this is returned in "value".
134 * b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a
135 * separate word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then
136 * this is also accepted, and "value" is returned as NULL.
138 * c) The "value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
140 * In all three cases, > 0 is returned if the key is found, 0 if not. */
145 if ((flags
& PROC_CMDLINE_VALUE_OPTIONAL
) && !value
)
148 r
= proc_cmdline(&line
);
154 _cleanup_free_
char *word
= NULL
;
157 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
163 /* Automatically filter out arguments that are intended only for the initrd, if we are not in the
165 if (!in_initrd() && startswith(word
, "rd."))
169 e
= proc_cmdline_key_startswith(word
, key
);
174 r
= free_and_strdup(&ret
, e
+1);
180 } else if (*e
== 0 && (flags
& PROC_CMDLINE_VALUE_OPTIONAL
))
184 if (streq(word
, key
))
190 *value
= TAKE_PTR(ret
);
195 int proc_cmdline_get_bool(const char *key
, bool *ret
) {
196 _cleanup_free_
char *v
= NULL
;
201 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &v
);
209 if (v
) { /* parameter passed */
210 r
= parse_boolean(v
);
214 } else /* no parameter passed */
220 int shall_restore_state(void) {
224 r
= proc_cmdline_get_bool("systemd.restore_state", &ret
);
228 return r
> 0 ? ret
: true;
231 static const char * const rlmap
[] = {
232 "emergency", SPECIAL_EMERGENCY_TARGET
,
233 "-b", SPECIAL_EMERGENCY_TARGET
,
234 "rescue", SPECIAL_RESCUE_TARGET
,
235 "single", SPECIAL_RESCUE_TARGET
,
236 "-s", SPECIAL_RESCUE_TARGET
,
237 "s", SPECIAL_RESCUE_TARGET
,
238 "S", SPECIAL_RESCUE_TARGET
,
239 "1", SPECIAL_RESCUE_TARGET
,
240 "2", SPECIAL_MULTI_USER_TARGET
,
241 "3", SPECIAL_MULTI_USER_TARGET
,
242 "4", SPECIAL_MULTI_USER_TARGET
,
243 "5", SPECIAL_GRAPHICAL_TARGET
,
247 static const char * const rlmap_initrd
[] = {
248 "emergency", SPECIAL_EMERGENCY_TARGET
,
249 "rescue", SPECIAL_RESCUE_TARGET
,
253 const char* runlevel_to_target(const char *word
) {
254 const char * const *rlmap_ptr
;
261 word
= startswith(word
, "rd.");
266 rlmap_ptr
= in_initrd() ? rlmap_initrd
: rlmap
;
268 for (i
= 0; rlmap_ptr
[i
]; i
+= 2)
269 if (streq(word
, rlmap_ptr
[i
]))
270 return rlmap_ptr
[i
+1];