41#include <sys/socket.h>
48#if defined(__linux__) && defined(SOCK_CLOEXEC) && defined(O_CLOEXEC)
49inline int XrdSysFD_Accept(
int sockfd,
struct sockaddr *addr, socklen_t *addrlen)
50 {
return accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);}
52inline int XrdSysFD_Dup(
int oldfd)
53 {
return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
55inline int XrdSysFD_Dup1(
int oldfd,
int minfd)
56 {
return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
58inline int XrdSysFD_Dup2(
int oldfd,
int newfd)
59 {
return dup3(oldfd, newfd, O_CLOEXEC);}
61inline int XrdSysFD_Open(
const char *path,
int flags)
62 {
return open(path, flags|O_CLOEXEC);}
64inline int XrdSysFD_Open(
const char *path,
int flags, mode_t mode)
65 {
return open(path, flags|O_CLOEXEC, mode);}
67inline int XrdSysFD_Pipe(
int pipefd[2])
68 {
return pipe2(pipefd, O_CLOEXEC);}
70inline int XrdSysFD_Socket(
int domain,
int type,
int protocol)
71 {
return socket(domain, type|SOCK_CLOEXEC, protocol);}
73inline int XrdSysFD_Socketpair(
int domain,
int type,
int protocol,
int sfd[2])
74 {
return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
76inline int XrdSysFD_Accept(
int sockfd,
struct sockaddr *addr, socklen_t *addrlen)
77 {
int newfd = accept(sockfd, addr, addrlen);
78 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
82inline int XrdSysFD_Dup(
int oldfd)
83 {
int newfd = dup(oldfd);
84 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
88inline int XrdSysFD_Dup1(
int oldfd,
int minfd)
89 {
int newfd = fcntl(oldfd, F_DUPFD, minfd);
90 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
94inline int XrdSysFD_Dup2(
int oldfd,
int newfd)
95 {
int rc = dup2(oldfd, newfd);
96 if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
100inline int XrdSysFD_Open(
const char *path,
int flags)
101 {
int newfd =
open(path, flags);
102 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
106inline int XrdSysFD_Open(
const char *path,
int flags, mode_t mode)
107 {
int newfd =
open(path, flags, mode);
108 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
112inline int XrdSysFD_Pipe(
int pipefd[2])
113 {
int rc = pipe(pipefd);
114 if (!rc) {fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
115 fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
120inline int XrdSysFD_Socket(
int domain,
int type,
int protocol)
121 {
int newfd = socket(domain, type, protocol);
122 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
126inline int XrdSysFD_Socketpair(
int domain,
int type,
int protocol,
int sfd[2])
127 {
int rc = socketpair(domain, type, protocol, sfd);
128 if (!rc) {fcntl(sfd[0], F_SETFD, FD_CLOEXEC);
129 fcntl(sfd[1], F_SETFD, FD_CLOEXEC);
135inline bool XrdSysFD_Yield(
int fd)
136 {
int fdFlags = fcntl(fd, F_GETFD);
137 if (fdFlags < 0)
return false;
138 return 0 == fcntl(fd, F_SETFD, fdFlags & ~FD_CLOEXEC);
#define open
Definition: XrdPosix.hh:71