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

systemd / systemd / 22601885815

02 Mar 2026 11:59PM UTC coverage: 72.342% (-0.2%) from 72.572%
22601885815

push

github

yuwata
network: fix error aggregation in wwan_check_and_set_configuration()

When removing marked routes, the condition `if (ret)` incorrectly
overwrites any previously accumulated error in `ret` with the latest
return value `r`, even if `r >= 0` (success). This means an earlier
real error can be silently cleared by a subsequent successful
route_remove() call.

The parallel address_remove() block just above uses the correct
`if (r < 0)` pattern. Apply the same fix to the route_remove() block.

0 of 1 new or added line in 1 file covered. (0.0%)

1949 existing lines in 60 files now uncovered.

314857 of 435233 relevant lines covered (72.34%)

1137439.04 hits per line

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

84.42
/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) {
278✔
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);
278✔
39

40
        if (ERRNO_IS_NEG_NOT_SUPPORTED(err))
278✔
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(
92,083✔
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;
92,083✔
57

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

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

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

84
                h->handle_bytes = n;
92,083✔
85

86
                if (ret_unique_mnt_id) {
92,083✔
87
                        uint64_t mnt_id;
42,198✔
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);
42,198✔
91
                        if (r >= 0) {
42,198✔
92
                                if (ret_handle)
41,926✔
93
                                        *ret_handle = TAKE_PTR(h);
41,926✔
94

95
                                *ret_unique_mnt_id = mnt_id;
41,926✔
96

97
                                if (ret_mnt_id)
41,926✔
98
                                        *ret_mnt_id = -1;
24,067✔
99

100
                                return 1;
42,198✔
101
                        }
102
                        if (errno == EOVERFLOW)
272✔
103
                                goto grow;
×
104
                        if (errno != EINVAL)
272✔
105
                                return -errno;
272✔
106
                }
107

108
                int mnt_id;
49,885✔
109
                r = name_to_handle_at(fd, path, h, &mnt_id, flags);
49,885✔
110
                if (r >= 0) {
49,885✔
111
                        if (ret_unique_mnt_id) {
49,879✔
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;
×
117
                                r = xstatx(fd, path,
×
118
                                           at_flags_normalize_nofollow(flags & (AT_SYMLINK_FOLLOW|AT_EMPTY_PATH))|AT_STATX_DONT_SYNC,
×
119
                                           STATX_MNT_ID_UNIQUE,
120
                                           &sx);
121
                                if (r >= 0) {
×
122
                                        if (ret_handle)
×
123
                                                *ret_handle = TAKE_PTR(h);
×
124

125
                                        *ret_unique_mnt_id = sx.stx_mnt_id;
×
126

127
                                        if (ret_mnt_id)
×
128
                                                *ret_mnt_id = -1;
×
129

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

135
                                *ret_unique_mnt_id = 0;
×
136
                        }
137

138
                        if (ret_handle)
49,879✔
139
                                *ret_handle = TAKE_PTR(h);
49,879✔
140

141
                        if (ret_mnt_id)
49,879✔
142
                                *ret_mnt_id = mnt_id;
6✔
143

144
                        return 0;
49,879✔
145
                }
146
                if (errno != EOVERFLOW)
6✔
147
                        return -errno;
6✔
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(
42,071✔
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;
42,071✔
174

175
        assert(fd >= 0 || fd == AT_FDCWD);
42,071✔
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);
42,071✔
182
        if (r >= 0 || is_name_to_handle_at_fatal_error(r))
42,071✔
183
                return r;
41,932✔
184

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

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

192
        assert(fd >= 0 || fd == AT_FDCWD);
49,873✔
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);
49,873✔
197
        if (r < 0)
49,873✔
198
                return r;
199
        if (h->handle_bytes < sizeof(uint64_t))
49,873✔
200
                return -EBADMSG;
201

202
        if (ret)
49,873✔
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);
49,872✔
205

206
        return 0;
207
}
208

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

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

220
struct file_handle* file_handle_dup(const struct file_handle *fh) {
8,413✔
221
        _cleanup_free_ struct file_handle *fh_copy = NULL;
16,826✔
222

223
        assert(fh);
8,413✔
224

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

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

233
        return TAKE_PTR(fh_copy);
8,413✔
234
}
235

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

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

242
        if (path_equal(path, "/"))
90,676✔
243
                return true;
90,676✔
244

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

248
        struct statx sx;
90,616✔
249
        r = xstatx_full(dir_fd, path,
90,616✔
250
                        at_flags_normalize_nofollow(flags) |
90,616✔
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)
90,616✔
258
                return r;
259

260
        if (FLAGS_SET(sx.stx_attributes, STATX_ATTR_MOUNT_ROOT))
88,101✔
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;
79,061✔
267
        r = xstatx(AT_FDCWD,
79,061✔
268
                   "/",
269
                   AT_STATX_DONT_SYNC,
270
                   STATX_TYPE|STATX_INO,
271
                   &sx2);
272
        if (r < 0)
79,061✔
273
                return r;
274

275
        return statx_inode_same(&sx, &sx2);
79,061✔
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) {
29,665✔
280
        _cleanup_close_ int dir_fd = -EBADF;
29,665✔
281
        int r;
29,665✔
282

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

286
        if (empty_or_root(root))
29,665✔
287
                return is_mount_point_at(AT_FDCWD, path, flags);
20,600✔
288

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

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

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

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

305
        r = xstatx(dir_fd, path,
9,105✔
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)
9,105✔
312
                return r;
9,105✔
313

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

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

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

326
        assert(mnt_id <= INT_MAX);
9,104✔
327
        *ret = (int) mnt_id;
9,104✔
328
        return 0;
9,104✔
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,754✔
336
        const char *x;
1,754✔
337

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

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

345
        /* Filesystems not present in the internal database */
346
        return STR_IN_SET(fstype,
1,750✔
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) {
54✔
380
        assert(fstype);
54✔
381

382
        const FilesystemSet *fs;
54✔
383
        FOREACH_ARGUMENT(fs,
243✔
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))
216✔
389
                    return true;
27✔
390

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

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

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

404
        return !STR_IN_SET(fstype, "9p", "overlay") && !fstype_is_network(fstype) && !fstype_is_api_vfs(fstype);
29✔
405
}
406

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

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

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

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

429
const char* fstype_norecovery_option(const char *fstype) {
237✔
430
        int r;
237✔
431

432
        assert(fstype);
237✔
433

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

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

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

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

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

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

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

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

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

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

502
        for (;;) {
15,873✔
503
                _cleanup_free_ char *line = NULL;
15,574✔
504
                int mid;
15,873✔
505

506
                r = read_line(proc_self_mountinfo, LONG_LINE_MAX, &line);
15,873✔
507
                if (r < 0)
15,873✔
508
                        return r;
509
                if (r == 0)
15,873✔
510
                        break;
511

512
                if (sscanf(line, "%i", &mid) != 1)
15,574✔
UNCOV
513
                        continue;
×
514

515
                if (mid != mount_id)
15,574✔
516
                        continue;
15,258✔
517

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

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

527
        return false;
299✔
528
}
529

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

537
        assert(target_fd >= 0);
54,161✔
538

539
        if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) {
54,161✔
540
                if (errno != ENOENT)
6✔
541
                        return -errno;
6✔
542

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

UNCOV
548
                return -ENOENT;
×
549
        }
550

551
        return 0;
54,155✔
552
}
553

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

561
        _cleanup_close_ int fd = -EBADF;
55,666✔
562

563
        assert(target);
55,666✔
564

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

574
        fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
55,666✔
575
        if (fd < 0)
55,666✔
576
                return -errno;
1,505✔
577

578
        return mount_fd(source, fd, filesystemtype, mountflags, data);
54,161✔
579
}
580

581
const char* mount_propagation_flag_to_string(unsigned long flags) {
62✔
582

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

UNCOV
594
        return NULL;
×
595
}
596

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

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

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

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

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

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

628
        return (cache = true);
3,243✔
629
}
630

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

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

638
        assert(fstype);
931✔
639
        assert(key);
931✔
640

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

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

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

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

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

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

678
bool path_below_api_vfs(const char *p) {
13,628✔
679
        assert(p);
13,628✔
680

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