]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include <sys/socket.h>
11 #include "stdio-util.h"
13 /* maximum length of fdname */
14 #define FDNAME_MAX 255
16 /* Make sure we can distinguish fd 0 and NULL */
17 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
18 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
20 /* Useful helpers for initializing pipe(), socketpair() or stdio fd arrays */
21 #define EBADF_PAIR { -EBADF, -EBADF }
22 #define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF }
24 int close_nointr(int fd
);
25 int safe_close(int fd
);
26 void safe_close_pair(int p
[static 2]);
28 static inline int safe_close_above_stdio(int fd
) {
29 if (fd
< 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -EBADF. */
32 return safe_close(fd
);
35 void close_many(const int fds
[], size_t n_fd
);
36 void close_many_unset(int fds
[], size_t n_fd
);
37 void close_many_and_free(int *fds
, size_t n_fds
);
39 int fclose_nointr(FILE *f
);
40 FILE* safe_fclose(FILE *f
);
41 DIR* safe_closedir(DIR *f
);
43 static inline void closep(int *fd
) {
47 static inline void close_pairp(int (*p
)[2]) {
51 static inline void fclosep(FILE **f
) {
55 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose
, NULL
);
56 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir
, NULL
);
58 #define _cleanup_close_ _cleanup_(closep)
59 #define _cleanup_fclose_ _cleanup_(fclosep)
60 #define _cleanup_pclose_ _cleanup_(pclosep)
61 #define _cleanup_closedir_ _cleanup_(closedirp)
62 #define _cleanup_close_pair_ _cleanup_(close_pairp)
64 int fd_nonblock(int fd
, bool nonblock
);
65 int fd_cloexec(int fd
, bool cloexec
);
66 int fd_cloexec_many(const int fds
[], size_t n_fds
, bool cloexec
);
70 int close_all_fds(const int except
[], size_t n_except
);
71 int close_all_fds_without_malloc(const int except
[], size_t n_except
);
73 int same_fd(int a
, int b
);
75 void cmsg_close_all(struct msghdr
*mh
);
77 bool fdname_is_valid(const char *s
);
79 int fd_get_path(int fd
, char **ret
);
81 int move_fd(int from
, int to
, int cloexec
);
83 int fd_move_above_stdio(int fd
);
85 int rearrange_stdio(int original_input_fd
, int original_output_fd
, int original_error_fd
);
87 static inline int make_null_stdio(void) {
88 return rearrange_stdio(-EBADF
, -EBADF
, -EBADF
);
91 /* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
92 #define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
94 /* Like free_and_replace(), but for file descriptors */
95 #define close_and_replace(a, b) \
99 *_fdp_ = TAKE_FD(b); \
103 int fd_reopen(int fd
, int flags
);
104 int fd_reopen_condition(int fd
, int flags
, int mask
, int *ret_new_fd
);
105 int fd_is_opath(int fd
);
106 int read_nr_open(void);
107 int fd_get_diskseq(int fd
, uint64_t *ret
);
109 int path_is_root_at(int dir_fd
, const char *path
);
110 static inline int path_is_root(const char *path
) {
111 return path_is_root_at(AT_FDCWD
, path
);
113 static inline int dir_fd_is_root(int dir_fd
) {
114 return path_is_root_at(dir_fd
, NULL
);
116 static inline int dir_fd_is_root_or_cwd(int dir_fd
) {
117 return dir_fd
== AT_FDCWD
? true : path_is_root_at(dir_fd
, NULL
);
120 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
121 #define PROC_FD_PATH_MAX \
122 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
124 static inline char *format_proc_fd_path(char buf
[static PROC_FD_PATH_MAX
], int fd
) {
127 assert_se(snprintf_ok(buf
, PROC_FD_PATH_MAX
, "/proc/self/fd/%i", fd
));
131 #define FORMAT_PROC_FD_PATH(fd) \
132 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
134 const char *accmode_to_string(int flags
);
136 /* Like ASSERT_PTR, but for fds */
137 #define ASSERT_FD(fd) \