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

systemd / systemd / 25705508282

11 May 2026 11:07PM UTC coverage: 72.65% (+0.1%) from 72.511%
25705508282

push

github

bluca
firstboot,sysinstall,hostnamed: always show FANCY_NAME=

This makes sure that whenever we want to show the OS name we can show
the fancy name. Thus this moves the escaping/validation of the fancy
name out of hostnamed into generic code, and then makes use of it in
sysinstall,firstboot,prompt-util.

17 of 36 new or added lines in 6 files covered. (47.22%)

2673 existing lines in 72 files now uncovered.

327104 of 450245 relevant lines covered (72.65%)

1200575.51 hits per line

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

93.17
/src/basic/fileio.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <stdio_ext.h>
5
#include <stdlib.h>
6
#include <sys/stat.h>
7
#include <unistd.h>
8

9
#include "alloc-util.h"
10
#include "chase.h"
11
#include "errno-util.h"
12
#include "extract-word.h"
13
#include "fd-util.h"
14
#include "fileio.h"
15
#include "fs-util.h"
16
#include "hexdecoct.h"
17
#include "io-util.h"
18
#include "iovec-util.h"
19
#include "label.h"
20
#include "log.h"
21
#include "mkdir.h"
22
#include "nulstr-util.h"
23
#include "parse-util.h"
24
#include "path-util.h"
25
#include "socket-util.h"
26
#include "stat-util.h"
27
#include "stdio-util.h"
28
#include "string-util.h"
29
#include "strv.h"
30
#include "sync-util.h"
31
#include "terminal-util.h"
32
#include "time-util.h"
33
#include "tmpfile-util.h"
34

35
/* The maximum size of the file we'll read in one go in read_full_file() (64M). */
36
#define READ_FULL_BYTES_MAX (64U * U64_MB - UINT64_C(1))
37
/* Used when a size is specified for read_full_file() with READ_FULL_FILE_UNBASE64 or _UNHEX */
38
#define READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY 3
39

40
/* The maximum size of virtual files (i.e. procfs, sysfs, and other virtual "API" files) we'll read in one go
41
 * in read_virtual_file(). Note that this limit is different (and much lower) than the READ_FULL_BYTES_MAX
42
 * limit. This reflects the fact that we use different strategies for reading virtual and regular files:
43
 * virtual files we generally have to read in a single read() syscall since the kernel doesn't support
44
 * continuation read()s for them. Thankfully they are somewhat size constrained. Thus we can allocate the
45
 * full potential buffer in advance. Regular files OTOH can be much larger, and there we grow the allocations
46
 * exponentially in a loop. We use a size limit of 4M-2 because 4M-1 is the maximum buffer that /proc/sys/
47
 * allows us to read() (larger reads will fail with ENOMEM), and we want to read one extra byte so that we
48
 * can detect EOFs. */
49
#define READ_VIRTUAL_BYTES_MAX (4U * U64_MB - UINT64_C(2))
50

51
int fdopen_unlocked(int fd, const char *options, FILE **ret) {
421,085✔
52
        assert(ret);
421,085✔
53

54
        FILE *f = fdopen(fd, options);
421,085✔
55
        if (!f)
421,085✔
56
                return -errno;
×
57

58
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
421,085✔
59

60
        *ret = f;
421,085✔
61
        return 0;
421,085✔
62
}
63

64
int take_fdopen_unlocked(int *fd, const char *options, FILE **ret) {
421,085✔
65
        int r;
421,085✔
66

67
        assert(fd);
421,085✔
68

69
        r = fdopen_unlocked(*fd, options, ret);
421,085✔
70
        if (r < 0)
421,085✔
71
                return r;
72

73
        *fd = -EBADF;
421,085✔
74

75
        return 0;
421,085✔
76
}
77

78
FILE* take_fdopen(int *fd, const char *options) {
37,513✔
79
        assert(fd);
37,513✔
80

81
        FILE *f = fdopen(*fd, options);
37,513✔
82
        if (!f)
37,513✔
83
                return NULL;
84

85
        *fd = -EBADF;
37,513✔
86

87
        return f;
37,513✔
88
}
89

90
DIR* take_fdopendir(int *dfd) {
216,413✔
91
        assert(dfd);
216,413✔
92

93
        DIR *d = fdopendir(*dfd);
216,413✔
94
        if (!d)
216,413✔
95
                return NULL;
96

97
        *dfd = -EBADF;
216,413✔
98

99
        return d;
216,413✔
100
}
101

102
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) {
822,475✔
103
        FILE *f = open_memstream(ptr, sizeloc);
822,475✔
104
        if (!f)
822,475✔
105
                return NULL;
106

107
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
822,475✔
108

109
        return f;
822,475✔
110
}
111

112
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode) {
91✔
113
        FILE *f = fmemopen(buf, size, mode);
91✔
114
        if (!f)
91✔
115
                return NULL;
116

117
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
91✔
118

119
        return f;
91✔
120
}
121

122
int write_string_stream_full(
238,781✔
123
                FILE *f,
124
                const char *line,
125
                WriteStringFileFlags flags,
126
                const struct timespec *ts) {
127

128
        bool needs_nl;
238,781✔
129
        int r, fd = -EBADF;
238,781✔
130

131
        assert(f);
238,781✔
132
        assert(line);
238,781✔
133

134
        if (ferror(f))
238,781✔
135
                return -EIO;
136

137
        if (ts) {
238,781✔
138
                /* If we shall set the timestamp we need the fd. But fmemopen() streams generally don't have
139
                 * an fd. Let's fail early in that case. */
140
                fd = fileno(f);
2✔
141
                if (fd < 0)
2✔
142
                        return -EBADF;
143
        }
144

145
        if (flags & WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) {
238,781✔
146
                _cleanup_free_ char *t = NULL;
8,981✔
147

148
                /* If value to be written is same as that of the existing value, then suppress the write. */
149

150
                if (fd < 0) {
8,981✔
151
                        fd = fileno(f);
8,981✔
152
                        if (fd < 0)
8,981✔
153
                                return -EBADF;
154
                }
155

156
                /* Read an additional byte to detect cases where the prefix matches but the rest
157
                 * doesn't. Also, 0 returned by read_virtual_file_fd() means the read was truncated and
158
                 * it won't be equal to the new value. */
159
                if (read_virtual_file_fd(fd, strlen(line)+1, &t, NULL) > 0 &&
17,768✔
160
                    streq_skip_trailing_chars(line, t, NEWLINE)) {
8,787✔
161
                        log_debug("No change in value '%s', suppressing write", line);
6,564✔
162
                        return 0;
163
                }
164

165
                if (lseek(fd, 0, SEEK_SET) < 0)
2,417✔
166
                        return -errno;
×
167
        }
168

169
        needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
232,217✔
170

171
        if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
79,014✔
172
                /* If STDIO buffering was disabled, then let's append the newline character to the string
173
                 * itself, so that the write goes out in one go, instead of two */
174

175
                line = strjoina(line, "\n");
387,985✔
176
                needs_nl = false;
77,597✔
177
        }
178

179
        if (fputs(line, f) == EOF)
232,217✔
180
                return -errno;
19✔
181

182
        if (needs_nl)
232,198✔
183
                if (fputc('\n', f) == EOF)
1,416✔
184
                        return -errno;
×
185

186
        if (flags & WRITE_STRING_FILE_SYNC)
232,198✔
187
                r = fflush_sync_and_check(f);
2,131✔
188
        else
189
                r = fflush_and_check(f);
230,067✔
190
        if (r < 0)
232,198✔
191
                return r;
192

193
        if (ts) {
232,170✔
194
                const struct timespec twice[2] = {*ts, *ts};
2✔
195

196
                assert(fd >= 0);
2✔
197
                if (futimens(fd, twice) < 0)
2✔
198
                        return -errno;
×
199
        }
200

201
        return 0;
202
}
203

204
static mode_t write_string_file_flags_to_mode(WriteStringFileFlags flags) {
125,695✔
205

206
        /* We support three different modes, that are the ones that really make sense for text files like this:
207
         *
208
         *     → 0600 (i.e. root-only)
209
         *     → 0444 (i.e. read-only)
210
         *     → 0644 (i.e. writable for root, readable for everyone else)
211
         */
212

213
        return FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0600) ? 0600 :
125,695✔
214
                FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0444) ? 0444 : 0644;
125,445✔
215
}
216

217
static int write_string_file_atomic_at(
3,077✔
218
                int dir_fd,
219
                const char *fn,
220
                const char *line,
221
                WriteStringFileFlags flags,
222
                const struct timespec *ts) {
223

224
        _cleanup_fclose_ FILE *f = NULL;
3,077✔
225
        _cleanup_free_ char *p = NULL;
3,077✔
226
        int r;
3,077✔
227

228
        assert(fn);
3,077✔
229
        assert(line);
3,077✔
230

231
        /* Note that we'd really like to use O_TMPFILE here, but can't really, since we want replacement
232
         * semantics here, and O_TMPFILE can't offer that. i.e. rename() replaces but linkat() doesn't. */
233

234
        mode_t mode = write_string_file_flags_to_mode(flags);
3,077✔
235

236
        bool call_label_ops_post = false;
3,077✔
237
        if (FLAGS_SET(flags, WRITE_STRING_FILE_LABEL)) {
3,077✔
238
                r = label_ops_pre(dir_fd, fn, mode);
188✔
239
                if (r < 0)
188✔
240
                        return r;
241

242
                call_label_ops_post = true;
243
        }
244

245
        r = fopen_temporary_at(dir_fd, fn, &f, &p);
3,077✔
246
        int k = call_label_ops_post ? label_ops_post(f ? fileno(f) : dir_fd, f ? NULL : fn, /* created= */ !!f) : 0;
3,265✔
247
        /* If fopen_temporary_at() failed in the above, propagate the error code, and ignore failures in
248
         * label_ops_post(). */
249
        if (r < 0)
3,077✔
250
                return r;
251
        CLEANUP_TMPFILE_AT(dir_fd, p);
3,076✔
252
        if (k < 0)
3,076✔
253
                return k;
254

255
        r = write_string_stream_full(f, line, flags, ts);
3,076✔
256
        if (r < 0)
3,076✔
257
                return r;
258

259
        r = fchmod_umask(fileno(f), mode);
3,076✔
260
        if (r < 0)
3,076✔
261
                return r;
262

263
        r = RET_NERRNO(renameat(dir_fd, p, dir_fd, fn));
3,076✔
264
        if (r < 0)
×
265
                return r;
266

267
        p = mfree(p); /* disarm CLEANUP_TMPFILE_AT() */
3,076✔
268

269
        if (FLAGS_SET(flags, WRITE_STRING_FILE_SYNC)) {
3,076✔
270
                /* Sync the rename, too */
271
                r = fsync_directory_of_file(fileno(f));
2,131✔
272
                if (r < 0)
2,131✔
273
                        return r;
×
274
        }
275

276
        return 0;
277
}
278

279
int write_string_file_full(
197,931✔
280
                int dir_fd,
281
                const char *fn,
282
                const char *line,
283
                WriteStringFileFlags flags,
284
                const struct timespec *ts,
285
                const char *label_fn) {
286

287
        bool made_file = false;
197,931✔
288
        _cleanup_fclose_ FILE *f = NULL;
197,931✔
289
        _cleanup_close_ int fd = -EBADF;
197,931✔
290
        int r;
197,931✔
291

292
        assert(dir_fd == AT_FDCWD || dir_fd >= 0);
197,931✔
293
        assert(line);
197,931✔
294

295
        /* We don't know how to verify whether the file contents was already on-disk. */
296
        assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
197,931✔
297

298
        if (flags & WRITE_STRING_FILE_MKDIR_0755) {
197,931✔
299
                assert(fn);
548✔
300

301
                r = mkdirat_parents(dir_fd, fn, 0755);
548✔
302
                if (r < 0)
548✔
303
                        return r;
304
        }
305

306
        if (flags & WRITE_STRING_FILE_ATOMIC) {
197,931✔
307
                assert(fn);
3,077✔
308
                assert(flags & WRITE_STRING_FILE_CREATE);
3,077✔
309

310
                r = write_string_file_atomic_at(dir_fd, fn, line, flags, ts);
3,077✔
311
                if (r < 0)
3,077✔
312
                        goto fail;
1✔
313

314
                return r;
315
        }
316

317
        /* We manually build our own version of fopen(..., "we") that works without O_CREAT and with O_NOFOLLOW if needed. */
318
        if (isempty(fn))
194,854✔
319
                r = fd = fd_reopen(
72,236✔
320
                                ASSERT_FD(dir_fd), O_CLOEXEC | O_NOCTTY |
72,236✔
321
                                (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
144,472✔
322
                                (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
72,236✔
323
                                (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0));
72,236✔
324
        else {
325
                mode_t mode = write_string_file_flags_to_mode(flags);
122,618✔
326
                bool call_label_ops_post = false;
122,618✔
327

328
                if (FLAGS_SET(flags, WRITE_STRING_FILE_LABEL|WRITE_STRING_FILE_CREATE)) {
122,618✔
329
                        r = label_ops_pre(dir_fd, label_fn ?: fn, mode);
828✔
330
                        if (r < 0)
828✔
331
                                goto fail;
×
332

333
                        call_label_ops_post = true;
334
                }
335

336
                r = fd = openat_report_new(
367,854✔
337
                                dir_fd, fn, O_CLOEXEC | O_NOCTTY |
122,618✔
338
                                (FLAGS_SET(flags, WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0) |
122,618✔
339
                                (FLAGS_SET(flags, WRITE_STRING_FILE_CREATE) ? O_CREAT : 0) |
122,618✔
340
                                (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
245,236✔
341
                                (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
122,618✔
342
                                (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0),
122,618✔
343
                                mode,
344
                                &made_file);
345
                if (call_label_ops_post)
122,618✔
346
                        /* If openat_report_new() failed in the above, propagate the error code, and ignore
347
                         * failures in label_ops_post(). */
348
                        RET_GATHER(r, label_ops_post(fd >= 0 ? fd : dir_fd, fd >= 0 ? NULL : fn, made_file));
2,484✔
349
        }
350
        if (r < 0)
194,854✔
351
                goto fail;
1,748✔
352

353
        r = take_fdopen_unlocked(&fd, "w", &f);
193,106✔
354
        if (r < 0)
193,106✔
355
                goto fail;
×
356

357
        if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
193,106✔
358
                setvbuf(f, NULL, _IONBF, 0);
191,301✔
359

360
        r = write_string_stream_full(f, line, flags, ts);
193,106✔
361
        if (r < 0)
193,106✔
362
                goto fail;
24✔
363

364
        return 0;
365

366
fail:
1,773✔
367
        if (made_file)
1,773✔
368
                (void) unlinkat(dir_fd, fn, 0);
×
369

370
        if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
1,773✔
371
                return r;
372

373
        f = safe_fclose(f);
1,631✔
374
        fd = safe_close(fd);
1,631✔
375

376
        /* OK, the operation failed, but let's see if the right contents in place already. If so, eat up the
377
         * error. */
378
        if (verify_file_at(dir_fd, fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) || (flags & WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE)) > 0)
1,633✔
379
                return 0;
463✔
380

381
        return r;
382
}
383

384
int write_string_filef(
253✔
385
                const char *fn,
386
                WriteStringFileFlags flags,
387
                const char *format, ...) {
388

389
        _cleanup_free_ char *p = NULL;
253✔
390
        va_list ap;
253✔
391
        int r;
253✔
392

393
        va_start(ap, format);
253✔
394
        r = vasprintf(&p, format, ap);
253✔
395
        va_end(ap);
253✔
396

397
        if (r < 0)
253✔
398
                return -ENOMEM;
399

400
        return write_string_file(fn, p, flags);
253✔
401
}
402

403
int write_base64_file_at(
15✔
404
                int dir_fd,
405
                const char *fn,
406
                const struct iovec *data,
407
                WriteStringFileFlags flags) {
408

409
        _cleanup_free_ char *encoded = NULL;
15✔
410
        ssize_t n;
15✔
411

412
        n = base64mem_full(data ? data->iov_base : NULL, data ? data->iov_len : 0, 79, &encoded);
15✔
413
        if (n < 0)
15✔
414
                return n;
×
415

416
        return write_string_file_at(dir_fd, fn, encoded, flags);
15✔
417
}
418

419
int read_one_line_file_at(int dir_fd, const char *filename, char **ret) {
243,892✔
420
        _cleanup_fclose_ FILE *f = NULL;
243,892✔
421
        int r;
243,892✔
422

423
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
243,892✔
424
        assert(filename);
243,892✔
425
        assert(ret);
243,892✔
426

427
        r = fopen_unlocked_at(dir_fd, filename, "re", /* open_flags= */ 0, &f);
243,892✔
428
        if (r < 0)
243,892✔
429
                return r;
430

431
        return read_line(f, LONG_LINE_MAX, ret);
243,892✔
432
}
433

434
int read_boolean_file_at(int dir_fd, const char *filename) {
939✔
435
        _cleanup_free_ char *s = NULL;
939✔
436
        int r;
939✔
437

438
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
939✔
439
        assert(filename);
939✔
440

441
        r = read_one_line_file_at(dir_fd, filename, &s);
939✔
442
        if (r < 0)
939✔
443
                return r;
444

445
        return parse_boolean(s);
519✔
446
}
447

448
int verify_file_at(int dir_fd, const char *fn, const char *blob, bool accept_extra_nl) {
1,631✔
449
        _cleanup_fclose_ FILE *f = NULL;
1,631✔
450
        _cleanup_free_ char *buf = NULL;
1,631✔
451
        size_t l, k;
1,631✔
452
        int r;
1,631✔
453

454
        assert(blob);
1,631✔
455

456
        l = strlen(blob);
1,631✔
457

458
        if (accept_extra_nl && endswith(blob, "\n"))
1,631✔
459
                accept_extra_nl = false;
1✔
460

461
        buf = malloc(l + accept_extra_nl + 1);
1,631✔
462
        if (!buf)
1,631✔
463
                return -ENOMEM;
464

465
        r = fopen_unlocked_at(dir_fd, strempty(fn), "re", 0, &f);
1,631✔
466
        if (r < 0)
1,631✔
467
                return r;
468

469
        /* We try to read one byte more than we need, so that we know whether we hit eof */
470
        errno = 0;
878✔
471
        k = fread(buf, 1, l + accept_extra_nl + 1, f);
878✔
472
        if (ferror(f))
878✔
473
                return errno_or_else(EIO);
1,631✔
474

475
        if (k != l && k != l + accept_extra_nl)
878✔
476
                return 0;
477
        if (memcmp(buf, blob, l) != 0)
748✔
478
                return 0;
479
        if (k > l && buf[l] != '\n')
463✔
UNCOV
480
                return 0;
×
481

482
        return 1;
483
}
484

485
int read_virtual_file_at(
817,429✔
486
                int dir_fd,
487
                const char *filename,
488
                size_t max_size,
489
                char **ret_contents,
490
                size_t *ret_size) {
491

492
        _cleanup_free_ char *buf = NULL;
817,429✔
493
        size_t n, size;
817,429✔
494
        int n_retries;
817,429✔
495
        bool truncated = false;
817,429✔
496

497
        /* Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work with two sorts of
498
         * virtual files. One sort uses "seq_file", and the results of the first read are buffered for the
499
         * second read. The other sort uses "raw" reads which always go direct to the device. In the latter
500
         * case, the content of the virtual file must be retrieved with a single read otherwise a second read
501
         * might get the new value instead of finding EOF immediately. That's the reason why the usage of
502
         * fread(3) is prohibited in this case as it always performs a second call to read(2) looking for
503
         * EOF. See issue #13585.
504
         *
505
         * max_size specifies a limit on the bytes read. If max_size is SIZE_MAX, the full file is read. If
506
         * the full file is too large to read, an error is returned. For other values of max_size, *partial
507
         * contents* may be returned. (Though the read is still done using one syscall.) Returns 0 on
508
         * partial success, 1 if untruncated contents were read.
509
         *
510
         * Rule: for kernfs files using "seq_file" → use regular read_full_file_at()
511
         *       for kernfs files using "raw" → use read_virtual_file_at()
512
         */
513

514
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
817,429✔
515
        assert(max_size <= READ_VIRTUAL_BYTES_MAX || max_size == SIZE_MAX);
817,429✔
516

517
        _cleanup_close_ int fd = -EBADF;
817,429✔
518
        if (isempty(filename))
817,429✔
519
                fd = fd_reopen(ASSERT_FD(dir_fd), O_RDONLY | O_NOCTTY | O_CLOEXEC);
641,457✔
520
        else
521
                fd = RET_NERRNO(openat(dir_fd, filename, O_RDONLY | O_NOCTTY | O_CLOEXEC));
175,972✔
522
        if (fd < 0)
817,429✔
523
                return fd;
524

525
        /* Limit the number of attempts to read the number of bytes returned by fstat(). */
526
        n_retries = 3;
527

528
        for (;;) {
763,586✔
529
                struct stat st;
763,585✔
530

531
                if (fstat(fd, &st) < 0)
763,585✔
532
                        return -errno;
1,181✔
533

534
                if (!S_ISREG(st.st_mode))
763,585✔
535
                        return -EBADF;
536

537
                /* Be prepared for files from /proc which generally report a file size of 0. */
538
                assert_cc(READ_VIRTUAL_BYTES_MAX < SSIZE_MAX);
763,585✔
539
                if (st.st_size > 0 && n_retries > 1) {
763,585✔
540
                        /* Let's use the file size if we have more than 1 attempt left. On the last attempt
541
                         * we'll ignore the file size */
542

543
                        if (st.st_size > SSIZE_MAX) { /* Avoid overflow with 32-bit size_t and 64-bit off_t. */
584,188✔
544

545
                                if (max_size == SIZE_MAX)
546
                                        return -EFBIG;
547

548
                                size = max_size;
549
                        } else {
550
                                size = MIN((size_t) st.st_size, max_size);
584,188✔
551

552
                                if (size > READ_VIRTUAL_BYTES_MAX)
584,188✔
553
                                        return -EFBIG;
554
                        }
555

556
                        n_retries--;
584,188✔
557
                } else if (n_retries > 1) {
179,397✔
558
                        /* Files in /proc are generally smaller than the page size so let's start with
559
                         * a page size buffer from malloc and only use the max buffer on the final try. */
560
                        size = MIN3(page_size() - 1, READ_VIRTUAL_BYTES_MAX, max_size);
179,396✔
561
                        n_retries = 1;
179,396✔
562
                } else {
563
                        size = MIN(READ_VIRTUAL_BYTES_MAX, max_size);
1✔
564
                        n_retries = 0;
1✔
565
                }
566

567
                buf = malloc(size + 1);
763,585✔
568
                if (!buf)
763,585✔
569
                        return -ENOMEM;
570

571
                /* Use a bigger allocation if we got it anyway, but not more than the limit. */
572
                size = MIN3(MALLOC_SIZEOF_SAFE(buf) - 1, max_size, READ_VIRTUAL_BYTES_MAX);
763,585✔
573

574
                for (;;) {
763,585✔
575
                        ssize_t k;
763,585✔
576

577
                        /* Read one more byte so we can detect whether the content of the
578
                         * file has already changed or the guessed size for files from /proc
579
                         * wasn't large enough . */
580
                        k = read(fd, buf, size + 1);
763,585✔
581
                        if (k >= 0) {
763,585✔
582
                                n = k;
762,404✔
583
                                break;
762,404✔
584
                        }
585

586
                        if (errno != EINTR)
1,181✔
587
                                return -errno;
1,181✔
588
                }
589

590
                /* Consider a short read as EOF */
591
                if (n <= size)
762,404✔
592
                        break;
593

594
                /* If a maximum size is specified and we already read more we know the file is larger, and
595
                 * can handle this as truncation case. Note that if the size of what we read equals the
596
                 * maximum size then this doesn't mean truncation, the file might or might not end on that
597
                 * byte. We need to rerun the loop in that case, with a larger buffer size, so that we read
598
                 * at least one more byte to be able to distinguish EOF from truncation. */
599
                if (max_size != SIZE_MAX && n > max_size) {
33,387✔
600
                        n = size; /* Make sure we never use more than what we sized the buffer for (so that
601
                                   * we have one free byte in it for the trailing NUL we add below). */
602
                        truncated = true;
603
                        break;
604
                }
605

606
                /* We have no further attempts left? Then the file is apparently larger than our limits. Give up. */
607
                if (n_retries <= 0)
1✔
608
                        return -EFBIG;
609

610
                /* Hmm... either we read too few bytes from /proc or less likely the content of the file
611
                 * might have been changed (and is now bigger) while we were processing, let's try again
612
                 * either with the new file size. */
613

614
                if (lseek(fd, 0, SEEK_SET) < 0)
1✔
UNCOV
615
                        return -errno;
×
616

617
                buf = mfree(buf);
1✔
618
        }
619

620
        if (ret_contents) {
762,403✔
621

622
                /* Safety check: if the caller doesn't want to know the size of what we just read it will
623
                 * rely on the trailing NUL byte. But if there's an embedded NUL byte, then we should refuse
624
                 * operation as otherwise there'd be ambiguity about what we just read. */
625
                if (!ret_size && memchr(buf, 0, n))
729,271✔
626
                        return -EBADMSG;
627

628
                if (n < size) {
729,271✔
629
                        char *p;
720,755✔
630

631
                        /* Return rest of the buffer to libc */
632
                        p = realloc(buf, n + 1);
720,755✔
633
                        if (!p)
720,755✔
634
                                return -ENOMEM;
635
                        buf = p;
720,755✔
636
                }
637

638
                buf[n] = 0;
729,271✔
639
                *ret_contents = TAKE_PTR(buf);
729,271✔
640
        }
641

642
        if (ret_size)
762,403✔
643
                *ret_size = n;
599,663✔
644

645
        return !truncated;
762,403✔
646
}
647

648
int read_full_stream_full(
544,658✔
649
                FILE *f,
650
                const char *filename,
651
                uint64_t offset,
652
                size_t size,
653
                ReadFullFileFlags flags,
654
                char **ret_contents,
655
                size_t *ret_size) {
656

657
        _cleanup_free_ char *buf = NULL;
544,658✔
658
        size_t n, n_next = 0, l, expected_decoded_size = size;
544,658✔
659
        int fd, r;
544,658✔
660

661
        assert(f);
544,658✔
662
        assert(ret_contents);
544,658✔
663
        assert(!FLAGS_SET(flags, READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX));
544,658✔
664
        assert(size != SIZE_MAX || !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER));
544,658✔
665

666
        if (offset != UINT64_MAX && offset > LONG_MAX) /* fseek() can only deal with "long" offsets */
544,658✔
667
                return -ERANGE;
668

669
        if ((flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) != 0) {
544,658✔
670
                if (size <= SIZE_MAX / READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY)
64✔
671
                        size *= READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY;
62✔
672
                else
673
                        size = SIZE_MAX;
674
        }
675

676
        fd = fileno(f);
544,658✔
677
        if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see
544,658✔
678
                        * fmemopen()), let's optimize our buffering */
679
                struct stat st;
544,580✔
680

681
                if (fstat(fd, &st) < 0)
544,580✔
UNCOV
682
                        return -errno;
×
683

684
                if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR)) {
544,580✔
UNCOV
685
                        r = stat_verify_regular(&st);
×
UNCOV
686
                        if (r < 0)
×
687
                                return r;
688
                }
689

690
                if (S_ISREG(st.st_mode)) {
544,580✔
691

692
                        /* Try to start with the right file size if we shall read the file in full. Note
693
                         * that we increase the size to read here by one, so that the first read attempt
694
                         * already makes us notice the EOF. If the reported size of the file is zero, we
695
                         * avoid this logic however, since quite likely it might be a virtual file in procfs
696
                         * that all report a zero file size. */
697

698
                        if (st.st_size > 0 &&
543,589✔
699
                            (size == SIZE_MAX || FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER))) {
1,359✔
700

701
                                uint64_t rsize =
1,516,539✔
702
                                        LESS_BY((uint64_t) st.st_size, offset == UINT64_MAX ? 0 : offset);
505,513✔
703

704
                                if (rsize < SIZE_MAX) /* overflow check */
505,513✔
705
                                        n_next = rsize + 1;
505,513✔
706
                        }
707

708
                        if (flags & READ_FULL_FILE_WARN_WORLD_READABLE)
543,589✔
709
                                (void) warn_file_is_world_accessible(filename, &st, NULL, 0);
142✔
710
                }
711
        } else if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR))
78✔
712
                return -EBADFD;
713

714
        /* If we don't know how much to read, figure it out now. If we shall read a part of the file, then
715
         * allocate the requested size. If we shall load the full file start with LINE_MAX. Note that if
716
         * READ_FULL_FILE_FAIL_WHEN_LARGER we consider the specified size a safety limit, and thus also start
717
         * with LINE_MAX, under assumption the file is most likely much shorter. */
718
        if (n_next == 0)
544,580✔
719
                n_next = size != SIZE_MAX && !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) ? size : LINE_MAX;
39,145✔
720

721
        /* Never read more than we need to determine that our own limit is hit */
722
        if (n_next > READ_FULL_BYTES_MAX)
505,713✔
UNCOV
723
                n_next = READ_FULL_BYTES_MAX + 1;
×
724

725
        if (offset != UINT64_MAX && fseek(f, offset, SEEK_SET) < 0)
544,658✔
UNCOV
726
                return -errno;
×
727

728
        n = l = 0;
544,658✔
729
        for (;;) {
545,410✔
730
                char *t;
545,034✔
731
                size_t k;
545,034✔
732

733
                /* If we shall fail when reading overly large data, then read exactly one byte more than the
734
                 * specified size at max, since that'll tell us if there's anymore data beyond the limit. */
735
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && n_next > size)
545,034✔
736
                        n_next = size + 1;
8✔
737

738
                if (flags & READ_FULL_FILE_SECURE) {
545,034✔
739
                        t = malloc(n_next + 1);
1,613✔
740
                        if (!t) {
1,613✔
UNCOV
741
                                r = -ENOMEM;
×
UNCOV
742
                                goto finalize;
×
743
                        }
744
                        memcpy_safe(t, buf, n);
1,613✔
745
                        explicit_bzero_safe(buf, n);
1,613✔
746
                        free(buf);
1,613✔
747
                } else {
748
                        t = realloc(buf, n_next + 1);
543,421✔
749
                        if (!t)
543,421✔
750
                                return -ENOMEM;
751
                }
752

753
                buf = t;
545,034✔
754
                /* Unless a size has been explicitly specified, try to read as much as fits into the memory
755
                 * we allocated (minus 1, to leave one byte for the safety NUL byte) */
756
                n = size == SIZE_MAX ? MALLOC_SIZEOF_SAFE(buf) - 1 : n_next;
545,034✔
757

758
                errno = 0;
545,034✔
759
                k = fread(buf + l, 1, n - l, f);
545,034✔
760

761
                assert(k <= n - l);
545,034✔
762
                l += k;
545,034✔
763

764
                if (ferror(f)) {
545,034✔
765
                        r = errno_or_else(EIO);
2✔
766
                        goto finalize;
2✔
767
                }
768
                if (feof(f))
545,032✔
769
                        break;
770

771
                if (size != SIZE_MAX && !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER)) { /* If we got asked to read some specific size, we already sized the buffer right, hence leave */
476✔
772
                        assert(l == size);
94✔
773
                        break;
774
                }
775

776
                assert(k > 0); /* we can't have read zero bytes because that would have been EOF */
382✔
777

778
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > size) {
382✔
779
                        r = -E2BIG;
6✔
780
                        goto finalize;
6✔
781
                }
782

783
                if (n >= READ_FULL_BYTES_MAX) {
376✔
UNCOV
784
                        r = -E2BIG;
×
UNCOV
785
                        goto finalize;
×
786
                }
787

788
                n_next = MIN(n * 2, READ_FULL_BYTES_MAX);
376✔
789
        }
790

791
        if (flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) {
544,650✔
792
                _cleanup_free_ void *decoded = NULL;
62✔
793
                size_t decoded_size;
62✔
794

795
                buf[l++] = 0;
62✔
796
                if (flags & READ_FULL_FILE_UNBASE64)
62✔
797
                        r = unbase64mem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
60✔
798
                else
799
                        r = unhexmem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
2✔
800
                if (r < 0)
62✔
UNCOV
801
                        goto finalize;
×
802

803
                if (flags & READ_FULL_FILE_SECURE)
62✔
804
                        explicit_bzero_safe(buf, n);
19✔
805
                free_and_replace(buf, decoded);
62✔
806
                n = l = decoded_size;
62✔
807

808
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > expected_decoded_size) {
62✔
UNCOV
809
                        r = -E2BIG;
×
UNCOV
810
                        goto finalize;
×
811
                }
812
        }
813

814
        if (!ret_size) {
544,650✔
815
                /* Safety check: if the caller doesn't want to know the size of what we just read it will rely on the
816
                 * trailing NUL byte. But if there's an embedded NUL byte, then we should refuse operation as otherwise
817
                 * there'd be ambiguity about what we just read. */
818

819
                if (memchr(buf, 0, l)) {
374,978✔
820
                        r = -EBADMSG;
2✔
821
                        goto finalize;
2✔
822
                }
823
        }
824

825
        buf[l] = 0;
544,648✔
826
        *ret_contents = TAKE_PTR(buf);
544,648✔
827

828
        if (ret_size)
544,648✔
829
                *ret_size = l;
169,672✔
830

831
        return 0;
832

833
finalize:
10✔
834
        if (flags & READ_FULL_FILE_SECURE)
10✔
835
                explicit_bzero_safe(buf, n);
3✔
836

837
        return r;
838
}
839

840
int read_full_file_full(
721,092✔
841
                int dir_fd,
842
                const char *filename,
843
                uint64_t offset,
844
                size_t size,
845
                ReadFullFileFlags flags,
846
                const char *bind_name,
847
                char **ret_contents,
848
                size_t *ret_size) {
849

850
        _cleanup_fclose_ FILE *f = NULL;
721,092✔
851
        XfopenFlags xflags = XFOPEN_UNLOCKED;
721,092✔
852
        int r;
721,092✔
853

854
        assert(ret_contents);
721,092✔
855

856
        if (FLAGS_SET(flags, READ_FULL_FILE_CONNECT_SOCKET) && /* If this is enabled, let's try to connect to it */
721,092✔
857
            offset == UINT64_MAX)                              /* Seeking is not supported on AF_UNIX sockets */
858
                xflags |= XFOPEN_SOCKET;
216✔
859

860
        r = xfopenat_full(dir_fd, filename, "re", 0, xflags, bind_name, &f);
721,092✔
861
        if (r < 0)
721,092✔
862
                return r;
863

864
        return read_full_stream_full(f, filename, offset, size, flags, ret_contents, ret_size);
522,360✔
865
}
866

867
int script_get_shebang_interpreter(const char *path, char **ret) {
8✔
868
        _cleanup_fclose_ FILE *f = NULL;
8✔
869
        int r;
8✔
870

871
        assert(path);
8✔
872

873
        f = fopen(path, "re");
8✔
874
        if (!f)
8✔
875
                return -errno;
1✔
876

877
        char c;
7✔
878
        r = safe_fgetc(f, &c);
7✔
879
        if (r < 0)
7✔
880
                return r;
881
        if (r == 0)
7✔
882
                return -EBADMSG;
883
        if (c != '#')
7✔
884
                return -EMEDIUMTYPE;
885
        r = safe_fgetc(f, &c);
2✔
886
        if (r < 0)
2✔
887
                return r;
888
        if (r == 0)
2✔
889
                return -EBADMSG;
890
        if (c != '!')
2✔
891
                return -EMEDIUMTYPE;
892

893
        _cleanup_free_ char *line = NULL;
2✔
894
        r = read_line(f, LONG_LINE_MAX, &line);
2✔
895
        if (r < 0)
2✔
896
                return r;
897

898
        _cleanup_free_ char *p = NULL;
2✔
899
        const char *s = line;
2✔
900

901
        r = extract_first_word(&s, &p, /* separators= */ NULL, /* flags= */ 0);
2✔
902
        if (r < 0)
2✔
903
                return r;
904
        if (r == 0)
2✔
905
                return -ENOEXEC;
906

907
        if (ret)
2✔
908
                *ret = TAKE_PTR(p);
2✔
909
        return 0;
910
}
911

912
int get_proc_field(const char *path, const char *key, char **ret) {
43,843✔
913
        _cleanup_fclose_ FILE *f = NULL;
43,843✔
914
        int r;
43,843✔
915

916
        /* Retrieve one field from a file like /proc/self/status. "key" matches the beginning of the line
917
         * and should not include whitespace or the delimiter (':').
918
         * Whitespaces after the ':' will be skipped. Only the first element is returned
919
         * (i.e. for /proc/meminfo line "MemTotal: 1024 kB" -> return "1024"). */
920

921
        assert(path);
43,843✔
922
        assert(key);
43,843✔
923

924
        r = fopen_unlocked(path, "re", &f);
43,843✔
925
        if (r == -ENOENT && proc_mounted() == 0)
43,843✔
926
                return -ENOSYS;
927
        if (r < 0)
43,828✔
928
                return r;
929

930
        for (;;) {
239,288✔
931
                 _cleanup_free_ char *line = NULL;
138,488✔
932

933
                 r = read_line(f, LONG_LINE_MAX, &line);
138,488✔
934
                 if (r < 0)
138,488✔
935
                         return r;
936
                 if (r == 0)
138,488✔
937
                         return -ENODATA;
938

939
                 char *l = startswith(line, key);
138,488✔
940
                 if (l && *l == ':') {
138,488✔
941
                         if (ret) {
37,688✔
942
                                 char *s = strdupcspn(skip_leading_chars(l + 1, " \t"), WHITESPACE);
37,688✔
943
                                 if (!s)
37,688✔
944
                                         return -ENOMEM;
945

946
                                 *ret = s;
37,688✔
947
                         }
948

949
                         return 0;
950
                 }
951
        }
952
}
953

954
DIR* xopendirat(int dir_fd, const char *path, int flags) {
176,909✔
955
        _cleanup_close_ int fd = -EBADF;
176,909✔
956

957
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
176,909✔
958
        assert(!(flags & (O_CREAT|O_TMPFILE)));
176,909✔
959

960
        if ((dir_fd == AT_FDCWD || path_is_absolute(path)) &&
178,922✔
961
            (flags &~ O_DIRECTORY) == 0)
1,406✔
UNCOV
962
                return opendir(path);
×
963

964
        if (isempty(path)) {
176,909✔
965
                path = ".";
173,490✔
966
                flags |= O_NOFOLLOW;
173,490✔
967
        }
968

969
        fd = openat(dir_fd, path, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
176,909✔
970
        if (fd < 0)
176,909✔
971
                return NULL;
972

973
        return take_fdopendir(&fd);
176,406✔
974
}
975

976
int fopen_mode_to_flags(const char *mode) {
88,113✔
977
        const char *p;
88,113✔
978
        int flags;
88,113✔
979

980
        assert(mode);
88,113✔
981

982
        if ((p = startswith(mode, "r+")))
88,113✔
983
                flags = O_RDWR;
88,113✔
984
        else if ((p = startswith(mode, "r")))
88,112✔
985
                flags = O_RDONLY;
UNCOV
986
        else if ((p = startswith(mode, "w+")))
×
987
                flags = O_RDWR|O_CREAT|O_TRUNC;
UNCOV
988
        else if ((p = startswith(mode, "w")))
×
989
                flags = O_WRONLY|O_CREAT|O_TRUNC;
UNCOV
990
        else if ((p = startswith(mode, "a+")))
×
991
                flags = O_RDWR|O_CREAT|O_APPEND;
992
        else if ((p = startswith(mode, "a")))
×
993
                flags = O_WRONLY|O_CREAT|O_APPEND;
994
        else
995
                return -EINVAL;
996

997
        for (; *p != 0; p++) {
171,720✔
998

999
                switch (*p) {
83,607✔
1000

1001
                case 'e':
83,607✔
1002
                        flags |= O_CLOEXEC;
83,607✔
1003
                        break;
83,607✔
1004

UNCOV
1005
                case 'x':
×
UNCOV
1006
                        flags |= O_EXCL;
×
UNCOV
1007
                        break;
×
1008

1009
                case 'm':
1010
                        /* ignore this here, fdopen() might care later though */
1011
                        break;
1012

1013
                case 'c': /* not sure what to do about this one */
1014
                default:
1015
                        return -EINVAL;
1016
                }
1017
        }
1018

1019
        return flags;
1020
}
1021

1022
static int xfopenat_regular(int dir_fd, const char *path, const char *mode, int open_flags, FILE **ret) {
1,093,083✔
1023
        FILE *f;
1,093,083✔
1024

1025
        /* A combination of fopen() with openat() */
1026

1027
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
1,093,083✔
1028
        assert(mode);
1,093,083✔
1029
        assert(ret);
1,093,083✔
1030

1031
        if (dir_fd == AT_FDCWD && path && open_flags == 0)
1,093,083✔
1032
                f = fopen(path, mode);
1,091,539✔
1033
        else if (dir_fd == XAT_FDROOT && path && open_flags == 0) {
1,544✔
1034
                _cleanup_free_ char *j = strjoin("/", path);
18✔
1035
                if (!j)
9✔
UNCOV
1036
                        return -ENOMEM;
×
1037

1038
                f = fopen(j, mode);
9✔
1039
        } else {
1040
                _cleanup_close_ int fd = -EBADF;
1,535✔
1041
                int mode_flags;
1,535✔
1042

1043
                mode_flags = fopen_mode_to_flags(mode);
1,535✔
1044
                if (mode_flags < 0)
1,535✔
1045
                        return mode_flags;
1046

1047
                if (path) {
1,535✔
1048
                        fd = openat(dir_fd, path, mode_flags | open_flags);
1,419✔
1049
                        if (fd < 0)
1,419✔
1050
                                return -errno;
595✔
1051
                } else {
1052
                        if (dir_fd == AT_FDCWD)
116✔
1053
                                return -EBADF;
1054

1055
                        fd = fd_reopen(dir_fd, (mode_flags | open_flags) & ~O_NOFOLLOW);
116✔
1056
                        if (fd < 0)
116✔
1057
                                return fd;
1058
                }
1059

1060
                f = take_fdopen(&fd, mode);
940✔
1061
        }
1062
        if (!f)
1,092,488✔
1063
                return -errno;
236,976✔
1064

1065
        *ret = f;
855,512✔
1066
        return 0;
855,512✔
1067
}
1068

1069
static int xfopenat_unix_socket(int dir_fd, const char *path, const char *bind_name, FILE **ret) {
2✔
1070
        _cleanup_close_ int sk = -EBADF;
2✔
1071
        FILE *f;
2✔
1072
        int r;
2✔
1073

1074
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
2✔
1075
        assert(ret);
2✔
1076

1077
        sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
2✔
1078
        if (sk < 0)
2✔
UNCOV
1079
                return -errno;
×
1080

1081
        if (bind_name) {
2✔
1082
                /* If the caller specified a socket name to bind to, do so before connecting. This is
1083
                 * useful to communicate some minor, short meta-information token from the client to
1084
                 * the server. */
1085
                union sockaddr_union bsa;
1✔
1086

1087
                r = sockaddr_un_set_path(&bsa.un, bind_name);
1✔
1088
                if (r < 0)
1✔
UNCOV
1089
                        return r;
×
1090

1091
                if (bind(sk, &bsa.sa, r) < 0)
1✔
UNCOV
1092
                        return -errno;
×
1093
        }
1094

1095
        r = connect_unix_path(sk, dir_fd, path);
2✔
1096
        if (r < 0)
2✔
1097
                return r;
1098

1099
        if (shutdown(sk, SHUT_WR) < 0)
2✔
UNCOV
1100
                return -errno;
×
1101

1102
        f = take_fdopen(&sk, "r");
2✔
1103
        if (!f)
2✔
UNCOV
1104
                return -errno;
×
1105

1106
        *ret = f;
2✔
1107
        return 0;
2✔
1108
}
1109

1110
int xfopenat_full(
1,093,083✔
1111
                int dir_fd,
1112
                const char *path,
1113
                const char *mode,
1114
                int open_flags,
1115
                XfopenFlags flags,
1116
                const char *bind_name,
1117
                FILE **ret) {
1118

1119
        FILE *f = NULL;  /* avoid false maybe-uninitialized warning */
1,093,083✔
1120
        int r;
1,093,083✔
1121

1122
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
1,093,083✔
1123
        assert(mode);
1,093,083✔
1124
        assert(ret);
1,093,083✔
1125

1126
        r = xfopenat_regular(dir_fd, path, mode, open_flags, &f);
1,093,083✔
1127
        if (r == -ENXIO && FLAGS_SET(flags, XFOPEN_SOCKET)) {
1,093,083✔
1128
                /* ENXIO is what Linux returns if we open a node that is an AF_UNIX socket */
1129
                r = xfopenat_unix_socket(dir_fd, path, bind_name, &f);
2✔
1130
                if (IN_SET(r, -ENOTSOCK, -EINVAL))
2✔
1131
                        return -ENXIO; /* propagate original error if this is not a socket after all */
1,093,083✔
1132
        }
1133
        if (r < 0)
1,093,083✔
1134
                return r;
1135

1136
        if (FLAGS_SET(flags, XFOPEN_UNLOCKED))
855,514✔
1137
                (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
855,254✔
1138

1139
        *ret = f;
855,514✔
1140
        return 0;
855,514✔
1141
}
1142

1143
int fdopen_independent(int fd, const char *mode, FILE **ret) {
14,479✔
1144
        _cleanup_close_ int copy_fd = -EBADF;
14,479✔
1145
        _cleanup_fclose_ FILE *f = NULL;
14,479✔
1146
        int mode_flags;
14,479✔
1147

1148
        assert(fd >= 0);
14,479✔
1149
        assert(mode);
14,479✔
1150
        assert(ret);
14,479✔
1151

1152
        /* A combination of fdopen() + fd_reopen(). i.e. reopens the inode the specified fd points to and
1153
         * returns a FILE* for it */
1154

1155
        mode_flags = fopen_mode_to_flags(mode);
14,479✔
1156
        if (mode_flags < 0)
14,479✔
1157
                return mode_flags;
1158

1159
        /* Flags returned by fopen_mode_to_flags might contain O_CREAT, but it doesn't make sense for fd_reopen
1160
         * since we're working on an existing fd anyway. Let's drop it here to avoid triggering assertion. */
1161
        copy_fd = fd_reopen(fd, mode_flags & ~O_CREAT);
14,479✔
1162
        if (copy_fd < 0)
14,479✔
1163
                return copy_fd;
1164

1165
        f = take_fdopen(&copy_fd, mode);
14,479✔
1166
        if (!f)
14,479✔
1167
                return -errno;
×
1168

1169
        *ret = TAKE_PTR(f);
14,479✔
1170
        return 0;
14,479✔
1171
}
1172

1173
static int search_and_open_internal(
28,244✔
1174
                const char *path,
1175
                int mode,            /* if ret_fd is NULL this is an [FRWX]_OK mode for access(), otherwise an open mode for open() */
1176
                const char *root,
1177
                char **search,
1178
                int *ret_fd,
1179
                char **ret_path) {
1180

1181
        int r;
28,244✔
1182

1183
        assert(!ret_fd || !FLAGS_SET(mode, O_CREAT)); /* We don't support O_CREAT for this */
28,244✔
1184
        assert(path);
28,244✔
1185

1186
        if (path_is_absolute(path)) {
28,244✔
UNCOV
1187
                _cleanup_close_ int fd = -EBADF;
×
1188

1189
                if (ret_fd)
15✔
1190
                        /* We only specify 0777 here to appease static analyzers, it's never used since we
1191
                         * don't support O_CREAT here */
1192
                        r = fd = RET_NERRNO(open(path, mode, 0777));
13✔
1193
                else
1194
                        r = RET_NERRNO(access(path, mode));
2✔
1195
                if (r < 0)
4✔
1196
                        return r;
1197

1198
                if (ret_path) {
11✔
1199
                        r = path_simplify_alloc(path, ret_path);
11✔
1200
                        if (r < 0)
11✔
1201
                                return r;
1202
                }
1203

1204
                if (ret_fd)
11✔
1205
                        *ret_fd = TAKE_FD(fd);
10✔
1206

1207
                return 0;
1208
        }
1209

1210
        if (!path_strv_resolve_uniq(search, root))
28,229✔
1211
                return -ENOMEM;
1212

1213
        STRV_FOREACH(i, search) {
145,342✔
1214
                _cleanup_close_ int fd = -EBADF;
145,357✔
1215
                _cleanup_free_ char *p = NULL;
121,835✔
1216

1217
                p = path_join(root, *i, path);
121,835✔
1218
                if (!p)
121,835✔
1219
                        return -ENOMEM;
1220

1221
                if (ret_fd)
121,835✔
1222
                        /* as above, 0777 is static analyzer appeasement */
1223
                        r = fd = RET_NERRNO(open(p, mode, 0777));
121,385✔
1224
                else
1225
                        r = RET_NERRNO(access(p, F_OK));
450✔
1226
                if (r >= 0) {
117,113✔
1227
                        if (ret_path)
4,722✔
1228
                                *ret_path = path_simplify(TAKE_PTR(p));
4,722✔
1229

1230
                        if (ret_fd)
4,722✔
1231
                                *ret_fd = TAKE_FD(fd);
4,710✔
1232

1233
                        return 0;
1234
                }
1235
                if (r != -ENOENT)
117,113✔
1236
                        return r;
1237
        }
1238

1239
        return -ENOENT;
1240
}
1241

1242
int search_and_open(
28,244✔
1243
                const char *path,
1244
                int mode,
1245
                const char *root,
1246
                char **search,
1247
                int *ret_fd,
1248
                char **ret_path) {
1249

1250
        _cleanup_strv_free_ char **copy = NULL;
28,244✔
1251

1252
        assert(path);
28,244✔
1253

1254
        copy = strv_copy(search);
28,244✔
1255
        if (!copy)
28,244✔
1256
                return -ENOMEM;
1257

1258
        return search_and_open_internal(path, mode, root, copy, ret_fd, ret_path);
28,244✔
1259
}
1260

1261
static int search_and_fopen_internal(
28,234✔
1262
                const char *path,
1263
                const char *mode,
1264
                const char *root,
1265
                char **search,
1266
                FILE **ret_file,
1267
                char **ret_path) {
1268

1269
        _cleanup_free_ char *found_path = NULL;
28,234✔
1270
        _cleanup_close_ int fd = -EBADF;
28,234✔
1271
        int r;
28,234✔
1272

1273
        assert(path);
28,234✔
1274
        assert(mode || !ret_file);
28,234✔
1275

1276
        r = search_and_open(
56,576✔
1277
                        path,
1278
                        mode ? fopen_mode_to_flags(mode) : 0,
28,027✔
1279
                        root,
1280
                        search,
1281
                        ret_file ? &fd : NULL,
1282
                        ret_path ? &found_path : NULL);
1283
        if (r < 0)
28,234✔
1284
                return r;
1285

1286
        if (ret_file) {
4,723✔
1287
                FILE *f = take_fdopen(&fd, mode);
4,720✔
1288
                if (!f)
4,720✔
UNCOV
1289
                        return -errno;
×
1290

1291
                *ret_file = f;
4,720✔
1292
        }
1293

1294
        if (ret_path)
4,723✔
1295
                *ret_path = TAKE_PTR(found_path);
4,723✔
1296

1297
        return 0;
1298
}
1299

1300
int search_and_fopen(
28✔
1301
                const char *path,
1302
                const char *mode,
1303
                const char *root,
1304
                const char **search,
1305
                FILE **ret_file,
1306
                char **ret_path) {
1307

1308
        _cleanup_strv_free_ char **copy = NULL;
28✔
1309

1310
        assert(path);
28✔
1311
        assert(mode || !ret_file);
28✔
1312

1313
        copy = strv_copy((char**) search);
28✔
1314
        if (!copy)
28✔
1315
                return -ENOMEM;
1316

1317
        return search_and_fopen_internal(path, mode, root, copy, ret_file, ret_path);
28✔
1318
}
1319

1320
int search_and_fopen_nulstr(
28,206✔
1321
                const char *path,
1322
                const char *mode,
1323
                const char *root,
1324
                const char *search,
1325
                FILE **ret_file,
1326
                char **ret_path) {
1327

1328
        _cleanup_strv_free_ char **l = NULL;
28,206✔
1329

1330
        assert(path);
28,206✔
1331
        assert(mode || !ret_file);
28,206✔
1332

1333
        l = strv_split_nulstr(search);
28,206✔
1334
        if (!l)
28,206✔
1335
                return -ENOMEM;
1336

1337
        return search_and_fopen_internal(path, mode, root, l, ret_file, ret_path);
28,206✔
1338
}
1339

1340
int fflush_and_check(FILE *f) {
1,245,416✔
1341
        assert(f);
1,245,416✔
1342

1343
        errno = 0;
1,245,416✔
1344
        fflush(f);
1,245,416✔
1345

1346
        if (ferror(f))
1,245,416✔
1347
                return errno_or_else(EIO);
28✔
1348

1349
        return 0;
1350
}
1351

1352
int fflush_sync_and_check(FILE *f) {
2,572✔
1353
        int r, fd;
2,572✔
1354

1355
        assert(f);
2,572✔
1356

1357
        r = fflush_and_check(f);
2,572✔
1358
        if (r < 0)
2,572✔
1359
                return r;
1360

1361
        /* Not all file streams have an fd associated (think: fmemopen()), let's handle this gracefully and
1362
         * assume that in that case we need no explicit syncing */
1363
        fd = fileno(f);
2,572✔
1364
        if (fd < 0)
2,572✔
1365
                return 0;
1366

1367
        r = fsync_full(fd);
2,572✔
1368
        if (r < 0)
2,572✔
UNCOV
1369
                return r;
×
1370

1371
        return 0;
1372
}
1373

1374
int write_timestamp_file_atomic(const char *fn, usec_t n) {
205✔
1375
        char ln[DECIMAL_STR_MAX(n)+2];
205✔
1376

1377
        /* Creates a "timestamp" file, that contains nothing but a
1378
         * usec_t timestamp, formatted in ASCII. */
1379

1380
        if (!timestamp_is_set(n))
205✔
1381
                return -ERANGE;
205✔
1382

1383
        xsprintf(ln, USEC_FMT "\n", n);
205✔
1384

1385
        return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
205✔
1386
}
1387

1388
int read_timestamp_file(const char *fn, usec_t *ret) {
28✔
1389
        _cleanup_free_ char *ln = NULL;
28✔
1390
        uint64_t t;
28✔
1391
        int r;
28✔
1392

1393
        assert(ret);
28✔
1394

1395
        r = read_one_line_file(fn, &ln);
28✔
1396
        if (r < 0)
28✔
1397
                return r;
1398

UNCOV
1399
        r = safe_atou64(ln, &t);
×
1400
        if (r < 0)
×
1401
                return r;
1402

UNCOV
1403
        if (!timestamp_is_set(t))
×
1404
                return -ERANGE;
1405

UNCOV
1406
        *ret = (usec_t) t;
×
UNCOV
1407
        return 0;
×
1408
}
1409

1410
int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space) {
3,474✔
1411
        assert(s);
3,474✔
1412
        assert(space);
3,474✔
1413

1414
        /* Outputs the specified string with fputs(), but optionally prefixes it with a separator.
1415
         * The *space parameter when specified shall initially point to a boolean variable initialized
1416
         * to false. It is set to true after the first invocation. This call is supposed to be use in loops,
1417
         * where a separator shall be inserted between each element, but not before the first one. */
1418

1419
        if (!f)
3,474✔
UNCOV
1420
                f = stdout;
×
1421

1422
        if (!separator)
3,474✔
1423
                separator = " ";
1,789✔
1424

1425
        if (*space)
3,474✔
1426
                if (fputs(separator, f) < 0)
1,533✔
1427
                        return -EIO;
1428

1429
        *space = true;
3,474✔
1430

1431
        if (fputs(s, f) < 0)
3,474✔
UNCOV
1432
                return -EIO;
×
1433

1434
        return 0;
1435
}
1436

1437
int fputs_with_newline(FILE *f, const char *s) {
5,731✔
1438

1439
        /* This is like fputs() but outputs a trailing newline char, but only if the string isn't empty
1440
         * and doesn't end in a newline already. Returns 0 in case we didn't append a newline, > 0 otherwise. */
1441

1442
        if (isempty(s))
5,731✔
1443
                return 0;
1444

1445
        if (!f)
5,731✔
UNCOV
1446
                f = stdout;
×
1447

1448
        if (fputs(s, f) < 0)
5,731✔
1449
                return -EIO;
1450

1451
        if (endswith(s, "\n"))
5,731✔
1452
                return 0;
1453

1454
        if (fputc('\n', f) < 0)
5,326✔
UNCOV
1455
                return -EIO;
×
1456

1457
        return 1;
1458
}
1459

1460
/* A bitmask of the EOL markers we know */
1461
typedef enum EndOfLineMarker {
1462
        EOL_NONE     = 0,
1463
        EOL_ZERO     = 1 << 0,  /* \0 (aka NUL) */
1464
        EOL_TEN      = 1 << 1,  /* \n (aka NL, aka LF)  */
1465
        EOL_THIRTEEN = 1 << 2,  /* \r (aka CR)  */
1466
} EndOfLineMarker;
1467

1468
static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
275,399,342✔
1469

1470
        if (!FLAGS_SET(flags, READ_LINE_ONLY_NUL)) {
275,399,342✔
1471
                if (c == '\n')
274,281,700✔
1472
                        return EOL_TEN;
1473
                if (c == '\r')
264,938,576✔
1474
                        return EOL_THIRTEEN;
1475
        }
1476

1477
        if (c == '\0')
266,056,202✔
1478
                return EOL_ZERO;
35,166✔
1479

1480
        return EOL_NONE;
1481
}
1482

1483
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, funlockfile, NULL);
8,836,597✔
1484

1485
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
8,836,597✔
1486
        _cleanup_free_ char *buffer = NULL;
8,836,597✔
1487
        size_t n = 0, count = 0;
8,836,597✔
1488
        int r;
8,836,597✔
1489

1490
        assert(f);
8,836,597✔
1491

1492
        /* Something like a bounded version of getline().
1493
         *
1494
         * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
1495
         * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
1496
         * endings:
1497
         *
1498
         *     • \n        (UNIX)
1499
         *     • \r        (old MacOS)
1500
         *     • \0        (C strings)
1501
         *     • \n\0
1502
         *     • \r\0
1503
         *     • \r\n      (Windows)
1504
         *     • \n\r
1505
         *     • \r\n\0
1506
         *     • \n\r\0
1507
         *
1508
         * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1509
         * the number of characters in the returned string). When EOF is hit, 0 is returned.
1510
         *
1511
         * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1512
         * delimiters. If the limit is hit we fail and return -ENOBUFS.
1513
         *
1514
         * If a line shall be skipped ret may be initialized as NULL. */
1515

1516
        if (ret) {
8,836,597✔
1517
                if (!GREEDY_REALLOC(buffer, 1))
8,836,566✔
1518
                        return -ENOMEM;
1519
        }
1520

1521
        {
1522
                _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
8,836,597✔
1523
                EndOfLineMarker previous_eol = EOL_NONE;
8,836,597✔
1524
                flockfile(f);
8,836,597✔
1525

1526
                for (;;) {
276,064,537✔
1527
                        EndOfLineMarker eol;
276,064,537✔
1528
                        char c;
276,064,537✔
1529

1530
                        if (n >= limit)
276,064,537✔
1531
                                return -ENOBUFS;
9✔
1532

1533
                        if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
276,064,531✔
1534
                                return -ENOBUFS;
1535

1536
                        r = safe_fgetc(f, &c);
276,064,531✔
1537
                        if (r < 0)
276,064,531✔
1538
                                return r;
1539
                        if (r == 0) /* EOF is definitely EOL */
276,064,528✔
1540
                                break;
1541

1542
                        eol = categorize_eol(c, flags);
275,399,342✔
1543

1544
                        if (FLAGS_SET(previous_eol, EOL_ZERO) ||
275,399,342✔
1545
                            (eol == EOL_NONE && previous_eol != EOL_NONE) ||
275,368,508✔
1546
                            (eol != EOL_NONE && (previous_eol & eol) != 0)) {
9,378,300✔
1547
                                /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
1548
                                 * EOL marker has been seen right before?  In either of these three cases we are
1549
                                 * done. But first, let's put this character back in the queue. (Note that we have to
1550
                                 * cast this to (unsigned char) here as ungetc() expects a positive 'int', and if we
1551
                                 * are on an architecture where 'char' equals 'signed char' we need to ensure we don't
1552
                                 * pass a negative value here. That said, to complicate things further ungetc() is
1553
                                 * actually happy with most negative characters and implicitly casts them back to
1554
                                 * positive ones as needed, except for \xff (aka -1, aka EOF), which it refuses. What a
1555
                                 * godawful API!) */
1556
                                assert_se(ungetc((unsigned char) c, f) != EOF);
8,171,402✔
1557
                                break;
1558
                        }
1559

1560
                        count++;
267,227,940✔
1561

1562
                        if (eol != EOL_NONE) {
258,576,154✔
1563
                                /* If we are on a tty, we can't shouldn't wait for more input, because that
1564
                                 * generally means waiting for the user, interactively. In the case of a TTY
1565
                                 * we expect only \n as the single EOL marker, so we are in the lucky
1566
                                 * position that there is no need to wait. We check this condition last, to
1567
                                 * avoid isatty() check if not necessary. */
1568

1569
                                if ((flags & (READ_LINE_IS_A_TTY|READ_LINE_NOT_A_TTY)) == 0) {
8,651,786✔
1570
                                        int fd;
7,901,116✔
1571

1572
                                        fd = fileno(f);
7,901,116✔
1573
                                        if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
7,901,116✔
1574
                                                     * and don't call isatty() on an invalid fd */
1575
                                                flags |= READ_LINE_NOT_A_TTY;
28✔
1576
                                        else
1577
                                                flags |= isatty_safe(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
15,802,176✔
1578
                                }
1579
                                if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
8,651,786✔
1580
                                        break;
1581
                        }
1582

1583
                        if (eol != EOL_NONE) {
267,227,940✔
1584
                                previous_eol |= eol;
8,651,786✔
1585
                                continue;
8,651,786✔
1586
                        }
1587

1588
                        if (ret) {
258,576,154✔
1589
                                if (!GREEDY_REALLOC(buffer, n + 2))
258,576,054✔
1590
                                        return -ENOMEM;
1591

1592
                                buffer[n] = c;
258,576,054✔
1593
                        }
1594

1595
                        n++;
258,576,154✔
1596
                }
1597
        }
1598

1599
        if (ret) {
8,836,588✔
1600
                buffer[n] = 0;
8,836,557✔
1601

1602
                *ret = TAKE_PTR(buffer);
8,836,557✔
1603
        }
1604

1605
        return (int) count;
8,836,588✔
1606
}
1607

1608
int read_stripped_line(FILE *f, size_t limit, char **ret) {
4,109,685✔
1609
        _cleanup_free_ char *s = NULL;
4,109,685✔
1610
        int r, k;
4,109,685✔
1611

1612
        assert(f);
4,109,685✔
1613

1614
        r = read_line(f, limit, ret ? &s : NULL);
4,109,685✔
1615
        if (r < 0)
4,109,685✔
1616
                return r;
1617

1618
        if (ret) {
4,109,685✔
1619
                const char *p = strstrip(s);
4,109,685✔
1620
                if (p == s)
4,109,685✔
1621
                        *ret = TAKE_PTR(s);
4,109,682✔
1622
                else {
1623
                        k = strdup_to(ret, p);
3✔
1624
                        if (k < 0)
3✔
1625
                                return k;
1626
                }
1627
        }
1628

1629
        return r > 0;          /* Return 1 if something was read. */
4,109,685✔
1630
}
1631

1632
int safe_fgetc(FILE *f, char *ret) {
276,071,865✔
1633
        int k;
276,071,865✔
1634

1635
        assert(f);
276,071,865✔
1636

1637
        /* A safer version of plain fgetc(): let's propagate the error that happened while reading as such, and
1638
         * separate the EOF condition from the byte read, to avoid those confusion signed/unsigned issues fgetc()
1639
         * has. */
1640

1641
        errno = 0;
276,071,865✔
1642
        k = fgetc(f);
276,071,865✔
1643
        if (k == EOF) {
276,071,865✔
1644
                if (ferror(f))
665,289✔
1645
                        return errno_or_else(EIO);
3✔
1646

1647
                if (ret)
665,286✔
1648
                        *ret = 0;
665,202✔
1649

1650
                return 0;
1651
        }
1652

1653
        if (ret)
275,406,576✔
1654
                *ret = k;
275,405,963✔
1655

1656
        return 1;
1657
}
1658

1659
int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line) {
164✔
1660
        struct stat _st;
164✔
1661

1662
        if (!filename)
164✔
1663
                return 0;
164✔
1664

1665
        if (!st) {
164✔
1666
                if (stat(filename, &_st) < 0)
22✔
UNCOV
1667
                        return -errno;
×
1668
                st = &_st;
1669
        }
1670

1671
        if ((st->st_mode & S_IRWXO) == 0)
164✔
1672
                return 0;
1673

1674
        if (unit)
40✔
UNCOV
1675
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
1676
                           "%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
1677
                           filename, st->st_mode & 07777);
1678
        else
1679
                log_warning("%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
40✔
1680
                            filename, st->st_mode & 07777);
1681
        return 0;
1682
}
1683

1684
int write_data_file_atomic_at(
13✔
1685
                int dir_fd,
1686
                const char *path,
1687
                const struct iovec *iovec,
1688
                WriteDataFileFlags flags) {
1689

1690
        int r;
13✔
1691

1692
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
13✔
1693

1694
        /* This is a cousin of write_string_file_atomic(), but operates with arbitrary struct iovec binary
1695
         * data (rather than strings), works without FILE* streams, and does direct syscalls instead. */
1696

1697
        _cleanup_free_ char *dn = NULL, *fn = NULL;
13✔
1698
        r = path_split_prefix_filename(path, &dn, &fn);
13✔
1699
        if (IN_SET(r, -EADDRNOTAVAIL, O_DIRECTORY))
13✔
1700
                return -EISDIR; /* path refers to "." or "/" (which are dirs, which we cannot write), or is suffixed with "/" */
1701
        if (r < 0)
10✔
1702
                return r;
1703

1704
        _cleanup_close_ int mfd = -EBADF;
13✔
1705
        if (dn) {
8✔
1706
                /* If there's a directory component, readjust our position */
1707
                r = chaseat(XAT_FDROOT,
14✔
1708
                            dir_fd,
1709
                            dn,
1710
                            FLAGS_SET(flags, WRITE_DATA_FILE_MKDIR_0755) ? CHASE_MKDIR_0755 : 0,
7✔
1711
                            /* ret_path= */ NULL,
1712
                            &mfd);
1713
                if (r < 0)
7✔
1714
                        return r;
1715

1716
                dir_fd = mfd;
6✔
1717
        }
1718

1719
        _cleanup_free_ char *t = NULL;
7✔
1720
        _cleanup_close_ int fd = open_tmpfile_linkable_at(dir_fd, fn, O_WRONLY|O_CLOEXEC, &t);
14✔
1721
        if (fd < 0)
7✔
1722
                return fd;
1723

1724
        CLEANUP_TMPFILE_AT(dir_fd, t);
7✔
1725

1726
        if (iovec_is_set(iovec)) {
7✔
1727
                r = loop_write(fd, iovec->iov_base, iovec->iov_len);
7✔
1728
                if (r < 0)
7✔
1729
                        return r;
1730
        }
1731

1732
        r = fchmod_umask(fd, FLAGS_SET(flags, WRITE_DATA_FILE_MODE_0400) ? 0400 : 0644);
14✔
1733
        if (r < 0)
7✔
1734
                return r;
1735

1736
        r = link_tmpfile_at(fd, dir_fd, t, fn, LINK_TMPFILE_REPLACE);
7✔
1737
        if (r < 0)
7✔
1738
                return r;
1739

1740
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT */
6✔
1741
        return 0;
6✔
1742
}
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