1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include <sys/types.h>
13 #include "time-util.h"
15 #define LONG_LINE_MAX (1U*1024U*1024U)
18 WRITE_STRING_FILE_CREATE
= 1 << 0,
19 WRITE_STRING_FILE_TRUNCATE
= 1 << 1,
20 WRITE_STRING_FILE_ATOMIC
= 1 << 2,
21 WRITE_STRING_FILE_AVOID_NEWLINE
= 1 << 3,
22 WRITE_STRING_FILE_VERIFY_ON_FAILURE
= 1 << 4,
23 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE
= 1 << 5,
24 WRITE_STRING_FILE_SYNC
= 1 << 6,
25 WRITE_STRING_FILE_DISABLE_BUFFER
= 1 << 7,
26 WRITE_STRING_FILE_NOFOLLOW
= 1 << 8,
27 WRITE_STRING_FILE_MKDIR_0755
= 1 << 9,
28 WRITE_STRING_FILE_MODE_0600
= 1 << 10,
29 WRITE_STRING_FILE_MODE_0444
= 1 << 11,
30 WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL
= 1 << 12,
32 /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
33 more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
36 } WriteStringFileFlags
;
39 READ_FULL_FILE_SECURE
= 1 << 0, /* erase any buffers we employ internally, after use */
40 READ_FULL_FILE_UNBASE64
= 1 << 1, /* base64 decode what we read */
41 READ_FULL_FILE_UNHEX
= 1 << 2, /* hex decode what we read */
42 READ_FULL_FILE_WARN_WORLD_READABLE
= 1 << 3, /* if regular file, log at LOG_WARNING level if access mode above 0700 */
43 READ_FULL_FILE_CONNECT_SOCKET
= 1 << 4, /* if socket inode, connect to it and read off it */
44 READ_FULL_FILE_FAIL_WHEN_LARGER
= 1 << 5, /* fail loading if file is larger than specified size */
47 int fdopen_unlocked(int fd
, const char *options
, FILE **ret
);
48 int take_fdopen_unlocked(int *fd
, const char *options
, FILE **ret
);
49 FILE* take_fdopen(int *fd
, const char *options
);
50 DIR* take_fdopendir(int *dfd
);
51 FILE* open_memstream_unlocked(char **ptr
, size_t *sizeloc
);
52 FILE* fmemopen_unlocked(void *buf
, size_t size
, const char *mode
);
54 int write_string_stream_ts(FILE *f
, const char *line
, WriteStringFileFlags flags
, const struct timespec
*ts
);
55 static inline int write_string_stream(FILE *f
, const char *line
, WriteStringFileFlags flags
) {
56 return write_string_stream_ts(f
, line
, flags
, NULL
);
58 int write_string_file_ts_at(int dir_fd
, const char *fn
, const char *line
, WriteStringFileFlags flags
, const struct timespec
*ts
);
59 static inline int write_string_file_ts(const char *fn
, const char *line
, WriteStringFileFlags flags
, const struct timespec
*ts
) {
60 return write_string_file_ts_at(AT_FDCWD
, fn
, line
, flags
, ts
);
62 static inline int write_string_file_at(int dir_fd
, const char *fn
, const char *line
, WriteStringFileFlags flags
) {
63 return write_string_file_ts_at(dir_fd
, fn
, line
, flags
, NULL
);
65 static inline int write_string_file(const char *fn
, const char *line
, WriteStringFileFlags flags
) {
66 return write_string_file_ts(fn
, line
, flags
, NULL
);
69 int write_string_filef(const char *fn
, WriteStringFileFlags flags
, const char *format
, ...) _printf_(3, 4);
71 int read_one_line_file_at(int dir_fd
, const char *filename
, char **ret
);
72 static inline int read_one_line_file(const char *filename
, char **ret
) {
73 return read_one_line_file_at(AT_FDCWD
, filename
, ret
);
75 int read_full_file_full(int dir_fd
, const char *filename
, uint64_t offset
, size_t size
, ReadFullFileFlags flags
, const char *bind_name
, char **ret_contents
, size_t *ret_size
);
76 static inline int read_full_file_at(int dir_fd
, const char *filename
, char **ret_contents
, size_t *ret_size
) {
77 return read_full_file_full(dir_fd
, filename
, UINT64_MAX
, SIZE_MAX
, 0, NULL
, ret_contents
, ret_size
);
79 static inline int read_full_file(const char *filename
, char **ret_contents
, size_t *ret_size
) {
80 return read_full_file_full(AT_FDCWD
, filename
, UINT64_MAX
, SIZE_MAX
, 0, NULL
, ret_contents
, ret_size
);
83 int read_virtual_file_fd(int fd
, size_t max_size
, char **ret_contents
, size_t *ret_size
);
84 int read_virtual_file_at(int dir_fd
, const char *filename
, size_t max_size
, char **ret_contents
, size_t *ret_size
);
85 static inline int read_virtual_file(const char *filename
, size_t max_size
, char **ret_contents
, size_t *ret_size
) {
86 return read_virtual_file_at(AT_FDCWD
, filename
, max_size
, ret_contents
, ret_size
);
88 static inline int read_full_virtual_file(const char *filename
, char **ret_contents
, size_t *ret_size
) {
89 return read_virtual_file(filename
, SIZE_MAX
, ret_contents
, ret_size
);
92 int read_full_stream_full(FILE *f
, const char *filename
, uint64_t offset
, size_t size
, ReadFullFileFlags flags
, char **ret_contents
, size_t *ret_size
);
93 static inline int read_full_stream(FILE *f
, char **ret_contents
, size_t *ret_size
) {
94 return read_full_stream_full(f
, NULL
, UINT64_MAX
, SIZE_MAX
, 0, ret_contents
, ret_size
);
97 int verify_file_at(int dir_fd
, const char *fn
, const char *blob
, bool accept_extra_nl
);
98 static inline int verify_file(const char *fn
, const char *blob
, bool accept_extra_nl
) {
99 return verify_file_at(AT_FDCWD
, fn
, blob
, accept_extra_nl
);
102 int executable_is_script(const char *path
, char **interpreter
);
104 int get_proc_field(const char *filename
, const char *pattern
, const char *terminator
, char **field
);
106 DIR *xopendirat(int dirfd
, const char *name
, int flags
);
108 typedef enum XfopenFlags
{
109 XFOPEN_UNLOCKED
= 1 << 0, /* call __fsetlocking(FSETLOCKING_BYCALLER) after opened */
110 XFOPEN_SOCKET
= 1 << 1, /* also try to open unix socket */
119 const char *bind_name
,
121 static inline int xfopenat(int dir_fd
, const char *path
, const char *mode
, int open_flags
, FILE **ret
) {
122 return xfopenat_full(dir_fd
, path
, mode
, open_flags
, 0, NULL
, ret
);
124 static inline int fopen_unlocked_at(int dir_fd
, const char *path
, const char *mode
, int open_flags
, FILE **ret
) {
125 return xfopenat_full(dir_fd
, path
, mode
, open_flags
, XFOPEN_UNLOCKED
, NULL
, ret
);
127 static inline int fopen_unlocked(const char *path
, const char *mode
, FILE **ret
) {
128 return fopen_unlocked_at(AT_FDCWD
, path
, mode
, 0, ret
);
131 int fdopen_independent(int fd
, const char *mode
, FILE **ret
);
133 int search_and_open(const char *path
, int mode
, const char *root
, char **search
, int *ret_fd
, char **ret_path
);
134 static inline int search_and_access(const char *path
, int mode
, const char *root
, char**search
, char **ret_path
) {
135 return search_and_open(path
, mode
, root
, search
, NULL
, ret_path
);
137 int search_and_fopen(const char *path
, const char *mode
, const char *root
, const char **search
, FILE **ret_file
, char **ret_path
);
138 int search_and_fopen_nulstr(const char *path
, const char *mode
, const char *root
, const char *search
, FILE **ret_file
, char **ret_path
);
140 int fflush_and_check(FILE *f
);
141 int fflush_sync_and_check(FILE *f
);
143 int write_timestamp_file_atomic(const char *fn
, usec_t n
);
144 int read_timestamp_file(const char *fn
, usec_t
*ret
);
146 int fputs_with_space(FILE *f
, const char *s
, const char *separator
, bool *space
);
148 typedef enum ReadLineFlags
{
149 READ_LINE_ONLY_NUL
= 1 << 0,
150 READ_LINE_IS_A_TTY
= 1 << 1,
151 READ_LINE_NOT_A_TTY
= 1 << 2,
154 int read_line_full(FILE *f
, size_t limit
, ReadLineFlags flags
, char **ret
);
156 static inline bool file_offset_beyond_memory_size(off_t x
) {
157 if (x
< 0) /* off_t is signed, filter that out */
159 return (uint64_t) x
> (uint64_t) SIZE_MAX
;
162 static inline int read_line(FILE *f
, size_t limit
, char **ret
) {
163 return read_line_full(f
, limit
, 0, ret
);
166 static inline int read_nul_string(FILE *f
, size_t limit
, char **ret
) {
167 return read_line_full(f
, limit
, READ_LINE_ONLY_NUL
, ret
);
170 int read_stripped_line(FILE *f
, size_t limit
, char **ret
);
172 int safe_fgetc(FILE *f
, char *ret
);
174 int warn_file_is_world_accessible(const char *filename
, struct stat
*st
, const char *unit
, unsigned line
);
176 int fopen_mode_to_flags(const char *mode
);