chiark / gitweb /
sysctl: accept multiple passed configuration files
[elogind.git] / src / sysctl.c
index 5ffb9695c6439349d6c26fa6c4a00828d41f26e8..17c6719841bacce1c70399fd3c303678a1a828c3 100644 (file)
 #include "log.h"
 #include "strv.h"
 #include "util.h"
+#include "strv.h"
 
 #define PROC_SYS_PREFIX "/proc/sys/"
 
-static const char *arg_prefix = NULL;
+static char **arg_prefixes = NULL;
 
 static int apply_sysctl(const char *property, const char *value) {
         char *p, *n;
@@ -54,10 +55,21 @@ static int apply_sysctl(const char *property, const char *value) {
                 if (*n == '.')
                         *n = '/';
 
-        if (arg_prefix && !path_startswith(p, arg_prefix)) {
-                log_debug("Skipping %s", p);
-                free(p);
-                return 0;
+        if (!strv_isempty(arg_prefixes)) {
+                char **i;
+                bool good = false;
+
+                STRV_FOREACH(i, arg_prefixes)
+                        if (path_startswith(p, *i)) {
+                                good = true;
+                                break;
+                        }
+
+                if (!good) {
+                        log_debug("Skipping %s", p);
+                        free(p);
+                        return 0;
+                }
         }
 
         k = write_one_line_file(p, value);
@@ -170,12 +182,20 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case ARG_PREFIX: {
                         char *p;
+                        char **l;
 
                         for (p = optarg; *p; p++)
                                 if (*p == '.')
                                         *p = '/';
 
-                        arg_prefix = optarg;
+                        l = strv_append(arg_prefixes, optarg);
+                        if (!l) {
+                                log_error("Out of memory");
+                                return -ENOMEM;
+                        }
+
+                        strv_free(arg_prefixes);
+                        arg_prefixes = l;
 
                         break;
                 }
@@ -199,26 +219,34 @@ int main(int argc, char *argv[]) {
         if (r <= 0)
                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 
-        if (argc-optind > 1) {
-                log_error("This program expects one or no arguments.");
-                return EXIT_FAILURE;
-        }
-
         log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
         log_open();
 
-        if (argc > optind)
-                r = apply_file(argv[optind], false);
-        else {
+        umask(0022);
+
+        if (argc > optind) {
+                int i;
+
+                for (i = optind; i < argc; i++) {
+                        int k;
+
+                        k = apply_file(argv[i], false);
+                        if (k < 0 && r == 0)
+                                r = k;
+                }
+        } else {
                 char **files, **f;
+                int k;
 
                 r = conf_files_list(&files, ".conf",
-                                    "/run/sysctl.d",
                                     "/etc/sysctl.d",
+                                    "/run/sysctl.d",
                                     "/usr/local/lib/sysctl.d",
                                     "/usr/lib/sysctl.d",
+#ifdef HAVE_SPLIT_USR
                                     "/lib/sysctl.d",
+#endif
                                     NULL);
                 if (r < 0) {
                         log_error("Failed to enumerate sysctl.d files: %s", strerror(-r));
@@ -226,17 +254,19 @@ int main(int argc, char *argv[]) {
                 }
 
                 STRV_FOREACH(f, files) {
-                        int k;
-
                         k = apply_file(*f, true);
                         if (k < 0 && r == 0)
                                 r = k;
                 }
 
-                apply_file("/etc/sysctl.conf", true);
+                k = apply_file("/etc/sysctl.conf", true);
+                if (k < 0 && r == 0)
+                        r = k;
 
                 strv_free(files);
         }
 finish:
+        strv_free(arg_prefixes);
+
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }