xrootd
Loading...
Searching...
No Matches
XrdSysFD.hh
Go to the documentation of this file.
1#ifndef __XRDSYS_FD_H__
2#define __XRDSYS_FD_H__
3/******************************************************************************/
4/* */
5/* X r d S y s F D . h h */
6/* */
7/* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */
8/* Produced by Andrew Hanushevsky for Stanford University under contract */
9/* DE-AC02-76-SFO0515 with the Department of Energy */
10/* */
11/* This file is part of the XRootD software suite. */
12/* */
13/* XRootD is free software: you can redistribute it and/or modify it under */
14/* the terms of the GNU Lesser General Public License as published by the */
15/* Free Software Foundation, either version 3 of the License, or (at your */
16/* option) any later version. */
17/* */
18/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21/* License for more details. */
22/* */
23/* You should have received a copy of the GNU Lesser General Public License */
24/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26/* */
27/* The copyright holder's institutional names and contributor's names may not */
28/* be used to endorse or promote products derived from this software without */
29/* specific prior written permission of the institution or contributor. */
30/******************************************************************************/
31
32//-----------------------------------------------------------------------------
38//-----------------------------------------------------------------------------
39
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <unistd.h>
43#include <sys/stat.h>
44#include <fcntl.h>
45#include <dirent.h>
46#include <cerrno>
47
48namespace
49{
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);}
53
54inline int XrdSysFD_Dup(int oldfd)
55 {return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
56
57inline int XrdSysFD_Dup1(int oldfd, int minfd)
58 {return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
59
60inline int XrdSysFD_Dup2(int oldfd, int newfd)
61 {return dup3(oldfd, newfd, O_CLOEXEC);}
62
63inline int XrdSysFD_Open(const char *path, int flags)
64 {return open(path, flags|O_CLOEXEC);}
65
66inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
67 {return open(path, flags|O_CLOEXEC, mode);}
68
69inline DIR* XrdSysFD_OpenDir(const char *path)
70 {int fd;
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;}
74 return dP;
75 }
76
77inline int XrdSysFD_Pipe(int pipefd[2])
78 {return pipe2(pipefd, O_CLOEXEC);}
79
80inline int XrdSysFD_Socket(int domain, int type, int protocol)
81 {return socket(domain, type|SOCK_CLOEXEC, protocol);}
82
83inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
84 {return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
85#else
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);
89 return newfd;
90 }
91
92inline int XrdSysFD_Dup(int oldfd)
93 {int newfd = dup(oldfd);
94 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
95 return newfd;
96 }
97
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);
101 return newfd;
102 }
103
104inline int XrdSysFD_Dup2(int oldfd, int newfd)
105 {int rc = dup2(oldfd, newfd);
106 if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
107 return rc;
108 }
109
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);
113 return newfd;
114 }
115
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);
119 return newfd;
120 }
121
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;}
128 return dP;
129 }
130
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);
135 }
136 return rc;
137 }
138
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);
142 return newfd;
143 }
144
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);
149 }
150 return rc;
151 }
152#endif
153
154// openat is part of POSIX.1-2008; in Linux, BSD, and Solaris
155inline int XrdSysFD_Openat(int dirfd, const char *pathname, int flags)
156 {return openat(dirfd, pathname, flags | O_CLOEXEC);}
157
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);
162 }
163}
164#endif
#define close(a)
Definition XrdPosix.hh:43
#define open
Definition XrdPosix.hh:71