1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
27 #include "extract-word.h"
30 #include "parse-util.h"
31 #include "proc-cmdline.h"
32 #include "process-util.h"
34 #include "string-util.h"
38 int proc_cmdline(char **ret
) {
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 parse_proc_cmdline(int (*parse_item
)(const char *key
, const char *value
)) {
48 _cleanup_free_
char *line
= NULL
;
54 r
= proc_cmdline(&line
);
60 _cleanup_free_
char *word
= NULL
;
63 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
69 /* Filter out arguments that are intended only for the
71 if (!in_initrd() && startswith(word
, "rd."))
74 value
= strchr(word
, '=');
78 r
= parse_item(word
, value
);
86 int get_proc_cmdline_key(const char *key
, char **value
) {
87 _cleanup_free_
char *line
= NULL
, *ret
= NULL
;
94 r
= proc_cmdline(&line
);
100 _cleanup_free_
char *word
= NULL
;
103 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_RELAX
);
109 /* Filter out arguments that are intended only for the
111 if (!in_initrd() && startswith(word
, "rd."))
115 e
= startswith(word
, key
);
119 r
= free_and_strdup(&ret
, e
);
125 if (streq(word
, key
))
139 int shall_restore_state(void) {
140 _cleanup_free_
char *value
= NULL
;
143 r
= get_proc_cmdline_key("systemd.restore_state=", &value
);
149 return parse_boolean(value
);
152 static const char * const rlmap
[] = {
153 "emergency", SPECIAL_EMERGENCY_TARGET
,
154 "-b", SPECIAL_EMERGENCY_TARGET
,
155 "rescue", SPECIAL_RESCUE_TARGET
,
156 "single", SPECIAL_RESCUE_TARGET
,
157 "-s", SPECIAL_RESCUE_TARGET
,
158 "s", SPECIAL_RESCUE_TARGET
,
159 "S", SPECIAL_RESCUE_TARGET
,
160 "1", SPECIAL_RESCUE_TARGET
,
161 "2", SPECIAL_MULTI_USER_TARGET
,
162 "3", SPECIAL_MULTI_USER_TARGET
,
163 "4", SPECIAL_MULTI_USER_TARGET
,
164 "5", SPECIAL_GRAPHICAL_TARGET
,
167 const char* runlevel_to_target(const char *word
) {
173 for (i
= 0; i
< ELEMENTSOF(rlmap
); i
+= 2)
174 if (streq(word
, rlmap
[i
]))