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