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