Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:42

0001 #ifndef __XRDSYS_FD_H__
0002 #define __XRDSYS_FD_H__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                           X r d S y s F D . h h                            */
0006 /*                                                                            */
0007 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0009 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
0010 /*                                                                            */
0011 /* This file is part of the XRootD software suite.                            */
0012 /*                                                                            */
0013 /* XRootD is free software: you can redistribute it and/or modify it under    */
0014 /* the terms of the GNU Lesser General Public License as published by the     */
0015 /* Free Software Foundation, either version 3 of the License, or (at your     */
0016 /* option) any later version.                                                 */
0017 /*                                                                            */
0018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0020 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0021 /* License for more details.                                                  */
0022 /*                                                                            */
0023 /* You should have received a copy of the GNU Lesser General Public License   */
0024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0025 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0026 /*                                                                            */
0027 /* The copyright holder's institutional names and contributor's names may not */
0028 /* be used to endorse or promote products derived from this software without  */
0029 /* specific prior written permission of the institution or contributor.       */
0030 /******************************************************************************/
0031 
0032 //-----------------------------------------------------------------------------
0033 //! XrdSysFD defines a set of alternate functions that make sure that the
0034 //! CLOEXEC attribute is associated with any new file descriptors returned by
0035 //! by commonly used functions. This is platform sensitive as some platforms
0036 //! allow atomic setting of the attribute while others do not. These functions
0037 //! are used to provide platform portability.
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 // openat is part of POSIX.1-2008; in Linux, BSD, and Solaris
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