]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dev-setup.c
Merge pull request #8700 from keszybz/hibernation
[thirdparty/systemd.git] / src / shared / dev-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010-2012 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "dev-setup.h"
14 #include "label.h"
15 #include "log.h"
16 #include "path-util.h"
17 #include "user-util.h"
18 #include "util.h"
19
20 int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
21 static const char symlinks[] =
22 "-/proc/kcore\0" "/dev/core\0"
23 "/proc/self/fd\0" "/dev/fd\0"
24 "/proc/self/fd/0\0" "/dev/stdin\0"
25 "/proc/self/fd/1\0" "/dev/stdout\0"
26 "/proc/self/fd/2\0" "/dev/stderr\0";
27
28 const char *j, *k;
29 int r;
30
31 NULSTR_FOREACH_PAIR(j, k, symlinks) {
32 _cleanup_free_ char *link_name = NULL;
33 const char *n;
34
35 if (j[0] == '-') {
36 j++;
37
38 if (access(j, F_OK) < 0)
39 continue;
40 }
41
42 if (prefix) {
43 link_name = prefix_root(prefix, k);
44 if (!link_name)
45 return -ENOMEM;
46
47 n = link_name;
48 } else
49 n = k;
50
51 r = symlink_label(j, n);
52 if (r < 0)
53 log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
54
55 if (uid != UID_INVALID || gid != GID_INVALID)
56 if (lchown(n, uid, gid) < 0)
57 log_debug_errno(errno, "Failed to chown %s: %m", n);
58 }
59
60 return 0;
61 }