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

systemd / systemd / 13610116813

01 Mar 2025 03:22PM UTC coverage: 71.857% (+0.04%) from 71.822%
13610116813

push

github

DaanDeMeyer
Add a few more bypass environment variables

When we're building ParticleOS images, we don't want the package
manager (or mkosi) to run systemd-sysusers, systemd-tmpfiles or
systemctl preset so let's add a few more bypass environment
variables that we can set to have execution of these skipped like
we already have $SYSTEMD_HWDB_UPDATE_BYPASS and $KERNEL_INSTALL_BYPASS.

12 of 16 new or added lines in 7 files covered. (75.0%)

4203 existing lines in 46 files now uncovered.

294791 of 410246 relevant lines covered (71.86%)

717494.72 hits per line

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

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

3
#include <errno.h>
4
#include <fcntl.h>
5
#if WANT_LINUX_FS_H
6
#include <linux/fs.h>
7
#endif
8
#include <linux/kcmp.h>
9
#include <linux/magic.h>
10
#include <sys/ioctl.h>
11
#include <sys/resource.h>
12
#include <sys/stat.h>
13
#include <unistd.h>
14

15
#include "alloc-util.h"
16
#include "dirent-util.h"
17
#include "fd-util.h"
18
#include "fileio.h"
19
#include "fs-util.h"
20
#include "io-util.h"
21
#include "macro.h"
22
#include "missing_fcntl.h"
23
#include "missing_fs.h"
24
#include "missing_syscall.h"
25
#include "mountpoint-util.h"
26
#include "parse-util.h"
27
#include "path-util.h"
28
#include "process-util.h"
29
#include "socket-util.h"
30
#include "sort-util.h"
31
#include "stat-util.h"
32
#include "stdio-util.h"
33
#include "tmpfile-util.h"
34

35
/* The maximum number of iterations in the loop to close descriptors in the fallback case
36
 * when /proc/self/fd/ is inaccessible. */
37
#define MAX_FD_LOOP_LIMIT (1024*1024)
38

39
int close_nointr(int fd) {
51,042,183✔
40
        assert(fd >= 0);
51,042,183✔
41

42
        if (close(fd) >= 0)
51,042,183✔
43
                return 0;
44

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

57
        return -errno;
16,396✔
58
}
59

60
int safe_close(int fd) {
121,727,381✔
61
        /*
62
         * Like close_nointr() but cannot fail. Guarantees errno is unchanged. Is a noop for negative fds,
63
         * and returns -EBADF, so that it can be used in this syntax:
64
         *
65
         * fd = safe_close(fd);
66
         */
67

68
        if (fd >= 0) {
121,727,381✔
69
                PROTECT_ERRNO;
×
70

71
                /* The kernel might return pretty much any error code
72
                 * via close(), but the fd will be closed anyway. The
73
                 * only condition we want to check for here is whether
74
                 * the fd was invalid at all... */
75

76
                assert_se(close_nointr(fd) != -EBADF);
50,614,057✔
77
        }
78

79
        return -EBADF;
121,727,381✔
80
}
81

82
void safe_close_pair(int p[static 2]) {
524,239✔
83
        assert(p);
524,239✔
84

85
        if (p[0] == p[1]) {
524,239✔
86
                /* Special case pairs which use the same fd in both
87
                 * directions... */
88
                p[0] = p[1] = safe_close(p[0]);
493,025✔
89
                return;
493,025✔
90
        }
91

92
        p[0] = safe_close(p[0]);
31,214✔
93
        p[1] = safe_close(p[1]);
31,214✔
94
}
95

96
void close_many(const int fds[], size_t n_fds) {
2,816,614✔
97
        assert(fds || n_fds == 0);
2,816,614✔
98

99
        FOREACH_ARRAY(fd, fds, n_fds)
2,865,047✔
100
                safe_close(*fd);
48,433✔
101
}
2,816,614✔
102

103
void close_many_unset(int fds[], size_t n_fds) {
54✔
104
        assert(fds || n_fds == 0);
54✔
105

106
        FOREACH_ARRAY(fd, fds, n_fds)
55✔
107
                *fd = safe_close(*fd);
1✔
108
}
54✔
109

110
void close_many_and_free(int *fds, size_t n_fds) {
122✔
111
        assert(fds || n_fds == 0);
122✔
112

113
        close_many(fds, n_fds);
122✔
114
        free(fds);
122✔
115
}
122✔
116

117
int fclose_nointr(FILE *f) {
1,739,566✔
118
        assert(f);
1,739,566✔
119

120
        /* Same as close_nointr(), but for fclose() */
121

122
        errno = 0; /* Extra safety: if the FILE* object is not encapsulating an fd, it might not set errno
1,739,566✔
123
                    * correctly. Let's hence initialize it to zero first, so that we aren't confused by any
124
                    * prior errno here */
125
        if (fclose(f) == 0)
1,739,566✔
126
                return 0;
127

128
        if (errno == EINTR)
×
129
                return 0;
130

131
        return errno_or_else(EIO);
×
132
}
133

134
FILE* safe_fclose(FILE *f) {
3,467,224✔
135

136
        /* Same as safe_close(), but for fclose() */
137

138
        if (f) {
3,467,224✔
139
                PROTECT_ERRNO;
×
140

141
                assert_se(fclose_nointr(f) != -EBADF);
1,739,566✔
142
        }
143

144
        return NULL;
3,467,224✔
145
}
146

147
DIR* safe_closedir(DIR *d) {
×
148

149
        if (d) {
×
150
                PROTECT_ERRNO;
×
151

152
                assert_se(closedir(d) >= 0 || errno != EBADF);
×
153
        }
154

155
        return NULL;
×
156
}
157

158
int fd_nonblock(int fd, bool nonblock) {
1,816,107✔
159
        int flags, nflags;
1,816,107✔
160

161
        assert(fd >= 0);
1,816,107✔
162

163
        flags = fcntl(fd, F_GETFL, 0);
1,816,107✔
164
        if (flags < 0)
1,816,107✔
165
                return -errno;
×
166

167
        nflags = UPDATE_FLAG(flags, O_NONBLOCK, nonblock);
1,816,107✔
168
        if (nflags == flags)
1,816,107✔
169
                return 0;
170

171
        if (fcntl(fd, F_SETFL, nflags) < 0)
1,795,596✔
172
                return -errno;
×
173

174
        return 1;
175
}
176

177
int stdio_disable_nonblock(void) {
13,608✔
178
        int ret = 0;
13,608✔
179

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

183
        RET_GATHER(ret, fd_nonblock(STDIN_FILENO, false));
13,608✔
184
        RET_GATHER(ret, fd_nonblock(STDOUT_FILENO, false));
13,608✔
185
        RET_GATHER(ret, fd_nonblock(STDERR_FILENO, false));
13,608✔
186

187
        return ret;
13,608✔
188
}
189

190
int fd_cloexec(int fd, bool cloexec) {
95,444✔
191
        int flags, nflags;
95,444✔
192

193
        assert(fd >= 0);
95,444✔
194

195
        flags = fcntl(fd, F_GETFD, 0);
95,444✔
196
        if (flags < 0)
95,444✔
197
                return -errno;
×
198

199
        nflags = UPDATE_FLAG(flags, FD_CLOEXEC, cloexec);
95,444✔
200
        if (nflags == flags)
95,444✔
201
                return 0;
202

203
        return RET_NERRNO(fcntl(fd, F_SETFD, nflags));
87,231✔
204
}
205

206
int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec) {
71✔
207
        int r = 0;
71✔
208

209
        assert(fds || n_fds == 0);
71✔
210

211
        FOREACH_ARRAY(fd, fds, n_fds) {
93✔
212
                if (*fd < 0) /* Skip gracefully over already invalidated fds */
22✔
213
                        continue;
×
214

215
                RET_GATHER(r, fd_cloexec(*fd, cloexec));
22✔
216
        }
217

218
        return r;
71✔
219
}
220

221
static bool fd_in_set(int fd, const int fds[], size_t n_fds) {
33,622✔
222
        assert(fd >= 0);
33,622✔
223
        assert(fds || n_fds == 0);
33,622✔
224

225
        FOREACH_ARRAY(i, fds, n_fds) {
13,358,497✔
226
                if (*i < 0)
13,327,102✔
227
                        continue;
×
228

229
                if (*i == fd)
13,327,102✔
230
                        return true;
231
        }
232

233
        return false;
234
}
235

236
int get_max_fd(void) {
9✔
237
        struct rlimit rl;
9✔
238
        rlim_t m;
9✔
239

240
        /* Return the highest possible fd, based RLIMIT_NOFILE, but enforcing FD_SETSIZE-1 as lower boundary
241
         * and INT_MAX as upper boundary. */
242

243
        if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
9✔
244
                return -errno;
×
245

246
        m = MAX(rl.rlim_cur, rl.rlim_max);
9✔
247
        if (m < FD_SETSIZE) /* Let's always cover at least 1024 fds */
9✔
248
                return FD_SETSIZE-1;
249

250
        if (m == RLIM_INFINITY || m > INT_MAX) /* Saturate on overflow. After all fds are "int", hence can
9✔
251
                                                * never be above INT_MAX */
252
                return INT_MAX;
253

254
        return (int) (m - 1);
9✔
255
}
256

257
static int close_all_fds_frugal(const int except[], size_t n_except) {
4✔
258
        int max_fd, r = 0;
4✔
259

260
        assert(except || n_except == 0);
4✔
261

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

267
        max_fd = get_max_fd();
4✔
268
        if (max_fd < 0)
4✔
269
                return max_fd;
4✔
270

271
        /* Refuse to do the loop over more too many elements. It's better to fail immediately than to
272
         * spin the CPU for a long time. */
273
        if (max_fd > MAX_FD_LOOP_LIMIT)
4✔
274
                return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
×
275
                                       "Refusing to loop over %d potential fds.", max_fd);
276

277
        for (int fd = 3; fd >= 0; fd = fd < max_fd ? fd + 1 : -EBADF) {
55,980✔
278
                int q;
27,988✔
279

280
                if (fd_in_set(fd, except, n_except))
27,988✔
281
                        continue;
1,593✔
282

283
                q = close_nointr(fd);
26,395✔
284
                if (q != -EBADF)
26,395✔
285
                        RET_GATHER(r, q);
10,000✔
286
        }
287

288
        return r;
289
}
290

291
static bool have_close_range = true; /* Assume we live in the future */
292

293
static int close_all_fds_special_case(const int except[], size_t n_except) {
44,678✔
294
        assert(n_except == 0 || except);
44,678✔
295

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

300
        if (!have_close_range)
44,678✔
301
                return 0;
302

303
        if (n_except == 1 && except[0] < 0) /* Minor optimization: if we only got one fd, and it's invalid,
44,674✔
304
                                             * we got none */
305
                n_except = 0;
306

307
        switch (n_except) {
44,674✔
308

309
        case 0:
17,039✔
310
                /* Close everything. Yay! */
311

312
                if (close_range(3, INT_MAX, 0) >= 0)
17,039✔
313
                        return 1;
314

315
                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
×
316
                        have_close_range = false;
×
317
                        return 0;
×
318
                }
319

320
                return -errno;
×
321

322
        case 1:
2,951✔
323
                /* Close all but exactly one, then we don't need no sorting. This is a pretty common
324
                 * case, hence let's handle it specially. */
325

326
                if ((except[0] <= 3 || close_range(3, except[0]-1, 0) >= 0) &&
2,951✔
327
                    (except[0] >= INT_MAX || close_range(MAX(3, except[0]+1), -1, 0) >= 0))
2,951✔
328
                        return 1;
2,951✔
329

330
                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
×
331
                        have_close_range = false;
×
332
                        return 0;
×
333
                }
334

335
                return -errno;
×
336

337
        default:
338
                return 0;
339
        }
340
}
341

342
int close_all_fds_without_malloc(const int except[], size_t n_except) {
×
343
        int r;
×
344

345
        assert(n_except == 0 || except);
×
346

347
        r = close_all_fds_special_case(except, n_except);
×
348
        if (r < 0)
×
349
                return r;
350
        if (r > 0) /* special case worked! */
×
351
                return 0;
352

353
        return close_all_fds_frugal(except, n_except);
×
354
}
355

356
int close_all_fds(const int except[], size_t n_except) {
44,678✔
357
        _cleanup_closedir_ DIR *d = NULL;
44,678✔
358
        int r = 0;
44,678✔
359

360
        assert(n_except == 0 || except);
44,678✔
361

362
        r = close_all_fds_special_case(except, n_except);
44,678✔
363
        if (r < 0)
44,678✔
364
                return r;
365
        if (r > 0) /* special case worked! */
44,678✔
366
                return 0;
367

368
        if (have_close_range) {
24,688✔
369
                _cleanup_free_ int *sorted_malloc = NULL;
24,684✔
370
                size_t n_sorted;
24,684✔
371
                int *sorted;
24,684✔
372

373
                /* In the best case we have close_range() to close all fds between a start and an end fd,
374
                 * which we can use on the "inverted" exception array, i.e. all intervals between all
375
                 * adjacent pairs from the sorted exception array. This changes loop complexity from O(n)
376
                 * where n is number of open fds to O(mâ‹…log(m)) where m is the number of fds to keep
377
                 * open. Given that we assume n ≫ m that's preferable to us. */
378

379
                assert(n_except < SIZE_MAX);
24,684✔
380
                n_sorted = n_except + 1;
24,684✔
381

382
                if (n_sorted > 64) /* Use heap for large numbers of fds, stack otherwise */
24,684✔
383
                        sorted = sorted_malloc = new(int, n_sorted);
4✔
384
                else
385
                        sorted = newa(int, n_sorted);
24,680✔
386

387
                if (sorted) {
24,684✔
388
                        memcpy(sorted, except, n_except * sizeof(int));
24,684✔
389

390
                        /* Let's add fd 2 to the list of fds, to simplify the loop below, as this
391
                         * allows us to cover the head of the array the same way as the body */
392
                        sorted[n_sorted-1] = 2;
24,684✔
393

394
                        typesafe_qsort(sorted, n_sorted, cmp_int);
24,684✔
395

396
                        for (size_t i = 0; i < n_sorted-1; i++) {
112,447✔
397
                                int start, end;
87,765✔
398

399
                                start = MAX(sorted[i], 2); /* The first three fds shall always remain open */
87,765✔
400
                                end = MAX(sorted[i+1], 2);
87,765✔
401

402
                                assert(end >= start);
87,765✔
403

404
                                if (end - start <= 1)
87,765✔
405
                                        continue;
34,530✔
406

407
                                /* Close everything between the start and end fds (both of which shall stay open) */
408
                                if (close_range(start + 1, end - 1, 0) < 0) {
53,235✔
409
                                        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
2✔
410
                                                return -errno;
×
411

412
                                        have_close_range = false;
2✔
413
                                        break;
2✔
414
                                }
415
                        }
416

417
                        if (have_close_range) {
24,684✔
418
                                /* The loop succeeded. Let's now close everything beyond the end */
419

420
                                if (sorted[n_sorted-1] >= INT_MAX) /* Dont let the addition below overflow */
24,682✔
421
                                        return 0;
422

423
                                if (close_range(sorted[n_sorted-1] + 1, INT_MAX, 0) >= 0)
24,682✔
424
                                        return 0;
425

426
                                if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
×
427
                                        return -errno;
×
428

429
                                have_close_range = false;
×
430
                        }
431
                }
432

433
                /* Fallback on OOM or if close_range() is not supported */
434
        }
435

436
        d = opendir("/proc/self/fd");
6✔
437
        if (!d)
6✔
438
                return close_all_fds_frugal(except, n_except); /* ultimate fallback if /proc/ is not available */
4✔
439

440
        FOREACH_DIRENT(de, d, return -errno) {
5,648✔
441
                int fd = -EBADF, q;
5,642✔
442

443
                if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
5,642✔
444
                        continue;
×
445

446
                fd = parse_fd(de->d_name);
5,642✔
447
                if (fd < 0)
5,642✔
448
                        /* Let's better ignore this, just in case */
449
                        continue;
×
450

451
                if (fd < 3)
5,642✔
452
                        continue;
6✔
453

454
                if (fd == dirfd(d))
5,636✔
455
                        continue;
2✔
456

457
                if (fd_in_set(fd, except, n_except))
5,634✔
458
                        continue;
634✔
459

460
                q = close_nointr(fd);
5,000✔
461
                if (q < 0 && q != -EBADF && r >= 0) /* Valgrind has its own FD and doesn't want to have it closed */
5,000✔
462
                        r = q;
×
463
        }
464

465
        return r;
466
}
467

468
int pack_fds(int fds[], size_t n_fds) {
11,144✔
469
        if (n_fds <= 0)
11,144✔
470
                return 0;
471

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

479
        assert(fds);
1,583✔
480

481
        for (int start = 0;;) {
482
                int restart_from = -1;
1,583✔
483

484
                for (int i = start; i < (int) n_fds; i++) {
4,169✔
485
                        int nfd;
2,586✔
486

487
                        /* Already at right index? */
488
                        if (fds[i] == i + 3)
2,586✔
489
                                continue;
×
490

491
                        nfd = fcntl(fds[i], F_DUPFD, i + 3);
2,586✔
492
                        if (nfd < 0)
2,586✔
493
                                return -errno;
×
494

495
                        safe_close(fds[i]);
2,586✔
496
                        fds[i] = nfd;
2,586✔
497

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

504
                if (restart_from < 0)
1,583✔
505
                        break;
506

507
                start = restart_from;
508
        }
509

510
        assert(fds[0] == 3);
1,583✔
511

512
        return 0;
513
}
514

515
int fd_validate(int fd) {
113,924✔
516
        if (fd < 0)
113,924✔
517
                return -EBADF;
518

519
        if (fcntl(fd, F_GETFD) < 0)
113,922✔
520
                return -errno;
44,601✔
521

522
        return 0;
523
}
524

525
int same_fd(int a, int b) {
21,265✔
526
        struct stat sta, stb;
21,265✔
527
        pid_t pid;
21,265✔
528
        int r, fa, fb;
21,265✔
529

530
        assert(a >= 0);
21,265✔
531
        assert(b >= 0);
21,265✔
532

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

542
        if (a == b) {
21,265✔
543
                /* Let's validate that the fd is valid */
544
                r = fd_validate(a);
7✔
545
                if (r < 0)
7✔
546
                        return r;
21,265✔
547

548
                return true;
6✔
549
        }
550

551
        /* Try to use F_DUPFD_QUERY if we have it first, as it is the nicest API */
552
        r = fcntl(a, F_DUPFD_QUERY, b);
21,258✔
553
        if (r > 0)
21,258✔
554
                return true;
555
        if (r == 0) {
21,258✔
556
                /* 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. */
557
                r = fd_validate(b);
13,897✔
558
                if (r < 0)
13,897✔
559
                        return r;
560

561
                return false;
13,897✔
562
        }
563
        /* 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. */
564
        if (errno == EBADF) {
7,361✔
565
                /* EBADF could mean two things: the first fd is not valid, or it is valid and is O_PATH and
566
                 * F_DUPFD_QUERY is not supported. Let's validate the fd explicitly, to distinguish this
567
                 * case. */
568
                r = fd_validate(a);
5✔
569
                if (r < 0)
5✔
570
                        return r;
571

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

576
        /* Try to use kcmp() if we have it. */
577
        pid = getpid_cached();
7,360✔
578
        r = kcmp(pid, pid, KCMP_FILE, a, b);
7,360✔
579
        if (r >= 0)
7,360✔
580
                return !r;
7,359✔
581
        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
1✔
582
                return -errno;
1✔
583

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

588
        if (fstat(b, &stb) < 0)
×
589
                return -errno;
×
590

591
        if (!stat_inode_same(&sta, &stb))
×
592
                return false;
593

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

597
        if (S_ISCHR(sta.st_mode) || S_ISBLK(sta.st_mode))
×
598
                return false;
599

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

606
        fb = fcntl(b, F_GETFL);
×
607
        if (fb < 0)
×
608
                return -errno;
×
609

610
        return fa == fb;
×
611
}
612

613
void cmsg_close_all(struct msghdr *mh) {
141,950✔
614
        assert(mh);
141,950✔
615

616
        struct cmsghdr *cmsg;
141,950✔
617
        CMSG_FOREACH(cmsg, mh) {
530,538✔
618
                if (cmsg->cmsg_level != SOL_SOCKET)
123,319✔
619
                        continue;
×
620

621
                if (cmsg->cmsg_type == SCM_RIGHTS)
123,319✔
622
                        close_many(CMSG_TYPED_DATA(cmsg, int),
×
623
                                   (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
×
624
                else if (cmsg->cmsg_type == SCM_PIDFD) {
123,319✔
625
                        assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
×
626
                        safe_close(*CMSG_TYPED_DATA(cmsg, int));
×
627
                }
628
        }
629
}
141,950✔
630

631
bool fdname_is_valid(const char *s) {
16,180✔
632
        const char *p;
16,180✔
633

634
        /* Validates a name for $LISTEN_FDNAMES. We basically allow
635
         * everything ASCII that's not a control character. Also, as
636
         * special exception the ":" character is not allowed, as we
637
         * use that as field separator in $LISTEN_FDNAMES.
638
         *
639
         * Note that the empty string is explicitly allowed
640
         * here. However, we limit the length of the names to 255
641
         * characters. */
642

643
        if (!s)
16,180✔
644
                return false;
645

646
        for (p = s; *p; p++) {
262,429✔
647
                if (*p < ' ')
246,254✔
648
                        return false;
649
                if (*p >= 127)
246,254✔
650
                        return false;
651
                if (*p == ':')
246,254✔
652
                        return false;
653
        }
654

655
        return p - s <= FDNAME_MAX;
16,175✔
656
}
657

658
int fd_get_path(int fd, char **ret) {
1,782,287✔
659
        int r;
1,782,287✔
660

661
        assert(fd >= 0 || fd == AT_FDCWD);
1,782,287✔
662

663
        if (fd == AT_FDCWD)
1,782,287✔
664
                return safe_getcwd(ret);
5,107✔
665

666
        r = readlink_malloc(FORMAT_PROC_FD_PATH(fd), ret);
1,777,180✔
667
        if (r == -ENOENT)
1,777,180✔
668
                return proc_fd_enoent_errno();
4✔
669
        return r;
670
}
671

672
int move_fd(int from, int to, int cloexec) {
25,706✔
673
        int r;
25,706✔
674

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

679
        if (from < 0)
25,706✔
680
                return -EBADF;
681
        if (to < 0)
25,706✔
682
                return -EBADF;
683

684
        if (from == to) {
25,706✔
685

686
                if (cloexec >= 0) {
×
687
                        r = fd_cloexec(to, cloexec);
×
688
                        if (r < 0)
×
689
                                return r;
690
                }
691

692
                return to;
×
693
        }
694

695
        if (cloexec < 0) {
25,706✔
696
                int fl;
×
697

698
                fl = fcntl(from, F_GETFD, 0);
×
699
                if (fl < 0)
×
700
                        return -errno;
×
701

702
                cloexec = FLAGS_SET(fl, FD_CLOEXEC);
×
703
        }
704

705
        r = dup3(from, to, cloexec ? O_CLOEXEC : 0);
51,412✔
706
        if (r < 0)
25,706✔
707
                return -errno;
×
708

709
        assert(r == to);
25,706✔
710

711
        safe_close(from);
25,706✔
712

713
        return to;
25,706✔
714
}
715

716
int fd_move_above_stdio(int fd) {
660,607✔
717
        int flags, copy;
660,607✔
718
        PROTECT_ERRNO;
660,607✔
719

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

733
        if (fd < 0 || fd > 2)
660,607✔
734
                return fd;
735

736
        flags = fcntl(fd, F_GETFD, 0);
103✔
737
        if (flags < 0)
103✔
738
                return fd;
739

740
        if (flags & FD_CLOEXEC)
103✔
741
                copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
101✔
742
        else
743
                copy = fcntl(fd, F_DUPFD, 3);
2✔
744
        if (copy < 0)
103✔
745
                return fd;
746

747
        assert(copy > 2);
103✔
748

749
        (void) close(fd);
103✔
750
        return copy;
751
}
752

753
int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd) {
13,982✔
754
        int fd[3] = { original_input_fd,             /* Put together an array of fds we work on */
13,982✔
755
                      original_output_fd,
756
                      original_error_fd },
757
            null_fd = -EBADF,                        /* If we open /dev/null, we store the fd to it here */
13,982✔
758
            copy_fd[3] = EBADF_TRIPLET,              /* This contains all fds we duplicate here
13,982✔
759
                                                      * temporarily, and hence need to close at the end. */
760
            r;
761
        bool null_readable, null_writable;
13,982✔
762

763
        /* Sets up stdin, stdout, stderr with the three file descriptors passed in. If any of the descriptors
764
         * is specified as -EBADF it will be connected with /dev/null instead. If any of the file descriptors
765
         * is passed as itself (e.g. stdin as STDIN_FILENO) it is left unmodified, but the O_CLOEXEC bit is
766
         * turned off should it be on.
767
         *
768
         * Note that if any of the passed file descriptors are > 2 they will be closed — both on success and
769
         * on failure! Thus, callers should assume that when this function returns the input fds are
770
         * invalidated.
771
         *
772
         * Note that when this function fails stdin/stdout/stderr might remain half set up!
773
         *
774
         * O_CLOEXEC is turned off for all three file descriptors (which is how it should be for
775
         * stdin/stdout/stderr). */
776

777
        null_readable = original_input_fd < 0;
13,982✔
778
        null_writable = original_output_fd < 0 || original_error_fd < 0;
13,982✔
779

780
        /* First step, open /dev/null once, if we need it */
781
        if (null_readable || null_writable) {
13,982✔
782

783
                /* Let's open this with O_CLOEXEC first, and convert it to non-O_CLOEXEC when we move the fd to the final position. */
784
                null_fd = open("/dev/null", (null_readable && null_writable ? O_RDWR :
23,197✔
785
                                             null_readable ? O_RDONLY : O_WRONLY) | O_CLOEXEC);
11,530✔
786
                if (null_fd < 0) {
11,667✔
787
                        r = -errno;
×
788
                        goto finish;
×
789
                }
790

791
                /* If this fd is in the 0…2 range, let's move it out of it */
792
                if (null_fd < 3) {
11,667✔
793
                        int copy;
75✔
794

795
                        copy = fcntl(null_fd, F_DUPFD_CLOEXEC, 3); /* Duplicate this with O_CLOEXEC set */
75✔
796
                        if (copy < 0) {
75✔
797
                                r = -errno;
×
798
                                goto finish;
×
799
                        }
800

801
                        close_and_replace(null_fd, copy);
75✔
802
                }
803
        }
804

805
        /* Let's assemble fd[] with the fds to install in place of stdin/stdout/stderr */
806
        for (int i = 0; i < 3; i++)
55,928✔
807
                if (fd[i] < 0)
41,946✔
808
                        fd[i] = null_fd;        /* A negative parameter means: connect this one to /dev/null */
11,937✔
809
                else if (fd[i] != i && fd[i] < 3) {
30,009✔
810
                        /* 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. */
811
                        copy_fd[i] = fcntl(fd[i], F_DUPFD_CLOEXEC, 3); /* Duplicate this with O_CLOEXEC set */
77✔
812
                        if (copy_fd[i] < 0) {
77✔
813
                                r = -errno;
×
814
                                goto finish;
×
815
                        }
816

817
                        fd[i] = copy_fd[i];
77✔
818
                }
819

820
        /* At this point we now have the fds to use in fd[], and they are all above the stdio range, so that
821
         * we have freedom to move them around. If the fds already were at the right places then the specific
822
         * fds are -EBADF. Let's now move them to the right places. This is the point of no return. */
823
        for (int i = 0; i < 3; i++)
55,928✔
824
                if (fd[i] == i) {
41,946✔
825
                        /* fd is already in place, but let's make sure O_CLOEXEC is off */
826
                        r = fd_cloexec(i, false);
4,425✔
827
                        if (r < 0)
4,425✔
828
                                goto finish;
×
829
                } else {
830
                        assert(fd[i] > 2);
37,521✔
831

832
                        if (dup2(fd[i], i) < 0) { /* Turns off O_CLOEXEC on the new fd. */
37,521✔
833
                                r = -errno;
×
834
                                goto finish;
×
835
                        }
836
                }
837

838
        r = 0;
839

840
finish:
13,982✔
841
        /* Close the original fds, but only if they were outside of the stdio range. Also, properly check for the same
842
         * fd passed in multiple times. */
843
        safe_close_above_stdio(original_input_fd);
13,982✔
844
        if (original_output_fd != original_input_fd)
13,982✔
845
                safe_close_above_stdio(original_output_fd);
13,679✔
846
        if (original_error_fd != original_input_fd && original_error_fd != original_output_fd)
13,982✔
847
                safe_close_above_stdio(original_error_fd);
13,500✔
848

849
        /* Close the copies we moved > 2 */
850
        close_many(copy_fd, 3);
13,982✔
851

852
        /* Close our null fd, if it's > 2 */
853
        safe_close_above_stdio(null_fd);
13,982✔
854

855
        return r;
13,982✔
856
}
857

858
int fd_reopen(int fd, int flags) {
10,684,945✔
859
        assert(fd >= 0 || fd == AT_FDCWD);
10,684,945✔
860
        assert(!FLAGS_SET(flags, O_CREAT));
10,684,945✔
861

862
        /* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to
863
         * turn O_RDWR fds into O_RDONLY fds.
864
         *
865
         * This doesn't work on sockets (since they cannot be open()ed, ever).
866
         *
867
         * This implicitly resets the file read index to 0.
868
         *
869
         * If AT_FDCWD is specified as file descriptor gets an fd to the current cwd.
870
         *
871
         * If the specified file descriptor refers to a symlink via O_PATH, then this function cannot be used
872
         * to follow that symlink. Because we cannot have non-O_PATH fds to symlinks reopening it without
873
         * O_PATH will always result in -ELOOP. Or in other words: if you have an O_PATH fd to a symlink you
874
         * can reopen it only if you pass O_PATH again. */
875

876
        if (FLAGS_SET(flags, O_NOFOLLOW))
10,684,945✔
877
                /* O_NOFOLLOW is not allowed in fd_reopen(), because after all this is primarily implemented
878
                 * via a symlink-based interface in /proc/self/fd. Let's refuse this here early. Note that
879
                 * the kernel would generate ELOOP here too, hence this manual check is mostly redundant –
880
                 * the only reason we add it here is so that the O_DIRECTORY special case (see below) behaves
881
                 * the same way as the non-O_DIRECTORY case. */
882
                return -ELOOP;
10,684,945✔
883

884
        if (FLAGS_SET(flags, O_DIRECTORY) || fd == AT_FDCWD)
10,684,939✔
885
                /* If we shall reopen the fd as directory we can just go via "." and thus bypass the whole
886
                 * magic /proc/ directory, and make ourselves independent of that being mounted. */
887
                return RET_NERRNO(openat(fd, ".", flags | O_DIRECTORY));
234,207✔
888

889
        int new_fd = open(FORMAT_PROC_FD_PATH(fd), flags);
10,450,738✔
890
        if (new_fd < 0) {
10,450,738✔
891
                if (errno != ENOENT)
95,451✔
892
                        return -errno;
95,448✔
893

894
                return proc_fd_enoent_errno();
3✔
895
        }
896

897
        return new_fd;
898
}
899

900
int fd_reopen_propagate_append_and_position(int fd, int flags) {
28✔
901
        /* Invokes fd_reopen(fd, flags), but propagates O_APPEND if set on original fd, and also tries to
902
         * keep current file position.
903
         *
904
         * You should use this if the original fd potentially is O_APPEND, otherwise we get rather
905
         * "unexpected" behavior. Unless you intentionally want to overwrite pre-existing data, and have
906
         * your output overwritten by the next user.
907
         *
908
         * Use case: "systemd-run --pty >> some-log".
909
         *
910
         * The "keep position" part is obviously nonsense for the O_APPEND case, but should reduce surprises
911
         * if someone carefully pre-positioned the passed in original input or non-append output FDs. */
912

913
        assert(fd >= 0);
28✔
914
        assert(!(flags & (O_APPEND|O_DIRECTORY)));
28✔
915

916
        int existing_flags = fcntl(fd, F_GETFL);
28✔
917
        if (existing_flags < 0)
28✔
918
                return -errno;
×
919

920
        int new_fd = fd_reopen(fd, flags | (existing_flags & O_APPEND));
28✔
921
        if (new_fd < 0)
28✔
922
                return new_fd;
923

924
        /* Try to adjust the offset, but ignore errors. */
925
        off_t p = lseek(fd, 0, SEEK_CUR);
19✔
926
        if (p > 0) {
19✔
927
                off_t new_p = lseek(new_fd, p, SEEK_SET);
×
928
                if (new_p < 0)
×
929
                        log_debug_errno(errno,
×
930
                                        "Failed to propagate file position for re-opened fd %d, ignoring: %m",
931
                                        fd);
932
                else if (new_p != p)
×
933
                        log_debug("Failed to propagate file position for re-opened fd %d (%lld != %lld), ignoring.",
×
934
                                  fd, (long long) new_p, (long long) p);
935
        }
936

937
        return new_fd;
938
}
939

940
int fd_reopen_condition(
1,197,381✔
941
                int fd,
942
                int flags,
943
                int mask,
944
                int *ret_new_fd) {
945

946
        int r, new_fd;
1,197,381✔
947

948
        assert(fd >= 0);
1,197,381✔
949
        assert(!FLAGS_SET(flags, O_CREAT));
1,197,381✔
950

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

955
        r = fcntl(fd, F_GETFL);
1,197,381✔
956
        if (r < 0)
1,197,381✔
957
                return -errno;
×
958

959
        if ((r & mask) == (flags & mask)) {
1,197,381✔
960
                *ret_new_fd = -EBADF;
1,193,383✔
961
                return fd;
1,193,383✔
962
        }
963

964
        new_fd = fd_reopen(fd, flags);
3,998✔
965
        if (new_fd < 0)
3,998✔
966
                return new_fd;
967

968
        *ret_new_fd = new_fd;
3,998✔
969
        return new_fd;
3,998✔
970
}
971

972
int fd_is_opath(int fd) {
444,163✔
973
        int r;
444,163✔
974

975
        assert(fd >= 0);
444,163✔
976

977
        r = fcntl(fd, F_GETFL);
444,163✔
978
        if (r < 0)
444,163✔
979
                return -errno;
×
980

981
        return FLAGS_SET(r, O_PATH);
444,163✔
982
}
983

984
int fd_verify_safe_flags_full(int fd, int extra_flags) {
461✔
985
        int flags, unexpected_flags;
461✔
986

987
        /* Check if an extrinsic fd is safe to work on (by a privileged service). This ensures that clients
988
         * can't trick a privileged service into giving access to a file the client doesn't already have
989
         * access to (especially via something like O_PATH).
990
         *
991
         * O_NOFOLLOW: For some reason the kernel will return this flag from fcntl(); it doesn't go away
992
         *             immediately after open(). It should have no effect whatsoever to an already-opened FD,
993
         *             and since we refuse O_PATH it should be safe.
994
         *
995
         * RAW_O_LARGEFILE: glibc secretly sets this and neglects to hide it from us if we call fcntl.
996
         *                  See comment in missing_fcntl.h for more details about this.
997
         *
998
         * If 'extra_flags' is specified as non-zero the included flags are also allowed.
999
         */
1000

1001
        assert(fd >= 0);
461✔
1002

1003
        flags = fcntl(fd, F_GETFL);
461✔
1004
        if (flags < 0)
461✔
1005
                return -errno;
×
1006

1007
        unexpected_flags = flags & ~(O_ACCMODE|O_NOFOLLOW|RAW_O_LARGEFILE|extra_flags);
461✔
1008
        if (unexpected_flags != 0)
461✔
1009
                return log_debug_errno(SYNTHETIC_ERRNO(EREMOTEIO),
×
1010
                                       "Unexpected flags set for extrinsic fd: 0%o",
1011
                                       (unsigned) unexpected_flags);
1012

1013
        return flags & (O_ACCMODE | extra_flags); /* return the flags variable, but remove the noise */
461✔
1014
}
1015

1016
int read_nr_open(void) {
27,755✔
1017
        _cleanup_free_ char *nr_open = NULL;
27,755✔
1018
        int r;
27,755✔
1019

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

1023
        r = read_one_line_file("/proc/sys/fs/nr_open", &nr_open);
27,755✔
1024
        if (r < 0)
27,755✔
1025
                log_debug_errno(r, "Failed to read /proc/sys/fs/nr_open, ignoring: %m");
27,755✔
1026
        else {
1027
                int v;
27,752✔
1028

1029
                r = safe_atoi(nr_open, &v);
27,752✔
1030
                if (r < 0)
27,752✔
1031
                        log_debug_errno(r, "Failed to parse /proc/sys/fs/nr_open value '%s', ignoring: %m", nr_open);
×
1032
                else
1033
                        return v;
27,752✔
1034
        }
1035

1036
        /* If we fail, fall back to the hard-coded kernel limit of 1024 * 1024. */
1037
        return 1024 * 1024;
1038
}
1039

1040
int fd_get_diskseq(int fd, uint64_t *ret) {
56,779✔
1041
        uint64_t diskseq;
56,779✔
1042

1043
        assert(fd >= 0);
56,779✔
1044
        assert(ret);
56,779✔
1045

1046
        if (ioctl(fd, BLKGETDISKSEQ, &diskseq) < 0) {
56,779✔
1047
                /* Note that the kernel is weird: non-existing ioctls currently return EINVAL
1048
                 * rather than ENOTTY on loopback block devices. They should fix that in the kernel,
1049
                 * but in the meantime we accept both here. */
1050
                if (!ERRNO_IS_NOT_SUPPORTED(errno) && errno != EINVAL)
×
1051
                        return -errno;
×
1052

1053
                return -EOPNOTSUPP;
1054
        }
1055

1056
        *ret = diskseq;
56,779✔
1057

1058
        return 0;
56,779✔
1059
}
1060

1061
int path_is_root_at(int dir_fd, const char *path) {
3,624,638✔
1062
        _cleanup_close_ int fd = -EBADF, pfd = -EBADF;
3,624,638✔
1063

1064
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
3,624,638✔
1065

1066
        if (!isempty(path)) {
3,624,638✔
1067
                fd = openat(dir_fd, path, O_PATH|O_DIRECTORY|O_CLOEXEC);
124,995✔
1068
                if (fd < 0)
124,995✔
1069
                        return errno == ENOTDIR ? false : -errno;
14,263✔
1070

1071
                dir_fd = fd;
1072
        }
1073

1074
        pfd = openat(dir_fd, "..", O_PATH|O_DIRECTORY|O_CLOEXEC);
3,610,375✔
1075
        if (pfd < 0)
3,610,375✔
1076
                return errno == ENOTDIR ? false : -errno;
2✔
1077

1078
        /* Even if the parent directory has the same inode, the fd may not point to the root directory "/",
1079
         * and we also need to check that the mount ids are the same. Otherwise, a construct like the
1080
         * following could be used to trick us:
1081
         *
1082
         * $ mkdir /tmp/x /tmp/x/y
1083
         * $ mount --bind /tmp/x /tmp/x/y
1084
         */
1085

1086
        return fds_are_same_mount(dir_fd, pfd);
3,610,373✔
1087
}
1088

1089
int fds_are_same_mount(int fd1, int fd2) {
3,610,443✔
1090
        struct statx sx1 = {}, sx2 = {}; /* explicitly initialize the struct to make msan silent. */
3,610,443✔
1091
        int r;
3,610,443✔
1092

1093
        assert(fd1 >= 0);
3,610,443✔
1094
        assert(fd2 >= 0);
3,610,443✔
1095

1096
        if (statx(fd1, "", AT_EMPTY_PATH, STATX_TYPE|STATX_INO|STATX_MNT_ID, &sx1) < 0)
3,610,443✔
UNCOV
1097
                return -errno;
×
1098

1099
        if (statx(fd2, "", AT_EMPTY_PATH, STATX_TYPE|STATX_INO|STATX_MNT_ID, &sx2) < 0)
3,610,443✔
UNCOV
1100
                return -errno;
×
1101

1102
        /* First, compare inode. If these are different, the fd does not point to the root directory "/". */
1103
        if (!statx_inode_same(&sx1, &sx2))
3,610,443✔
1104
                return false;
1105

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

1110
        if (!FLAGS_SET(sx1.stx_mask, STATX_MNT_ID)) {
3,283,508✔
UNCOV
1111
                int mntid;
×
1112

UNCOV
1113
                r = path_get_mnt_id_at_fallback(fd1, "", &mntid);
×
1114
                if (r < 0)
×
UNCOV
1115
                        return r;
×
1116
                assert(mntid >= 0);
×
1117

1118
                sx1.stx_mnt_id = mntid;
×
1119
                sx1.stx_mask |= STATX_MNT_ID;
×
1120
        }
1121

1122
        if (!FLAGS_SET(sx2.stx_mask, STATX_MNT_ID)) {
3,283,508✔
UNCOV
1123
                int mntid;
×
1124

UNCOV
1125
                r = path_get_mnt_id_at_fallback(fd2, "", &mntid);
×
1126
                if (r < 0)
×
UNCOV
1127
                        return r;
×
1128
                assert(mntid >= 0);
×
1129

1130
                sx2.stx_mnt_id = mntid;
×
1131
                sx2.stx_mask |= STATX_MNT_ID;
×
1132
        }
1133

1134
        return statx_mount_same(&sx1, &sx2);
3,283,508✔
1135
}
1136

1137
const char* accmode_to_string(int flags) {
176✔
1138
        switch (flags & O_ACCMODE) {
176✔
1139
        case O_RDONLY:
1140
                return "ro";
1141
        case O_WRONLY:
3✔
1142
                return "wo";
3✔
1143
        case O_RDWR:
170✔
1144
                return "rw";
170✔
UNCOV
1145
        default:
×
UNCOV
1146
                return NULL;
×
1147
        }
1148
}
1149

1150
char* format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd) {
1✔
1151
        assert(buf);
1✔
1152
        assert(fd >= 0);
1✔
1153
        assert(pid >= 0);
1✔
1154
        assert_se(snprintf_ok(buf, PROC_PID_FD_PATH_MAX, "/proc/" PID_FMT "/fd/%i", pid == 0 ? getpid_cached() : pid, fd));
1✔
1155
        return buf;
1✔
1156
}
1157

1158
int proc_fd_enoent_errno(void) {
7✔
1159
        int r;
7✔
1160

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

1165
        r = proc_mounted();
7✔
1166
        if (r == 0)
7✔
1167
                return -ENOSYS;  /* /proc/ is not available or not set up properly, we're most likely
1168
                                    in some chroot environment. */
1169
        if (r > 0)
7✔
1170
                return -EBADF;   /* If /proc/ is definitely around then this means the fd is not valid. */
7✔
1171

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