X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fdev-setup.c;h=25ad918b85d16946b478a4e0b93df86a0ce0502e;hb=d896ac2d2fbce41a0b11a0618a685adeaf18b8fe;hp=b0ac02d4611ef6e7110624d5764036161b350b5f;hpb=f274ece0f76b5709408821e317e87aef76123db6;p=elogind.git diff --git a/src/shared/dev-setup.c b/src/shared/dev-setup.c index b0ac02d46..25ad918b8 100644 --- a/src/shared/dev-setup.c +++ b/src/shared/dev-setup.c @@ -20,59 +20,53 @@ ***/ #include -#include #include -#include -#include #include -#include "dev-setup.h" -#include "log.h" -#include "macro.h" #include "util.h" #include "label.h" +#include "path-util.h" +#include "dev-setup.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(const char *prefix) { - const char *j, *k; - +int dev_setup(const char *prefix, uid_t uid, gid_t gid) { 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"; + const char *j, *k; + int r; + NULSTR_FOREACH_PAIR(j, k, symlinks) { + _cleanup_free_ char *link_name = NULL; + const char *n; - if (prefix) { - char *linkname; + if (j[0] == '-') { + j++; - if (asprintf(&linkname, "%s/%s", prefix, k) < 0) { - log_oom(); - break; - } + if (access(j, F_OK) < 0) + continue; + } - symlink_and_label(j, linkname); - free(linkname); + if (prefix) { + link_name = prefix_root(prefix, k); + if (!link_name) + return -ENOMEM; + + n = link_name; } else - symlink_and_label(j, k); + n = k; + + r = symlink_label(j, n); + if (r < 0) + log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n); + + if (uid != UID_INVALID || gid != GID_INVALID) + if (lchown(n, uid, gid) < 0) + log_debug_errno(errno, "Failed to chown %s: %m", n); } + + return 0; }