Support: quest -f cjk_ngram
[xapian.git] / xapian-core / common / closefrom.cc
blobb5ae41f765331d350dcf1467c791b1777b123ecc
1 /** @file closefrom.cc
2 * @brief Implementation of closefrom() function.
3 */
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
21 #include <config.h>
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>
35 #endif
37 #if defined __linux__ || defined __APPLE__
38 # include "safedirent.h"
39 # include <cstdlib>
41 using namespace std;
42 #endif
44 static int
45 get_maxfd() {
46 #ifdef F_MAXFD
47 int maxfd = fcntl(0, F_MAXFD);
48 if (maxfd >= 0) return maxfd;
49 #endif
50 #ifdef HAVE_GETRLIMIT
51 struct rlimit rl;
52 if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
53 rl.rlim_max != RLIM_INFINITY) {
54 return static_cast<int>(rl.rlim_max) - 1;
56 #endif
57 return static_cast<int>(sysconf(_SC_OPEN_MAX)) - 1;
60 void
61 Xapian::Internal::closefrom(int fd)
63 int maxfd = -1;
64 #ifdef F_CLOSEM
65 // Apparently supported by at least NetBSD, AIX, IRIX.
66 if (fcntl(fd, F_CLOSEM, 0) >= 0)
67 return;
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) { }
78 ++fd;
79 #endif
80 #if 0
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
83 // code can be used.
84 string path = "/proc/";
85 path += str(getpid());
86 path += "/fd";
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");
92 #endif
93 if (dir) {
94 while (true) {
95 errno = 0;
96 struct dirent *entry = readdir(dir);
97 if (entry == NULL) {
98 closedir(dir);
99 // Fallback if readdir() or closedir() fails.
100 if (errno) break;
101 return;
103 char ch;
104 ch = entry->d_name[0];
105 if (ch < '0' || ch > '9')
106 continue;
107 int n = atoi(entry->d_name);
108 if (n >= fd) {
109 #if defined HAVE_DIRFD || defined dirfd
110 if (n == dirfd(dir)) continue;
111 #endif
112 #ifdef __linux__
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
120 // valgrind.
121 if (n >= 1024) {
122 if (maxfd < 0)
123 maxfd = get_maxfd();
124 if (n > maxfd)
125 continue;
127 #endif
128 // Retry on EINTR.
129 while (close(n) < 0 && errno == EINTR) { }
133 #endif
134 if (maxfd < 0)
135 maxfd = get_maxfd();
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) { }
140 ++fd;
144 #endif