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