1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
12 #include "alloc-util.h"
13 #include "extract-word.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"
24 int proc_cmdline(char **ret) {
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");
41 if (detect_container() > 0)
42 return get_process_cmdline(1, 0, false, ret);
44 return read_one_line_file("/proc/cmdline", ret);
47 int proc_cmdline_parse(proc_cmdline_parse_t parse_item, void *data, unsigned flags) {
49 _cleanup_free_ char *line = NULL;
55 r = proc_cmdline(&line);
61 _cleanup_free_ char *word = NULL;
62 char *value, *key, *q;
64 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
72 /* Filter out arguments that are intended only for the initrd */
73 q = startswith(word, "rd.");
78 if (flags & PROC_CMDLINE_STRIP_RD_PREFIX)
82 value = strchr(key, '=');
86 r = parse_item(key, value, data);
94 static bool relaxed_equal_char(char a, char b) {
97 (a == '_' && b == '-') ||
98 (a == '-' && b == '_');
101 #if 0 /// UNNEEDED by elogind
102 char *proc_cmdline_key_startswith(const char *s, const char *prefix) {
107 /* Much like startswith(), but considers "-" and "_" the same */
109 for (; *prefix != 0; s++, prefix++)
110 if (!relaxed_equal_char(*s, *prefix))
117 bool proc_cmdline_key_streq(const char *x, const char *y) {
121 /* Much like streq(), but considers "-" and "_" the same */
123 for (; *x != 0 || *y != 0; x++, y++)
124 if (!relaxed_equal_char(*x, *y))
130 #if 0 /// UNNEEDED by elogind
131 int proc_cmdline_get_key(const char *key, unsigned flags, char **value) {
132 _cleanup_free_ char *line = NULL, *ret = NULL;
137 /* Looks for a specific key on the kernel command line. Supports two modes:
139 * a) The "value" parameter is used. In this case a parameter beginning with the "key" string followed by "="
140 * is searched, and the value following this is returned in "value".
142 * b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a
143 * separate word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then
144 * this is also accepted, and "value" is returned as NULL.
146 * c) The "value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
148 * In all three cases, > 0 is returned if the key is found, 0 if not. */
153 if ((flags & PROC_CMDLINE_VALUE_OPTIONAL) && !value)
156 r = proc_cmdline(&line);
162 _cleanup_free_ char *word = NULL;
165 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
171 /* Automatically filter out arguments that are intended only for the initrd, if we are not in the
173 if (!in_initrd() && startswith(word, "rd."))
177 e = proc_cmdline_key_startswith(word, key);
182 r = free_and_strdup(&ret, e+1);
188 } else if (*e == 0 && (flags & PROC_CMDLINE_VALUE_OPTIONAL))
192 if (streq(word, key))
198 *value = TAKE_PTR(ret);
203 int proc_cmdline_get_bool(const char *key, bool *ret) {
204 _cleanup_free_ char *v = NULL;
209 r = proc_cmdline_get_key(key, PROC_CMDLINE_VALUE_OPTIONAL, &v);
217 if (v) { /* parameter passed */
218 r = parse_boolean(v);
222 } else /* no parameter passed */
228 int shall_restore_state(void) {
232 r = proc_cmdline_get_bool("systemd.restore_state", &ret);
236 return r > 0 ? ret : true;
239 static const char * const rlmap[] = {
240 "emergency", SPECIAL_EMERGENCY_TARGET,
241 "-b", SPECIAL_EMERGENCY_TARGET,
242 "rescue", SPECIAL_RESCUE_TARGET,
243 "single", SPECIAL_RESCUE_TARGET,
244 "-s", SPECIAL_RESCUE_TARGET,
245 "s", SPECIAL_RESCUE_TARGET,
246 "S", SPECIAL_RESCUE_TARGET,
247 "1", SPECIAL_RESCUE_TARGET,
248 "2", SPECIAL_MULTI_USER_TARGET,
249 "3", SPECIAL_MULTI_USER_TARGET,
250 "4", SPECIAL_MULTI_USER_TARGET,
251 "5", SPECIAL_GRAPHICAL_TARGET,
255 static const char * const rlmap_initrd[] = {
256 "emergency", SPECIAL_EMERGENCY_TARGET,
257 "rescue", SPECIAL_RESCUE_TARGET,
261 const char* runlevel_to_target(const char *word) {
262 const char * const *rlmap_ptr;
269 word = startswith(word, "rd.");
274 rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap;
276 for (i = 0; rlmap_ptr[i]; i += 2)
277 if (streq(word, rlmap_ptr[i]))
278 return rlmap_ptr[i+1];