• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15232239991

24 May 2025 08:01PM UTC coverage: 72.053% (-0.02%) from 72.07%
15232239991

push

github

web-flow
docs: add man pages for sd_device_enumerator_[new,ref,unref,unrefp] (#37586)

For #20929.

299160 of 415197 relevant lines covered (72.05%)

703671.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/shared/dev-setup.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/sysmacros.h>
4
#include <unistd.h>
5

6
#include "alloc-util.h"
7
#include "dev-setup.h"
8
#include "fd-util.h"
9
#include "fs-util.h"
10
#include "label-util.h"
11
#include "log.h"
12
#include "mkdir-label.h"
13
#include "nulstr-util.h"
14
#include "path-util.h"
15
#include "stat-util.h"
16
#include "umask-util.h"
17
#include "user-util.h"
18

19
int dev_setup(const char *prefix, uid_t uid, gid_t gid) {
957✔
20
        static const char symlinks[] =
957✔
21
                "-/proc/kcore\0"     "/dev/core\0"
22
                "/proc/self/fd\0"    "/dev/fd\0"
23
                "/proc/self/fd/0\0"  "/dev/stdin\0"
24
                "/proc/self/fd/1\0"  "/dev/stdout\0"
25
                "/proc/self/fd/2\0"  "/dev/stderr\0";
26

27
        int r;
957✔
28

29
        NULSTR_FOREACH_PAIR(j, k, symlinks) {
10,527✔
30
                _cleanup_free_ char *link_name = NULL;
4,785✔
31
                const char *n;
4,785✔
32

33
                if (j[0] == '-') {
4,785✔
34
                        j++;
957✔
35

36
                        if (access(j, F_OK) < 0)
957✔
37
                                continue;
×
38
                }
39

40
                if (prefix) {
4,785✔
41
                        link_name = path_join(prefix, k);
4,485✔
42
                        if (!link_name)
4,485✔
43
                                return -ENOMEM;
×
44

45
                        n = link_name;
46
                } else
47
                        n = k;
48

49
                r = symlink_label(j, n);
4,785✔
50
                if (r < 0)
4,785✔
51
                        log_debug_errno(r, "Failed to symlink %s to %s: %m", j, n);
240✔
52

53
                if (uid != UID_INVALID || gid != GID_INVALID)
4,785✔
54
                        if (lchown(n, uid, gid) < 0)
1,100✔
55
                                log_debug_errno(errno, "Failed to chown %s: %m", n);
4,785✔
56
        }
57

58
        return 0;
59
}
60

61
int make_inaccessible_nodes(
419✔
62
                const char *parent_dir,
63
                uid_t uid,
64
                gid_t gid) {
65

66
        static const mode_t table[] = {
419✔
67
                S_IFREG,
68
                S_IFDIR,
69
                S_IFIFO,
70
                S_IFSOCK,
71

72
                /* The following two are likely to fail if we lack the privs for it (for example in an userns
73
                 * environment, if CAP_SYS_MKNOD is missing, or if a device node policy prohibits creation of
74
                 * device nodes with a major/minor of 0). But that's entirely fine. Consumers of these files
75
                 * should implement falling back to use a different node then, for example
76
                 * <root>/inaccessible/sock, which is close enough in behaviour and semantics for most uses.
77
                 */
78
                S_IFCHR,
79
                S_IFBLK,
80

81
                /* NB: S_IFLNK is not listed here, as there is no such thing as an inaccessible symlink */
82
        };
83

84
        _cleanup_close_ int parent_fd = -EBADF, inaccessible_fd = -EBADF;
419✔
85
        int r;
419✔
86

87
        if (!parent_dir)
419✔
88
                parent_dir = "/run/systemd";
×
89

90
        BLOCK_WITH_UMASK(0000);
838✔
91

92
        parent_fd = open(parent_dir, O_DIRECTORY|O_CLOEXEC|O_PATH, 0);
419✔
93
        if (parent_fd < 0)
419✔
94
                return -errno;
×
95

96
        inaccessible_fd = open_mkdir_at_full(parent_fd, "inaccessible", O_CLOEXEC, XO_LABEL, 0755);
419✔
97
        if (inaccessible_fd < 0)
419✔
98
                return inaccessible_fd;
99

100
        /* Set up inaccessible (and empty) file nodes of all types. This are used to as mount sources for over-mounting
101
         * ("masking") file nodes that shall become inaccessible and empty for specific containers or services. We try
102
         * to lock down these nodes as much as we can, but otherwise try to match them as closely as possible with the
103
         * underlying file, i.e. in the best case we offer the same node type as the underlying node. */
104

105
        FOREACH_ELEMENT(m, table) {
2,933✔
106
                _cleanup_free_ char *path = NULL;
2,514✔
107
                mode_t inode_type = *m;
2,514✔
108
                const char *fn;
2,514✔
109

110
                fn = inode_type_to_string(inode_type);
2,514✔
111
                path = path_join(parent_dir, fn);
2,514✔
112
                if (!path)
2,514✔
113
                        return log_oom();
×
114

115
                if (S_ISDIR(inode_type))
2,514✔
116
                        r = mkdirat_label(inaccessible_fd, fn, 0000);
419✔
117
                else
118
                        r = mknodat_label(inaccessible_fd, fn, inode_type | 0000, makedev(0, 0));
2,095✔
119
                if (r == -EEXIST) {
2,514✔
120
                        if (fchmodat(inaccessible_fd, fn, 0000, AT_SYMLINK_NOFOLLOW) < 0)
64✔
121
                                log_debug_errno(errno, "Failed to adjust access mode of existing inode '%s', ignoring: %m", path);
×
122
                } else if (r < 0) {
2,450✔
123
                        log_debug_errno(r, "Failed to create '%s', ignoring: %m", path);
123✔
124
                        continue;
123✔
125
                }
126

127
                if (uid_is_valid(uid) || gid_is_valid(gid))
3,458✔
128
                        if (fchownat(inaccessible_fd, fn, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
1,324✔
129
                                log_debug_errno(errno, "Failed to chown '%s', ignoring: %m", path);
2,391✔
130
        }
131

132
        if (fchmod(inaccessible_fd, 0555) < 0)
419✔
133
                log_debug_errno(errno, "Failed to mark inaccessible directory read-only, ignoring: %m");
419✔
134

135
        return 0;
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc