]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fileio.c
fileio: let's minimize 'count' inc/dec calls
[thirdparty/systemd.git] / src / basic / fileio.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <ctype.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <stdarg.h>
8 #include <stdint.h>
9 #include <stdio_ext.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include "alloc-util.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "fs-util.h"
20 #include "log.h"
21 #include "macro.h"
22 #include "missing.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "stdio-util.h"
26 #include "string-util.h"
27 #include "tmpfile-util.h"
28
29 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
30
31 int write_string_stream_ts(
32 FILE *f,
33 const char *line,
34 WriteStringFileFlags flags,
35 struct timespec *ts) {
36
37 bool needs_nl;
38 int r;
39
40 assert(f);
41 assert(line);
42
43 if (ferror(f))
44 return -EIO;
45
46 needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
47
48 if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
49 /* If STDIO buffering was disabled, then let's append the newline character to the string itself, so
50 * that the write goes out in one go, instead of two */
51
52 line = strjoina(line, "\n");
53 needs_nl = false;
54 }
55
56 if (fputs(line, f) == EOF)
57 return -errno;
58
59 if (needs_nl)
60 if (fputc('\n', f) == EOF)
61 return -errno;
62
63 if (flags & WRITE_STRING_FILE_SYNC)
64 r = fflush_sync_and_check(f);
65 else
66 r = fflush_and_check(f);
67 if (r < 0)
68 return r;
69
70 if (ts) {
71 struct timespec twice[2] = {*ts, *ts};
72
73 if (futimens(fileno(f), twice) < 0)
74 return -errno;
75 }
76
77 return 0;
78 }
79
80 static int write_string_file_atomic(
81 const char *fn,
82 const char *line,
83 WriteStringFileFlags flags,
84 struct timespec *ts) {
85
86 _cleanup_fclose_ FILE *f = NULL;
87 _cleanup_free_ char *p = NULL;
88 int r;
89
90 assert(fn);
91 assert(line);
92
93 r = fopen_temporary(fn, &f, &p);
94 if (r < 0)
95 return r;
96
97 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
98 (void) fchmod_umask(fileno(f), 0644);
99
100 r = write_string_stream_ts(f, line, flags, ts);
101 if (r < 0)
102 goto fail;
103
104 if (rename(p, fn) < 0) {
105 r = -errno;
106 goto fail;
107 }
108
109 return 0;
110
111 fail:
112 (void) unlink(p);
113 return r;
114 }
115
116 int write_string_file_ts(
117 const char *fn,
118 const char *line,
119 WriteStringFileFlags flags,
120 struct timespec *ts) {
121
122 _cleanup_fclose_ FILE *f = NULL;
123 int q, r;
124
125 assert(fn);
126 assert(line);
127
128 /* We don't know how to verify whether the file contents was already on-disk. */
129 assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
130
131 if (flags & WRITE_STRING_FILE_ATOMIC) {
132 assert(flags & WRITE_STRING_FILE_CREATE);
133
134 r = write_string_file_atomic(fn, line, flags, ts);
135 if (r < 0)
136 goto fail;
137
138 return r;
139 } else
140 assert(!ts);
141
142 if (flags & WRITE_STRING_FILE_CREATE) {
143 f = fopen(fn, "we");
144 if (!f) {
145 r = -errno;
146 goto fail;
147 }
148 } else {
149 int fd;
150
151 /* We manually build our own version of fopen(..., "we") that
152 * works without O_CREAT */
153 fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY | ((flags & WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0));
154 if (fd < 0) {
155 r = -errno;
156 goto fail;
157 }
158
159 f = fdopen(fd, "w");
160 if (!f) {
161 r = -errno;
162 safe_close(fd);
163 goto fail;
164 }
165 }
166
167 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
168
169 if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
170 setvbuf(f, NULL, _IONBF, 0);
171
172 r = write_string_stream_ts(f, line, flags, ts);
173 if (r < 0)
174 goto fail;
175
176 return 0;
177
178 fail:
179 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
180 return r;
181
182 f = safe_fclose(f);
183
184 /* OK, the operation failed, but let's see if the right
185 * contents in place already. If so, eat up the error. */
186
187 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
188 if (q <= 0)
189 return r;
190
191 return 0;
192 }
193
194 int write_string_filef(
195 const char *fn,
196 WriteStringFileFlags flags,
197 const char *format, ...) {
198
199 _cleanup_free_ char *p = NULL;
200 va_list ap;
201 int r;
202
203 va_start(ap, format);
204 r = vasprintf(&p, format, ap);
205 va_end(ap);
206
207 if (r < 0)
208 return -ENOMEM;
209
210 return write_string_file(fn, p, flags);
211 }
212
213 int read_one_line_file(const char *fn, char **line) {
214 _cleanup_fclose_ FILE *f = NULL;
215 int r;
216
217 assert(fn);
218 assert(line);
219
220 f = fopen(fn, "re");
221 if (!f)
222 return -errno;
223
224 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
225
226 r = read_line(f, LONG_LINE_MAX, line);
227 return r < 0 ? r : 0;
228 }
229
230 int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
231 _cleanup_fclose_ FILE *f = NULL;
232 _cleanup_free_ char *buf = NULL;
233 size_t l, k;
234
235 assert(fn);
236 assert(blob);
237
238 l = strlen(blob);
239
240 if (accept_extra_nl && endswith(blob, "\n"))
241 accept_extra_nl = false;
242
243 buf = malloc(l + accept_extra_nl + 1);
244 if (!buf)
245 return -ENOMEM;
246
247 f = fopen(fn, "re");
248 if (!f)
249 return -errno;
250
251 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
252
253 /* We try to read one byte more than we need, so that we know whether we hit eof */
254 errno = 0;
255 k = fread(buf, 1, l + accept_extra_nl + 1, f);
256 if (ferror(f))
257 return errno > 0 ? -errno : -EIO;
258
259 if (k != l && k != l + accept_extra_nl)
260 return 0;
261 if (memcmp(buf, blob, l) != 0)
262 return 0;
263 if (k > l && buf[l] != '\n')
264 return 0;
265
266 return 1;
267 }
268
269 int read_full_stream(
270 FILE *f,
271 char **ret_contents,
272 size_t *ret_size) {
273
274 _cleanup_free_ char *buf = NULL;
275 struct stat st;
276 size_t n, l;
277 int fd;
278
279 assert(f);
280 assert(ret_contents);
281
282 n = LINE_MAX; /* Start size */
283
284 fd = fileno(f);
285 if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see fmemopen(), let's
286 * optimize our buffering) */
287
288 if (fstat(fileno(f), &st) < 0)
289 return -errno;
290
291 if (S_ISREG(st.st_mode)) {
292
293 /* Safety check */
294 if (st.st_size > READ_FULL_BYTES_MAX)
295 return -E2BIG;
296
297 /* Start with the right file size, but be prepared for files from /proc which generally report a file
298 * size of 0. Note that we increase the size to read here by one, so that the first read attempt
299 * already makes us notice the EOF. */
300 if (st.st_size > 0)
301 n = st.st_size + 1;
302 }
303 }
304
305 l = 0;
306 for (;;) {
307 char *t;
308 size_t k;
309
310 t = realloc(buf, n + 1);
311 if (!t)
312 return -ENOMEM;
313
314 buf = t;
315 errno = 0;
316 k = fread(buf + l, 1, n - l, f);
317 if (k > 0)
318 l += k;
319
320 if (ferror(f))
321 return errno > 0 ? -errno : -EIO;
322
323 if (feof(f))
324 break;
325
326 /* We aren't expecting fread() to return a short read outside
327 * of (error && eof), assert buffer is full and enlarge buffer.
328 */
329 assert(l == n);
330
331 /* Safety check */
332 if (n >= READ_FULL_BYTES_MAX)
333 return -E2BIG;
334
335 n = MIN(n * 2, READ_FULL_BYTES_MAX);
336 }
337
338 if (!ret_size) {
339 /* Safety check: if the caller doesn't want to know the size of what we just read it will rely on the
340 * trailing NUL byte. But if there's an embedded NUL byte, then we should refuse operation as otherwise
341 * there'd be ambiguity about what we just read. */
342
343 if (memchr(buf, 0, l))
344 return -EBADMSG;
345 }
346
347 buf[l] = 0;
348 *ret_contents = TAKE_PTR(buf);
349
350 if (ret_size)
351 *ret_size = l;
352
353 return 0;
354 }
355
356 int read_full_file(const char *fn, char **contents, size_t *size) {
357 _cleanup_fclose_ FILE *f = NULL;
358
359 assert(fn);
360 assert(contents);
361
362 f = fopen(fn, "re");
363 if (!f)
364 return -errno;
365
366 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
367
368 return read_full_stream(f, contents, size);
369 }
370
371 int executable_is_script(const char *path, char **interpreter) {
372 _cleanup_free_ char *line = NULL;
373 size_t len;
374 char *ans;
375 int r;
376
377 assert(path);
378
379 r = read_one_line_file(path, &line);
380 if (r == -ENOBUFS) /* First line overly long? if so, then it's not a script */
381 return 0;
382 if (r < 0)
383 return r;
384
385 if (!startswith(line, "#!"))
386 return 0;
387
388 ans = strstrip(line + 2);
389 len = strcspn(ans, " \t");
390
391 if (len == 0)
392 return 0;
393
394 ans = strndup(ans, len);
395 if (!ans)
396 return -ENOMEM;
397
398 *interpreter = ans;
399 return 1;
400 }
401
402 /**
403 * Retrieve one field from a file like /proc/self/status. pattern
404 * should not include whitespace or the delimiter (':'). pattern matches only
405 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
406 * zeros after the ':' will be skipped. field must be freed afterwards.
407 * terminator specifies the terminating characters of the field value (not
408 * included in the value).
409 */
410 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
411 _cleanup_free_ char *status = NULL;
412 char *t, *f;
413 size_t len;
414 int r;
415
416 assert(terminator);
417 assert(filename);
418 assert(pattern);
419 assert(field);
420
421 r = read_full_file(filename, &status, NULL);
422 if (r < 0)
423 return r;
424
425 t = status;
426
427 do {
428 bool pattern_ok;
429
430 do {
431 t = strstr(t, pattern);
432 if (!t)
433 return -ENOENT;
434
435 /* Check that pattern occurs in beginning of line. */
436 pattern_ok = (t == status || t[-1] == '\n');
437
438 t += strlen(pattern);
439
440 } while (!pattern_ok);
441
442 t += strspn(t, " \t");
443 if (!*t)
444 return -ENOENT;
445
446 } while (*t != ':');
447
448 t++;
449
450 if (*t) {
451 t += strspn(t, " \t");
452
453 /* Also skip zeros, because when this is used for
454 * capabilities, we don't want the zeros. This way the
455 * same capability set always maps to the same string,
456 * irrespective of the total capability set size. For
457 * other numbers it shouldn't matter. */
458 t += strspn(t, "0");
459 /* Back off one char if there's nothing but whitespace
460 and zeros */
461 if (!*t || isspace(*t))
462 t--;
463 }
464
465 len = strcspn(t, terminator);
466
467 f = strndup(t, len);
468 if (!f)
469 return -ENOMEM;
470
471 *field = f;
472 return 0;
473 }
474
475 DIR *xopendirat(int fd, const char *name, int flags) {
476 int nfd;
477 DIR *d;
478
479 assert(!(flags & O_CREAT));
480
481 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
482 if (nfd < 0)
483 return NULL;
484
485 d = fdopendir(nfd);
486 if (!d) {
487 safe_close(nfd);
488 return NULL;
489 }
490
491 return d;
492 }
493
494 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
495 char **i;
496
497 assert(path);
498 assert(mode);
499 assert(_f);
500
501 if (!path_strv_resolve_uniq(search, root))
502 return -ENOMEM;
503
504 STRV_FOREACH(i, search) {
505 _cleanup_free_ char *p = NULL;
506 FILE *f;
507
508 if (root)
509 p = strjoin(root, *i, "/", path);
510 else
511 p = strjoin(*i, "/", path);
512 if (!p)
513 return -ENOMEM;
514
515 f = fopen(p, mode);
516 if (f) {
517 *_f = f;
518 return 0;
519 }
520
521 if (errno != ENOENT)
522 return -errno;
523 }
524
525 return -ENOENT;
526 }
527
528 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
529 _cleanup_strv_free_ char **copy = NULL;
530
531 assert(path);
532 assert(mode);
533 assert(_f);
534
535 if (path_is_absolute(path)) {
536 FILE *f;
537
538 f = fopen(path, mode);
539 if (f) {
540 *_f = f;
541 return 0;
542 }
543
544 return -errno;
545 }
546
547 copy = strv_copy((char**) search);
548 if (!copy)
549 return -ENOMEM;
550
551 return search_and_fopen_internal(path, mode, root, copy, _f);
552 }
553
554 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
555 _cleanup_strv_free_ char **s = NULL;
556
557 if (path_is_absolute(path)) {
558 FILE *f;
559
560 f = fopen(path, mode);
561 if (f) {
562 *_f = f;
563 return 0;
564 }
565
566 return -errno;
567 }
568
569 s = strv_split_nulstr(search);
570 if (!s)
571 return -ENOMEM;
572
573 return search_and_fopen_internal(path, mode, root, s, _f);
574 }
575
576 int fflush_and_check(FILE *f) {
577 assert(f);
578
579 errno = 0;
580 fflush(f);
581
582 if (ferror(f))
583 return errno > 0 ? -errno : -EIO;
584
585 return 0;
586 }
587
588 int fflush_sync_and_check(FILE *f) {
589 int r;
590
591 assert(f);
592
593 r = fflush_and_check(f);
594 if (r < 0)
595 return r;
596
597 if (fsync(fileno(f)) < 0)
598 return -errno;
599
600 r = fsync_directory_of_file(fileno(f));
601 if (r < 0)
602 return r;
603
604 return 0;
605 }
606
607 int write_timestamp_file_atomic(const char *fn, usec_t n) {
608 char ln[DECIMAL_STR_MAX(n)+2];
609
610 /* Creates a "timestamp" file, that contains nothing but a
611 * usec_t timestamp, formatted in ASCII. */
612
613 if (n <= 0 || n >= USEC_INFINITY)
614 return -ERANGE;
615
616 xsprintf(ln, USEC_FMT "\n", n);
617
618 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
619 }
620
621 int read_timestamp_file(const char *fn, usec_t *ret) {
622 _cleanup_free_ char *ln = NULL;
623 uint64_t t;
624 int r;
625
626 r = read_one_line_file(fn, &ln);
627 if (r < 0)
628 return r;
629
630 r = safe_atou64(ln, &t);
631 if (r < 0)
632 return r;
633
634 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
635 return -ERANGE;
636
637 *ret = (usec_t) t;
638 return 0;
639 }
640
641 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
642 int r;
643
644 assert(s);
645
646 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
647 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
648 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
649 * element, but not before the first one. */
650
651 if (!f)
652 f = stdout;
653
654 if (space) {
655 if (!separator)
656 separator = " ";
657
658 if (*space) {
659 r = fputs(separator, f);
660 if (r < 0)
661 return r;
662 }
663
664 *space = true;
665 }
666
667 return fputs(s, f);
668 }
669
670 /* A bitmask of the EOL markers we know */
671 typedef enum EndOfLineMarker {
672 EOL_NONE = 0,
673 EOL_ZERO = 1 << 0, /* \0 (aka NUL) */
674 EOL_TEN = 1 << 1, /* \n (aka NL, aka LF) */
675 EOL_THIRTEEN = 1 << 2, /* \r (aka CR) */
676 } EndOfLineMarker;
677
678 static EndOfLineMarker categorize_eol(char c, ReadLineFlags flags) {
679
680 if (!IN_SET(flags, READ_LINE_ONLY_NUL)) {
681 if (c == '\n')
682 return EOL_TEN;
683 if (c == '\r')
684 return EOL_THIRTEEN;
685 }
686
687 if (c == '\0')
688 return EOL_ZERO;
689
690 return EOL_NONE;
691 }
692
693 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
694
695 int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
696 size_t n = 0, allocated = 0, count = 0;
697 _cleanup_free_ char *buffer = NULL;
698 int r;
699
700 assert(f);
701
702 /* Something like a bounded version of getline().
703 *
704 * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
705 * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
706 * endings:
707 *
708 * • \n (UNIX)
709 * • \r (old MacOS)
710 * • \0 (C strings)
711 * • \n\0
712 * • \r\0
713 * • \r\n (Windows)
714 * • \n\r
715 * • \r\n\0
716 * • \n\r\0
717 *
718 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
719 * the number of characters in the returned string). When EOF is hit, 0 is returned.
720 *
721 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
722 * delimiters. If the limit is hit we fail and return -ENOBUFS.
723 *
724 * If a line shall be skipped ret may be initialized as NULL. */
725
726 if (ret) {
727 if (!GREEDY_REALLOC(buffer, allocated, 1))
728 return -ENOMEM;
729 }
730
731 {
732 _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
733 EndOfLineMarker previous_eol = EOL_NONE;
734 flockfile(f);
735
736 for (;;) {
737 EndOfLineMarker eol;
738 char c;
739
740 if (n >= limit)
741 return -ENOBUFS;
742
743 if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
744 return -ENOBUFS;
745
746 r = safe_fgetc(f, &c);
747 if (r < 0)
748 return r;
749 if (r == 0) /* EOF is definitely EOL */
750 break;
751
752 eol = categorize_eol(c, flags);
753
754 if (FLAGS_SET(previous_eol, EOL_ZERO) ||
755 (eol == EOL_NONE && previous_eol != EOL_NONE) ||
756 (eol != EOL_NONE && (previous_eol & eol) != 0)) {
757 /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
758 * EOL marker has been seen right before? In either of these three cases we are
759 * done. But first, let's put this character back in the queue. */
760 assert_se(ungetc(c, f) != EOF);
761 break;
762 }
763
764 count++;
765
766 if (eol != EOL_NONE) {
767 previous_eol |= eol;
768 continue;
769 }
770
771 if (ret) {
772 if (!GREEDY_REALLOC(buffer, allocated, n + 2))
773 return -ENOMEM;
774
775 buffer[n] = c;
776 }
777
778 n++;
779 }
780 }
781
782 if (ret) {
783 buffer[n] = 0;
784
785 *ret = TAKE_PTR(buffer);
786 }
787
788 return (int) count;
789 }
790
791 int safe_fgetc(FILE *f, char *ret) {
792 int k;
793
794 assert(f);
795
796 /* A safer version of plain fgetc(): let's propagate the error that happened while reading as such, and
797 * separate the EOF condition from the byte read, to avoid those confusion signed/unsigned issues fgetc()
798 * has. */
799
800 errno = 0;
801 k = fgetc(f);
802 if (k == EOF) {
803 if (ferror(f))
804 return errno > 0 ? -errno : -EIO;
805
806 if (ret)
807 *ret = 0;
808
809 return 0;
810 }
811
812 if (ret)
813 *ret = k;
814
815 return 1;
816 }