00001 #ifndef __XRDSYS_FD_H__
00002 #define __XRDSYS_FD_H__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00038
00039
00040 #include <sys/types.h>
00041 #include <sys/socket.h>
00042 #include <unistd.h>
00043 #include <sys/stat.h>
00044 #include <fcntl.h>
00045
00046 namespace
00047 {
00048 #if defined(__linux__) && defined(SOCK_CLOEXEC) && defined(O_CLOEXEC)
00049 inline int XrdSysFD_Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
00050 {return accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);}
00051
00052 inline int XrdSysFD_Dup(int oldfd)
00053 {return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
00054
00055 inline int XrdSysFD_Dup1(int oldfd, int minfd)
00056 {return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
00057
00058 inline int XrdSysFD_Dup2(int oldfd, int newfd)
00059 {return dup3(oldfd, newfd, O_CLOEXEC);}
00060
00061 inline int XrdSysFD_Open(const char *path, int flags)
00062 {return open(path, flags|O_CLOEXEC);}
00063
00064 inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
00065 {return open(path, flags|O_CLOEXEC, mode);}
00066
00067 inline int XrdSysFD_Pipe(int pipefd[2])
00068 {return pipe2(pipefd, O_CLOEXEC);}
00069
00070 inline int XrdSysFD_Socket(int domain, int type, int protocol)
00071 {return socket(domain, type|SOCK_CLOEXEC, protocol);}
00072
00073 inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
00074 {return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
00075 #else
00076 inline int XrdSysFD_Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
00077 {int newfd = accept(sockfd, addr, addrlen);
00078 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
00079 return newfd;
00080 }
00081
00082 inline int XrdSysFD_Dup(int oldfd)
00083 {int newfd = dup(oldfd);
00084 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
00085 return newfd;
00086 }
00087
00088 inline int XrdSysFD_Dup1(int oldfd, int minfd)
00089 {int newfd = fcntl(oldfd, F_DUPFD, minfd);
00090 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
00091 return newfd;
00092 }
00093
00094 inline int XrdSysFD_Dup2(int oldfd, int newfd)
00095 {int rc = dup2(oldfd, newfd);
00096 if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
00097 return rc;
00098 }
00099
00100 inline int XrdSysFD_Open(const char *path, int flags)
00101 {int newfd = open(path, flags);
00102 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
00103 return newfd;
00104 }
00105
00106 inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
00107 {int newfd = open(path, flags, mode);
00108 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
00109 return newfd;
00110 }
00111
00112 inline int XrdSysFD_Pipe(int pipefd[2])
00113 {int rc = pipe(pipefd);
00114 if (!rc) {fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
00115 fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
00116 }
00117 return rc;
00118 }
00119
00120 inline int XrdSysFD_Socket(int domain, int type, int protocol)
00121 {int newfd = socket(domain, type, protocol);
00122 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
00123 return newfd;
00124 }
00125
00126 inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
00127 {int rc = socketpair(domain, type, protocol, sfd);
00128 if (!rc) {fcntl(sfd[0], F_SETFD, FD_CLOEXEC);
00129 fcntl(sfd[1], F_SETFD, FD_CLOEXEC);
00130 }
00131 return rc;
00132 }
00133 #endif
00134
00135 inline bool XrdSysFD_Yield(int fd)
00136 {int fdFlags = fcntl(fd, F_GETFD);
00137 if (fdFlags < 0) return false;
00138 return 0 == fcntl(fd, F_SETFD, fdFlags & ~FD_CLOEXEC);
00139 }
00140 }
00141 #endif