Bug Summary

File:build-scan/../src/basic/fileio.c
Warning:line 406, column 28
Dereference of null pointer (loaded from variable 'p')

Annotated Source Code

Press '?' to see keyboard shortcuts

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