xapian-core  1.4.31
closefrom.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2010,2011,2012,2016,2018,2019,2026 Olly Betts
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <config.h>
22 
23 // We don't currently need closefrom() on __WIN32__.
24 #if !defined HAVE_CLOSEFROM && !defined __WIN32__
25 
26 #include "closefrom.h"
27 
28 #include <cerrno>
29 #include "safefcntl.h"
30 #include "safeunistd.h"
31 
32 #ifdef HAVE_SYS_RESOURCE_H
33 # include <sys/types.h>
34 # include <sys/resource.h>
35 #endif
36 
37 #if defined __linux__
38 # include "alignment_cast.h"
39 # include "safedirent.h"
40 # include "parseint.h"
41 #elif defined __APPLE__
42 # include <sys/attr.h>
43 # include <cstring>
44 # include "parseint.h"
45 #endif
46 
47 static int
49 #ifdef F_MAXFD
50  // May only be supported by NetBSD, modern versions of which implement
51  // closefrom(). Leave this in so that if other platforms have it or add
52  // it they will benefit.
53  int maxfd = fcntl(0, F_MAXFD);
54  if (maxfd >= 0) return maxfd;
55 #endif
56 #ifdef HAVE_GETRLIMIT
57  struct rlimit rl;
58  if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
59  rl.rlim_max != RLIM_INFINITY) {
60  return static_cast<int>(rl.rlim_max) - 1;
61  }
62 #endif
63  return static_cast<int>(sysconf(_SC_OPEN_MAX)) - 1;
64 }
65 
66 // These platforms are known to provide closefrom():
67 // FreeBSD >= 8.0, NetBSD >= 3.0, OpenBSD >= 3.5, Solaris >= 9,
68 // Linux >= 5.9 with glibc >= 2.34
69 //
70 // These platforms are known to support fcntl() with F_CLOSEM:
71 // AIX, NetBSD >= 2.0
72 //
73 // These platforms are known to provide close_range() but not closefrom():
74 // Cygwin >= 3.5.0, Android NDK >= r34
75 //
76 // These platforms have getdirentries() and a "magic" directory with an entry
77 // for each FD open in the current process:
78 // Linux (at least with glibc)
79 //
80 // These platforms have getdirentriesattr() and a "magic" directory with an
81 // entry for each FD open in the current process:
82 // macOS
83 //
84 // Other platforms just use a loop up to a limit obtained from
85 // fcntl(0, F_MAXFD), getrlimit(RLIMIT_NOFILE, ...), or sysconf(_SC_OPEN_MAX)
86 // - known examples:
87 // Android < NDK r34 (bionic libc doesn't provide getdirentries())
88 // Cygwin < 3.5.0
89 
90 void
92 {
93  int maxfd = -1;
94 #ifdef F_CLOSEM
95  if (fcntl(fd, F_CLOSEM, 0) >= 0)
96  return;
97 #elif defined HAVE_CLOSE_RANGE
98  if (close_range(fd, ~0U, 0) >= 0)
99  return;
100 #elif defined HAVE_GETDIRENTRIES && defined __linux__
101  const char* path = "/proc/self/fd";
102  int dir = open(path, O_RDONLY|O_DIRECTORY);
103  if (dir >= 0) {
104  off_t base = 0;
105  while (true) {
106  char buf[1024];
107  errno = 0;
108  // We use getdirentries() instead of opendir()/readdir() here
109  // because the latter can call malloc(), which isn't safe to do
110  // between fork() and exec() in a multi-threaded program.
111  ssize_t c = getdirentries(dir, buf, sizeof(buf), &base);
112  if (c == 0) {
113  close(dir);
114  return;
115  }
116  if (c < 0) {
117  // Fallback if getdirentries() fails.
118  break;
119  }
120  struct dirent* d;
121  for (ssize_t pos = 0; pos < c; pos += d->d_reclen) {
122  d = alignment_cast<struct dirent*>(buf + pos);
123  const char* leaf = d->d_name;
124  int n;
125  if (!parse_signed(leaf, n)) {
126  // Skip '.' and '..'.
127  continue;
128  }
129  if (n < fd) {
130  // FD below threshold.
131  continue;
132  }
133  if (n == dir) {
134  // Don't close the fd open on the directory.
135  continue;
136  }
137 
138  // Running under valgrind causes some entries above the
139  // reported RLIMIT_NOFILE value to appear in
140  // /proc/self/fd - see:
141  // https://bugs.kde.org/show_bug.cgi?id=191758
142  //
143  // If we try to close these, valgrind issues a warning about
144  // trying to close an invalid file descriptor. These entries
145  // start at 1024, so we check that value first so we can
146  // usually avoid having to read the fd limit when we're not
147  // running under valgrind.
148  if (n >= 1024) {
149  if (maxfd < 0)
150  maxfd = get_maxfd();
151  if (n > maxfd)
152  continue;
153  }
154 
155  // Retry on EINTR.
156  while (close(n) < 0 && errno == EINTR) { }
157  }
158  }
159  close(dir);
160  }
161 #elif defined __APPLE__ // macOS
162  const char* path = "/dev/fd";
163 #ifdef __LP64__
164  typedef unsigned int gdea_type;
165 #else
166  typedef unsigned long gdea_type;
167 #endif
168  int dir = open(path, O_RDONLY|O_DIRECTORY);
169  if (dir >= 0) {
170  gdea_type base = 0;
171  struct attrlist alist;
172  std::memset(&alist, 0, sizeof(alist));
173  alist.bitmapcount = ATTR_BIT_MAP_COUNT;
174  alist.commonattr = ATTR_CMN_NAME;
175  while (true) {
176  char buf[1024];
177  errno = 0;
178  // We use getdirentriesattr() instead of opendir()/readdir() here
179  // because the latter can call malloc(), which isn't safe to do
180  // between fork() and exec() in a multi-threaded program. We only
181  // want filename, but can't use getdirentries() because it's not
182  // available with 64-bit inode_t, which seems to be tied to LFS.
183  gdea_type count = sizeof(buf);
184  gdea_type new_state;
185  int r = getdirentriesattr(dir, &alist, buf, sizeof(buf),
186  &count, &base, &new_state, 0);
187  (void)new_state;
188  if (r < 0) {
189  // Fallback if getdirentriesattr() fails.
190  break;
191  }
192  char* p = buf;
193  while (count-- > 0) {
194  const char* leaf = p + sizeof(u_int32_t);
195  p += *static_cast<u_int32_t*>(static_cast<void*>(p));
196 
197  int n;
198  if (!parse_signed(leaf, n)) {
199  // Skip '.' and '..'.
200  continue;
201  }
202  if (n < fd) {
203  // FD below threshold.
204  continue;
205  }
206  if (n == dir) {
207  // Don't close the fd open on the directory.
208  continue;
209  }
210 
211  // Retry on EINTR.
212  while (close(n) < 0 && errno == EINTR) { }
213  }
214  if (r == 1) {
215  // We've had the last entry.
216  close(dir);
217  return;
218  }
219  }
220  close(dir);
221  }
222 #elif 0
223  // Some platforms have /proc/<pid>/fd but not /proc/self - if any such
224  // platforms don't have either closefrom() or F_CLOSEM but do have
225  // getdirentries() then this code can be used. AIX is an example of
226  // a platform of the former, but apparently has F_CLOSEM.
227  char path[6 + sizeof(pid_t) * 3 + 4];
228 # ifdef SNPRINTF
229  snprintf(path, sizeof(path), "/proc/%ld/fd", long(getpid()));
230  path[sizeof(path) - 1] = '\0';
231 # else
232  sprintf(path, "/proc/%ld/fd", long(getpid()));
233 # endif
234 #endif
235  if (maxfd < 0)
236  maxfd = get_maxfd();
237  while (fd <= maxfd) {
238  // Retry on EINTR; just ignore other errors (we'll get EBADF if fd
239  // isn't open so that's OK).
240  while (close(fd) < 0 && errno == EINTR) { }
241  ++fd;
242  }
243 }
244 
245 #endif
Cast a pointer we know is suitably aligned.
static int get_maxfd()
Definition: closefrom.cc:48
Implementation of closefrom() function.
int close(FD &fd)
Definition: fd.h:63
WritableDatabase open()
Construct a WritableDatabase object for a new, empty InMemory database.
Definition: dbfactory.h:104
void closefrom(int fd)
Definition: closefrom.cc:91
Parse signed and unsigned type from string and check for trailing characters.
bool parse_signed(const char *p, T &res)
Definition: parseint.h:43
include <dirent.h>, with alternative implementation for windows.
include <fcntl.h>, but working around broken platforms.
<unistd.h>, but with compat.
char * d_name
Definition: msvc_dirent.h:36