4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/socket.h>
29 /* Make sure we can distinguish fd 0 and NULL */
30 #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
31 #define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
33 int close_nointr(int fd);
34 int safe_close(int fd);
35 void safe_close_pair(int p[]);
37 void close_many(const int fds[], unsigned n_fd);
39 int fclose_nointr(FILE *f);
40 FILE* safe_fclose(FILE *f);
41 #if 0 /// UNNEEDED by elogind
42 DIR* safe_closedir(DIR *f);
45 static inline void closep(int *fd) {
49 static inline void close_pairp(int (*p)[2]) {
53 static inline void fclosep(FILE **f) {
57 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
58 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
60 #define _cleanup_close_ _cleanup_(closep)
61 #define _cleanup_fclose_ _cleanup_(fclosep)
62 #define _cleanup_pclose_ _cleanup_(pclosep)
63 #define _cleanup_closedir_ _cleanup_(closedirp)
64 #define _cleanup_close_pair_ _cleanup_(close_pairp)
66 int fd_nonblock(int fd, bool nonblock);
67 int fd_cloexec(int fd, bool cloexec);
68 void stdio_unset_cloexec(void);
70 int close_all_fds(const int except[], unsigned n_except);
72 #if 0 /// UNNEEDED by elogind
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);
82 /* Hint: ENETUNREACH happens if we try to connect to "non-existing" special IP addresses, such as ::5 */
83 #define ERRNO_IS_DISCONNECT(r) \
84 IN_SET(r, ENOTCONN, ECONNRESET, ECONNREFUSED, ECONNABORTED, EPIPE, ENETUNREACH)