chiark / gitweb /
unit: introduce ConditionFileIsExecutable= and use it where we check for a binary...
[elogind.git] / src / modules-load.c
index 44ff02e7b83c46518008a7c49899ddf029a1bc59..d76defa515732896d61f2d11a9ce1e7b6b432860 100644 (file)
 #include "util.h"
 #include "strv.h"
 
-/* This reads all module names listed in /etc/modules.d/?*.modules 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)
-                return 0;
-
-        return endswith(d->d_name, ".modules");
-}
-
 int main(int argc, char *argv[]) {
-        struct dirent **de = NULL;
-        int r = 1, 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.");
-                return 1;
+                return EXIT_FAILURE;
         }
 
         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
@@ -71,41 +53,34 @@ int main(int argc, char *argv[]) {
 
         n_arguments = n_allocated = 3;
 
-        if ((n = scandir("/etc/modules.d/", &de, scandir_filter, alphasort)) < 0) {
-
-                if (errno == ENOENT)
-                        r = 0;
-                else
-                        log_error("Failed to enumerate /etc/modules.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 = 0;
+        r = EXIT_SUCCESS;
 
-        for (i = 0; i < n; i++) {
-                int k;
-                char *fn;
+        STRV_FOREACH(fn, files) {
                 FILE *f;
 
-                k = asprintf(&fn, "/etc/modules.d/%s", de[i]->d_name);
-                free(de[i]);
-
-                if (k < 0) {
-                        log_error("Failed to allocate file name.");
-                        r = 1;
-                        continue;
-                }
-
-                f = fopen(fn, "re");
-                free(fn);
-
+                f = fopen(*fn, "re");
                 if (!f) {
-                        log_error("Failed to open %s: %m", fn);
-                        r = 1;
+                        if (errno == ENOENT)
+                                continue;
+
+                        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;
 
@@ -130,7 +105,7 @@ int main(int argc, char *argv[]) {
                                 if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
                                         log_error("Failed to increase module array size.");
                                         free(t);
-                                        r = 1;
+                                        r = EXIT_FAILURE;
                                         continue;
                                 }
 
@@ -142,15 +117,14 @@ int main(int argc, char *argv[]) {
                 }
 
                 if (ferror(f)) {
-                        r = 1;
+                        r = EXIT_FAILURE;
                         log_error("Failed to read from file: %m");
                 }
 
                 fclose(f);
         }
 
-        free(de);
-
+        strv_free(files);
 finish:
 
         if (n_arguments > 3) {
@@ -158,7 +132,7 @@ finish:
                 execv("/sbin/modprobe", arguments);
 
                 log_error("Failed to execute /sbin/modprobe: %m");
-                r = 1;
+                r = EXIT_FAILURE;
         }
 
         strv_free(arguments);