]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fileio.c
udev: use typedef for struct udev_event
[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(FILE *f, char **contents, size_t *size) {
270 _cleanup_free_ char *buf = NULL;
271 struct stat st;
272 size_t n, l;
273 int fd;
274
275 assert(f);
276 assert(contents);
277
278 n = LINE_MAX;
279
280 fd = fileno(f);
281 if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see fmemopen(), let's
282 * optimize our buffering) */
283
284 if (fstat(fileno(f), &st) < 0)
285 return -errno;
286
287 if (S_ISREG(st.st_mode)) {
288
289 /* Safety check */
290 if (st.st_size > READ_FULL_BYTES_MAX)
291 return -E2BIG;
292
293 /* Start with the right file size, but be prepared for files from /proc which generally report a file
294 * size of 0. Note that we increase the size to read here by one, so that the first read attempt
295 * already makes us notice the EOF. */
296 if (st.st_size > 0)
297 n = st.st_size + 1;
298 }
299 }
300
301 l = 0;
302 for (;;) {
303 char *t;
304 size_t k;
305
306 t = realloc(buf, n + 1);
307 if (!t)
308 return -ENOMEM;
309
310 buf = t;
311 errno = 0;
312 k = fread(buf + l, 1, n - l, f);
313 if (k > 0)
314 l += k;
315
316 if (ferror(f))
317 return errno > 0 ? -errno : -EIO;
318
319 if (feof(f))
320 break;
321
322 /* We aren't expecting fread() to return a short read outside
323 * of (error && eof), assert buffer is full and enlarge buffer.
324 */
325 assert(l == n);
326
327 /* Safety check */
328 if (n >= READ_FULL_BYTES_MAX)
329 return -E2BIG;
330
331 n = MIN(n * 2, READ_FULL_BYTES_MAX);
332 }
333
334 buf[l] = 0;
335 *contents = TAKE_PTR(buf);
336
337 if (size)
338 *size = l;
339
340 return 0;
341 }
342
343 int read_full_file(const char *fn, char **contents, size_t *size) {
344 _cleanup_fclose_ FILE *f = NULL;
345
346 assert(fn);
347 assert(contents);
348
349 f = fopen(fn, "re");
350 if (!f)
351 return -errno;
352
353 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
354
355 return read_full_stream(f, contents, size);
356 }
357
358 int executable_is_script(const char *path, char **interpreter) {
359 _cleanup_free_ char *line = NULL;
360 size_t len;
361 char *ans;
362 int r;
363
364 assert(path);
365
366 r = read_one_line_file(path, &line);
367 if (r == -ENOBUFS) /* First line overly long? if so, then it's not a script */
368 return 0;
369 if (r < 0)
370 return r;
371
372 if (!startswith(line, "#!"))
373 return 0;
374
375 ans = strstrip(line + 2);
376 len = strcspn(ans, " \t");
377
378 if (len == 0)
379 return 0;
380
381 ans = strndup(ans, len);
382 if (!ans)
383 return -ENOMEM;
384
385 *interpreter = ans;
386 return 1;
387 }
388
389 /**
390 * Retrieve one field from a file like /proc/self/status. pattern
391 * should not include whitespace or the delimiter (':'). pattern matches only
392 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
393 * zeros after the ':' will be skipped. field must be freed afterwards.
394 * terminator specifies the terminating characters of the field value (not
395 * included in the value).
396 */
397 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
398 _cleanup_free_ char *status = NULL;
399 char *t, *f;
400 size_t len;
401 int r;
402
403 assert(terminator);
404 assert(filename);
405 assert(pattern);
406 assert(field);
407
408 r = read_full_file(filename, &status, NULL);
409 if (r < 0)
410 return r;
411
412 t = status;
413
414 do {
415 bool pattern_ok;
416
417 do {
418 t = strstr(t, pattern);
419 if (!t)
420 return -ENOENT;
421
422 /* Check that pattern occurs in beginning of line. */
423 pattern_ok = (t == status || t[-1] == '\n');
424
425 t += strlen(pattern);
426
427 } while (!pattern_ok);
428
429 t += strspn(t, " \t");
430 if (!*t)
431 return -ENOENT;
432
433 } while (*t != ':');
434
435 t++;
436
437 if (*t) {
438 t += strspn(t, " \t");
439
440 /* Also skip zeros, because when this is used for
441 * capabilities, we don't want the zeros. This way the
442 * same capability set always maps to the same string,
443 * irrespective of the total capability set size. For
444 * other numbers it shouldn't matter. */
445 t += strspn(t, "0");
446 /* Back off one char if there's nothing but whitespace
447 and zeros */
448 if (!*t || isspace(*t))
449 t--;
450 }
451
452 len = strcspn(t, terminator);
453
454 f = strndup(t, len);
455 if (!f)
456 return -ENOMEM;
457
458 *field = f;
459 return 0;
460 }
461
462 DIR *xopendirat(int fd, const char *name, int flags) {
463 int nfd;
464 DIR *d;
465
466 assert(!(flags & O_CREAT));
467
468 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
469 if (nfd < 0)
470 return NULL;
471
472 d = fdopendir(nfd);
473 if (!d) {
474 safe_close(nfd);
475 return NULL;
476 }
477
478 return d;
479 }
480
481 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
482 char **i;
483
484 assert(path);
485 assert(mode);
486 assert(_f);
487
488 if (!path_strv_resolve_uniq(search, root))
489 return -ENOMEM;
490
491 STRV_FOREACH(i, search) {
492 _cleanup_free_ char *p = NULL;
493 FILE *f;
494
495 if (root)
496 p = strjoin(root, *i, "/", path);
497 else
498 p = strjoin(*i, "/", path);
499 if (!p)
500 return -ENOMEM;
501
502 f = fopen(p, mode);
503 if (f) {
504 *_f = f;
505 return 0;
506 }
507
508 if (errno != ENOENT)
509 return -errno;
510 }
511
512 return -ENOENT;
513 }
514
515 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
516 _cleanup_strv_free_ char **copy = NULL;
517
518 assert(path);
519 assert(mode);
520 assert(_f);
521
522 if (path_is_absolute(path)) {
523 FILE *f;
524
525 f = fopen(path, mode);
526 if (f) {
527 *_f = f;
528 return 0;
529 }
530
531 return -errno;
532 }
533
534 copy = strv_copy((char**) search);
535 if (!copy)
536 return -ENOMEM;
537
538 return search_and_fopen_internal(path, mode, root, copy, _f);
539 }
540
541 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
542 _cleanup_strv_free_ char **s = NULL;
543
544 if (path_is_absolute(path)) {
545 FILE *f;
546
547 f = fopen(path, mode);
548 if (f) {
549 *_f = f;
550 return 0;
551 }
552
553 return -errno;
554 }
555
556 s = strv_split_nulstr(search);
557 if (!s)
558 return -ENOMEM;
559
560 return search_and_fopen_internal(path, mode, root, s, _f);
561 }
562
563 int fflush_and_check(FILE *f) {
564 assert(f);
565
566 errno = 0;
567 fflush(f);
568
569 if (ferror(f))
570 return errno > 0 ? -errno : -EIO;
571
572 return 0;
573 }
574
575 int fflush_sync_and_check(FILE *f) {
576 int r;
577
578 assert(f);
579
580 r = fflush_and_check(f);
581 if (r < 0)
582 return r;
583
584 if (fsync(fileno(f)) < 0)
585 return -errno;
586
587 r = fsync_directory_of_file(fileno(f));
588 if (r < 0)
589 return r;
590
591 return 0;
592 }
593
594 int write_timestamp_file_atomic(const char *fn, usec_t n) {
595 char ln[DECIMAL_STR_MAX(n)+2];
596
597 /* Creates a "timestamp" file, that contains nothing but a
598 * usec_t timestamp, formatted in ASCII. */
599
600 if (n <= 0 || n >= USEC_INFINITY)
601 return -ERANGE;
602
603 xsprintf(ln, USEC_FMT "\n", n);
604
605 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
606 }
607
608 int read_timestamp_file(const char *fn, usec_t *ret) {
609 _cleanup_free_ char *ln = NULL;
610 uint64_t t;
611 int r;
612
613 r = read_one_line_file(fn, &ln);
614 if (r < 0)
615 return r;
616
617 r = safe_atou64(ln, &t);
618 if (r < 0)
619 return r;
620
621 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
622 return -ERANGE;
623
624 *ret = (usec_t) t;
625 return 0;
626 }
627
628 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
629 int r;
630
631 assert(s);
632
633 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
634 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
635 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
636 * element, but not before the first one. */
637
638 if (!f)
639 f = stdout;
640
641 if (space) {
642 if (!separator)
643 separator = " ";
644
645 if (*space) {
646 r = fputs(separator, f);
647 if (r < 0)
648 return r;
649 }
650
651 *space = true;
652 }
653
654 return fputs(s, f);
655 }
656
657 int read_nul_string(FILE *f, char **ret) {
658 _cleanup_free_ char *x = NULL;
659 size_t allocated = 0, n = 0;
660
661 assert(f);
662 assert(ret);
663
664 /* Reads a NUL-terminated string from the specified file. */
665
666 for (;;) {
667 int c;
668
669 if (!GREEDY_REALLOC(x, allocated, n+2))
670 return -ENOMEM;
671
672 c = fgetc(f);
673 if (c == 0) /* Terminate at NUL byte */
674 break;
675 if (c == EOF) {
676 if (ferror(f))
677 return -errno;
678 break; /* Terminate at EOF */
679 }
680
681 x[n++] = (char) c;
682 }
683
684 if (x)
685 x[n] = 0;
686 else {
687 x = new0(char, 1);
688 if (!x)
689 return -ENOMEM;
690 }
691
692 *ret = TAKE_PTR(x);
693
694 return 0;
695 }
696
697 /* A bitmask of the EOL markers we know */
698 typedef enum EndOfLineMarker {
699 EOL_NONE = 0,
700 EOL_ZERO = 1 << 0, /* \0 (aka NUL) */
701 EOL_TEN = 1 << 1, /* \n (aka NL, aka LF) */
702 EOL_THIRTEEN = 1 << 2, /* \r (aka CR) */
703 } EndOfLineMarker;
704
705 static EndOfLineMarker categorize_eol(char c) {
706 if (c == '\n')
707 return EOL_TEN;
708 if (c == '\r')
709 return EOL_THIRTEEN;
710 if (c == '\0')
711 return EOL_ZERO;
712
713 return EOL_NONE;
714 }
715
716 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
717
718 int read_line(FILE *f, size_t limit, char **ret) {
719 size_t n = 0, allocated = 0, count = 0;
720 _cleanup_free_ char *buffer = NULL;
721
722 assert(f);
723
724 /* Something like a bounded version of getline().
725 *
726 * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
727 * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
728 * endings:
729 *
730 * • \n (UNIX)
731 * • \r (old MacOS)
732 * • \0 (C strings)
733 * • \n\0
734 * • \r\0
735 * • \r\n (Windows)
736 * • \n\r
737 * • \r\n\0
738 * • \n\r\0
739 *
740 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
741 * the number of characters in the returned string). When EOF is hit, 0 is returned.
742 *
743 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
744 * delimiters. If the limit is hit we fail and return -ENOBUFS.
745 *
746 * If a line shall be skipped ret may be initialized as NULL. */
747
748 if (ret) {
749 if (!GREEDY_REALLOC(buffer, allocated, 1))
750 return -ENOMEM;
751 }
752
753 {
754 _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
755 EndOfLineMarker previous_eol = EOL_NONE;
756 flockfile(f);
757
758 for (;;) {
759 EndOfLineMarker eol;
760 int c;
761
762 if (n >= limit)
763 return -ENOBUFS;
764
765 errno = 0;
766 c = fgetc_unlocked(f);
767 if (c == EOF) {
768 /* if we read an error, and have no data to return, then propagate the error */
769 if (ferror_unlocked(f) && n == 0)
770 return errno > 0 ? -errno : -EIO;
771
772 /* EOF is line ending too. */
773 break;
774 }
775
776 count++;
777
778 eol = categorize_eol(c);
779
780 if (FLAGS_SET(previous_eol, EOL_ZERO) ||
781 (eol == EOL_NONE && previous_eol != EOL_NONE) ||
782 (eol != EOL_NONE && (previous_eol & eol) != 0)) {
783 /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
784 * EOL marker has been seen right before? In either of these three cases we are
785 * done. But first, let's put this character back in the queue. */
786 assert_se(ungetc(c, f) != EOF);
787 count--;
788 break;
789 }
790
791 if (eol != EOL_NONE) {
792 previous_eol |= eol;
793 continue;
794 }
795
796 if (ret) {
797 if (!GREEDY_REALLOC(buffer, allocated, n + 2))
798 return -ENOMEM;
799
800 buffer[n] = (char) c;
801 }
802
803 n++;
804 }
805 }
806
807 if (ret) {
808 buffer[n] = 0;
809
810 *ret = TAKE_PTR(buffer);
811 }
812
813 return (int) count;
814 }