1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include "alloc-util.h"
26 #include "extract-word.h"
29 #include "parse-util.h"
30 #include "proc-cmdline.h"
31 #include "process-util.h"
33 #include "string-util.h"
37 int proc_cmdline(char **ret
) {
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");
54 if (detect_container() > 0)
55 return get_process_cmdline(1, 0, false, ret
);
57 return read_one_line_file("/proc/cmdline", ret
);
60 int proc_cmdline_parse(proc_cmdline_parse_t parse_item
, void *data
, unsigned flags
) {
62 _cleanup_free_
char *line
= NULL
;
68 r
= proc_cmdline(&line
);
74 _cleanup_free_
char *word
= NULL
;
75 char *value
, *key
, *q
;
77 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
85 /* Filter out arguments that are intended only for the initrd */
86 q
= startswith(word
, "rd.");
91 if (flags
& PROC_CMDLINE_STRIP_RD_PREFIX
)
95 value
= strchr(key
, '=');
99 r
= parse_item(key
, value
, data
);
107 static bool relaxed_equal_char(char a
, char b
) {
110 (a
== '_' && b
== '-') ||
111 (a
== '-' && b
== '_');
114 char *proc_cmdline_key_startswith(const char *s
, const char *prefix
) {
119 /* Much like startswith(), but considers "-" and "_" the same */
121 for (; *prefix
!= 0; s
++, prefix
++)
122 if (!relaxed_equal_char(*s
, *prefix
))
128 bool proc_cmdline_key_streq(const char *x
, const char *y
) {
132 /* Much like streq(), but considers "-" and "_" the same */
134 for (; *x
!= 0 || *y
!= 0; x
++, y
++)
135 if (!relaxed_equal_char(*x
, *y
))
141 int proc_cmdline_get_key(const char *key
, unsigned flags
, char **value
) {
142 _cleanup_free_
char *line
= NULL
, *ret
= NULL
;
147 /* Looks for a specific key on the kernel command line. Supports two modes:
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".
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.
156 * c) The "value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
158 * In all three cases, > 0 is returned if the key is found, 0 if not. */
163 if ((flags
& PROC_CMDLINE_VALUE_OPTIONAL
) && !value
)
166 r
= proc_cmdline(&line
);
172 _cleanup_free_
char *word
= NULL
;
175 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
181 /* Automatically filter out arguments that are intended only for the initrd, if we are not in the
183 if (!in_initrd() && startswith(word
, "rd."))
187 e
= proc_cmdline_key_startswith(word
, key
);
192 r
= free_and_strdup(&ret
, e
+1);
198 } else if (*e
== 0 && (flags
& PROC_CMDLINE_VALUE_OPTIONAL
))
202 if (streq(word
, key
))
215 int proc_cmdline_get_bool(const char *key
, bool *ret
) {
216 _cleanup_free_
char *v
= NULL
;
221 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &v
);
229 if (v
) { /* parameter passed */
230 r
= parse_boolean(v
);
234 } else /* no parameter passed */
240 int shall_restore_state(void) {
244 r
= proc_cmdline_get_bool("systemd.restore_state", &ret
);
248 return r
> 0 ? ret
: true;
251 static const char * const rlmap
[] = {
252 "emergency", SPECIAL_EMERGENCY_TARGET
,
253 "-b", SPECIAL_EMERGENCY_TARGET
,
254 "rescue", SPECIAL_RESCUE_TARGET
,
255 "single", SPECIAL_RESCUE_TARGET
,
256 "-s", SPECIAL_RESCUE_TARGET
,
257 "s", SPECIAL_RESCUE_TARGET
,
258 "S", SPECIAL_RESCUE_TARGET
,
259 "1", SPECIAL_RESCUE_TARGET
,
260 "2", SPECIAL_MULTI_USER_TARGET
,
261 "3", SPECIAL_MULTI_USER_TARGET
,
262 "4", SPECIAL_MULTI_USER_TARGET
,
263 "5", SPECIAL_GRAPHICAL_TARGET
,
267 static const char * const rlmap_initrd
[] = {
268 "emergency", SPECIAL_EMERGENCY_TARGET
,
269 "rescue", SPECIAL_RESCUE_TARGET
,
273 const char* runlevel_to_target(const char *word
) {
274 const char * const *rlmap_ptr
;
281 word
= startswith(word
, "rd.");
286 rlmap_ptr
= in_initrd() ? rlmap_initrd
: rlmap
;
288 for (i
= 0; rlmap_ptr
[i
]; i
+= 2)
289 if (streq(word
, rlmap_ptr
[i
]))
290 return rlmap_ptr
[i
+1];