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

systemd / systemd / 17419082553

02 Sep 2025 08:38AM UTC coverage: 72.223% (+0.009%) from 72.214%
17419082553

push

github

bluca
TEST-45-TIMEDATE: rotate journal before parsing log message

Similar to 04ade57a4.

Fixes the following failure (long line is wrapped):
```
TEST-45-TIMEDATE.sh[234]: + journalctl --sync
TEST-45-TIMEDATE.sh[234]: + for _ in {0..9}
TEST-45-TIMEDATE.sh[234]: + journalctl -q -n 1 '--since=2025-09-01 17:09:25.442936' -p info -t busctl --grep .
TEST-45-TIMEDATE.sh[363]: Sep 01 17:09:25 H busctl[351]: {"type":"signal","endian":"l","flags":1,"version":1,
  "cookie":67,"timestamp-realtime":1756746565578529,"sender":":1.11","path":"/org/freedesktop/timedate1",
  "interface":"org.freedesktop.DBus.Properties","member":"PropertiesChanged","payload":{"type":"sa{sv}as",
    "data":["org.freedesktop.timedate1",{"NTP":{"type":"b","data":false}},[]]}}
TEST-45-TIMEDATE.sh[366]: ++ journalctl -q -n 1 '--since=2025-09-01 17:09:25.442936' -p info -t busctl -o cat
TEST-45-TIMEDATE.sh[367]: ++ jq -r '.payload.data[1].NTP.data'
TEST-45-TIMEDATE.sh[366]: Journal file /run/log/journal/07946b7846564ac0ad1c86e8cabed329/system.journal is truncated, ignoring file.
TEST-45-TIMEDATE.sh[234]: + [[ '' == \f\a\l\s\e ]]
```

302474 of 418806 relevant lines covered (72.22%)

641746.43 hits per line

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

77.53
/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,474,211✔
34
        assert(fd >= 0);
35,474,211✔
35

36
        if (close(fd) >= 0)
35,474,211✔
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)
16,232✔
49
                return 0;
50

51
        return -errno;
16,232✔
52
}
53

54
int safe_close(int fd) {
93,943,496✔
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,943,496✔
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,043,897✔
71
        }
72

73
        return -EBADF;
93,943,496✔
74
}
75

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

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

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

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

93
        FOREACH_ARRAY(fd, fds, n_fds)
3,267,874✔
94
                safe_close(*fd);
51,708✔
95
}
3,216,166✔
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) {
653✔
105
        assert(fds || n_fds == 0);
653✔
106

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

111
int fclose_nointr(FILE *f) {
1,885,664✔
112
        assert(f);
1,885,664✔
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,885,664✔
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,885,664✔
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,622,133✔
129

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

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

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

138
        return NULL;
3,622,133✔
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,762,053✔
153
        int flags, nflags;
1,762,053✔
154

155
        assert(fd >= 0);
1,762,053✔
156

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

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

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

168
        return 1;
169
}
170

171
int stdio_disable_nonblock(void) {
14,925✔
172
        int ret = 0;
14,925✔
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));
14,925✔
178
        RET_GATHER(ret, fd_nonblock(STDOUT_FILENO, false));
14,925✔
179
        RET_GATHER(ret, fd_nonblock(STDERR_FILENO, false));
14,925✔
180

181
        return ret;
14,925✔
182
}
183

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

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

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

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

197
        return RET_NERRNO(fcntl(fd, F_SETFD, nflags));
73,276✔
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) {
33,342✔
216
        assert(fd >= 0);
33,342✔
217
        assert(fds || n_fds == 0);
33,342✔
218

219
        FOREACH_ARRAY(i, fds, n_fds) {
12,491,589✔
220
                if (*i < 0)
12,460,358✔
221
                        continue;
×
222

223
                if (*i == fd)
12,460,358✔
224
                        return true;
225
        }
226

227
        return false;
228
}
229

230
int get_max_fd(void) {
9✔
231
        struct rlimit rl;
9✔
232
        rlim_t m;
9✔
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)
9✔
238
                return -errno;
×
239

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

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

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

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

254
        assert(except || n_except == 0);
4✔
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();
4✔
262
        if (max_fd < 0)
4✔
263
                return max_fd;
4✔
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)
4✔
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) {
27,992✔
272
                int q;
27,988✔
273

274
                if (fd_in_set(fd, except, n_except))
27,988✔
275
                        continue;
1,757✔
276

277
                q = close_nointr(fd);
26,231✔
278
                if (q != -EBADF)
26,231✔
279
                        RET_GATHER(r, q);
10,000✔
280
        }
281

282
        return r;
283
}
284

285
static bool have_close_range = true; /* Assume we live in the future */
286

287
static int close_all_fds_special_case(const int except[], size_t n_except) {
44,646✔
288
        assert(n_except == 0 || except);
44,646✔
289

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

294
        if (!have_close_range)
44,646✔
295
                return 0;
296

297
        if (n_except == 1 && except[0] < 0) /* Minor optimization: if we only got one fd, and it's invalid,
44,642✔
298
                                             * we got none */
299
                n_except = 0;
300

301
        switch (n_except) {
44,642✔
302

303
        case 0:
8,322✔
304
                /* Close everything. Yay! */
305

306
                if (close_range(3, INT_MAX, 0) >= 0)
8,322✔
307
                        return 1;
308

309
                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
×
310
                        have_close_range = false;
×
311
                        return 0;
×
312
                }
313

314
                return -errno;
×
315

316
        case 1:
14,727✔
317
                /* Close all but exactly one, then we don't need no sorting. This is a pretty common
318
                 * case, hence let's handle it specially. */
319

320
                if ((except[0] <= 3 || close_range(3, except[0]-1, 0) >= 0) &&
14,727✔
321
                    (except[0] >= INT_MAX || close_range(MAX(3, except[0]+1), -1, 0) >= 0))
14,727✔
322
                        return 1;
14,727✔
323

324
                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
×
325
                        have_close_range = false;
×
326
                        return 0;
×
327
                }
328

329
                return -errno;
×
330

331
        default:
332
                return 0;
333
        }
334
}
335

336
int close_all_fds_without_malloc(const int except[], size_t n_except) {
×
337
        int r;
×
338

339
        assert(n_except == 0 || except);
×
340

341
        r = close_all_fds_special_case(except, n_except);
×
342
        if (r < 0)
×
343
                return r;
344
        if (r > 0) /* special case worked! */
×
345
                return 0;
346

347
        return close_all_fds_frugal(except, n_except);
×
348
}
349

350
int close_all_fds(const int except[], size_t n_except) {
44,646✔
351
        _cleanup_closedir_ DIR *d = NULL;
44,646✔
352
        int r = 0;
44,646✔
353

354
        assert(n_except == 0 || except);
44,646✔
355

356
        r = close_all_fds_special_case(except, n_except);
44,646✔
357
        if (r < 0)
44,646✔
358
                return r;
359
        if (r > 0) /* special case worked! */
44,646✔
360
                return 0;
361

362
        if (have_close_range) {
21,597✔
363
                _cleanup_free_ int *sorted_malloc = NULL;
21,593✔
364
                size_t n_sorted;
21,593✔
365
                int *sorted;
21,593✔
366

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

373
                assert(n_except < SIZE_MAX);
21,593✔
374
                n_sorted = n_except + 1;
21,593✔
375

376
                if (n_sorted > 64) /* Use heap for large numbers of fds, stack otherwise */
21,593✔
377
                        sorted = sorted_malloc = new(int, n_sorted);
4✔
378
                else
379
                        sorted = newa(int, n_sorted);
21,589✔
380

381
                if (sorted) {
21,593✔
382
                        memcpy(sorted, except, n_except * sizeof(int));
21,593✔
383

384
                        /* Let's add fd 2 to the list of fds, to simplify the loop below, as this
385
                         * allows us to cover the head of the array the same way as the body */
386
                        sorted[n_sorted-1] = 2;
21,593✔
387

388
                        typesafe_qsort(sorted, n_sorted, cmp_int);
21,593✔
389

390
                        for (size_t i = 0; i < n_sorted-1; i++) {
102,141✔
391
                                int start, end;
80,550✔
392

393
                                start = MAX(sorted[i], 2); /* The first three fds shall always remain open */
80,550✔
394
                                end = MAX(sorted[i+1], 2);
80,550✔
395

396
                                assert(end >= start);
80,550✔
397

398
                                if (end - start <= 1)
80,550✔
399
                                        continue;
32,370✔
400

401
                                /* Close everything between the start and end fds (both of which shall stay open) */
402
                                if (close_range(start + 1, end - 1, 0) < 0) {
48,180✔
403
                                        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
2✔
404
                                                return -errno;
×
405

406
                                        have_close_range = false;
2✔
407
                                        break;
2✔
408
                                }
409
                        }
410

411
                        if (have_close_range) {
21,593✔
412
                                /* The loop succeeded. Let's now close everything beyond the end */
413

414
                                if (sorted[n_sorted-1] >= INT_MAX) /* Dont let the addition below overflow */
21,591✔
415
                                        return 0;
416

417
                                if (close_range(sorted[n_sorted-1] + 1, INT_MAX, 0) >= 0)
21,591✔
418
                                        return 0;
419

420
                                if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
×
421
                                        return -errno;
×
422

423
                                have_close_range = false;
×
424
                        }
425
                }
426

427
                /* Fallback on OOM or if close_range() is not supported */
428
        }
429

430
        d = opendir("/proc/self/fd");
6✔
431
        if (!d)
6✔
432
                return close_all_fds_frugal(except, n_except); /* ultimate fallback if /proc/ is not available */
4✔
433

434
        FOREACH_DIRENT(de, d, return -errno) {
5,368✔
435
                int fd = -EBADF, q;
5,362✔
436

437
                if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
5,362✔
438
                        continue;
×
439

440
                fd = parse_fd(de->d_name);
5,362✔
441
                if (fd < 0)
5,362✔
442
                        /* Let's better ignore this, just in case */
443
                        continue;
×
444

445
                if (fd < 3)
5,362✔
446
                        continue;
6✔
447

448
                if (fd == dirfd(d))
5,356✔
449
                        continue;
2✔
450

451
                if (fd_in_set(fd, except, n_except))
5,354✔
452
                        continue;
354✔
453

454
                q = close_nointr(fd);
5,000✔
455
                if (q < 0 && q != -EBADF && r >= 0) /* Valgrind has its own FD and doesn't want to have it closed */
5,000✔
456
                        r = q;
×
457
        }
458

459
        return r;
460
}
461

462
int pack_fds(int fds[], size_t n_fds) {
9,828✔
463
        if (n_fds <= 0)
9,828✔
464
                return 0;
465

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

473
        assert(fds);
1,675✔
474

475
        for (int start = 0;;) {
476
                int restart_from = -1;
1,675✔
477

478
                for (int i = start; i < (int) n_fds; i++) {
4,818✔
479
                        int nfd;
3,143✔
480

481
                        /* Already at right index? */
482
                        if (fds[i] == i + 3)
3,143✔
483
                                continue;
3✔
484

485
                        nfd = fcntl(fds[i], F_DUPFD, i + 3);
3,140✔
486
                        if (nfd < 0)
3,140✔
487
                                return -errno;
×
488

489
                        safe_close(fds[i]);
3,140✔
490
                        fds[i] = nfd;
3,140✔
491

492
                        /* Hmm, the fd we wanted isn't free? Then
493
                         * let's remember that and try again from here */
494
                        if (nfd != i + 3 && restart_from < 0)
3,140✔
495
                                restart_from = i;
×
496
                }
497

498
                if (restart_from < 0)
1,675✔
499
                        break;
500

501
                start = restart_from;
502
        }
503

504
        assert(fds[0] == 3);
1,675✔
505

506
        return 0;
507
}
508

509
int fd_validate(int fd) {
110,057✔
510
        if (fd < 0)
110,057✔
511
                return -EBADF;
512

513
        if (fcntl(fd, F_GETFD) < 0)
110,055✔
514
                return -errno;
43,958✔
515

516
        return 0;
517
}
518

519
int same_fd(int a, int b) {
10,050✔
520
        struct stat sta, stb;
10,050✔
521
        pid_t pid;
10,050✔
522
        int r, fa, fb;
10,050✔
523

524
        assert(a >= 0);
10,050✔
525
        assert(b >= 0);
10,050✔
526

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

536
        if (a == b) {
10,050✔
537
                /* Let's validate that the fd is valid */
538
                r = fd_validate(a);
7✔
539
                if (r < 0)
7✔
540
                        return r;
10,050✔
541

542
                return true;
6✔
543
        }
544

545
        /* Try to use F_DUPFD_QUERY if we have it first, as it is the nicest API */
546
        r = fcntl(a, F_DUPFD_QUERY, b);
10,043✔
547
        if (r > 0)
10,043✔
548
                return true;
549
        if (r == 0) {
10,035✔
550
                /* 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. */
551
                r = fd_validate(b);
10,034✔
552
                if (r < 0)
10,034✔
553
                        return r;
554

555
                return false;
10,033✔
556
        }
557
        /* 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. */
558
        if (errno == EBADF) {
1✔
559
                /* EBADF could mean two things: the first fd is not valid, or it is valid and is O_PATH and
560
                 * F_DUPFD_QUERY is not supported. Let's validate the fd explicitly, to distinguish this
561
                 * case. */
562
                r = fd_validate(a);
1✔
563
                if (r < 0)
1✔
564
                        return r;
565

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

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

578
        /* We have neither F_DUPFD_QUERY nor kcmp(), use fstat() instead. */
579
        if (fstat(a, &sta) < 0)
×
580
                return -errno;
×
581

582
        if (fstat(b, &stb) < 0)
×
583
                return -errno;
×
584

585
        if (!stat_inode_same(&sta, &stb))
×
586
                return false;
587

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

591
        if (S_ISCHR(sta.st_mode) || S_ISBLK(sta.st_mode))
×
592
                return false;
593

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

600
        fb = fcntl(b, F_GETFL);
×
601
        if (fb < 0)
×
602
                return -errno;
×
603

604
        return fa == fb;
×
605
}
606

607
bool fdname_is_valid(const char *s) {
13,729✔
608
        const char *p;
13,729✔
609

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

619
        if (!s)
13,729✔
620
                return false;
621

622
        for (p = s; *p; p++) {
209,244✔
623
                if (*p < ' ')
195,520✔
624
                        return false;
625
                if (*p >= 127)
195,520✔
626
                        return false;
627
                if (*p == ':')
195,520✔
628
                        return false;
629
        }
630

631
        return p - s <= FDNAME_MAX;
13,724✔
632
}
633

634
int fd_get_path(int fd, char **ret) {
1,712,393✔
635
        int r;
1,712,393✔
636

637
        assert(fd >= 0 || fd == AT_FDCWD);
1,712,393✔
638

639
        if (fd == AT_FDCWD)
1,712,393✔
640
                return safe_getcwd(ret);
5,107✔
641

642
        r = readlink_malloc(FORMAT_PROC_FD_PATH(fd), ret);
1,707,286✔
643
        if (r == -ENOENT)
1,707,286✔
644
                return proc_fd_enoent_errno();
4✔
645
        return r;
646
}
647

648
int move_fd(int from, int to, int cloexec) {
22,120✔
649
        int r;
22,120✔
650

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

655
        if (from < 0)
22,120✔
656
                return -EBADF;
657
        if (to < 0)
22,120✔
658
                return -EBADF;
659

660
        if (from == to) {
22,120✔
661

662
                if (cloexec >= 0) {
×
663
                        r = fd_cloexec(to, cloexec);
×
664
                        if (r < 0)
×
665
                                return r;
666
                }
667

668
                return to;
×
669
        }
670

671
        if (cloexec < 0) {
22,120✔
672
                int fl;
×
673

674
                fl = fcntl(from, F_GETFD, 0);
×
675
                if (fl < 0)
×
676
                        return -errno;
×
677

678
                cloexec = FLAGS_SET(fl, FD_CLOEXEC);
×
679
        }
680

681
        r = dup3(from, to, cloexec ? O_CLOEXEC : 0);
44,240✔
682
        if (r < 0)
22,120✔
683
                return -errno;
×
684

685
        assert(r == to);
22,120✔
686

687
        safe_close(from);
22,120✔
688

689
        return to;
22,120✔
690
}
691

692
int fd_move_above_stdio(int fd) {
715,706✔
693
        int flags, copy;
715,706✔
694
        PROTECT_ERRNO;
715,706✔
695

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

709
        if (fd < 0 || fd > 2)
715,706✔
710
                return fd;
711

712
        flags = fcntl(fd, F_GETFD, 0);
110✔
713
        if (flags < 0)
110✔
714
                return fd;
715

716
        if (flags & FD_CLOEXEC)
110✔
717
                copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
108✔
718
        else
719
                copy = fcntl(fd, F_DUPFD, 3);
2✔
720
        if (copy < 0)
110✔
721
                return fd;
722

723
        assert(copy > 2);
110✔
724

725
        (void) close(fd);
110✔
726
        return copy;
727
}
728

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

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

753
        null_readable = original_input_fd < 0;
15,227✔
754
        null_writable = original_output_fd < 0 || original_error_fd < 0;
15,227✔
755

756
        /* First step, open /dev/null once, if we need it */
757
        if (null_readable || null_writable) {
15,227✔
758

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

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

771
                        copy = fcntl(null_fd, F_DUPFD_CLOEXEC, 3); /* Duplicate this with O_CLOEXEC set */
13✔
772
                        if (copy < 0) {
13✔
773
                                r = -errno;
×
774
                                goto finish;
×
775
                        }
776

777
                        close_and_replace(null_fd, copy);
13✔
778
                }
779
        }
780

781
        /* Let's assemble fd[] with the fds to install in place of stdin/stdout/stderr */
782
        for (int i = 0; i < 3; i++)
60,908✔
783
                if (fd[i] < 0)
45,681✔
784
                        fd[i] = null_fd;        /* A negative parameter means: connect this one to /dev/null */
12,847✔
785
                else if (fd[i] != i && fd[i] < 3) {
32,834✔
786
                        /* 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. */
787
                        copy_fd[i] = fcntl(fd[i], F_DUPFD_CLOEXEC, 3); /* Duplicate this with O_CLOEXEC set */
81✔
788
                        if (copy_fd[i] < 0) {
81✔
789
                                r = -errno;
×
790
                                goto finish;
×
791
                        }
792

793
                        fd[i] = copy_fd[i];
81✔
794
                }
795

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

808
                        if (dup2(fd[i], i) < 0) { /* Turns off O_CLOEXEC on the new fd. */
40,765✔
809
                                r = -errno;
×
810
                                goto finish;
×
811
                        }
812
                }
813

814
        r = 0;
815

816
finish:
15,227✔
817
        /* Close the original fds, but only if they were outside of the stdio range. Also, properly check for the same
818
         * fd passed in multiple times. */
819
        safe_close_above_stdio(original_input_fd);
15,227✔
820
        if (original_output_fd != original_input_fd)
15,227✔
821
                safe_close_above_stdio(original_output_fd);
14,985✔
822
        if (original_error_fd != original_input_fd && original_error_fd != original_output_fd)
15,227✔
823
                safe_close_above_stdio(original_error_fd);
14,812✔
824

825
        /* Close the copies we moved > 2 */
826
        close_many(copy_fd, 3);
15,227✔
827

828
        /* Close our null fd, if it's > 2 */
829
        safe_close_above_stdio(null_fd);
15,227✔
830

831
        return r;
15,227✔
832
}
833

834
int fd_reopen(int fd, int flags) {
1,337,559✔
835
        assert(fd >= 0 || fd == AT_FDCWD);
1,337,559✔
836
        assert(!FLAGS_SET(flags, O_CREAT));
1,337,559✔
837

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

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

860
        if (FLAGS_SET(flags, O_DIRECTORY) || fd == AT_FDCWD)
1,337,553✔
861
                /* If we shall reopen the fd as directory we can just go via "." and thus bypass the whole
862
                 * magic /proc/ directory, and make ourselves independent of that being mounted. */
863
                return RET_NERRNO(openat(fd, ".", flags | O_DIRECTORY));
219,062✔
864

865
        int new_fd = open(FORMAT_PROC_FD_PATH(fd), flags);
1,118,497✔
866
        if (new_fd < 0) {
1,118,497✔
867
                if (errno != ENOENT)
47,367✔
868
                        return -errno;
47,363✔
869

870
                return proc_fd_enoent_errno();
4✔
871
        }
872

873
        return new_fd;
874
}
875

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

889
        assert(fd >= 0);
38✔
890
        assert(!(flags & (O_APPEND|O_DIRECTORY)));
38✔
891

892
        int existing_flags = fcntl(fd, F_GETFL);
38✔
893
        if (existing_flags < 0)
38✔
894
                return -errno;
×
895

896
        int new_fd = fd_reopen(fd, flags | (existing_flags & O_APPEND));
38✔
897
        if (new_fd < 0)
38✔
898
                return new_fd;
899

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

913
        return new_fd;
914
}
915

916
int fd_reopen_condition(
1,205,949✔
917
                int fd,
918
                int flags,
919
                int mask,
920
                int *ret_new_fd) {
921

922
        int r, new_fd;
1,205,949✔
923

924
        assert(fd >= 0);
1,205,949✔
925
        assert(!FLAGS_SET(flags, O_CREAT));
1,205,949✔
926

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

931
        r = fcntl(fd, F_GETFL);
1,205,949✔
932
        if (r < 0)
1,205,949✔
933
                return -errno;
×
934

935
        if ((r & mask) == (flags & mask)) {
1,205,949✔
936
                *ret_new_fd = -EBADF;
1,200,443✔
937
                return fd;
1,200,443✔
938
        }
939

940
        new_fd = fd_reopen(fd, flags);
5,506✔
941
        if (new_fd < 0)
5,506✔
942
                return new_fd;
943

944
        *ret_new_fd = new_fd;
5,506✔
945
        return new_fd;
5,506✔
946
}
947

948
int fd_is_opath(int fd) {
446,596✔
949
        int r;
446,596✔
950

951
        assert(fd >= 0);
446,596✔
952

953
        r = fcntl(fd, F_GETFL);
446,596✔
954
        if (r < 0)
446,596✔
955
                return -errno;
×
956

957
        return FLAGS_SET(r, O_PATH);
446,596✔
958
}
959

960
int fd_verify_safe_flags_full(int fd, int extra_flags) {
507✔
961
        int flags, unexpected_flags;
507✔
962

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

977
        assert(fd >= 0);
507✔
978

979
        flags = fcntl(fd, F_GETFL);
507✔
980
        if (flags < 0)
507✔
981
                return -errno;
×
982

983
        unexpected_flags = flags & ~(O_ACCMODE_STRICT|O_NOFOLLOW|RAW_O_LARGEFILE|extra_flags);
507✔
984
        if (unexpected_flags != 0)
507✔
985
                return log_debug_errno(SYNTHETIC_ERRNO(EREMOTEIO),
×
986
                                       "Unexpected flags set for extrinsic fd: 0%o",
987
                                       (unsigned) unexpected_flags);
988

989
        return flags & (O_ACCMODE_STRICT | extra_flags); /* return the flags variable, but remove the noise */
507✔
990
}
991

992
unsigned read_nr_open(void) {
29,929✔
993
        _cleanup_free_ char *nr_open = NULL;
29,929✔
994
        int r;
29,929✔
995

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

999
        r = read_one_line_file("/proc/sys/fs/nr_open", &nr_open);
29,929✔
1000
        if (r < 0)
29,929✔
1001
                log_debug_errno(r, "Failed to read /proc/sys/fs/nr_open, ignoring: %m");
29,929✔
1002
        else {
1003
                unsigned v;
29,926✔
1004

1005
                r = safe_atou(nr_open, &v);
29,926✔
1006
                if (r < 0)
29,926✔
1007
                        log_debug_errno(r, "Failed to parse /proc/sys/fs/nr_open value '%s', ignoring: %m", nr_open);
×
1008
                else
1009
                        return v;
29,926✔
1010
        }
1011

1012
        /* If we fail, fall back to the hard-coded kernel limit of 1024 * 1024. */
1013
        return NR_OPEN_DEFAULT;
1014
}
1015

1016
int fd_get_diskseq(int fd, uint64_t *ret) {
52,772✔
1017
        uint64_t diskseq;
52,772✔
1018

1019
        assert(fd >= 0);
52,772✔
1020
        assert(ret);
52,772✔
1021

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

1029
                return -EOPNOTSUPP;
1030
        }
1031

1032
        *ret = diskseq;
52,772✔
1033

1034
        return 0;
52,772✔
1035
}
1036

1037
int path_is_root_at(int dir_fd, const char *path) {
3,708,644✔
1038
        _cleanup_close_ int fd = -EBADF, pfd = -EBADF;
3,708,644✔
1039

1040
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
3,708,644✔
1041

1042
        if (!isempty(path)) {
3,708,644✔
1043
                fd = openat(dir_fd, path, O_PATH|O_DIRECTORY|O_CLOEXEC);
99,341✔
1044
                if (fd < 0)
99,341✔
1045
                        return errno == ENOTDIR ? false : -errno;
15,390✔
1046

1047
                dir_fd = fd;
1048
        }
1049

1050
        pfd = openat(dir_fd, "..", O_PATH|O_DIRECTORY|O_CLOEXEC);
3,693,254✔
1051
        if (pfd < 0)
3,693,254✔
1052
                return errno == ENOTDIR ? false : -errno;
2✔
1053

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

1062
        return fds_are_same_mount(dir_fd, pfd);
3,693,252✔
1063
}
1064

1065
int fds_are_same_mount(int fd1, int fd2) {
3,693,272✔
1066
        struct statx sx1 = {}, sx2 = {}; /* explicitly initialize the struct to make msan silent. */
3,693,272✔
1067
        int r;
3,693,272✔
1068

1069
        assert(fd1 >= 0);
3,693,272✔
1070
        assert(fd2 >= 0);
3,693,272✔
1071

1072
        if (statx(fd1, "", AT_EMPTY_PATH, STATX_TYPE|STATX_INO|STATX_MNT_ID, &sx1) < 0)
3,693,272✔
1073
                return -errno;
×
1074

1075
        if (statx(fd2, "", AT_EMPTY_PATH, STATX_TYPE|STATX_INO|STATX_MNT_ID, &sx2) < 0)
3,693,272✔
1076
                return -errno;
×
1077

1078
        /* First, compare inode. If these are different, the fd does not point to the root directory "/". */
1079
        if (!statx_inode_same(&sx1, &sx2))
3,693,272✔
1080
                return false;
1081

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

1086
        if (!FLAGS_SET(sx1.stx_mask, STATX_MNT_ID)) {
3,444,845✔
1087
                int mntid;
×
1088

1089
                r = path_get_mnt_id_at_fallback(fd1, "", &mntid);
×
1090
                if (r < 0)
×
1091
                        return r;
×
1092
                assert(mntid >= 0);
×
1093

1094
                sx1.stx_mnt_id = mntid;
×
1095
                sx1.stx_mask |= STATX_MNT_ID;
×
1096
        }
1097

1098
        if (!FLAGS_SET(sx2.stx_mask, STATX_MNT_ID)) {
3,444,845✔
1099
                int mntid;
×
1100

1101
                r = path_get_mnt_id_at_fallback(fd2, "", &mntid);
×
1102
                if (r < 0)
×
1103
                        return r;
×
1104
                assert(mntid >= 0);
×
1105

1106
                sx2.stx_mnt_id = mntid;
×
1107
                sx2.stx_mask |= STATX_MNT_ID;
×
1108
        }
1109

1110
        return statx_mount_same(&sx1, &sx2);
3,444,845✔
1111
}
1112

1113
char* format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
3,092,314✔
1114
        assert(buf);
3,092,314✔
1115
        assert(fd >= 0);
3,092,314✔
1116
        assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
3,092,314✔
1117
        return buf;
3,092,314✔
1118
}
1119

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

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

1141
int proc_fd_enoent_errno(void) {
8✔
1142
        int r;
8✔
1143

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

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

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