diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-11-12 12:41:23 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-11-14 17:01:54 +0100 |
commit | 78b30ee056796e7fbb2eb5e8ed9363511da9fa5d (patch) | |
tree | 6cbec11c62c974d55dfd837111f64d783c32e0a6 /src/basic | |
parent | proc-cmdline: split out rd. prefix handling in proc_cmdline_parse_given() and... (diff) | |
download | systemd-78b30ee056796e7fbb2eb5e8ed9363511da9fa5d.tar.xz systemd-78b30ee056796e7fbb2eb5e8ed9363511da9fa5d.zip |
proc-cmdline: introduce new proc_cmdline_get_key_many() helper
This is like parse_env_file(), but from the kernel command line
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/proc-cmdline.c | 56 | ||||
-rw-r--r-- | src/basic/proc-cmdline.h | 3 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c index 112f3b7aa2..1670001364 100644 --- a/src/basic/proc-cmdline.c +++ b/src/basic/proc-cmdline.c @@ -251,6 +251,62 @@ int proc_cmdline_get_bool(const char *key, bool *ret) { return 1; } +int proc_cmdline_get_key_many_internal(ProcCmdlineFlags flags, ...) { + _cleanup_free_ char *line = NULL; + const char *p; + va_list ap; + int r, ret = 0; + + /* The PROC_CMDLINE_VALUE_OPTIONAL flag doesn't really make sense for proc_cmdline_get_key_many(), let's make + * this clear. */ + assert(!FLAGS_SET(flags, PROC_CMDLINE_VALUE_OPTIONAL)); + + /* This call may clobber arguments on failure! */ + + r = proc_cmdline(&line); + if (r < 0) + return r; + + p = line; + for (;;) { + _cleanup_free_ char *word = NULL; + + r = proc_cmdline_extract_first(&p, &word, flags); + if (r < 0) + return r; + if (r == 0) + break; + + va_start(ap, flags); + + for (;;) { + char **v; + const char *k, *e; + + k = va_arg(ap, const char*); + if (!k) + break; + + assert_se(v = va_arg(ap, char**)); + + e = proc_cmdline_key_startswith(word, k); + if (e && *e == '=') { + r = free_and_strdup(v, e + 1); + if (r < 0) { + va_end(ap); + return r; + } + + ret++; + } + } + + va_end(ap); + } + + return ret; +} + int shall_restore_state(void) { bool ret; int r; diff --git a/src/basic/proc-cmdline.h b/src/basic/proc-cmdline.h index cf04cb46c2..ff04379fbd 100644 --- a/src/basic/proc-cmdline.h +++ b/src/basic/proc-cmdline.h @@ -21,6 +21,9 @@ int proc_cmdline_parse(const proc_cmdline_parse_t parse, void *userdata, ProcCmd int proc_cmdline_get_key(const char *parameter, ProcCmdlineFlags flags, char **value); int proc_cmdline_get_bool(const char *key, bool *ret); +int proc_cmdline_get_key_many_internal(ProcCmdlineFlags flags, ...); +#define proc_cmdline_get_key_many(flags, ...) proc_cmdline_get_key_many_internal(flags, __VA_ARGS__, NULL) + char *proc_cmdline_key_startswith(const char *s, const char *prefix); bool proc_cmdline_key_streq(const char *x, const char *y); |