util-lib: move /proc/cmdline parsing code to proc-cmdline.[ch]

This commit is contained in:
Lennart Poettering
2015-10-27 13:25:57 +01:00
parent d4510856a0
commit 4e731273ed
24 changed files with 199 additions and 126 deletions
-114
View File
@@ -963,120 +963,6 @@ bool id128_is_valid(const char *s) {
return true;
}
int shall_restore_state(void) {
_cleanup_free_ char *value = NULL;
int r;
r = get_proc_cmdline_key("systemd.restore_state=", &value);
if (r < 0)
return r;
if (r == 0)
return true;
return parse_boolean(value) != 0;
}
int proc_cmdline(char **ret) {
assert(ret);
if (detect_container() > 0)
return get_process_cmdline(1, 0, false, ret);
else
return read_one_line_file("/proc/cmdline", ret);
}
int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value)) {
_cleanup_free_ char *line = NULL;
const char *p;
int r;
assert(parse_item);
r = proc_cmdline(&line);
if (r < 0)
return r;
p = line;
for (;;) {
_cleanup_free_ char *word = NULL;
char *value = NULL;
r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
if (r < 0)
return r;
if (r == 0)
break;
/* Filter out arguments that are intended only for the
* initrd */
if (!in_initrd() && startswith(word, "rd."))
continue;
value = strchr(word, '=');
if (value)
*(value++) = 0;
r = parse_item(word, value);
if (r < 0)
return r;
}
return 0;
}
int get_proc_cmdline_key(const char *key, char **value) {
_cleanup_free_ char *line = NULL, *ret = NULL;
bool found = false;
const char *p;
int r;
assert(key);
r = proc_cmdline(&line);
if (r < 0)
return r;
p = line;
for (;;) {
_cleanup_free_ char *word = NULL;
const char *e;
r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
if (r < 0)
return r;
if (r == 0)
break;
/* Filter out arguments that are intended only for the
* initrd */
if (!in_initrd() && startswith(word, "rd."))
continue;
if (value) {
e = startswith(word, key);
if (!e)
continue;
r = free_and_strdup(&ret, e);
if (r < 0)
return r;
found = true;
} else {
if (streq(word, key))
found = true;
}
}
if (value) {
*value = ret;
ret = NULL;
}
return found;
}
int container_get_leader(const char *machine, pid_t *pid) {
_cleanup_free_ char *s = NULL, *class = NULL;
const char *p;