]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fd-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <sys/socket.h>
10 #include "stdio-util.h"
12 /* maximum length of fdname */
13 #define FDNAME_MAX 255
15 /* Make sure we can distinguish fd 0 and NULL */
16 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
17 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
19 int close_nointr(int fd
);
20 int safe_close(int fd
);
21 void safe_close_pair(int p
[static 2]);
23 static inline int safe_close_above_stdio(int fd
) {
24 if (fd
< 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */
27 return safe_close(fd
);
30 void close_many(const int fds
[], size_t n_fd
);
32 int fclose_nointr(FILE *f
);
33 FILE* safe_fclose(FILE *f
);
34 DIR* safe_closedir(DIR *f
);
36 static inline void closep(int *fd
) {
40 static inline void close_pairp(int (*p
)[2]) {
44 static inline void fclosep(FILE **f
) {
48 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, pclose
, NULL
);
49 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir
, NULL
);
51 #define _cleanup_close_ _cleanup_(closep)
52 #define _cleanup_fclose_ _cleanup_(fclosep)
53 #define _cleanup_pclose_ _cleanup_(pclosep)
54 #define _cleanup_closedir_ _cleanup_(closedirp)
55 #define _cleanup_close_pair_ _cleanup_(close_pairp)
57 int fd_nonblock(int fd
, bool nonblock
);
58 int fd_cloexec(int fd
, bool cloexec
);
59 int fd_cloexec_many(const int fds
[], size_t n_fds
, bool cloexec
);
63 int close_all_fds(const int except
[], size_t n_except
);
64 int close_all_fds_without_malloc(const int except
[], size_t n_except
);
66 int same_fd(int a
, int b
);
68 void cmsg_close_all(struct msghdr
*mh
);
70 bool fdname_is_valid(const char *s
);
72 int fd_get_path(int fd
, char **ret
);
74 int move_fd(int from
, int to
, int cloexec
);
77 ACQUIRE_NO_DEV_NULL
= 1 << 0,
78 ACQUIRE_NO_MEMFD
= 1 << 1,
79 ACQUIRE_NO_PIPE
= 1 << 2,
80 ACQUIRE_NO_TMPFILE
= 1 << 3,
81 ACQUIRE_NO_REGULAR
= 1 << 4,
84 int fd_move_above_stdio(int fd
);
86 int rearrange_stdio(int original_input_fd
, int original_output_fd
, int original_error_fd
);
88 static inline int make_null_stdio(void) {
89 return rearrange_stdio(-1, -1, -1);
92 /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
101 /* Like free_and_replace(), but for file descriptors */
102 #define close_and_replace(a, b) \
105 safe_close(*_fdp_); \
106 *_fdp_ = TAKE_FD(b); \
110 int fd_reopen(int fd
, int flags
);
111 int fd_reopen_condition(int fd
, int flags
, int mask
, int *ret_new_fd
);
112 int read_nr_open(void);
113 int fd_get_diskseq(int fd
, uint64_t *ret
);
115 /* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
116 #define PROC_FD_PATH_MAX \
117 (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
119 static inline char *format_proc_fd_path(char buf
[static PROC_FD_PATH_MAX
], int fd
) {
122 assert_se(snprintf_ok(buf
, PROC_FD_PATH_MAX
, "/proc/self/fd/%i", fd
));
126 #define FORMAT_PROC_FD_PATH(fd) \
127 format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))