1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
8 #include "extract-word.h"
11 #include "parse-util.h"
12 #include "proc-cmdline.h"
13 #include "process-util.h"
15 #include "string-util.h"
19 int proc_cmdline(char **ret
) {
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");
36 if (detect_container() > 0)
37 return get_process_cmdline(1, SIZE_MAX
, 0, ret
);
39 return read_one_line_file("/proc/cmdline", ret
);
42 static int proc_cmdline_extract_first(const char **p
, char **ret_word
, ProcCmdlineFlags flags
) {
47 _cleanup_free_
char *word
= NULL
;
50 r
= extract_first_word(&q
, &word
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RELAX
);
56 /* Filter out arguments that are intended only for the initrd */
57 c
= startswith(word
, "rd.");
62 if (FLAGS_SET(flags
, PROC_CMDLINE_STRIP_RD_PREFIX
)) {
63 r
= free_and_strdup(&word
, c
);
68 } else if (FLAGS_SET(flags
, PROC_CMDLINE_RD_STRICT
) && in_initrd())
69 continue; /* And optionally filter out arguments that are intended only for the host */
72 *ret_word
= TAKE_PTR(word
);
81 int proc_cmdline_parse_given(const char *line
, proc_cmdline_parse_t parse_item
, void *data
, ProcCmdlineFlags flags
) {
87 /* The PROC_CMDLINE_VALUE_OPTIONAL flag doesn't really make sense for proc_cmdline_parse(), let's make this
89 assert(!FLAGS_SET(flags
, PROC_CMDLINE_VALUE_OPTIONAL
));
93 _cleanup_free_
char *word
= NULL
;
96 r
= proc_cmdline_extract_first(&p
, &word
, flags
);
102 value
= strchr(word
, '=');
106 r
= parse_item(word
, value
, data
);
114 int proc_cmdline_parse(proc_cmdline_parse_t parse_item
, void *data
, ProcCmdlineFlags flags
) {
115 _cleanup_free_
char *line
= NULL
;
120 /* We parse the EFI variable first, because later settings have higher priority. */
122 if (!FLAGS_SET(flags
, PROC_CMDLINE_IGNORE_EFI_OPTIONS
)) {
123 r
= systemd_efi_options_variable(&line
);
126 log_debug_errno(r
, "Failed to get SystemdOptions EFI variable, ignoring: %m");
128 r
= proc_cmdline_parse_given(line
, parse_item
, data
, flags
);
136 r
= proc_cmdline(&line
);
140 return proc_cmdline_parse_given(line
, parse_item
, data
, flags
);
143 static bool relaxed_equal_char(char a
, char b
) {
145 (a
== '_' && b
== '-') ||
146 (a
== '-' && b
== '_');
149 char *proc_cmdline_key_startswith(const char *s
, const char *prefix
) {
153 /* Much like startswith(), but considers "-" and "_" the same */
155 for (; *prefix
!= 0; s
++, prefix
++)
156 if (!relaxed_equal_char(*s
, *prefix
))
162 bool proc_cmdline_key_streq(const char *x
, const char *y
) {
166 /* Much like streq(), but considers "-" and "_" the same */
168 for (; *x
!= 0 || *y
!= 0; x
++, y
++)
169 if (!relaxed_equal_char(*x
, *y
))
175 static int cmdline_get_key(const char *line
, const char *key
, ProcCmdlineFlags flags
, char **ret_value
) {
176 _cleanup_free_
char *ret
= NULL
;
186 _cleanup_free_
char *word
= NULL
;
188 r
= proc_cmdline_extract_first(&p
, &word
, flags
);
197 e
= proc_cmdline_key_startswith(word
, key
);
202 r
= free_and_strdup(&ret
, e
+1);
208 } else if (*e
== 0 && FLAGS_SET(flags
, PROC_CMDLINE_VALUE_OPTIONAL
))
212 if (streq(word
, key
)) {
214 break; /* we found what we were looking for */
220 *ret_value
= TAKE_PTR(ret
);
225 int proc_cmdline_get_key(const char *key
, ProcCmdlineFlags flags
, char **ret_value
) {
226 _cleanup_free_
char *line
= NULL
, *v
= NULL
;
229 /* Looks for a specific key on the kernel command line and (with lower priority) the EFI variable.
230 * Supports three modes:
232 * a) The "ret_value" parameter is used. In this case a parameter beginning with the "key" string followed by
233 * "=" is searched for, and the value following it is returned in "ret_value".
235 * b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a separate
236 * word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then this is
237 * also accepted, and "value" is returned as NULL.
239 * c) The "ret_value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
241 * In all three cases, > 0 is returned if the key is found, 0 if not. */
246 if (FLAGS_SET(flags
, PROC_CMDLINE_VALUE_OPTIONAL
) && !ret_value
)
249 r
= proc_cmdline(&line
);
253 if (FLAGS_SET(flags
, PROC_CMDLINE_IGNORE_EFI_OPTIONS
)) /* Shortcut */
254 return cmdline_get_key(line
, key
, flags
, ret_value
);
256 r
= cmdline_get_key(line
, key
, flags
, ret_value
? &v
: NULL
);
261 *ret_value
= TAKE_PTR(v
);
267 r
= systemd_efi_options_variable(&line
);
272 return false; /* Not found */
277 return cmdline_get_key(line
, key
, flags
, ret_value
);
280 int proc_cmdline_get_bool(const char *key
, bool *ret
) {
281 _cleanup_free_
char *v
= NULL
;
286 r
= proc_cmdline_get_key(key
, PROC_CMDLINE_VALUE_OPTIONAL
, &v
);
289 if (r
== 0) { /* key not specified at all */
294 if (v
) { /* key with parameter passed */
295 r
= parse_boolean(v
);
299 } else /* key without parameter passed */
305 int proc_cmdline_get_key_many_internal(ProcCmdlineFlags flags
, ...) {
306 _cleanup_free_
char *line
= NULL
;
307 bool processing_efi
= true;
312 /* The PROC_CMDLINE_VALUE_OPTIONAL flag doesn't really make sense for proc_cmdline_get_key_many(), let's make
314 assert(!FLAGS_SET(flags
, PROC_CMDLINE_VALUE_OPTIONAL
));
316 /* This call may clobber arguments on failure! */
318 if (!FLAGS_SET(flags
, PROC_CMDLINE_IGNORE_EFI_OPTIONS
)) {
319 r
= systemd_efi_options_variable(&line
);
320 if (r
< 0 && r
!= -ENODATA
)
321 log_debug_errno(r
, "Failed to get SystemdOptions EFI variable, ignoring: %m");
326 _cleanup_free_
char *word
= NULL
;
328 r
= proc_cmdline_extract_first(&p
, &word
, flags
);
332 /* We finished with this command line. If this was the EFI one, then let's proceed with the regular one */
333 if (processing_efi
) {
334 processing_efi
= false;
337 r
= proc_cmdline(&line
);
354 k
= va_arg(ap
, const char*);
358 assert_se(v
= va_arg(ap
, char**));
360 e
= proc_cmdline_key_startswith(word
, k
);
361 if (e
&& *e
== '=') {
362 r
= free_and_strdup(v
, e
+ 1);