]>
Commit | Line | Data |
---|---|---|
4e731273 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
11c3a366 TA |
20 | #include <stdbool.h> |
21 | #include <stddef.h> | |
22 | #include <string.h> | |
23 | ||
b5efdb8a | 24 | #include "alloc-util.h" |
4e731273 LP |
25 | #include "extract-word.h" |
26 | #include "fileio.h" | |
27 | #include "macro.h" | |
28 | #include "parse-util.h" | |
29 | #include "proc-cmdline.h" | |
30 | #include "process-util.h" | |
c573dcfe | 31 | #include "special.h" |
4e731273 LP |
32 | #include "string-util.h" |
33 | #include "util.h" | |
34 | #include "virt.h" | |
35 | ||
36 | int proc_cmdline(char **ret) { | |
2467cc55 | 37 | const char *e; |
4e731273 LP |
38 | assert(ret); |
39 | ||
2467cc55 LP |
40 | /* For testing purposes it is sometimes useful to be able to override what we consider /proc/cmdline to be */ |
41 | e = secure_getenv("SYSTEMD_PROC_CMDLINE"); | |
42 | if (e) { | |
43 | char *m; | |
44 | ||
45 | m = strdup(e); | |
46 | if (!m) | |
47 | return -ENOMEM; | |
48 | ||
49 | *ret = m; | |
50 | return 0; | |
51 | } | |
52 | ||
4e731273 LP |
53 | if (detect_container() > 0) |
54 | return get_process_cmdline(1, 0, false, ret); | |
55 | else | |
56 | return read_one_line_file("/proc/cmdline", ret); | |
57 | } | |
58 | ||
1d84ad94 LP |
59 | int proc_cmdline_parse(proc_cmdline_parse_t parse_item, void *data, unsigned flags) { |
60 | ||
4e731273 LP |
61 | _cleanup_free_ char *line = NULL; |
62 | const char *p; | |
63 | int r; | |
64 | ||
65 | assert(parse_item); | |
66 | ||
67 | r = proc_cmdline(&line); | |
68 | if (r < 0) | |
69 | return r; | |
70 | ||
71 | p = line; | |
72 | for (;;) { | |
73 | _cleanup_free_ char *word = NULL; | |
1d84ad94 | 74 | char *value, *key, *q; |
4e731273 LP |
75 | |
76 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); | |
77 | if (r < 0) | |
78 | return r; | |
79 | if (r == 0) | |
80 | break; | |
81 | ||
1d84ad94 LP |
82 | key = word; |
83 | ||
84 | /* Filter out arguments that are intended only for the initrd */ | |
85 | q = startswith(word, "rd."); | |
86 | if (q) { | |
87 | if (!in_initrd()) | |
88 | continue; | |
89 | ||
90 | if (flags & PROC_CMDLINE_STRIP_RD_PREFIX) | |
91 | key = q; | |
92 | } | |
4e731273 | 93 | |
1d84ad94 | 94 | value = strchr(key, '='); |
4e731273 LP |
95 | if (value) |
96 | *(value++) = 0; | |
97 | ||
1d84ad94 | 98 | r = parse_item(key, value, data); |
4e731273 LP |
99 | if (r < 0) |
100 | return r; | |
101 | } | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
1d84ad94 LP |
106 | static bool relaxed_equal_char(char a, char b) { |
107 | ||
108 | return a == b || | |
109 | (a == '_' && b == '-') || | |
110 | (a == '-' && b == '_'); | |
111 | } | |
112 | ||
113 | char *proc_cmdline_key_startswith(const char *s, const char *prefix) { | |
114 | ||
115 | assert(s); | |
116 | assert(prefix); | |
117 | ||
118 | /* Much like startswith(), but considers "-" and "_" the same */ | |
119 | ||
120 | for (; *prefix != 0; s++, prefix++) | |
121 | if (!relaxed_equal_char(*s, *prefix)) | |
122 | return NULL; | |
123 | ||
124 | return (char*) s; | |
125 | } | |
126 | ||
127 | bool proc_cmdline_key_streq(const char *x, const char *y) { | |
128 | assert(x); | |
129 | assert(y); | |
130 | ||
131 | /* Much like streq(), but considers "-" and "_" the same */ | |
132 | ||
133 | for (; *x != 0 || *y != 0; x++, y++) | |
134 | if (!relaxed_equal_char(*x, *y)) | |
135 | return false; | |
136 | ||
137 | return true; | |
138 | } | |
139 | ||
140 | int proc_cmdline_get_key(const char *key, unsigned flags, char **value) { | |
4e731273 LP |
141 | _cleanup_free_ char *line = NULL, *ret = NULL; |
142 | bool found = false; | |
143 | const char *p; | |
144 | int r; | |
145 | ||
1d84ad94 LP |
146 | /* Looks for a specific key on the kernel command line. Supports two modes: |
147 | * | |
148 | * a) The "value" parameter is used. In this case a parameter beginning with the "key" string followed by "=" | |
149 | * is searched, and the value following this is returned in "value". | |
150 | * | |
1a012455 | 151 | * b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a |
1d84ad94 LP |
152 | * separate word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then |
153 | * this is also accepted, and "value" is returned as NULL. | |
154 | * | |
155 | * c) The "value" parameter is NULL. In this case a search for the exact "key" parameter is performed. | |
156 | * | |
13e785f7 | 157 | * In all three cases, > 0 is returned if the key is found, 0 if not. */ |
1d84ad94 LP |
158 | |
159 | if (isempty(key)) | |
160 | return -EINVAL; | |
161 | ||
162 | if ((flags & PROC_CMDLINE_VALUE_OPTIONAL) && !value) | |
163 | return -EINVAL; | |
4e731273 LP |
164 | |
165 | r = proc_cmdline(&line); | |
166 | if (r < 0) | |
167 | return r; | |
168 | ||
169 | p = line; | |
170 | for (;;) { | |
171 | _cleanup_free_ char *word = NULL; | |
172 | const char *e; | |
173 | ||
174 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); | |
175 | if (r < 0) | |
176 | return r; | |
177 | if (r == 0) | |
178 | break; | |
179 | ||
1d84ad94 LP |
180 | /* Automatically filter out arguments that are intended only for the initrd, if we are not in the |
181 | * initrd. */ | |
4e731273 LP |
182 | if (!in_initrd() && startswith(word, "rd.")) |
183 | continue; | |
184 | ||
185 | if (value) { | |
1d84ad94 | 186 | e = proc_cmdline_key_startswith(word, key); |
4e731273 LP |
187 | if (!e) |
188 | continue; | |
189 | ||
1d84ad94 LP |
190 | if (*e == '=') { |
191 | r = free_and_strdup(&ret, e+1); | |
192 | if (r < 0) | |
193 | return r; | |
194 | ||
195 | found = true; | |
196 | ||
197 | } else if (*e == 0 && (flags & PROC_CMDLINE_VALUE_OPTIONAL)) | |
198 | found = true; | |
4e731273 | 199 | |
4e731273 LP |
200 | } else { |
201 | if (streq(word, key)) | |
202 | found = true; | |
203 | } | |
204 | } | |
205 | ||
206 | if (value) { | |
207 | *value = ret; | |
208 | ret = NULL; | |
209 | } | |
210 | ||
211 | return found; | |
1d84ad94 LP |
212 | } |
213 | ||
214 | int proc_cmdline_get_bool(const char *key, bool *ret) { | |
215 | _cleanup_free_ char *v = NULL; | |
216 | int r; | |
217 | ||
218 | assert(ret); | |
219 | ||
220 | r = proc_cmdline_get_key(key, PROC_CMDLINE_VALUE_OPTIONAL, &v); | |
221 | if (r < 0) | |
222 | return r; | |
223 | if (r == 0) { | |
224 | *ret = false; | |
225 | return 0; | |
226 | } | |
227 | ||
228 | if (v) { /* parameter passed */ | |
229 | r = parse_boolean(v); | |
230 | if (r < 0) | |
231 | return r; | |
232 | *ret = r; | |
233 | } else /* no parameter passed */ | |
234 | *ret = true; | |
4e731273 | 235 | |
1d84ad94 | 236 | return 1; |
4e731273 LP |
237 | } |
238 | ||
239 | int shall_restore_state(void) { | |
1d84ad94 | 240 | bool ret; |
4e731273 LP |
241 | int r; |
242 | ||
1d84ad94 | 243 | r = proc_cmdline_get_bool("systemd.restore_state", &ret); |
4e731273 LP |
244 | if (r < 0) |
245 | return r; | |
4e731273 | 246 | |
1d84ad94 | 247 | return r > 0 ? ret : true; |
4e731273 | 248 | } |
c573dcfe EV |
249 | |
250 | static const char * const rlmap[] = { | |
251 | "emergency", SPECIAL_EMERGENCY_TARGET, | |
252 | "-b", SPECIAL_EMERGENCY_TARGET, | |
253 | "rescue", SPECIAL_RESCUE_TARGET, | |
254 | "single", SPECIAL_RESCUE_TARGET, | |
255 | "-s", SPECIAL_RESCUE_TARGET, | |
256 | "s", SPECIAL_RESCUE_TARGET, | |
257 | "S", SPECIAL_RESCUE_TARGET, | |
258 | "1", SPECIAL_RESCUE_TARGET, | |
259 | "2", SPECIAL_MULTI_USER_TARGET, | |
260 | "3", SPECIAL_MULTI_USER_TARGET, | |
261 | "4", SPECIAL_MULTI_USER_TARGET, | |
262 | "5", SPECIAL_GRAPHICAL_TARGET, | |
dcd61450 IS |
263 | NULL |
264 | }; | |
265 | ||
266 | static const char * const rlmap_initrd[] = { | |
267 | "emergency", SPECIAL_EMERGENCY_TARGET, | |
268 | "rescue", SPECIAL_RESCUE_TARGET, | |
269 | NULL | |
c573dcfe EV |
270 | }; |
271 | ||
272 | const char* runlevel_to_target(const char *word) { | |
273 | size_t i; | |
dcd61450 IS |
274 | const char * const *rlmap_ptr = in_initrd() ? rlmap_initrd |
275 | : rlmap; | |
c573dcfe EV |
276 | |
277 | if (!word) | |
278 | return NULL; | |
279 | ||
dcd61450 IS |
280 | if (in_initrd() && (word = startswith(word, "rd.")) == NULL) |
281 | return NULL; | |
282 | ||
283 | for (i = 0; rlmap_ptr[i] != NULL; i += 2) | |
284 | if (streq(word, rlmap_ptr[i])) | |
285 | return rlmap_ptr[i+1]; | |
c573dcfe EV |
286 | |
287 | return NULL; | |
288 | } |