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

systemd / systemd / 21121026098

18 Jan 2026 06:15PM UTC coverage: 72.736% (+0.2%) from 72.561%
21121026098

push

github

YHNdnzj
cryptenroll,cryptsetup,shutdown: only call mlockall if we have CAP_IPC_LOCK

Calling mlockall in an unprivileged process most notably had the effect
of making systemd-cryptenroll OOM while trying to open a normal-sized
argon2 keyslot due to it hitting RLIMIT_MEMLOCK.

9 of 14 new or added lines in 4 files covered. (64.29%)

1479 existing lines in 62 files now uncovered.

310610 of 427035 relevant lines covered (72.74%)

1127441.3 hits per line

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

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

3
#include <fcntl.h>
4
#include <linux/magic.h>
5
#include <sys/statvfs.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "chase.h"
10
#include "dirent-util.h"
11
#include "errno-util.h"
12
#include "fd-util.h"
13
#include "filesystems.h"
14
#include "fs-util.h"
15
#include "hash-funcs.h"
16
#include "log.h"
17
#include "mountpoint-util.h"
18
#include "path-util.h"
19
#include "siphash24.h"
20
#include "stat-util.h"
21
#include "string-util.h"
22
#include "time-util.h"
23

24
static int verify_stat_at(
2,082,673✔
25
                int fd,
26
                const char *path,
27
                bool follow,
28
                int (*verify_func)(const struct stat *st),
29
                bool verify) {
30

31
        struct stat st;
2,082,673✔
32
        int r;
2,082,673✔
33

34
        assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
2,082,673✔
35
        assert(!isempty(path) || !follow);
2,082,673✔
36
        assert(verify_func);
2,082,673✔
37

38
        _cleanup_free_ char *p = NULL;
2,082,673✔
39
        if (fd == XAT_FDROOT) {
2,082,673✔
UNCOV
40
                fd = AT_FDCWD;
×
41

UNCOV
42
                if (isempty(path))
×
43
                        path = "/";
UNCOV
44
                else if (!path_is_absolute(path)) {
×
UNCOV
45
                        p = strjoin("/", path);
×
UNCOV
46
                        if (!p)
×
47
                                return -ENOMEM;
48

49
                        path = p;
50
                }
51
        }
52

53
        if (fstatat(fd, strempty(path), &st,
5,024,576✔
54
                    (isempty(path) ? AT_EMPTY_PATH : 0) | (follow ? 0 : AT_SYMLINK_NOFOLLOW)) < 0)
2,082,673✔
55
                return -errno;
7,271✔
56

57
        r = verify_func(&st);
2,075,402✔
58
        return verify ? r : r >= 0;
2,075,402✔
59
}
60

61
int stat_verify_regular(const struct stat *st) {
2,779,639✔
62
        assert(st);
2,779,639✔
63

64
        /* Checks whether the specified stat() structure refers to a regular file. If not returns an
65
         * appropriate error code. */
66

67
        if (S_ISDIR(st->st_mode))
2,779,639✔
68
                return -EISDIR;
69

70
        if (S_ISLNK(st->st_mode))
2,777,446✔
71
                return -ELOOP;
72

73
        if (!S_ISREG(st->st_mode))
2,777,411✔
74
                return -EBADFD;
17✔
75

76
        return 0;
77
}
78

79
int verify_regular_at(int fd, const char *path, bool follow) {
854,736✔
80
        return verify_stat_at(fd, path, follow, stat_verify_regular, true);
854,736✔
81
}
82

83
int fd_verify_regular(int fd) {
854,457✔
84
        if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
854,457✔
85
                return -EISDIR;
86

87
        return verify_regular_at(fd, /* path= */ NULL, /* follow= */ false);
854,457✔
88
}
89

90
int stat_verify_directory(const struct stat *st) {
1,225,861✔
91
        assert(st);
1,225,861✔
92

93
        if (S_ISLNK(st->st_mode))
1,225,861✔
94
                return -ELOOP;
95

96
        if (!S_ISDIR(st->st_mode))
1,225,851✔
97
                return -ENOTDIR;
4✔
98

99
        return 0;
100
}
101

102
int fd_verify_directory(int fd) {
50✔
103
        if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
50✔
104
                return 0;
105

106
        return verify_stat_at(fd, NULL, false, stat_verify_directory, true);
49✔
107
}
108

109
int is_dir_at(int fd, const char *path, bool follow) {
1,206,207✔
110
        return verify_stat_at(fd, path, follow, stat_verify_directory, false);
1,206,207✔
111
}
112

113
int is_dir(const char *path, bool follow) {
438,801✔
114
        assert(!isempty(path));
438,801✔
115
        return is_dir_at(AT_FDCWD, path, follow);
438,801✔
116
}
117

118
int stat_verify_symlink(const struct stat *st) {
21,504✔
119
        assert(st);
21,504✔
120

121
        if (S_ISDIR(st->st_mode))
21,504✔
122
                return -EISDIR;
123

124
        if (!S_ISLNK(st->st_mode))
21,487✔
125
                return -ENOLINK;
274✔
126

127
        return 0;
128
}
129

130
int fd_verify_symlink(int fd) {
4,677✔
131
        return verify_stat_at(fd, /* path= */ NULL, /* follow= */ false, stat_verify_symlink, /* verify= */ true);
4,677✔
132
}
133

134
int is_symlink(const char *path) {
16,827✔
135
        assert(!isempty(path));
16,827✔
136
        return verify_stat_at(AT_FDCWD, path, false, stat_verify_symlink, false);
16,827✔
137
}
138

139
int stat_verify_linked(const struct stat *st) {
1,865,333✔
140
        assert(st);
1,865,333✔
141

142
        if (st->st_nlink <= 0)
1,865,333✔
143
                return -EIDRM; /* recognizable error. */
2✔
144

145
        return 0;
146
}
147

148
int fd_verify_linked(int fd) {
48✔
149

150
        if (fd == XAT_FDROOT)
48✔
151
                return 0;
152

153
        return verify_stat_at(fd, NULL, false, stat_verify_linked, true);
47✔
154
}
155

156
int stat_verify_device_node(const struct stat *st) {
3,475✔
157
        assert(st);
3,475✔
158

159
        if (S_ISLNK(st->st_mode))
3,475✔
160
                return -ELOOP;
161

162
        if (S_ISDIR(st->st_mode))
3,475✔
163
                return -EISDIR;
164

165
        if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
3,354✔
166
                return -ENOTTY;
5✔
167

168
        return 0;
169
}
170

171
int is_device_node(const char *path) {
130✔
172
        assert(!isempty(path));
130✔
173
        return verify_stat_at(AT_FDCWD, path, false, stat_verify_device_node, false);
130✔
174
}
175

176
int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) {
38,058✔
177
        _cleanup_close_ int fd = -EBADF;
38,058✔
178
        struct dirent *buf;
38,058✔
179
        size_t m;
38,058✔
180

181
        fd = xopenat(dir_fd, path, O_DIRECTORY|O_CLOEXEC);
38,058✔
182
        if (fd < 0)
38,058✔
183
                return fd;
184

185
        /* Allocate space for at least 3 full dirents, since every dir has at least two entries ("."  +
186
         * ".."), and only once we have seen if there's a third we know whether the dir is empty or not. If
187
         * 'ignore_hidden_or_backup' is true we'll allocate a bit more, since we might skip over a bunch of
188
         * entries that we end up ignoring. */
189
        m = (ignore_hidden_or_backup ? 16 : 3) * DIRENT_SIZE_MAX;
2,073✔
190
        buf = alloca(m);
2,073✔
191

192
        for (;;) {
3,350✔
193
                struct dirent *de;
3,350✔
194
                ssize_t n;
3,350✔
195

196
                n = getdents64(fd, buf, m);
3,350✔
197
                if (n < 0)
3,350✔
UNCOV
198
                        return -errno;
×
199
                if (n == 0)
3,350✔
200
                        break;
201

202
                assert((size_t) n <= m);
2,073✔
203
                msan_unpoison(buf, n);
2,073✔
204

205
                FOREACH_DIRENT_IN_BUFFER(de, buf, n)
5,902✔
206
                        if (!(ignore_hidden_or_backup ? hidden_or_backup_file(de->d_name) : dot_or_dot_dot(de->d_name)))
4,625✔
207
                                return 0;
208
        }
209

210
        return 1;
211
}
212

213
bool stat_may_be_dev_null(struct stat *st) {
421,829✔
214
        assert(st);
421,829✔
215

216
        /* We don't want to hardcode the major/minor of /dev/null, hence we do a simpler "is this a character
217
         * device node?" check. */
218

219
        return S_ISCHR(st->st_mode);
421,829✔
220
}
221

222
bool stat_is_empty(struct stat *st) {
405,661✔
223
        assert(st);
405,661✔
224

225
        return S_ISREG(st->st_mode) && st->st_size <= 0;
405,661✔
226
}
227

228
int null_or_empty_path_with_root(const char *fn, const char *root) {
305,975✔
229
        struct stat st;
305,975✔
230
        int r;
305,975✔
231

232
        assert(fn);
305,975✔
233

234
        /* A symlink to /dev/null or an empty file?
235
         * When looking under root_dir, we can't expect /dev/ to be mounted,
236
         * so let's see if the path is a (possibly dangling) symlink to /dev/null. */
237

238
        if (path_equal(path_startswith(fn, root ?: "/"), "dev/null"))
611,950✔
239
                return true;
305,975✔
240

241
        r = chase_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st);
305,805✔
242
        if (r < 0)
305,805✔
243
                return r;
244

245
        return null_or_empty(&st);
305,689✔
246
}
247

248
static int xfstatfs(int fd, struct statfs *ret) {
8,079,774✔
249
        assert(ret);
8,079,774✔
250

251
        if (fd == AT_FDCWD)
8,079,774✔
UNCOV
252
                return RET_NERRNO(statfs(".", ret));
×
253
        if (fd == XAT_FDROOT)
8,079,774✔
254
                return RET_NERRNO(statfs("/", ret));
2✔
255

256
        assert(fd >= 0);
8,079,772✔
257
        return RET_NERRNO(fstatfs(fd, ret));
8,079,772✔
258
}
259

260
int fd_is_read_only_fs(int fd) {
1,808✔
261
        int r;
1,808✔
262

263
        struct statfs st;
1,808✔
264
        r = xfstatfs(fd, &st);
1,808✔
265
        if (r < 0)
1,808✔
266
                return r;
1,808✔
267

268
        if (st.f_flags & ST_RDONLY)
1,808✔
269
                return true;
270

271
        if (is_network_fs(&st))
1,437✔
272
                /* On NFS, fstatfs() might not reflect whether we can actually write to the remote share.
273
                 * Let's try again with access(W_OK) which is more reliable, at least sometimes. */
UNCOV
274
                return access_fd(fd, W_OK) == -EROFS;
×
275

276
        return false;
277
}
278

279
int path_is_read_only_fs(const char *path) {
1,512✔
280
        _cleanup_close_ int fd = -EBADF;
1,512✔
281

282
        assert(path);
1,512✔
283

284
        fd = open(path, O_CLOEXEC | O_PATH);
1,512✔
285
        if (fd < 0)
1,512✔
286
                return -errno;
152✔
287

288
        return fd_is_read_only_fs(fd);
1,360✔
289
}
290

291
int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags) {
13,939✔
292
        struct stat sta, stb;
13,939✔
293
        int r;
13,939✔
294

295
        assert(fda >= 0 || fda == AT_FDCWD);
13,939✔
296
        assert(fdb >= 0 || fdb == AT_FDCWD);
13,939✔
297
        assert((flags & ~(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT)) == 0);
13,939✔
298

299
        /* Refuse an unset filea or fileb early unless AT_EMPTY_PATH is set */
300
        if ((isempty(filea) || isempty(fileb)) && !FLAGS_SET(flags, AT_EMPTY_PATH))
27,545✔
301
                return -EINVAL;
13,939✔
302

303
        /* Shortcut: comparing the same fd with itself means we can return true */
304
        if (fda >= 0 && fda == fdb && isempty(filea) && isempty(fileb) && FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW))
13,941✔
305
                return true;
306

307
        _cleanup_close_ int pin_a = -EBADF, pin_b = -EBADF;
27,877✔
308
        if (!FLAGS_SET(flags, AT_NO_AUTOMOUNT)) {
13,938✔
309
                /* Let's try to use the name_to_handle_at() AT_HANDLE_FID API to identify identical
310
                 * inodes. We have to issue multiple calls on the same file for that (first, to acquire the
311
                 * FID, and then to check if .st_dev is actually the same). Hence let's pin the inode in
312
                 * between via O_PATH, unless we already have an fd for it. */
313

314
                if (!isempty(filea)) {
13,938✔
315
                        pin_a = openat(fda, filea, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
13,606✔
316
                        if (pin_a < 0)
13,606✔
317
                                return -errno;
13,707✔
318

319
                        fda = pin_a;
13,235✔
320
                        filea = NULL;
13,235✔
321
                        flags |= AT_EMPTY_PATH;
13,235✔
322
                }
323

324
                if (!isempty(fileb)) {
13,567✔
325
                        pin_b = openat(fdb, fileb, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
13,233✔
326
                        if (pin_b < 0)
13,233✔
327
                                return -errno;
4✔
328

329
                        fdb = pin_b;
13,229✔
330
                        fileb = NULL;
13,229✔
331
                        flags |= AT_EMPTY_PATH;
13,229✔
332
                }
333

334
                int ntha_flags = at_flags_normalize_follow(flags) & (AT_EMPTY_PATH|AT_SYMLINK_FOLLOW);
13,563✔
335
                _cleanup_free_ struct file_handle *ha = NULL, *hb = NULL;
13,563✔
336
                int mntida = -1, mntidb = -1;
13,563✔
337

338
                r = name_to_handle_at_try_fid(
13,563✔
339
                                fda,
340
                                filea,
341
                                &ha,
342
                                &mntida,
343
                                ntha_flags);
344
                if (r < 0) {
13,563✔
UNCOV
345
                        if (is_name_to_handle_at_fatal_error(r))
×
346
                                return r;
347

UNCOV
348
                        goto fallback;
×
349
                }
350

351
                r = name_to_handle_at_try_fid(
13,563✔
352
                                fdb,
353
                                fileb,
354
                                &hb,
355
                                &mntidb,
356
                                ntha_flags);
357
                if (r < 0) {
13,563✔
UNCOV
358
                        if (is_name_to_handle_at_fatal_error(r))
×
359
                                return r;
360

UNCOV
361
                        goto fallback;
×
362
                }
363

364
                /* Now compare the two file handles */
365
                if (!file_handle_equal(ha, hb))
13,563✔
366
                        return false;
367

368
                /* If the file handles are the same and they come from the same mount ID? Great, then we are
369
                 * good, they are definitely the same */
370
                if (mntida == mntidb)
13,232✔
371
                        return true;
372

373
                /* File handles are the same, they are not on the same mount id. This might either be because
374
                 * they are on two entirely different file systems, that just happen to have the same FIDs
375
                 * (because they originally where created off the same disk images), or it could be because
376
                 * they are located on two distinct bind mounts of the same fs. To check that, let's look at
377
                 * .st_rdev of the inode. We simply reuse the fallback codepath for that, since it checks
378
                 * exactly that (it checks slightly more, but we don't care.) */
379
        }
380

UNCOV
381
fallback:
×
382
        if (fstatat(fda, strempty(filea), &sta, flags) < 0)
462✔
UNCOV
383
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(filea));
×
384

385
        if (fstatat(fdb, strempty(fileb), &stb, flags) < 0)
462✔
UNCOV
386
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(fileb));
×
387

388
        return stat_inode_same(&sta, &stb);
231✔
389
}
390

391
bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
8,061,473✔
392
        assert(s);
8,061,473✔
393
        assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
8,061,473✔
394

395
        return F_TYPE_EQUAL(s->f_type, magic_value);
8,061,473✔
396
}
397

398
int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value) {
8,056,420✔
399
        int r;
8,056,420✔
400

401
        struct statfs s;
8,056,420✔
402
        r = xstatfsat(dir_fd, path, &s);
8,056,420✔
403
        if (r < 0)
8,056,420✔
404
                return r;
8,056,420✔
405

406
        return is_fs_type(&s, magic_value);
8,056,419✔
407
}
408

409
bool is_temporary_fs(const struct statfs *s) {
9,839✔
410
        return fs_in_group(s, FILESYSTEM_SET_TEMPORARY);
9,839✔
411
}
412

413
bool is_network_fs(const struct statfs *s) {
22,493✔
414
        return fs_in_group(s, FILESYSTEM_SET_NETWORK);
22,493✔
415
}
416

417
int fd_is_temporary_fs(int fd) {
155✔
418
        int r;
155✔
419

420
        struct statfs s;
155✔
421
        r = xfstatfs(fd, &s);
155✔
422
        if (r < 0)
155✔
423
                return r;
155✔
424

425
        return is_temporary_fs(&s);
155✔
426
}
427

428
int fd_is_network_fs(int fd) {
20,949✔
429
        int r;
20,949✔
430

431
        struct statfs s;
20,949✔
432
        r = xfstatfs(fd, &s);
20,949✔
433
        if (r < 0)
20,949✔
434
                return r;
20,949✔
435

436
        return is_network_fs(&s);
20,949✔
437
}
438

439
int path_is_temporary_fs(const char *path) {
11✔
440
        struct statfs s;
11✔
441

442
        if (statfs(path, &s) < 0)
11✔
443
                return -errno;
2✔
444

445
        return is_temporary_fs(&s);
9✔
446
}
447

UNCOV
448
int path_is_network_fs(const char *path) {
×
UNCOV
449
        struct statfs s;
×
450

UNCOV
451
        if (statfs(path, &s) < 0)
×
UNCOV
452
                return -errno;
×
453

UNCOV
454
        return is_network_fs(&s);
×
455
}
456

457
int proc_mounted(void) {
27,627✔
458
        int r;
27,627✔
459

460
        /* A quick check of procfs is properly mounted */
461

462
        r = path_is_fs_type("/proc/", PROC_SUPER_MAGIC);
27,627✔
463
        if (r == -ENOENT) /* not mounted at all */
27,627✔
UNCOV
464
                return false;
×
465

466
        return r;
467
}
468

469
bool stat_inode_same(const struct stat *a, const struct stat *b) {
1,173,250✔
470

471
        /* Returns if the specified stat structure references the same (though possibly modified) inode. Does
472
         * a thorough check, comparing inode nr, backing device and if the inode is still of the same type. */
473

474
        return stat_is_set(a) && stat_is_set(b) &&
2,344,327✔
475
                ((a->st_mode ^ b->st_mode) & S_IFMT) == 0 &&  /* same inode type */
1,130,458✔
476
                a->st_dev == b->st_dev &&
2,261,531✔
477
                a->st_ino == b->st_ino;
1,088,281✔
478
}
479

480
bool stat_inode_unmodified(const struct stat *a, const struct stat *b) {
129,052✔
481

482
        /* Returns if the specified stat structures reference the same, unmodified inode. This check tries to
483
         * be reasonably careful when detecting changes: we check both inode and mtime, to cater for file
484
         * systems where mtimes are fixed to 0 (think: ostree/nixos type installations). We also check file
485
         * size, backing device, inode type and if this refers to a device not the major/minor.
486
         *
487
         * Note that we don't care if file attributes such as ownership or access mode change, this here is
488
         * about contents of the file. The purpose here is to detect file contents changes, and nothing
489
         * else. */
490

491
        return stat_inode_same(a, b) &&
129,052✔
492
                a->st_mtim.tv_sec == b->st_mtim.tv_sec &&
79,236✔
493
                a->st_mtim.tv_nsec == b->st_mtim.tv_nsec &&
78,952✔
494
                (!S_ISREG(a->st_mode) || a->st_size == b->st_size) && /* if regular file, compare file size */
207,968✔
495
                (!(S_ISCHR(a->st_mode) || S_ISBLK(a->st_mode)) || a->st_rdev == b->st_rdev); /* if device node, also compare major/minor, because we can */
78,916✔
496
}
497

498
bool statx_inode_same(const struct statx *a, const struct statx *b) {
4,313,461✔
499

500
        /* Same as stat_inode_same() but for struct statx */
501

502
        if (!statx_is_set(a) || !statx_is_set(b))
8,626,922✔
503
                return false;
504

505
        assert(FLAGS_SET(a->stx_mask, STATX_TYPE|STATX_INO));
4,313,461✔
506
        assert(FLAGS_SET(b->stx_mask, STATX_TYPE|STATX_INO));
4,313,461✔
507

508
        return
4,313,461✔
509
                ((a->stx_mode ^ b->stx_mode) & S_IFMT) == 0 &&
8,623,133✔
510
                a->stx_dev_major == b->stx_dev_major &&
4,309,672✔
511
                a->stx_dev_minor == b->stx_dev_minor &&
12,746,612✔
512
                a->stx_ino == b->stx_ino;
4,117,179✔
513
}
514

515
bool statx_mount_same(const struct statx *a, const struct statx *b) {
4,031,545✔
516
        if (!statx_is_set(a) || !statx_is_set(b))
8,063,090✔
517
                return false;
518

519
        assert(FLAGS_SET(a->stx_mask, STATX_MNT_ID));
4,031,545✔
520
        assert(FLAGS_SET(b->stx_mask, STATX_MNT_ID));
4,031,545✔
521

522
        return a->stx_mnt_id == b->stx_mnt_id;
4,031,545✔
523
}
524

525
int xstatfsat(int dir_fd, const char *path, struct statfs *ret) {
8,056,874✔
526
        _cleanup_close_ int fd = -EBADF;
8,056,874✔
527

528
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
8,056,874✔
529
        assert(ret);
8,056,874✔
530

531
        if (!isempty(path)) {
8,056,874✔
532
                fd = xopenat(dir_fd, path, O_PATH|O_CLOEXEC|O_NOCTTY);
28,207✔
533
                if (fd < 0)
28,207✔
534
                        return fd;
535
                dir_fd = fd;
536
        }
537

538
        return RET_NERRNO(xfstatfs(dir_fd, ret));
8,056,874✔
539
}
540

541
usec_t statx_timestamp_load(const struct statx_timestamp *ts) {
5,116✔
542
        return timespec_load(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
5,116✔
543
}
544
nsec_t statx_timestamp_load_nsec(const struct statx_timestamp *ts) {
820✔
545
        return timespec_load_nsec(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
820✔
546
}
547

548
void inode_hash_func(const struct stat *q, struct siphash *state) {
37,729✔
549
        siphash24_compress_typesafe(q->st_dev, state);
37,729✔
550
        siphash24_compress_typesafe(q->st_ino, state);
37,729✔
551
}
37,729✔
552

553
int inode_compare_func(const struct stat *a, const struct stat *b) {
30,931✔
554
        int r;
30,931✔
555

556
        r = CMP(a->st_dev, b->st_dev);
30,931✔
557
        if (r != 0)
26,156✔
558
                return r;
4,848✔
559

560
        return CMP(a->st_ino, b->st_ino);
26,083✔
561
}
562

563
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(inode_hash_ops, struct stat, inode_hash_func, inode_compare_func, free);
861✔
564

565
const char* inode_type_to_string(mode_t m) {
13,532✔
566

567
        /* Returns a short string for the inode type. We use the same name as the underlying macros for each
568
         * inode type. */
569

570
        switch (m & S_IFMT) {
13,532✔
571
        case S_IFREG:
572
                return "reg";
573
        case S_IFDIR:
5,888✔
574
                return "dir";
5,888✔
575
        case S_IFLNK:
1✔
576
                return "lnk";
1✔
577
        case S_IFCHR:
999✔
578
                return "chr";
999✔
579
        case S_IFBLK:
460✔
580
                return "blk";
460✔
581
        case S_IFIFO:
460✔
582
                return "fifo";
460✔
583
        case S_IFSOCK:
576✔
584
                return "sock";
576✔
585
        }
586

587
        /* Note anonymous inodes in the kernel will have a zero type. Hence fstat() of an eventfd() will
588
         * return an .st_mode where we'll return NULL here! */
589
        return NULL;
4✔
590
}
591

592
mode_t inode_type_from_string(const char *s) {
13✔
593
        if (!s)
13✔
594
                return MODE_INVALID;
595

596
        if (streq(s, "reg"))
13✔
597
                return S_IFREG;
598
        if (streq(s, "dir"))
10✔
599
                return S_IFDIR;
600
        if (streq(s, "lnk"))
7✔
601
                return S_IFLNK;
602
        if (streq(s, "chr"))
6✔
603
                return S_IFCHR;
604
        if (streq(s, "blk"))
5✔
605
                return S_IFBLK;
606
        if (streq(s, "fifo"))
4✔
607
                return S_IFIFO;
608
        if (streq(s, "sock"))
2✔
609
                return S_IFSOCK;
2✔
610

611
        return MODE_INVALID;
612
}
613

614
int statx_warn_mount_root(const struct statx *sx, int log_level) {
81,721✔
615
        assert(sx);
81,721✔
616

617
        /* The STATX_ATTR_MOUNT_ROOT flag is supported since kernel v5.8. */
618
        if (!FLAGS_SET(sx->stx_attributes_mask, STATX_ATTR_MOUNT_ROOT))
81,721✔
UNCOV
619
                return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOSYS),
×
620
                                      "statx() did not set STATX_ATTR_MOUNT_ROOT, running on an old kernel?");
621

622
        return 0;
623
}
624

625
int statx_warn_mount_id(const struct statx *sx, int log_level) {
6,900✔
626
        assert(sx);
6,900✔
627

628
        /* The STATX_MNT_ID flag is supported since kernel v5.10. */
629
        if (!FLAGS_SET(sx->stx_mask, STATX_MNT_ID))
6,900✔
UNCOV
630
                return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOSYS),
×
631
                                      "statx() does not support STATX_MNT_ID, running on an old kernel?");
632

633
        return 0;
634
}
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