chiark / gitweb /
72368310021a8a3e8f584bd77d00ea98d9a3ec65
[elogind.git] / src / basic / proc-cmdline.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <string.h>
11
12 #include "alloc-util.h"
13 #include "extract-word.h"
14 #include "fileio.h"
15 #include "macro.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"
21 #include "util.h"
22 #include "virt.h"
23
24 int proc_cmdline(char **ret) {
25         const char *e;
26         assert(ret);
27
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");
30         if (e) {
31                 char *m;
32
33                 m = strdup(e);
34                 if (!m)
35                         return -ENOMEM;
36
37                 *ret = m;
38                 return 0;
39         }
40
41         if (detect_container() > 0)
42                 return get_process_cmdline(1, 0, false, ret);
43         else
44                 return read_one_line_file("/proc/cmdline", ret);
45 }
46
47 int proc_cmdline_parse(proc_cmdline_parse_t parse_item, void *data, unsigned flags) {
48
49         _cleanup_free_ char *line = NULL;
50         const char *p;
51         int r;
52
53         assert(parse_item);
54
55         r = proc_cmdline(&line);
56         if (r < 0)
57                 return r;
58
59         p = line;
60         for (;;) {
61                 _cleanup_free_ char *word = NULL;
62                 char *value, *key, *q;
63
64                 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
65                 if (r < 0)
66                         return r;
67                 if (r == 0)
68                         break;
69
70                 key = word;
71
72                 /* Filter out arguments that are intended only for the initrd */
73                 q = startswith(word, "rd.");
74                 if (q) {
75                         if (!in_initrd())
76                                 continue;
77
78                         if (flags & PROC_CMDLINE_STRIP_RD_PREFIX)
79                                 key = q;
80                 }
81
82                 value = strchr(key, '=');
83                 if (value)
84                         *(value++) = 0;
85
86                 r = parse_item(key, value, data);
87                 if (r < 0)
88                         return r;
89         }
90
91         return 0;
92 }
93
94 static bool relaxed_equal_char(char a, char b) {
95
96         return a == b ||
97                 (a == '_' && b == '-') ||
98                 (a == '-' && b == '_');
99 }
100
101 #if 0 /// UNNEEDED by elogind
102 char *proc_cmdline_key_startswith(const char *s, const char *prefix) {
103
104         assert(s);
105         assert(prefix);
106
107         /* Much like startswith(), but considers "-" and "_" the same */
108
109         for (; *prefix != 0; s++, prefix++)
110                 if (!relaxed_equal_char(*s, *prefix))
111                         return NULL;
112
113         return (char*) s;
114 }
115 #endif // 0
116
117 bool proc_cmdline_key_streq(const char *x, const char *y) {
118         assert(x);
119         assert(y);
120
121         /* Much like streq(), but considers "-" and "_" the same */
122
123         for (; *x != 0 || *y != 0; x++, y++)
124                 if (!relaxed_equal_char(*x, *y))
125                         return false;
126
127         return true;
128 }
129
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;
133         bool found = false;
134         const char *p;
135         int r;
136
137         /* Looks for a specific key on the kernel command line. Supports two modes:
138          *
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".
141          *
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.
145          *
146          * c) The "value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
147          *
148          * In all three cases, > 0 is returned if the key is found, 0 if not. */
149
150         if (isempty(key))
151                 return -EINVAL;
152
153         if ((flags & PROC_CMDLINE_VALUE_OPTIONAL) && !value)
154                 return -EINVAL;
155
156         r = proc_cmdline(&line);
157         if (r < 0)
158                 return r;
159
160         p = line;
161         for (;;) {
162                 _cleanup_free_ char *word = NULL;
163                 const char *e;
164
165                 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
166                 if (r < 0)
167                         return r;
168                 if (r == 0)
169                         break;
170
171                 /* Automatically filter out arguments that are intended only for the initrd, if we are not in the
172                  * initrd. */
173                 if (!in_initrd() && startswith(word, "rd."))
174                         continue;
175
176                 if (value) {
177                         e = proc_cmdline_key_startswith(word, key);
178                         if (!e)
179                                 continue;
180
181                         if (*e == '=') {
182                                 r = free_and_strdup(&ret, e+1);
183                                 if (r < 0)
184                                         return r;
185
186                                 found = true;
187
188                         } else if (*e == 0 && (flags & PROC_CMDLINE_VALUE_OPTIONAL))
189                                 found = true;
190
191                 } else {
192                         if (streq(word, key))
193                                 found = true;
194                 }
195         }
196
197         if (value)
198                 *value = TAKE_PTR(ret);
199
200         return found;
201 }
202
203 int proc_cmdline_get_bool(const char *key, bool *ret) {
204         _cleanup_free_ char *v = NULL;
205         int r;
206
207         assert(ret);
208
209         r = proc_cmdline_get_key(key, PROC_CMDLINE_VALUE_OPTIONAL, &v);
210         if (r < 0)
211                 return r;
212         if (r == 0) {
213                 *ret = false;
214                 return 0;
215         }
216
217         if (v) { /* parameter passed */
218                 r = parse_boolean(v);
219                 if (r < 0)
220                         return r;
221                 *ret = r;
222         } else /* no parameter passed */
223                 *ret = true;
224
225         return 1;
226 }
227
228 int shall_restore_state(void) {
229         bool ret;
230         int r;
231
232         r = proc_cmdline_get_bool("systemd.restore_state", &ret);
233         if (r < 0)
234                 return r;
235
236         return r > 0 ? ret : true;
237 }
238
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,
252         NULL
253 };
254
255 static const char * const rlmap_initrd[] = {
256         "emergency", SPECIAL_EMERGENCY_TARGET,
257         "rescue",    SPECIAL_RESCUE_TARGET,
258         NULL
259 };
260
261 const char* runlevel_to_target(const char *word) {
262         const char * const *rlmap_ptr;
263         size_t i;
264
265         if (!word)
266                 return NULL;
267
268         if (in_initrd()) {
269                 word = startswith(word, "rd.");
270                 if (!word)
271                         return NULL;
272         }
273
274         rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap;
275
276         for (i = 0; rlmap_ptr[i]; i += 2)
277                 if (streq(word, rlmap_ptr[i]))
278                         return rlmap_ptr[i+1];
279
280         return NULL;
281 }
282 #endif // 0