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

systemd / systemd / 24642716378

18 Apr 2026 10:29PM UTC coverage: 70.661% (-1.2%) from 71.872%
24642716378

push

github

web-flow
iovec-wrapper: fix memleak, rename functions for consistency, and introduce several helper functions (#41689)

227 of 237 new or added lines in 4 files covered. (95.78%)

3483 existing lines in 82 files now uncovered.

76021 of 107585 relevant lines covered (70.66%)

4993286.11 hits per line

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

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

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

6
#include "alloc-util.h"
7
#include "chase.h"
8
#include "errno-util.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 "mountpoint-util.h"
15
#include "nulstr-util.h"
16
#include "path-util.h"
17
#include "stat-util.h"
18
#include "string-util.h"
19
#include "strv.h"
20
#include "unaligned.h"
21

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

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

38
        assert(err < 0);
×
39

40
        if (ERRNO_IS_NEG_NOT_SUPPORTED(err))
×
41
                return false;
42
        if (ERRNO_IS_NEG_PRIVILEGE(err))
×
43
                return false;
44

45
        return !IN_SET(err, -EOVERFLOW, -EINVAL);
×
46
}
47

48
int name_to_handle_at_loop(
97,110✔
49
                int fd,
50
                const char *path,
51
                struct file_handle **ret_handle,
52
                int *ret_mnt_id,
53
                uint64_t *ret_unique_mnt_id,
54
                int flags) {
55

56
        int r;
97,110✔
57

58
        assert(fd >= 0 || fd == AT_FDCWD);
97,110✔
59
        assert((flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|AT_HANDLE_FID)) == 0);
97,110✔
60

61
        /* We need to invoke name_to_handle_at() in a loop, given that it might return EOVERFLOW when the specified
62
         * buffer is too small. Note that in contrast to what the docs might suggest, MAX_HANDLE_SZ is only good as a
63
         * start value, it is not an upper bound on the buffer size required.
64
         *
65
         * This improves on raw name_to_handle_at() also in one other regard: ret_handle and ret_mnt_id can be passed
66
         * as NULL if there's no interest in either.
67
         *
68
         * If unique mount id is requested via ret_unique_mnt_id, try AT_HANDLE_MNT_ID_UNIQUE flag first
69
         * (needs kernel v6.12), and fall back to statx() if not supported. If neither worked, and caller
70
         * also specifies ret_mnt_id, then the old-style mount id is returned, -EUNATCH otherwise. */
71

72
        if (isempty(path)) {
97,110✔
73
                flags |= AT_EMPTY_PATH;
89,484✔
74
                path = "";
89,484✔
75
        }
76

77
        for (size_t n = ORIGINAL_MAX_HANDLE_SZ;;) {
97,110✔
78
                _cleanup_free_ struct file_handle *h = NULL;
97,110✔
79

80
                h = malloc0(offsetof(struct file_handle, f_handle) + n);
97,110✔
81
                if (!h)
97,110✔
82
                        return -ENOMEM;
83

84
                h->handle_bytes = n;
97,110✔
85

86
                if (ret_unique_mnt_id) {
97,110✔
87
                        /* Here, explicitly initialize mnt_id, otherwise valgrind complains:
88
                         *
89
                         * ==175708== Conditional jump or move depends on uninitialised value(s)
90
                         * ==175708==    at 0x4BC33D1: inode_same_at (stat-util.c:610)
91
                         * ==175708==    by 0x4BF1972: inode_same (stat-util.h:86)
92
                         */
93
                        uint64_t mnt_id = 0;
43,845✔
94

95
                        /* The kernel will still use this as uint64_t pointer */
96
                        r = name_to_handle_at(fd, path, h, (int *) &mnt_id, flags|AT_HANDLE_MNT_ID_UNIQUE);
43,845✔
97
                        if (r >= 0) {
43,845✔
98
                                if (ret_handle)
43,845✔
99
                                        *ret_handle = TAKE_PTR(h);
43,845✔
100

101
                                *ret_unique_mnt_id = mnt_id;
43,845✔
102

103
                                if (ret_mnt_id)
43,845✔
104
                                        *ret_mnt_id = -1;
26,471✔
105

106
                                return 1;
43,845✔
107
                        }
UNCOV
108
                        if (errno == EOVERFLOW)
×
UNCOV
109
                                goto grow;
×
UNCOV
110
                        if (errno != EINVAL)
×
UNCOV
111
                                return -errno;
×
112
                }
113

114
                int mnt_id;
53,265✔
115
                r = name_to_handle_at(fd, path, h, &mnt_id, flags);
53,265✔
116
                if (r >= 0) {
53,265✔
117
                        if (ret_unique_mnt_id) {
53,265✔
118
                                /* Hmm, AT_HANDLE_MNT_ID_UNIQUE is not supported? Let's try to acquire
119
                                 * the unique mount id from statx() then, which has a slightly lower
120
                                 * kernel version requirement (6.8 vs 6.12). */
121

122
                                struct statx sx;
×
123
                                r = xstatx(fd, path,
×
UNCOV
124
                                           at_flags_normalize_nofollow(flags & (AT_SYMLINK_FOLLOW|AT_EMPTY_PATH))|AT_STATX_DONT_SYNC,
×
125
                                           STATX_MNT_ID_UNIQUE,
126
                                           &sx);
127
                                if (r >= 0) {
×
128
                                        if (ret_handle)
×
UNCOV
129
                                                *ret_handle = TAKE_PTR(h);
×
130

UNCOV
131
                                        *ret_unique_mnt_id = sx.stx_mnt_id;
×
132

UNCOV
133
                                        if (ret_mnt_id)
×
UNCOV
134
                                                *ret_mnt_id = -1;
×
135

UNCOV
136
                                        return 1;
×
137
                                }
UNCOV
138
                                if (r != -EUNATCH || !ret_mnt_id)
×
139
                                        return r;
140

UNCOV
141
                                *ret_unique_mnt_id = 0;
×
142
                        }
143

144
                        if (ret_handle)
53,265✔
145
                                *ret_handle = TAKE_PTR(h);
53,265✔
146

147
                        if (ret_mnt_id)
53,265✔
148
                                *ret_mnt_id = mnt_id;
9✔
149

150
                        return 0;
53,265✔
151
                }
UNCOV
152
                if (errno != EOVERFLOW)
×
153
                        return -errno;
×
154

UNCOV
155
        grow:
×
156
                /* If name_to_handle_at() didn't increase the byte size, then this EOVERFLOW is caused by
157
                 * something else (apparently EOVERFLOW is returned for untriggered nfs4 autofs mounts
158
                 * sometimes), not by the too small buffer. In that case propagate EOVERFLOW */
UNCOV
159
                if (h->handle_bytes <= n)
×
160
                        return -EOVERFLOW;
161

162
                /* The buffer was too small. Size the new buffer by what name_to_handle_at() returned. */
UNCOV
163
                n = h->handle_bytes;
×
164

165
                /* paranoia: check for overflow (note that .handle_bytes is unsigned only) */
UNCOV
166
                if (n > UINT_MAX - offsetof(struct file_handle, f_handle))
×
167
                        return -EOVERFLOW;
168
        }
169
}
170

171
int name_to_handle_at_try_fid(
43,854✔
172
                int fd,
173
                const char *path,
174
                struct file_handle **ret_handle,
175
                int *ret_mnt_id,
176
                uint64_t *ret_unique_mnt_id,
177
                int flags) {
178

179
        int r;
43,854✔
180

181
        assert(fd >= 0 || fd == AT_FDCWD);
43,854✔
182

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

187
        r = name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, ret_unique_mnt_id, flags | AT_HANDLE_FID);
43,854✔
188
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
43,854✔
189
                return r;
43,854✔
190

UNCOV
191
        return name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, ret_unique_mnt_id, flags & ~AT_HANDLE_FID);
×
192
}
193

194
int name_to_handle_at_u64(int fd, const char *path, uint64_t *ret) {
53,256✔
195
        _cleanup_free_ struct file_handle *h = NULL;
53,256✔
196
        int r;
53,256✔
197

198
        assert(fd >= 0 || fd == AT_FDCWD);
53,256✔
199

200
        /* This provides the first 64bit of the file handle. */
201

202
        r = name_to_handle_at_loop(fd, path, &h, /* ret_mnt_id= */ NULL, /* ret_unique_mnt_id= */ NULL, /* flags= */ 0);
53,256✔
203
        if (r < 0)
53,256✔
204
                return r;
205
        if (h->handle_bytes < sizeof(uint64_t))
53,256✔
206
                return -EBADMSG;
207

208
        if (ret)
53,256✔
209
                /* Note, "struct file_handle" is 32bit aligned usually, but we need to read a 64bit value from it */
210
                *ret = unaligned_read_ne64(h->f_handle);
53,255✔
211

212
        return 0;
213
}
214

215
bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) {
17,281✔
216
        if (a == b)
17,281✔
217
                return true;
218
        if (!a != !b)
17,281✔
219
                return false;
220
        if (a->handle_type != b->handle_type)
17,281✔
221
                return false;
222

223
        return memcmp_nn(a->f_handle, a->handle_bytes, b->f_handle, b->handle_bytes) == 0;
17,258✔
224
}
225

226
struct file_handle* file_handle_dup(const struct file_handle *fh) {
9,110✔
227
        _cleanup_free_ struct file_handle *fh_copy = NULL;
18,220✔
228

229
        assert(fh);
9,110✔
230

231
        fh_copy = malloc0(offsetof(struct file_handle, f_handle) + fh->handle_bytes);
9,110✔
232
        if (!fh_copy)
9,110✔
233
                return NULL;
234

235
        fh_copy->handle_bytes = fh->handle_bytes;
9,110✔
236
        fh_copy->handle_type = fh->handle_type;
9,110✔
237
        memcpy(fh_copy->f_handle, fh->f_handle, fh->handle_bytes);
9,110✔
238

239
        return TAKE_PTR(fh_copy);
9,110✔
240
}
241

242
int is_mount_point_at(int dir_fd, const char *path, int flags) {
90,137✔
243
        int r;
90,137✔
244

245
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
90,137✔
246
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
90,137✔
247

248
        if (path_equal(path, "/"))
90,137✔
249
                return true;
90,137✔
250

251
        if (dir_fd == XAT_FDROOT && isempty(path))
90,072✔
252
                return true;
253

254
        struct statx sx;
90,072✔
255
        r = xstatx_full(dir_fd, path,
90,072✔
256
                        at_flags_normalize_nofollow(flags) |
90,072✔
257
                        AT_NO_AUTOMOUNT |            /* don't trigger automounts – mounts are a local concept, hence no need to trigger automounts to determine STATX_ATTR_MOUNT_ROOT */
258
                        AT_STATX_DONT_SYNC,          /* don't go to the network for this – for similar reasons */
259
                        /* xstatx_flags = */ 0,
260
                        STATX_TYPE|STATX_INO,
261
                        /* optional_mask = */ 0,
262
                        STATX_ATTR_MOUNT_ROOT,
263
                        &sx);
264
        if (r < 0)
90,072✔
265
                return r;
266

267
        if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT))
87,527✔
268
                return true;
269

270
        /* When running on chroot environment, the root may not be a mount point, but we unconditionally
271
         * return true when the input is "/" in the above, but the shortcut may not work e.g. when the path
272
         * is relative. */
273
        struct statx sx2;
78,202✔
274
        r = xstatx(AT_FDCWD,
78,202✔
275
                   "/",
276
                   AT_STATX_DONT_SYNC,
277
                   STATX_TYPE|STATX_INO,
278
                   &sx2);
279
        if (r < 0)
78,202✔
280
                return r;
281

282
        return statx_inode_same(&sx, &sx2);
78,202✔
283
}
284

285
/* flags can be AT_SYMLINK_FOLLOW or 0 */
286
int path_is_mount_point_full(const char *path, const char *root, int flags) {
30,485✔
287
        _cleanup_close_ int dir_fd = -EBADF;
30,485✔
288
        int r;
30,485✔
289

290
        assert(path);
30,485✔
291
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
30,485✔
292

293
        if (empty_or_root(root))
30,485✔
294
                return is_mount_point_at(AT_FDCWD, path, flags);
21,041✔
295

296
        r = chase(path, root,
9,444✔
297
                  FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? 0 : CHASE_NOFOLLOW,
9,444✔
298
                  /* ret_path= */ NULL, &dir_fd);
299
        if (r < 0)
9,444✔
300
                return r;
301

302
        return is_mount_point_at(dir_fd, /* path= */ NULL, flags);
7,932✔
303
}
304

305
static int path_get_mnt_id_at_internal(int dir_fd, const char *path, bool unique, uint64_t *ret) {
9,921✔
306
        struct statx sx;
9,921✔
307
        int r;
9,921✔
308

309
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
9,921✔
310
        assert(ret);
9,921✔
311

312
        r = xstatx(dir_fd, path,
9,921✔
313
                   AT_SYMLINK_NOFOLLOW |
314
                   AT_NO_AUTOMOUNT |    /* don't trigger automounts, mnt_id is a local concept */
315
                   AT_STATX_DONT_SYNC,  /* don't go to the network, mnt_id is a local concept */
316
                   unique ? STATX_MNT_ID_UNIQUE : STATX_MNT_ID,
317
                   &sx);
318
        if (r < 0)
9,921✔
319
                return r;
9,921✔
320

321
        *ret = sx.stx_mnt_id;
9,920✔
322
        return 0;
9,920✔
323
}
324

325
int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) {
9,921✔
326
        uint64_t mnt_id;
9,921✔
327
        int r;
9,921✔
328

329
        assert(ret);
9,921✔
330

331
        r = path_get_mnt_id_at_internal(dir_fd, path, /* unique = */ false, &mnt_id);
9,921✔
332
        if (r < 0)
9,921✔
333
                return r;
9,921✔
334

335
        assert(mnt_id <= INT_MAX);
9,920✔
336
        *ret = (int) mnt_id;
9,920✔
337
        return 0;
9,920✔
338
}
339

UNCOV
340
int path_get_unique_mnt_id_at(int dir_fd, const char *path, uint64_t *ret) {
×
UNCOV
341
        return path_get_mnt_id_at_internal(dir_fd, path, /* unique = */ true, ret);
×
342
}
343

344
bool fstype_is_network(const char *fstype) {
2,075✔
345
        const char *x;
2,075✔
346

347
        x = startswith(fstype, "fuse.");
2,075✔
348
        if (x)
2,075✔
UNCOV
349
                fstype = x;
×
350

351
        if (nulstr_contains(filesystem_sets[FILESYSTEM_SET_NETWORK].value, fstype))
2,075✔
352
                return true;
2,075✔
353

354
        /* Filesystems not present in the internal database */
355
        return STR_IN_SET(fstype,
2,071✔
356
                          "davfs",
357
                          "glusterfs",
358
                          "lustre",
359
                          "sshfs");
360
}
361

UNCOV
362
bool fstype_needs_quota(const char *fstype) {
×
363
       /* 1. quotacheck needs to be run for some filesystems after they are mounted
364
        *    if the filesystem was not unmounted cleanly.
365
        * 2. You may need to run quotaon to enable quota usage tracking and/or
366
        *    enforcement.
367
        * ext2     - needs 1) and 2)
368
        * ext3     - needs 2) if configured using usrjquota/grpjquota mount options
369
        * ext4     - needs 1) if created without journal, needs 2) if created without QUOTA
370
        *            filesystem feature
371
        * reiserfs - needs 2).
372
        * jfs      - needs 2)
373
        * f2fs     - needs 2) if configured using usrjquota/grpjquota/prjjquota mount options
374
        * xfs      - nothing needed
375
        * gfs2     - nothing needed
376
        * ocfs2    - nothing needed
377
        * btrfs    - nothing needed
378
        * for reference see filesystem and quota manpages */
UNCOV
379
        return STR_IN_SET(fstype,
×
380
                          "ext2",
381
                          "ext3",
382
                          "ext4",
383
                          "reiserfs",
384
                          "jfs",
385
                          "f2fs");
386
}
387

388
bool fstype_is_api_vfs(const char *fstype) {
54✔
389
        assert(fstype);
54✔
390

391
        const FilesystemSet *fs;
54✔
392
        FOREACH_ARGUMENT(fs,
243✔
393
                         filesystem_sets + FILESYSTEM_SET_BASIC_API,
394
                         filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
395
                         filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
396
                         filesystem_sets + FILESYSTEM_SET_TEMPORARY)
397
                if (nulstr_contains(fs->value, fstype))
216✔
398
                    return true;
27✔
399

400
        /* Filesystems not present in the internal database */
401
        return STR_IN_SET(fstype,
27✔
402
                          "autofs",
403
                          "cpuset");
404
}
405

406
bool fstype_is_blockdev_backed(const char *fstype) {
29✔
407
        const char *x;
29✔
408

409
        x = startswith(fstype, "fuse.");
29✔
410
        if (x)
29✔
UNCOV
411
                fstype = x;
×
412

413
        return !STR_IN_SET(fstype, "9p", "overlay") && !fstype_is_network(fstype) && !fstype_is_api_vfs(fstype);
29✔
414
}
415

416
bool fstype_is_ro(const char *fstype) {
3,078✔
417
        /* All Linux file systems that are necessarily read-only */
418
        return STR_IN_SET(fstype,
3,078✔
419
                          "DM_verity_hash",
420
                          "cramfs",
421
                          "erofs",
422
                          "iso9660",
423
                          "squashfs");
424
}
425

426
bool fstype_can_discard(const char *fstype) {
9✔
427
        assert(fstype);
9✔
428

429
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
430
         * not be allowed in our MAC context. */
431
        if (STR_IN_SET(fstype, "btrfs", "f2fs", "ext4", "vfat", "xfs"))
9✔
432
                return true;
6✔
433

434
        /* On new kernels we can just ask the kernel */
435
        return mount_option_supported(fstype, "discard", NULL) > 0;
3✔
436
}
437

438
const char* fstype_norecovery_option(const char *fstype) {
259✔
439
        int r;
259✔
440

441
        assert(fstype);
259✔
442

443
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
444
         * not be allowed in our MAC context. */
445
        if (STR_IN_SET(fstype, "ext3", "ext4", "xfs"))
259✔
446
                return "norecovery";
20✔
447

448
        /* btrfs dropped support for the "norecovery" option in 6.8
449
         * (https://github.com/torvalds/linux/commit/a1912f712188291f9d7d434fba155461f1ebef66) and replaced
450
         * it with rescue=nologreplay, so we check for the new name first and fall back to checking for the
451
         * old name if the new name doesn't work. */
452
        if (streq(fstype, "btrfs")) {
239✔
453
                r = mount_option_supported(fstype, "rescue=nologreplay", NULL);
×
454
                if (r == -EAGAIN) {
×
UNCOV
455
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming old kernel with 'norecovery': %m");
×
UNCOV
456
                        return "norecovery";
×
457
                }
UNCOV
458
                if (r < 0)
×
UNCOV
459
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming it is not supported: %m");
×
UNCOV
460
                if (r > 0)
×
461
                        return "rescue=nologreplay";
462
        }
463

464
        /* On new kernels we can just ask the kernel */
465
        return mount_option_supported(fstype, "norecovery", NULL) > 0 ? "norecovery" : NULL;
239✔
466
}
467

468
bool fstype_can_fmask_dmask(const char *fstype) {
57✔
469
        assert(fstype);
57✔
470

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

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

484
        return STR_IN_SET(fstype,
1✔
485
                          "adfs",
486
                          "exfat",
487
                          "fat",
488
                          "hfs",
489
                          "hpfs",
490
                          "iso9660",
491
                          "msdos",
492
                          "ntfs",
493
                          "vfat");
494
}
495

496
int dev_is_devtmpfs(void) {
318✔
497
        _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
318✔
498
        int mount_id, r;
318✔
499
        char *e;
318✔
500

501
        r = path_get_mnt_id("/dev", &mount_id);
318✔
502
        if (r < 0)
318✔
503
                return r;
504

505
        r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
318✔
506
        if (r == -ENOENT)
318✔
UNCOV
507
                return proc_mounted() > 0 ? -ENOENT : -ENOSYS;
×
508
        if (r < 0)
318✔
509
                return r;
510

511
        for (;;) {
15,499✔
512
                _cleanup_free_ char *line = NULL;
15,200✔
513
                int mid;
15,499✔
514

515
                r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line);
15,499✔
516
                if (r < 0)
15,499✔
517
                        return r;
518
                if (r == 0)
15,499✔
519
                        break;
520

521
                if (sscanf(line, "%i", &mid) != 1)
15,200✔
UNCOV
522
                        continue;
×
523

524
                if (mid != mount_id)
15,200✔
525
                        continue;
14,882✔
526

527
                e = strstrafter(line, " - ");
318✔
528
                if (!e)
318✔
UNCOV
529
                        continue;
×
530

531
                /* accept any name that starts with the currently expected type */
532
                if (startswith(e, "devtmpfs"))
318✔
533
                        return true;
534
        }
535

536
        return false;
299✔
537
}
538

539
static int mount_fd(
56,664✔
540
                const char *source,
541
                int target_fd,
542
                const char *filesystemtype,
543
                unsigned long mountflags,
544
                const void *data) {
545

546
        assert(target_fd >= 0);
56,664✔
547

548
        if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) {
56,664✔
UNCOV
549
                if (errno != ENOENT)
×
UNCOV
550
                        return -errno;
×
551

552
                /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't
553
                 * mounted. Check for the latter to generate better error messages. */
UNCOV
554
                if (proc_mounted() == 0)
×
555
                        return -ENOSYS;
556

UNCOV
557
                return -ENOENT;
×
558
        }
559

560
        return 0;
56,664✔
561
}
562

563
int mount_nofollow(
58,178✔
564
                const char *source,
565
                const char *target,
566
                const char *filesystemtype,
567
                unsigned long mountflags,
568
                const void *data) {
569

570
        _cleanup_close_ int fd = -EBADF;
58,178✔
571

572
        assert(target);
58,178✔
573

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

583
        fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
58,178✔
584
        if (fd < 0)
58,178✔
585
                return -errno;
1,514✔
586

587
        return mount_fd(source, fd, filesystemtype, mountflags, data);
56,664✔
588
}
589

590
const char* mount_propagation_flag_to_string(unsigned long flags) {
114✔
591

592
        switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) {
114✔
593
        case 0:
594
                return "";
595
        case MS_SHARED:
1✔
596
                return "shared";
1✔
597
        case MS_SLAVE:
1✔
598
                return "slave";
1✔
599
        case MS_PRIVATE:
3✔
600
                return "private";
3✔
601
        }
602

UNCOV
603
        return NULL;
×
604
}
605

606
int mount_propagation_flag_from_string(const char *name, unsigned long *ret) {
11✔
607

608
        POINTER_MAY_BE_NULL(name);
11✔
609
        assert(ret);
11✔
610

611
        if (isempty(name))
11✔
612
                *ret = 0;
2✔
613
        else if (streq(name, "shared"))
9✔
614
                *ret = MS_SHARED;
2✔
615
        else if (streq(name, "slave"))
7✔
616
                *ret = MS_SLAVE;
2✔
617
        else if (streq(name, "private"))
5✔
618
                *ret = MS_PRIVATE;
3✔
619
        else
620
                return -EINVAL;
621
        return 0;
622
}
623

624
bool mount_propagation_flag_is_valid(unsigned long flag) {
2,331✔
625
        return IN_SET(flag, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE);
2,331✔
626
}
627

628
bool mount_new_api_supported(void) {
5,073✔
629
        static int cache = -1;
5,073✔
630
        int r;
5,073✔
631

632
        if (cache >= 0)
5,073✔
633
                return cache;
1,730✔
634

635
        /* This is the newest API among the ones we use, so use it as boundary */
636
        r = RET_NERRNO(mount_setattr(-EBADF, NULL, 0, NULL, 0));
3,343✔
637
        if (r == 0 || ERRNO_IS_NOT_SUPPORTED(r)) /* This should return an error if it is working properly */
3,343✔
UNCOV
638
                return (cache = false);
×
639

640
        return (cache = true);
3,343✔
641
}
642

643
int mount_option_supported(const char *fstype, const char *key, const char *value) {
967✔
644
        _cleanup_close_ int fd = -EBADF;
967✔
645
        int r;
967✔
646

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

650
        assert(fstype);
967✔
651
        assert(key);
967✔
652

653
        fd = fsopen(fstype, FSOPEN_CLOEXEC);
967✔
654
        if (fd < 0)
967✔
UNCOV
655
                return log_debug_errno(errno, "Failed to open superblock context for '%s': %m", fstype);
×
656

657
        /* Various file systems support fs context only in recent kernels (e.g. btrfs). For older kernels
658
         * fsconfig() with FSCONFIG_SET_STRING/FSCONFIG_SET_FLAG never fail. Which sucks, because we want to
659
         * use it for testing support, after all. Let's hence do a check if the file system got converted yet
660
         * first. */
661
        if (fsconfig(fd, FSCONFIG_SET_FD, "adefinitelynotexistingmountoption", NULL, fd) < 0) {
967✔
662
                /* If FSCONFIG_SET_FD is not supported for the fs, then the file system was not converted to
663
                 * the new mount API yet. If it returns EINVAL the mount option doesn't exist, but the fstype
664
                 * is converted. */
665
                if (errno == EOPNOTSUPP)
967✔
666
                        return -EAGAIN; /* fs not converted to new mount API → don't know */
667
                if (errno != EINVAL)
967✔
UNCOV
668
                        return log_debug_errno(errno, "Failed to check if file system '%s' has been converted to new mount API: %m", fstype);
×
669

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

675
        if (value)
967✔
676
                r = fsconfig(fd, FSCONFIG_SET_STRING, key, value, 0);
30✔
677
        else
678
                r = fsconfig(fd, FSCONFIG_SET_FLAG, key, NULL, 0);
937✔
679
        if (r < 0) {
967✔
680
                if (errno == EINVAL)
297✔
681
                        return false; /* EINVAL means option not supported. */
682

UNCOV
683
                return log_debug_errno(errno, "Failed to set '%s%s%s' on '%s' superblock context: %m",
×
684
                                       key, value ? "=" : "", strempty(value), fstype);
685
        }
686

687
        return true; /* works! */
688
}
689

690
bool path_below_api_vfs(const char *p) {
14,059✔
691
        assert(p);
14,059✔
692

693
        /* API VFS are either directly mounted on any of these three paths, or below it. */
694
        return PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc");
14,059✔
695
}
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