Debian Codegraph @

src/basic/fileio.c

raw
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) {
52 assert(ret);
53
54 FILE *f = fdopen(fd, options);
55 if (!f)
56 return -errno;
57
58 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
59
60 *ret = f;
61 return 0;
62 }
63
64 int take_fdopen_unlocked(int *fd, const char *options, FILE **ret) {
65 int r;
66
67 assert(fd);
68
69 r = fdopen_unlocked(*fd, options, ret);
70 if (r < 0)
71 return r;
72
73 *fd = -EBADF;
74
75 return 0;
76 }
77
78 FILE* take_fdopen(int *fd, const char *options) {
79 assert(fd);
80
81 FILE *f = fdopen(*fd, options);
82 if (!f)
83 return NULL;
84
85 *fd = -EBADF;
86
87 return f;
88 }
89
90 DIR* take_fdopendir(int *dfd) {
91 assert(dfd);
92
93 DIR *d = fdopendir(*dfd);
94 if (!d)
95 return NULL;
96
97 *dfd = -EBADF;
98
99 return d;
100 }
101
102 FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) {
103 FILE *f = open_memstream(ptr, sizeloc);
104 if (!f)
105 return NULL;
106
107 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
108
109 return f;
110 }
111
112 FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode) {
113 FILE *f = fmemopen(buf, size, mode);
114 if (!f)
115 return NULL;
116
117 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
118
119 return f;
120 }
121
122 int write_string_stream_full(
123 FILE *f,
124 const char *line,
125 WriteStringFileFlags flags,
126 const struct timespec *ts) {
127
128 bool needs_nl;
129 int r, fd = -EBADF;
130
131 assert(f);
132 assert(line);
133
134 if (ferror(f))
135 return -EIO;
136
137 if (ts) {
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);
141 if (fd < 0)
142 return -EBADF;
143 }
144
145 if (flags & WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) {
146 _cleanup_free_ char *t = NULL;
147
148 /* If value to be written is same as that of the existing value, then suppress the write. */
149
150 if (fd < 0) {
151 fd = fileno(f);
152 if (fd < 0)
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 &&
160 streq_skip_trailing_chars(line, t, NEWLINE)) {
161 log_debug("No change in value '%s', suppressing write", line);
162 return 0;
163 }
164
165 if (lseek(fd, 0, SEEK_SET) < 0)
166 return -errno;
167 }
168
169 needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
170
171 if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
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");
176 needs_nl = false;
177 }
178
179 if (fputs(line, f) == EOF)
180 return -errno;
181
182 if (needs_nl)
183 if (fputc('\n', f) == EOF)
184 return -errno;
185
186 if (flags & WRITE_STRING_FILE_SYNC)
187 r = fflush_sync_and_check(f);
188 else
189 r = fflush_and_check(f);
190 if (r < 0)
191 return r;
192
193 if (ts) {
194 const struct timespec twice[2] = {*ts, *ts};
195
196 assert(fd >= 0);
197 if (futimens(fd, twice) < 0)
198 return -errno;
199 }
200
201 return 0;
202 }
203
204 static mode_t write_string_file_flags_to_mode(WriteStringFileFlags flags) {
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 :
214 FLAGS_SET(flags, WRITE_STRING_FILE_MODE_0444) ? 0444 : 0644;
215 }
216
217 static int write_string_file_atomic_at(
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;
225 _cleanup_free_ char *p = NULL;
226 int r;
227
228 assert(fn);
229 assert(line);
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);
235
236 bool call_label_ops_post = false;
237 if (FLAGS_SET(flags, WRITE_STRING_FILE_LABEL)) {
238 r = label_ops_pre(dir_fd, fn, mode);
239 if (r < 0)
240 return r;
241
242 call_label_ops_post = true;
243 }
244
245 r = fopen_temporary_at(dir_fd, fn, &f, &p);
246 int k = call_label_ops_post ? label_ops_post(f ? fileno(f) : dir_fd, f ? NULL : fn, /* created= */ !!f) : 0;
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)
250 return r;
251 CLEANUP_TMPFILE_AT(dir_fd, p);
252 if (k < 0)
253 return k;
254
255 r = write_string_stream_full(f, line, flags, ts);
256 if (r < 0)
257 return r;
258
259 r = fchmod_umask(fileno(f), mode);
260 if (r < 0)
261 return r;
262
263 r = RET_NERRNO(renameat(dir_fd, p, dir_fd, fn));
264 if (r < 0)
265 return r;
266
267 p = mfree(p); /* disarm CLEANUP_TMPFILE_AT() */
268
269 if (FLAGS_SET(flags, WRITE_STRING_FILE_SYNC)) {
270 /* Sync the rename, too */
271 r = fsync_directory_of_file(fileno(f));
272 if (r < 0)
273 return r;
274 }
275
276 return 0;
277 }
278
279 int write_string_file_full(
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;
288 _cleanup_fclose_ FILE *f = NULL;
289 _cleanup_close_ int fd = -EBADF;
290 int r;
291
292 assert(dir_fd == AT_FDCWD || dir_fd >= 0);
293 assert(line);
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)));
297
298 if (flags & WRITE_STRING_FILE_MKDIR_0755) {
299 assert(fn);
300
301 r = mkdirat_parents(dir_fd, fn, 0755);
302 if (r < 0)
303 return r;
304 }
305
306 if (flags & WRITE_STRING_FILE_ATOMIC) {
307 assert(fn);
308 assert(flags & WRITE_STRING_FILE_CREATE);
309
310 r = write_string_file_atomic_at(dir_fd, fn, line, flags, ts);
311 if (r < 0)
312 goto fail;
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))
319 r = fd = fd_reopen(
320 ASSERT_FD(dir_fd), O_CLOEXEC | O_NOCTTY |
321 (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
322 (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
323 (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0));
324 else {
325 mode_t mode = write_string_file_flags_to_mode(flags);
326 bool call_label_ops_post = false;
327
328 if (FLAGS_SET(flags, WRITE_STRING_FILE_LABEL|WRITE_STRING_FILE_CREATE)) {
329 r = label_ops_pre(dir_fd, label_fn ?: fn, mode);
330 if (r < 0)
331 goto fail;
332
333 call_label_ops_post = true;
334 }
335
336 r = fd = openat_report_new(
337 dir_fd, fn, O_CLOEXEC | O_NOCTTY |
338 (FLAGS_SET(flags, WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0) |
339 (FLAGS_SET(flags, WRITE_STRING_FILE_CREATE) ? O_CREAT : 0) |
340 (FLAGS_SET(flags, WRITE_STRING_FILE_TRUNCATE) ? O_TRUNC : 0) |
341 (FLAGS_SET(flags, WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL) ? O_RDWR : O_WRONLY) |
342 (FLAGS_SET(flags, WRITE_STRING_FILE_OPEN_NONBLOCKING) ? O_NONBLOCK : 0),
343 mode,
344 &made_file);
345 if (call_label_ops_post)
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));
349 }
350 if (r < 0)
351 goto fail;
352
353 r = take_fdopen_unlocked(&fd, "w", &f);
354 if (r < 0)
355 goto fail;
356
357 if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
358 setvbuf(f, NULL, _IONBF, 0);
359
360 r = write_string_stream_full(f, line, flags, ts);
361 if (r < 0)
362 goto fail;
363
364 return 0;
365
366 fail:
367 if (made_file)
368 (void) unlinkat(dir_fd, fn, 0);
369
370 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
371 return r;
372
373 f = safe_fclose(f);
374 fd = safe_close(fd);
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)
379 return 0;
380
381 return r;
382 }
383
384 int write_string_filef(
385 const char *fn,
386 WriteStringFileFlags flags,
387 const char *format, ...) {
388
389 _cleanup_free_ char *p = NULL;
390 va_list ap;
391 int r;
392
393 va_start(ap, format);
394 r = vasprintf(&p, format, ap);
395 va_end(ap);
396
397 if (r < 0)
398 return -ENOMEM;
399
400 return write_string_file(fn, p, flags);
401 }
402
403 int write_base64_file_at(
404 int dir_fd,
405 const char *fn,
406 const struct iovec *data,
407 WriteStringFileFlags flags) {
408
409 _cleanup_free_ char *encoded = NULL;
410 ssize_t n;
411
412 n = base64mem_full(data ? data->iov_base : NULL, data ? data->iov_len : 0, 79, &encoded);
413 if (n < 0)
414 return n;
415
416 return write_string_file_at(dir_fd, fn, encoded, flags);
417 }
418
419 int read_one_line_file_at(int dir_fd, const char *filename, char **ret) {
420 _cleanup_fclose_ FILE *f = NULL;
421 int r;
422
423 assert(wildcard_fd_is_valid(dir_fd));
424 assert(filename);
425 assert(ret);
426
427 r = fopen_unlocked_at(dir_fd, filename, "re", /* open_flags= */ 0, &f);
428 if (r < 0)
429 return r;
430
431 return read_line(f, LONG_LINE_MAX, ret);
432 }
433
434 int read_boolean_file_at(int dir_fd, const char *filename) {
435 _cleanup_free_ char *s = NULL;
436 int r;
437
438 assert(wildcard_fd_is_valid(dir_fd));
439 assert(filename);
440
441 r = read_one_line_file_at(dir_fd, filename, &s);
442 if (r < 0)
443 return r;
444
445 return parse_boolean(s);
446 }
447
448 int verify_file_at(int dir_fd, const char *fn, const char *blob, bool accept_extra_nl) {
449 _cleanup_fclose_ FILE *f = NULL;
450 _cleanup_free_ char *buf = NULL;
451 size_t l, k;
452 int r;
453
454 assert(blob);
455
456 l = strlen(blob);
457
458 if (accept_extra_nl && endswith(blob, "\n"))
459 accept_extra_nl = false;
460
461 buf = malloc(l + accept_extra_nl + 1);
462 if (!buf)
463 return -ENOMEM;
464
465 r = fopen_unlocked_at(dir_fd, strempty(fn), "re", 0, &f);
466 if (r < 0)
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;
471 k = fread(buf, 1, l + accept_extra_nl + 1, f);
472 if (ferror(f))
473 return errno_or_else(EIO);
474
475 if (k != l && k != l + accept_extra_nl)
476 return 0;
477 if (memcmp(buf, blob, l) != 0)
478 return 0;
479 if (k > l && buf[l] != '\n')
480 return 0;
481
482 return 1;
483 }
484
485 int read_virtual_file_at(
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;
493 size_t n, size;
494 int n_retries;
495 bool truncated = false;
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);
515 assert(max_size <= READ_VIRTUAL_BYTES_MAX || max_size == SIZE_MAX);
516
517 _cleanup_close_ int fd = -EBADF;
518 if (isempty(filename))
519 fd = fd_reopen(ASSERT_FD(dir_fd), O_RDONLY | O_NOCTTY | O_CLOEXEC);
520 else
521 fd = RET_NERRNO(openat(dir_fd, filename, O_RDONLY | O_NOCTTY | O_CLOEXEC));
522 if (fd < 0)
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 (;;) {
529 struct stat st;
530
531 if (fstat(fd, &st) < 0)
532 return -errno;
533
534 if (!S_ISREG(st.st_mode))
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);
539 if (st.st_size > 0 && n_retries > 1) {
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. */
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);
551
552 if (size > READ_VIRTUAL_BYTES_MAX)
553 return -EFBIG;
554 }
555
556 n_retries--;
557 } else if (n_retries > 1) {
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);
561 n_retries = 1;
562 } else {
563 size = MIN(READ_VIRTUAL_BYTES_MAX, max_size);
564 n_retries = 0;
565 }
566
567 buf = malloc(size + 1);
568 if (!buf)
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);
573
574 for (;;) {
575 ssize_t k;
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);
581 if (k >= 0) {
582 n = k;
583 break;
584 }
585
586 if (errno != EINTR)
587 return -errno;
588 }
589
590 /* Consider a short read as EOF */
591 if (n <= size)
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) {
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)
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)
615 return -errno;
616
617 buf = mfree(buf);
618 }
619
620 if (ret_contents) {
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))
626 return -EBADMSG;
627
628 if (n < size) {
629 char *p;
630
631 /* Return rest of the buffer to libc */
632 p = realloc(buf, n + 1);
633 if (!p)
634 return -ENOMEM;
635 buf = p;
636 }
637
638 buf[n] = 0;
639 *ret_contents = TAKE_PTR(buf);
640 }
641
642 if (ret_size)
643 *ret_size = n;
644
645 return !truncated;
646 }
647
648 int read_full_stream_full(
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;
658 size_t n, n_next = 0, l, expected_decoded_size = size;
659 int fd, r;
660
661 assert(f);
662 assert(ret_contents);
663 assert(!FLAGS_SET(flags, READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX));
664 assert(size != SIZE_MAX || !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER));
665
666 if (offset != UINT64_MAX && offset > LONG_MAX) /* fseek() can only deal with "long" offsets */
667 return -ERANGE;
668
669 if ((flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) != 0) {
670 if (size <= SIZE_MAX / READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY)
671 size *= READ_FULL_FILE_ENCODED_STRING_AMPLIFICATION_BOUNDARY;
672 else
673 size = SIZE_MAX;
674 }
675
676 fd = fileno(f);
677 if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see
678 * fmemopen()), let's optimize our buffering */
679 struct stat st;
680
681 if (fstat(fd, &st) < 0)
682 return -errno;
683
684 if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR)) {
685 r = stat_verify_regular(&st);
686 if (r < 0)
687 return r;
688 }
689
690 if (S_ISREG(st.st_mode)) {
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 &&
699 (size == SIZE_MAX || FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER))) {
700
701 uint64_t rsize =
702 LESS_BY((uint64_t) st.st_size, offset == UINT64_MAX ? 0 : offset);
703
704 if (rsize < SIZE_MAX) /* overflow check */
705 n_next = rsize + 1;
706 }
707
708 if (flags & READ_FULL_FILE_WARN_WORLD_READABLE)
709 (void) warn_file_is_world_accessible(filename, &st, NULL, 0);
710 }
711 } else if (FLAGS_SET(flags, READ_FULL_FILE_VERIFY_REGULAR))
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)
719 n_next = size != SIZE_MAX && !FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) ? size : LINE_MAX;
720
721 /* Never read more than we need to determine that our own limit is hit */
722 if (n_next > READ_FULL_BYTES_MAX)
723 n_next = READ_FULL_BYTES_MAX + 1;
724
725 if (offset != UINT64_MAX && fseek(f, offset, SEEK_SET) < 0)
726 return -errno;
727
728 n = l = 0;
729 for (;;) {
730 char *t;
731 size_t k;
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)
736 n_next = size + 1;
737
738 if (flags & READ_FULL_FILE_SECURE) {
739 t = malloc(n_next + 1);
740 if (!t) {
741 r = -ENOMEM;
742 goto finalize;
743 }
744 memcpy_safe(t, buf, n);
745 explicit_bzero_safe(buf, n);
746 free(buf);
747 } else {
748 t = realloc(buf, n_next + 1);
749 if (!t)
750 return -ENOMEM;
751 }
752
753 buf = t;
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;
757
758 errno = 0;
759 k = fread(buf + l, 1, n - l, f);
760
761 assert(k <= n - l);
762 l += k;
763
764 if (ferror(f)) {
765 r = errno_or_else(EIO);
766 goto finalize;
767 }
768 if (feof(f))
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 */
772 assert(l == size);
773 break;
774 }
775
776 assert(k > 0); /* we can't have read zero bytes because that would have been EOF */
777
778 if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > size) {
779 r = -E2BIG;
780 goto finalize;
781 }
782
783 if (n >= READ_FULL_BYTES_MAX) {
784 r = -E2BIG;
785 goto finalize;
786 }
787
788 n_next = MIN(n * 2, READ_FULL_BYTES_MAX);
789 }
790
791 if (flags & (READ_FULL_FILE_UNBASE64 | READ_FULL_FILE_UNHEX)) {
792 _cleanup_free_ void *decoded = NULL;
793 size_t decoded_size;
794
795 buf[l++] = 0;
796 if (flags & READ_FULL_FILE_UNBASE64)
797 r = unbase64mem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
798 else
799 r = unhexmem_full(buf, l, flags & READ_FULL_FILE_SECURE, &decoded, &decoded_size);
800 if (r < 0)
801 goto finalize;
802
803 if (flags & READ_FULL_FILE_SECURE)
804 explicit_bzero_safe(buf, n);
805 free_and_replace(buf, decoded);
806 n = l = decoded_size;
807
808 if (FLAGS_SET(flags, READ_FULL_FILE_FAIL_WHEN_LARGER) && l > expected_decoded_size) {
809 r = -E2BIG;
810 goto finalize;
811 }
812 }
813
814 if (!ret_size) {
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)) {
820 r = -EBADMSG;
821 goto finalize;
822 }
823 }
824
825 buf[l] = 0;
826 *ret_contents = TAKE_PTR(buf);
827
828 if (ret_size)
829 *ret_size = l;
830
831 return 0;
832
833 finalize:
834 if (flags & READ_FULL_FILE_SECURE)
835 explicit_bzero_safe(buf, n);
836
837 return r;
838 }
839
840 int read_full_file_full(
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;
851 XfopenFlags xflags = XFOPEN_UNLOCKED;
852 int r;
853
854 assert(ret_contents);
855
856 if (FLAGS_SET(flags, READ_FULL_FILE_CONNECT_SOCKET) && /* If this is enabled, let's try to connect to it */
857 offset == UINT64_MAX) /* Seeking is not supported on AF_UNIX sockets */
858 xflags |= XFOPEN_SOCKET;
859
860 r = xfopenat_full(dir_fd, filename, "re", 0, xflags, bind_name, &f);
861 if (r < 0)
862 return r;
863
864 return read_full_stream_full(f, filename, offset, size, flags, ret_contents, ret_size);
865 }
866
867 int script_get_shebang_interpreter(const char *path, char **ret) {
868 _cleanup_fclose_ FILE *f = NULL;
869 int r;
870
871 assert(path);
872
873 f = fopen(path, "re");
874 if (!f)
875 return -errno;
876
877 char c;
878 r = safe_fgetc(f, &c);
879 if (r < 0)
880 return r;
881 if (r == 0)
882 return -EBADMSG;
883 if (c != '#')
884 return -EMEDIUMTYPE;
885 r = safe_fgetc(f, &c);
886 if (r < 0)
887 return r;
888 if (r == 0)
889 return -EBADMSG;
890 if (c != '!')
891 return -EMEDIUMTYPE;
892
893 _cleanup_free_ char *line = NULL;
894 r = read_line(f, LONG_LINE_MAX, &line);
895 if (r < 0)
896 return r;
897
898 _cleanup_free_ char *p = NULL;
899 const char *s = line;
900
901 r = extract_first_word(&s, &p, /* separators= */ NULL, /* flags= */ 0);
902 if (r < 0)
903 return r;
904 if (r == 0)
905 return -ENOEXEC;
906
907 if (ret)
908 *ret = TAKE_PTR(p);
909 return 0;
910 }
911
912 int get_proc_field(const char *path, const char *key, char **ret) {
913 _cleanup_fclose_ FILE *f = NULL;
914 int r;
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);
922 assert(key);
923
924 r = fopen_unlocked(path, "re", &f);
925 if (r == -ENOENT && proc_mounted() == 0)
926 return -ENOSYS;
927 if (r < 0)
928 return r;
929
930 for (;;) {
931 _cleanup_free_ char *line = NULL;
932
933 r = read_line(f, LONG_LINE_MAX, &line);
934 if (r < 0)
935 return r;
936 if (r == 0)
937 return -ENODATA;
938
939 char *l = startswith(line, key);
940 if (l && *l == ':') {
941 if (ret) {
942 char *s = strdupcspn(skip_leading_chars(l + 1, " \t"), WHITESPACE);
943 if (!s)
944 return -ENOMEM;
945
946 *ret = s;
947 }
948
949 return 0;
950 }
951 }
952 }
953
954 DIR* xopendirat(int dir_fd, const char *path, int flags) {
955 _cleanup_close_ int fd = -EBADF;
956
957 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
958 assert(!(flags & (O_CREAT|O_TMPFILE)));
959
960 if ((dir_fd == AT_FDCWD || path_is_absolute(path)) &&
961 (flags &~ O_DIRECTORY) == 0)
962 return opendir(path);
963
964 if (isempty(path)) {
965 path = ".";
966 flags |= O_NOFOLLOW;
967 }
968
969 fd = openat(dir_fd, path, O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags);
970 if (fd < 0)
971 return NULL;
972
973 return take_fdopendir(&fd);
974 }
975
976 int fopen_mode_to_flags(const char *mode) {
977 const char *p;
978 int flags;
979
980 assert(mode);
981
982 if ((p = startswith(mode, "r+")))
983 flags = O_RDWR;
984 else if ((p = startswith(mode, "r")))
985 flags = O_RDONLY;
986 else if ((p = startswith(mode, "w+")))
987 flags = O_RDWR|O_CREAT|O_TRUNC;
988 else if ((p = startswith(mode, "w")))
989 flags = O_WRONLY|O_CREAT|O_TRUNC;
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++) {
998
999 switch (*p) {
1000
1001 case 'e':
1002 flags |= O_CLOEXEC;
1003 break;
1004
1005 case 'x':
1006 flags |= O_EXCL;
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) {
1023 FILE *f;
1024
1025 /* A combination of fopen() with openat() */
1026
1027 assert(wildcard_fd_is_valid(dir_fd));
1028 assert(mode);
1029 assert(ret);
1030
1031 if (dir_fd == AT_FDCWD && path && open_flags == 0)
1032 f = fopen(path, mode);
1033 else if (dir_fd == XAT_FDROOT && path && open_flags == 0) {
1034 _cleanup_free_ char *j = strjoin("/", path);
1035 if (!j)
1036 return -ENOMEM;
1037
1038 f = fopen(j, mode);
1039 } else {
1040 _cleanup_close_ int fd = -EBADF;
1041 int mode_flags;
1042
1043 mode_flags = fopen_mode_to_flags(mode);
1044 if (mode_flags < 0)
1045 return mode_flags;
1046
1047 if (path) {
1048 fd = openat(dir_fd, path, mode_flags | open_flags);
1049 if (fd < 0)
1050 return -errno;
1051 } else {
1052 if (dir_fd == AT_FDCWD)
1053 return -EBADF;
1054
1055 fd = fd_reopen(dir_fd, (mode_flags | open_flags) & ~O_NOFOLLOW);
1056 if (fd < 0)
1057 return fd;
1058 }
1059
1060 f = take_fdopen(&fd, mode);
1061 }
1062 if (!f)
1063 return -errno;
1064
1065 *ret = f;
1066 return 0;
1067 }
1068
1069 static int xfopenat_unix_socket(int dir_fd, const char *path, const char *bind_name, FILE **ret) {
1070 _cleanup_close_ int sk = -EBADF;
1071 FILE *f;
1072 int r;
1073
1074 assert(wildcard_fd_is_valid(dir_fd));
1075 assert(ret);
1076
1077 sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1078 if (sk < 0)
1079 return -errno;
1080
1081 if (bind_name) {
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;
1086
1087 r = sockaddr_un_set_path(&bsa.un, bind_name);
1088 if (r < 0)
1089 return r;
1090
1091 if (bind(sk, &bsa.sa, r) < 0)
1092 return -errno;
1093 }
1094
1095 r = connect_unix_path(sk, dir_fd, path);
1096 if (r < 0)
1097 return r;
1098
1099 if (shutdown(sk, SHUT_WR) < 0)
1100 return -errno;
1101
1102 f = take_fdopen(&sk, "r");
1103 if (!f)
1104 return -errno;
1105
1106 *ret = f;
1107 return 0;
1108 }
1109
1110 int xfopenat_full(
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 */
1120 int r;
1121
1122 assert(wildcard_fd_is_valid(dir_fd));
1123 assert(mode);
1124 assert(ret);
1125
1126 r = xfopenat_regular(dir_fd, path, mode, open_flags, &f);
1127 if (r == -ENXIO && FLAGS_SET(flags, XFOPEN_SOCKET)) {
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);
1130 if (IN_SET(r, -ENOTSOCK, -EINVAL))
1131 return -ENXIO; /* propagate original error if this is not a socket after all */
1132 }
1133 if (r < 0)
1134 return r;
1135
1136 if (FLAGS_SET(flags, XFOPEN_UNLOCKED))
1137 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
1138
1139 *ret = f;
1140 return 0;
1141 }
1142
1143 int fdopen_independent(int fd, const char *mode, FILE **ret) {
1144 _cleanup_close_ int copy_fd = -EBADF;
1145 _cleanup_fclose_ FILE *f = NULL;
1146 int mode_flags;
1147
1148 assert(fd >= 0);
1149 assert(mode);
1150 assert(ret);
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);
1156 if (mode_flags < 0)
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);
1162 if (copy_fd < 0)
1163 return copy_fd;
1164
1165 f = take_fdopen(&copy_fd, mode);
1166 if (!f)
1167 return -errno;
1168
1169 *ret = TAKE_PTR(f);
1170 return 0;
1171 }
1172
1173 static int search_and_open_internal(
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;
1182
1183 assert(!ret_fd || !FLAGS_SET(mode, O_CREAT)); /* We don't support O_CREAT for this */
1184 assert(path);
1185
1186 if (path_is_absolute(path)) {
1187 _cleanup_close_ int fd = -EBADF;
1188
1189 if (ret_fd)
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));
1193 else
1194 r = RET_NERRNO(access(path, mode));
1195 if (r < 0)
1196 return r;
1197
1198 if (ret_path) {
1199 r = path_simplify_alloc(path, ret_path);
1200 if (r < 0)
1201 return r;
1202 }
1203
1204 if (ret_fd)
1205 *ret_fd = TAKE_FD(fd);
1206
1207 return 0;
1208 }
1209
1210 if (!path_strv_resolve_uniq(search, root))
1211 return -ENOMEM;
1212
1213 STRV_FOREACH(i, search) {
1214 _cleanup_close_ int fd = -EBADF;
1215 _cleanup_free_ char *p = NULL;
1216
1217 p = path_join(root, *i, path);
1218 if (!p)
1219 return -ENOMEM;
1220
1221 if (ret_fd)
1222 /* as above, 0777 is static analyzer appeasement */
1223 r = fd = RET_NERRNO(open(p, mode, 0777));
1224 else
1225 r = RET_NERRNO(access(p, F_OK));
1226 if (r >= 0) {
1227 if (ret_path)
1228 *ret_path = path_simplify(TAKE_PTR(p));
1229
1230 if (ret_fd)
1231 *ret_fd = TAKE_FD(fd);
1232
1233 return 0;
1234 }
1235 if (r != -ENOENT)
1236 return r;
1237 }
1238
1239 return -ENOENT;
1240 }
1241
1242 int search_and_open(
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;
1251
1252 assert(path);
1253
1254 copy = strv_copy(search);
1255 if (!copy)
1256 return -ENOMEM;
1257
1258 return search_and_open_internal(path, mode, root, copy, ret_fd, ret_path);
1259 }
1260
1261 static int search_and_fopen_internal(
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;
1270 _cleanup_close_ int fd = -EBADF;
1271 int r;
1272
1273 assert(path);
1274 assert(mode || !ret_file);
1275
1276 r = search_and_open(
1277 path,
1278 mode ? fopen_mode_to_flags(mode) : 0,
1279 root,
1280 search,
1281 ret_file ? &fd : NULL,
1282 ret_path ? &found_path : NULL);
1283 if (r < 0)
1284 return r;
1285
1286 if (ret_file) {
1287 FILE *f = take_fdopen(&fd, mode);
1288 if (!f)
1289 return -errno;
1290
1291 *ret_file = f;
1292 }
1293
1294 if (ret_path)
1295 *ret_path = TAKE_PTR(found_path);
1296
1297 return 0;
1298 }
1299
1300 int search_and_fopen(
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;
1309
1310 assert(path);
1311 assert(mode || !ret_file);
1312
1313 copy = strv_copy((char**) search);
1314 if (!copy)
1315 return -ENOMEM;
1316
1317 return search_and_fopen_internal(path, mode, root, copy, ret_file, ret_path);
1318 }
1319
1320 int search_and_fopen_nulstr(
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;
1329
1330 assert(path);
1331 assert(mode || !ret_file);
1332
1333 l = strv_split_nulstr(search);
1334 if (!l)
1335 return -ENOMEM;
1336
1337 return search_and_fopen_internal(path, mode, root, l, ret_file, ret_path);
1338 }
1339
1340 int fflush_and_check(FILE *f) {
1341 assert(f);
1342
1343 errno = 0;
1344 fflush(f);
1345
1346 if (ferror(f))
1347 return errno_or_else(EIO);
1348
1349 return 0;
1350 }
1351
1352 int fflush_sync_and_check(FILE *f) {
1353 int r, fd;
1354
1355 assert(f);
1356
1357 r = fflush_and_check(f);
1358 if (r < 0)
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);
1364 if (fd < 0)
1365 return 0;
1366
1367 r = fsync_full(fd);
1368 if (r < 0)
1369 return r;
1370
1371 return 0;
1372 }
1373
1374 int write_timestamp_file_atomic(const char *fn, usec_t n) {
1375 char ln[DECIMAL_STR_MAX(n)+2];
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))
1381 return -ERANGE;
1382
1383 xsprintf(ln, USEC_FMT "\n", n);
1384
1385 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1386 }
1387
1388 int read_timestamp_file(const char *fn, usec_t *ret) {
1389 _cleanup_free_ char *ln = NULL;
1390 uint64_t t;
1391 int r;
1392
1393 assert(ret);
1394
1395 r = read_one_line_file(fn, &ln);
1396 if (r < 0)
1397 return r;
1398
1399 r = safe_atou64(ln, &t);
1400 if (r < 0)
1401 return r;
1402
1403 if (!timestamp_is_set(t))
1404 return -ERANGE;
1405
1406 *ret = (usec_t) t;
1407 return 0;
1408 }
1409
1410 int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space) {
1411 assert(s);
1412 assert(space);
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)
1420 f = stdout;
1421
1422 if (!separator)
1423 separator = " ";
1424
1425 if (*space)
1426 if (fputs(separator, f) < 0)
1427 return -EIO;
1428
1429 *space = true;
1430
1431 if (fputs(s, f) < 0)
1432 return -EIO;
1433
1434 return 0;
1435 }
1436
1437 int fputs_with_newline(FILE *f, const char *s) {
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))
1443 return 0;
1444
1445 if (!f)
1446 f = stdout;
1447
1448 if (fputs(s, f) < 0)
1449 return -EIO;
1450
1451 if (endswith(s, "\n"))
1452 return 0;
1453
1454 if (fputc('\n', f) < 0)
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) {
1469
1470 if (!FLAGS_SET(flags, READ_LINE_ONLY_NUL)) {
1471 if (c == '\n')
1472 return EOL_TEN;
1473 if (c == '\r')
1474 return EOL_THIRTEEN;
1475 }
1476
1477 if (c == '\0')
1478 return EOL_ZERO;
1479
1480 return EOL_NONE;
1481 }
1482
1483 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, funlockfile, NULL);
1484
1485 int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
1486 _cleanup_free_ char *buffer = NULL;
1487 size_t n = 0, count = 0;
1488 int r;
1489
1490 assert(f);
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) {
1517 if (!GREEDY_REALLOC(buffer, 1))
1518 return -ENOMEM;
1519 }
1520
1521 {
1522 _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
1523 EndOfLineMarker previous_eol = EOL_NONE;
1524 flockfile(f);
1525
1526 for (;;) {
1527 EndOfLineMarker eol;
1528 char c;
1529
1530 if (n >= limit)
1531 return -ENOBUFS;
1532
1533 if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
1534 return -ENOBUFS;
1535
1536 r = safe_fgetc(f, &c);
1537 if (r < 0)
1538 return r;
1539 if (r == 0) /* EOF is definitely EOL */
1540 break;
1541
1542 eol = categorize_eol(c, flags);
1543
1544 if (FLAGS_SET(previous_eol, EOL_ZERO) ||
1545 (eol == EOL_NONE && previous_eol != EOL_NONE) ||
1546 (eol != EOL_NONE && (previous_eol & eol) != 0)) {
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);
1557 break;
1558 }
1559
1560 count++;
1561
1562 if (eol != EOL_NONE) {
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) {
1570 int fd;
1571
1572 fd = fileno(f);
1573 if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
1574 * and don't call isatty() on an invalid fd */
1575 flags |= READ_LINE_NOT_A_TTY;
1576 else
1577 flags |= isatty_safe(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
1578 }
1579 if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
1580 break;
1581 }
1582
1583 if (eol != EOL_NONE) {
1584 previous_eol |= eol;
1585 continue;
1586 }
1587
1588 if (ret) {
1589 if (!GREEDY_REALLOC(buffer, n + 2))
1590 return -ENOMEM;
1591
1592 buffer[n] = c;
1593 }
1594
1595 n++;
1596 }
1597 }
1598
1599 if (ret) {
1600 buffer[n] = 0;
1601
1602 *ret = TAKE_PTR(buffer);
1603 }
1604
1605 return (int) count;
1606 }
1607
1608 int read_stripped_line(FILE *f, size_t limit, char **ret) {
1609 _cleanup_free_ char *s = NULL;
1610 int r, k;
1611
1612 assert(f);
1613
1614 r = read_line(f, limit, ret ? &s : NULL);
1615 if (r < 0)
1616 return r;
1617
1618 if (ret) {
1619 const char *p = strstrip(s);
1620 if (p == s)
1621 *ret = TAKE_PTR(s);
1622 else {
1623 k = strdup_to(ret, p);
1624 if (k < 0)
1625 return k;
1626 }
1627 }
1628
1629 return r > 0; /* Return 1 if something was read. */
1630 }
1631
1632 int safe_fgetc(FILE *f, char *ret) {
1633 int k;
1634
1635 assert(f);
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;
1642 k = fgetc(f);
1643 if (k == EOF) {
1644 if (ferror(f))
1645 return errno_or_else(EIO);
1646
1647 if (ret)
1648 *ret = 0;
1649
1650 return 0;
1651 }
1652
1653 if (ret)
1654 *ret = k;
1655
1656 return 1;
1657 }
1658
1659 int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line) {
1660 struct stat _st;
1661
1662 if (!filename)
1663 return 0;
1664
1665 if (!st) {
1666 if (stat(filename, &_st) < 0)
1667 return -errno;
1668 st = &_st;
1669 }
1670
1671 if ((st->st_mode & S_IRWXO) == 0)
1672 return 0;
1673
1674 if (unit)
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.",
1680 filename, st->st_mode & 07777);
1681 return 0;
1682 }
1683
1684 int write_data_file_atomic_at(
1685 int dir_fd,
1686 const char *path,
1687 const struct iovec *iovec,
1688 WriteDataFileFlags flags) {
1689
1690 int r;
1691
1692 assert(wildcard_fd_is_valid(dir_fd));
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;
1698 r = path_split_prefix_filename(path, &dn, &fn);
1699 if (IN_SET(r, -EADDRNOTAVAIL, O_DIRECTORY))
1700 return -EISDIR; /* path refers to "." or "/" (which are dirs, which we cannot write), or is suffixed with "/" */
1701 if (r < 0)
1702 return r;
1703
1704 _cleanup_close_ int mfd = -EBADF;
1705 if (dn) {
1706 /* If there's a directory component, readjust our position */
1707 r = chaseat(XAT_FDROOT,
1708 dir_fd,
1709 dn,
1710 FLAGS_SET(flags, WRITE_DATA_FILE_MKDIR_0755) ? CHASE_MKDIR_0755 : 0,
1711 /* ret_path= */ NULL,
1712 &mfd);
1713 if (r < 0)
1714 return r;
1715
1716 dir_fd = mfd;
1717 }
1718
1719 _cleanup_free_ char *t = NULL;
1720 _cleanup_close_ int fd = open_tmpfile_linkable_at(dir_fd, fn, O_WRONLY|O_CLOEXEC, &t);
1721 if (fd < 0)
1722 return fd;
1723
1724 CLEANUP_TMPFILE_AT(dir_fd, t);
1725
1726 if (iovec_is_set(iovec)) {
1727 r = loop_write(fd, iovec->iov_base, iovec->iov_len);
1728 if (r < 0)
1729 return r;
1730 }
1731
1732 r = fchmod_umask(fd, FLAGS_SET(flags, WRITE_DATA_FILE_MODE_0400) ? 0400 : 0644);
1733 if (r < 0)
1734 return r;
1735
1736 r = link_tmpfile_at(fd, dir_fd, t, fn, LINK_TMPFILE_REPLACE);
1737 if (r < 0)
1738 return r;
1739
1740 t = mfree(t); /* disarm CLEANUP_TMPFILE_AT */
1741 return 0;
1742 }