2 * @brief Implementation of closefrom() function.
4 /* Copyright (C) 2010,2011,2012 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 // We don't currently need closefrom() on __WIN32__.
24 #if !defined HAVE_CLOSEFROM && !defined __WIN32__
26 #include "closefrom.h"
28 #include "safeerrno.h"
29 #include "safefcntl.h"
30 #include "safeunistd.h"
32 #ifdef HAVE_SYS_RESOURCE_H
33 # include <sys/types.h>
34 # include <sys/resource.h>
37 #if defined __linux__ || defined __APPLE__
38 # include "safedirent.h"
47 int maxfd
= fcntl(0, F_MAXFD
);
48 if (maxfd
>= 0) return maxfd
;
52 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0 &&
53 rl
.rlim_max
!= RLIM_INFINITY
) {
54 return static_cast<int>(rl
.rlim_max
) - 1;
57 return static_cast<int>(sysconf(_SC_OPEN_MAX
)) - 1;
61 Xapian::Internal::closefrom(int fd
)
65 // Apparently supported by at least NetBSD, AIX, IRIX.
66 if (fcntl(fd
, F_CLOSEM
, 0) >= 0)
68 #elif defined __linux__ || defined __APPLE__
69 // The loop might close the fd associated with dir if we don't take
70 // special care to avoid that by either skipping this fd in the closing
71 // loop (if dirfd() is available) or making sure we have a free fd below
72 // the first we close in the loop.
73 #if !defined HAVE_DIRFD && !defined dirfd
74 // Make sure that the lowest fd we have been asked to close is closed, and
75 // then raise this lower bound - this should ensure that opendir() gets
76 // an fd below the new lower bound.
77 while (close(fd
) < 0 && errno
== EINTR
) { }
81 // Some platforms (e.g. AIX) have /proc/<pid>/fd but not /proc/self - if
82 // any such platforms don't have either closefrom() or F_CLOSEM then this
84 string path
= "/proc/";
85 path
+= str(getpid());
87 DIR * dir
= opendir(path
.c_str());
88 #elif defined __linux__
89 DIR * dir
= opendir("/proc/self/fd");
90 #elif defined __APPLE__ // Mac OS X
91 DIR * dir
= opendir("/dev/fd");
96 struct dirent
*entry
= readdir(dir
);
99 // Fallback if readdir() or closedir() fails.
104 ch
= entry
->d_name
[0];
105 if (ch
< '0' || ch
> '9')
107 int n
= atoi(entry
->d_name
);
109 #if defined HAVE_DIRFD || defined dirfd
110 if (n
== dirfd(dir
)) continue;
113 // Running under valgrind causes some entries above the
114 // reported RLIMIT_NOFILE value to appear in /proc/self/fd
115 // (https://bugs.kde.org/show_bug.cgi?id=191758). If we try
116 // to close these, valgrind issues a warning about trying to
117 // close an invalid file descriptor. These entries start at
118 // 1024, so we check that value first so we can usually avoid
119 // having to read the fd limit when we're not running under
129 while (close(n
) < 0 && errno
== EINTR
) { }
136 while (fd
<= maxfd
) {
137 // Retry on EINTR; just ignore other errors (we'll get EBADF if fd
138 // isn't open so that's OK).
139 while (close(fd
) < 0 && errno
== EINTR
) { }