1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
13 #include <sys/socket.h>
17 /* Make sure we can distinguish fd 0 and NULL */
18 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
19 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
21 int close_nointr(int fd);
22 int safe_close(int fd);
23 void safe_close_pair(int p[]);
25 static inline int safe_close_above_stdio(int fd) {
26 if (fd < 3) /* Don't close stdin/stdout/stderr, but still invalidate the fd by returning -1 */
29 return safe_close(fd);
32 void close_many(const int fds[], size_t n_fd);
34 int fclose_nointr(FILE *f);
35 FILE* safe_fclose(FILE *f);
36 #if 0 /// UNNEEDED by elogind
37 DIR* safe_closedir(DIR *f);
40 static inline void closep(int *fd) {
44 static inline void close_pairp(int (*p)[2]) {
48 static inline void fclosep(FILE **f) {
52 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
53 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
55 #define _cleanup_close_ _cleanup_(closep)
56 #define _cleanup_fclose_ _cleanup_(fclosep)
57 #define _cleanup_pclose_ _cleanup_(pclosep)
58 #define _cleanup_closedir_ _cleanup_(closedirp)
59 #define _cleanup_close_pair_ _cleanup_(close_pairp)
61 int fd_nonblock(int fd, bool nonblock);
62 int fd_cloexec(int fd, bool cloexec);
64 int close_all_fds(const int except[], size_t n_except);
66 #if 0 /// UNNEEDED by elogind
67 int same_fd(int a, int b);
69 void cmsg_close_all(struct msghdr *mh);
71 bool fdname_is_valid(const char *s);
74 int fd_get_path(int fd, char **ret);
76 int move_fd(int from, int to, int cloexec);
79 ACQUIRE_NO_DEV_NULL = 1 << 0,
80 ACQUIRE_NO_MEMFD = 1 << 1,
81 ACQUIRE_NO_PIPE = 1 << 2,
82 ACQUIRE_NO_TMPFILE = 1 << 3,
83 ACQUIRE_NO_REGULAR = 1 << 4,
86 int acquire_data_fd(const void *data, size_t size, unsigned flags);
88 int fd_duplicate_data_fd(int fd);
90 /* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
91 #define ERRNO_IS_DISCONNECT(r) \
92 IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)
94 /* Resource exhaustion, could be our fault or general system trouble */
95 #define ERRNO_IS_RESOURCE(r) \
96 IN_SET(r, ENOMEM, EMFILE, ENFILE)
98 int fd_move_above_stdio(int fd);
100 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
102 static inline int make_null_stdio(void) {
103 return rearrange_stdio(-1, -1, -1);
106 /* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
107 #define TAKE_FD(fd) \
114 int fd_reopen(int fd, int flags);
116 int read_nr_open(void);