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

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

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

88.04
/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) {
3,673✔
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);
3,673✔
39

40
        if (ERRNO_IS_NEG_NOT_SUPPORTED(err))
3,673✔
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(
78,074✔
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;
78,074✔
57

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

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

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

84
                h->handle_bytes = n;
78,074✔
85

86
                if (ret_unique_mnt_id) {
78,074✔
87
                        uint64_t mnt_id;
36,783✔
88

89
                        /* The kernel will still use this as uint64_t pointer */
90
                        r = name_to_handle_at(fd, path, h, (int *) &mnt_id, flags|AT_HANDLE_MNT_ID_UNIQUE);
36,783✔
91
                        if (r >= 0) {
36,783✔
92
                                if (ret_handle)
28,289✔
93
                                        *ret_handle = TAKE_PTR(h);
28,289✔
94

95
                                *ret_unique_mnt_id = mnt_id;
28,289✔
96

97
                                if (ret_mnt_id)
28,289✔
98
                                        *ret_mnt_id = -1;
16,819✔
99

100
                                return 1;
28,289✔
101
                        }
102
                        if (errno == EOVERFLOW)
8,494✔
103
                                goto grow;
×
104
                        if (errno != EINVAL)
8,494✔
105
                                return -errno;
×
106
                }
107

108
                int mnt_id;
49,785✔
109
                r = name_to_handle_at(fd, path, h, &mnt_id, flags);
49,785✔
110
                if (r >= 0) {
49,785✔
111
                        if (ret_unique_mnt_id) {
46,112✔
112
                                /* Hmm, AT_HANDLE_MNT_ID_UNIQUE is not supported? Let's try to acquire
113
                                 * the unique mount id from statx() then, which has a slightly lower
114
                                 * kernel version requirement (6.8 vs 6.12). */
115

116
                                struct statx sx;
8,494✔
117
                                r = xstatx(fd, path,
8,494✔
118
                                           at_flags_normalize_nofollow(flags & (AT_SYMLINK_FOLLOW|AT_EMPTY_PATH))|AT_STATX_DONT_SYNC,
8,494✔
119
                                           STATX_MNT_ID_UNIQUE,
120
                                           &sx);
121
                                if (r >= 0) {
8,494✔
122
                                        if (ret_handle)
8,494✔
123
                                                *ret_handle = TAKE_PTR(h);
8,494✔
124

125
                                        *ret_unique_mnt_id = sx.stx_mnt_id;
8,494✔
126

127
                                        if (ret_mnt_id)
8,494✔
128
                                                *ret_mnt_id = -1;
4,286✔
129

130
                                        return 1;
8,494✔
131
                                }
132
                                if (r != -EUNATCH || !ret_mnt_id)
×
133
                                        return r;
134

135
                                *ret_unique_mnt_id = 0;
×
136
                        }
137

138
                        if (ret_handle)
37,618✔
139
                                *ret_handle = TAKE_PTR(h);
37,618✔
140

141
                        if (ret_mnt_id)
37,618✔
142
                                *ret_mnt_id = mnt_id;
9✔
143

144
                        return 0;
37,618✔
145
                }
146
                if (errno != EOVERFLOW)
3,673✔
147
                        return -errno;
3,673✔
148

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

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

159
                /* paranoia: check for overflow (note that .handle_bytes is unsigned only) */
160
                if (n > UINT_MAX - offsetof(struct file_handle, f_handle))
×
161
                        return -EOVERFLOW;
162
        }
163
}
164

165
int name_to_handle_at_try_fid(
36,792✔
166
                int fd,
167
                const char *path,
168
                struct file_handle **ret_handle,
169
                int *ret_mnt_id,
170
                uint64_t *ret_unique_mnt_id,
171
                int flags) {
172

173
        int r;
36,792✔
174

175
        assert(fd >= 0 || fd == AT_FDCWD);
36,792✔
176

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

181
        r = name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, ret_unique_mnt_id, flags | AT_HANDLE_FID);
36,792✔
182
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
36,792✔
183
                return r;
36,792✔
184

185
        return name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, ret_unique_mnt_id, flags & ~AT_HANDLE_FID);
×
186
}
187

188
int name_to_handle_at_u64(int fd, const char *path, uint64_t *ret) {
41,282✔
189
        _cleanup_free_ struct file_handle *h = NULL;
41,282✔
190
        int r;
41,282✔
191

192
        assert(fd >= 0 || fd == AT_FDCWD);
41,282✔
193

194
        /* This provides the first 64bit of the file handle. */
195

196
        r = name_to_handle_at_loop(fd, path, &h, /* ret_mnt_id= */ NULL, /* ret_unique_mnt_id= */ NULL, /* flags= */ 0);
41,282✔
197
        if (r < 0)
41,282✔
198
                return r;
199
        if (h->handle_bytes < sizeof(uint64_t))
37,609✔
200
                return -EBADMSG;
201

202
        if (ret)
37,609✔
203
                /* Note, "struct file_handle" is 32bit aligned usually, but we need to read a 64bit value from it */
204
                *ret = unaligned_read_ne64(h->f_handle);
37,609✔
205

206
        return 0;
207
}
208

209
bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) {
13,760✔
210
        if (a == b)
13,760✔
211
                return true;
212
        if (!a != !b)
13,760✔
213
                return false;
214
        if (a->handle_type != b->handle_type)
13,760✔
215
                return false;
216

217
        return memcmp_nn(a->f_handle, a->handle_bytes, b->f_handle, b->handle_bytes) == 0;
13,739✔
218
}
219

220
struct file_handle* file_handle_dup(const struct file_handle *fh) {
7,266✔
221
        _cleanup_free_ struct file_handle *fh_copy = NULL;
14,532✔
222

223
        assert(fh);
7,266✔
224

225
        fh_copy = malloc0(offsetof(struct file_handle, f_handle) + fh->handle_bytes);
7,266✔
226
        if (!fh_copy)
7,266✔
227
                return NULL;
228

229
        fh_copy->handle_bytes = fh->handle_bytes;
7,266✔
230
        fh_copy->handle_type = fh->handle_type;
7,266✔
231
        memcpy(fh_copy->f_handle, fh->f_handle, fh->handle_bytes);
7,266✔
232

233
        return TAKE_PTR(fh_copy);
7,266✔
234
}
235

236
int is_mount_point_at(int dir_fd, const char *path, int flags) {
84,929✔
237
        int r;
84,929✔
238

239
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
84,929✔
240
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
84,929✔
241

242
        if (path_equal(path, "/"))
84,929✔
243
                return true;
84,929✔
244

245
        if (dir_fd == XAT_FDROOT && isempty(path))
84,869✔
246
                return true;
247

248
        struct statx sx;
84,869✔
249
        r = xstatx_full(dir_fd, path,
84,869✔
250
                        at_flags_normalize_nofollow(flags) |
84,869✔
251
                        AT_NO_AUTOMOUNT |            /* don't trigger automounts – mounts are a local concept, hence no need to trigger automounts to determine STATX_ATTR_MOUNT_ROOT */
252
                        AT_STATX_DONT_SYNC,          /* don't go to the network for this – for similar reasons */
253
                        STATX_TYPE|STATX_INO,
254
                        /* optional_mask = */ 0,
255
                        STATX_ATTR_MOUNT_ROOT,
256
                        &sx);
257
        if (r < 0)
84,869✔
258
                return r;
259

260
        if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT))
82,402✔
261
                return true;
262

263
        /* When running on chroot environment, the root may not be a mount point, but we unconditionally
264
         * return true when the input is "/" in the above, but the shortcut may not work e.g. when the path
265
         * is relative. */
266
        struct statx sx2;
73,487✔
267
        r = xstatx(AT_FDCWD,
73,487✔
268
                   "/",
269
                   AT_STATX_DONT_SYNC,
270
                   STATX_TYPE|STATX_INO,
271
                   &sx2);
272
        if (r < 0)
73,487✔
273
                return r;
274

275
        return statx_inode_same(&sx, &sx2);
73,487✔
276
}
277

278
/* flags can be AT_SYMLINK_FOLLOW or 0 */
279
int path_is_mount_point_full(const char *path, const char *root, int flags) {
27,644✔
280
        _cleanup_close_ int dir_fd = -EBADF;
27,644✔
281
        int r;
27,644✔
282

283
        assert(path);
27,644✔
284
        assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
27,644✔
285

286
        if (empty_or_root(root))
27,644✔
287
                return is_mount_point_at(AT_FDCWD, path, flags);
18,544✔
288

289
        r = chase(path, root,
9,100✔
290
                  FLAGS_SET(flags, AT_SYMLINK_FOLLOW) ? 0 : CHASE_NOFOLLOW,
9,100✔
291
                  /* ret_path= */ NULL, &dir_fd);
292
        if (r < 0)
9,100✔
293
                return r;
294

295
        return is_mount_point_at(dir_fd, /* path= */ NULL, flags);
7,653✔
296
}
297

298
static int path_get_mnt_id_at_internal(int dir_fd, const char *path, bool unique, uint64_t *ret) {
7,081✔
299
        struct statx sx;
7,081✔
300
        int r;
7,081✔
301

302
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
7,081✔
303
        assert(ret);
7,081✔
304

305
        r = xstatx(dir_fd, path,
7,081✔
306
                   AT_SYMLINK_NOFOLLOW |
307
                   AT_NO_AUTOMOUNT |    /* don't trigger automounts, mnt_id is a local concept */
308
                   AT_STATX_DONT_SYNC,  /* don't go to the network, mnt_id is a local concept */
309
                   unique ? STATX_MNT_ID_UNIQUE : STATX_MNT_ID,
310
                   &sx);
311
        if (r < 0)
7,081✔
312
                return r;
7,081✔
313

314
        *ret = sx.stx_mnt_id;
7,080✔
315
        return 0;
7,080✔
316
}
317

318
int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) {
7,081✔
319
        uint64_t mnt_id;
7,081✔
320
        int r;
7,081✔
321

322
        r = path_get_mnt_id_at_internal(dir_fd, path, /* unique = */ false, &mnt_id);
7,081✔
323
        if (r < 0)
7,081✔
324
                return r;
7,081✔
325

326
        assert(mnt_id <= INT_MAX);
7,080✔
327
        *ret = (int) mnt_id;
7,080✔
328
        return 0;
7,080✔
329
}
330

331
int path_get_unique_mnt_id_at(int dir_fd, const char *path, uint64_t *ret) {
×
332
        return path_get_mnt_id_at_internal(dir_fd, path, /* unique = */ true, ret);
×
333
}
334

335
bool fstype_is_network(const char *fstype) {
1,750✔
336
        const char *x;
1,750✔
337

338
        x = startswith(fstype, "fuse.");
1,750✔
339
        if (x)
1,750✔
340
                fstype = x;
×
341

342
        if (nulstr_contains(filesystem_sets[FILESYSTEM_SET_NETWORK].value, fstype))
1,750✔
343
                return true;
1,750✔
344

345
        /* Filesystems not present in the internal database */
346
        return STR_IN_SET(fstype,
1,746✔
347
                          "davfs",
348
                          "glusterfs",
349
                          "lustre",
350
                          "sshfs");
351
}
352

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

379
bool fstype_is_api_vfs(const char *fstype) {
58✔
380
        assert(fstype);
58✔
381

382
        const FilesystemSet *fs;
58✔
383
        FOREACH_ARGUMENT(fs,
263✔
384
                         filesystem_sets + FILESYSTEM_SET_BASIC_API,
385
                         filesystem_sets + FILESYSTEM_SET_AUXILIARY_API,
386
                         filesystem_sets + FILESYSTEM_SET_PRIVILEGED_API,
387
                         filesystem_sets + FILESYSTEM_SET_TEMPORARY)
388
                if (nulstr_contains(fs->value, fstype))
232✔
389
                    return true;
27✔
390

391
        /* Filesystems not present in the internal database */
392
        return STR_IN_SET(fstype,
31✔
393
                          "autofs",
394
                          "cpuset",
395
                          "devtmpfs");
396
}
397

398
bool fstype_is_blockdev_backed(const char *fstype) {
29✔
399
        const char *x;
29✔
400

401
        x = startswith(fstype, "fuse.");
29✔
402
        if (x)
29✔
403
                fstype = x;
×
404

405
        return !streq(fstype, "9p") && !fstype_is_network(fstype) && !fstype_is_api_vfs(fstype);
29✔
406
}
407

408
bool fstype_is_ro(const char *fstype) {
2,723✔
409
        /* All Linux file systems that are necessarily read-only */
410
        return STR_IN_SET(fstype,
2,723✔
411
                          "DM_verity_hash",
412
                          "cramfs",
413
                          "erofs",
414
                          "iso9660",
415
                          "squashfs");
416
}
417

418
bool fstype_can_discard(const char *fstype) {
9✔
419
        assert(fstype);
9✔
420

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

426
        /* On new kernels we can just ask the kernel */
427
        return mount_option_supported(fstype, "discard", NULL) > 0;
3✔
428
}
429

430
const char* fstype_norecovery_option(const char *fstype) {
231✔
431
        int r;
231✔
432

433
        assert(fstype);
231✔
434

435
        /* Use a curated list as first check, to avoid calling fsopen() which might load kmods, which might
436
         * not be allowed in our MAC context. */
437
        if (STR_IN_SET(fstype, "ext3", "ext4", "xfs"))
231✔
438
                return "norecovery";
18✔
439

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

456
        /* On new kernels we can just ask the kernel */
457
        return mount_option_supported(fstype, "norecovery", NULL) > 0 ? "norecovery" : NULL;
213✔
458
}
459

460
bool fstype_can_fmask_dmask(const char *fstype) {
57✔
461
        assert(fstype);
57✔
462

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

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

476
        return STR_IN_SET(fstype,
1✔
477
                          "adfs",
478
                          "exfat",
479
                          "fat",
480
                          "hfs",
481
                          "hpfs",
482
                          "iso9660",
483
                          "msdos",
484
                          "ntfs",
485
                          "vfat");
486
}
487

488
int dev_is_devtmpfs(void) {
316✔
489
        _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
316✔
490
        int mount_id, r;
316✔
491
        char *e;
316✔
492

493
        r = path_get_mnt_id("/dev", &mount_id);
316✔
494
        if (r < 0)
316✔
495
                return r;
496

497
        r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo);
316✔
498
        if (r == -ENOENT)
316✔
499
                return proc_mounted() > 0 ? -ENOENT : -ENOSYS;
×
500
        if (r < 0)
316✔
501
                return r;
502

503
        for (;;) {
14,387✔
504
                _cleanup_free_ char *line = NULL;
14,088✔
505
                int mid;
14,387✔
506

507
                r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line);
14,387✔
508
                if (r < 0)
14,387✔
509
                        return r;
510
                if (r == 0)
14,387✔
511
                        break;
512

513
                if (sscanf(line, "%i", &mid) != 1)
14,088✔
514
                        continue;
×
515

516
                if (mid != mount_id)
14,088✔
517
                        continue;
13,772✔
518

519
                e = strstrafter(line, " - ");
316✔
520
                if (!e)
316✔
521
                        continue;
×
522

523
                /* accept any name that starts with the currently expected type */
524
                if (startswith(e, "devtmpfs"))
316✔
525
                        return true;
526
        }
527

528
        return false;
299✔
529
}
530

531
static int mount_fd(
53,184✔
532
                const char *source,
533
                int target_fd,
534
                const char *filesystemtype,
535
                unsigned long mountflags,
536
                const void *data) {
537

538
        assert(target_fd >= 0);
53,184✔
539

540
        if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) {
53,184✔
541
                if (errno != ENOENT)
1,129✔
542
                        return -errno;
1,129✔
543

544
                /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't
545
                 * mounted. Check for the latter to generate better error messages. */
546
                if (proc_mounted() == 0)
555✔
547
                        return -ENOSYS;
548

549
                return -ENOENT;
555✔
550
        }
551

552
        return 0;
52,055✔
553
}
554

555
int mount_nofollow(
54,663✔
556
                const char *source,
557
                const char *target,
558
                const char *filesystemtype,
559
                unsigned long mountflags,
560
                const void *data) {
561

562
        _cleanup_close_ int fd = -EBADF;
54,663✔
563

564
        assert(target);
54,663✔
565

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

575
        fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
54,663✔
576
        if (fd < 0)
54,663✔
577
                return -errno;
1,479✔
578

579
        return mount_fd(source, fd, filesystemtype, mountflags, data);
53,184✔
580
}
581

582
const char* mount_propagation_flag_to_string(unsigned long flags) {
56✔
583

584
        switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) {
56✔
585
        case 0:
586
                return "";
587
        case MS_SHARED:
1✔
588
                return "shared";
1✔
589
        case MS_SLAVE:
1✔
590
                return "slave";
1✔
591
        case MS_PRIVATE:
3✔
592
                return "private";
3✔
593
        }
594

595
        return NULL;
×
596
}
597

598
int mount_propagation_flag_from_string(const char *name, unsigned long *ret) {
11✔
599

600
        if (isempty(name))
11✔
601
                *ret = 0;
2✔
602
        else if (streq(name, "shared"))
9✔
603
                *ret = MS_SHARED;
2✔
604
        else if (streq(name, "slave"))
7✔
605
                *ret = MS_SLAVE;
2✔
606
        else if (streq(name, "private"))
5✔
607
                *ret = MS_PRIVATE;
3✔
608
        else
609
                return -EINVAL;
610
        return 0;
611
}
612

613
bool mount_propagation_flag_is_valid(unsigned long flag) {
2,266✔
614
        return IN_SET(flag, 0, MS_SHARED, MS_PRIVATE, MS_SLAVE);
2,266✔
615
}
616

617
bool mount_new_api_supported(void) {
4,943✔
618
        static int cache = -1;
4,943✔
619
        int r;
4,943✔
620

621
        if (cache >= 0)
4,943✔
622
                return cache;
1,694✔
623

624
        /* This is the newer API among the ones we use, so use it as boundary */
625
        r = RET_NERRNO(mount_setattr(-EBADF, NULL, 0, NULL, 0));
3,249✔
626
        if (r == 0 || ERRNO_IS_NOT_SUPPORTED(r)) /* This should return an error if it is working properly */
3,249✔
627
                return (cache = false);
×
628

629
        return (cache = true);
3,249✔
630
}
631

632
int mount_option_supported(const char *fstype, const char *key, const char *value) {
923✔
633
        _cleanup_close_ int fd = -EBADF;
923✔
634
        int r;
923✔
635

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

639
        assert(fstype);
923✔
640
        assert(key);
923✔
641

642
        fd = fsopen(fstype, FSOPEN_CLOEXEC);
923✔
643
        if (fd < 0)
923✔
644
                return log_debug_errno(errno, "Failed to open superblock context for '%s': %m", fstype);
×
645

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

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

664
        if (value)
923✔
665
                r = fsconfig(fd, FSCONFIG_SET_STRING, key, value, 0);
30✔
666
        else
667
                r = fsconfig(fd, FSCONFIG_SET_FLAG, key, NULL, 0);
893✔
668
        if (r < 0) {
923✔
669
                if (errno == EINVAL)
269✔
670
                        return false; /* EINVAL means option not supported. */
671

672
                return log_debug_errno(errno, "Failed to set '%s%s%s' on '%s' superblock context: %m",
×
673
                                       key, value ? "=" : "", strempty(value), fstype);
674
        }
675

676
        return true; /* works! */
677
}
678

679
bool path_below_api_vfs(const char *p) {
12,494✔
680
        assert(p);
12,494✔
681

682
        /* API VFS are either directly mounted on any of these three paths, or below it. */
683
        return PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc");
12,494✔
684
}
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