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

systemd / systemd / 26922495997

03 Jun 2026 09:23PM UTC coverage: 72.843% (-0.04%) from 72.884%
26922495997

push

github

bluca
hwdb: add Griffin PowerMate

336801 of 462365 relevant lines covered (72.84%)

1329395.69 hits per line

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

80.36
/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(
112,516✔
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;
112,516✔
57

58
        assert(fd >= 0 || fd == AT_FDCWD);
112,516✔
59
        assert((flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|AT_HANDLE_FID)) == 0);
112,516✔
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)) {
112,516✔
73
                flags |= AT_EMPTY_PATH;
100,517✔
74
                path = "";
100,517✔
75
        }
76

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

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

84
                h->handle_bytes = n;
112,516✔
85

86
                if (ret_unique_mnt_id) {
112,516✔
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;
52,336✔
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);
52,336✔
97
                        if (r >= 0) {
52,336✔
98
                                if (ret_handle)
52,336✔
99
                                        *ret_handle = TAKE_PTR(h);
52,336✔
100

101
                                *ret_unique_mnt_id = mnt_id;
52,336✔
102

103
                                if (ret_mnt_id)
52,336✔
104
                                        *ret_mnt_id = -1;
30,725✔
105

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

114
                int mnt_id;
60,180✔
115
                r = name_to_handle_at(fd, path, h, &mnt_id, flags);
60,180✔
116
                if (r >= 0) {
60,180✔
117
                        if (ret_unique_mnt_id) {
60,180✔
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,
×
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)
×
129
                                                *ret_handle = TAKE_PTR(h);
×
130

131
                                        *ret_unique_mnt_id = sx.stx_mnt_id;
×
132

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

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

141
                                *ret_unique_mnt_id = 0;
×
142
                        }
143

144
                        if (ret_handle)
60,180✔
145
                                *ret_handle = TAKE_PTR(h);
60,180✔
146

147
                        if (ret_mnt_id)
60,180✔
148
                                *ret_mnt_id = mnt_id;
9✔
149

150
                        return 0;
151
                }
152
                if (errno != EOVERFLOW)
×
153
                        return -errno;
×
154

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 */
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. */
163
                n = h->handle_bytes;
×
164

165
                /* paranoia: check for overflow (note that .handle_bytes is unsigned only) */
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(
52,345✔
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;
52,345✔
180

181
        assert(fd >= 0 || fd == AT_FDCWD);
52,345✔
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);
52,345✔
188
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
52,345✔
189
                return r;
190

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) {
60,171✔
195
        _cleanup_free_ struct file_handle *h = NULL;
60,171✔
196
        int r;
60,171✔
197

198
        assert(fd >= 0 || fd == AT_FDCWD);
60,171✔
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);
60,171✔
203
        if (r < 0)
60,171✔
204
                return r;
205
        if (h->handle_bytes < sizeof(uint64_t))
60,171✔
206
                return -EBADMSG;
207

208
        if (ret)
60,171✔
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);
60,170✔
211

212
        return 0;
213
}
214

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

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

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

229
        assert(fh);
9,127✔
230

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

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

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

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

245
        assert(wildcard_fd_is_valid(dir_fd));
84,096✔
246
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
84,096✔
247

248
        if (path_equal(path, "/"))
84,096✔
249
                return true;
84,096✔
250

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

254
        struct statx sx;
84,035✔
255
        r = xstatx_full(dir_fd, path,
84,035✔
256
                        at_flags_normalize_nofollow(flags) |
84,035✔
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)
84,035✔
265
                return r;
266

267
        if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT))
81,353✔
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;
71,835✔
274
        r = xstatx(AT_FDCWD,
71,835✔
275
                   "/",
276
                   AT_STATX_DONT_SYNC,
277
                   STATX_TYPE|STATX_INO,
278
                   &sx2);
279
        if (r < 0)
71,835✔
280
                return r;
281

282
        return statx_inode_same(&sx, &sx2);
71,835✔
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) {
31,142✔
287
        _cleanup_close_ int dir_fd = -EBADF;
31,142✔
288
        int r;
31,142✔
289

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

293
        if (empty_or_root(root))
31,142✔
294
                return is_mount_point_at(AT_FDCWD, path, flags);
21,639✔
295

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

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

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

309
        assert(wildcard_fd_is_valid(dir_fd));
10,030✔
310
        assert(ret);
10,030✔
311

312
        r = xstatx(dir_fd, path,
10,030✔
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)
10,030✔
319
                return r;
10,030✔
320

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

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

329
        assert(ret);
10,030✔
330

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

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

340
int path_get_unique_mnt_id_at(int dir_fd, const char *path, uint64_t *ret) {
×
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,842✔
345
        const char *x;
2,842✔
346

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

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

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

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 */
379
        return STR_IN_SET(fstype,
×
380
                          "ext2",
381
                          "ext3",
382
                          "ext4",
383
                          "reiserfs",
384
                          "jfs",
385
                          "f2fs");
386
}
387

388
bool fstype_has_internal_quota(const char *fstype) {
×
389
        /* These filesystems have built-in quota support and do not need
390
         * external quotacheck/quotaon services - see the "nothing needed"
391
         * entries in fstype_needs_quota() above. */
392
        return STR_IN_SET(fstype,
×
393
                          "xfs",
394
                          "gfs2",
395
                          "ocfs2",
396
                          "btrfs");
397
}
398

399
bool fstype_is_api_vfs(const char *fstype) {
54✔
400
        assert(fstype);
54✔
401

402
        const FilesystemSet *fs;
54✔
403
        FOREACH_ARGUMENT(fs,
243✔
404
                         filesystem_sets + FILESYSTEM_SET_BASIC_API,
405
                         filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
406
                         filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
407
                         filesystem_sets + FILESYSTEM_SET_TEMPORARY)
408
                if (nulstr_contains(fs->value, fstype))
216✔
409
                    return true;
27✔
410

411
        /* Filesystems not present in the internal database */
412
        return STR_IN_SET(fstype,
27✔
413
                          "autofs",
414
                          "cpuset");
415
}
416

417
bool fstype_is_blockdev_backed(const char *fstype) {
29✔
418
        const char *x;
29✔
419

420
        x = startswith(fstype, "fuse.");
29✔
421
        if (x)
29✔
422
                fstype = x;
×
423

424
        return !STR_IN_SET(fstype, "9p", "overlay") && !fstype_is_network(fstype) && !fstype_is_api_vfs(fstype);
29✔
425
}
426

427
bool fstype_is_ro(const char *fstype) {
3,431✔
428
        /* All Linux file systems that are necessarily read-only */
429
        return STR_IN_SET(fstype,
3,431✔
430
                          "DM_verity_hash",
431
                          "cramfs",
432
                          "erofs",
433
                          "iso9660",
434
                          "squashfs");
435
}
436

437
bool fstype_can_discard(const char *fstype) {
11✔
438
        assert(fstype);
11✔
439

440
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
441
         * not be allowed in our MAC context. */
442
        if (STR_IN_SET(fstype, "btrfs", "f2fs", "ext4", "vfat", "xfs"))
11✔
443
                return true;
8✔
444

445
        /* On new kernels we can just ask the kernel */
446
        return mount_option_supported(fstype, "discard", NULL) > 0;
3✔
447
}
448

449
const char* fstype_norecovery_option(const char *fstype) {
262✔
450
        int r;
262✔
451

452
        assert(fstype);
262✔
453

454
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
455
         * not be allowed in our MAC context. */
456
        if (STR_IN_SET(fstype, "ext3", "ext4", "xfs"))
262✔
457
                return "norecovery";
21✔
458

459
        /* btrfs dropped support for the "norecovery" option in 6.8
460
         * (https://github.com/torvalds/linux/commit/a1912f712188291f9d7d434fba155461f1ebef66) and replaced
461
         * it with rescue=nologreplay, so we check for the new name first and fall back to checking for the
462
         * old name if the new name doesn't work. */
463
        if (streq(fstype, "btrfs")) {
241✔
464
                r = mount_option_supported(fstype, "rescue=nologreplay", NULL);
×
465
                if (r == -EAGAIN) {
×
466
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming old kernel with 'norecovery': %m");
×
467
                        return "norecovery";
468
                }
469
                if (r < 0)
×
470
                        log_debug_errno(r, "Failed to check for btrfs 'rescue=nologreplay' option, assuming it is not supported: %m");
×
471
                if (r > 0)
×
472
                        return "rescue=nologreplay";
473
        }
474

475
        /* On new kernels we can just ask the kernel */
476
        return mount_option_supported(fstype, "norecovery", NULL) > 0 ? "norecovery" : NULL;
241✔
477
}
478

479
bool fstype_can_fmask_dmask(const char *fstype) {
59✔
480
        assert(fstype);
59✔
481

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

488
bool fstype_can_ownership(const char *fstype) {
93✔
489
        /* File systems which are not known to not support uid/gid ownership.
490
         * For some types, this can be a bit murky. So just exclude the ones that for sure
491
         * don't support with the current implementations in Linux. */
492

493
        return !STR_IN_SET(ASSERT_PTR(fstype),
93✔
494
                           "adfs",
495
                           "exfat",
496
                           "fat",
497
                           "hfs",
498
                           "hpfs",
499
                           "msdos",
500
                           "ntfs",
501
                           "vfat");
502
}
503

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

511
        return STR_IN_SET(fstype,
1✔
512
                          "adfs",
513
                          "exfat",
514
                          "fat",
515
                          "hfs",
516
                          "hpfs",
517
                          "iso9660",
518
                          "msdos",
519
                          "ntfs",
520
                          "vfat");
521
}
522

523
int dev_is_devtmpfs(void) {
324✔
524
        _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
324✔
525
        int mount_id, r;
324✔
526
        char *e;
324✔
527

528
        r = path_get_mnt_id("/dev", &mount_id);
324✔
529
        if (r < 0)
324✔
530
                return r;
531

532
        r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
324✔
533
        if (r == -ENOENT)
324✔
534
                return proc_mounted() > 0 ? -ENOENT : -ENOSYS;
×
535
        if (r < 0)
324✔
536
                return r;
537

538
        for (;;) {
15,530✔
539
                _cleanup_free_ char *line = NULL;
15,231✔
540
                int mid;
15,530✔
541

542
                r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line);
15,530✔
543
                if (r < 0)
15,530✔
544
                        return r;
545
                if (r == 0)
15,530✔
546
                        break;
547

548
                if (sscanf(line, "%i", &mid) != 1)
15,231✔
549
                        continue;
×
550

551
                if (mid != mount_id)
15,231✔
552
                        continue;
14,907✔
553

554
                e = strstrafter(line, " - ");
324✔
555
                if (!e)
324✔
556
                        continue;
×
557

558
                /* accept any name that starts with the currently expected type */
559
                if (startswith(e, "devtmpfs"))
324✔
560
                        return true;
561
        }
562

563
        return false;
299✔
564
}
565

566
static int mount_fd(
57,645✔
567
                const char *source,
568
                int target_fd,
569
                const char *filesystemtype,
570
                unsigned long mountflags,
571
                const void *data) {
572

573
        assert(target_fd >= 0);
57,645✔
574

575
        if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) {
57,645✔
576
                if (errno != ENOENT)
×
577
                        return -errno;
×
578

579
                /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't
580
                 * mounted. Check for the latter to generate better error messages. */
581
                if (proc_mounted() == 0)
×
582
                        return -ENOSYS;
583

584
                return -ENOENT;
×
585
        }
586

587
        return 0;
57,645✔
588
}
589

590
int mount_nofollow(
59,204✔
591
                const char *source,
592
                const char *target,
593
                const char *filesystemtype,
594
                unsigned long mountflags,
595
                const void *data) {
596

597
        _cleanup_close_ int fd = -EBADF;
59,204✔
598

599
        assert(target);
59,204✔
600

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

610
        fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
59,204✔
611
        if (fd < 0)
59,204✔
612
                return -errno;
1,559✔
613

614
        return mount_fd(source, fd, filesystemtype, mountflags, data);
57,645✔
615
}
616

617
const char* mount_propagation_flag_to_string(unsigned long flags) {
3,676✔
618

619
        switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) {
3,676✔
620
        case 0:
621
                return "";
622
        case MS_SHARED:
1✔
623
                return "shared";
1✔
624
        case MS_SLAVE:
1✔
625
                return "slave";
1✔
626
        case MS_PRIVATE:
3✔
627
                return "private";
3✔
628
        }
629

630
        return NULL;
×
631
}
632

633
int mount_propagation_flag_from_string(const char *name, unsigned long *ret) {
11✔
634

635
        POINTER_MAY_BE_NULL(name);
11✔
636
        assert(ret);
11✔
637

638
        if (isempty(name))
11✔
639
                *ret = 0;
2✔
640
        else if (streq(name, "shared"))
9✔
641
                *ret = MS_SHARED;
2✔
642
        else if (streq(name, "slave"))
7✔
643
                *ret = MS_SLAVE;
2✔
644
        else if (streq(name, "private"))
5✔
645
                *ret = MS_PRIVATE;
3✔
646
        else
647
                return -EINVAL;
648
        return 0;
649
}
650

651
bool mount_propagation_flag_is_valid(unsigned long flag) {
2,371✔
652
        return IN_SET(flag, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE);
2,371✔
653
}
654

655
bool mount_new_api_supported(void) {
5,142✔
656
        static int cache = -1;
5,142✔
657
        int r;
5,142✔
658

659
        if (cache >= 0)
5,142✔
660
                return cache;
1,757✔
661

662
        /* This is the newest API among the ones we use, so use it as boundary */
663
        r = RET_NERRNO(mount_setattr(-EBADF, NULL, 0, NULL, 0));
3,385✔
664
        if (r == 0 || ERRNO_IS_NOT_SUPPORTED(r)) /* This should return an error if it is working properly */
3,385✔
665
                return (cache = false);
×
666

667
        return (cache = true);
3,385✔
668
}
669

670
int mount_option_supported(const char *fstype, const char *key, const char *value) {
1,013✔
671
        _cleanup_close_ int fd = -EBADF;
1,013✔
672
        int r;
1,013✔
673

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

677
        assert(fstype);
1,013✔
678
        assert(key);
1,013✔
679

680
        fd = fsopen(fstype, FSOPEN_CLOEXEC);
1,013✔
681
        if (fd < 0)
1,013✔
682
                return log_debug_errno(errno, "Failed to open superblock context for '%s': %m", fstype);
×
683

684
        /* Various file systems support fs context only in recent kernels (e.g. btrfs). For older kernels
685
         * fsconfig() with FSCONFIG_SET_STRING/FSCONFIG_SET_FLAG never fail. Which sucks, because we want to
686
         * use it for testing support, after all. Let's hence do a check if the file system got converted yet
687
         * first. */
688
        if (fsconfig(fd, FSCONFIG_SET_FD, "adefinitelynotexistingmountoption", NULL, fd) < 0) {
1,013✔
689
                /* If FSCONFIG_SET_FD is not supported for the fs, then the file system was not converted to
690
                 * the new mount API yet. If it returns EINVAL the mount option doesn't exist, but the fstype
691
                 * is converted. */
692
                if (errno == EOPNOTSUPP)
1,013✔
693
                        return -EAGAIN; /* fs not converted to new mount API → don't know */
694
                if (errno != EINVAL)
1,013✔
695
                        return log_debug_errno(errno, "Failed to check if file system '%s' has been converted to new mount API: %m", fstype);
×
696

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

702
        if (value)
1,013✔
703
                r = fsconfig(fd, FSCONFIG_SET_STRING, key, value, 0);
30✔
704
        else
705
                r = fsconfig(fd, FSCONFIG_SET_FLAG, key, NULL, 0);
983✔
706
        if (r < 0) {
1,013✔
707
                if (errno == EINVAL)
305✔
708
                        return false; /* EINVAL means option not supported. */
709

710
                return log_debug_errno(errno, "Failed to set '%s%s%s' on '%s' superblock context: %m",
×
711
                                       key, value ? "=" : "", strempty(value), fstype);
712
        }
713

714
        return true; /* works! */
715
}
716

717
bool path_below_api_vfs(const char *p) {
15,564✔
718
        assert(p);
15,564✔
719

720
        /* API VFS are either directly mounted on any of these three paths, or below it. */
721
        return PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc");
15,564✔
722
}
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