41#include <sys/socket.h>
50#if ( defined(__linux__) || defined(__GNU__) ) && defined(SOCK_CLOEXEC) && defined(O_CLOEXEC)
51inline int XrdSysFD_Accept(
int sockfd,
struct sockaddr *addr, socklen_t *addrlen)
52 {
return accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);}
54inline int XrdSysFD_Dup(
int oldfd)
55 {
return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
57inline int XrdSysFD_Dup1(
int oldfd,
int minfd)
58 {
return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
60inline int XrdSysFD_Dup2(
int oldfd,
int newfd)
61 {
return dup3(oldfd, newfd, O_CLOEXEC);}
63inline int XrdSysFD_Open(
const char *path,
int flags)
64 {
return open(path, flags|O_CLOEXEC);}
66inline int XrdSysFD_Open(
const char *path,
int flags, mode_t mode)
67 {
return open(path, flags|O_CLOEXEC, mode);}
69inline 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;}
77inline int XrdSysFD_Pipe(
int pipefd[2])
78 {
return pipe2(pipefd, O_CLOEXEC);}
80inline int XrdSysFD_Socket(
int domain,
int type,
int protocol)
81 {
return socket(domain, type|SOCK_CLOEXEC, protocol);}
83inline int XrdSysFD_Socketpair(
int domain,
int type,
int protocol,
int sfd[2])
84 {
return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
86inline 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);
92inline int XrdSysFD_Dup(
int oldfd)
93 {
int newfd = dup(oldfd);
94 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
98inline 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);
104inline int XrdSysFD_Dup2(
int oldfd,
int newfd)
105 {
int rc = dup2(oldfd, newfd);
106 if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
110inline int XrdSysFD_Open(
const char *path,
int flags)
111 {
int newfd =
open(path, flags);
112 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
116inline 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);
122inline 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;}
131inline 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);
139inline 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);
145inline 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);
155inline int XrdSysFD_Openat(
int dirfd,
const char *pathname,
int flags)
156 {
return openat(dirfd, pathname, flags | O_CLOEXEC);}
158inline 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