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"
39 #include "hexdecoct.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
46 #include "random-util.h"
47 #include "stdio-util.h"
48 #include "string-util.h"
50 #include "time-util.h"
51 #include "umask-util.h"
54 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
56 int write_string_stream_ts(
59 WriteStringFileFlags flags
,
60 struct timespec
*ts
) {
66 if (!(flags
& WRITE_STRING_FILE_AVOID_NEWLINE
) && !endswith(line
, "\n"))
70 struct timespec twice
[2] = {*ts
, *ts
};
72 if (futimens(fileno(f
), twice
) < 0)
76 if (flags
& WRITE_STRING_FILE_SYNC
)
77 return fflush_sync_and_check(f
);
79 return fflush_and_check(f
);
82 static int write_string_file_atomic(
85 WriteStringFileFlags flags
,
86 struct timespec
*ts
) {
88 _cleanup_fclose_
FILE *f
= NULL
;
89 _cleanup_free_
char *p
= NULL
;
95 r
= fopen_temporary(fn
, &f
, &p
);
99 (void) fchmod_umask(fileno(f
), 0644);
101 r
= write_string_stream_ts(f
, line
, flags
, ts
);
105 if (rename(p
, fn
) < 0) {
117 int write_string_file_ts(
120 WriteStringFileFlags flags
,
121 struct timespec
*ts
) {
123 _cleanup_fclose_
FILE *f
= NULL
;
129 /* We don't know how to verify whether the file contents was already on-disk. */
130 assert(!((flags
& WRITE_STRING_FILE_VERIFY_ON_FAILURE
) && (flags
& WRITE_STRING_FILE_SYNC
)));
132 if (flags
& WRITE_STRING_FILE_ATOMIC
) {
133 assert(flags
& WRITE_STRING_FILE_CREATE
);
135 r
= write_string_file_atomic(fn
, line
, flags
, ts
);
143 if (flags
& WRITE_STRING_FILE_CREATE
) {
152 /* We manually build our own version of fopen(..., "we") that
153 * works without O_CREAT */
154 fd
= open(fn
, O_WRONLY
|O_CLOEXEC
|O_NOCTTY
);
160 f
= fdopen(fd
, "we");
168 r
= write_string_stream_ts(f
, line
, flags
, ts
);
175 if (!(flags
& WRITE_STRING_FILE_VERIFY_ON_FAILURE
))
180 /* OK, the operation failed, but let's see if the right
181 * contents in place already. If so, eat up the error. */
183 q
= verify_file(fn
, line
, !(flags
& WRITE_STRING_FILE_AVOID_NEWLINE
));
190 int read_one_line_file(const char *fn
, char **line
) {
191 _cleanup_fclose_
FILE *f
= NULL
;
201 r
= read_line(f
, LONG_LINE_MAX
, line
);
202 return r
< 0 ? r
: 0;
205 int verify_file(const char *fn
, const char *blob
, bool accept_extra_nl
) {
206 _cleanup_fclose_
FILE *f
= NULL
;
207 _cleanup_free_
char *buf
= NULL
;
215 if (accept_extra_nl
&& endswith(blob
, "\n"))
216 accept_extra_nl
= false;
218 buf
= malloc(l
+ accept_extra_nl
+ 1);
226 /* We try to read one byte more than we need, so that we know whether we hit eof */
228 k
= fread(buf
, 1, l
+ accept_extra_nl
+ 1, f
);
230 return errno
> 0 ? -errno
: -EIO
;
232 if (k
!= l
&& k
!= l
+ accept_extra_nl
)
234 if (memcmp(buf
, blob
, l
) != 0)
236 if (k
> l
&& buf
[l
] != '\n')
242 int read_full_stream(FILE *f
, char **contents
, size_t *size
) {
244 _cleanup_free_
char *buf
= NULL
;
250 if (fstat(fileno(f
), &st
) < 0)
255 if (S_ISREG(st
.st_mode
)) {
258 if (st
.st_size
> READ_FULL_BYTES_MAX
)
261 /* Start with the right file size, but be prepared for files from /proc which generally report a file
262 * size of 0. Note that we increase the size to read here by one, so that the first read attempt
263 * already makes us notice the EOF. */
273 t
= realloc(buf
, n
+ 1);
279 k
= fread(buf
+ l
, 1, n
- l
, f
);
284 return errno
> 0 ? -errno
: -EIO
;
289 /* We aren't expecting fread() to return a short read outside
290 * of (error && eof), assert buffer is full and enlarge buffer.
295 if (n
>= READ_FULL_BYTES_MAX
)
298 n
= MIN(n
* 2, READ_FULL_BYTES_MAX
);
303 buf
= NULL
; /* do not free */
311 int read_full_file(const char *fn
, char **contents
, size_t *size
) {
312 _cleanup_fclose_
FILE *f
= NULL
;
321 return read_full_stream(f
, contents
, size
);
324 static int parse_env_file_internal(
328 int (*push
) (const char *filename
, unsigned line
,
329 const char *key
, char *value
, void *userdata
, int *n_pushed
),
333 _cleanup_free_
char *contents
= NULL
, *key
= NULL
;
334 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;
335 char *p
, *value
= NULL
;
346 SINGLE_QUOTE_VALUE_ESCAPE
,
348 DOUBLE_QUOTE_VALUE_ESCAPE
,
356 r
= read_full_stream(f
, &contents
, NULL
);
358 r
= read_full_file(fname
, &contents
, NULL
);
362 for (p
= contents
; *p
; p
++) {
368 if (strchr(COMMENTS
, c
))
370 else if (!strchr(WHITESPACE
, c
)) {
372 last_key_whitespace
= (size_t) -1;
374 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2)) {
384 if (strchr(newline
, c
)) {
388 } else if (c
== '=') {
390 last_value_whitespace
= (size_t) -1;
392 if (!strchr(WHITESPACE
, c
))
393 last_key_whitespace
= (size_t) -1;
394 else if (last_key_whitespace
== (size_t) -1)
395 last_key_whitespace
= n_key
;
397 if (!GREEDY_REALLOC(key
, key_alloc
, n_key
+2)) {
408 if (strchr(newline
, c
)) {
416 /* strip trailing whitespace from key */
417 if (last_key_whitespace
!= (size_t) -1)
418 key
[last_key_whitespace
] = 0;
420 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
426 value_alloc
= n_value
= 0;
428 } else if (c
== '\'')
429 state
= SINGLE_QUOTE_VALUE
;
431 state
= DOUBLE_QUOTE_VALUE
;
433 state
= VALUE_ESCAPE
;
434 else if (!strchr(WHITESPACE
, c
)) {
437 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
442 value
[n_value
++] = c
;
448 if (strchr(newline
, c
)) {
457 /* Chomp off trailing whitespace from value */
458 if (last_value_whitespace
!= (size_t) -1)
459 value
[last_value_whitespace
] = 0;
461 /* strip trailing whitespace from key */
462 if (last_key_whitespace
!= (size_t) -1)
463 key
[last_key_whitespace
] = 0;
465 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
471 value_alloc
= n_value
= 0;
473 } else if (c
== '\\') {
474 state
= VALUE_ESCAPE
;
475 last_value_whitespace
= (size_t) -1;
477 if (!strchr(WHITESPACE
, c
))
478 last_value_whitespace
= (size_t) -1;
479 else if (last_value_whitespace
== (size_t) -1)
480 last_value_whitespace
= n_value
;
482 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
487 value
[n_value
++] = c
;
495 if (!strchr(newline
, c
)) {
496 /* Escaped newlines we eat up entirely */
497 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
502 value
[n_value
++] = c
;
506 case SINGLE_QUOTE_VALUE
:
510 state
= SINGLE_QUOTE_VALUE_ESCAPE
;
512 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
517 value
[n_value
++] = c
;
522 case SINGLE_QUOTE_VALUE_ESCAPE
:
523 state
= SINGLE_QUOTE_VALUE
;
525 if (!strchr(newline
, c
)) {
526 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
531 value
[n_value
++] = c
;
535 case DOUBLE_QUOTE_VALUE
:
539 state
= DOUBLE_QUOTE_VALUE_ESCAPE
;
541 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
546 value
[n_value
++] = c
;
551 case DOUBLE_QUOTE_VALUE_ESCAPE
:
552 state
= DOUBLE_QUOTE_VALUE
;
554 if (!strchr(newline
, c
)) {
555 if (!GREEDY_REALLOC(value
, value_alloc
, n_value
+2)) {
560 value
[n_value
++] = c
;
566 state
= COMMENT_ESCAPE
;
567 else if (strchr(newline
, c
)) {
584 SINGLE_QUOTE_VALUE_ESCAPE
,
586 DOUBLE_QUOTE_VALUE_ESCAPE
)) {
594 if (last_value_whitespace
!= (size_t) -1)
595 value
[last_value_whitespace
] = 0;
597 /* strip trailing whitespace from key */
598 if (last_key_whitespace
!= (size_t) -1)
599 key
[last_key_whitespace
] = 0;
601 r
= push(fname
, line
, key
, value
, userdata
, n_pushed
);
613 static int check_utf8ness_and_warn(
614 const char *filename
, unsigned line
,
615 const char *key
, char *value
) {
617 if (!utf8_is_valid(key
)) {
618 _cleanup_free_
char *p
= NULL
;
620 p
= utf8_escape_invalid(key
);
621 log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename
), line
, p
);
625 if (value
&& !utf8_is_valid(value
)) {
626 _cleanup_free_
char *p
= NULL
;
628 p
= utf8_escape_invalid(value
);
629 log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename
), line
, key
, p
);
636 static int parse_env_file_push(
637 const char *filename
, unsigned line
,
638 const char *key
, char *value
,
643 va_list aq
, *ap
= userdata
;
646 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
652 while ((k
= va_arg(aq
, const char *))) {
655 v
= va_arg(aq
, char **);
677 const char *newline
, ...) {
685 va_start(ap
, newline
);
686 r
= parse_env_file_internal(NULL
, fname
, newline
, parse_env_file_push
, &ap
, &n_pushed
);
689 return r
< 0 ? r
: n_pushed
;
692 static int load_env_file_push(
693 const char *filename
, unsigned line
,
694 const char *key
, char *value
,
697 char ***m
= userdata
;
701 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
705 p
= strjoin(key
, "=", value
);
709 r
= strv_env_replace(m
, p
);
722 int load_env_file(FILE *f
, const char *fname
, const char *newline
, char ***rl
) {
729 r
= parse_env_file_internal(f
, fname
, newline
, load_env_file_push
, &m
, NULL
);
739 static int load_env_file_push_pairs(
740 const char *filename
, unsigned line
,
741 const char *key
, char *value
,
744 char ***m
= userdata
;
747 r
= check_utf8ness_and_warn(filename
, line
, key
, value
);
751 r
= strv_extend(m
, key
);
756 r
= strv_extend(m
, "");
760 r
= strv_push(m
, value
);
771 int load_env_file_pairs(FILE *f
, const char *fname
, const char *newline
, char ***rl
) {
778 r
= parse_env_file_internal(f
, fname
, newline
, load_env_file_push_pairs
, &m
, NULL
);
788 static int merge_env_file_push(
789 const char *filename
, unsigned line
,
790 const char *key
, char *value
,
794 char ***env
= userdata
;
795 char *expanded_value
;
800 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename
), line
, key
);
804 if (!env_name_is_valid(key
)) {
805 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename
), line
, key
);
810 expanded_value
= replace_env(value
, *env
,
811 REPLACE_ENV_USE_ENVIRONMENT
|
812 REPLACE_ENV_ALLOW_BRACELESS
|
813 REPLACE_ENV_ALLOW_EXTENDED
);
817 free_and_replace(value
, expanded_value
);
819 return load_env_file_push(filename
, line
, key
, value
, env
, n_pushed
);
827 /* NOTE: this function supports braceful and braceless variable expansions,
828 * plus "extended" substitutions, unlike other exported parsing functions.
831 return parse_env_file_internal(f
, fname
, NEWLINE
, merge_env_file_push
, env
, NULL
);
834 static void write_env_var(FILE *f
, const char *v
) {
840 fputs_unlocked(v
, f
);
841 fputc_unlocked('\n', f
);
846 fwrite_unlocked(v
, 1, p
-v
, f
);
848 if (string_has_cc(p
, NULL
) || chars_intersect(p
, WHITESPACE SHELL_NEED_QUOTES
)) {
849 fputc_unlocked('\"', f
);
852 if (strchr(SHELL_NEED_ESCAPE
, *p
))
853 fputc_unlocked('\\', f
);
855 fputc_unlocked(*p
, f
);
858 fputc_unlocked('\"', f
);
860 fputs_unlocked(p
, f
);
862 fputc_unlocked('\n', f
);
865 int write_env_file(const char *fname
, char **l
) {
866 _cleanup_fclose_
FILE *f
= NULL
;
867 _cleanup_free_
char *p
= NULL
;
873 r
= fopen_temporary(fname
, &f
, &p
);
877 fchmod_umask(fileno(f
), 0644);
880 write_env_var(f
, *i
);
882 r
= fflush_and_check(f
);
884 if (rename(p
, fname
) >= 0)
894 int executable_is_script(const char *path
, char **interpreter
) {
896 _cleanup_free_
char *line
= NULL
;
902 r
= read_one_line_file(path
, &line
);
906 if (!startswith(line
, "#!"))
909 ans
= strstrip(line
+ 2);
910 len
= strcspn(ans
, " \t");
915 ans
= strndup(ans
, len
);
924 * Retrieve one field from a file like /proc/self/status. pattern
925 * should not include whitespace or the delimiter (':'). pattern matches only
926 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
927 * zeros after the ':' will be skipped. field must be freed afterwards.
928 * terminator specifies the terminating characters of the field value (not
929 * included in the value).
931 int get_proc_field(const char *filename
, const char *pattern
, const char *terminator
, char **field
) {
932 _cleanup_free_
char *status
= NULL
;
942 r
= read_full_file(filename
, &status
, NULL
);
952 t
= strstr(t
, pattern
);
956 /* Check that pattern occurs in beginning of line. */
957 pattern_ok
= (t
== status
|| t
[-1] == '\n');
959 t
+= strlen(pattern
);
961 } while (!pattern_ok
);
963 t
+= strspn(t
, " \t");
972 t
+= strspn(t
, " \t");
974 /* Also skip zeros, because when this is used for
975 * capabilities, we don't want the zeros. This way the
976 * same capability set always maps to the same string,
977 * irrespective of the total capability set size. For
978 * other numbers it shouldn't matter. */
980 /* Back off one char if there's nothing but whitespace
982 if (!*t
|| isspace(*t
))
986 len
= strcspn(t
, terminator
);
996 DIR *xopendirat(int fd
, const char *name
, int flags
) {
1000 assert(!(flags
& O_CREAT
));
1002 nfd
= openat(fd
, name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|flags
, 0);
1015 static int search_and_fopen_internal(const char *path
, const char *mode
, const char *root
, char **search
, FILE **_f
) {
1022 if (!path_strv_resolve_uniq(search
, root
))
1025 STRV_FOREACH(i
, search
) {
1026 _cleanup_free_
char *p
= NULL
;
1030 p
= strjoin(root
, *i
, "/", path
);
1032 p
= strjoin(*i
, "/", path
);
1042 if (errno
!= ENOENT
)
1049 int search_and_fopen(const char *path
, const char *mode
, const char *root
, const char **search
, FILE **_f
) {
1050 _cleanup_strv_free_
char **copy
= NULL
;
1056 if (path_is_absolute(path
)) {
1059 f
= fopen(path
, mode
);
1068 copy
= strv_copy((char**) search
);
1072 return search_and_fopen_internal(path
, mode
, root
, copy
, _f
);
1075 int search_and_fopen_nulstr(const char *path
, const char *mode
, const char *root
, const char *search
, FILE **_f
) {
1076 _cleanup_strv_free_
char **s
= NULL
;
1078 if (path_is_absolute(path
)) {
1081 f
= fopen(path
, mode
);
1090 s
= strv_split_nulstr(search
);
1094 return search_and_fopen_internal(path
, mode
, root
, s
, _f
);
1097 int fopen_temporary(const char *path
, FILE **_f
, char **_temp_path
) {
1106 r
= tempfn_xxxxxx(path
, NULL
, &t
);
1110 fd
= mkostemp_safe(t
);
1116 f
= fdopen(fd
, "we");
1130 int fflush_and_check(FILE *f
) {
1137 return errno
> 0 ? -errno
: -EIO
;
1142 int fflush_sync_and_check(FILE *f
) {
1147 r
= fflush_and_check(f
);
1151 if (fsync(fileno(f
)) < 0)
1157 /* This is much like mkostemp() but is subject to umask(). */
1158 int mkostemp_safe(char *pattern
) {
1159 _cleanup_umask_ mode_t u
= 0;
1166 fd
= mkostemp(pattern
, O_CLOEXEC
);
1173 int tempfn_xxxxxx(const char *p
, const char *extra
, char **ret
) {
1185 * /foo/bar/.#<extra>waldoXXXXXX
1189 if (!filename_is_valid(fn
))
1195 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 6 + 1);
1199 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
), "XXXXXX");
1201 *ret
= path_kill_slashes(t
);
1205 int tempfn_random(const char *p
, const char *extra
, char **ret
) {
1219 * /foo/bar/.#<extra>waldobaa2a261115984a9
1223 if (!filename_is_valid(fn
))
1229 t
= new(char, strlen(p
) + 2 + strlen(extra
) + 16 + 1);
1233 x
= stpcpy(stpcpy(stpcpy(mempcpy(t
, p
, fn
- p
), ".#"), extra
), fn
);
1236 for (i
= 0; i
< 16; i
++) {
1237 *(x
++) = hexchar(u
& 0xF);
1243 *ret
= path_kill_slashes(t
);
1247 int tempfn_random_child(const char *p
, const char *extra
, char **ret
) {
1258 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
1270 t
= new(char, strlen(p
) + 3 + strlen(extra
) + 16 + 1);
1274 x
= stpcpy(stpcpy(stpcpy(t
, p
), "/.#"), extra
);
1277 for (i
= 0; i
< 16; i
++) {
1278 *(x
++) = hexchar(u
& 0xF);
1284 *ret
= path_kill_slashes(t
);
1288 int write_timestamp_file_atomic(const char *fn
, usec_t n
) {
1289 char ln
[DECIMAL_STR_MAX(n
)+2];
1291 /* Creates a "timestamp" file, that contains nothing but a
1292 * usec_t timestamp, formatted in ASCII. */
1294 if (n
<= 0 || n
>= USEC_INFINITY
)
1297 xsprintf(ln
, USEC_FMT
"\n", n
);
1299 return write_string_file(fn
, ln
, WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
1302 int read_timestamp_file(const char *fn
, usec_t
*ret
) {
1303 _cleanup_free_
char *ln
= NULL
;
1307 r
= read_one_line_file(fn
, &ln
);
1311 r
= safe_atou64(ln
, &t
);
1315 if (t
<= 0 || t
>= (uint64_t) USEC_INFINITY
)
1322 int fputs_with_space(FILE *f
, const char *s
, const char *separator
, bool *space
) {
1327 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
1328 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
1329 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
1330 * element, but not before the first one. */
1340 r
= fputs(separator
, f
);
1351 int open_tmpfile_unlinkable(const char *directory
, int flags
) {
1356 r
= tmp_dir(&directory
);
1361 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
1363 /* Try O_TMPFILE first, if it is supported */
1364 fd
= open(directory
, flags
|O_TMPFILE
|O_EXCL
, S_IRUSR
|S_IWUSR
);
1368 /* Fall back to unguessable name + unlinking */
1369 p
= strjoina(directory
, "/systemd-tmp-XXXXXX");
1371 fd
= mkostemp_safe(p
);
1380 int open_tmpfile_linkable(const char *target
, int flags
, char **ret_path
) {
1381 _cleanup_free_
char *tmp
= NULL
;
1387 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
1388 assert((flags
& O_EXCL
) == 0);
1390 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
1391 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
1392 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
1395 _cleanup_free_
char *dn
= NULL
;
1397 dn
= dirname_malloc(target
);
1401 fd
= open(dn
, O_TMPFILE
|flags
, 0640);
1407 log_debug_errno(errno
, "Failed to use O_TMPFILE on %s: %m", dn
);
1410 r
= tempfn_random(target
, NULL
, &tmp
);
1414 fd
= open(tmp
, O_CREAT
|O_EXCL
|O_NOFOLLOW
|O_NOCTTY
|flags
, 0640);
1424 int open_serialization_fd(const char *ident
) {
1427 fd
= memfd_create(ident
, MFD_CLOEXEC
);
1431 path
= getpid_cached() == 1 ? "/run/systemd" : "/tmp";
1432 fd
= open_tmpfile_unlinkable(path
, O_RDWR
|O_CLOEXEC
);
1436 log_debug("Serializing %s to %s.", ident
, path
);
1438 log_debug("Serializing %s to memfd.", ident
);
1443 int link_tmpfile(int fd
, const char *path
, const char *target
) {
1448 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
1449 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
1450 * on the directory, and renameat2() is used instead.
1452 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
1453 * operation currently (renameat2() does), and there is no nice way to emulate this. */
1456 if (rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, target
) < 0)
1459 char proc_fd_path
[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd
) + 1];
1461 xsprintf(proc_fd_path
, "/proc/self/fd/%i", fd
);
1463 if (linkat(AT_FDCWD
, proc_fd_path
, AT_FDCWD
, target
, AT_SYMLINK_FOLLOW
) < 0)
1470 int read_nul_string(FILE *f
, char **ret
) {
1471 _cleanup_free_
char *x
= NULL
;
1472 size_t allocated
= 0, n
= 0;
1477 /* Reads a NUL-terminated string from the specified file. */
1482 if (!GREEDY_REALLOC(x
, allocated
, n
+2))
1486 if (c
== 0) /* Terminate at NUL byte */
1491 break; /* Terminate at EOF */
1511 int mkdtemp_malloc(const char *template, char **ret
) {
1517 p
= strdup(template);
1530 static inline void funlockfilep(FILE **f
) {
1534 int read_line(FILE *f
, size_t limit
, char **ret
) {
1535 _cleanup_free_
char *buffer
= NULL
;
1536 size_t n
= 0, allocated
= 0, count
= 0;
1540 /* Something like a bounded version of getline().
1542 * Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
1545 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1546 * the number of characters in the returned string). When EOF is hit, 0 is returned.
1548 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1549 * delimiters. If the limit is hit we fail and return -ENOBUFS.
1551 * If a line shall be skipped ret may be initialized as NULL. */
1554 if (!GREEDY_REALLOC(buffer
, allocated
, 1))
1559 _unused_
_cleanup_(funlockfilep
) FILE *flocked
= f
;
1569 c
= fgetc_unlocked(f
);
1571 /* if we read an error, and have no data to return, then propagate the error */
1572 if (ferror_unlocked(f
) && n
== 0)
1573 return errno
> 0 ? -errno
: -EIO
;
1580 if (IN_SET(c
, '\n', 0)) /* Reached a delimiter */
1584 if (!GREEDY_REALLOC(buffer
, allocated
, n
+ 2))
1587 buffer
[n
] = (char) c
;