chiark / gitweb /
modules-load: filter out double modules
[elogind.git] / src / modules-load.c
index 2dd432695f5f4c028264cd111b5f5912cfcbb567..17b5e0bf80d5b3213d39eeb6ea1941509098287d 100644 (file)
 #include "util.h"
 #include "strv.h"
 
-/* This reads all module names listed in /etc/modules-load.d/?*.conf and
- * loads them into the kernel. This follows roughly Debian's way to
- * handle modules, but uses a directory of fragments instead of a
- * single /etc/modules file. */
-
-static int scandir_filter(const struct dirent *d) {
-        assert(d);
-
-        if (ignore_file(d->d_name))
-                return 0;
-
-        if (d->d_type != DT_REG &&
-            d->d_type != DT_LNK &&
-            d->d_type != DT_UNKNOWN)
-                return 0;
-
-        return endswith(d->d_name, ".conf");
-}
-
 int main(int argc, char *argv[]) {
-        struct dirent **de = NULL;
-        int r = EXIT_FAILURE, n, i;
+        int r = EXIT_FAILURE;
         char **arguments = NULL;
         unsigned n_arguments = 0, n_allocated = 0;
+        char **files, **fn;
 
         if (argc > 1) {
                 log_error("This program takes no argument.");
@@ -65,6 +46,8 @@ int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
 
+        umask(0022);
+
         if (!(arguments = strv_new("/sbin/modprobe", "-sab", "--", NULL))) {
                 log_error("Failed to allocate string array");
                 goto finish;
@@ -72,44 +55,34 @@ int main(int argc, char *argv[]) {
 
         n_arguments = n_allocated = 3;
 
-        if ((n = scandir("/etc/modules-load.d/", &de, scandir_filter, alphasort)) < 0) {
-
-                if (errno == ENOENT)
-                        r = EXIT_SUCCESS;
-                else
-                        log_error("Failed to enumerate /etc/modules-load.d/ files: %m");
-
+        if (conf_files_list(&files, ".conf",
+                            "/run/modules-load.d",
+                            "/etc/modules-load.d",
+                            "/usr/local/lib/modules-load.d",
+                            "/usr/lib/modules-load.d",
+                            "/lib/modules-load.d",
+                            NULL) < 0) {
+                log_error("Failed to enumerate modules-load.d files: %s", strerror(-r));
                 goto finish;
         }
 
         r = EXIT_SUCCESS;
 
-        for (i = 0; i < n; i++) {
-                int k;
-                char *fn;
+        STRV_FOREACH(fn, files) {
                 FILE *f;
 
-                k = asprintf(&fn, "/etc/modules-load.d/%s", de[i]->d_name);
-                free(de[i]);
-
-                if (k < 0) {
-                        log_error("Failed to allocate file name.");
-                        r = EXIT_FAILURE;
-                        continue;
-                }
-
-                f = fopen(fn, "re");
-                free(fn);
-
+                f = fopen(*fn, "re");
                 if (!f) {
                         if (errno == ENOENT)
                                 continue;
 
-                        log_error("Failed to open %s: %m", fn);
+                        log_error("Failed to open %s: %m", *fn);
+                        free(fn);
                         r = EXIT_FAILURE;
                         continue;
                 }
 
+                log_debug("apply: %s\n", *fn);
                 for (;;) {
                         char line[LINE_MAX], *l, *t;
 
@@ -153,12 +126,12 @@ int main(int argc, char *argv[]) {
                 fclose(f);
         }
 
-        free(de);
-
+        strv_free(files);
 finish:
 
         if (n_arguments > 3) {
                 arguments[n_arguments] = NULL;
+                strv_uniq(arguments);
                 execv("/sbin/modprobe", arguments);
 
                 log_error("Failed to execute /sbin/modprobe: %m");