-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdSysFD.hh
160 lines (138 loc) · 7.17 KB
/
XrdSysFD.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef __XRDSYS_FD_H__
#define __XRDSYS_FD_H__
/******************************************************************************/
/* */
/* X r d S y s F D . h h */
/* */
/* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
//-----------------------------------------------------------------------------
//! XrdSysFD defines a set of alternate functions that make sure that the
//! CLOEXEC attribute is associated with any new file descriptors returned by
//! by commonly used functions. This is platform sensitive as some platforms
//! allow atomic setting of the attribute while others do not. These functions
//! are used to provide platform portability.
//-----------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
namespace
{
#if defined(__linux__) && defined(SOCK_CLOEXEC) && defined(O_CLOEXEC)
inline int XrdSysFD_Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{return accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);}
inline int XrdSysFD_Dup(int oldfd)
{return fcntl(oldfd, F_DUPFD_CLOEXEC, 0);}
inline int XrdSysFD_Dup1(int oldfd, int minfd)
{return fcntl(oldfd, F_DUPFD_CLOEXEC, minfd);}
inline int XrdSysFD_Dup2(int oldfd, int newfd)
{return dup3(oldfd, newfd, O_CLOEXEC);}
inline int XrdSysFD_Open(const char *path, int flags)
{return open(path, flags|O_CLOEXEC);}
inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
{return open(path, flags|O_CLOEXEC, mode);}
inline DIR* XrdSysFD_OpenDir(const char *path)
{int fd;
if ((fd = open(path, O_RDONLY|O_CLOEXEC)) < 0) return 0;
DIR *dP = fdopendir(fd);
if (!dP) {int rc = errno; close(fd); errno = rc;}
return dP;
}
inline int XrdSysFD_Pipe(int pipefd[2])
{return pipe2(pipefd, O_CLOEXEC);}
inline int XrdSysFD_Socket(int domain, int type, int protocol)
{return socket(domain, type|SOCK_CLOEXEC, protocol);}
inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
{return socketpair(domain, type|SOCK_CLOEXEC, protocol, sfd);}
#else
inline int XrdSysFD_Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{int newfd = accept(sockfd, addr, addrlen);
if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
return newfd;
}
inline int XrdSysFD_Dup(int oldfd)
{int newfd = dup(oldfd);
if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
return newfd;
}
inline int XrdSysFD_Dup1(int oldfd, int minfd)
{int newfd = fcntl(oldfd, F_DUPFD, minfd);
if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
return newfd;
}
inline int XrdSysFD_Dup2(int oldfd, int newfd)
{int rc = dup2(oldfd, newfd);
if (!rc) fcntl(newfd, F_SETFD, FD_CLOEXEC);
return rc;
}
inline int XrdSysFD_Open(const char *path, int flags)
{int newfd = open(path, flags);
if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
return newfd;
}
inline int XrdSysFD_Open(const char *path, int flags, mode_t mode)
{int newfd = open(path, flags, mode);
if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
return newfd;
}
inline DIR* XrdSysFD_OpenDir(const char *path)
{int fd = XrdSysFD_Open(path, O_RDONLY);
if (fd < 0) return 0;
fcntl(fd, F_SETFD, FD_CLOEXEC);
DIR *dP = fdopendir(fd);
if (!dP) {int rc = errno, close(fd); errno = rc;}
return dP;
}
inline int XrdSysFD_Pipe(int pipefd[2])
{int rc = pipe(pipefd);
if (!rc) {fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
}
return rc;
}
inline int XrdSysFD_Socket(int domain, int type, int protocol)
{int newfd = socket(domain, type, protocol);
if (newfd >= 0) fcntl(newfd, F_SETFD, FD_CLOEXEC);
return newfd;
}
inline int XrdSysFD_Socketpair(int domain, int type, int protocol, int sfd[2])
{int rc = socketpair(domain, type, protocol, sfd);
if (!rc) {fcntl(sfd[0], F_SETFD, FD_CLOEXEC);
fcntl(sfd[1], F_SETFD, FD_CLOEXEC);
}
return rc;
}
#endif
inline bool XrdSysFD_Yield(int fd)
{int fdFlags = fcntl(fd, F_GETFD);
if (fdFlags < 0) return false;
return 0 == fcntl(fd, F_SETFD, fdFlags & ~FD_CLOEXEC);
}
}
#endif