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:
parent
1f0cd86b3d
commit
74df0fca09
|
@ -291,7 +291,7 @@ int main(int argc, char *argv[]) {
|
|||
* device probing should be complete), so that the validity
|
||||
* 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;
|
||||
|
||||
if (!validate_device(udev, device))
|
||||
|
|
|
@ -86,14 +86,11 @@ static bool test_kernel_command_line(const char *parameter) {
|
|||
|
||||
assert(parameter);
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return false;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
equal = !!strchr(parameter, '=');
|
||||
pl = strlen(parameter);
|
||||
|
|
|
@ -691,19 +691,14 @@ static int parse_config_file(void) {
|
|||
static int parse_proc_cmdline(void) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
char *w, *state;
|
||||
int r;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
/* Don't read /proc/cmdline if we are in a container, since
|
||||
* that is only relevant for the host system */
|
||||
if (detect_container(NULL) > 0)
|
||||
return 0;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
_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[]
|
||||
* instead. */
|
||||
|
||||
for (a = argv; a < argv + argc; a++)
|
||||
if ((r = parse_proc_cmdline_word(*a)) < 0) {
|
||||
for (a = argv; a < argv + argc; a++) {
|
||||
r = parse_proc_cmdline_word(*a);
|
||||
if (r < 0) {
|
||||
log_error("Failed on cmdline argument %s: %s", *a, strerror(-r));
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -140,8 +140,8 @@ int main(int argc, char *argv[]) {
|
|||
int cmd, r;
|
||||
|
||||
/* suppress shutdown status output if 'quiet' is used */
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r >= 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r > 0) {
|
||||
char *w, *state;
|
||||
size_t l;
|
||||
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
#include "util.h"
|
||||
#include "unit-name.h"
|
||||
#include "mkdir.h"
|
||||
#include "virt.h"
|
||||
#include "strv.h"
|
||||
#include "fileio.h"
|
||||
|
||||
|
@ -241,20 +240,21 @@ static int create_disk(
|
|||
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;
|
||||
char *w = NULL, *state = NULL;
|
||||
int r;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return 0;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
_cleanup_free_ char *word = NULL;
|
||||
|
|
|
@ -35,7 +35,6 @@
|
|||
#include "bus-util.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-errors.h"
|
||||
#include "virt.h"
|
||||
#include "fileio.h"
|
||||
#include "udev-util.h"
|
||||
|
||||
|
@ -75,17 +74,14 @@ static void start_target(const char *target) {
|
|||
|
||||
static int parse_proc_cmdline(void) {
|
||||
char *line, *w, *state;
|
||||
int r;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return 0;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
|
||||
|
|
|
@ -32,7 +32,6 @@
|
|||
#include "mount-setup.h"
|
||||
#include "special.h"
|
||||
#include "mkdir.h"
|
||||
#include "virt.h"
|
||||
#include "fileio.h"
|
||||
|
||||
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) {
|
||||
_cleanup_free_ char *what = NULL, *type = NULL, *opts = NULL, *line = NULL;
|
||||
char *w, *state;
|
||||
int r;
|
||||
size_t l;
|
||||
bool noauto, nofail;
|
||||
char *w, *state;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
log_error("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
opts = strdup("ro");
|
||||
type = strdup("auto");
|
||||
|
@ -477,17 +476,14 @@ static int parse_new_root_from_proc_cmdline(void) {
|
|||
static int parse_proc_cmdline(void) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
char *w, *state;
|
||||
int r;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return 0;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
_cleanup_free_ char *word = NULL;
|
||||
|
|
|
@ -27,10 +27,10 @@
|
|||
#include <sys/timerfd.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 "mkdir.h"
|
||||
#include "hashmap.h"
|
||||
|
@ -38,20 +38,19 @@
|
|||
#include "socket-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "list.h"
|
||||
#include "virt.h"
|
||||
#include "missing.h"
|
||||
#include "conf-parser.h"
|
||||
#include "selinux-util.h"
|
||||
#include "journal-internal.h"
|
||||
#include "journal-vacuum.h"
|
||||
#include "journal-authenticate.h"
|
||||
#include "journald-server.h"
|
||||
#include "journald-rate-limit.h"
|
||||
#include "journald-kmsg.h"
|
||||
#include "journald-syslog.h"
|
||||
#include "journald-stream.h"
|
||||
#include "journald-console.h"
|
||||
#include "journald-native.h"
|
||||
#include "selinux-util.h"
|
||||
#include "journald-server.h"
|
||||
|
||||
#ifdef HAVE_ACL
|
||||
#include <sys/acl.h>
|
||||
|
@ -1313,17 +1312,14 @@ static int open_signalfd(Server *s) {
|
|||
static int server_parse_proc_cmdline(Server *s) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
char *w, *state;
|
||||
int r;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return 0;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
_cleanup_free_ char *word;
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
#include "util.h"
|
||||
#include "strv.h"
|
||||
#include "conf-files.h"
|
||||
#include "virt.h"
|
||||
#include "fileio.h"
|
||||
|
||||
static char **arg_proc_cmdline_modules = NULL;
|
||||
|
@ -78,17 +77,14 @@ static int add_modules(const char *p) {
|
|||
static int parse_proc_cmdline(void) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
char *w, *state;
|
||||
int r;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return 0;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
_cleanup_free_ char *word;
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "virt.h"
|
||||
#include "fileio.h"
|
||||
|
||||
static bool arg_skip = false;
|
||||
|
@ -38,14 +37,11 @@ static int parse_proc_cmdline(void) {
|
|||
size_t l;
|
||||
int r;
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return 0;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
if (r <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ int main(int argc, char *argv[]) {
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (streq(argv[1], "load") && restore_state()) {
|
||||
if (streq(argv[1], "load") && shall_restore_state()) {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
|
||||
r = read_one_line_file(saved, &value);
|
||||
|
|
|
@ -5992,24 +5992,36 @@ int split_pair(const char *s, const char *sep, char **l, char **r) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
bool restore_state(void) {
|
||||
int shall_restore_state(void) {
|
||||
_cleanup_free_ char *line;
|
||||
char *w, *state;
|
||||
int r;
|
||||
size_t l;
|
||||
int r;
|
||||
|
||||
if (detect_container(NULL) > 0)
|
||||
return true;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
|
||||
return true; /* something is very wrong, let's not make it worse */
|
||||
}
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) /* Container ... */
|
||||
return 1;
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state)
|
||||
if (strneq(w, "systemd.restore_state=0", l))
|
||||
return false;
|
||||
if (l == 23 && memcmp(w, "systemd.restore_state=0", 23))
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
bool restore_state(void);
|
||||
int shall_restore_state(void);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
int proc_cmdline(char **ret);
|
||||
|
|
|
@ -195,11 +195,11 @@ static bool enable_name_policy(void) {
|
|||
int r;
|
||||
size_t l;
|
||||
|
||||
r = read_one_line_file("/proc/cmdline", &line);
|
||||
if (r < 0) {
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
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)
|
||||
if (strneq(w, "net.ifnames=0", l))
|
||||
|
|
|
@ -857,10 +857,15 @@ static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
|
|||
*/
|
||||
static void kernel_cmdline_options(struct udev *udev)
|
||||
{
|
||||
char *line, *w, *state;
|
||||
_cleanup_free_ char *line = NULL;
|
||||
char *w, *state;
|
||||
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;
|
||||
|
||||
FOREACH_WORD_QUOTED(w, l, line, state) {
|
||||
|
@ -890,8 +895,6 @@ static void kernel_cmdline_options(struct udev *udev)
|
|||
|
||||
free(s);
|
||||
}
|
||||
|
||||
free(line);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
|
Loading…
Reference in New Issue