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

systemd / systemd / 14785896885

01 May 2025 10:34AM UTC coverage: 72.243% (+0.02%) from 72.225%
14785896885

push

github

yuwata
build(deps): bump softprops/action-gh-release from 2.2.1 to 2.2.2

Bumps [softprops/action-gh-release](https://github.com/softprops/action-gh-release) from 2.2.1 to 2.2.2.
- [Release notes](https://github.com/softprops/action-gh-release/releases)
- [Changelog](https://github.com/softprops/action-gh-release/blob/master/CHANGELOG.md)
- [Commits](https://github.com/softprops/action-gh-release/compare/c95fe1489...da05d5525)

---
updated-dependencies:
- dependency-name: softprops/action-gh-release
  dependency-version: 2.2.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

297212 of 411406 relevant lines covered (72.24%)

692806.75 hits per line

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

63.79
/src/basic/mountpoint-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <fcntl.h>
5
#include <sys/mount.h>
6

7
#include "alloc-util.h"
8
#include "chase.h"
9
#include "fd-util.h"
10
#include "fileio.h"
11
#include "filesystems.h"
12
#include "fs-util.h"
13
#include "log.h"
14
#include "missing_fcntl.h"
15
#include "missing_fs.h"
16
#include "missing_syscall.h"
17
#include "mkdir.h"
18
#include "mountpoint-util.h"
19
#include "nulstr-util.h"
20
#include "parse-util.h"
21
#include "path-util.h"
22
#include "stat-util.h"
23
#include "stdio-util.h"
24
#include "strv.h"
25
#include "user-util.h"
26

27
/* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of
28
 * any more currently defined value to future-proof things: if the size is increased in the API headers, and our code
29
 * is recompiled then it would cease working on old kernels, as those refuse any sizes larger than this value with
30
 * EINVAL right-away. Hence, let's disconnect ourselves from any such API changes, and stick to the original definition
31
 * from when it was introduced. We use it as a start value only anyway (see below), and hence should be able to deal
32
 * with large file handles anyway. */
33
#define ORIGINAL_MAX_HANDLE_SZ 128
34

35
bool is_name_to_handle_at_fatal_error(int err) {
36
        /* name_to_handle_at() can return "acceptable" errors that are due to the context. For example
37
         * the file system does not support name_to_handle_at() (EOPNOTSUPP), or the syscall was blocked
38
         * (EACCES/EPERM; maybe through seccomp, because we are running inside of a container), or
39
         * the mount point is not triggered yet (EOVERFLOW, think autofs+nfs4), or some general name_to_handle_at()
40
         * flakiness (EINVAL). However other errors are not supposed to happen and therefore are considered
41
         * fatal ones. */
42

43
        assert(err < 0);
3,603✔
44

45
        if (ERRNO_IS_NEG_NOT_SUPPORTED(err))
3,603✔
46
                return false;
47
        if (ERRNO_IS_NEG_PRIVILEGE(err))
×
48
                return false;
49

50
        return !IN_SET(err, -EOVERFLOW, -EINVAL);
×
51
}
52

53
int name_to_handle_at_loop(
54
                int fd,
55
                const char *path,
56
                struct file_handle **ret_handle,
57
                int *ret_mnt_id,
58
                int flags) {
59

60
        size_t n = ORIGINAL_MAX_HANDLE_SZ;
27,349✔
61

62
        assert(fd >= 0 || fd == AT_FDCWD);
27,349✔
63
        assert((flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|AT_HANDLE_FID)) == 0);
27,349✔
64

65
        /* We need to invoke name_to_handle_at() in a loop, given that it might return EOVERFLOW when the specified
66
         * buffer is too small. Note that in contrast to what the docs might suggest, MAX_HANDLE_SZ is only good as a
67
         * start value, it is not an upper bound on the buffer size required.
68
         *
69
         * This improves on raw name_to_handle_at() also in one other regard: ret_handle and ret_mnt_id can be passed
70
         * as NULL if there's no interest in either. */
71

72
        for (;;) {
27,349✔
73
                _cleanup_free_ struct file_handle *h = NULL;
×
74
                int mnt_id = -1;
27,349✔
75

76
                h = malloc0(offsetof(struct file_handle, f_handle) + n);
27,349✔
77
                if (!h)
27,349✔
78
                        return -ENOMEM;
79

80
                h->handle_bytes = n;
27,349✔
81

82
                if (name_to_handle_at(fd, strempty(path), h, &mnt_id, flags) >= 0) {
54,689✔
83

84
                        if (ret_handle)
27,349✔
85
                                *ret_handle = TAKE_PTR(h);
27,349✔
86

87
                        if (ret_mnt_id)
27,349✔
88
                                *ret_mnt_id = mnt_id;
27,349✔
89

90
                        return 0;
27,349✔
91
                }
92
                if (errno != EOVERFLOW)
×
93
                        return -errno;
×
94

95
                if (!ret_handle && ret_mnt_id && mnt_id >= 0) {
×
96

97
                        /* As it appears, name_to_handle_at() fills in mnt_id even when it returns EOVERFLOW when the
98
                         * buffer is too small, but that's undocumented. Hence, let's make use of this if it appears to
99
                         * be filled in, and the caller was interested in only the mount ID an nothing else. */
100

101
                        *ret_mnt_id = mnt_id;
×
102
                        return 0;
×
103
                }
104

105
                /* If name_to_handle_at() didn't increase the byte size, then this EOVERFLOW is caused by
106
                 * something else (apparently EOVERFLOW is returned for untriggered nfs4 autofs mounts
107
                 * sometimes), not by the too small buffer. In that case propagate EOVERFLOW */
108
                if (h->handle_bytes <= n)
×
109
                        return -EOVERFLOW;
110

111
                /* The buffer was too small. Size the new buffer by what name_to_handle_at() returned. */
112
                n = h->handle_bytes;
×
113

114
                /* paranoia: check for overflow (note that .handle_bytes is unsigned only) */
115
                if (n > UINT_MAX - offsetof(struct file_handle, f_handle))
×
116
                        return -EOVERFLOW;
117
        }
118
}
119

120
int name_to_handle_at_try_fid(
121
                int fd,
122
                const char *path,
123
                struct file_handle **ret_handle,
124
                int *ret_mnt_id,
125
                int flags) {
126

127
        int r;
27,349✔
128

129
        assert(fd >= 0 || fd == AT_FDCWD);
27,349✔
130

131
        /* First issues name_to_handle_at() with AT_HANDLE_FID. If this fails and this is not a fatal error
132
         * we'll try without the flag, in order to support older kernels that didn't have AT_HANDLE_FID
133
         * (i.e. older than Linux 6.5). */
134

135
        r = name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, flags | AT_HANDLE_FID);
27,349✔
136
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
27,349✔
137
                return r;
27,349✔
138

139
        return name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, flags & ~AT_HANDLE_FID);
×
140
}
141

142
static int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *ret_mnt_id) {
×
143
        char path[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
×
144
        _cleanup_close_ int subfd = -EBADF;
×
145
        int r;
×
146

147
        assert((flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH)) == 0);
×
148
        assert(ret_mnt_id);
×
149

150
        if ((flags & AT_EMPTY_PATH) && isempty(filename))
×
151
                xsprintf(path, "/proc/self/fdinfo/%i", fd);
×
152
        else {
153
                subfd = openat(fd, filename, O_CLOEXEC|O_PATH|(flags & AT_SYMLINK_FOLLOW ? 0 : O_NOFOLLOW));
×
154
                if (subfd < 0)
×
155
                        return -errno;
×
156

157
                xsprintf(path, "/proc/self/fdinfo/%i", subfd);
×
158
        }
159

160
        _cleanup_free_ char *p = NULL;
×
161
        r = get_proc_field(path, "mnt_id", &p);
×
162
        if (r == -ENOENT)
×
163
                return -EBADF;
164
        if (r < 0)
×
165
                return r;
166

167
        return safe_atoi(p, ret_mnt_id);
×
168
}
169

170
static bool filename_possibly_with_slash_suffix(const char *s) {
79,830✔
171
        const char *slash, *copied;
79,830✔
172

173
        /* Checks whether the specified string is either file name, or a filename with a suffix of
174
         * slashes. But nothing else.
175
         *
176
         * this is OK: foo, bar, foo/, bar/, foo//, bar///
177
         * this is not OK: "", "/", "/foo", "foo/bar", ".", ".." … */
178

179
        slash = strchr(s, '/');
79,830✔
180
        if (!slash)
79,830✔
181
                return filename_is_valid(s);
79,816✔
182

183
        if (slash - s > PATH_MAX) /* We want to allocate on the stack below, hence do a size check first */
14✔
184
                return false;
185

186
        if (slash[strspn(slash, "/")] != 0) /* Check that the suffix consist only of one or more slashes */
14✔
187
                return false;
188

189
        copied = strndupa_safe(s, slash - s);
10✔
190
        return filename_is_valid(copied);
10✔
191
}
192

193
bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) {
194
        if (a == b)
13,670✔
195
                return true;
196
        if (!a != !b)
13,670✔
197
                return false;
198
        if (a->handle_type != b->handle_type)
13,670✔
199
                return false;
200

201
        return memcmp_nn(a->f_handle, a->handle_bytes, b->f_handle, b->handle_bytes) == 0;
13,656✔
202
}
203

204
int is_mount_point_at(int fd, const char *filename, int flags) {
205
        bool fd_is_self;
80,078✔
206
        int r;
80,078✔
207

208
        assert(fd >= 0 || fd == AT_FDCWD);
80,078✔
209
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
80,078✔
210

211
        if (isempty(filename)) {
80,078✔
212
                if (fd == AT_FDCWD)
199✔
213
                        filename = ".";
214
                else {
215
                        /* If the file name is empty we'll see if the specified 'fd' is a mount point.
216
                         * That's only supported by statx(), or if the inode specified via 'fd' refers to a
217
                         * directory. Otherwise, we'll have to fail (ENOTDIR), because we have no kernel API
218
                         * to query the information we need. */
219
                        flags |= AT_EMPTY_PATH;
197✔
220
                        filename = "";
197✔
221
                }
222

223
                fd_is_self = true;
224
        } else if (STR_IN_SET(filename, ".", "./"))
79,879✔
225
                fd_is_self = true;
226
        else {
227
                /* Insist that the specified filename is actually a filename, and not a path, i.e. some inode
228
                 * further up or down the tree then immediately below the specified directory fd. */
229
                if (!filename_possibly_with_slash_suffix(filename))
79,830✔
230
                        return -EINVAL;
7✔
231

232
                fd_is_self = false;
233
        }
234

235
        /* First we will try statx()' STATX_ATTR_MOUNT_ROOT attribute, which is our ideal API, available
236
         * since kernel 5.8.
237
         *
238
         * If that fails, our second try is the name_to_handle_at() syscall, which tells us the mount id and
239
         * an opaque file "handle". It is not supported everywhere though (kernel compile-time option, not
240
         * all file systems are hooked up). If it works the mount id is usually good enough to tell us
241
         * whether something is a mount point.
242
         *
243
         * If that didn't work we will try to read the mount id from /proc/self/fdinfo/<fd>. This is almost
244
         * as good as name_to_handle_at(), however, does not return the opaque file handle. The opaque file
245
         * handle is pretty useful to detect the root directory, which we should always consider a mount
246
         * point. Hence we use this only as fallback.
247
         *
248
         * Note that traditionally the check is done via fstat()-based st_dev comparisons. However, various
249
         * file systems don't guarantee same st_dev across single fs anymore, e.g. unionfs exposes file systems
250
         * with a variety of st_dev reported. Also, btrfs subvolumes have different st_dev, even though
251
         * they aren't real mounts of their own. */
252

253
        struct statx sx = {}; /* explicitly initialize the struct to make msan silent. */
80,071✔
254
        if (statx(fd, filename,
80,071✔
255
                  at_flags_normalize_nofollow(flags) |
80,071✔
256
                  AT_NO_AUTOMOUNT |            /* don't trigger automounts – mounts are a local concept, hence no need to trigger automounts to determine STATX_ATTR_MOUNT_ROOT */
257
                  AT_STATX_DONT_SYNC,          /* don't go to the network for this – for similar reasons */
258
                  STATX_TYPE,
259
                  &sx) < 0)
260
                return -errno;
3,793✔
261

262
        if (FLAGS_SET(sx.stx_attributes_mask, STATX_ATTR_MOUNT_ROOT)) /* yay! */
76,278✔
263
                return FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT);
76,278✔
264

265
        _cleanup_free_ struct file_handle *h = NULL, *h_parent = NULL;
×
266
        int mount_id = -1, mount_id_parent = -1;
×
267
        bool nosupp = false;
×
268

269
        r = name_to_handle_at_try_fid(fd, filename, &h, &mount_id, flags);
×
270
        if (r < 0) {
×
271
                if (is_name_to_handle_at_fatal_error(r))
×
272
                        return r;
273
                if (!ERRNO_IS_NOT_SUPPORTED(r))
×
274
                        goto fallback_fdinfo;
×
275

276
                /* This file system does not support name_to_handle_at(), hence let's see if the upper fs
277
                 * supports it (in which case it is a mount point), otherwise fall back to the fdinfo logic. */
278
                nosupp = true;
279
        }
280

281
        if (fd_is_self)
×
282
                r = name_to_handle_at_try_fid(fd, "..", &h_parent, &mount_id_parent, 0); /* can't work for non-directories 😢 */
×
283
        else
284
                r = name_to_handle_at_try_fid(fd, "", &h_parent, &mount_id_parent, AT_EMPTY_PATH);
×
285
        if (r < 0) {
×
286
                if (is_name_to_handle_at_fatal_error(r))
×
287
                        return r;
288
                if (!ERRNO_IS_NOT_SUPPORTED(r))
×
289
                        goto fallback_fdinfo;
×
290
                if (nosupp)
×
291
                        /* Both the parent and the directory can't do name_to_handle_at() */
292
                        goto fallback_fdinfo;
×
293

294
                /* The parent can't do name_to_handle_at() but the directory we are
295
                 * interested in can?  If so, it must be a mount point. */
296
                return 1;
297
        }
298

299
        /* The parent can do name_to_handle_at() but the directory we are interested in can't? If
300
         * so, it must be a mount point. */
301
        if (nosupp)
×
302
                return 1;
303

304
        /* If the file handle for the directory we are interested in and its parent are identical,
305
         * we assume this is the root directory, which is a mount point. */
306
        if (file_handle_equal(h_parent, h))
×
307
                return 1;
308

309
        return mount_id != mount_id_parent;
×
310

311
fallback_fdinfo:
×
312
        r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id);
×
313
        if (r < 0)
×
314
                return r;
315

316
        if (fd_is_self)
×
317
                r = fd_fdinfo_mnt_id(fd, "..", 0, &mount_id_parent); /* can't work for non-directories 😢 */
×
318
        else
319
                r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent);
×
320
        if (r < 0)
×
321
                return r;
322

323
        if (mount_id != mount_id_parent)
×
324
                return 1;
325

326
        /* Hmm, so, the mount ids are the same. This leaves one special case though for the root file
327
         * system. For that, let's see if the parent directory has the same inode as we are interested
328
         * in. */
329

330
        struct stat a, b;
×
331

332
        /* yay for fstatat() taking a different set of flags than the other _at() above */
333
        if (fstatat(fd, filename, &a, at_flags_normalize_nofollow(flags)) < 0)
×
334
                return -errno;
×
335

336
        if (fd_is_self)
×
337
                r = fstatat(fd, "..", &b, 0);
×
338
        else
339
                r = fstatat(fd, "", &b, AT_EMPTY_PATH);
×
340
        if (r < 0)
×
341
                return -errno;
×
342

343
        /* A directory with same device and inode as its parent must be the root directory. Otherwise
344
         * not a mount point.
345
         *
346
         * NB: we avoid inode_same_at() here because it internally attempts name_to_handle_at_try_fid() first,
347
         * which is redundant. */
348
        return stat_inode_same(&a, &b);
×
349
}
350

351
/* flags can be AT_SYMLINK_FOLLOW or 0 */
352
int path_is_mount_point_full(const char *path, const char *root, int flags) {
353
        _cleanup_close_ int dfd = -EBADF;
26,395✔
354
        _cleanup_free_ char *fn = NULL;
26,395✔
355

356
        assert(path);
26,395✔
357
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
26,395✔
358

359
        if (path_equal(path, "/"))
26,395✔
360
                return 1;
361

362
        /* we need to resolve symlinks manually, we can't just rely on is_mount_point_at() to do that for us;
363
         * if we have a structure like /bin -> /usr/bin/ and /usr is a mount point, then the parent that we
364
         * look at needs to be /usr, not /. */
365
        dfd = chase_and_open_parent(path, root,
26,388✔
366
                                    CHASE_TRAIL_SLASH|(FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? 0 : CHASE_NOFOLLOW),
26,388✔
367
                                    &fn);
368
        if (dfd < 0)
26,388✔
369
                return dfd;
370

371
        return is_mount_point_at(dfd, fn, flags);
26,195✔
372
}
373

374
int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret) {
375
        int r;
×
376

377
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
×
378
        assert(ret);
×
379

380
        r = name_to_handle_at_loop(dir_fd, path, NULL, ret, isempty(path) ? AT_EMPTY_PATH : 0);
×
381
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
×
382
                return r;
×
383

384
        return fd_fdinfo_mnt_id(dir_fd, path, isempty(path) ? AT_EMPTY_PATH : 0, ret);
×
385
}
386

387
int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) {
388
        struct statx sx;
6,238✔
389

390
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
6,238✔
391
        assert(ret);
6,238✔
392

393
        if (statx(dir_fd,
12,476✔
394
                  strempty(path),
6,238✔
395
                  (isempty(path) ? AT_EMPTY_PATH : AT_SYMLINK_NOFOLLOW) |
6,238✔
396
                  AT_NO_AUTOMOUNT |    /* don't trigger automounts, mnt_id is a local concept */
397
                  AT_STATX_DONT_SYNC,  /* don't go to the network, mnt_id is a local concept */
398
                  STATX_MNT_ID,
399
                  &sx) < 0)
400
                return -errno;
1✔
401

402
        if (FLAGS_SET(sx.stx_mask, STATX_MNT_ID)) {
6,237✔
403
                *ret = sx.stx_mnt_id;
6,237✔
404
                return 0;
6,237✔
405
        }
406

407
        return path_get_mnt_id_at_fallback(dir_fd, path, ret);
×
408
}
409

410
bool fstype_is_network(const char *fstype) {
411
        const char *x;
2,040✔
412

413
        x = startswith(fstype, "fuse.");
2,040✔
414
        if (x)
2,040✔
415
                fstype = x;
×
416

417
        if (nulstr_contains(filesystem_sets[FILESYSTEM_SET_NETWORK].value, fstype))
2,040✔
418
                return true;
2,040✔
419

420
        /* Filesystems not present in the internal database */
421
        return STR_IN_SET(fstype,
2,036✔
422
                          "davfs",
423
                          "glusterfs",
424
                          "lustre",
425
                          "sshfs");
426
}
427

428
bool fstype_needs_quota(const char *fstype) {
429
       /* 1. quotacheck needs to be run for some filesystems after they are mounted
430
        *    if the filesystem was not unmounted cleanly.
431
        * 2. You may need to run quotaon to enable quota usage tracking and/or
432
        *    enforcement.
433
        * ext2     - needs 1) and 2)
434
        * ext3     - needs 2) if configured using usrjquota/grpjquota mount options
435
        * ext4     - needs 1) if created without journal, needs 2) if created without QUOTA
436
        *            filesystem feature
437
        * reiserfs - needs 2).
438
        * jfs      - needs 2)
439
        * f2fs     - needs 2) if configured using usrjquota/grpjquota/prjjquota mount options
440
        * xfs      - nothing needed
441
        * gfs2     - nothing needed
442
        * ocfs2    - nothing needed
443
        * btrfs    - nothing needed
444
        * for reference see filesystem and quota manpages */
445
        return STR_IN_SET(fstype,
×
446
                          "ext2",
447
                          "ext3",
448
                          "ext4",
449
                          "reiserfs",
450
                          "jfs",
451
                          "f2fs");
452
}
453

454
bool fstype_is_api_vfs(const char *fstype) {
455
        assert(fstype);
59✔
456

457
        const FilesystemSet *fs;
59✔
458
        FOREACH_ARGUMENT(fs,
267✔
459
                         filesystem_sets + FILESYSTEM_SET_BASIC_API,
460
                         filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
461
                         filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
462
                         filesystem_sets + FILESYSTEM_SET_TEMPORARY)
463
                if (nulstr_contains(fs->value, fstype))
236✔
464
                    return true;
28✔
465

466
        /* Filesystems not present in the internal database */
467
        return STR_IN_SET(fstype,
31✔
468
                          "autofs",
469
                          "cpuset",
470
                          "devtmpfs");
471
}
472

473
bool fstype_is_blockdev_backed(const char *fstype) {
474
        const char *x;
29✔
475

476
        x = startswith(fstype, "fuse.");
29✔
477
        if (x)
29✔
478
                fstype = x;
×
479

480
        return !streq(fstype, "9p") && !fstype_is_network(fstype) && !fstype_is_api_vfs(fstype);
29✔
481
}
482

483
bool fstype_is_ro(const char *fstype) {
484
        /* All Linux file systems that are necessarily read-only */
485
        return STR_IN_SET(fstype,
2,762✔
486
                          "DM_verity_hash",
487
                          "cramfs",
488
                          "erofs",
489
                          "iso9660",
490
                          "squashfs");
491
}
492

493
bool fstype_can_discard(const char *fstype) {
494
        assert(fstype);
4✔
495

496
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
497
         * not be allowed in our MAC context. */
498
        if (STR_IN_SET(fstype, "btrfs", "f2fs", "ext4", "vfat", "xfs"))
4✔
499
                return true;
1✔
500

501
        /* On new kernels we can just ask the kernel */
502
        return mount_option_supported(fstype, "discard", NULL) > 0;
3✔
503
}
504

505
const char* fstype_norecovery_option(const char *fstype) {
506
        int r;
162✔
507

508
        assert(fstype);
162✔
509

510
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
511
         * not be allowed in our MAC context. */
512
        if (STR_IN_SET(fstype, "ext3", "ext4", "xfs"))
162✔
513
                return "norecovery";
17✔
514

515
        /* btrfs dropped support for the "norecovery" option in 6.8
516
         * (https://github.com/torvalds/linux/commit/a1912f712188291f9d7d434fba155461f1ebef66) and replaced
517
         * it with rescue=nologreplay, so we check for the new name first and fall back to checking for the
518
         * old name if the new name doesn't work. */
519
        if (streq(fstype, "btrfs")) {
145✔
520
                r = mount_option_supported(fstype, "rescue=nologreplay", NULL);
×
521
                if (r == -EAGAIN) {
×
522
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming old kernel with 'norecovery': %m");
×
523
                        return "norecovery";
×
524
                }
525
                if (r < 0)
×
526
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming it is not supported: %m");
×
527
                if (r > 0)
×
528
                        return "rescue=nologreplay";
529
        }
530

531
        /* On new kernels we can just ask the kernel */
532
        return mount_option_supported(fstype, "norecovery", NULL) > 0 ? "norecovery" : NULL;
145✔
533
}
534

535
bool fstype_can_fmask_dmask(const char *fstype) {
536
        assert(fstype);
57✔
537

538
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
539
         * not be allowed in our MAC context. If we don't know ourselves, on new kernels we can just ask the
540
         * kernel. */
541
        return streq(fstype, "vfat") || (mount_option_supported(fstype, "fmask", "0177") > 0 && mount_option_supported(fstype, "dmask", "0077") > 0);
57✔
542
}
543

544
bool fstype_can_uid_gid(const char *fstype) {
545
        /* All file systems that have a uid=/gid= mount option that fixates the owners of all files and
546
         * directories, current and future. Note that this does *not* ask the kernel via
547
         * mount_option_supported() here because the uid=/gid= setting of various file systems mean different
548
         * things: some apply it only to the root dir inode, others to all inodes in the file system. Thus we
549
         * maintain the curated list below. 😢 */
550

551
        return STR_IN_SET(fstype,
1✔
552
                          "adfs",
553
                          "exfat",
554
                          "fat",
555
                          "hfs",
556
                          "hpfs",
557
                          "iso9660",
558
                          "msdos",
559
                          "ntfs",
560
                          "vfat");
561
}
562

563
int dev_is_devtmpfs(void) {
564
        _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
249✔
565
        int mount_id, r;
249✔
566
        char *e;
249✔
567

568
        r = path_get_mnt_id("/dev", &mount_id);
249✔
569
        if (r < 0)
249✔
570
                return r;
571

572
        r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
249✔
573
        if (r == -ENOENT)
249✔
574
                return proc_mounted() > 0 ? -ENOENT : -ENOSYS;
×
575
        if (r < 0)
249✔
576
                return r;
577

578
        for (;;) {
11,137✔
579
                _cleanup_free_ char *line = NULL;
10,903✔
580
                int mid;
11,137✔
581

582
                r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line);
11,137✔
583
                if (r < 0)
11,137✔
584
                        return r;
585
                if (r == 0)
11,137✔
586
                        break;
587

588
                if (sscanf(line, "%i", &mid) != 1)
10,903✔
589
                        continue;
×
590

591
                if (mid != mount_id)
10,903✔
592
                        continue;
10,654✔
593

594
                e = strstrafter(line, " - ");
249✔
595
                if (!e)
249✔
596
                        continue;
×
597

598
                /* accept any name that starts with the currently expected type */
599
                if (startswith(e, "devtmpfs"))
249✔
600
                        return true;
601
        }
602

603
        return false;
234✔
604
}
605

606
static int mount_fd(
65,856✔
607
                const char *source,
608
                int target_fd,
609
                const char *filesystemtype,
610
                unsigned long mountflags,
611
                const void *data) {
612

613
        assert(target_fd >= 0);
65,856✔
614

615
        if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) {
65,856✔
616
                if (errno != ENOENT)
991✔
617
                        return -errno;
991✔
618

619
                /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't
620
                 * mounted. Check for the latter to generate better error messages. */
621
                if (proc_mounted() == 0)
490✔
622
                        return -ENOSYS;
623

624
                return -ENOENT;
490✔
625
        }
626

627
        return 0;
64,865✔
628
}
629

630
int mount_nofollow(
631
                const char *source,
632
                const char *target,
633
                const char *filesystemtype,
634
                unsigned long mountflags,
635
                const void *data) {
636

637
        _cleanup_close_ int fd = -EBADF;
67,298✔
638

639
        assert(target);
67,298✔
640

641
        /* In almost all cases we want to manipulate the mount table without following symlinks, hence
642
         * mount_nofollow() is usually the way to go. The only exceptions are environments where /proc/ is
643
         * not available yet, since we need /proc/self/fd/ for this logic to work. i.e. during the early
644
         * initialization of namespacing/container stuff where /proc is not yet mounted (and maybe even the
645
         * fs to mount) we can only use traditional mount() directly.
646
         *
647
         * Note that this disables following only for the final component of the target, i.e symlinks within
648
         * the path of the target are honoured, as are symlinks in the source path everywhere. */
649

650
        fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
67,298✔
651
        if (fd < 0)
67,298✔
652
                return -errno;
1,442✔
653

654
        return mount_fd(source, fd, filesystemtype, mountflags, data);
65,856✔
655
}
656

657
const char* mount_propagation_flag_to_string(unsigned long flags) {
658

659
        switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) {
9✔
660
        case 0:
661
                return "";
662
        case MS_SHARED:
1✔
663
                return "shared";
1✔
664
        case MS_SLAVE:
1✔
665
                return "slave";
1✔
666
        case MS_PRIVATE:
3✔
667
                return "private";
3✔
668
        }
669

670
        return NULL;
×
671
}
672

673
int mount_propagation_flag_from_string(const char *name, unsigned long *ret) {
674

675
        if (isempty(name))
8✔
676
                *ret = 0;
2✔
677
        else if (streq(name, "shared"))
6✔
678
                *ret = MS_SHARED;
1✔
679
        else if (streq(name, "slave"))
5✔
680
                *ret = MS_SLAVE;
1✔
681
        else if (streq(name, "private"))
4✔
682
                *ret = MS_PRIVATE;
2✔
683
        else
684
                return -EINVAL;
685
        return 0;
686
}
687

688
bool mount_propagation_flag_is_valid(unsigned long flag) {
689
        return IN_SET(flag, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE);
2,190✔
690
}
691

692
bool mount_new_api_supported(void) {
693
        static int cache = -1;
6,010✔
694
        int r;
6,010✔
695

696
        if (cache >= 0)
6,010✔
697
                return cache;
1,604✔
698

699
        /* This is the newer API among the ones we use, so use it as boundary */
700
        r = RET_NERRNO(mount_setattr(-EBADF, NULL, 0, NULL, 0));
4,406✔
701
        if (r == 0 || ERRNO_IS_NOT_SUPPORTED(r)) /* This should return an error if it is working properly */
4,406✔
702
                return (cache = false);
×
703

704
        return (cache = true);
4,406✔
705
}
706

707
unsigned long ms_nosymfollow_supported(void) {
708
        _cleanup_close_ int fsfd = -EBADF, mntfd = -EBADF;
4,856✔
709
        static int cache = -1;
4,856✔
710

711
        /* Returns MS_NOSYMFOLLOW if it is supported, zero otherwise. */
712

713
        if (cache >= 0)
4,856✔
714
                return cache ? MS_NOSYMFOLLOW : 0;
2,530✔
715

716
        if (!mount_new_api_supported())
2,326✔
717
                goto not_supported;
×
718

719
        /* Checks if MS_NOSYMFOLLOW is supported (which was added in 5.10). We use the new mount API's
720
         * mount_setattr() call for that, which was added in 5.12, which is close enough. */
721

722
        fsfd = fsopen("tmpfs", FSOPEN_CLOEXEC);
2,326✔
723
        if (fsfd < 0) {
2,326✔
724
                if (ERRNO_IS_NOT_SUPPORTED(errno))
2✔
725
                        goto not_supported;
×
726

727
                log_debug_errno(errno, "Failed to open superblock context for tmpfs: %m");
2✔
728
                return 0;
2✔
729
        }
730

731
        if (fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) {
2,324✔
732
                if (ERRNO_IS_NOT_SUPPORTED(errno))
×
733
                        goto not_supported;
×
734

735
                log_debug_errno(errno, "Failed to create tmpfs superblock: %m");
×
736
                return 0;
×
737
        }
738

739
        mntfd = fsmount(fsfd, FSMOUNT_CLOEXEC, 0);
2,324✔
740
        if (mntfd < 0) {
2,324✔
741
                if (ERRNO_IS_NOT_SUPPORTED(errno))
×
742
                        goto not_supported;
×
743

744
                log_debug_errno(errno, "Failed to turn superblock fd into mount fd: %m");
×
745
                return 0;
×
746
        }
747

748
        if (mount_setattr(mntfd, "", AT_EMPTY_PATH|AT_RECURSIVE,
2,324✔
749
                          &(struct mount_attr) {
2,324✔
750
                                  .attr_set = MOUNT_ATTR_NOSYMFOLLOW,
751
                          }, sizeof(struct mount_attr)) < 0) {
752
                if (ERRNO_IS_NOT_SUPPORTED(errno))
×
753
                        goto not_supported;
×
754

755
                log_debug_errno(errno, "Failed to set MOUNT_ATTR_NOSYMFOLLOW mount attribute: %m");
×
756
                return 0;
×
757
        }
758

759
        cache = true;
2,324✔
760
        return MS_NOSYMFOLLOW;
2,324✔
761

762
not_supported:
×
763
        cache = false;
×
764
        return 0;
×
765
}
766

767
int mount_option_supported(const char *fstype, const char *key, const char *value) {
768
        _cleanup_close_ int fd = -EBADF;
3,616✔
769
        int r;
3,616✔
770

771
        /* Checks if the specified file system supports a mount option. Returns > 0 if it supports it, == 0 if
772
         * it does not. Return -EAGAIN if we can't determine it. And any other error otherwise. */
773

774
        assert(fstype);
3,616✔
775
        assert(key);
3,616✔
776

777
        fd = fsopen(fstype, FSOPEN_CLOEXEC);
3,616✔
778
        if (fd < 0)
3,616✔
779
                return log_debug_errno(errno, "Failed to open superblock context for '%s': %m", fstype);
1✔
780

781
        /* Various file systems support fs context only in recent kernels (e.g. btrfs). For older kernels
782
         * fsconfig() with FSCONFIG_SET_STRING/FSCONFIG_SET_FLAG never fail. Which sucks, because we want to
783
         * use it for testing support, after all. Let's hence do a check if the file system got converted yet
784
         * first. */
785
        if (fsconfig(fd, FSCONFIG_SET_FD, "adefinitelynotexistingmountoption", NULL, fd) < 0) {
3,615✔
786
                /* If FSCONFIG_SET_FD is not supported for the fs, then the file system was not converted to
787
                 * the new mount API yet. If it returns EINVAL the mount option doesn't exist, but the fstype
788
                 * is converted. */
789
                if (errno == EOPNOTSUPP)
3,615✔
790
                        return -EAGAIN; /* fs not converted to new mount API → don't know */
791
                if (errno != EINVAL)
3,615✔
792
                        return log_debug_errno(errno, "Failed to check if file system '%s' has been converted to new mount API: %m", fstype);
×
793

794
                /* So FSCONFIG_SET_FD worked, but the option didn't exist (we got EINVAL), this means the fs
795
                 * is converted. Let's now ask the actual question we wonder about. */
796
        } else
797
                return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "FSCONFIG_SET_FD worked unexpectedly for '%s', whoa!", fstype);
×
798

799
        if (value)
3,615✔
800
                r = fsconfig(fd, FSCONFIG_SET_STRING, key, value, 0);
872✔
801
        else
802
                r = fsconfig(fd, FSCONFIG_SET_FLAG, key, NULL, 0);
2,743✔
803
        if (r < 0) {
3,615✔
804
                if (errno == EINVAL)
193✔
805
                        return false; /* EINVAL means option not supported. */
806

807
                return log_debug_errno(errno, "Failed to set '%s%s%s' on '%s' superblock context: %m",
×
808
                                       key, value ? "=" : "", strempty(value), fstype);
809
        }
810

811
        return true; /* works! */
812
}
813

814
bool path_below_api_vfs(const char *p) {
815
        assert(p);
9,277✔
816

817
        /* API VFS are either directly mounted on any of these three paths, or below it. */
818
        return PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc");
9,277✔
819
}
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