1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

tree-wide: make parse_proc_cmdline() strip "rd." prefix automatically

This stripping is contolled by a new boolean parameter. When the parameter
is true, it means that the caller does not care about the distinction between
initrd and real root, and wants to act on both rd-dot-prefixed and unprefixed
parameters in the initramfs, and only on the unprefixed parameters in real
root. If the parameter is false, behaviour is the same as before.

Changes by caller:
log.c (systemd.log_*):      changed to accept rd-dot-prefix params
pid1:                       no change, custom logic
cryptsetup-generator:       no change, still accepts rd-dot-prefix params
debug-generator:            no change, does not accept rd-dot-prefix params
fsck:                       changed to accept rd-dot-prefix params
fstab-generator:            no change, custom logic
gpt-auto-generator:         no change, custom logic
hibernate-resume-generator: no change, does not accept rd-dot-prefix params
journald:                   changed to accept rd-dot-prefix params
modules-load:               no change, still accepts rd-dot-prefix params
quote-check:                no change, does not accept rd-dot-prefix params
udevd:                      no change, still accepts rd-dot-prefix params

I added support for "rd." params in the three cases where I think it's
useful: logging, fsck options, journald forwarding options.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2016-10-22 15:31:14 -04:00
parent 92e724670f
commit d7f69e16f1
15 changed files with 30 additions and 28 deletions

View File

@ -1012,7 +1012,7 @@ void log_parse_environment(void) {
/* Only try to read the command line in daemons.
We assume that anything that has a controlling
tty is user stuff. */
(void) parse_proc_cmdline(parse_proc_cmdline_item, NULL);
(void) parse_proc_cmdline(parse_proc_cmdline_item, NULL, true);
e = secure_getenv("SYSTEMD_LOG_TARGET");
if (e && log_set_target_from_string(e) < 0)

View File

@ -42,7 +42,9 @@ int proc_cmdline(char **ret) {
return read_one_line_file("/proc/cmdline", ret);
}
int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value, void *data), void *data) {
int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value, void *data),
void *data,
bool strip_prefix) {
_cleanup_free_ char *line = NULL;
const char *p;
int r;
@ -56,7 +58,7 @@ int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value, voi
p = line;
for (;;) {
_cleanup_free_ char *word = NULL;
char *value = NULL;
char *value = NULL, *unprefixed;
r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
if (r < 0)
@ -66,14 +68,15 @@ int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value, voi
/* Filter out arguments that are intended only for the
* initrd */
if (!in_initrd() && startswith(word, "rd."))
unprefixed = startswith(word, "rd.");
if (unprefixed && !in_initrd())
continue;
value = strchr(word, '=');
if (value)
*(value++) = 0;
r = parse_item(word, value, data);
r = parse_item(strip_prefix && unprefixed ? unprefixed : word, value, data);
if (r < 0)
return r;
}

View File

@ -20,7 +20,9 @@
***/
int proc_cmdline(char **ret);
int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value, void *data), void *data);
int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value, void *data),
void *data,
bool strip_prefix);
int get_proc_cmdline_key(const char *parameter, char **value);
int shall_restore_state(void);

View File

@ -1570,7 +1570,7 @@ int main(int argc, char *argv[]) {
}
if (arg_system) {
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
}

View File

@ -282,7 +282,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
crypto_device *d;
_cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
if (STR_IN_SET(key, "luks", "rd.luks") && value) {
if (streq(key, "luks") && value) {
r = parse_boolean(value);
if (r < 0)
@ -290,7 +290,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
else
arg_enabled = r;
} else if (STR_IN_SET(key, "luks.crypttab", "rd.luks.crypttab") && value) {
} else if (streq(key, "luks.crypttab") && value) {
r = parse_boolean(value);
if (r < 0)
@ -298,7 +298,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
else
arg_read_crypttab = r;
} else if (STR_IN_SET(key, "luks.uuid", "rd.luks.uuid") && value) {
} else if (streq(key, "luks.uuid") && value) {
d = get_crypto_device(startswith(value, "luks-") ? value+5 : value);
if (!d)
@ -306,7 +306,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
d->create = arg_whitelist = true;
} else if (STR_IN_SET(key, "luks.options", "rd.luks.options") && value) {
} else if (streq(key, "luks.options") && value) {
r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
if (r == 2) {
@ -320,7 +320,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
} else if (free_and_strdup(&arg_default_options, value) < 0)
return log_oom();
} else if (STR_IN_SET(key, "luks.key", "rd.luks.key") && value) {
} else if (streq(key, "luks.key") && value) {
r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
if (r == 2) {
@ -334,7 +334,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
} else if (free_and_strdup(&arg_default_keyfile, value) < 0)
return log_oom();
} else if (STR_IN_SET(key, "luks.name", "rd.luks.name") && value) {
} else if (streq(key, "luks.name") && value) {
r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
if (r == 2) {
@ -478,7 +478,7 @@ int main(int argc, char *argv[]) {
if (!arg_disks)
goto cleanup;
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, true);
if (r < 0) {
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
r = EXIT_FAILURE;

View File

@ -178,7 +178,7 @@ int main(int argc, char *argv[]) {
goto finish;
}
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");

View File

@ -293,7 +293,7 @@ int main(int argc, char *argv[]) {
umask(0022);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, true);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");

View File

@ -674,7 +674,7 @@ int main(int argc, char *argv[]) {
umask(0022);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");

View File

@ -1018,7 +1018,7 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS;
}
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");

View File

@ -88,7 +88,7 @@ int main(int argc, char *argv[]) {
if (!in_initrd())
return EXIT_SUCCESS;
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");

View File

@ -1900,7 +1900,7 @@ int server_init(Server *s) {
journal_reset_metrics(&s->runtime_storage.metrics);
server_parse_config_file(s);
parse_proc_cmdline(parse_proc_cmdline_item, s);
parse_proc_cmdline(parse_proc_cmdline_item, s, true);
if (!!s->rate_limit_interval ^ !!s->rate_limit_burst) {
log_debug("Setting both rate limit interval and burst from "USEC_FMT",%u to 0,0",

View File

@ -62,7 +62,7 @@ static int add_modules(const char *p) {
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
int r;
if (STR_IN_SET(key, "modules-load", "rd.modules-load") && value) {
if (streq(key, "modules-load") && value) {
r = add_modules(value);
if (r < 0)
return r;
@ -226,7 +226,7 @@ int main(int argc, char *argv[]) {
umask(0022);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, true);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");

View File

@ -88,7 +88,7 @@ int main(int argc, char *argv[]) {
umask(0022);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");

View File

@ -36,7 +36,7 @@ static int parse_item(const char *key, const char *value, void *data) {
}
static void test_parse_proc_cmdline(void) {
assert_se(parse_proc_cmdline(parse_item, &obj) >= 0);
assert_se(parse_proc_cmdline(parse_item, &obj, true) >= 0);
}
static void test_runlevel_to_target(void) {

View File

@ -1370,9 +1370,6 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
if (!value)
return 0;
if (startswith(key, "rd."))
key += strlen("rd.");
if (streq(key, "udev.log-priority") && value) {
r = util_log_priority(value);
if (r >= 0)
@ -1652,7 +1649,7 @@ int main(int argc, char *argv[]) {
if (r <= 0)
goto exit;
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, true);
if (r < 0)
log_warning_errno(r, "failed to parse kernel command line, ignoring: %m");