chiark / gitweb /
getty: simplify things a bit
[elogind.git] / src / binfmt.c
index 6ebd212ee1dba318c14f9ccaf42682f7be345b1c..552d8cc227ec5131c1071573560197782e1b6181 100644 (file)
 #include <string.h>
 #include <stdio.h>
 #include <limits.h>
+#include <stdarg.h>
 
 #include "log.h"
+#include "hashmap.h"
+#include "strv.h"
 #include "util.h"
 
 static int delete_rule(const char *rule) {
@@ -80,6 +83,7 @@ static int apply_file(const char *path, bool ignore_enoent) {
                 return -errno;
         }
 
+        log_debug("apply: %s\n", path);
         while (!feof(f)) {
                 char l[LINE_MAX], *p;
                 int k;
@@ -111,57 +115,6 @@ finish:
         return r;
 }
 
-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");
-}
-
-static int apply_tree(const char *path) {
-        struct dirent **de = NULL;
-        int n, i, r = 0;
-
-        if ((n = scandir(path, &de, scandir_filter, alphasort)) < 0) {
-
-                if (errno == ENOENT)
-                        return 0;
-
-                log_error("Failed to enumerate %s files: %m", path);
-                return -errno;
-        }
-
-        for (i = 0; i < n; i++) {
-                char *fn;
-                int k;
-
-                k = asprintf(&fn, "%s/%s", path, de[i]->d_name);
-                free(de[i]);
-
-                if (k < 0) {
-                        log_error("Failed to allocate file name.");
-
-                        if (r == 0)
-                                r = -ENOMEM;
-                        continue;
-                }
-
-                if ((k = apply_file(fn, true)) < 0 && r == 0)
-                        r = k;
-        }
-
-        free(de);
-
-        return r;
-}
-
 int main(int argc, char *argv[]) {
         int r = 0;
 
@@ -174,14 +127,38 @@ int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
 
-        if (argc > 1)
+        umask(0022);
+
+        if (argc > 1) {
                 r = apply_file(argv[1], false);
-        else {
+        } else {
+                char **files, **f;
+
                 /* Flush out all rules */
                 write_one_line_file("/proc/sys/fs/binfmt_misc/status", "-1");
 
-                r = apply_tree("/etc/binfmt.d");
-        }
+                r = conf_files_list(&files, ".conf",
+                                    "/run/binfmt.d",
+                                    "/etc/binfmt.d",
+                                    "/usr/local/lib/binfmt.d",
+                                    "/usr/lib/binfmt.d",
+                                    NULL);
+
+                if (r < 0) {
+                        log_error("Failed to enumerate binfmt.d files: %s", strerror(-r));
+                        goto finish;
+                }
+
+                STRV_FOREACH(f, files) {
+                        int k;
 
+                        k = apply_file(*f, true);
+                        if (k < 0 && r == 0)
+                                r = k;
+                }
+
+                strv_free(files);
+        }
+finish:
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }