File indexing completed on 2025-01-18 10:15:42
0001 #ifndef __XRDSYS_FD_H__
0002 #define __XRDSYS_FD_H__
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #include <sys/types.h>
0041 #include <sys/socket.h>
0042 #include <unistd.h>
0043 #include <sys/stat.h>
0044 #include <fcntl.h>
0045 #include <dirent.h>
0046 #include <cerrno>
0047
0048 namespace
0049 {
0050 #if ( defined(__linux__) || defined(__GNU__) ) && defined(SOCK_CLOEXEC) && defined(O_CLOEXEC)
0051 inline int XrdSysFD_Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
0052 {return accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);}
0053
0054 inline int XrdSysFD_Dup(int oldfd)
0055 {return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
0056
0057 inline int XrdSysFD_Dup1(int oldfd, int minfd)
0058 {return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
0059
0060 inline int XrdSysFD_Dup2(int oldfd, int newfd)
0061 {return dup3(oldfd, newfd, O_CLOEXEC);}
0062
0063 inline int XrdSysFD_Open(const char *path, int flags)
0064 {return open(path, flags|O_CLOEXEC);}
0065
0066 inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
0067 {return open(path, flags|O_CLOEXEC, mode);}
0068
0069 inline DIR* XrdSysFD_OpenDir(const char *path)
0070 {int fd;
0071 if ((fd = open(path, O_RDONLY|O_CLOEXEC)) < 0) return 0;
0072 DIR *dP = fdopendir(fd);
0073 if (!dP) {int rc = errno; close(fd); errno = rc;}
0074 return dP;
0075 }
0076
0077 inline int XrdSysFD_Pipe(int pipefd[2])
0078 {return pipe2(pipefd, O_CLOEXEC);}
0079
0080 inline int XrdSysFD_Socket(int domain, int type, int protocol)
0081 {return socket(domain, type|SOCK_CLOEXEC, protocol);}
0082
0083 inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
0084 {return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
0085 #else
0086 inline int XrdSysFD_Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
0087 {int newfd = accept(sockfd, addr, addrlen);
0088 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
0089 return newfd;
0090 }
0091
0092 inline int XrdSysFD_Dup(int oldfd)
0093 {int newfd = dup(oldfd);
0094 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
0095 return newfd;
0096 }
0097
0098 inline int XrdSysFD_Dup1(int oldfd, int minfd)
0099 {int newfd = fcntl(oldfd, F_DUPFD, minfd);
0100 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
0101 return newfd;
0102 }
0103
0104 inline int XrdSysFD_Dup2(int oldfd, int newfd)
0105 {int rc = dup2(oldfd, newfd);
0106 if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
0107 return rc;
0108 }
0109
0110 inline int XrdSysFD_Open(const char *path, int flags)
0111 {int newfd = open(path, flags);
0112 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
0113 return newfd;
0114 }
0115
0116 inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
0117 {int newfd = open(path, flags, mode);
0118 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
0119 return newfd;
0120 }
0121
0122 inline DIR* XrdSysFD_OpenDir(const char *path)
0123 {int fd = XrdSysFD_Open(path, O_RDONLY);
0124 if (fd < 0) return 0;
0125 fcntl(fd, F_SETFD, FD_CLOEXEC);
0126 DIR *dP = fdopendir(fd);
0127 if (!dP) {int rc = errno; close(fd); errno = rc;}
0128 return dP;
0129 }
0130
0131 inline int XrdSysFD_Pipe(int pipefd[2])
0132 {int rc = pipe(pipefd);
0133 if (!rc) {fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
0134 fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
0135 }
0136 return rc;
0137 }
0138
0139 inline int XrdSysFD_Socket(int domain, int type, int protocol)
0140 {int newfd = socket(domain, type, protocol);
0141 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
0142 return newfd;
0143 }
0144
0145 inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
0146 {int rc = socketpair(domain, type, protocol, sfd);
0147 if (!rc) {fcntl(sfd[0], F_SETFD, FD_CLOEXEC);
0148 fcntl(sfd[1], F_SETFD, FD_CLOEXEC);
0149 }
0150 return rc;
0151 }
0152 #endif
0153
0154
0155 inline int XrdSysFD_Openat(int dirfd, const char *pathname, int flags)
0156 {return openat(dirfd, pathname, flags | O_CLOEXEC);}
0157
0158 inline bool XrdSysFD_Yield(int fd)
0159 {int fdFlags = fcntl(fd, F_GETFD);
0160 if (fdFlags < 0) return false;
0161 return 0 == fcntl(fd, F_SETFD, fdFlags & ~FD_CLOEXEC);
0162 }
0163 }
0164 #endif