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

systemd / systemd / 28272947092

26 Jun 2026 08:38PM UTC coverage: 72.893% (+0.2%) from 72.703%
28272947092

push

github

poettering
sysupdate: Address review feedback on CheckNew varlink scaffolding

Follow-up to #42422:

 - Rename process_image() to context_process_image(), since it now
   operates on a Context object.
 - Use IN_SET() in image_type_can_sysupdate() instead of a switch.
 - Name the return parameters of context_list_components() ret_xyz, per
   our coding style.
 - Drop a redundant "else" after a return in vl_method_check_new().

9 of 11 new or added lines in 1 file covered. (81.82%)

12567 existing lines in 144 files now uncovered.

341026 of 467845 relevant lines covered (72.89%)

1339355.33 hits per line

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

92.36
/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-util.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) {
466,597✔
52
        assert(ret);
466,597✔
53

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

58
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
466,597✔
59

60
        *ret = f;
466,597✔
61
        return 0;
466,597✔
62
}
63

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

67
        assert(fd);
466,597✔
68

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

73
        *fd = -EBADF;
466,597✔
74

75
        return 0;
466,597✔
76
}
77

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

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

85
        *fd = -EBADF;
42,069✔
86

87
        return f;
42,069✔
88
}
89

90
DIR* take_fdopendir(int *dfd) {
230,912✔
91
        assert(dfd);
230,912✔
92

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

97
        *dfd = -EBADF;
230,912✔
98

99
        return d;
230,912✔
100
}
101

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

107
        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
986,170✔
108

109
        return f;
986,170✔
110
}
111

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

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

119
        return f;
106✔
120
}
121

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

128
        bool needs_nl;
306,442✔
129
        int r, fd = -EBADF;
306,442✔
130

131
        assert(f);
306,442✔
132
        assert(line);
306,442✔
133

134
        if (ferror(f))
306,442✔
135
                return -EIO;
136

137
        if (ts) {
306,442✔
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) {
306,442✔
146
                _cleanup_free_ char *t = NULL;
9,153✔
147

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

150
                if (fd < 0) {
9,153✔
151
                        fd = fileno(f);
9,153✔
152
                        if (fd < 0)
9,153✔
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 &&
18,113✔
160
                    streq_skip_trailing_chars(line, t, NEWLINE)) {
8,960✔
161
                        log_debug("No change in value '%s', suppressing write", line);
6,713✔
162
                        return 0;
163
                }
164

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

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

171
        if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
113,315✔
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");
559,070✔
176
                needs_nl = false;
111,814✔
177
        }
178

179
        if (fputs(line, f) == EOF)
299,729✔
180
                return -errno;
9✔
181

182
        if (needs_nl)
299,720✔
183
                if (fputc('\n', f) == EOF)
1,500✔
184
                        return -errno;
×
185

186
        if (flags & WRITE_STRING_FILE_SYNC)
299,720✔
187
                r = fflush_sync_and_check(f);
2,140✔
188
        else
189
                r = fflush_and_check(f);
297,580✔
190
        if (r < 0)
299,720✔
191
                return r;
192

193
        if (ts) {
299,688✔
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) {
167,919✔
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 :
167,919✔
214
                FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0444) ? 0444 : 0644;
167,660✔
215
}
216

217
static int write_string_file_atomic_at(
3,147✔
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,147✔
225
        _cleanup_free_ char *p = NULL;
3,147✔
226
        int r;
3,147✔
227

228
        assert(fn);
3,147✔
229
        assert(line);
3,147✔
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,147✔
235

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

242
                call_label_ops_post = true;
243
        }
244

245
        r = fopen_temporary_at(dir_fd, fn, &f, &p);
3,147✔
246
        int k = call_label_ops_post ? label_ops_post(f ? fileno(f) : dir_fd, f ? NULL : fn, /* created= */ !!f) : 0;
3,357✔
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,147✔
250
                return r;
251
        CLEANUP_TMPFILE_AT(dir_fd, p);
3,146✔
252
        if (k < 0)
3,146✔
253
                return k;
254

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

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

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

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

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

276
        return 0;
277
}
278

279
int write_string_file_full(
245,813✔
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;
245,813✔
288
        _cleanup_fclose_ FILE *f = NULL;
245,813✔
289
        _cleanup_close_ int fd = -EBADF;
245,813✔
290
        int r;
245,813✔
291

292
        assert(dir_fd == AT_FDCWD || dir_fd >= 0);
245,813✔
293
        assert(line);
245,813✔
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)));
245,813✔
297

298
        if (flags & WRITE_STRING_FILE_MKDIR_0755) {
245,813✔
299
                assert(fn);
563✔
300

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

306
        if (flags & WRITE_STRING_FILE_ATOMIC) {
245,813✔
307
                assert(fn);
3,147✔
308
                assert(flags & WRITE_STRING_FILE_CREATE);
3,147✔
309

310
                r = write_string_file_atomic_at(dir_fd, fn, line, flags, ts);
3,147✔
311
                if (r < 0)
3,147✔
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))
242,666✔
319
                r = fd = fd_reopen(
77,894✔
320
                                ASSERT_FD(dir_fd), O_CLOEXEC | O_NOCTTY |
77,894✔
321
                                (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
155,788✔
322
                                (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
77,894✔
323
                                (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0));
77,894✔
324
        else {
325
                mode_t mode = write_string_file_flags_to_mode(flags);
164,772✔
326
                bool call_label_ops_post = false;
164,772✔
327

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

333
                        call_label_ops_post = true;
334
                }
335

336
                r = fd = openat_report_new(
494,316✔
337
                                dir_fd, fn, O_CLOEXEC | O_NOCTTY |
164,772✔
338
                                (FLAGS_SET(flags, WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0) |
164,772✔
339
                                (FLAGS_SET(flags, WRITE_STRING_FILE_CREATE) ? O_CREAT : 0) |
164,772✔
340
                                (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
329,544✔
341
                                (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
164,772✔
342
                                (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0),
164,772✔
343
                                mode,
344
                                &made_file);
345
                if (call_label_ops_post)
164,772✔
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,556✔
349
        }
350
        if (r < 0)
242,666✔
351
                goto fail;
1,258✔
352

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

357
        if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
241,408✔
358
                setvbuf(f, NULL, _IONBF, 0);
239,533✔
359

360
        r = write_string_stream_full(f, line, flags, ts);
241,408✔
361
        if (r < 0)
241,408✔
362
                goto fail;
14✔
363

364
        return 0;
365

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

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

373
        f = safe_fclose(f);
1,124✔
374
        fd = safe_close(fd);
1,124✔
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,126✔
379
                return 0;
463✔
380

381
        return r;
382
}
383

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

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

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

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

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

UNCOV
403
int write_string_filef_at(
×
404
                int dir_fd,
405
                const char *fn,
406
                WriteStringFileFlags flags,
407
                const char *format, ...) {
408

UNCOV
409
        _cleanup_free_ char *p = NULL;
×
UNCOV
410
        va_list ap;
×
UNCOV
411
        int r;
×
412

UNCOV
413
        va_start(ap, format);
×
414
        r = vasprintf(&p, format, ap);
×
UNCOV
415
        va_end(ap);
×
416

UNCOV
417
        if (r < 0)
×
418
                return -ENOMEM;
419

UNCOV
420
        return write_string_file_at(dir_fd, fn, p, flags);
×
421
}
422

423
int write_base64_file_at(
15✔
424
                int dir_fd,
425
                const char *fn,
426
                const struct iovec *data,
427
                WriteStringFileFlags flags) {
428

429
        _cleanup_free_ char *encoded = NULL;
15✔
430
        ssize_t n;
15✔
431

432
        n = base64mem_full(data ? data->iov_base : NULL, data ? data->iov_len : 0, 79, &encoded);
15✔
433
        if (n < 0)
15✔
UNCOV
434
                return n;
×
435

436
        return write_string_file_at(dir_fd, fn, encoded, flags);
15✔
437
}
438

439
int read_one_line_file_at(int dir_fd, const char *filename, char **ret) {
329,209✔
440
        _cleanup_fclose_ FILE *f = NULL;
329,209✔
441
        int r;
329,209✔
442

443
        assert(wildcard_fd_is_valid(dir_fd));
329,209✔
444
        assert(filename);
329,209✔
445
        assert(ret);
329,209✔
446

447
        r = fopen_unlocked_at(dir_fd, filename, "re", /* open_flags= */ 0, &f);
329,209✔
448
        if (r < 0)
329,209✔
449
                return r;
450

451
        return read_line(f, LONG_LINE_MAX, ret);
329,209✔
452
}
453

454
int read_boolean_file_at(int dir_fd, const char *filename) {
1,016✔
455
        _cleanup_free_ char *s = NULL;
1,016✔
456
        int r;
1,016✔
457

458
        assert(wildcard_fd_is_valid(dir_fd));
1,016✔
459
        assert(filename);
1,016✔
460

461
        r = read_one_line_file_at(dir_fd, filename, &s);
1,016✔
462
        if (r < 0)
1,016✔
463
                return r;
464

465
        return parse_boolean(s);
617✔
466
}
467

468
int verify_file_at(int dir_fd, const char *fn, const char *blob, bool accept_extra_nl) {
1,124✔
469
        _cleanup_fclose_ FILE *f = NULL;
1,124✔
470
        _cleanup_free_ char *buf = NULL;
1,124✔
471
        size_t l, k;
1,124✔
472
        int r;
1,124✔
473

474
        assert(blob);
1,124✔
475

476
        l = strlen(blob);
1,124✔
477

478
        if (accept_extra_nl && endswith(blob, "\n"))
1,124✔
479
                accept_extra_nl = false;
1✔
480

481
        buf = malloc(l + accept_extra_nl + 1);
1,124✔
482
        if (!buf)
1,124✔
483
                return -ENOMEM;
484

485
        r = fopen_unlocked_at(dir_fd, strempty(fn), "re", 0, &f);
1,124✔
486
        if (r < 0)
1,124✔
487
                return r;
488

489
        /* We try to read one byte more than we need, so that we know whether we hit eof */
490
        errno = 0;
868✔
491
        k = fread(buf, 1, l + accept_extra_nl + 1, f);
868✔
492
        if (ferror(f))
868✔
493
                return errno_or_else(EIO);
1,124✔
494

495
        if (k != l && k != l + accept_extra_nl)
868✔
496
                return 0;
497
        if (memcmp(buf, blob, l) != 0)
738✔
498
                return 0;
499
        if (k > l && buf[l] != '\n')
463✔
UNCOV
500
                return 0;
×
501

502
        return 1;
503
}
504

505
int read_virtual_file_at(
799,180✔
506
                int dir_fd,
507
                const char *filename,
508
                size_t max_size,
509
                char **ret_contents,
510
                size_t *ret_size) {
511

512
        _cleanup_free_ char *buf = NULL;
799,180✔
513
        size_t n, size;
799,180✔
514
        int n_retries;
799,180✔
515
        bool truncated = false;
799,180✔
516

517
        /* Virtual filesystems such as sysfs or procfs use kernfs, and kernfs can work with two sorts of
518
         * virtual files. One sort uses "seq_file", and the results of the first read are buffered for the
519
         * second read. The other sort uses "raw" reads which always go direct to the device. In the latter
520
         * case, the content of the virtual file must be retrieved with a single read otherwise a second read
521
         * might get the new value instead of finding EOF immediately. That's the reason why the usage of
522
         * fread(3) is prohibited in this case as it always performs a second call to read(2) looking for
523
         * EOF. See issue #13585.
524
         *
525
         * max_size specifies a limit on the bytes read. If max_size is SIZE_MAX, the full file is read. If
526
         * the full file is too large to read, an error is returned. For other values of max_size, *partial
527
         * contents* may be returned. (Though the read is still done using one syscall.) Returns 0 on
528
         * partial success, 1 if untruncated contents were read.
529
         *
530
         * Rule: for kernfs files using "seq_file" → use regular read_full_file_at()
531
         *       for kernfs files using "raw" → use read_virtual_file_at()
532
         */
533

534
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
799,180✔
535
        assert(max_size <= READ_VIRTUAL_BYTES_MAX || max_size == SIZE_MAX);
799,180✔
536

537
        _cleanup_close_ int fd = -EBADF;
799,180✔
538
        if (isempty(filename))
799,180✔
539
                fd = fd_reopen(ASSERT_FD(dir_fd), O_RDONLY | O_NOCTTY | O_CLOEXEC);
646,612✔
540
        else
541
                fd = RET_NERRNO(openat(dir_fd, filename, O_RDONLY | O_NOCTTY | O_CLOEXEC));
152,568✔
542
        if (fd < 0)
799,180✔
543
                return fd;
544

545
        /* Limit the number of attempts to read the number of bytes returned by fstat(). */
546
        n_retries = 3;
547

548
        for (;;) {
741,091✔
549
                struct stat st;
741,090✔
550

551
                if (fstat(fd, &st) < 0)
741,090✔
552
                        return -errno;
537✔
553

554
                if (!S_ISREG(st.st_mode))
741,090✔
555
                        return -EBADF;
556

557
                /* Be prepared for files from /proc which generally report a file size of 0. */
558
                assert_cc(READ_VIRTUAL_BYTES_MAX < SSIZE_MAX);
741,090✔
559
                if (st.st_size > 0 && n_retries > 1) {
741,090✔
560
                        /* Let's use the file size if we have more than 1 attempt left. On the last attempt
561
                         * we'll ignore the file size */
562

563
                        if (st.st_size > SSIZE_MAX) { /* Avoid overflow with 32-bit size_t and 64-bit off_t. */
585,427✔
564

565
                                if (max_size == SIZE_MAX)
566
                                        return -EFBIG;
567

568
                                size = max_size;
569
                        } else {
570
                                size = MIN((size_t) st.st_size, max_size);
585,427✔
571

572
                                if (size > READ_VIRTUAL_BYTES_MAX)
585,427✔
573
                                        return -EFBIG;
574
                        }
575

576
                        n_retries--;
585,427✔
577
                } else if (n_retries > 1) {
155,663✔
578
                        /* Files in /proc are generally smaller than the page size so let's start with
579
                         * a page size buffer from malloc and only use the max buffer on the final try. */
580
                        size = MIN3(page_size() - 1, READ_VIRTUAL_BYTES_MAX, max_size);
155,662✔
581
                        n_retries = 1;
155,662✔
582
                } else {
583
                        size = MIN(READ_VIRTUAL_BYTES_MAX, max_size);
1✔
584
                        n_retries = 0;
1✔
585
                }
586

587
                buf = malloc(size + 1);
741,090✔
588
                if (!buf)
741,090✔
589
                        return -ENOMEM;
590

591
                /* Use a bigger allocation if we got it anyway, but not more than the limit. */
592
                size = MIN3(MALLOC_SIZEOF_SAFE(buf) - 1, max_size, READ_VIRTUAL_BYTES_MAX);
741,090✔
593

594
                for (;;) {
741,090✔
595
                        ssize_t k;
741,090✔
596

597
                        /* Read one more byte so we can detect whether the content of the
598
                         * file has already changed or the guessed size for files from /proc
599
                         * wasn't large enough . */
600
                        k = read(fd, buf, size + 1);
741,090✔
601
                        if (k >= 0) {
741,090✔
602
                                n = k;
740,553✔
603
                                break;
740,553✔
604
                        }
605

606
                        if (errno != EINTR)
537✔
607
                                return -errno;
537✔
608
                }
609

610
                /* Consider a short read as EOF */
611
                if (n <= size)
740,553✔
612
                        break;
613

614
                /* If a maximum size is specified and we already read more we know the file is larger, and
615
                 * can handle this as truncation case. Note that if the size of what we read equals the
616
                 * maximum size then this doesn't mean truncation, the file might or might not end on that
617
                 * byte. We need to rerun the loop in that case, with a larger buffer size, so that we read
618
                 * at least one more byte to be able to distinguish EOF from truncation. */
619
                if (max_size != SIZE_MAX && n > max_size) {
39,734✔
620
                        n = size; /* Make sure we never use more than what we sized the buffer for (so that
621
                                   * we have one free byte in it for the trailing NUL we add below). */
622
                        truncated = true;
623
                        break;
624
                }
625

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

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

634
                if (lseek(fd, 0, SEEK_SET) < 0)
1✔
UNCOV
635
                        return -errno;
×
636

637
                buf = mfree(buf);
1✔
638
        }
639

640
        if (ret_contents) {
740,552✔
641

642
                /* Safety check: if the caller doesn't want to know the size of what we just read it will
643
                 * rely on the trailing NUL byte. But if there's an embedded NUL byte, then we should refuse
644
                 * operation as otherwise there'd be ambiguity about what we just read. */
645
                if (!ret_size && memchr(buf, 0, n))
701,066✔
646
                        return -EBADMSG;
647

648
                if (n < size) {
701,066✔
649
                        char *p;
692,409✔
650

651
                        /* Return rest of the buffer to libc */
652
                        p = realloc(buf, n + 1);
692,409✔
653
                        if (!p)
692,409✔
654
                                return -ENOMEM;
655
                        buf = p;
692,409✔
656
                }
657

658
                buf[n] = 0;
701,066✔
659
                *ret_contents = TAKE_PTR(buf);
701,066✔
660
        }
661

662
        if (ret_size)
740,552✔
663
                *ret_size = n;
603,582✔
664

665
        return !truncated;
740,552✔
666
}
667

668
int read_full_stream_full(
544,406✔
669
                FILE *f,
670
                const char *filename,
671
                uint64_t offset,
672
                size_t size,
673
                ReadFullFileFlags flags,
674
                char **ret_contents,
675
                size_t *ret_size) {
676

677
        _cleanup_free_ char *buf = NULL;
544,406✔
678
        size_t n, n_next = 0, l, expected_decoded_size = size;
544,406✔
679
        int fd, r;
544,406✔
680

681
        assert(f);
544,406✔
682
        assert(ret_contents);
544,406✔
683
        assert(!FLAGS_SET(flags, READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX));
544,406✔
684
        assert(size != SIZE_MAX || !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER));
544,406✔
685

686
        if (offset != UINT64_MAX && offset > LONG_MAX) /* fseek() can only deal with "long" offsets */
544,406✔
687
                return -ERANGE;
688

689
        if ((flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) != 0) {
544,406✔
690
                if (size <= SIZE_MAX / READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY)
73✔
691
                        size *= READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY;
71✔
692
                else
693
                        size = SIZE_MAX;
694
        }
695

696
        fd = fileno(f);
544,406✔
697
        if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see
544,406✔
698
                        * fmemopen()), let's optimize our buffering */
699
                struct stat st;
544,313✔
700

701
                if (fstat(fd, &st) < 0)
544,313✔
UNCOV
702
                        return -errno;
×
703

704
                if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR)) {
544,313✔
705
                        r = stat_verify_regular(&st);
6✔
706
                        if (r < 0)
6✔
707
                                return r;
708
                }
709

710
                if (S_ISREG(st.st_mode)) {
544,313✔
711

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

718
                        if (st.st_size > 0 &&
543,106✔
719
                            (size == SIZE_MAX || FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER))) {
1,456✔
720

721
                                uint64_t rsize =
1,457,877✔
722
                                        LESS_BY((uint64_t) st.st_size, offset == UINT64_MAX ? 0 : offset);
485,959✔
723

724
                                if (rsize < SIZE_MAX) /* overflow check */
485,959✔
725
                                        n_next = rsize + 1;
485,959✔
726
                        }
727

728
                        if (flags & READ_FULL_FILE_WARN_WORLD_READABLE)
543,106✔
729
                                (void) warn_file_is_world_accessible(filename, &st, NULL, 0);
145✔
730
                }
731
        } else if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR))
93✔
732
                return -EBADFD;
733

734
        /* If we don't know how much to read, figure it out now. If we shall read a part of the file, then
735
         * allocate the requested size. If we shall load the full file start with LINE_MAX. Note that if
736
         * READ_FULL_FILE_FAIL_WHEN_LARGER we consider the specified size a safety limit, and thus also start
737
         * with LINE_MAX, under assumption the file is most likely much shorter. */
738
        if (n_next == 0)
544,313✔
739
                n_next = size != SIZE_MAX && !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) ? size : LINE_MAX;
58,447✔
740

741
        /* Never read more than we need to determine that our own limit is hit */
742
        if (n_next > READ_FULL_BYTES_MAX)
486,193✔
UNCOV
743
                n_next = READ_FULL_BYTES_MAX + 1;
×
744

745
        if (offset != UINT64_MAX && fseek(f, offset, SEEK_SET) < 0)
544,406✔
UNCOV
746
                return -errno;
×
747

748
        n = l = 0;
544,406✔
749
        for (;;) {
545,886✔
750
                char *t;
545,146✔
751
                size_t k;
545,146✔
752

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

758
                if (flags & READ_FULL_FILE_SECURE) {
545,146✔
759
                        t = malloc(n_next + 1);
1,747✔
760
                        if (!t) {
1,747✔
UNCOV
761
                                r = -ENOMEM;
×
UNCOV
762
                                goto finalize;
×
763
                        }
764
                        memcpy_safe(t, buf, n);
1,747✔
765
                        explicit_bzero_safe(buf, n);
1,747✔
766
                        free(buf);
1,747✔
767
                } else {
768
                        t = realloc(buf, n_next + 1);
543,399✔
769
                        if (!t)
543,399✔
770
                                return -ENOMEM;
771
                }
772

773
                buf = t;
545,146✔
774
                /* Unless a size has been explicitly specified, try to read as much as fits into the memory
775
                 * we allocated (minus 1, to leave one byte for the safety NUL byte) */
776
                n = size == SIZE_MAX ? MALLOC_SIZEOF_SAFE(buf) - 1 : n_next;
545,146✔
777

778
                errno = 0;
545,146✔
779
                k = fread(buf + l, 1, n - l, f);
545,146✔
780

781
                assert(k <= n - l);
545,146✔
782
                l += k;
545,146✔
783

784
                if (ferror(f)) {
545,146✔
785
                        r = errno_or_else(EIO);
2✔
786
                        goto finalize;
2✔
787
                }
788
                if (feof(f))
545,144✔
789
                        break;
790

791
                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 */
855✔
792
                        assert(l == size);
109✔
793
                        break;
794
                }
795

796
                assert(k > 0); /* we can't have read zero bytes because that would have been EOF */
746✔
797

798
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > size) {
746✔
799
                        r = -E2BIG;
6✔
800
                        goto finalize;
6✔
801
                }
802

803
                if (n >= READ_FULL_BYTES_MAX) {
740✔
UNCOV
804
                        r = -E2BIG;
×
UNCOV
805
                        goto finalize;
×
806
                }
807

808
                n_next = MIN(n * 2, READ_FULL_BYTES_MAX);
740✔
809
        }
810

811
        if (flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) {
544,398✔
812
                _cleanup_free_ void *decoded = NULL;
71✔
813
                size_t decoded_size;
71✔
814

815
                buf[l++] = 0;
71✔
816
                if (flags & READ_FULL_FILE_UNBASE64)
71✔
817
                        r = unbase64mem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
69✔
818
                else
819
                        r = unhexmem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
2✔
820
                if (r < 0)
71✔
UNCOV
821
                        goto finalize;
×
822

823
                if (flags & READ_FULL_FILE_SECURE)
71✔
824
                        explicit_bzero_safe(buf, n);
19✔
825
                free_and_replace(buf, decoded);
71✔
826
                n = l = decoded_size;
71✔
827

828
                if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > expected_decoded_size) {
71✔
UNCOV
829
                        r = -E2BIG;
×
UNCOV
830
                        goto finalize;
×
831
                }
832
        }
833

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

839
                if (memchr(buf, 0, l)) {
373,422✔
840
                        r = -EBADMSG;
2✔
841
                        goto finalize;
2✔
842
                }
843
        }
844

845
        buf[l] = 0;
544,396✔
846
        *ret_contents = TAKE_PTR(buf);
544,396✔
847

848
        if (ret_size)
544,396✔
849
                *ret_size = l;
170,976✔
850

851
        return 0;
852

853
finalize:
10✔
854
        if (flags & READ_FULL_FILE_SECURE)
10✔
855
                explicit_bzero_safe(buf, n);
3✔
856

857
        return r;
858
}
859

860
int read_full_file_full(
716,627✔
861
                int dir_fd,
862
                const char *filename,
863
                uint64_t offset,
864
                size_t size,
865
                ReadFullFileFlags flags,
866
                const char *bind_name,
867
                char **ret_contents,
868
                size_t *ret_size) {
869

870
        _cleanup_fclose_ FILE *f = NULL;
716,627✔
871
        XfopenFlags xflags = XFOPEN_UNLOCKED;
716,627✔
872
        int r;
716,627✔
873

874
        assert(ret_contents);
716,627✔
875

876
        if (FLAGS_SET(flags, READ_FULL_FILE_CONNECT_SOCKET) && /* If this is enabled, let's try to connect to it */
716,627✔
877
            offset == UINT64_MAX)                              /* Seeking is not supported on AF_UNIX sockets */
878
                xflags |= XFOPEN_SOCKET;
203✔
879

880
        r = xfopenat_full(dir_fd, filename, "re", 0, xflags, bind_name, &f);
716,627✔
881
        if (r < 0)
716,627✔
882
                return r;
883

884
        return read_full_stream_full(f, filename, offset, size, flags, ret_contents, ret_size);
520,811✔
885
}
886

887
int script_get_shebang_interpreter(const char *path, char **ret) {
8✔
888
        _cleanup_fclose_ FILE *f = NULL;
8✔
889
        int r;
8✔
890

891
        assert(path);
8✔
892

893
        f = fopen(path, "re");
8✔
894
        if (!f)
8✔
895
                return -errno;
1✔
896

897
        char c;
7✔
898
        r = safe_fgetc(f, &c);
7✔
899
        if (r < 0)
7✔
900
                return r;
901
        if (r == 0)
7✔
902
                return -EBADMSG;
903
        if (c != '#')
7✔
904
                return -EMEDIUMTYPE;
905
        r = safe_fgetc(f, &c);
2✔
906
        if (r < 0)
2✔
907
                return r;
908
        if (r == 0)
2✔
909
                return -EBADMSG;
910
        if (c != '!')
2✔
911
                return -EMEDIUMTYPE;
912

913
        _cleanup_free_ char *line = NULL;
2✔
914
        r = read_line(f, LONG_LINE_MAX, &line);
2✔
915
        if (r < 0)
2✔
916
                return r;
917

918
        _cleanup_free_ char *p = NULL;
2✔
919
        const char *s = line;
2✔
920

921
        r = extract_first_word(&s, &p, /* separators= */ NULL, /* flags= */ 0);
2✔
922
        if (r < 0)
2✔
923
                return r;
924
        if (r == 0)
2✔
925
                return -ENOEXEC;
926

927
        if (ret)
2✔
928
                *ret = TAKE_PTR(p);
2✔
929
        return 0;
930
}
931

932
int get_proc_field(const char *path, const char *key, char **ret) {
47,633✔
933
        _cleanup_fclose_ FILE *f = NULL;
47,633✔
934
        int r;
47,633✔
935

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

941
        assert(path);
47,633✔
942
        assert(key);
47,633✔
943

944
        r = fopen_unlocked(path, "re", &f);
47,633✔
945
        if (r == -ENOENT && proc_mounted() == 0)
47,633✔
946
                return -ENOSYS;
947
        if (r < 0)
47,608✔
948
                return r;
949

950
        for (;;) {
277,208✔
951
                 _cleanup_free_ char *line = NULL;
159,445✔
952

953
                 r = read_line(f, LONG_LINE_MAX, &line);
159,445✔
954
                 if (r < 0)
159,445✔
955
                         return r;
956
                 if (r == 0)
159,445✔
957
                         return -ENODATA;
958

959
                 char *l = startswith(line, key);
159,445✔
960
                 if (l && *l == ':') {
159,445✔
961
                         if (ret) {
41,682✔
962
                                 char *s = strdupcspn(skip_leading_chars(l + 1, " \t"), WHITESPACE);
41,682✔
963
                                 if (!s)
41,682✔
964
                                         return -ENOMEM;
965

966
                                 *ret = s;
41,682✔
967
                         }
968

969
                         return 0;
970
                 }
971
        }
972
}
973

974
DIR* xopendirat(int dir_fd, const char *path, int flags) {
191,105✔
975
        _cleanup_close_ int fd = -EBADF;
191,105✔
976

977
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
191,105✔
978
        assert(!(flags & (O_CREAT|O_TMPFILE)));
191,105✔
979

980
        if ((dir_fd == AT_FDCWD || path_is_absolute(path)) &&
195,474✔
981
            (flags &~ O_DIRECTORY) == 0)
1,505✔
UNCOV
982
                return opendir(path);
×
983

984
        if (isempty(path)) {
191,105✔
985
                path = ".";
185,231✔
986
                flags |= O_NOFOLLOW;
185,231✔
987
        }
988

989
        fd = openat(dir_fd, path, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
191,105✔
990
        if (fd < 0)
191,105✔
991
                return NULL;
992

993
        return take_fdopendir(&fd);
190,566✔
994
}
995

996
int fopen_mode_to_flags(const char *mode) {
100,564✔
997
        const char *p;
100,564✔
998
        int flags;
100,564✔
999

1000
        assert(mode);
100,564✔
1001

1002
        if ((p = startswith(mode, "r+")))
100,564✔
1003
                flags = O_RDWR;
100,564✔
1004
        else if ((p = startswith(mode, "r")))
100,563✔
1005
                flags = O_RDONLY;
1006
        else if ((p = startswith(mode, "w+")))
×
1007
                flags = O_RDWR|O_CREAT|O_TRUNC;
UNCOV
1008
        else if ((p = startswith(mode, "w")))
×
1009
                flags = O_WRONLY|O_CREAT|O_TRUNC;
UNCOV
1010
        else if ((p = startswith(mode, "a+")))
×
1011
                flags = O_RDWR|O_CREAT|O_APPEND;
UNCOV
1012
        else if ((p = startswith(mode, "a")))
×
1013
                flags = O_WRONLY|O_CREAT|O_APPEND;
1014
        else
1015
                return -EINVAL;
1016

1017
        for (; *p != 0; p++) {
196,622✔
1018

1019
                switch (*p) {
96,058✔
1020

1021
                case 'e':
96,058✔
1022
                        flags |= O_CLOEXEC;
96,058✔
1023
                        break;
96,058✔
1024

UNCOV
1025
                case 'x':
×
UNCOV
1026
                        flags |= O_EXCL;
×
UNCOV
1027
                        break;
×
1028

1029
                case 'm':
1030
                        /* ignore this here, fdopen() might care later though */
1031
                        break;
1032

1033
                case 'c': /* not sure what to do about this one */
1034
                default:
1035
                        return -EINVAL;
1036
                }
1037
        }
1038

1039
        return flags;
1040
}
1041

1042
static int xfopenat_regular(int dir_fd, const char *path, const char *mode, int open_flags, FILE **ret) {
1,213,353✔
1043
        FILE *f;
1,213,353✔
1044

1045
        /* A combination of fopen() with openat() */
1046

1047
        assert(wildcard_fd_is_valid(dir_fd));
1,213,353✔
1048
        assert(mode);
1,213,353✔
1049
        assert(ret);
1,213,353✔
1050

1051
        if (dir_fd == AT_FDCWD && path && open_flags == 0)
1,213,353✔
1052
                f = fopen(path, mode);
1,211,685✔
1053
        else if (dir_fd == XAT_FDROOT && path && open_flags == 0) {
1,668✔
1054
                _cleanup_free_ char *j = strjoin("/", path);
18✔
1055
                if (!j)
9✔
UNCOV
1056
                        return -ENOMEM;
×
1057

1058
                f = fopen(j, mode);
9✔
1059
        } else {
1060
                _cleanup_close_ int fd = -EBADF;
1,659✔
1061
                int mode_flags;
1,659✔
1062

1063
                mode_flags = fopen_mode_to_flags(mode);
1,659✔
1064
                if (mode_flags < 0)
1,659✔
1065
                        return mode_flags;
1066

1067
                if (path) {
1,659✔
1068
                        fd = openat(dir_fd, path, mode_flags | open_flags);
1,639✔
1069
                        if (fd < 0)
1,639✔
1070
                                return -errno;
767✔
1071
                } else {
1072
                        if (dir_fd == AT_FDCWD)
20✔
1073
                                return -EBADF;
1074

1075
                        fd = fd_reopen(dir_fd, (mode_flags | open_flags) & ~O_NOFOLLOW);
20✔
1076
                        if (fd < 0)
20✔
1077
                                return fd;
1078
                }
1079

1080
                f = take_fdopen(&fd, mode);
892✔
1081
        }
1082
        if (!f)
1,212,586✔
1083
                return -errno;
238,955✔
1084

1085
        *ret = f;
973,631✔
1086
        return 0;
973,631✔
1087
}
1088

1089
static int xfopenat_unix_socket(int dir_fd, const char *path, const char *bind_name, FILE **ret) {
2✔
1090
        _cleanup_close_ int sk = -EBADF;
2✔
1091
        FILE *f;
2✔
1092
        int r;
2✔
1093

1094
        assert(wildcard_fd_is_valid(dir_fd));
2✔
1095
        assert(ret);
2✔
1096

1097
        sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
2✔
1098
        if (sk < 0)
2✔
UNCOV
1099
                return -errno;
×
1100

1101
        if (bind_name) {
2✔
1102
                /* If the caller specified a socket name to bind to, do so before connecting. This is
1103
                 * useful to communicate some minor, short meta-information token from the client to
1104
                 * the server. */
1105
                union sockaddr_union bsa;
1✔
1106

1107
                r = sockaddr_un_set_path(&bsa.un, bind_name);
1✔
1108
                if (r < 0)
1✔
UNCOV
1109
                        return r;
×
1110

1111
                if (bind(sk, &bsa.sa, r) < 0)
1✔
UNCOV
1112
                        return -errno;
×
1113
        }
1114

1115
        r = connect_unix_path(sk, dir_fd, path);
2✔
1116
        if (r < 0)
2✔
1117
                return r;
1118

1119
        if (shutdown(sk, SHUT_WR) < 0)
2✔
UNCOV
1120
                return -errno;
×
1121

1122
        f = take_fdopen(&sk, "r");
2✔
1123
        if (!f)
2✔
UNCOV
1124
                return -errno;
×
1125

1126
        *ret = f;
2✔
1127
        return 0;
2✔
1128
}
1129

1130
int xfopenat_full(
1,213,353✔
1131
                int dir_fd,
1132
                const char *path,
1133
                const char *mode,
1134
                int open_flags,
1135
                XfopenFlags flags,
1136
                const char *bind_name,
1137
                FILE **ret) {
1138

1139
        FILE *f = NULL;  /* avoid false maybe-uninitialized warning */
1,213,353✔
1140
        int r;
1,213,353✔
1141

1142
        assert(wildcard_fd_is_valid(dir_fd));
1,213,353✔
1143
        assert(mode);
1,213,353✔
1144
        assert(ret);
1,213,353✔
1145

1146
        r = xfopenat_regular(dir_fd, path, mode, open_flags, &f);
1,213,353✔
1147
        if (r == -ENXIO && FLAGS_SET(flags, XFOPEN_SOCKET)) {
1,213,353✔
1148
                /* ENXIO is what Linux returns if we open a node that is an AF_UNIX socket */
1149
                r = xfopenat_unix_socket(dir_fd, path, bind_name, &f);
2✔
1150
                if (IN_SET(r, -ENOTSOCK, -EINVAL))
2✔
1151
                        return -ENXIO; /* propagate original error if this is not a socket after all */
1,213,353✔
1152
        }
1153
        if (r < 0)
1,213,353✔
1154
                return r;
1155

1156
        if (FLAGS_SET(flags, XFOPEN_UNLOCKED))
973,633✔
1157
                (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
973,354✔
1158

1159
        *ret = f;
973,633✔
1160
        return 0;
973,633✔
1161
}
1162

1163
int fdopen_independent(int fd, const char *mode, FILE **ret) {
15,002✔
1164
        _cleanup_close_ int copy_fd = -EBADF;
15,002✔
1165
        _cleanup_fclose_ FILE *f = NULL;
15,002✔
1166
        int mode_flags;
15,002✔
1167

1168
        assert(fd >= 0);
15,002✔
1169
        assert(mode);
15,002✔
1170
        assert(ret);
15,002✔
1171

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

1175
        mode_flags = fopen_mode_to_flags(mode);
15,002✔
1176
        if (mode_flags < 0)
15,002✔
1177
                return mode_flags;
1178

1179
        /* Flags returned by fopen_mode_to_flags might contain O_CREAT, but it doesn't make sense for fd_reopen
1180
         * since we're working on an existing fd anyway. Let's drop it here to avoid triggering assertion. */
1181
        copy_fd = fd_reopen(fd, mode_flags & ~O_CREAT);
15,002✔
1182
        if (copy_fd < 0)
15,002✔
1183
                return copy_fd;
1184

1185
        f = take_fdopen(&copy_fd, mode);
15,002✔
1186
        if (!f)
15,002✔
1187
                return -errno;
×
1188

1189
        *ret = TAKE_PTR(f);
15,002✔
1190
        return 0;
15,002✔
1191
}
1192

1193
static int search_and_open_internal(
30,137✔
1194
                const char *path,
1195
                int mode,            /* if ret_fd is NULL this is an [FRWX]_OK mode for access(), otherwise an open mode for open() */
1196
                const char *root,
1197
                char **search,
1198
                int *ret_fd,
1199
                char **ret_path) {
1200

1201
        int r;
30,137✔
1202

1203
        assert(!ret_fd || !FLAGS_SET(mode, O_CREAT)); /* We don't support O_CREAT for this */
30,137✔
1204
        assert(path);
30,137✔
1205

1206
        if (path_is_absolute(path)) {
30,137✔
UNCOV
1207
                _cleanup_close_ int fd = -EBADF;
×
1208

1209
                if (ret_fd)
15✔
1210
                        /* We only specify 0777 here to appease static analyzers, it's never used since we
1211
                         * don't support O_CREAT here */
1212
                        r = fd = RET_NERRNO(open(path, mode, 0777));
13✔
1213
                else
1214
                        r = RET_NERRNO(access(path, mode));
2✔
1215
                if (r < 0)
4✔
1216
                        return r;
1217

1218
                if (ret_path) {
11✔
1219
                        r = path_simplify_alloc(path, ret_path);
11✔
1220
                        if (r < 0)
11✔
1221
                                return r;
1222
                }
1223

1224
                if (ret_fd)
11✔
1225
                        *ret_fd = TAKE_FD(fd);
10✔
1226

1227
                return 0;
1228
        }
1229

1230
        if (!path_strv_resolve_uniq(search, root))
30,122✔
1231
                return -ENOMEM;
1232

1233
        STRV_FOREACH(i, search) {
154,338✔
1234
                _cleanup_close_ int fd = -EBADF;
154,353✔
1235
                _cleanup_free_ char *p = NULL;
129,467✔
1236

1237
                p = path_join(root, *i, path);
129,467✔
1238
                if (!p)
129,467✔
1239
                        return -ENOMEM;
1240

1241
                if (ret_fd)
129,467✔
1242
                        /* as above, 0777 is static analyzer appeasement */
1243
                        r = fd = RET_NERRNO(open(p, mode, 0777));
129,007✔
1244
                else
1245
                        r = RET_NERRNO(access(p, F_OK));
460✔
1246
                if (r >= 0) {
124,216✔
1247
                        if (ret_path)
5,251✔
1248
                                *ret_path = path_simplify(TAKE_PTR(p));
5,224✔
1249

1250
                        if (ret_fd)
5,251✔
1251
                                *ret_fd = TAKE_FD(fd);
5,239✔
1252

1253
                        return 0;
1254
                }
1255
                if (r != -ENOENT)
124,216✔
1256
                        return r;
1257
        }
1258

1259
        return -ENOENT;
1260
}
1261

1262
int search_and_open(
30,137✔
1263
                const char *path,
1264
                int mode,
1265
                const char *root,
1266
                char **search,
1267
                int *ret_fd,
1268
                char **ret_path) {
1269

1270
        _cleanup_strv_free_ char **copy = NULL;
30,137✔
1271

1272
        assert(path);
30,137✔
1273

1274
        copy = strv_copy(search);
30,137✔
1275
        if (!copy)
30,137✔
1276
                return -ENOMEM;
1277

1278
        return search_and_open_internal(path, mode, root, copy, ret_fd, ret_path);
30,137✔
1279
}
1280

1281
static int search_and_fopen_internal(
30,127✔
1282
                const char *path,
1283
                const char *mode,
1284
                const char *root,
1285
                char **search,
1286
                FILE **ret_file,
1287
                char **ret_path) {
1288

1289
        _cleanup_free_ char *found_path = NULL;
30,127✔
1290
        _cleanup_close_ int fd = -EBADF;
30,127✔
1291
        int r;
30,127✔
1292

1293
        assert(path);
30,127✔
1294
        assert(mode || !ret_file);
30,127✔
1295

1296
        r = search_and_open(
60,402✔
1297
                        path,
1298
                        mode ? fopen_mode_to_flags(mode) : 0,
29,915✔
1299
                        root,
1300
                        search,
1301
                        ret_file ? &fd : NULL,
1302
                        ret_path ? &found_path : NULL);
1303
        if (r < 0)
30,127✔
1304
                return r;
1305

1306
        if (ret_file) {
5,252✔
1307
                FILE *f = take_fdopen(&fd, mode);
5,249✔
1308
                if (!f)
5,249✔
UNCOV
1309
                        return -errno;
×
1310

1311
                *ret_file = f;
5,249✔
1312
        }
1313

1314
        if (ret_path)
5,252✔
1315
                *ret_path = TAKE_PTR(found_path);
5,225✔
1316

1317
        return 0;
1318
}
1319

1320
int search_and_fopen(
57✔
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 **copy = NULL;
57✔
1329

1330
        assert(path);
57✔
1331
        assert(mode || !ret_file);
57✔
1332

1333
        copy = strv_copy((char**) search);
57✔
1334
        if (!copy)
57✔
1335
                return -ENOMEM;
1336

1337
        return search_and_fopen_internal(path, mode, root, copy, ret_file, ret_path);
57✔
1338
}
1339

1340
int search_and_fopen_nulstr(
30,070✔
1341
                const char *path,
1342
                const char *mode,
1343
                const char *root,
1344
                const char *search,
1345
                FILE **ret_file,
1346
                char **ret_path) {
1347

1348
        _cleanup_strv_free_ char **l = NULL;
30,070✔
1349

1350
        assert(path);
30,070✔
1351
        assert(mode || !ret_file);
30,070✔
1352

1353
        l = strv_split_nulstr(search);
30,070✔
1354
        if (!l)
30,070✔
1355
                return -ENOMEM;
1356

1357
        return search_and_fopen_internal(path, mode, root, l, ret_file, ret_path);
30,070✔
1358
}
1359

1360
int fflush_and_check(FILE *f) {
1,473,594✔
1361
        assert(f);
1,473,594✔
1362

1363
        errno = 0;
1,473,594✔
1364
        fflush(f);
1,473,594✔
1365

1366
        if (ferror(f))
1,473,594✔
1367
                return errno_or_else(EIO);
32✔
1368

1369
        return 0;
1370
}
1371

1372
int fflush_sync_and_check(FILE *f) {
2,583✔
1373
        int r, fd;
2,583✔
1374

1375
        assert(f);
2,583✔
1376

1377
        r = fflush_and_check(f);
2,583✔
1378
        if (r < 0)
2,583✔
1379
                return r;
1380

1381
        /* Not all file streams have an fd associated (think: fmemopen()), let's handle this gracefully and
1382
         * assume that in that case we need no explicit syncing */
1383
        fd = fileno(f);
2,583✔
1384
        if (fd < 0)
2,583✔
1385
                return 0;
1386

1387
        r = fsync_full(fd);
2,583✔
1388
        if (r < 0)
2,583✔
UNCOV
1389
                return r;
×
1390

1391
        return 0;
1392
}
1393

1394
int write_timestamp_file_atomic(const char *fn, usec_t n) {
207✔
1395
        char ln[DECIMAL_STR_MAX(n)+2];
207✔
1396

1397
        /* Creates a "timestamp" file, that contains nothing but a
1398
         * usec_t timestamp, formatted in ASCII. */
1399

1400
        if (!timestamp_is_set(n))
207✔
1401
                return -ERANGE;
207✔
1402

1403
        xsprintf(ln, USEC_FMT "\n", n);
207✔
1404

1405
        return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
207✔
1406
}
1407

1408
int read_timestamp_file(const char *fn, usec_t *ret) {
44✔
1409
        _cleanup_free_ char *ln = NULL;
44✔
1410
        uint64_t t;
44✔
1411
        int r;
44✔
1412

1413
        assert(ret);
44✔
1414

1415
        r = read_one_line_file(fn, &ln);
44✔
1416
        if (r < 0)
44✔
1417
                return r;
1418

UNCOV
1419
        r = safe_atou64(ln, &t);
×
1420
        if (r < 0)
×
1421
                return r;
1422

UNCOV
1423
        if (!timestamp_is_set(t))
×
1424
                return -ERANGE;
1425

UNCOV
1426
        *ret = (usec_t) t;
×
UNCOV
1427
        return 0;
×
1428
}
1429

1430
int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space) {
3,440✔
1431
        assert(s);
3,440✔
1432
        assert(space);
3,440✔
1433

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

1439
        if (!f)
3,440✔
UNCOV
1440
                f = stdout;
×
1441

1442
        if (!separator)
3,440✔
1443
                separator = " ";
1,750✔
1444

1445
        if (*space)
3,440✔
1446
                if (fputs(separator, f) < 0)
1,517✔
1447
                        return -EIO;
1448

1449
        *space = true;
3,440✔
1450

1451
        if (fputs(s, f) < 0)
3,440✔
UNCOV
1452
                return -EIO;
×
1453

1454
        return 0;
1455
}
1456

1457
int fputs_with_newline(FILE *f, const char *s) {
5,978✔
1458

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

1462
        if (isempty(s))
5,978✔
1463
                return 0;
1464

1465
        if (!f)
5,978✔
UNCOV
1466
                f = stdout;
×
1467

1468
        if (fputs(s, f) < 0)
5,978✔
1469
                return -EIO;
1470

1471
        if (endswith(s, "\n"))
5,978✔
1472
                return 0;
1473

1474
        if (fputc('\n', f) < 0)
5,555✔
UNCOV
1475
                return -EIO;
×
1476

1477
        return 1;
1478
}
1479

1480
/* A bitmask of the EOL markers we know */
1481
typedef enum EndOfLineMarker {
1482
        EOL_NONE     = 0,
1483
        EOL_ZERO     = 1 << 0,  /* \0 (aka NUL) */
1484
        EOL_TEN      = 1 << 1,  /* \n (aka NL, aka LF)  */
1485
        EOL_THIRTEEN = 1 << 2,  /* \r (aka CR)  */
1486
} EndOfLineMarker;
1487

1488
static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
340,470,459✔
1489

1490
        if (!FLAGS_SET(flags, READ_LINE_ONLY_NUL)) {
340,470,459✔
1491
                if (c == '\n')
338,900,243✔
1492
                        return EOL_TEN;
1493
                if (c == '\r')
327,311,682✔
1494
                        return EOL_THIRTEEN;
1495
        }
1496

1497
        if (c == '\0')
328,881,882✔
1498
                return EOL_ZERO;
49,740✔
1499

1500
        return EOL_NONE;
1501
}
1502

1503
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, funlockfile, NULL);
11,019,277✔
1504

1505
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
11,019,277✔
1506
        _cleanup_free_ char *buffer = NULL;
11,019,277✔
1507
        size_t n = 0, count = 0;
11,019,277✔
1508
        int r;
11,019,277✔
1509

1510
        assert(f);
11,019,277✔
1511

1512
        /* Something like a bounded version of getline().
1513
         *
1514
         * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
1515
         * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
1516
         * endings:
1517
         *
1518
         *     • \n        (UNIX)
1519
         *     • \r        (old MacOS)
1520
         *     • \0        (C strings)
1521
         *     • \n\0
1522
         *     • \r\0
1523
         *     • \r\n      (Windows)
1524
         *     • \n\r
1525
         *     • \r\n\0
1526
         *     • \n\r\0
1527
         *
1528
         * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1529
         * the number of characters in the returned string). When EOF is hit, 0 is returned.
1530
         *
1531
         * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1532
         * delimiters. If the limit is hit we fail and return -ENOBUFS.
1533
         *
1534
         * If a line shall be skipped ret may be initialized as NULL. */
1535

1536
        if (ret) {
11,019,277✔
1537
                if (!GREEDY_REALLOC(buffer, 1))
11,019,246✔
1538
                        return -ENOMEM;
1539
        }
1540

1541
        {
1542
                _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
11,019,277✔
1543
                EndOfLineMarker previous_eol = EOL_NONE;
11,019,277✔
1544
                flockfile(f);
11,019,277✔
1545

1546
                for (;;) {
341,335,030✔
1547
                        EndOfLineMarker eol;
341,335,030✔
1548
                        char c;
341,335,030✔
1549

1550
                        if (n >= limit)
341,335,030✔
1551
                                return -ENOBUFS;
8✔
1552

1553
                        if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
341,335,024✔
1554
                                return -ENOBUFS;
1555

1556
                        r = safe_fgetc(f, &c);
341,335,024✔
1557
                        if (r < 0)
341,335,024✔
1558
                                return r;
1559
                        if (r == 0) /* EOF is definitely EOL */
341,335,022✔
1560
                                break;
1561

1562
                        eol = categorize_eol(c, flags);
340,470,459✔
1563

1564
                        if (FLAGS_SET(previous_eol, EOL_ZERO) ||
340,470,459✔
1565
                            (eol == EOL_NONE && previous_eol != EOL_NONE) ||
340,426,922✔
1566
                            (eol != EOL_NONE && (previous_eol & eol) != 0)) {
11,638,311✔
1567
                                /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
1568
                                 * EOL marker has been seen right before?  In either of these three cases we are
1569
                                 * done. But first, let's put this character back in the queue. (Note that we have to
1570
                                 * cast this to (unsigned char) here as ungetc() expects a positive 'int', and if we
1571
                                 * are on an architecture where 'char' equals 'signed char' we need to ensure we don't
1572
                                 * pass a negative value here. That said, to complicate things further ungetc() is
1573
                                 * actually happy with most negative characters and implicitly casts them back to
1574
                                 * positive ones as needed, except for \xff (aka -1, aka EOF), which it refuses. What a
1575
                                 * godawful API!) */
1576
                                assert_se(ungetc((unsigned char) c, f) != EOF);
10,154,706✔
1577
                                break;
1578
                        }
1579

1580
                        count++;
330,315,753✔
1581

1582
                        if (eol != EOL_NONE) {
319,523,147✔
1583
                                /* If we are on a tty, we can't shouldn't wait for more input, because that
1584
                                 * generally means waiting for the user, interactively. In the case of a TTY
1585
                                 * we expect only \n as the single EOL marker, so we are in the lucky
1586
                                 * position that there is no need to wait. We check this condition last, to
1587
                                 * avoid isatty() check if not necessary. */
1588

1589
                                if ((flags & (READ_LINE_IS_A_TTY|READ_LINE_NOT_A_TTY)) == 0) {
10,792,606✔
1590
                                        int fd;
10,030,372✔
1591

1592
                                        fd = fileno(f);
10,030,372✔
1593
                                        if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
10,030,372✔
1594
                                                     * and don't call isatty() on an invalid fd */
1595
                                                flags |= READ_LINE_NOT_A_TTY;
28✔
1596
                                        else
1597
                                                flags |= isatty_safe(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
20,060,688✔
1598
                                }
1599
                                if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
10,792,606✔
1600
                                        break;
1601
                        }
1602

1603
                        if (eol != EOL_NONE) {
330,315,753✔
1604
                                previous_eol |= eol;
10,792,606✔
1605
                                continue;
10,792,606✔
1606
                        }
1607

1608
                        if (ret) {
319,523,147✔
1609
                                if (!GREEDY_REALLOC(buffer, n + 2))
319,523,047✔
1610
                                        return -ENOMEM;
1611

1612
                                buffer[n] = c;
319,523,047✔
1613
                        }
1614

1615
                        n++;
319,523,147✔
1616
                }
1617
        }
1618

1619
        if (ret) {
11,019,269✔
1620
                buffer[n] = 0;
11,019,238✔
1621

1622
                *ret = TAKE_PTR(buffer);
11,019,238✔
1623
        }
1624

1625
        return (int) count;
11,019,269✔
1626
}
1627

1628
int read_stripped_line(FILE *f, size_t limit, char **ret) {
5,359,502✔
1629
        _cleanup_free_ char *s = NULL;
5,359,502✔
1630
        int r, k;
5,359,502✔
1631

1632
        assert(f);
5,359,502✔
1633

1634
        r = read_line(f, limit, ret ? &s : NULL);
5,359,502✔
1635
        if (r < 0)
5,359,502✔
1636
                return r;
1637

1638
        if (ret) {
5,359,502✔
1639
                const char *p = strstrip(s);
5,359,502✔
1640
                if (p == s)
5,359,502✔
1641
                        *ret = TAKE_PTR(s);
5,359,499✔
1642
                else {
1643
                        k = strdup_to(ret, p);
3✔
1644
                        if (k < 0)
3✔
1645
                                return k;
1646
                }
1647
        }
1648

1649
        return r > 0;          /* Return 1 if something was read. */
5,359,502✔
1650
}
1651

1652
int safe_fgetc(FILE *f, char *ret) {
341,342,429✔
1653
        int k;
341,342,429✔
1654

1655
        assert(f);
341,342,429✔
1656

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

1661
        errno = 0;
341,342,429✔
1662
        k = fgetc(f);
341,342,429✔
1663
        if (k == EOF) {
341,342,429✔
1664
                if (ferror(f))
864,667✔
1665
                        return errno_or_else(EIO);
2✔
1666

1667
                if (ret)
864,665✔
1668
                        *ret = 0;
864,579✔
1669

1670
                return 0;
1671
        }
1672

1673
        if (ret)
340,477,762✔
1674
                *ret = k;
340,477,124✔
1675

1676
        return 1;
1677
}
1678

1679
int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line) {
167✔
1680
        struct stat _st;
167✔
1681

1682
        if (!filename)
167✔
1683
                return 0;
167✔
1684

1685
        if (!st) {
167✔
1686
                if (stat(filename, &_st) < 0)
22✔
UNCOV
1687
                        return -errno;
×
1688
                st = &_st;
1689
        }
1690

1691
        if ((st->st_mode & S_IRWXO) == 0)
167✔
1692
                return 0;
1693

1694
        if (unit)
41✔
UNCOV
1695
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
1696
                           "%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
1697
                           filename, st->st_mode & 07777);
1698
        else
1699
                log_warning("%s has %04o mode that is too permissive, please adjust the ownership and access mode.",
41✔
1700
                            filename, st->st_mode & 07777);
1701
        return 0;
1702
}
1703

1704
int write_data_file_atomic_at(
19✔
1705
                int dir_fd,
1706
                const char *path,
1707
                const struct iovec *iovec,
1708
                WriteDataFileFlags flags) {
1709

1710
        int r;
19✔
1711

1712
        assert(wildcard_fd_is_valid(dir_fd));
19✔
1713

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

1717
        _cleanup_free_ char *dn = NULL, *fn = NULL;
19✔
1718
        r = path_split_prefix_filename(path, &dn, &fn);
19✔
1719
        if (IN_SET(r, -EADDRNOTAVAIL, O_DIRECTORY))
19✔
1720
                return -EISDIR; /* path refers to "." or "/" (which are dirs, which we cannot write), or is suffixed with "/" */
1721
        if (r < 0)
16✔
1722
                return r;
1723

1724
        _cleanup_close_ int mfd = -EBADF;
19✔
1725
        if (dn) {
14✔
1726
                /* If there's a directory component, readjust our position */
1727
                r = chaseat(XAT_FDROOT,
26✔
1728
                            dir_fd,
1729
                            dn,
1730
                            FLAGS_SET(flags, WRITE_DATA_FILE_MKDIR_0755) ? CHASE_MKDIR_0755 : 0,
13✔
1731
                            /* ret_path= */ NULL,
1732
                            &mfd);
1733
                if (r < 0)
13✔
1734
                        return r;
1735

1736
                dir_fd = mfd;
12✔
1737
        }
1738

1739
        _cleanup_free_ char *t = NULL;
13✔
1740
        _cleanup_close_ int fd = open_tmpfile_linkable_at(dir_fd, fn, O_WRONLY|O_CLOEXEC, &t);
26✔
1741
        if (fd < 0)
13✔
1742
                return fd;
1743

1744
        CLEANUP_TMPFILE_AT(dir_fd, t);
13✔
1745

1746
        if (iovec_is_set(iovec)) {
13✔
1747
                r = loop_write(fd, iovec->iov_base, iovec->iov_len);
13✔
1748
                if (r < 0)
13✔
1749
                        return r;
1750
        }
1751

1752
        r = fchmod_umask(fd, FLAGS_SET(flags, WRITE_DATA_FILE_MODE_0400) ? 0400 : 0644);
20✔
1753
        if (r < 0)
13✔
1754
                return r;
1755

1756
        r = link_tmpfile_at(fd, dir_fd, t, fn, LINK_TMPFILE_REPLACE);
13✔
1757
        if (r < 0)
13✔
1758
                return r;
1759

1760
        t = mfree(t); /* disarm CLEANUP_TMPFILE_AT */
12✔
1761
        return 0;
12✔
1762
}
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