]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fileio.c
Merge pull request #6894 from poettering/read-full-file-optimize
[thirdparty/systemd.git] / src / basic / fileio.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include "alloc-util.h"
32 #include "ctype.h"
33 #include "env-util.h"
34 #include "escape.h"
35 #include "fd-util.h"
36 #include "fileio.h"
37 #include "fs-util.h"
38 #include "hexdecoct.h"
39 #include "log.h"
40 #include "macro.h"
41 #include "missing.h"
42 #include "parse-util.h"
43 #include "path-util.h"
44 #include "process-util.h"
45 #include "random-util.h"
46 #include "stdio-util.h"
47 #include "string-util.h"
48 #include "strv.h"
49 #include "time-util.h"
50 #include "umask-util.h"
51 #include "utf8.h"
52
53 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
54
55 int write_string_stream_ts(
56 FILE *f,
57 const char *line,
58 WriteStringFileFlags flags,
59 struct timespec *ts) {
60
61 assert(f);
62 assert(line);
63
64 fputs(line, f);
65 if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"))
66 fputc('\n', f);
67
68 if (ts) {
69 struct timespec twice[2] = {*ts, *ts};
70
71 if (futimens(fileno(f), twice) < 0)
72 return -errno;
73 }
74
75 if (flags & WRITE_STRING_FILE_SYNC)
76 return fflush_sync_and_check(f);
77 else
78 return fflush_and_check(f);
79 }
80
81 static int write_string_file_atomic(
82 const char *fn,
83 const char *line,
84 WriteStringFileFlags flags,
85 struct timespec *ts) {
86
87 _cleanup_fclose_ FILE *f = NULL;
88 _cleanup_free_ char *p = NULL;
89 int r;
90
91 assert(fn);
92 assert(line);
93
94 r = fopen_temporary(fn, &f, &p);
95 if (r < 0)
96 return r;
97
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 == NULL);
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);
154 if (fd < 0) {
155 r = -errno;
156 goto fail;
157 }
158
159 f = fdopen(fd, "we");
160 if (!f) {
161 r = -errno;
162 safe_close(fd);
163 goto fail;
164 }
165 }
166
167 r = write_string_stream_ts(f, line, flags, ts);
168 if (r < 0)
169 goto fail;
170
171 return 0;
172
173 fail:
174 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
175 return r;
176
177 f = safe_fclose(f);
178
179 /* OK, the operation failed, but let's see if the right
180 * contents in place already. If so, eat up the error. */
181
182 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
183 if (q <= 0)
184 return r;
185
186 return 0;
187 }
188
189 int read_one_line_file(const char *fn, char **line) {
190 _cleanup_fclose_ FILE *f = NULL;
191 char t[LINE_MAX], *c;
192
193 assert(fn);
194 assert(line);
195
196 f = fopen(fn, "re");
197 if (!f)
198 return -errno;
199
200 if (!fgets(t, sizeof(t), f)) {
201
202 if (ferror(f))
203 return errno > 0 ? -errno : -EIO;
204
205 t[0] = 0;
206 }
207
208 c = strdup(t);
209 if (!c)
210 return -ENOMEM;
211 truncate_nl(c);
212
213 *line = c;
214 return 0;
215 }
216
217 int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
218 _cleanup_fclose_ FILE *f = NULL;
219 _cleanup_free_ char *buf = NULL;
220 size_t l, k;
221
222 assert(fn);
223 assert(blob);
224
225 l = strlen(blob);
226
227 if (accept_extra_nl && endswith(blob, "\n"))
228 accept_extra_nl = false;
229
230 buf = malloc(l + accept_extra_nl + 1);
231 if (!buf)
232 return -ENOMEM;
233
234 f = fopen(fn, "re");
235 if (!f)
236 return -errno;
237
238 /* We try to read one byte more than we need, so that we know whether we hit eof */
239 errno = 0;
240 k = fread(buf, 1, l + accept_extra_nl + 1, f);
241 if (ferror(f))
242 return errno > 0 ? -errno : -EIO;
243
244 if (k != l && k != l + accept_extra_nl)
245 return 0;
246 if (memcmp(buf, blob, l) != 0)
247 return 0;
248 if (k > l && buf[l] != '\n')
249 return 0;
250
251 return 1;
252 }
253
254 int read_full_stream(FILE *f, char **contents, size_t *size) {
255 size_t n, l;
256 _cleanup_free_ char *buf = NULL;
257 struct stat st;
258
259 assert(f);
260 assert(contents);
261
262 if (fstat(fileno(f), &st) < 0)
263 return -errno;
264
265 n = LINE_MAX;
266
267 if (S_ISREG(st.st_mode)) {
268
269 /* Safety check */
270 if (st.st_size > READ_FULL_BYTES_MAX)
271 return -E2BIG;
272
273 /* Start with the right file size, but be prepared for files from /proc which generally report a file
274 * size of 0. Note that we increase the size to read here by one, so that the first read attempt
275 * already makes us notice the EOF. */
276 if (st.st_size > 0)
277 n = st.st_size + 1;
278 }
279
280 l = 0;
281 for (;;) {
282 char *t;
283 size_t k;
284
285 t = realloc(buf, n + 1);
286 if (!t)
287 return -ENOMEM;
288
289 buf = t;
290 errno = 0;
291 k = fread(buf + l, 1, n - l, f);
292 if (k > 0)
293 l += k;
294
295 if (ferror(f))
296 return errno > 0 ? -errno : -EIO;
297
298 if (feof(f))
299 break;
300
301 /* We aren't expecting fread() to return a short read outside
302 * of (error && eof), assert buffer is full and enlarge buffer.
303 */
304 assert(l == n);
305
306 /* Safety check */
307 if (n >= READ_FULL_BYTES_MAX)
308 return -E2BIG;
309
310 n = MIN(n * 2, READ_FULL_BYTES_MAX);
311 }
312
313 buf[l] = 0;
314 *contents = buf;
315 buf = NULL; /* do not free */
316
317 if (size)
318 *size = l;
319
320 return 0;
321 }
322
323 int read_full_file(const char *fn, char **contents, size_t *size) {
324 _cleanup_fclose_ FILE *f = NULL;
325
326 assert(fn);
327 assert(contents);
328
329 f = fopen(fn, "re");
330 if (!f)
331 return -errno;
332
333 return read_full_stream(f, contents, size);
334 }
335
336 static int parse_env_file_internal(
337 FILE *f,
338 const char *fname,
339 const char *newline,
340 int (*push) (const char *filename, unsigned line,
341 const char *key, char *value, void *userdata, int *n_pushed),
342 void *userdata,
343 int *n_pushed) {
344
345 _cleanup_free_ char *contents = NULL, *key = NULL;
346 size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_value_whitespace = (size_t) -1, last_key_whitespace = (size_t) -1;
347 char *p, *value = NULL;
348 int r;
349 unsigned line = 1;
350
351 enum {
352 PRE_KEY,
353 KEY,
354 PRE_VALUE,
355 VALUE,
356 VALUE_ESCAPE,
357 SINGLE_QUOTE_VALUE,
358 SINGLE_QUOTE_VALUE_ESCAPE,
359 DOUBLE_QUOTE_VALUE,
360 DOUBLE_QUOTE_VALUE_ESCAPE,
361 COMMENT,
362 COMMENT_ESCAPE
363 } state = PRE_KEY;
364
365 assert(newline);
366
367 if (f)
368 r = read_full_stream(f, &contents, NULL);
369 else
370 r = read_full_file(fname, &contents, NULL);
371 if (r < 0)
372 return r;
373
374 for (p = contents; *p; p++) {
375 char c = *p;
376
377 switch (state) {
378
379 case PRE_KEY:
380 if (strchr(COMMENTS, c))
381 state = COMMENT;
382 else if (!strchr(WHITESPACE, c)) {
383 state = KEY;
384 last_key_whitespace = (size_t) -1;
385
386 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
387 r = -ENOMEM;
388 goto fail;
389 }
390
391 key[n_key++] = c;
392 }
393 break;
394
395 case KEY:
396 if (strchr(newline, c)) {
397 state = PRE_KEY;
398 line++;
399 n_key = 0;
400 } else if (c == '=') {
401 state = PRE_VALUE;
402 last_value_whitespace = (size_t) -1;
403 } else {
404 if (!strchr(WHITESPACE, c))
405 last_key_whitespace = (size_t) -1;
406 else if (last_key_whitespace == (size_t) -1)
407 last_key_whitespace = n_key;
408
409 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
410 r = -ENOMEM;
411 goto fail;
412 }
413
414 key[n_key++] = c;
415 }
416
417 break;
418
419 case PRE_VALUE:
420 if (strchr(newline, c)) {
421 state = PRE_KEY;
422 line++;
423 key[n_key] = 0;
424
425 if (value)
426 value[n_value] = 0;
427
428 /* strip trailing whitespace from key */
429 if (last_key_whitespace != (size_t) -1)
430 key[last_key_whitespace] = 0;
431
432 r = push(fname, line, key, value, userdata, n_pushed);
433 if (r < 0)
434 goto fail;
435
436 n_key = 0;
437 value = NULL;
438 value_alloc = n_value = 0;
439
440 } else if (c == '\'')
441 state = SINGLE_QUOTE_VALUE;
442 else if (c == '\"')
443 state = DOUBLE_QUOTE_VALUE;
444 else if (c == '\\')
445 state = VALUE_ESCAPE;
446 else if (!strchr(WHITESPACE, c)) {
447 state = VALUE;
448
449 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
450 r = -ENOMEM;
451 goto fail;
452 }
453
454 value[n_value++] = c;
455 }
456
457 break;
458
459 case VALUE:
460 if (strchr(newline, c)) {
461 state = PRE_KEY;
462 line++;
463
464 key[n_key] = 0;
465
466 if (value)
467 value[n_value] = 0;
468
469 /* Chomp off trailing whitespace from value */
470 if (last_value_whitespace != (size_t) -1)
471 value[last_value_whitespace] = 0;
472
473 /* strip trailing whitespace from key */
474 if (last_key_whitespace != (size_t) -1)
475 key[last_key_whitespace] = 0;
476
477 r = push(fname, line, key, value, userdata, n_pushed);
478 if (r < 0)
479 goto fail;
480
481 n_key = 0;
482 value = NULL;
483 value_alloc = n_value = 0;
484
485 } else if (c == '\\') {
486 state = VALUE_ESCAPE;
487 last_value_whitespace = (size_t) -1;
488 } else {
489 if (!strchr(WHITESPACE, c))
490 last_value_whitespace = (size_t) -1;
491 else if (last_value_whitespace == (size_t) -1)
492 last_value_whitespace = n_value;
493
494 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
495 r = -ENOMEM;
496 goto fail;
497 }
498
499 value[n_value++] = c;
500 }
501
502 break;
503
504 case VALUE_ESCAPE:
505 state = VALUE;
506
507 if (!strchr(newline, c)) {
508 /* Escaped newlines we eat up entirely */
509 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
510 r = -ENOMEM;
511 goto fail;
512 }
513
514 value[n_value++] = c;
515 }
516 break;
517
518 case SINGLE_QUOTE_VALUE:
519 if (c == '\'')
520 state = PRE_VALUE;
521 else if (c == '\\')
522 state = SINGLE_QUOTE_VALUE_ESCAPE;
523 else {
524 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
525 r = -ENOMEM;
526 goto fail;
527 }
528
529 value[n_value++] = c;
530 }
531
532 break;
533
534 case SINGLE_QUOTE_VALUE_ESCAPE:
535 state = SINGLE_QUOTE_VALUE;
536
537 if (!strchr(newline, c)) {
538 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
539 r = -ENOMEM;
540 goto fail;
541 }
542
543 value[n_value++] = c;
544 }
545 break;
546
547 case DOUBLE_QUOTE_VALUE:
548 if (c == '\"')
549 state = PRE_VALUE;
550 else if (c == '\\')
551 state = DOUBLE_QUOTE_VALUE_ESCAPE;
552 else {
553 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
554 r = -ENOMEM;
555 goto fail;
556 }
557
558 value[n_value++] = c;
559 }
560
561 break;
562
563 case DOUBLE_QUOTE_VALUE_ESCAPE:
564 state = DOUBLE_QUOTE_VALUE;
565
566 if (!strchr(newline, c)) {
567 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
568 r = -ENOMEM;
569 goto fail;
570 }
571
572 value[n_value++] = c;
573 }
574 break;
575
576 case COMMENT:
577 if (c == '\\')
578 state = COMMENT_ESCAPE;
579 else if (strchr(newline, c)) {
580 state = PRE_KEY;
581 line++;
582 }
583 break;
584
585 case COMMENT_ESCAPE:
586 state = COMMENT;
587 break;
588 }
589 }
590
591 if (IN_SET(state,
592 PRE_VALUE,
593 VALUE,
594 VALUE_ESCAPE,
595 SINGLE_QUOTE_VALUE,
596 SINGLE_QUOTE_VALUE_ESCAPE,
597 DOUBLE_QUOTE_VALUE,
598 DOUBLE_QUOTE_VALUE_ESCAPE)) {
599
600 key[n_key] = 0;
601
602 if (value)
603 value[n_value] = 0;
604
605 if (state == VALUE)
606 if (last_value_whitespace != (size_t) -1)
607 value[last_value_whitespace] = 0;
608
609 /* strip trailing whitespace from key */
610 if (last_key_whitespace != (size_t) -1)
611 key[last_key_whitespace] = 0;
612
613 r = push(fname, line, key, value, userdata, n_pushed);
614 if (r < 0)
615 goto fail;
616 }
617
618 return 0;
619
620 fail:
621 free(value);
622 return r;
623 }
624
625 static int check_utf8ness_and_warn(
626 const char *filename, unsigned line,
627 const char *key, char *value) {
628
629 if (!utf8_is_valid(key)) {
630 _cleanup_free_ char *p = NULL;
631
632 p = utf8_escape_invalid(key);
633 log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename), line, p);
634 return -EINVAL;
635 }
636
637 if (value && !utf8_is_valid(value)) {
638 _cleanup_free_ char *p = NULL;
639
640 p = utf8_escape_invalid(value);
641 log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, p);
642 return -EINVAL;
643 }
644
645 return 0;
646 }
647
648 static int parse_env_file_push(
649 const char *filename, unsigned line,
650 const char *key, char *value,
651 void *userdata,
652 int *n_pushed) {
653
654 const char *k;
655 va_list aq, *ap = userdata;
656 int r;
657
658 r = check_utf8ness_and_warn(filename, line, key, value);
659 if (r < 0)
660 return r;
661
662 va_copy(aq, *ap);
663
664 while ((k = va_arg(aq, const char *))) {
665 char **v;
666
667 v = va_arg(aq, char **);
668
669 if (streq(key, k)) {
670 va_end(aq);
671 free(*v);
672 *v = value;
673
674 if (n_pushed)
675 (*n_pushed)++;
676
677 return 1;
678 }
679 }
680
681 va_end(aq);
682 free(value);
683
684 return 0;
685 }
686
687 int parse_env_file(
688 const char *fname,
689 const char *newline, ...) {
690
691 va_list ap;
692 int r, n_pushed = 0;
693
694 if (!newline)
695 newline = NEWLINE;
696
697 va_start(ap, newline);
698 r = parse_env_file_internal(NULL, fname, newline, parse_env_file_push, &ap, &n_pushed);
699 va_end(ap);
700
701 return r < 0 ? r : n_pushed;
702 }
703
704 static int load_env_file_push(
705 const char *filename, unsigned line,
706 const char *key, char *value,
707 void *userdata,
708 int *n_pushed) {
709 char ***m = userdata;
710 char *p;
711 int r;
712
713 r = check_utf8ness_and_warn(filename, line, key, value);
714 if (r < 0)
715 return r;
716
717 p = strjoin(key, "=", value);
718 if (!p)
719 return -ENOMEM;
720
721 r = strv_env_replace(m, p);
722 if (r < 0) {
723 free(p);
724 return r;
725 }
726
727 if (n_pushed)
728 (*n_pushed)++;
729
730 free(value);
731 return 0;
732 }
733
734 int load_env_file(FILE *f, const char *fname, const char *newline, char ***rl) {
735 char **m = NULL;
736 int r;
737
738 if (!newline)
739 newline = NEWLINE;
740
741 r = parse_env_file_internal(f, fname, newline, load_env_file_push, &m, NULL);
742 if (r < 0) {
743 strv_free(m);
744 return r;
745 }
746
747 *rl = m;
748 return 0;
749 }
750
751 static int load_env_file_push_pairs(
752 const char *filename, unsigned line,
753 const char *key, char *value,
754 void *userdata,
755 int *n_pushed) {
756 char ***m = userdata;
757 int r;
758
759 r = check_utf8ness_and_warn(filename, line, key, value);
760 if (r < 0)
761 return r;
762
763 r = strv_extend(m, key);
764 if (r < 0)
765 return -ENOMEM;
766
767 if (!value) {
768 r = strv_extend(m, "");
769 if (r < 0)
770 return -ENOMEM;
771 } else {
772 r = strv_push(m, value);
773 if (r < 0)
774 return r;
775 }
776
777 if (n_pushed)
778 (*n_pushed)++;
779
780 return 0;
781 }
782
783 int load_env_file_pairs(FILE *f, const char *fname, const char *newline, char ***rl) {
784 char **m = NULL;
785 int r;
786
787 if (!newline)
788 newline = NEWLINE;
789
790 r = parse_env_file_internal(f, fname, newline, load_env_file_push_pairs, &m, NULL);
791 if (r < 0) {
792 strv_free(m);
793 return r;
794 }
795
796 *rl = m;
797 return 0;
798 }
799
800 static int merge_env_file_push(
801 const char *filename, unsigned line,
802 const char *key, char *value,
803 void *userdata,
804 int *n_pushed) {
805
806 char ***env = userdata;
807 char *expanded_value;
808
809 assert(env);
810
811 if (!value) {
812 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key);
813 return 0;
814 }
815
816 if (!env_name_is_valid(key)) {
817 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename), line, key);
818 free(value);
819 return 0;
820 }
821
822 expanded_value = replace_env(value, *env,
823 REPLACE_ENV_USE_ENVIRONMENT|
824 REPLACE_ENV_ALLOW_BRACELESS|
825 REPLACE_ENV_ALLOW_EXTENDED);
826 if (!expanded_value)
827 return -ENOMEM;
828
829 free_and_replace(value, expanded_value);
830
831 return load_env_file_push(filename, line, key, value, env, n_pushed);
832 }
833
834 int merge_env_file(
835 char ***env,
836 FILE *f,
837 const char *fname) {
838
839 /* NOTE: this function supports braceful and braceless variable expansions,
840 * plus "extended" substitutions, unlike other exported parsing functions.
841 */
842
843 return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL);
844 }
845
846 static void write_env_var(FILE *f, const char *v) {
847 const char *p;
848
849 p = strchr(v, '=');
850 if (!p) {
851 /* Fallback */
852 fputs_unlocked(v, f);
853 fputc_unlocked('\n', f);
854 return;
855 }
856
857 p++;
858 fwrite_unlocked(v, 1, p-v, f);
859
860 if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
861 fputc_unlocked('\"', f);
862
863 for (; *p; p++) {
864 if (strchr(SHELL_NEED_ESCAPE, *p))
865 fputc_unlocked('\\', f);
866
867 fputc_unlocked(*p, f);
868 }
869
870 fputc_unlocked('\"', f);
871 } else
872 fputs_unlocked(p, f);
873
874 fputc_unlocked('\n', f);
875 }
876
877 int write_env_file(const char *fname, char **l) {
878 _cleanup_fclose_ FILE *f = NULL;
879 _cleanup_free_ char *p = NULL;
880 char **i;
881 int r;
882
883 assert(fname);
884
885 r = fopen_temporary(fname, &f, &p);
886 if (r < 0)
887 return r;
888
889 fchmod_umask(fileno(f), 0644);
890
891 STRV_FOREACH(i, l)
892 write_env_var(f, *i);
893
894 r = fflush_and_check(f);
895 if (r >= 0) {
896 if (rename(p, fname) >= 0)
897 return 0;
898
899 r = -errno;
900 }
901
902 unlink(p);
903 return r;
904 }
905
906 int executable_is_script(const char *path, char **interpreter) {
907 int r;
908 _cleanup_free_ char *line = NULL;
909 int len;
910 char *ans;
911
912 assert(path);
913
914 r = read_one_line_file(path, &line);
915 if (r < 0)
916 return r;
917
918 if (!startswith(line, "#!"))
919 return 0;
920
921 ans = strstrip(line + 2);
922 len = strcspn(ans, " \t");
923
924 if (len == 0)
925 return 0;
926
927 ans = strndup(ans, len);
928 if (!ans)
929 return -ENOMEM;
930
931 *interpreter = ans;
932 return 1;
933 }
934
935 /**
936 * Retrieve one field from a file like /proc/self/status. pattern
937 * should not include whitespace or the delimiter (':'). pattern matches only
938 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
939 * zeros after the ':' will be skipped. field must be freed afterwards.
940 * terminator specifies the terminating characters of the field value (not
941 * included in the value).
942 */
943 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
944 _cleanup_free_ char *status = NULL;
945 char *t, *f;
946 size_t len;
947 int r;
948
949 assert(terminator);
950 assert(filename);
951 assert(pattern);
952 assert(field);
953
954 r = read_full_file(filename, &status, NULL);
955 if (r < 0)
956 return r;
957
958 t = status;
959
960 do {
961 bool pattern_ok;
962
963 do {
964 t = strstr(t, pattern);
965 if (!t)
966 return -ENOENT;
967
968 /* Check that pattern occurs in beginning of line. */
969 pattern_ok = (t == status || t[-1] == '\n');
970
971 t += strlen(pattern);
972
973 } while (!pattern_ok);
974
975 t += strspn(t, " \t");
976 if (!*t)
977 return -ENOENT;
978
979 } while (*t != ':');
980
981 t++;
982
983 if (*t) {
984 t += strspn(t, " \t");
985
986 /* Also skip zeros, because when this is used for
987 * capabilities, we don't want the zeros. This way the
988 * same capability set always maps to the same string,
989 * irrespective of the total capability set size. For
990 * other numbers it shouldn't matter. */
991 t += strspn(t, "0");
992 /* Back off one char if there's nothing but whitespace
993 and zeros */
994 if (!*t || isspace(*t))
995 t--;
996 }
997
998 len = strcspn(t, terminator);
999
1000 f = strndup(t, len);
1001 if (!f)
1002 return -ENOMEM;
1003
1004 *field = f;
1005 return 0;
1006 }
1007
1008 DIR *xopendirat(int fd, const char *name, int flags) {
1009 int nfd;
1010 DIR *d;
1011
1012 assert(!(flags & O_CREAT));
1013
1014 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
1015 if (nfd < 0)
1016 return NULL;
1017
1018 d = fdopendir(nfd);
1019 if (!d) {
1020 safe_close(nfd);
1021 return NULL;
1022 }
1023
1024 return d;
1025 }
1026
1027 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
1028 char **i;
1029
1030 assert(path);
1031 assert(mode);
1032 assert(_f);
1033
1034 if (!path_strv_resolve_uniq(search, root))
1035 return -ENOMEM;
1036
1037 STRV_FOREACH(i, search) {
1038 _cleanup_free_ char *p = NULL;
1039 FILE *f;
1040
1041 if (root)
1042 p = strjoin(root, *i, "/", path);
1043 else
1044 p = strjoin(*i, "/", path);
1045 if (!p)
1046 return -ENOMEM;
1047
1048 f = fopen(p, mode);
1049 if (f) {
1050 *_f = f;
1051 return 0;
1052 }
1053
1054 if (errno != ENOENT)
1055 return -errno;
1056 }
1057
1058 return -ENOENT;
1059 }
1060
1061 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
1062 _cleanup_strv_free_ char **copy = NULL;
1063
1064 assert(path);
1065 assert(mode);
1066 assert(_f);
1067
1068 if (path_is_absolute(path)) {
1069 FILE *f;
1070
1071 f = fopen(path, mode);
1072 if (f) {
1073 *_f = f;
1074 return 0;
1075 }
1076
1077 return -errno;
1078 }
1079
1080 copy = strv_copy((char**) search);
1081 if (!copy)
1082 return -ENOMEM;
1083
1084 return search_and_fopen_internal(path, mode, root, copy, _f);
1085 }
1086
1087 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
1088 _cleanup_strv_free_ char **s = NULL;
1089
1090 if (path_is_absolute(path)) {
1091 FILE *f;
1092
1093 f = fopen(path, mode);
1094 if (f) {
1095 *_f = f;
1096 return 0;
1097 }
1098
1099 return -errno;
1100 }
1101
1102 s = strv_split_nulstr(search);
1103 if (!s)
1104 return -ENOMEM;
1105
1106 return search_and_fopen_internal(path, mode, root, s, _f);
1107 }
1108
1109 int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
1110 FILE *f;
1111 char *t;
1112 int r, fd;
1113
1114 assert(path);
1115 assert(_f);
1116 assert(_temp_path);
1117
1118 r = tempfn_xxxxxx(path, NULL, &t);
1119 if (r < 0)
1120 return r;
1121
1122 fd = mkostemp_safe(t);
1123 if (fd < 0) {
1124 free(t);
1125 return -errno;
1126 }
1127
1128 f = fdopen(fd, "we");
1129 if (!f) {
1130 unlink_noerrno(t);
1131 free(t);
1132 safe_close(fd);
1133 return -errno;
1134 }
1135
1136 *_f = f;
1137 *_temp_path = t;
1138
1139 return 0;
1140 }
1141
1142 int fflush_and_check(FILE *f) {
1143 assert(f);
1144
1145 errno = 0;
1146 fflush(f);
1147
1148 if (ferror(f))
1149 return errno > 0 ? -errno : -EIO;
1150
1151 return 0;
1152 }
1153
1154 int fflush_sync_and_check(FILE *f) {
1155 int r;
1156
1157 assert(f);
1158
1159 r = fflush_and_check(f);
1160 if (r < 0)
1161 return r;
1162
1163 if (fsync(fileno(f)) < 0)
1164 return -errno;
1165
1166 return 0;
1167 }
1168
1169 /* This is much like mkostemp() but is subject to umask(). */
1170 int mkostemp_safe(char *pattern) {
1171 _cleanup_umask_ mode_t u = 0;
1172 int fd;
1173
1174 assert(pattern);
1175
1176 u = umask(077);
1177
1178 fd = mkostemp(pattern, O_CLOEXEC);
1179 if (fd < 0)
1180 return -errno;
1181
1182 return fd;
1183 }
1184
1185 int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
1186 const char *fn;
1187 char *t;
1188
1189 assert(p);
1190 assert(ret);
1191
1192 /*
1193 * Turns this:
1194 * /foo/bar/waldo
1195 *
1196 * Into this:
1197 * /foo/bar/.#<extra>waldoXXXXXX
1198 */
1199
1200 fn = basename(p);
1201 if (!filename_is_valid(fn))
1202 return -EINVAL;
1203
1204 if (extra == NULL)
1205 extra = "";
1206
1207 t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
1208 if (!t)
1209 return -ENOMEM;
1210
1211 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
1212
1213 *ret = path_kill_slashes(t);
1214 return 0;
1215 }
1216
1217 int tempfn_random(const char *p, const char *extra, char **ret) {
1218 const char *fn;
1219 char *t, *x;
1220 uint64_t u;
1221 unsigned i;
1222
1223 assert(p);
1224 assert(ret);
1225
1226 /*
1227 * Turns this:
1228 * /foo/bar/waldo
1229 *
1230 * Into this:
1231 * /foo/bar/.#<extra>waldobaa2a261115984a9
1232 */
1233
1234 fn = basename(p);
1235 if (!filename_is_valid(fn))
1236 return -EINVAL;
1237
1238 if (!extra)
1239 extra = "";
1240
1241 t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
1242 if (!t)
1243 return -ENOMEM;
1244
1245 x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
1246
1247 u = random_u64();
1248 for (i = 0; i < 16; i++) {
1249 *(x++) = hexchar(u & 0xF);
1250 u >>= 4;
1251 }
1252
1253 *x = 0;
1254
1255 *ret = path_kill_slashes(t);
1256 return 0;
1257 }
1258
1259 int tempfn_random_child(const char *p, const char *extra, char **ret) {
1260 char *t, *x;
1261 uint64_t u;
1262 unsigned i;
1263 int r;
1264
1265 assert(ret);
1266
1267 /* Turns this:
1268 * /foo/bar/waldo
1269 * Into this:
1270 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
1271 */
1272
1273 if (!p) {
1274 r = tmp_dir(&p);
1275 if (r < 0)
1276 return r;
1277 }
1278
1279 if (!extra)
1280 extra = "";
1281
1282 t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
1283 if (!t)
1284 return -ENOMEM;
1285
1286 x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
1287
1288 u = random_u64();
1289 for (i = 0; i < 16; i++) {
1290 *(x++) = hexchar(u & 0xF);
1291 u >>= 4;
1292 }
1293
1294 *x = 0;
1295
1296 *ret = path_kill_slashes(t);
1297 return 0;
1298 }
1299
1300 int write_timestamp_file_atomic(const char *fn, usec_t n) {
1301 char ln[DECIMAL_STR_MAX(n)+2];
1302
1303 /* Creates a "timestamp" file, that contains nothing but a
1304 * usec_t timestamp, formatted in ASCII. */
1305
1306 if (n <= 0 || n >= USEC_INFINITY)
1307 return -ERANGE;
1308
1309 xsprintf(ln, USEC_FMT "\n", n);
1310
1311 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1312 }
1313
1314 int read_timestamp_file(const char *fn, usec_t *ret) {
1315 _cleanup_free_ char *ln = NULL;
1316 uint64_t t;
1317 int r;
1318
1319 r = read_one_line_file(fn, &ln);
1320 if (r < 0)
1321 return r;
1322
1323 r = safe_atou64(ln, &t);
1324 if (r < 0)
1325 return r;
1326
1327 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
1328 return -ERANGE;
1329
1330 *ret = (usec_t) t;
1331 return 0;
1332 }
1333
1334 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
1335 int r;
1336
1337 assert(s);
1338
1339 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
1340 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
1341 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
1342 * element, but not before the first one. */
1343
1344 if (!f)
1345 f = stdout;
1346
1347 if (space) {
1348 if (!separator)
1349 separator = " ";
1350
1351 if (*space) {
1352 r = fputs(separator, f);
1353 if (r < 0)
1354 return r;
1355 }
1356
1357 *space = true;
1358 }
1359
1360 return fputs(s, f);
1361 }
1362
1363 int open_tmpfile_unlinkable(const char *directory, int flags) {
1364 char *p;
1365 int fd, r;
1366
1367 if (!directory) {
1368 r = tmp_dir(&directory);
1369 if (r < 0)
1370 return r;
1371 }
1372
1373 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
1374
1375 /* Try O_TMPFILE first, if it is supported */
1376 fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
1377 if (fd >= 0)
1378 return fd;
1379
1380 /* Fall back to unguessable name + unlinking */
1381 p = strjoina(directory, "/systemd-tmp-XXXXXX");
1382
1383 fd = mkostemp_safe(p);
1384 if (fd < 0)
1385 return fd;
1386
1387 (void) unlink(p);
1388
1389 return fd;
1390 }
1391
1392 int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
1393 _cleanup_free_ char *tmp = NULL;
1394 int r, fd;
1395
1396 assert(target);
1397 assert(ret_path);
1398
1399 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
1400 assert((flags & O_EXCL) == 0);
1401
1402 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
1403 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
1404 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
1405
1406 {
1407 _cleanup_free_ char *dn = NULL;
1408
1409 dn = dirname_malloc(target);
1410 if (!dn)
1411 return -ENOMEM;
1412
1413 fd = open(dn, O_TMPFILE|flags, 0640);
1414 if (fd >= 0) {
1415 *ret_path = NULL;
1416 return fd;
1417 }
1418
1419 log_debug_errno(errno, "Failed to use O_TMPFILE on %s: %m", dn);
1420 }
1421
1422 r = tempfn_random(target, NULL, &tmp);
1423 if (r < 0)
1424 return r;
1425
1426 fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
1427 if (fd < 0)
1428 return -errno;
1429
1430 *ret_path = tmp;
1431 tmp = NULL;
1432
1433 return fd;
1434 }
1435
1436 int open_serialization_fd(const char *ident) {
1437 int fd = -1;
1438
1439 fd = memfd_create(ident, MFD_CLOEXEC);
1440 if (fd < 0) {
1441 const char *path;
1442
1443 path = getpid_cached() == 1 ? "/run/systemd" : "/tmp";
1444 fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
1445 if (fd < 0)
1446 return fd;
1447
1448 log_debug("Serializing %s to %s.", ident, path);
1449 } else
1450 log_debug("Serializing %s to memfd.", ident);
1451
1452 return fd;
1453 }
1454
1455 int link_tmpfile(int fd, const char *path, const char *target) {
1456
1457 assert(fd >= 0);
1458 assert(target);
1459
1460 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
1461 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
1462 * on the directory, and renameat2() is used instead.
1463 *
1464 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
1465 * operation currently (renameat2() does), and there is no nice way to emulate this. */
1466
1467 if (path) {
1468 if (rename_noreplace(AT_FDCWD, path, AT_FDCWD, target) < 0)
1469 return -errno;
1470 } else {
1471 char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
1472
1473 xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
1474
1475 if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
1476 return -errno;
1477 }
1478
1479 return 0;
1480 }
1481
1482 int read_nul_string(FILE *f, char **ret) {
1483 _cleanup_free_ char *x = NULL;
1484 size_t allocated = 0, n = 0;
1485
1486 assert(f);
1487 assert(ret);
1488
1489 /* Reads a NUL-terminated string from the specified file. */
1490
1491 for (;;) {
1492 int c;
1493
1494 if (!GREEDY_REALLOC(x, allocated, n+2))
1495 return -ENOMEM;
1496
1497 c = fgetc(f);
1498 if (c == 0) /* Terminate at NUL byte */
1499 break;
1500 if (c == EOF) {
1501 if (ferror(f))
1502 return -errno;
1503 break; /* Terminate at EOF */
1504 }
1505
1506 x[n++] = (char) c;
1507 }
1508
1509 if (x)
1510 x[n] = 0;
1511 else {
1512 x = new0(char, 1);
1513 if (!x)
1514 return -ENOMEM;
1515 }
1516
1517 *ret = x;
1518 x = NULL;
1519
1520 return 0;
1521 }
1522
1523 int mkdtemp_malloc(const char *template, char **ret) {
1524 char *p;
1525
1526 assert(template);
1527 assert(ret);
1528
1529 p = strdup(template);
1530 if (!p)
1531 return -ENOMEM;
1532
1533 if (!mkdtemp(p)) {
1534 free(p);
1535 return -errno;
1536 }
1537
1538 *ret = p;
1539 return 0;
1540 }