chiark / gitweb /
shared: add formats-util.h
[elogind.git] / src / shared / dev-setup.c
index 0b3d648acb879daedc77ef1aacbdd010155e3ef8..cb15da8a5d0bb5f415866c60ef6a130680b9e4c7 100644 (file)
 ***/
 
 #include <errno.h>
-#include <sys/stat.h>
 #include <stdlib.h>
-#include <string.h>
-#include <assert.h>
 #include <unistd.h>
 
 #include "dev-setup.h"
-#include "log.h"
-#include "macro.h"
 #include "util.h"
 #include "label.h"
 
-static int symlink_and_label(const char *old_path, const char *new_path) {
-        int r;
-
-        assert(old_path);
-        assert(new_path);
-
-        r = label_context_set(new_path, S_IFLNK);
-        if (r < 0)
-                return r;
-
-        if (symlink(old_path, new_path) < 0)
-                r = -errno;
-
-        label_context_clear();
-
-        return r;
-}
-
-void dev_setup(void) {
+int dev_setup(const char *prefix) {
         const char *j, *k;
 
         static const char symlinks[] =
-                "/proc/kcore\0"      "/dev/core\0"
+                "-/proc/kcore\0"     "/dev/core\0"
                 "/proc/self/fd\0"    "/dev/fd\0"
                 "/proc/self/fd/0\0"  "/dev/stdin\0"
                 "/proc/self/fd/1\0"  "/dev/stdout\0"
                 "/proc/self/fd/2\0"  "/dev/stderr\0";
 
-        NULSTR_FOREACH_PAIR(j, k, symlinks)
-                symlink_and_label(j, k);
+        NULSTR_FOREACH_PAIR(j, k, symlinks) {
+                if (j[0] == '-') {
+                        j++;
+
+                        if (access(j, F_OK) < 0)
+                                continue;
+                }
+
+                if (prefix) {
+                        _cleanup_free_ char *link_name = NULL;
+
+                        link_name = strjoin(prefix, "/", k, NULL);
+                        if (!link_name)
+                                return -ENOMEM;
+
+                        symlink_label(j, link_name);
+                } else
+                        symlink_label(j, k);
+        }
+
+        return 0;
 }