2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
25 #include "extract-word.h"
28 #include "parse-util.h"
29 #include "proc-cmdline.h"
30 #include "process-util.h"
32 #include "string-util.h"
36 int proc_cmdline(char **ret
) {
39 if (detect_container() > 0)
40 return get_process_cmdline(1, 0, false, ret
);
42 return read_one_line_file("/proc/cmdline", ret
);
45 int parse_proc_cmdline(int (*parse_item
)(const char *key
, const char *value
, void *data
),
48 _cleanup_free_
char *line
= NULL
;
54 r
= proc_cmdline(&line
);
60 _cleanup_free_
char *word
= NULL
;
61 char *value
= NULL
, *unprefixed
;
63 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
69 /* Filter out arguments that are intended only for the
71 unprefixed
= startswith(word
, "rd.");
72 if (unprefixed
&& !in_initrd())
75 value
= strchr(word
, '=');
79 r
= parse_item(strip_prefix
&& unprefixed
? unprefixed
: word
, value
, data
);
87 int get_proc_cmdline_key(const char *key
, char **value
) {
88 _cleanup_free_
char *line
= NULL
, *ret
= NULL
;
95 r
= proc_cmdline(&line
);
101 _cleanup_free_
char *word
= NULL
;
104 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
110 /* Filter out arguments that are intended only for the
112 if (!in_initrd() && startswith(word
, "rd."))
116 e
= startswith(word
, key
);
120 r
= free_and_strdup(&ret
, e
);
126 if (streq(word
, key
))
140 int shall_restore_state(void) {
141 _cleanup_free_
char *value
= NULL
;
144 r
= get_proc_cmdline_key("systemd.restore_state=", &value
);
150 return parse_boolean(value
);
153 static const char * const rlmap
[] = {
154 "emergency", SPECIAL_EMERGENCY_TARGET
,
155 "-b", SPECIAL_EMERGENCY_TARGET
,
156 "rescue", SPECIAL_RESCUE_TARGET
,
157 "single", SPECIAL_RESCUE_TARGET
,
158 "-s", SPECIAL_RESCUE_TARGET
,
159 "s", SPECIAL_RESCUE_TARGET
,
160 "S", SPECIAL_RESCUE_TARGET
,
161 "1", SPECIAL_RESCUE_TARGET
,
162 "2", SPECIAL_MULTI_USER_TARGET
,
163 "3", SPECIAL_MULTI_USER_TARGET
,
164 "4", SPECIAL_MULTI_USER_TARGET
,
165 "5", SPECIAL_GRAPHICAL_TARGET
,
169 static const char * const rlmap_initrd
[] = {
170 "emergency", SPECIAL_EMERGENCY_TARGET
,
171 "rescue", SPECIAL_RESCUE_TARGET
,
175 const char* runlevel_to_target(const char *word
) {
177 const char * const *rlmap_ptr
= in_initrd() ? rlmap_initrd
183 if (in_initrd() && (word
= startswith(word
, "rd.")) == NULL
)
186 for (i
= 0; rlmap_ptr
[i
] != NULL
; i
+= 2)
187 if (streq(word
, rlmap_ptr
[i
]))
188 return rlmap_ptr
[i
+1];