]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
25ad3e9361e34660b3da02b31e04ecb7272ba43d
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 /* maximum length of fdname */
11 #define FDNAME_MAX 255
13 /* Make sure we can distinguish fd 0 and NULL */
14 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
15 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
17 /* Useful helpers for initializing pipe(), socketpair() or stdio fd arrays */
18 #define EBADF_PAIR { -EBADF, -EBADF }
19 #define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF }
21 /* So O_LARGEFILE is generally implied by glibc, and defined to zero hence, because we only build in LFS
22 * mode. However, when invoking fcntl(F_GETFL) the flag is ORed into the result anyway — glibc does not mask
23 * it away. Which sucks. Let's define the actual value here, so that we can mask it ourselves.
25 * The precise definition is arch specific, so we use the values defined in the kernel (note that some
26 * are hexa and others are octal; duplicated as-is from the kernel definitions):
27 * - alpha, arm, arm64, m68k, mips, parisc, powerpc, sparc: each has a specific value;
28 * - others: they use the "generic" value (defined in include/uapi/asm-generic/fcntl.h) */
30 # define RAW_O_LARGEFILE O_LARGEFILE
32 # if defined(__alpha__) || defined(__arm__) || defined(__aarch64__) || defined(__m68k__)
33 # define RAW_O_LARGEFILE 0400000
34 # elif defined(__mips__)
35 # define RAW_O_LARGEFILE 0x2000
36 # elif defined(__parisc__) || defined(__hppa__)
37 # define RAW_O_LARGEFILE 000004000
38 # elif defined(__powerpc__)
39 # define RAW_O_LARGEFILE 0200000
40 # elif defined(__sparc__)
41 # define RAW_O_LARGEFILE 0x40000
43 # define RAW_O_LARGEFILE 00100000
47 /* On musl, O_ACCMODE is defined as (03|O_SEARCH), unlike glibc which defines it as
48 * (O_RDONLY|O_WRONLY|O_RDWR). Additionally, O_SEARCH is simply defined as O_PATH. This changes the behaviour
49 * of O_ACCMODE in certain situations, which we don't want. This definition is copied from glibc and works
50 * around the problems with musl's definition. */
51 #define O_ACCMODE_STRICT (O_RDONLY|O_WRONLY|O_RDWR)
53 int close_nointr(int fd
);
54 int safe_close(int fd
);
55 void safe_close_pair(int p
[static 2]);
57 static inline int safe_close_above_stdio(int fd
) {
58 if (fd
< 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */
61 return safe_close(fd
);
64 static inline void* close_fd_ptr(void *p
) {
65 safe_close(PTR_TO_FD(p
));
69 void close_many(const int fds
[], size_t n_fds
);
70 void close_many_unset(int fds
[], size_t n_fds
);
71 void close_many_and_free(int *fds
, size_t n_fds
);
73 int fclose_nointr(FILE *f
);
74 FILE* safe_fclose(FILE *f
);
75 DIR* safe_closedir(DIR *f
);
77 static inline void closep(int *fd
) {
81 static inline void close_pairp(int (*p
)[2]) {
85 static inline void fclosep(FILE **f
) {
89 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose
, NULL
);
90 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir
, NULL
);
92 #define _cleanup_close_ _cleanup_(closep)
93 #define _cleanup_fclose_ _cleanup_(fclosep)
94 #define _cleanup_pclose_ _cleanup_(pclosep)
95 #define _cleanup_closedir_ _cleanup_(closedirp)
96 #define _cleanup_close_pair_ _cleanup_(close_pairp)
98 int fd_nonblock(int fd
, bool nonblock
);
99 int stdio_disable_nonblock(void);
101 int fd_cloexec(int fd
, bool cloexec
);
102 int fd_cloexec_many(const int fds
[], size_t n_fds
, bool cloexec
);
104 int get_max_fd(void);
106 int close_all_fds(const int except
[], size_t n_except
);
107 int close_all_fds_without_malloc(const int except
[], size_t n_except
);
109 int pack_fds(int fds
[], size_t n
);
111 int fd_validate(int fd
);
112 int same_fd(int a
, int b
);
114 bool fdname_is_valid(const char *s
);
116 int fd_get_path(int fd
, char **ret
);
118 int move_fd(int from
, int to
, int cloexec
);
120 int fd_move_above_stdio(int fd
);
122 int rearrange_stdio(int original_input_fd
, int original_output_fd
, int original_error_fd
);
124 static inline int make_null_stdio(void) {
125 return rearrange_stdio(-EBADF
, -EBADF
, -EBADF
);
128 /* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
129 #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
131 /* Like free_and_replace(), but for file descriptors */
132 #define close_and_replace(a, b) \
135 safe_close(*_fdp_); \
136 *_fdp_ = TAKE_FD(b); \
140 int fd_reopen(int fd
, int flags
);
141 int fd_reopen_propagate_append_and_position(int fd
, int flags
);
142 int fd_reopen_condition(int fd
, int flags
, int mask
, int *ret_new_fd
);
144 int fd_is_opath(int fd
);
146 int fd_verify_safe_flags_full(int fd
, int extra_flags
);
147 static inline int fd_verify_safe_flags(int fd
) {
148 return fd_verify_safe_flags_full(fd
, 0);
151 int read_nr_open(void);
152 int fd_get_diskseq(int fd
, uint64_t *ret
);
154 int path_is_root_at(int dir_fd
, const char *path
);
155 static inline int path_is_root(const char *path
) {
156 return path_is_root_at(AT_FDCWD
, path
);
158 static inline int dir_fd_is_root(int dir_fd
) {
159 return path_is_root_at(dir_fd
, NULL
);
161 static inline int dir_fd_is_root_or_cwd(int dir_fd
) {
162 return dir_fd
== AT_FDCWD
? true : path_is_root_at(dir_fd
, NULL
);
165 int fds_are_same_mount(int fd1
, int fd2
);
167 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
168 #define PROC_FD_PATH_MAX \
169 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
171 char* format_proc_fd_path(char buf
[static PROC_FD_PATH_MAX
], int fd
);
173 #define FORMAT_PROC_FD_PATH(fd) \
174 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
176 /* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */
177 #define PROC_PID_FD_PATH_MAX \
178 (STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int))
180 char* format_proc_pid_fd_path(char buf
[static PROC_PID_FD_PATH_MAX
], pid_t pid
, int fd
);
182 /* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */
183 #define FORMAT_PROC_PID_FD_PATH(pid, fd) \
184 format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd))
186 int proc_fd_enoent_errno(void);
188 const char* accmode_to_string(int flags
);
190 /* Like ASSERT_PTR, but for fds */
191 #define ASSERT_FD(fd) \