chiark / gitweb /
Extract looping over /proc/cmdline into a shared function
[elogind.git] / src / quotacheck / quotacheck.c
index 32f3ff904d7998e237f46b0c126d19742551009c..8ff0f7f0c3eb0d862ed88accf76750d4a52172a6 100644 (file)
 #include <unistd.h>
 
 #include "util.h"
-#include "virt.h"
 #include "fileio.h"
 
 static bool arg_skip = false;
 static bool arg_force = false;
 
-static int parse_proc_cmdline(void) {
-        char *line, *w, *state;
-        int r;
-        size_t l;
-
-        if (detect_container(NULL) > 0)
-                return 0;
-
-        if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
-                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
-                return 0;
-        }
-
-        FOREACH_WORD_QUOTED(w, l, line, state) {
-
-                if (strneq(w, "quotacheck.mode=auto", l))
-                        arg_force = arg_skip = false;
-                else if (strneq(w, "quotacheck.mode=force", l))
-                        arg_force = true;
-                else if (strneq(w, "quotacheck.mode=skip", l))
-                        arg_skip = true;
-                else if (startswith(w, "quotacheck"))
-                        log_warning("Invalid quotacheck parameter. Ignoring.");
+static int parse_proc_cmdline_word(const char *w) {
+        if (streq(w, "quotacheck.mode=auto"))
+                arg_force = arg_skip = false;
+        else if (streq(w, "quotacheck.mode=force"))
+                arg_force = true;
+        else if (streq(w, "quotacheck.mode=skip"))
+                arg_skip = true;
+        else if (startswith(w, "quotacheck"))
+                log_warning("Invalid quotacheck parameter. Ignoring.");
 #ifdef HAVE_SYSV_COMPAT
-                else if (strneq(w, "forcequotacheck", l)) {
-                        log_error("Please use 'quotacheck.mode=force' rather than 'forcequotacheck' on the kernel command line.");
-                        arg_force = true;
-                }
-#endif
+        else if (streq(w, "forcequotacheck")) {
+                log_error("Please use 'quotacheck.mode=force' rather than 'forcequotacheck' on the kernel command line.");
+                arg_force = true;
         }
+#endif
 
-        free(line);
         return 0;
 }
 
@@ -77,13 +60,13 @@ static void test_files(void) {
 }
 
 int main(int argc, char *argv[]) {
+
         static const char * const cmdline[] = {
-                "/sbin/quotacheck",
+                QUOTACHECK,
                 "-anug",
                 NULL
         };
 
-        int r = EXIT_FAILURE;
         pid_t pid;
 
         if (argc > 1) {
@@ -97,28 +80,26 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        parse_proc_cmdline();
+        parse_proc_cmdline(parse_proc_cmdline_word);
         test_files();
 
         if (!arg_force) {
                 if (arg_skip)
-                        return 0;
+                        return EXIT_SUCCESS;
 
                 if (access("/run/systemd/quotacheck", F_OK) < 0)
-                        return 0;
+                        return EXIT_SUCCESS;
         }
 
-        if ((pid = fork()) < 0) {
+        pid = fork();
+        if (pid < 0) {
                 log_error("fork(): %m");
-                goto finish;
+                return EXIT_FAILURE;
         } else if (pid == 0) {
                 /* Child */
                 execv(cmdline[0], (char**) cmdline);
                 _exit(1); /* Operational error */
         }
 
-        r = wait_for_terminate_and_warn("quotacheck", pid) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
-
-finish:
-        return r;
+        return wait_for_terminate_and_warn("quotacheck", pid) >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }