2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
31 #include "alloc-util.h"
38 #include "hexdecoct.h"
42 #include "parse-util.h"
43 #include "path-util.h"
44 #include "process-util.h"
45 #include "random-util.h"
46 #include "stdio-util.h"
47 #include "string-util.h"
49 #include "time-util.h"
50 #include "umask-util.h"
53 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
55 int write_string_stream_ts(FILE *f
, const char *line
, bool enforce_newline
, struct timespec
*ts
) {
61 if (enforce_newline
&& !endswith(line
, "\n"))
65 struct timespec twice
[2] = {*ts
, *ts
};
67 if (futimens(fileno(f
), twice
) < 0)
71 return fflush_and_check(f
);
74 static int write_string_file_atomic(
79 struct timespec
*ts
) {
81 _cleanup_fclose_
FILE *f
= NULL
;
82 _cleanup_free_
char *p
= NULL
;
88 r
= fopen_temporary(fn
, &f
, &p
);
92 (void) fchmod_umask(fileno(f
), 0644);
94 r
= write_string_stream_ts(f
, line
, enforce_newline
, ts
);
95 if (r
>= 0 && do_fsync
)
96 r
= fflush_sync_and_check(f
);
98 if (rename(p
, fn
) < 0)
108 int write_string_file_ts(const char *fn
, const char *line
, WriteStringFileFlags flags
, struct timespec
*ts
) {
109 _cleanup_fclose_
FILE *f
= NULL
;
115 /* We don't know how to verify whether the file contents was already on-disk. */
116 assert(!((flags
& WRITE_STRING_FILE_VERIFY_ON_FAILURE
) && (flags
& WRITE_STRING_FILE_SYNC
)));
118 if (flags
& WRITE_STRING_FILE_ATOMIC
) {
119 assert(flags
& WRITE_STRING_FILE_CREATE
);
121 r
= write_string_file_atomic(fn
,
123 !(flags
& WRITE_STRING_FILE_AVOID_NEWLINE
),
124 flags
& WRITE_STRING_FILE_SYNC
, ts
);
132 if (flags
& WRITE_STRING_FILE_CREATE
) {
141 /* We manually build our own version of fopen(..., "we") that
142 * works without O_CREAT */
143 fd
= open(fn
, O_WRONLY
|O_CLOEXEC
|O_NOCTTY
);
149 f
= fdopen(fd
, "we");
157 r
= write_string_stream_ts(f
, line
, !(flags
& WRITE_STRING_FILE_AVOID_NEWLINE
), ts
);
161 if (flags
& WRITE_STRING_FILE_SYNC
) {
162 r
= fflush_sync_and_check(f
);
170 if (!(flags
& WRITE_STRING_FILE_VERIFY_ON_FAILURE
))
175 /* OK, the operation failed, but let's see if the right
176 * contents in place already. If so, eat up the error. */
178 q
= verify_file(fn
, line
, !(flags
& WRITE_STRING_FILE_AVOID_NEWLINE
));
185 int read_one_line_file(const char *fn
, char **line
) {
186 _cleanup_fclose_
FILE *f
= NULL
;
187 char t
[LINE_MAX
], *c
;
196 if (!fgets(t
, sizeof(t
), f
)) {
199 return errno
> 0 ? -errno
: -EIO
;
213 int verify_file(const char *fn
, const char *blob
, bool accept_extra_nl
) {
214 _cleanup_fclose_
FILE *f
= NULL
;
215 _cleanup_free_
char *buf
= NULL
;
223 if (accept_extra_nl
&& endswith(blob
, "\n"))
224 accept_extra_nl
= false;
226 buf
= malloc(l
+ accept_extra_nl
+ 1);
234 /* We try to read one byte more than we need, so that we know whether we hit eof */
236 k
= fread(buf
, 1, l
+ accept_extra_nl
+ 1, f
);
238 return errno
> 0 ? -errno
: -EIO
;
240 if (k
!= l
&& k
!= l
+ accept_extra_nl
)
242 if (memcmp(buf
, blob
, l
) != 0)
244 if (k
> l
&& buf
[l
] != '\n')
250 int read_full_stream(FILE *f
, char **contents
, size_t *size
) {
252 _cleanup_free_
char *buf
= NULL
;
258 if (fstat(fileno(f
), &st
) < 0)
263 if (S_ISREG(st
.st_mode
)) {
266 if (st
.st_size
> READ_FULL_BYTES_MAX
)
269 /* Start with the right file size, but be prepared for
270 * files from /proc which generally report a file size
281 t
= realloc(buf
, n
+ 1);
286 k
= fread(buf
+ l
, 1, n
- l
, f
);
296 /* We aren't expecting fread() to return a short read outside
297 * of (error && eof), assert buffer is full and enlarge buffer.
302 if (n
>= READ_FULL_BYTES_MAX
)
305 n
= MIN(n
* 2, READ_FULL_BYTES_MAX
);
310 buf
= NULL
; /* do not free */
318 int read_full_file(const char *fn
, char **contents
, size_t *size
) {
319 _cleanup_fclose_
FILE *f
= NULL
;
328 return read_full_stream(f
, contents
, size
);
331 static int parse_env_file_internal(
335 int (*push
) (const char *filename
, unsigned line
,
336 const char *key
, char *value
, void *userdata
, int *n_pushed
),
340 _cleanup_free_
char *contents
= NULL
, *key
= NULL
;
341 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;
342 char *p
, *value
= NULL
;
353 SINGLE_QUOTE_VALUE_ESCAPE
,
355 DOUBLE_QUOTE_VALUE_ESCAPE
,
363 r
= read_full_stream(f
, &contents
, NULL
);
365 r
= read_full_file(fname
, &contents
, NULL
);
369 for (p
= contents
; *p
; p
++) {
375 if (strchr(COMMENTS
, c
))
377 else if (!strchr(WHITESPACE
, c
)) {
379 last_key_whitespace
= (size_t) -1;
381 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2)) {
391 if (strchr(newline
, c
)) {
395 } else if (c
== '=') {
397 last_value_whitespace
= (size_t) -1;
399 if (!strchr(WHITESPACE
, c
))
400 last_key_whitespace
= (size_t) -1;
401 else if (last_key_whitespace
== (size_t) -1)
402 last_key_whitespace
= n_key
;
404 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2)) {
415 if (strchr(newline
, c
)) {
423 /* strip trailing whitespace from key */
424 if (last_key_whitespace
!= (size_t) -1)
425 key
[last_key_whitespace
] = 0;
427 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
433 value_alloc
= n_value
= 0;
435 } else if (c
== '\'')
436 state
= SINGLE_QUOTE_VALUE
;
438 state
= DOUBLE_QUOTE_VALUE
;
440 state
= VALUE_ESCAPE
;
441 else if (!strchr(WHITESPACE
, c
)) {
444 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
449 value
[n_value
++] = c
;
455 if (strchr(newline
, c
)) {
464 /* Chomp off trailing whitespace from value */
465 if (last_value_whitespace
!= (size_t) -1)
466 value
[last_value_whitespace
] = 0;
468 /* strip trailing whitespace from key */
469 if (last_key_whitespace
!= (size_t) -1)
470 key
[last_key_whitespace
] = 0;
472 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
478 value_alloc
= n_value
= 0;
480 } else if (c
== '\\') {
481 state
= VALUE_ESCAPE
;
482 last_value_whitespace
= (size_t) -1;
484 if (!strchr(WHITESPACE
, c
))
485 last_value_whitespace
= (size_t) -1;
486 else if (last_value_whitespace
== (size_t) -1)
487 last_value_whitespace
= n_value
;
489 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
494 value
[n_value
++] = c
;
502 if (!strchr(newline
, c
)) {
503 /* Escaped newlines we eat up entirely */
504 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
509 value
[n_value
++] = c
;
513 case SINGLE_QUOTE_VALUE
:
517 state
= SINGLE_QUOTE_VALUE_ESCAPE
;
519 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
524 value
[n_value
++] = c
;
529 case SINGLE_QUOTE_VALUE_ESCAPE
:
530 state
= SINGLE_QUOTE_VALUE
;
532 if (!strchr(newline
, c
)) {
533 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
538 value
[n_value
++] = c
;
542 case DOUBLE_QUOTE_VALUE
:
546 state
= DOUBLE_QUOTE_VALUE_ESCAPE
;
548 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
553 value
[n_value
++] = c
;
558 case DOUBLE_QUOTE_VALUE_ESCAPE
:
559 state
= DOUBLE_QUOTE_VALUE
;
561 if (!strchr(newline
, c
)) {
562 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
567 value
[n_value
++] = c
;
573 state
= COMMENT_ESCAPE
;
574 else if (strchr(newline
, c
)) {
591 SINGLE_QUOTE_VALUE_ESCAPE
,
593 DOUBLE_QUOTE_VALUE_ESCAPE
)) {
601 if (last_value_whitespace
!= (size_t) -1)
602 value
[last_value_whitespace
] = 0;
604 /* strip trailing whitespace from key */
605 if (last_key_whitespace
!= (size_t) -1)
606 key
[last_key_whitespace
] = 0;
608 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
620 static int check_utf8ness_and_warn(
621 const char *filename
, unsigned line
,
622 const char *key
, char *value
) {
624 if (!utf8_is_valid(key
)) {
625 _cleanup_free_
char *p
= NULL
;
627 p
= utf8_escape_invalid(key
);
628 log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename
), line
, p
);
632 if (value
&& !utf8_is_valid(value
)) {
633 _cleanup_free_
char *p
= NULL
;
635 p
= utf8_escape_invalid(value
);
636 log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename
), line
, key
, p
);
643 static int parse_env_file_push(
644 const char *filename
, unsigned line
,
645 const char *key
, char *value
,
650 va_list aq
, *ap
= userdata
;
653 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
659 while ((k
= va_arg(aq
, const char *))) {
662 v
= va_arg(aq
, char **);
684 const char *newline
, ...) {
692 va_start(ap
, newline
);
693 r
= parse_env_file_internal(NULL
, fname
, newline
, parse_env_file_push
, &ap
, &n_pushed
);
696 return r
< 0 ? r
: n_pushed
;
699 static int load_env_file_push(
700 const char *filename
, unsigned line
,
701 const char *key
, char *value
,
704 char ***m
= userdata
;
708 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
712 p
= strjoin(key
, "=", value
);
716 r
= strv_env_replace(m
, p
);
729 int load_env_file(FILE *f
, const char *fname
, const char *newline
, char ***rl
) {
736 r
= parse_env_file_internal(f
, fname
, newline
, load_env_file_push
, &m
, NULL
);
746 static int load_env_file_push_pairs(
747 const char *filename
, unsigned line
,
748 const char *key
, char *value
,
751 char ***m
= userdata
;
754 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
758 r
= strv_extend(m
, key
);
763 r
= strv_extend(m
, "");
767 r
= strv_push(m
, value
);
778 int load_env_file_pairs(FILE *f
, const char *fname
, const char *newline
, char ***rl
) {
785 r
= parse_env_file_internal(f
, fname
, newline
, load_env_file_push_pairs
, &m
, NULL
);
795 static int merge_env_file_push(
796 const char *filename
, unsigned line
,
797 const char *key
, char *value
,
801 char ***env
= userdata
;
802 char *expanded_value
;
807 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename
), line
, key
);
811 if (!env_name_is_valid(key
)) {
812 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename
), line
, key
);
817 expanded_value
= replace_env(value
, *env
,
818 REPLACE_ENV_USE_ENVIRONMENT
|
819 REPLACE_ENV_ALLOW_BRACELESS
|
820 REPLACE_ENV_ALLOW_EXTENDED
);
824 free_and_replace(value
, expanded_value
);
826 return load_env_file_push(filename
, line
, key
, value
, env
, n_pushed
);
834 /* NOTE: this function supports braceful and braceless variable expansions,
835 * plus "extended" substitutions, unlike other exported parsing functions.
838 return parse_env_file_internal(f
, fname
, NEWLINE
, merge_env_file_push
, env
, NULL
);
841 static void write_env_var(FILE *f
, const char *v
) {
847 fputs_unlocked(v
, f
);
848 fputc_unlocked('\n', f
);
853 fwrite_unlocked(v
, 1, p
-v
, f
);
855 if (string_has_cc(p
, NULL
) || chars_intersect(p
, WHITESPACE SHELL_NEED_QUOTES
)) {
856 fputc_unlocked('\"', f
);
859 if (strchr(SHELL_NEED_ESCAPE
, *p
))
860 fputc_unlocked('\\', f
);
862 fputc_unlocked(*p
, f
);
865 fputc_unlocked('\"', f
);
867 fputs_unlocked(p
, f
);
869 fputc_unlocked('\n', f
);
872 int write_env_file(const char *fname
, char **l
) {
873 _cleanup_fclose_
FILE *f
= NULL
;
874 _cleanup_free_
char *p
= NULL
;
880 r
= fopen_temporary(fname
, &f
, &p
);
884 fchmod_umask(fileno(f
), 0644);
887 write_env_var(f
, *i
);
889 r
= fflush_and_check(f
);
891 if (rename(p
, fname
) >= 0)
901 int executable_is_script(const char *path
, char **interpreter
) {
903 _cleanup_free_
char *line
= NULL
;
909 r
= read_one_line_file(path
, &line
);
913 if (!startswith(line
, "#!"))
916 ans
= strstrip(line
+ 2);
917 len
= strcspn(ans
, " \t");
922 ans
= strndup(ans
, len
);
931 * Retrieve one field from a file like /proc/self/status. pattern
932 * should not include whitespace or the delimiter (':'). pattern matches only
933 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
934 * zeros after the ':' will be skipped. field must be freed afterwards.
935 * terminator specifies the terminating characters of the field value (not
936 * included in the value).
938 int get_proc_field(const char *filename
, const char *pattern
, const char *terminator
, char **field
) {
939 _cleanup_free_
char *status
= NULL
;
949 r
= read_full_file(filename
, &status
, NULL
);
959 t
= strstr(t
, pattern
);
963 /* Check that pattern occurs in beginning of line. */
964 pattern_ok
= (t
== status
|| t
[-1] == '\n');
966 t
+= strlen(pattern
);
968 } while (!pattern_ok
);
970 t
+= strspn(t
, " \t");
979 t
+= strspn(t
, " \t");
981 /* Also skip zeros, because when this is used for
982 * capabilities, we don't want the zeros. This way the
983 * same capability set always maps to the same string,
984 * irrespective of the total capability set size. For
985 * other numbers it shouldn't matter. */
987 /* Back off one char if there's nothing but whitespace
989 if (!*t
|| isspace(*t
))
993 len
= strcspn(t
, terminator
);
1003 DIR *xopendirat(int fd
, const char *name
, int flags
) {
1007 assert(!(flags
& O_CREAT
));
1009 nfd
= openat(fd
, name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|flags
, 0);
1022 static int search_and_fopen_internal(const char *path
, const char *mode
, const char *root
, char **search
, FILE **_f
) {
1029 if (!path_strv_resolve_uniq(search
, root
))
1032 STRV_FOREACH(i
, search
) {
1033 _cleanup_free_
char *p
= NULL
;
1037 p
= strjoin(root
, *i
, "/", path
);
1039 p
= strjoin(*i
, "/", path
);
1049 if (errno
!= ENOENT
)
1056 int search_and_fopen(const char *path
, const char *mode
, const char *root
, const char **search
, FILE **_f
) {
1057 _cleanup_strv_free_
char **copy
= NULL
;
1063 if (path_is_absolute(path
)) {
1066 f
= fopen(path
, mode
);
1075 copy
= strv_copy((char**) search
);
1079 return search_and_fopen_internal(path
, mode
, root
, copy
, _f
);
1082 int search_and_fopen_nulstr(const char *path
, const char *mode
, const char *root
, const char *search
, FILE **_f
) {
1083 _cleanup_strv_free_
char **s
= NULL
;
1085 if (path_is_absolute(path
)) {
1088 f
= fopen(path
, mode
);
1097 s
= strv_split_nulstr(search
);
1101 return search_and_fopen_internal(path
, mode
, root
, s
, _f
);
1104 int fopen_temporary(const char *path
, FILE **_f
, char **_temp_path
) {
1113 r
= tempfn_xxxxxx(path
, NULL
, &t
);
1117 fd
= mkostemp_safe(t
);
1123 f
= fdopen(fd
, "we");
1137 int fflush_and_check(FILE *f
) {
1144 return errno
> 0 ? -errno
: -EIO
;
1149 int fflush_sync_and_check(FILE *f
) {
1154 r
= fflush_and_check(f
);
1158 if (fsync(fileno(f
)) < 0)
1164 /* This is much like mkostemp() but is subject to umask(). */
1165 int mkostemp_safe(char *pattern
) {
1166 _cleanup_umask_ mode_t u
= 0;
1173 fd
= mkostemp(pattern
, O_CLOEXEC
);
1180 int tempfn_xxxxxx(const char *p
, const char *extra
, char **ret
) {
1192 * /foo/bar/.#<extra>waldoXXXXXX
1196 if (!filename_is_valid(fn
))
1202 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 6 + 1);
1206 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
), "XXXXXX");
1208 *ret
= path_kill_slashes(t
);
1212 int tempfn_random(const char *p
, const char *extra
, char **ret
) {
1226 * /foo/bar/.#<extra>waldobaa2a261115984a9
1230 if (!filename_is_valid(fn
))
1236 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 16 + 1);
1240 x
= stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
);
1243 for (i
= 0; i
< 16; i
++) {
1244 *(x
++) = hexchar(u
& 0xF);
1250 *ret
= path_kill_slashes(t
);
1254 int tempfn_random_child(const char *p
, const char *extra
, char **ret
) {
1265 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
1277 t
= new(char, strlen(p
) + 3 + strlen(extra
) + 16 + 1);
1281 x
= stpcpy(stpcpy(stpcpy(t
, p
), "/.#"), extra
);
1284 for (i
= 0; i
< 16; i
++) {
1285 *(x
++) = hexchar(u
& 0xF);
1291 *ret
= path_kill_slashes(t
);
1295 int write_timestamp_file_atomic(const char *fn
, usec_t n
) {
1296 char ln
[DECIMAL_STR_MAX(n
)+2];
1298 /* Creates a "timestamp" file, that contains nothing but a
1299 * usec_t timestamp, formatted in ASCII. */
1301 if (n
<= 0 || n
>= USEC_INFINITY
)
1304 xsprintf(ln
, USEC_FMT
"\n", n
);
1306 return write_string_file(fn
, ln
, WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
1309 int read_timestamp_file(const char *fn
, usec_t
*ret
) {
1310 _cleanup_free_
char *ln
= NULL
;
1314 r
= read_one_line_file(fn
, &ln
);
1318 r
= safe_atou64(ln
, &t
);
1322 if (t
<= 0 || t
>= (uint64_t) USEC_INFINITY
)
1329 int fputs_with_space(FILE *f
, const char *s
, const char *separator
, bool *space
) {
1334 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
1335 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
1336 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
1337 * element, but not before the first one. */
1347 r
= fputs(separator
, f
);
1358 int open_tmpfile_unlinkable(const char *directory
, int flags
) {
1363 r
= tmp_dir(&directory
);
1368 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
1370 /* Try O_TMPFILE first, if it is supported */
1371 fd
= open(directory
, flags
|O_TMPFILE
|O_EXCL
, S_IRUSR
|S_IWUSR
);
1375 /* Fall back to unguessable name + unlinking */
1376 p
= strjoina(directory
, "/systemd-tmp-XXXXXX");
1378 fd
= mkostemp_safe(p
);
1387 int open_tmpfile_linkable(const char *target
, int flags
, char **ret_path
) {
1388 _cleanup_free_
char *tmp
= NULL
;
1394 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
1395 assert((flags
& O_EXCL
) == 0);
1397 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
1398 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
1399 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
1402 _cleanup_free_
char *dn
= NULL
;
1404 dn
= dirname_malloc(target
);
1408 fd
= open(dn
, O_TMPFILE
|flags
, 0640);
1414 log_debug_errno(errno
, "Failed to use O_TMPFILE on %s: %m", dn
);
1417 r
= tempfn_random(target
, NULL
, &tmp
);
1421 fd
= open(tmp
, O_CREAT
|O_EXCL
|O_NOFOLLOW
|O_NOCTTY
|flags
, 0640);
1431 int open_serialization_fd(const char *ident
) {
1434 fd
= memfd_create(ident
, MFD_CLOEXEC
);
1438 path
= getpid_cached() == 1 ? "/run/systemd" : "/tmp";
1439 fd
= open_tmpfile_unlinkable(path
, O_RDWR
|O_CLOEXEC
);
1443 log_debug("Serializing %s to %s.", ident
, path
);
1445 log_debug("Serializing %s to memfd.", ident
);
1450 int link_tmpfile(int fd
, const char *path
, const char *target
) {
1455 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
1456 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
1457 * on the directory, and renameat2() is used instead.
1459 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
1460 * operation currently (renameat2() does), and there is no nice way to emulate this. */
1463 if (rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, target
) < 0)
1466 char proc_fd_path
[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd
) + 1];
1468 xsprintf(proc_fd_path
, "/proc/self/fd/%i", fd
);
1470 if (linkat(AT_FDCWD
, proc_fd_path
, AT_FDCWD
, target
, AT_SYMLINK_FOLLOW
) < 0)
1477 int read_nul_string(FILE *f
, char **ret
) {
1478 _cleanup_free_
char *x
= NULL
;
1479 size_t allocated
= 0, n
= 0;
1484 /* Reads a NUL-terminated string from the specified file. */
1489 if (!GREEDY_REALLOC(x
, allocated
, n
+2))
1493 if (c
== 0) /* Terminate at NUL byte */
1498 break; /* Terminate at EOF */
1518 int mkdtemp_malloc(const char *template, char **ret
) {
1524 p
= strdup(template);