1 #ifndef __XRDSYS_FD_H__ 2 #define __XRDSYS_FD_H__ 40 #include <sys/types.h> 41 #include <sys/socket.h> 50 #if ( defined(__linux__) || defined(__GNU__) ) && defined(SOCK_CLOEXEC) && defined(O_CLOEXEC) 51 inline int XrdSysFD_Accept(
int sockfd,
struct sockaddr *addr, socklen_t *addrlen)
52 {
return accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);}
54 inline int XrdSysFD_Dup(
int oldfd)
55 {
return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
57 inline int XrdSysFD_Dup1(
int oldfd,
int minfd)
58 {
return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
60 inline int XrdSysFD_Dup2(
int oldfd,
int newfd)
61 {
return dup3(oldfd, newfd, O_CLOEXEC);}
63 inline int XrdSysFD_Open(
const char *path,
int flags)
64 {
return open(path, flags|O_CLOEXEC);}
66 inline int XrdSysFD_Open(
const char *path,
int flags, mode_t mode)
67 {
return open(path, flags|O_CLOEXEC, mode);}
69 inline DIR* XrdSysFD_OpenDir(
const char *path)
71 if ((fd =
open(path, O_RDONLY|O_CLOEXEC)) < 0)
return 0;
72 DIR *dP = fdopendir(fd);
73 if (!dP) {
int rc = errno;
close(fd); errno = rc;}
77 inline int XrdSysFD_Pipe(
int pipefd[2])
78 {
return pipe2(pipefd, O_CLOEXEC);}
80 inline int XrdSysFD_Socket(
int domain,
int type,
int protocol)
81 {
return socket(domain, type|SOCK_CLOEXEC, protocol);}
83 inline int XrdSysFD_Socketpair(
int domain,
int type,
int protocol,
int sfd[2])
84 {
return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
86 inline int XrdSysFD_Accept(
int sockfd,
struct sockaddr *addr, socklen_t *addrlen)
87 {
int newfd = accept(sockfd, addr, addrlen);
88 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
92 inline int XrdSysFD_Dup(
int oldfd)
93 {
int newfd = dup(oldfd);
94 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
98 inline int XrdSysFD_Dup1(
int oldfd,
int minfd)
99 {
int newfd = fcntl(oldfd, F_DUPFD, minfd);
100 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
104 inline int XrdSysFD_Dup2(
int oldfd,
int newfd)
105 {
int rc = dup2(oldfd, newfd);
106 if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
110 inline int XrdSysFD_Open(
const char *path,
int flags)
111 {
int newfd =
open(path, flags);
112 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
116 inline int XrdSysFD_Open(
const char *path,
int flags, mode_t mode)
117 {
int newfd =
open(path, flags, mode);
118 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
122 inline DIR* XrdSysFD_OpenDir(
const char *path)
123 {
int fd = XrdSysFD_Open(path, O_RDONLY);
124 if (fd < 0)
return 0;
125 fcntl(fd, F_SETFD, FD_CLOEXEC);
126 DIR *dP = fdopendir(fd);
127 if (!dP) {
int rc = errno;
close(fd); errno = rc;}
131 inline int XrdSysFD_Pipe(
int pipefd[2])
132 {
int rc = pipe(pipefd);
133 if (!rc) {fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
134 fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
139 inline int XrdSysFD_Socket(
int domain,
int type,
int protocol)
140 {
int newfd = socket(domain, type, protocol);
141 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
145 inline int XrdSysFD_Socketpair(
int domain,
int type,
int protocol,
int sfd[2])
146 {
int rc = socketpair(domain, type, protocol, sfd);
147 if (!rc) {fcntl(sfd[0], F_SETFD, FD_CLOEXEC);
148 fcntl(sfd[1], F_SETFD, FD_CLOEXEC);
155 inline int XrdSysFD_Openat(
int dirfd,
const char *pathname,
int flags)
156 {
return openat(dirfd, pathname, flags | O_CLOEXEC);}
158 inline bool XrdSysFD_Yield(
int fd)
159 {
int fdFlags = fcntl(fd, F_GETFD);
160 if (fdFlags < 0)
return false;
161 return 0 == fcntl(fd, F_SETFD, fdFlags & ~FD_CLOEXEC);
#define close(a)
Definition: XrdPosix.hh:43
#define open
Definition: XrdPosix.hh:71