chiark / gitweb /
util: unify reading of /proc/cmdline
authorLennart Poettering <lennart@poettering.net>
Wed, 6 Nov 2013 02:15:16 +0000 (03:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 6 Nov 2013 02:15:16 +0000 (03:15 +0100)
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.

15 files changed:
src/backlight/backlight.c
src/core/condition.c
src/core/main.c
src/core/shutdown.c
src/cryptsetup/cryptsetup-generator.c
src/fsck/fsck.c
src/fstab-generator/fstab-generator.c
src/journal/journald-server.c
src/modules-load/modules-load.c
src/quotacheck/quotacheck.c
src/rfkill/rfkill.c
src/shared/util.c
src/shared/util.h
src/udev/net/link-config.c
src/udev/udevd.c

index 6c00b00..9ec29f7 100644 (file)
@@ -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))
index c53d406..436e784 100644 (file)
@@ -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);
index b21fb49..b62234e 100644 (file)
@@ -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;
index ea02b60..aa9548e 100644 (file)
@@ -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;
 
index e1798a3..9c98f0b 100644 (file)
@@ -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;
index 10c9686..9b67ccf 100644 (file)
@@ -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) {
 
index 78d7609..96b0906 100644 (file)
@@ -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;
+        bool noauto, nofail;
         char *w, *state;
-        int r;
         size_t l;
-        bool noauto, nofail;
+        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;
index 518e9ec..ce419d4 100644 (file)
 #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"
 #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;
index 49ee420..7772e3d 100644 (file)
@@ -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;
index 1f63785..622952a 100644 (file)
@@ -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) {
 
index d5b6f27..65fd9bc 100644 (file)
@@ -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);
index ef3b67b..d753781 100644 (file)
@@ -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 = proc_cmdline(&line);
+        if (r < 0)
+                return r;
+        if (r == 0) /* Container ... */
+                return 1;
 
-        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 */
+        FOREACH_WORD_QUOTED(w, l, line, state)
+                if (l == 23 && memcmp(w, "systemd.restore_state=0", 23))
+                        return 0;
+
+        return 1;
+}
+
+int proc_cmdline(char **ret) {
+        int r;
+
+        if (detect_container(NULL) > 0) {
+                *ret = NULL;
+                return 0;
         }
 
-        FOREACH_WORD_QUOTED(w, l, line, state)
-                if (strneq(w, "systemd.restore_state=0", l))
-                        return false;
+        r = read_one_line_file("/proc/cmdline", ret);
+        if (r < 0)
+                return r;
 
-        return true;
+        return 1;
 }
index d90b808..8b4c155 100644 (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);
 
-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);
index 9d0f236..7269458 100644 (file)
@@ -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))
index 1c2d00f..559d33e 100644 (file)
@@ -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[])