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

systemd / systemd / 17813902210

17 Sep 2025 11:56PM UTC coverage: 72.24% (-0.04%) from 72.281%
17813902210

push

github

web-flow
sysupdate: use conf_files_list_strv_full() where possible (#38198)

37 of 44 new or added lines in 1 file covered. (84.09%)

6236 existing lines in 79 files now uncovered.

302695 of 419015 relevant lines covered (72.24%)

1046897.1 hits per line

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

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

3
#include <fcntl.h>
4
#include <linux/fs.h>
5
#include <sys/ioctl.h>
6
#include <sys/kcmp.h>
7
#include <sys/resource.h>
8
#include <sys/stat.h>
9
#include <unistd.h>
10

11
#include "alloc-util.h"
12
#include "dirent-util.h"
13
#include "errno-util.h"
14
#include "fd-util.h"
15
#include "fileio.h"
16
#include "format-util.h"
17
#include "fs-util.h"
18
#include "log.h"
19
#include "mountpoint-util.h"
20
#include "parse-util.h"
21
#include "path-util.h"
22
#include "process-util.h"
23
#include "socket-util.h"
24
#include "sort-util.h"
25
#include "stat-util.h"
26
#include "stdio-util.h"
27
#include "string-util.h"
28

29
/* The maximum number of iterations in the loop to close descriptors in the fallback case
30
 * when /proc/self/fd/ is inaccessible. */
31
#define MAX_FD_LOOP_LIMIT (1024*1024)
32

33
int close_nointr(int fd) {
35,626,841✔
34
        assert(fd >= 0);
35,626,841✔
35

36
        if (close(fd) >= 0)
35,626,841✔
37
                return 0;
38

39
        /*
40
         * Just ignore EINTR; a retry loop is the wrong thing to do on
41
         * Linux.
42
         *
43
         * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
44
         * https://bugzilla.gnome.org/show_bug.cgi?id=682819
45
         * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
46
         * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
47
         */
48
        if (errno == EINTR)
10,245✔
49
                return 0;
50

51
        return -errno;
10,245✔
52
}
53

54
int safe_close(int fd) {
93,903,184✔
55
        /*
56
         * Like close_nointr() but cannot fail. Guarantees errno is unchanged. Is a noop for negative fds,
57
         * and returns -EBADF, so that it can be used in this syntax:
58
         *
59
         * fd = safe_close(fd);
60
         */
61

62
        if (fd >= 0) {
93,903,184✔
63
                PROTECT_ERRNO;
×
64

65
                /* The kernel might return pretty much any error code
66
                 * via close(), but the fd will be closed anyway. The
67
                 * only condition we want to check for here is whether
68
                 * the fd was invalid at all... */
69

70
                assert_se(close_nointr(fd) != -EBADF);
35,203,882✔
71
        }
72

73
        return -EBADF;
93,903,184✔
74
}
75

76
void safe_close_pair(int p[static 2]) {
418,158✔
77
        assert(p);
418,158✔
78

79
        if (p[0] == p[1]) {
418,158✔
80
                /* Special case pairs which use the same fd in both
81
                 * directions... */
82
                p[0] = p[1] = safe_close(p[0]);
381,743✔
83
                return;
381,743✔
84
        }
85

86
        p[0] = safe_close(p[0]);
36,415✔
87
        p[1] = safe_close(p[1]);
36,415✔
88
}
89

90
void close_many(const int fds[], size_t n_fds) {
3,256,495✔
91
        assert(fds || n_fds == 0);
3,256,495✔
92

93
        FOREACH_ARRAY(fd, fds, n_fds)
3,308,456✔
94
                safe_close(*fd);
51,961✔
95
}
3,256,495✔
96

97
void close_many_unset(int fds[], size_t n_fds) {
28✔
98
        assert(fds || n_fds == 0);
28✔
99

100
        FOREACH_ARRAY(fd, fds, n_fds)
29✔
101
                *fd = safe_close(*fd);
1✔
102
}
28✔
103

104
void close_many_and_free(int *fds, size_t n_fds) {
656✔
105
        assert(fds || n_fds == 0);
656✔
106

107
        close_many(fds, n_fds);
656✔
108
        free(fds);
656✔
109
}
656✔
110

111
int fclose_nointr(FILE *f) {
1,890,555✔
112
        assert(f);
1,890,555✔
113

114
        /* Same as close_nointr(), but for fclose() */
115

116
        errno = 0; /* Extra safety: if the FILE* object is not encapsulating an fd, it might not set errno
1,890,555✔
117
                    * correctly. Let's hence initialize it to zero first, so that we aren't confused by any
118
                    * prior errno here */
119
        if (fclose(f) == 0)
1,890,555✔
120
                return 0;
121

122
        if (errno == EINTR)
×
123
                return 0;
124

125
        return errno_or_else(EIO);
×
126
}
127

128
FILE* safe_fclose(FILE *f) {
3,638,360✔
129

130
        /* Same as safe_close(), but for fclose() */
131

132
        if (f) {
3,638,360✔
133
                PROTECT_ERRNO;
×
134

135
                assert_se(fclose_nointr(f) != -EBADF);
1,890,555✔
136
        }
137

138
        return NULL;
3,638,360✔
139
}
140

141
DIR* safe_closedir(DIR *d) {
×
142

143
        if (d) {
×
144
                PROTECT_ERRNO;
×
145

146
                assert_se(closedir(d) >= 0 || errno != EBADF);
×
147
        }
148

149
        return NULL;
×
150
}
151

152
int fd_nonblock(int fd, bool nonblock) {
1,944,524✔
153
        int flags, nflags;
1,944,524✔
154

155
        assert(fd >= 0);
1,944,524✔
156

157
        flags = fcntl(fd, F_GETFL, 0);
1,944,524✔
158
        if (flags < 0)
1,944,524✔
159
                return -errno;
×
160

161
        nflags = UPDATE_FLAG(flags, O_NONBLOCK, nonblock);
1,944,524✔
162
        if (nflags == flags)
1,944,524✔
163
                return 0;
164

165
        if (fcntl(fd, F_SETFL, nflags) < 0)
1,922,098✔
166
                return -errno;
×
167

168
        return 1;
169
}
170

171
int stdio_disable_nonblock(void) {
15,010✔
172
        int ret = 0;
15,010✔
173

174
        /* stdin/stdout/stderr really should have O_NONBLOCK, which would confuse apps if left on, as
175
         * write()s might unexpectedly fail with EAGAIN. */
176

177
        RET_GATHER(ret, fd_nonblock(STDIN_FILENO, false));
15,010✔
178
        RET_GATHER(ret, fd_nonblock(STDOUT_FILENO, false));
15,010✔
179
        RET_GATHER(ret, fd_nonblock(STDERR_FILENO, false));
15,010✔
180

181
        return ret;
15,010✔
182
}
183

184
int fd_cloexec(int fd, bool cloexec) {
83,430✔
185
        int flags, nflags;
83,430✔
186

187
        assert(fd >= 0);
83,430✔
188

189
        flags = fcntl(fd, F_GETFD, 0);
83,430✔
190
        if (flags < 0)
83,430✔
191
                return -errno;
×
192

193
        nflags = UPDATE_FLAG(flags, FD_CLOEXEC, cloexec);
83,430✔
194
        if (nflags == flags)
83,430✔
195
                return 0;
196

197
        return RET_NERRNO(fcntl(fd, F_SETFD, nflags));
73,193✔
198
}
199

200
int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec) {
101✔
201
        int r = 0;
101✔
202

203
        assert(fds || n_fds == 0);
101✔
204

205
        FOREACH_ARRAY(fd, fds, n_fds) {
127✔
206
                if (*fd < 0) /* Skip gracefully over already invalidated fds */
26✔
207
                        continue;
×
208

209
                RET_GATHER(r, fd_cloexec(*fd, cloexec));
26✔
210
        }
211

212
        return r;
101✔
213
}
214

215
static bool fd_in_set(int fd, const int fds[], size_t n_fds) {
28,385✔
216
        assert(fd >= 0);
28,385✔
217
        assert(fds || n_fds == 0);
28,385✔
218

219
        FOREACH_ARRAY(i, fds, n_fds) {
24,068,804✔
220
                if (*i < 0)
24,045,174✔
221
                        continue;
×
222

223
                if (*i == fd)
24,045,174✔
224
                        return true;
225
        }
226

227
        return false;
228
}
229

230
int get_max_fd(void) {
7✔
231
        struct rlimit rl;
7✔
232
        rlim_t m;
7✔
233

234
        /* Return the highest possible fd, based RLIMIT_NOFILE, but enforcing FD_SETSIZE-1 as lower boundary
235
         * and INT_MAX as upper boundary. */
236

237
        if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
7✔
238
                return -errno;
×
239

240
        m = MAX(rl.rlim_cur, rl.rlim_max);
7✔
241
        if (m < FD_SETSIZE) /* Let's always cover at least 1024 fds */
7✔
242
                return FD_SETSIZE-1;
243

244
        if (m == RLIM_INFINITY || m > INT_MAX) /* Saturate on overflow. After all fds are "int", hence can
7✔
245
                                                * never be above INT_MAX */
246
                return INT_MAX;
247

248
        return (int) (m - 1);
7✔
249
}
250

251
int close_all_fds_frugal(const int except[], size_t n_except) {
3✔
252
        int max_fd, r = 0;
3✔
253

254
        assert(except || n_except == 0);
3✔
255

256
        /* This is the inner fallback core of close_all_fds(). This never calls malloc() or opendir() or so
257
         * and hence is safe to be called in signal handler context. Most users should call close_all_fds(),
258
         * but when we assume we are called from signal handler context, then use this simpler call
259
         * instead. */
260

261
        max_fd = get_max_fd();
3✔
262
        if (max_fd < 0)
3✔
263
                return max_fd;
3✔
264

265
        /* Refuse to do the loop over more too many elements. It's better to fail immediately than to
266
         * spin the CPU for a long time. */
267
        if (max_fd > MAX_FD_LOOP_LIMIT)
3✔
268
                return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
×
269
                                       "Refusing to loop over %d potential fds.", max_fd);
270

271
        for (int fd = 3; fd >= 0; fd = fd < max_fd ? fd + 1 : -EBADF) {
20,994✔
272
                int q;
20,991✔
273

274
                if (fd_in_set(fd, except, n_except))
20,991✔
275
                        continue;
2,361✔
276

277
                q = close_nointr(fd);
18,630✔
278
                if (q != -EBADF)
18,630✔
279
                        RET_GATHER(r, q);
8,386✔
280
        }
281

282
        return r;
283
}
284

285
int close_all_fds_by_proc(const int except[], size_t n_except) {
2✔
286
        _cleanup_closedir_ DIR *d = NULL;
2✔
287
        int r = 0;
2✔
288

289
        d = opendir("/proc/self/fd");
2✔
290
        if (!d)
2✔
UNCOV
291
                return close_all_fds_frugal(except, n_except); /* ultimate fallback if /proc/ is not available */
×
292

293
        FOREACH_DIRENT(de, d, return -errno) {
7,408✔
294
                int fd = -EBADF, q;
7,402✔
295

296
                if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
7,402✔
UNCOV
297
                        continue;
×
298

299
                fd = parse_fd(de->d_name);
7,402✔
300
                if (fd < 0)
7,402✔
301
                        /* Let's better ignore this, just in case */
UNCOV
302
                        continue;
×
303

304
                if (fd < 3)
7,402✔
305
                        continue;
6✔
306

307
                if (fd == dirfd(d))
7,396✔
308
                        continue;
2✔
309

310
                if (fd_in_set(fd, except, n_except))
7,394✔
311
                        continue;
2,394✔
312

313
                q = close_nointr(fd);
5,000✔
314
                if (q != -EBADF) /* Valgrind has its own FD and doesn't want to have it closed */
5,000✔
315
                        RET_GATHER(r, q);
5,000✔
316
        }
317

318
        return r;
319
}
320

321
static bool have_close_range = true; /* Assume we live in the future */
322

323
static int close_all_fds_special_case(const int except[], size_t n_except) {
44,792✔
324
        assert(n_except == 0 || except);
44,792✔
325

326
        /* Handles a few common special cases separately, since they are common and can be optimized really
327
         * nicely, since we won't need sorting for them. Returns > 0 if the special casing worked, 0
328
         * otherwise. */
329

330
        if (!have_close_range)
44,792✔
331
                return 0;
332

333
        if (n_except == 1 && except[0] < 0) /* Minor optimization: if we only got one fd, and it's invalid,
44,792✔
334
                                             * we got none */
335
                n_except = 0;
336

337
        switch (n_except) {
44,792✔
338

339
        case 0:
8,366✔
340
                /* Close everything. Yay! */
341

342
                if (close_range(3, INT_MAX, 0) >= 0)
8,366✔
343
                        return 1;
344

UNCOV
345
                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
×
UNCOV
346
                        have_close_range = false;
×
347
                        return 0;
×
348
                }
349

UNCOV
350
                return -errno;
×
351

352
        case 1:
14,732✔
353
                /* Close all but exactly one, then we don't need no sorting. This is a pretty common
354
                 * case, hence let's handle it specially. */
355

356
                if ((except[0] <= 3 || close_range(3, except[0]-1, 0) >= 0) &&
14,732✔
357
                    (except[0] >= INT_MAX || close_range(MAX(3, except[0]+1), -1, 0) >= 0))
14,732✔
358
                        return 1;
14,732✔
359

UNCOV
360
                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
×
UNCOV
361
                        have_close_range = false;
×
UNCOV
362
                        return 0;
×
363
                }
364

UNCOV
365
                return -errno;
×
366

367
        default:
368
                return 0;
369
        }
370
}
371

372
int close_all_fds_without_malloc(const int except[], size_t n_except) {
2✔
373
        int r;
2✔
374

375
        assert(n_except == 0 || except);
2✔
376

377
        r = close_all_fds_special_case(except, n_except);
2✔
378
        if (r < 0)
2✔
379
                return r;
380
        if (r > 0) /* special case worked! */
2✔
381
                return 0;
382

383
        return close_all_fds_frugal(except, n_except);
1✔
384
}
385

386
int close_all_fds(const int except[], size_t n_except) {
44,790✔
387
        int r;
44,790✔
388

389
        assert(n_except == 0 || except);
44,790✔
390

391
        r = close_all_fds_special_case(except, n_except);
44,790✔
392
        if (r < 0)
44,790✔
393
                return r;
44,790✔
394
        if (r > 0) /* special case worked! */
44,790✔
395
                return 0;
396

397
        if (!have_close_range)
21,693✔
UNCOV
398
                return close_all_fds_by_proc(except, n_except);
×
399

400
        _cleanup_free_ int *sorted_malloc = NULL;
21,693✔
401
        size_t n_sorted;
21,693✔
402
        int *sorted;
21,693✔
403

404
        /* In the best case we have close_range() to close all fds between a start and an end fd, which we
405
         * can use on the "inverted" exception array, i.e. all intervals between all adjacent pairs from the
406
         * sorted exception array. This changes loop complexity from O(n) where n is number of open fds to
407
         * O(m⋅log(m)) where m is the number of fds to keep open. Given that we assume n ≫ m that's
408
         * preferable to us. */
409

410
        assert(n_except < SIZE_MAX);
21,693✔
411
        n_sorted = n_except + 1;
21,693✔
412

413
        if (n_sorted > ALLOCA_MAX / sizeof(int)) /* Use heap for large numbers of fds, stack otherwise */
21,693✔
UNCOV
414
                sorted = sorted_malloc = new(int, n_sorted);
×
415
        else
416
                sorted = newa(int, n_sorted);
21,693✔
417

418
        if (!sorted) /* Fallback on OOM. */
21,693✔
UNCOV
419
                return close_all_fds_by_proc(except, n_except);
×
420

421
        memcpy(sorted, except, n_except * sizeof(int));
21,693✔
422

423
        /* Let's add fd 2 to the list of fds, to simplify the loop below, as this
424
         * allows us to cover the head of the array the same way as the body */
425
        sorted[n_sorted-1] = 2;
21,693✔
426

427
        typesafe_qsort(sorted, n_sorted, cmp_int);
21,693✔
428

429
        for (size_t i = 0; i < n_sorted-1; i++) {
99,725✔
430
                int start, end;
78,032✔
431

432
                start = MAX(sorted[i], 2); /* The first three fds shall always remain open */
78,032✔
433
                end = MAX(sorted[i+1], 2);
78,032✔
434

435
                assert(end >= start);
78,032✔
436

437
                if (end - start <= 1)
78,032✔
438
                        continue;
31,316✔
439

440
                /* Close everything between the start and end fds (both of which shall stay open) */
441
                if (close_range(start + 1, end - 1, 0) < 0) {
46,716✔
UNCOV
442
                        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
×
443
                                return -errno;
×
444

UNCOV
445
                        have_close_range = false;
×
UNCOV
446
                        return close_all_fds_by_proc(except, n_except);
×
447
                }
448
        }
449

450
        /* The loop succeeded. Let's now close everything beyond the end */
451

452
        if (sorted[n_sorted-1] >= INT_MAX) /* Dont let the addition below overflow */
21,693✔
453
                return 0;
454

455
        if (close_range(sorted[n_sorted-1] + 1, INT_MAX, 0) < 0) {
21,693✔
456
                if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
×
UNCOV
457
                        return -errno;
×
458

UNCOV
459
                have_close_range = false;
×
UNCOV
460
                return close_all_fds_by_proc(except, n_except);
×
461
        }
462

463
        return 0;
464
}
465

466
int pack_fds(int fds[], size_t n_fds) {
9,881✔
467
        if (n_fds <= 0)
9,881✔
468
                return 0;
469

470
        /* Shifts around the fds in the provided array such that they
471
         * all end up packed next to each-other, in order, starting
472
         * from SD_LISTEN_FDS_START. This must be called after close_all_fds();
473
         * it is likely to freeze up otherwise. You should probably use safe_fork_full
474
         * with FORK_CLOSE_ALL_FDS|FORK_PACK_FDS set, to ensure that this is done correctly.
475
         * The fds array is modified in place with the new FD numbers. */
476

477
        assert(fds);
1,680✔
478

479
        for (int start = 0;;) {
480
                int restart_from = -1;
1,680✔
481

482
                for (int i = start; i < (int) n_fds; i++) {
4,827✔
483
                        int nfd;
3,147✔
484

485
                        /* Already at right index? */
486
                        if (fds[i] == i + 3)
3,147✔
487
                                continue;
3✔
488

489
                        nfd = fcntl(fds[i], F_DUPFD, i + 3);
3,144✔
490
                        if (nfd < 0)
3,144✔
UNCOV
491
                                return -errno;
×
492

493
                        safe_close(fds[i]);
3,144✔
494
                        fds[i] = nfd;
3,144✔
495

496
                        /* Hmm, the fd we wanted isn't free? Then
497
                         * let's remember that and try again from here */
498
                        if (nfd != i + 3 && restart_from < 0)
3,144✔
UNCOV
499
                                restart_from = i;
×
500
                }
501

502
                if (restart_from < 0)
1,680✔
503
                        break;
504

505
                start = restart_from;
506
        }
507

508
        assert(fds[0] == 3);
1,680✔
509

510
        return 0;
511
}
512

513
int fd_validate(int fd) {
89,932✔
514
        if (fd < 0)
89,932✔
515
                return -EBADF;
516

517
        if (fcntl(fd, F_GETFD) < 0)
89,930✔
518
                return -errno;
34,157✔
519

520
        return 0;
521
}
522

523
int same_fd(int a, int b) {
9,925✔
524
        struct stat sta, stb;
9,925✔
525
        pid_t pid;
9,925✔
526
        int r, fa, fb;
9,925✔
527

528
        assert(a >= 0);
9,925✔
529
        assert(b >= 0);
9,925✔
530

531
        /* Compares two file descriptors. Note that semantics are quite different depending on whether we
532
         * have F_DUPFD_QUERY/kcmp() or we don't. If we have F_DUPFD_QUERY/kcmp() this will only return true
533
         * for dup()ed file descriptors, but not otherwise. If we don't have F_DUPFD_QUERY/kcmp() this will
534
         * also return true for two fds of the same file, created by separate open() calls. Since we use this
535
         * call mostly for filtering out duplicates in the fd store this difference hopefully doesn't matter
536
         * too much.
537
         *
538
         * Guarantees that if either of the passed fds is not allocated we'll return -EBADF. */
539

540
        if (a == b) {
9,925✔
541
                /* Let's validate that the fd is valid */
542
                r = fd_validate(a);
7✔
543
                if (r < 0)
7✔
544
                        return r;
9,925✔
545

546
                return true;
6✔
547
        }
548

549
        /* Try to use F_DUPFD_QUERY if we have it first, as it is the nicest API */
550
        r = fcntl(a, F_DUPFD_QUERY, b);
9,918✔
551
        if (r > 0)
9,918✔
552
                return true;
553
        if (r == 0) {
9,910✔
554
                /* The kernel will return 0 in case the first fd is allocated, but the 2nd is not. (Which is different in the kcmp() case) Explicitly validate it hence. */
555
                r = fd_validate(b);
9,909✔
556
                if (r < 0)
9,909✔
557
                        return r;
558

559
                return false;
9,908✔
560
        }
561
        /* On old kernels (< 6.10) that do not support F_DUPFD_QUERY this will return EINVAL for regular fds, and EBADF on O_PATH fds. Confusing. */
562
        if (errno == EBADF) {
1✔
563
                /* EBADF could mean two things: the first fd is not valid, or it is valid and is O_PATH and
564
                 * F_DUPFD_QUERY is not supported. Let's validate the fd explicitly, to distinguish this
565
                 * case. */
566
                r = fd_validate(a);
1✔
567
                if (r < 0)
1✔
568
                        return r;
569

570
                /* If the fd is valid, but we got EBADF, then let's try kcmp(). */
571
        } else if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno) && errno != EINVAL)
×
572
                return -errno;
×
573

574
        /* Try to use kcmp() if we have it. */
575
        pid = getpid_cached();
×
576
        r = kcmp(pid, pid, KCMP_FILE, a, b);
×
UNCOV
577
        if (r >= 0)
×
UNCOV
578
                return !r;
×
579
        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
×
580
                return -errno;
×
581

582
        /* We have neither F_DUPFD_QUERY nor kcmp(), use fstat() instead. */
583
        if (fstat(a, &sta) < 0)
×
UNCOV
584
                return -errno;
×
585

UNCOV
586
        if (fstat(b, &stb) < 0)
×
UNCOV
587
                return -errno;
×
588

UNCOV
589
        if (!stat_inode_same(&sta, &stb))
×
590
                return false;
591

592
        /* We consider all device fds different, since two device fds might refer to quite different device
593
         * contexts even though they share the same inode and backing dev_t. */
594

UNCOV
595
        if (S_ISCHR(sta.st_mode) || S_ISBLK(sta.st_mode))
×
596
                return false;
597

598
        /* The fds refer to the same inode on disk, let's also check if they have the same fd flags. This is
599
         * useful to distinguish the read and write side of a pipe created with pipe(). */
600
        fa = fcntl(a, F_GETFL);
×
601
        if (fa < 0)
×
602
                return -errno;
×
603

604
        fb = fcntl(b, F_GETFL);
×
UNCOV
605
        if (fb < 0)
×
UNCOV
606
                return -errno;
×
607

UNCOV
608
        return fa == fb;
×
609
}
610

611
bool fdname_is_valid(const char *s) {
13,968✔
612
        const char *p;
13,968✔
613

614
        /* Validates a name for $LISTEN_FDNAMES. We basically allow
615
         * everything ASCII that's not a control character. Also, as
616
         * special exception the ":" character is not allowed, as we
617
         * use that as field separator in $LISTEN_FDNAMES.
618
         *
619
         * Note that the empty string is explicitly allowed
620
         * here. However, we limit the length of the names to 255
621
         * characters. */
622

623
        if (!s)
13,968✔
624
                return false;
625

626
        for (p = s; *p; p++) {
212,302✔
627
                if (*p < ' ')
198,339✔
628
                        return false;
629
                if (*p >= 127)
198,339✔
630
                        return false;
631
                if (*p == ':')
198,339✔
632
                        return false;
633
        }
634

635
        return p - s <= FDNAME_MAX;
13,963✔
636
}
637

638
int fd_get_path(int fd, char **ret) {
1,891,858✔
639
        int r;
1,891,858✔
640

641
        assert(fd >= 0 || fd == AT_FDCWD);
1,891,858✔
642

643
        if (fd == AT_FDCWD)
1,891,858✔
644
                return safe_getcwd(ret);
5,105✔
645

646
        r = readlink_malloc(FORMAT_PROC_FD_PATH(fd), ret);
1,886,753✔
647
        if (r == -ENOENT)
1,886,753✔
648
                return proc_fd_enoent_errno();
4✔
649
        return r;
650
}
651

652
int move_fd(int from, int to, int cloexec) {
22,219✔
653
        int r;
22,219✔
654

655
        /* Move fd 'from' to 'to', make sure FD_CLOEXEC remains equal if requested, and release the old fd. If
656
         * 'cloexec' is passed as -1, the original FD_CLOEXEC is inherited for the new fd. If it is 0, it is turned
657
         * off, if it is > 0 it is turned on. */
658

659
        if (from < 0)
22,219✔
660
                return -EBADF;
661
        if (to < 0)
22,219✔
662
                return -EBADF;
663

664
        if (from == to) {
22,219✔
665

UNCOV
666
                if (cloexec >= 0) {
×
UNCOV
667
                        r = fd_cloexec(to, cloexec);
×
668
                        if (r < 0)
×
669
                                return r;
670
                }
671

672
                return to;
×
673
        }
674

675
        if (cloexec < 0) {
22,219✔
676
                int fl;
×
677

678
                fl = fcntl(from, F_GETFD, 0);
×
UNCOV
679
                if (fl < 0)
×
UNCOV
680
                        return -errno;
×
681

UNCOV
682
                cloexec = FLAGS_SET(fl, FD_CLOEXEC);
×
683
        }
684

685
        r = dup3(from, to, cloexec ? O_CLOEXEC : 0);
44,438✔
686
        if (r < 0)
22,219✔
UNCOV
687
                return -errno;
×
688

689
        assert(r == to);
22,219✔
690

691
        safe_close(from);
22,219✔
692

693
        return to;
22,219✔
694
}
695

696
int fd_move_above_stdio(int fd) {
725,009✔
697
        int flags, copy;
725,009✔
698
        PROTECT_ERRNO;
725,009✔
699

700
        /* Moves the specified file descriptor if possible out of the range [0…2], i.e. the range of
701
         * stdin/stdout/stderr. If it can't be moved outside of this range the original file descriptor is
702
         * returned. This call is supposed to be used for long-lasting file descriptors we allocate in our code that
703
         * might get loaded into foreign code, and where we want ensure our fds are unlikely used accidentally as
704
         * stdin/stdout/stderr of unrelated code.
705
         *
706
         * Note that this doesn't fix any real bugs, it just makes it less likely that our code will be affected by
707
         * buggy code from others that mindlessly invokes 'fprintf(stderr, …' or similar in places where stderr has
708
         * been closed before.
709
         *
710
         * This function is written in a "best-effort" and "least-impact" style. This means whenever we encounter an
711
         * error we simply return the original file descriptor, and we do not touch errno. */
712

713
        if (fd < 0 || fd > 2)
725,009✔
714
                return fd;
715

716
        flags = fcntl(fd, F_GETFD, 0);
115✔
717
        if (flags < 0)
115✔
718
                return fd;
719

720
        if (flags & FD_CLOEXEC)
115✔
721
                copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
113✔
722
        else
723
                copy = fcntl(fd, F_DUPFD, 3);
2✔
724
        if (copy < 0)
115✔
725
                return fd;
726

727
        assert(copy > 2);
115✔
728

729
        (void) close(fd);
115✔
730
        return copy;
731
}
732

733
int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd) {
15,315✔
734
        int fd[3] = { original_input_fd,             /* Put together an array of fds we work on */
15,315✔
735
                      original_output_fd,
736
                      original_error_fd },
737
            null_fd = -EBADF,                        /* If we open /dev/null, we store the fd to it here */
15,315✔
738
            copy_fd[3] = EBADF_TRIPLET,              /* This contains all fds we duplicate here
15,315✔
739
                                                      * temporarily, and hence need to close at the end. */
740
            r;
741
        bool null_readable, null_writable;
15,315✔
742

743
        /* Sets up stdin, stdout, stderr with the three file descriptors passed in. If any of the descriptors
744
         * is specified as -EBADF it will be connected with /dev/null instead. If any of the file descriptors
745
         * is passed as itself (e.g. stdin as STDIN_FILENO) it is left unmodified, but the O_CLOEXEC bit is
746
         * turned off should it be on.
747
         *
748
         * Note that if any of the passed file descriptors are > 2 they will be closed — both on success and
749
         * on failure! Thus, callers should assume that when this function returns the input fds are
750
         * invalidated.
751
         *
752
         * Note that when this function fails stdin/stdout/stderr might remain half set up!
753
         *
754
         * O_CLOEXEC is turned off for all three file descriptors (which is how it should be for
755
         * stdin/stdout/stderr). */
756

757
        null_readable = original_input_fd < 0;
15,315✔
758
        null_writable = original_output_fd < 0 || original_error_fd < 0;
15,315✔
759

760
        /* First step, open /dev/null once, if we need it */
761
        if (null_readable || null_writable) {
15,315✔
762

763
                /* Let's open this with O_CLOEXEC first, and convert it to non-O_CLOEXEC when we move the fd to the final position. */
764
                null_fd = open("/dev/null", (null_readable && null_writable ? O_RDWR :
25,452✔
765
                                             null_readable ? O_RDONLY : O_WRONLY) | O_CLOEXEC);
12,685✔
766
                if (null_fd < 0) {
12,767✔
UNCOV
767
                        r = -errno;
×
UNCOV
768
                        goto finish;
×
769
                }
770

771
                /* If this fd is in the 0…2 range, let's move it out of it */
772
                if (null_fd < 3) {
12,767✔
773
                        int copy;
13✔
774

775
                        copy = fcntl(null_fd, F_DUPFD_CLOEXEC, 3); /* Duplicate this with O_CLOEXEC set */
13✔
776
                        if (copy < 0) {
13✔
UNCOV
777
                                r = -errno;
×
UNCOV
778
                                goto finish;
×
779
                        }
780

781
                        close_and_replace(null_fd, copy);
13✔
782
                }
783
        }
784

785
        /* Let's assemble fd[] with the fds to install in place of stdin/stdout/stderr */
786
        for (int i = 0; i < 3; i++)
61,260✔
787
                if (fd[i] < 0)
45,945✔
788
                        fd[i] = null_fd;        /* A negative parameter means: connect this one to /dev/null */
12,923✔
789
                else if (fd[i] != i && fd[i] < 3) {
33,022✔
790
                        /* This fd is in the 0…2 territory, but not at its intended place, move it out of there, so that we can work there. */
791
                        copy_fd[i] = fcntl(fd[i], F_DUPFD_CLOEXEC, 3); /* Duplicate this with O_CLOEXEC set */
82✔
792
                        if (copy_fd[i] < 0) {
82✔
UNCOV
793
                                r = -errno;
×
UNCOV
794
                                goto finish;
×
795
                        }
796

797
                        fd[i] = copy_fd[i];
82✔
798
                }
799

800
        /* At this point we now have the fds to use in fd[], and they are all above the stdio range, so that
801
         * we have freedom to move them around. If the fds already were at the right places then the specific
802
         * fds are -EBADF. Let's now move them to the right places. This is the point of no return. */
803
        for (int i = 0; i < 3; i++)
61,260✔
804
                if (fd[i] == i) {
45,945✔
805
                        /* fd is already in place, but let's make sure O_CLOEXEC is off */
806
                        r = fd_cloexec(i, false);
4,935✔
807
                        if (r < 0)
4,935✔
UNCOV
808
                                goto finish;
×
809
                } else {
810
                        assert(fd[i] > 2);
41,010✔
811

812
                        if (dup2(fd[i], i) < 0) { /* Turns off O_CLOEXEC on the new fd. */
41,010✔
UNCOV
813
                                r = -errno;
×
UNCOV
814
                                goto finish;
×
815
                        }
816
                }
817

818
        r = 0;
819

820
finish:
15,315✔
821
        /* Close the original fds, but only if they were outside of the stdio range. Also, properly check for the same
822
         * fd passed in multiple times. */
823
        safe_close_above_stdio(original_input_fd);
15,315✔
824
        if (original_output_fd != original_input_fd)
15,315✔
825
                safe_close_above_stdio(original_output_fd);
15,070✔
826
        if (original_error_fd != original_input_fd && original_error_fd != original_output_fd)
15,315✔
827
                safe_close_above_stdio(original_error_fd);
14,896✔
828

829
        /* Close the copies we moved > 2 */
830
        close_many(copy_fd, 3);
15,315✔
831

832
        /* Close our null fd, if it's > 2 */
833
        safe_close_above_stdio(null_fd);
15,315✔
834

835
        return r;
15,315✔
836
}
837

838
int fd_reopen(int fd, int flags) {
1,337,194✔
839
        assert(fd >= 0 || fd == AT_FDCWD);
1,337,194✔
840
        assert(!FLAGS_SET(flags, O_CREAT));
1,337,194✔
841

842
        /* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to
843
         * turn O_RDWR fds into O_RDONLY fds.
844
         *
845
         * This doesn't work on sockets (since they cannot be open()ed, ever).
846
         *
847
         * This implicitly resets the file read index to 0.
848
         *
849
         * If AT_FDCWD is specified as file descriptor gets an fd to the current cwd.
850
         *
851
         * If the specified file descriptor refers to a symlink via O_PATH, then this function cannot be used
852
         * to follow that symlink. Because we cannot have non-O_PATH fds to symlinks reopening it without
853
         * O_PATH will always result in -ELOOP. Or in other words: if you have an O_PATH fd to a symlink you
854
         * can reopen it only if you pass O_PATH again. */
855

856
        if (FLAGS_SET(flags, O_NOFOLLOW))
1,337,194✔
857
                /* O_NOFOLLOW is not allowed in fd_reopen(), because after all this is primarily implemented
858
                 * via a symlink-based interface in /proc/self/fd. Let's refuse this here early. Note that
859
                 * the kernel would generate ELOOP here too, hence this manual check is mostly redundant –
860
                 * the only reason we add it here is so that the O_DIRECTORY special case (see below) behaves
861
                 * the same way as the non-O_DIRECTORY case. */
862
                return -ELOOP;
1,337,194✔
863

864
        if (FLAGS_SET(flags, O_DIRECTORY) || fd == AT_FDCWD)
1,337,192✔
865
                /* If we shall reopen the fd as directory we can just go via "." and thus bypass the whole
866
                 * magic /proc/ directory, and make ourselves independent of that being mounted. */
867
                return RET_NERRNO(openat(fd, ".", flags | O_DIRECTORY));
222,477✔
868

869
        int new_fd = open(FORMAT_PROC_FD_PATH(fd), flags);
1,114,717✔
870
        if (new_fd < 0) {
1,114,717✔
871
                if (errno != ENOENT)
46,912✔
872
                        return -errno;
46,911✔
873

874
                return proc_fd_enoent_errno();
1✔
875
        }
876

877
        return new_fd;
878
}
879

880
int fd_reopen_propagate_append_and_position(int fd, int flags) {
38✔
881
        /* Invokes fd_reopen(fd, flags), but propagates O_APPEND if set on original fd, and also tries to
882
         * keep current file position.
883
         *
884
         * You should use this if the original fd potentially is O_APPEND, otherwise we get rather
885
         * "unexpected" behavior. Unless you intentionally want to overwrite pre-existing data, and have
886
         * your output overwritten by the next user.
887
         *
888
         * Use case: "systemd-run --pty >> some-log".
889
         *
890
         * The "keep position" part is obviously nonsense for the O_APPEND case, but should reduce surprises
891
         * if someone carefully pre-positioned the passed in original input or non-append output FDs. */
892

893
        assert(fd >= 0);
38✔
894
        assert(!(flags & (O_APPEND|O_DIRECTORY)));
38✔
895

896
        int existing_flags = fcntl(fd, F_GETFL);
38✔
897
        if (existing_flags < 0)
38✔
UNCOV
898
                return -errno;
×
899

900
        int new_fd = fd_reopen(fd, flags | (existing_flags & O_APPEND));
38✔
901
        if (new_fd < 0)
38✔
902
                return new_fd;
903

904
        /* Try to adjust the offset, but ignore errors. */
905
        off_t p = lseek(fd, 0, SEEK_CUR);
27✔
906
        if (p > 0) {
27✔
UNCOV
907
                off_t new_p = lseek(new_fd, p, SEEK_SET);
×
908
                if (new_p < 0)
×
909
                        log_debug_errno(errno,
×
910
                                        "Failed to propagate file position for re-opened fd %d, ignoring: %m",
911
                                        fd);
UNCOV
912
                else if (new_p != p)
×
UNCOV
913
                        log_debug("Failed to propagate file position for re-opened fd %d (%lld != %lld), ignoring.",
×
914
                                  fd, (long long) new_p, (long long) p);
915
        }
916

917
        return new_fd;
918
}
919

920
int fd_reopen_condition(
1,206,710✔
921
                int fd,
922
                int flags,
923
                int mask,
924
                int *ret_new_fd) {
925

926
        int r, new_fd;
1,206,710✔
927

928
        assert(fd >= 0);
1,206,710✔
929
        assert(!FLAGS_SET(flags, O_CREAT));
1,206,710✔
930

931
        /* Invokes fd_reopen(fd, flags), but only if the existing F_GETFL flags don't match the specified
932
         * flags (masked by the specified mask). This is useful for converting O_PATH fds into real fds if
933
         * needed, but only then. */
934

935
        r = fcntl(fd, F_GETFL);
1,206,710✔
936
        if (r < 0)
1,206,710✔
UNCOV
937
                return -errno;
×
938

939
        if ((r & mask) == (flags & mask)) {
1,206,710✔
940
                *ret_new_fd = -EBADF;
1,201,194✔
941
                return fd;
1,201,194✔
942
        }
943

944
        new_fd = fd_reopen(fd, flags);
5,516✔
945
        if (new_fd < 0)
5,516✔
946
                return new_fd;
947

948
        *ret_new_fd = new_fd;
5,516✔
949
        return new_fd;
5,516✔
950
}
951

952
int fd_is_opath(int fd) {
446,952✔
953
        int r;
446,952✔
954

955
        assert(fd >= 0);
446,952✔
956

957
        r = fcntl(fd, F_GETFL);
446,952✔
958
        if (r < 0)
446,952✔
UNCOV
959
                return -errno;
×
960

961
        return FLAGS_SET(r, O_PATH);
446,952✔
962
}
963

964
int fd_verify_safe_flags_full(int fd, int extra_flags) {
484✔
965
        int flags, unexpected_flags;
484✔
966

967
        /* Check if an extrinsic fd is safe to work on (by a privileged service). This ensures that clients
968
         * can't trick a privileged service into giving access to a file the client doesn't already have
969
         * access to (especially via something like O_PATH).
970
         *
971
         * O_NOFOLLOW: For some reason the kernel will return this flag from fcntl(); it doesn't go away
972
         *             immediately after open(). It should have no effect whatsoever to an already-opened FD,
973
         *             and since we refuse O_PATH it should be safe.
974
         *
975
         * RAW_O_LARGEFILE: glibc secretly sets this and neglects to hide it from us if we call fcntl.
976
         *                  See comment in src/basic/include/fcntl.h for more details about this.
977
         *
978
         * If 'extra_flags' is specified as non-zero the included flags are also allowed.
979
         */
980

981
        assert(fd >= 0);
484✔
982

983
        flags = fcntl(fd, F_GETFL);
484✔
984
        if (flags < 0)
484✔
985
                return -errno;
×
986

987
        unexpected_flags = flags & ~(O_ACCMODE_STRICT|O_NOFOLLOW|RAW_O_LARGEFILE|extra_flags);
484✔
988
        if (unexpected_flags != 0)
484✔
UNCOV
989
                return log_debug_errno(SYNTHETIC_ERRNO(EREMOTEIO),
×
990
                                       "Unexpected flags set for extrinsic fd: 0%o",
991
                                       (unsigned) unexpected_flags);
992

993
        return flags & (O_ACCMODE_STRICT | extra_flags); /* return the flags variable, but remove the noise */
484✔
994
}
995

996
unsigned read_nr_open(void) {
30,095✔
997
        _cleanup_free_ char *nr_open = NULL;
30,095✔
998
        int r;
30,095✔
999

1000
        /* Returns the kernel's current fd limit, either by reading it of /proc/sys if that works, or using the
1001
         * hard-coded default compiled-in value of current kernels (1M) if not. This call will never fail. */
1002

1003
        r = read_one_line_file("/proc/sys/fs/nr_open", &nr_open);
30,095✔
1004
        if (r < 0)
30,095✔
1005
                log_debug_errno(r, "Failed to read /proc/sys/fs/nr_open, ignoring: %m");
30,095✔
1006
        else {
1007
                unsigned v;
30,095✔
1008

1009
                r = safe_atou(nr_open, &v);
30,095✔
1010
                if (r < 0)
30,095✔
UNCOV
1011
                        log_debug_errno(r, "Failed to parse /proc/sys/fs/nr_open value '%s', ignoring: %m", nr_open);
×
1012
                else
1013
                        return v;
30,095✔
1014
        }
1015

1016
        /* If we fail, fall back to the hard-coded kernel limit of 1024 * 1024. */
1017
        return NR_OPEN_DEFAULT;
1018
}
1019

1020
int fd_get_diskseq(int fd, uint64_t *ret) {
52,582✔
1021
        uint64_t diskseq;
52,582✔
1022

1023
        assert(fd >= 0);
52,582✔
1024
        assert(ret);
52,582✔
1025

1026
        if (ioctl(fd, BLKGETDISKSEQ, &diskseq) < 0) {
52,582✔
1027
                /* Note that the kernel is weird: non-existing ioctls currently return EINVAL
1028
                 * rather than ENOTTY on loopback block devices. They should fix that in the kernel,
1029
                 * but in the meantime we accept both here. */
UNCOV
1030
                if (!ERRNO_IS_NOT_SUPPORTED(errno) && errno != EINVAL)
×
UNCOV
1031
                        return -errno;
×
1032

1033
                return -EOPNOTSUPP;
1034
        }
1035

1036
        *ret = diskseq;
52,582✔
1037

1038
        return 0;
52,582✔
1039
}
1040

1041
int path_is_root_at(int dir_fd, const char *path) {
3,666,937✔
1042
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
3,666,937✔
1043

1044
        _cleanup_close_ int fd = -EBADF;
3,666,937✔
1045
        if (!isempty(path)) {
3,666,937✔
1046
                fd = openat(dir_fd, path, O_PATH|O_DIRECTORY|O_CLOEXEC);
100,179✔
1047
                if (fd < 0)
100,179✔
1048
                        return errno == ENOTDIR ? false : -errno;
15,389✔
1049

1050
                dir_fd = fd;
1051
        }
1052

1053
        _cleanup_close_ int root_fd = openat(AT_FDCWD, "/", O_PATH|O_DIRECTORY|O_CLOEXEC);
7,318,485✔
1054
        if (root_fd < 0)
3,651,548✔
UNCOV
1055
                return -errno;
×
1056

1057
        /* Even if the root directory has the same inode as our fd, the fd may not point to the root
1058
         * directory "/", and we also need to check that the mount ids are the same. Otherwise, a construct
1059
         * like the following could be used to trick us:
1060
         *
1061
         * $ mkdir /tmp/x
1062
         * $ mount --bind / /tmp/x
1063
         */
1064

1065
        return fds_are_same_mount(dir_fd, root_fd);
3,651,548✔
1066
}
1067

1068
int fds_are_same_mount(int fd1, int fd2) {
3,651,562✔
1069
        struct statx sx1 = {}, sx2 = {}; /* explicitly initialize the struct to make msan silent. */
3,651,562✔
1070
        int r;
3,651,562✔
1071

1072
        assert(fd1 >= 0);
3,651,562✔
1073
        assert(fd2 >= 0);
3,651,562✔
1074

1075
        if (statx(fd1, "", AT_EMPTY_PATH, STATX_TYPE|STATX_INO|STATX_MNT_ID, &sx1) < 0)
3,651,562✔
1076
                return -errno;
×
1077

1078
        if (statx(fd2, "", AT_EMPTY_PATH, STATX_TYPE|STATX_INO|STATX_MNT_ID, &sx2) < 0)
3,651,562✔
UNCOV
1079
                return -errno;
×
1080

1081
        /* First, compare inode. If these are different, the fd does not point to the root directory "/". */
1082
        if (!statx_inode_same(&sx1, &sx2))
3,651,562✔
1083
                return false;
1084

1085
        /* Note, statx() does not provide the mount ID and path_get_mnt_id_at() does not work when an old
1086
         * kernel is used. In that case, let's assume that we do not have such spurious mount points in an
1087
         * early boot stage, and silently skip the following check. */
1088

1089
        if (!FLAGS_SET(sx1.stx_mask, STATX_MNT_ID)) {
3,501,783✔
1090
                int mntid;
×
1091

1092
                r = path_get_mnt_id_at_fallback(fd1, "", &mntid);
×
UNCOV
1093
                if (r < 0)
×
1094
                        return r;
×
1095
                assert(mntid >= 0);
×
1096

UNCOV
1097
                sx1.stx_mnt_id = mntid;
×
UNCOV
1098
                sx1.stx_mask |= STATX_MNT_ID;
×
1099
        }
1100

1101
        if (!FLAGS_SET(sx2.stx_mask, STATX_MNT_ID)) {
3,501,783✔
1102
                int mntid;
×
1103

1104
                r = path_get_mnt_id_at_fallback(fd2, "", &mntid);
×
UNCOV
1105
                if (r < 0)
×
1106
                        return r;
×
1107
                assert(mntid >= 0);
×
1108

UNCOV
1109
                sx2.stx_mnt_id = mntid;
×
UNCOV
1110
                sx2.stx_mask |= STATX_MNT_ID;
×
1111
        }
1112

1113
        return statx_mount_same(&sx1, &sx2);
3,501,783✔
1114
}
1115

1116
char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
3,268,341✔
1117
        assert(buf);
3,268,341✔
1118
        assert(fd >= 0);
3,268,341✔
1119
        assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
3,268,341✔
1120
        return buf;
3,268,341✔
1121
}
1122

1123
const char* accmode_to_string(int flags) {
165✔
1124
        switch (flags & O_ACCMODE_STRICT) {
165✔
1125
        case O_RDONLY:
1126
                return "ro";
1127
        case O_WRONLY:
3✔
1128
                return "wo";
3✔
1129
        case O_RDWR:
159✔
1130
                return "rw";
159✔
UNCOV
1131
        default:
×
UNCOV
1132
                return NULL;
×
1133
        }
1134
}
1135

1136
char* format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd) {
1✔
1137
        assert(buf);
1✔
1138
        assert(fd >= 0);
1✔
1139
        assert(pid >= 0);
1✔
1140
        assert_se(snprintf_ok(buf, PROC_PID_FD_PATH_MAX, "/proc/" PID_FMT "/fd/%i", pid == 0 ? getpid_cached() : pid, fd));
1✔
1141
        return buf;
1✔
1142
}
1143

1144
int proc_fd_enoent_errno(void) {
5✔
1145
        int r;
5✔
1146

1147
        /* When ENOENT is returned during the use of FORMAT_PROC_FD_PATH, it can mean two things:
1148
         * that the fd does not exist or that /proc/ is not mounted.
1149
         * Let's make things debuggable and figure out the most appropriate errno. */
1150

1151
        r = proc_mounted();
5✔
1152
        if (r == 0)
5✔
1153
                return -ENOSYS;  /* /proc/ is not available or not set up properly, we're most likely
1154
                                    in some chroot environment. */
1155
        if (r > 0)
5✔
1156
                return -EBADF;   /* If /proc/ is definitely around then this means the fd is not valid. */
5✔
1157

1158
        return -ENOENT;          /* Otherwise let's propagate the original ENOENT. */
1159
}
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