1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <sys/socket.h>
11 /* Make sure we can distinguish fd 0 and NULL */
12 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
13 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
15 int close_nointr(int fd);
16 int safe_close(int fd);
17 void safe_close_pair(int p[]);
19 static inline int safe_close_above_stdio(int fd) {
20 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */
23 return safe_close(fd);
26 void close_many(const int fds[], size_t n_fd);
28 int fclose_nointr(FILE *f);
29 FILE* safe_fclose(FILE *f);
30 #if 0 /// UNNEEDED by elogind
31 DIR* safe_closedir(DIR *f);
34 static inline void closep(int *fd) {
38 static inline void close_pairp(int (*p)[2]) {
42 static inline void fclosep(FILE **f) {
46 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
47 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
49 #define _cleanup_close_ _cleanup_(closep)
50 #define _cleanup_fclose_ _cleanup_(fclosep)
51 #define _cleanup_pclose_ _cleanup_(pclosep)
52 #define _cleanup_closedir_ _cleanup_(closedirp)
53 #define _cleanup_close_pair_ _cleanup_(close_pairp)
55 int fd_nonblock(int fd, bool nonblock);
56 int fd_cloexec(int fd, bool cloexec);
58 int close_all_fds(const int except[], size_t n_except);
60 #if 0 /// UNNEEDED by elogind
61 int same_fd(int a, int b);
63 void cmsg_close_all(struct msghdr *mh);
65 bool fdname_is_valid(const char *s);
68 int fd_get_path(int fd, char **ret);
70 int move_fd(int from, int to, int cloexec);
73 ACQUIRE_NO_DEV_NULL = 1 << 0,
74 ACQUIRE_NO_MEMFD = 1 << 1,
75 ACQUIRE_NO_PIPE = 1 << 2,
76 ACQUIRE_NO_TMPFILE = 1 << 3,
77 ACQUIRE_NO_REGULAR = 1 << 4,
80 int acquire_data_fd(const void *data, size_t size, unsigned flags);
82 #if 0 /// UNNEEDED by elogind
83 int fd_duplicate_data_fd(int fd);
86 /* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
87 #define ERRNO_IS_DISCONNECT(r) \
88 IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)
90 /* Resource exhaustion, could be our fault or general system trouble */
91 #define ERRNO_IS_RESOURCE(r) \
92 IN_SET(r, ENOMEM, EMFILE, ENFILE)
94 int fd_move_above_stdio(int fd);
96 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
98 static inline int make_null_stdio(void) {
99 return rearrange_stdio(-1, -1, -1);
102 /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
103 #define TAKE_FD(fd) \
110 int fd_reopen(int fd, int flags);
112 int read_nr_open(void);