• 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

74.42
/src/shared/base-filesystem.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <sys/stat.h>
5
#include <syslog.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "base-filesystem.h"
10
#include "errno-util.h"
11
#include "fd-util.h"
12
#include "log.h"
13
#include "nulstr-util.h"
14
#include "path-util.h"
15
#include "umask-util.h"
16
#include "user-util.h"
17

18
typedef enum BaseFilesystemFlags {
19
        BASE_FILESYSTEM_IGNORE_ON_FAILURE = 1 << 0,
20
        BASE_FILESYSTEM_EMPTY_MARKER      = 1 << 1, /* If this is missing, then we are booting on an empty filesystem - see comment below */
21
        BASE_FILESYSTEM_EMPTY_ONLY        = 1 << 2, /* If booting on an empty filesystem create this, otherwise skip it - see comment below */
22
} BaseFilesystemFlags;
23

24
typedef struct BaseFilesystem {
25
        const char *dir;      /* directory or symlink to create */
26
        mode_t mode;
27
        const char *target;   /* if non-NULL create as symlink to this target */
28
        const char *exists;   /* conditionalize this entry on existence of this file */
29
        BaseFilesystemFlags flags; /* various modifiers for behaviour on creation */
30
} BaseFilesystem;
31

32
/* Note that as entries are processed in order, entries with BASE_FILESYSTEM_EMPTY_MARKER must be listed
33
 * before entries with BASE_FILESYSTEM_EMPTY_ONLY. */
34
static const BaseFilesystem table[] = {
35
        { "bin",      0, "usr/bin\0",                  NULL,                        BASE_FILESYSTEM_EMPTY_MARKER      },
36
        { "lib",      0, "usr/lib\0",                  NULL,                        BASE_FILESYSTEM_EMPTY_MARKER      },
37
        { "root",  0750, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
38
        { "sbin",     0, "usr/sbin\0",                 NULL,                        BASE_FILESYSTEM_EMPTY_MARKER      },
39
        { "usr",   0755, NULL,                         NULL },
40
        { "var",   0755, NULL,                         NULL },
41
        { "etc",   0755, NULL,                         NULL },
42
        { "proc",  0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
43
        { "sys",   0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
44
        { "dev",   0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
45
        { "run",   0555, NULL,                         NULL,                        BASE_FILESYSTEM_IGNORE_ON_FAILURE },
46
        /* We don't add /tmp/ here for now (even though it's necessary for regular operation), because we
47
         * want to support both cases where /tmp/ is a mount of its own (in which case we probably should set
48
         * the mode to 1555, to indicate that no one should write to it, not even root) and when it's part of
49
         * the rootfs (in which case we should set mode 1777), and we simply don't know what's right. */
50

51
        /* Various architecture ABIs define the path to the dynamic loader via the /lib64/ subdirectory of
52
         * the root directory. When booting from an otherwise empty root file system (where only /usr/ has
53
         * been mounted into) it is thus necessary to create a symlink pointing to the right subdirectory of
54
         * /usr/ first — otherwise we couldn't invoke any dynamic binary. Let's detect this case here, and
55
         * create the symlink as needed should it be missing. We prefer doing this consistently with Debian's
56
         * multiarch logic, but support Fedora-style and Arch-style multilib too. */
57
#if defined(__aarch64__)
58
        /* aarch64 ELF ABI actually says dynamic loader is in /lib/, but Fedora puts it in /lib64/ anyway and
59
         * just symlinks /lib/ld-linux-aarch64.so.1 to ../lib64/ld-linux-aarch64.so.1. For this to work
60
         * correctly, /lib64/ must be symlinked to /usr/lib64/. On the flip side, we must not create /lib64/
61
         * on Debian and derivatives as they expect the target to be different from what Fedora et al. use,
62
         * which is problematic for example when nspawn from some other distribution boots a Debian
63
         * container with only /usr/, so we only create this symlink when at least one other symlink is
64
         * missing, and let the image builder/package manager worry about not creating incomplete persistent
65
         * filesystem hierarchies instead. The key purpose of this code is to ensure we can bring up a system
66
         * with a volatile root filesystem after all. */
67
        { "lib64",    0, "usr/lib64\0"
68
                         "usr/lib\0",                "ld-linux-aarch64.so.1",       BASE_FILESYSTEM_EMPTY_ONLY        },
69
#  define KNOW_LIB64_DIRS 1
70
#elif defined(__alpha__)
71
#elif defined(__arc__) || defined(__tilegx__)
72
#elif defined(__arm__)
73
        /* No /lib64 on arm. The linker is /lib/ld-linux-armhf.so.3. */
74
#  define KNOW_LIB64_DIRS 1
75
#elif defined(__i386__) || defined(__x86_64__)
76
        { "lib64",    0, "usr/lib64\0"
77
                         "usr/lib\0",                "ld-linux-x86-64.so.2" },
78
#  define KNOW_LIB64_DIRS 1
79
#elif defined(__ia64__)
80
#elif defined(__loongarch_lp64)
81
#  define KNOW_LIB64_DIRS 1
82
#  if defined(__loongarch_double_float)
83
        { "lib64",    0, "usr/lib64\0"
84
                         "usr/lib\0",                "ld-linux-loongarch-lp64d.so.1" },
85
#  elif defined(__loongarch_single_float)
86
        { "lib64",    0, "usr/lib64\0"
87
                         "usr/lib\0",                "ld-linux-loongarch-lp64f.so.1" },
88
#  elif defined(__loongarch_soft_float)
89
        { "lib64",    0, "usr/lib64\0"
90
                         "usr/lib\0",                "ld-linux-loongarch-lp64s.so.1" },
91
#  else
92
#    error "Unknown LoongArch ABI"
93
#  endif
94
#elif defined(__m68k__)
95
        /* No link needed. */
96
#  define KNOW_LIB64_DIRS 1
97
#elif defined(_MIPS_SIM)
98
#  if _MIPS_SIM == _MIPS_SIM_ABI32
99
#  elif _MIPS_SIM == _MIPS_SIM_NABI32
100
#  elif _MIPS_SIM == _MIPS_SIM_ABI64
101
#  else
102
#    error "Unknown MIPS ABI"
103
#  endif
104
#elif defined(__powerpc__)
105
#  if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
106
        { "lib64",    0, "usr/lib64\0"
107
                         "usr/lib\0",                "ld64.so.2" },
108
#    define KNOW_LIB64_DIRS 1
109
#  elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
110
        /* powerpc64-linux-gnu */
111
#  else
112
        /* powerpc-linux-gnu */
113
#  endif
114
#elif defined(__riscv)
115
#  if __riscv_xlen == 32
116
#  elif __riscv_xlen == 64
117
        /* Same situation as for aarch64 */
118
        { "lib64",    0, "usr/lib64\0"
119
                         "usr/lib\0",                "ld-linux-riscv64-lp64d.so.1", BASE_FILESYSTEM_EMPTY_ONLY        },
120
#    define KNOW_LIB64_DIRS 1
121
#  else
122
#    error "Unknown RISC-V ABI"
123
#  endif
124
#elif defined(__s390x__)
125
        /* Same situation as for aarch64 */
126
        { "lib64",    0, "usr/lib64\0"
127
                         "usr/lib\0",                "ld-lsb-s390x.so.3",           BASE_FILESYSTEM_EMPTY_ONLY        },
128
#    define KNOW_LIB64_DIRS 1
129
#elif defined(__s390__)
130
        /* s390-linux-gnu */
131
#elif defined(__sparc__)
132
#endif
133
        /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
134
};
135

136
#ifndef KNOW_LIB64_DIRS
137
#  pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
138
#endif
139

140
int base_filesystem_create_fd(int fd, const char *root, uid_t uid, gid_t gid) {
232✔
141
        bool empty_fs = false;
232✔
142
        int r;
232✔
143

144
        assert(fd >= 0);
232✔
145
        assert(root);
232✔
146

147
        /* The "root" parameter is decoration only – it's only used as part of log messages */
148

149
        FOREACH_ELEMENT(i, table) {
3,016✔
150
                if (FLAGS_SET(i->flags, BASE_FILESYSTEM_EMPTY_ONLY) && !empty_fs)
2,784✔
151
                        continue;
×
152

153
                if (faccessat(fd, i->dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
2,784✔
154
                        continue;
2,696✔
155

156
                if (FLAGS_SET(i->flags, BASE_FILESYSTEM_EMPTY_MARKER))
88✔
157
                        empty_fs = true;
24✔
158

159
                if (i->target) { /* Create as symlink? */
88✔
160
                        const char *target = NULL;
32✔
161

162
                        /* check if one of the targets exists */
163
                        NULSTR_FOREACH(s, i->target) {
32✔
164
                                if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
32✔
165
                                        continue;
×
166

167
                                /* check if a specific file exists at the target path */
168
                                if (i->exists) {
32✔
169
                                        _cleanup_free_ char *p = NULL;
8✔
170

171
                                        p = path_join(s, i->exists);
8✔
172
                                        if (!p)
8✔
173
                                                return log_oom();
×
174

175
                                        if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
8✔
176
                                                continue;
×
177
                                }
178

179
                                target = s;
180
                                break;
181
                        }
182

183
                        if (!target)
32✔
184
                                continue;
×
185

186
                        r = RET_NERRNO(symlinkat(target, fd, i->dir));
32✔
187
                } else {
188
                        /* Create as directory. */
189
                        WITH_UMASK(0000)
112✔
190
                                r = RET_NERRNO(mkdirat(fd, i->dir, i->mode));
56✔
191
                }
192
                if (r < 0) {
56✔
193
                        bool ignore = IN_SET(r, -EEXIST, -EROFS) || FLAGS_SET(i->flags, BASE_FILESYSTEM_IGNORE_ON_FAILURE);
×
194
                        log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
×
195
                                       "Failed to create %s/%s: %m", root, i->dir);
196
                        if (ignore)
×
197
                                continue;
×
198

199
                        return r;
200
                }
201

202
                if (uid_is_valid(uid) || gid_is_valid(gid))
88✔
203
                        if (fchownat(fd, i->dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
88✔
204
                                return log_error_errno(errno, "Failed to chown %s/%s: %m", root, i->dir);
×
205
        }
206

207
        return 0;
208
}
209

210
int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
223✔
211
        _cleanup_close_ int fd = -EBADF;
223✔
212

213
        fd = open(ASSERT_PTR(root), O_DIRECTORY|O_CLOEXEC);
223✔
214
        if (fd < 0)
223✔
215
                return log_error_errno(errno, "Failed to open root file system: %m");
×
216

217
        return base_filesystem_create_fd(fd, root, uid, gid);
223✔
218
}
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