xrootd
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
46namespace
47{
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);}
51
52inline int XrdSysFD_Dup(int oldfd)
53 {return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
54
55inline int XrdSysFD_Dup1(int oldfd, int minfd)
56 {return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
57
58inline int XrdSysFD_Dup2(int oldfd, int newfd)
59 {return dup3(oldfd, newfd, O_CLOEXEC);}
60
61inline int XrdSysFD_Open(const char *path, int flags)
62 {return open(path, flags|O_CLOEXEC);}
63
64inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
65 {return open(path, flags|O_CLOEXEC, mode);}
66
67inline int XrdSysFD_Pipe(int pipefd[2])
68 {return pipe2(pipefd, O_CLOEXEC);}
69
70inline int XrdSysFD_Socket(int domain, int type, int protocol)
71 {return socket(domain, type|SOCK_CLOEXEC, protocol);}
72
73inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
74 {return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
75#else
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);
79 return newfd;
80 }
81
82inline int XrdSysFD_Dup(int oldfd)
83 {int newfd = dup(oldfd);
84 if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
85 return newfd;
86 }
87
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);
91 return newfd;
92 }
93
94inline int XrdSysFD_Dup2(int oldfd, int newfd)
95 {int rc = dup2(oldfd, newfd);
96 if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
97 return rc;
98 }
99
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);
103 return newfd;
104 }
105
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);
109 return newfd;
110 }
111
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);
116 }
117 return rc;
118 }
119
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);
123 return newfd;
124 }
125
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);
130 }
131 return rc;
132 }
133#endif
134
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);
139 }
140}
141#endif
#define open
Definition: XrdPosix.hh:71