util: unify reading of /proc/cmdline

Instead of individually checking for containers in each user do this
once in a new call proc_cmdline() that read the file only if we are not
in a container.
This commit is contained in:
Lennart Poettering 2013-11-06 03:15:16 +01:00
parent 1f0cd86b3d
commit 74df0fca09
15 changed files with 94 additions and 103 deletions

View file

@ -291,7 +291,7 @@ int main(int argc, char *argv[]) {
* device probing should be complete), so that the validity * device probing should be complete), so that the validity
* check at boot time doesn't have to be reliable. */ * check at boot time doesn't have to be reliable. */
if (streq(argv[1], "load") && restore_state()) { if (streq(argv[1], "load") && shall_restore_state()) {
_cleanup_free_ char *value = NULL; _cleanup_free_ char *value = NULL;
if (!validate_device(udev, device)) if (!validate_device(udev, device))

View file

@ -86,14 +86,11 @@ static bool test_kernel_command_line(const char *parameter) {
assert(parameter); assert(parameter);
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return false; if (r < 0)
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return false; return false;
}
equal = !!strchr(parameter, '='); equal = !!strchr(parameter, '=');
pl = strlen(parameter); pl = strlen(parameter);

View file

@ -691,19 +691,14 @@ static int parse_config_file(void) {
static int parse_proc_cmdline(void) { static int parse_proc_cmdline(void) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *w, *state; char *w, *state;
int r;
size_t l; size_t l;
int r;
/* Don't read /proc/cmdline if we are in a container, since r = proc_cmdline(&line);
* that is only relevant for the host system */ if (r < 0)
if (detect_container(NULL) > 0)
return 0;
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {
_cleanup_free_ char *word; _cleanup_free_ char *word;
@ -979,11 +974,13 @@ static int parse_argv(int argc, char *argv[]) {
* relevant for the container, hence we rely on argv[] * relevant for the container, hence we rely on argv[]
* instead. */ * instead. */
for (a = argv; a < argv + argc; a++) for (a = argv; a < argv + argc; a++) {
if ((r = parse_proc_cmdline_word(*a)) < 0) { r = parse_proc_cmdline_word(*a);
if (r < 0) {
log_error("Failed on cmdline argument %s: %s", *a, strerror(-r)); log_error("Failed on cmdline argument %s: %s", *a, strerror(-r));
return r; return r;
} }
}
} }
return 0; return 0;

View file

@ -140,8 +140,8 @@ int main(int argc, char *argv[]) {
int cmd, r; int cmd, r;
/* suppress shutdown status output if 'quiet' is used */ /* suppress shutdown status output if 'quiet' is used */
r = read_one_line_file("/proc/cmdline", &line); r = proc_cmdline(&line);
if (r >= 0) { if (r > 0) {
char *w, *state; char *w, *state;
size_t l; size_t l;

View file

@ -27,7 +27,6 @@
#include "util.h" #include "util.h"
#include "unit-name.h" #include "unit-name.h"
#include "mkdir.h" #include "mkdir.h"
#include "virt.h"
#include "strv.h" #include "strv.h"
#include "fileio.h" #include "fileio.h"
@ -241,20 +240,21 @@ static int create_disk(
return 0; return 0;
} }
static int parse_proc_cmdline(char ***arg_proc_cmdline_disks, char ***arg_proc_cmdline_options, char **arg_proc_cmdline_keyfile) { static int parse_proc_cmdline(
char ***arg_proc_cmdline_disks,
char ***arg_proc_cmdline_options,
char **arg_proc_cmdline_keyfile) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *w = NULL, *state = NULL; char *w = NULL, *state = NULL;
int r;
size_t l; size_t l;
int r;
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return 0; if (r < 0)
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {
_cleanup_free_ char *word = NULL; _cleanup_free_ char *word = NULL;

View file

@ -35,7 +35,6 @@
#include "bus-util.h" #include "bus-util.h"
#include "bus-error.h" #include "bus-error.h"
#include "bus-errors.h" #include "bus-errors.h"
#include "virt.h"
#include "fileio.h" #include "fileio.h"
#include "udev-util.h" #include "udev-util.h"
@ -75,17 +74,14 @@ static void start_target(const char *target) {
static int parse_proc_cmdline(void) { static int parse_proc_cmdline(void) {
char *line, *w, *state; char *line, *w, *state;
int r;
size_t l; size_t l;
int r;
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return 0; if (r < 0)
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {

View file

@ -32,7 +32,6 @@
#include "mount-setup.h" #include "mount-setup.h"
#include "special.h" #include "special.h"
#include "mkdir.h" #include "mkdir.h"
#include "virt.h"
#include "fileio.h" #include "fileio.h"
static const char *arg_dest = "/tmp"; static const char *arg_dest = "/tmp";
@ -395,16 +394,16 @@ static int parse_fstab(const char *prefix, bool initrd) {
static int parse_new_root_from_proc_cmdline(void) { static int parse_new_root_from_proc_cmdline(void) {
_cleanup_free_ char *what = NULL, *type = NULL, *opts = NULL, *line = NULL; _cleanup_free_ char *what = NULL, *type = NULL, *opts = NULL, *line = NULL;
char *w, *state;
int r;
size_t l;
bool noauto, nofail; bool noauto, nofail;
char *w, *state;
size_t l;
int r;
r = read_one_line_file("/proc/cmdline", &line); r = proc_cmdline(&line);
if (r < 0) { if (r < 0)
log_error("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
opts = strdup("ro"); opts = strdup("ro");
type = strdup("auto"); type = strdup("auto");
@ -477,17 +476,14 @@ static int parse_new_root_from_proc_cmdline(void) {
static int parse_proc_cmdline(void) { static int parse_proc_cmdline(void) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *w, *state; char *w, *state;
int r;
size_t l; size_t l;
int r;
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return 0; if (r < 0)
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {
_cleanup_free_ char *word = NULL; _cleanup_free_ char *word = NULL;

View file

@ -27,10 +27,10 @@
#include <sys/timerfd.h> #include <sys/timerfd.h>
#include <libudev.h> #include <libudev.h>
#include <systemd/sd-journal.h>
#include <systemd/sd-messages.h>
#include <systemd/sd-daemon.h>
#include "sd-journal.h"
#include "sd-messages.h"
#include "sd-daemon.h"
#include "fileio.h" #include "fileio.h"
#include "mkdir.h" #include "mkdir.h"
#include "hashmap.h" #include "hashmap.h"
@ -38,20 +38,19 @@
#include "socket-util.h" #include "socket-util.h"
#include "cgroup-util.h" #include "cgroup-util.h"
#include "list.h" #include "list.h"
#include "virt.h"
#include "missing.h" #include "missing.h"
#include "conf-parser.h" #include "conf-parser.h"
#include "selinux-util.h"
#include "journal-internal.h" #include "journal-internal.h"
#include "journal-vacuum.h" #include "journal-vacuum.h"
#include "journal-authenticate.h" #include "journal-authenticate.h"
#include "journald-server.h"
#include "journald-rate-limit.h" #include "journald-rate-limit.h"
#include "journald-kmsg.h" #include "journald-kmsg.h"
#include "journald-syslog.h" #include "journald-syslog.h"
#include "journald-stream.h" #include "journald-stream.h"
#include "journald-console.h" #include "journald-console.h"
#include "journald-native.h" #include "journald-native.h"
#include "selinux-util.h" #include "journald-server.h"
#ifdef HAVE_ACL #ifdef HAVE_ACL
#include <sys/acl.h> #include <sys/acl.h>
@ -1313,17 +1312,14 @@ static int open_signalfd(Server *s) {
static int server_parse_proc_cmdline(Server *s) { static int server_parse_proc_cmdline(Server *s) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *w, *state; char *w, *state;
int r;
size_t l; size_t l;
int r;
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return 0; if (r < 0)
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {
_cleanup_free_ char *word; _cleanup_free_ char *word;

View file

@ -33,7 +33,6 @@
#include "util.h" #include "util.h"
#include "strv.h" #include "strv.h"
#include "conf-files.h" #include "conf-files.h"
#include "virt.h"
#include "fileio.h" #include "fileio.h"
static char **arg_proc_cmdline_modules = NULL; static char **arg_proc_cmdline_modules = NULL;
@ -78,17 +77,14 @@ static int add_modules(const char *p) {
static int parse_proc_cmdline(void) { static int parse_proc_cmdline(void) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *w, *state; char *w, *state;
int r;
size_t l; size_t l;
int r;
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return 0; if (r < 0)
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {
_cleanup_free_ char *word; _cleanup_free_ char *word;

View file

@ -26,7 +26,6 @@
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
#include "virt.h"
#include "fileio.h" #include "fileio.h"
static bool arg_skip = false; static bool arg_skip = false;
@ -38,14 +37,11 @@ static int parse_proc_cmdline(void) {
size_t l; size_t l;
int r; int r;
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return 0; if (r < 0)
r = read_one_line_file("/proc/cmdline", &line);
if (r < 0) {
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return 0; return 0;
}
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {

View file

@ -95,7 +95,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (streq(argv[1], "load") && restore_state()) { if (streq(argv[1], "load") && shall_restore_state()) {
_cleanup_free_ char *value = NULL; _cleanup_free_ char *value = NULL;
r = read_one_line_file(saved, &value); r = read_one_line_file(saved, &value);

View file

@ -5992,24 +5992,36 @@ int split_pair(const char *s, const char *sep, char **l, char **r) {
return 0; return 0;
} }
bool restore_state(void) { int shall_restore_state(void) {
_cleanup_free_ char *line; _cleanup_free_ char *line;
char *w, *state; char *w, *state;
int r;
size_t l; size_t l;
int r;
if (detect_container(NULL) > 0) r = proc_cmdline(&line);
return true; if (r < 0)
return r;
r = read_one_line_file("/proc/cmdline", &line); if (r == 0) /* Container ... */
if (r < 0) { return 1;
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
return true; /* something is very wrong, let's not make it worse */
}
FOREACH_WORD_QUOTED(w, l, line, state) FOREACH_WORD_QUOTED(w, l, line, state)
if (strneq(w, "systemd.restore_state=0", l)) if (l == 23 && memcmp(w, "systemd.restore_state=0", 23))
return false; return 0;
return true; return 1;
}
int proc_cmdline(char **ret) {
int r;
if (detect_container(NULL) > 0) {
*ret = NULL;
return 0;
}
r = read_one_line_file("/proc/cmdline", ret);
if (r < 0)
return r;
return 1;
} }

View file

@ -770,7 +770,7 @@ void parse_user_at_host(char *arg, char **user, char **host);
int split_pair(const char *s, const char *sep, char **l, char **r); int split_pair(const char *s, const char *sep, char **l, char **r);
bool restore_state(void); int shall_restore_state(void);
/** /**
* Normal qsort requires base to be nonnull. Here were require * Normal qsort requires base to be nonnull. Here were require
@ -783,3 +783,5 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size,
qsort(base, nmemb, size, compar); qsort(base, nmemb, size, compar);
} }
} }
int proc_cmdline(char **ret);

View file

@ -195,11 +195,11 @@ static bool enable_name_policy(void) {
int r; int r;
size_t l; size_t l;
r = read_one_line_file("/proc/cmdline", &line); r = proc_cmdline(&line);
if (r < 0) { if (r < 0)
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
return true; /* something is very wrong, let's not make it worse */ if (r <= 0)
} return true;
FOREACH_WORD_QUOTED(w, l, line, state) FOREACH_WORD_QUOTED(w, l, line, state)
if (strneq(w, "net.ifnames=0", l)) if (strneq(w, "net.ifnames=0", l))

View file

@ -857,10 +857,15 @@ static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
*/ */
static void kernel_cmdline_options(struct udev *udev) static void kernel_cmdline_options(struct udev *udev)
{ {
char *line, *w, *state; _cleanup_free_ char *line = NULL;
char *w, *state;
size_t l; size_t l;
int r;
if (read_one_line_file("/proc/cmdline", &line) < 0) r = proc_cmdline(&line);
if (r < 0)
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
if (r <= 0)
return; return;
FOREACH_WORD_QUOTED(w, l, line, state) { FOREACH_WORD_QUOTED(w, l, line, state) {
@ -890,8 +895,6 @@ static void kernel_cmdline_options(struct udev *udev)
free(s); free(s);
} }
free(line);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])