24 #if !defined HAVE_CLOSEFROM && !defined __WIN32__ 32 #ifdef HAVE_SYS_RESOURCE_H 33 # include <sys/types.h> 34 # include <sys/resource.h> 41 #elif defined __APPLE__ 42 # include <sys/attr.h> 55 int maxfd = fcntl(0, F_MAXFD);
56 if (maxfd >= 0)
return maxfd;
60 if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
61 rl.rlim_max != RLIM_INFINITY) {
62 return static_cast<int>(rl.rlim_max) - 1;
65 return static_cast<int>(sysconf(_SC_OPEN_MAX)) - 1;
93 if (fcntl(fd, F_CLOSEM, 0) >= 0)
95 #elif defined HAVE_GETDIRENTRIES && defined __linux__ 96 const char* path =
"/proc/self/fd";
97 int dir =
open(path, O_RDONLY|O_DIRECTORY);
106 ssize_t c = getdirentries(dir, buf,
sizeof(buf), &base);
116 for (ssize_t pos = 0; pos < c; pos += d->d_reclen) {
118 const char* leaf = d->
d_name;
151 while (
close(n) < 0 && errno == EINTR) { }
156 #elif defined __APPLE__ // macOS 157 const char* path =
"/dev/fd";
159 typedef unsigned int gdea_type;
161 typedef unsigned long gdea_type;
163 int dir =
open(path, O_RDONLY|O_DIRECTORY);
166 struct attrlist alist;
167 memset(&alist, 0,
sizeof(alist));
168 alist.bitmapcount = ATTR_BIT_MAP_COUNT;
169 alist.commonattr = ATTR_CMN_NAME;
178 gdea_type count =
sizeof(buf);
180 int r = getdirentriesattr(dir, &alist, buf,
sizeof(buf),
181 &count, &base, &new_state, 0);
188 while (count-- > 0) {
189 const char* leaf = p +
sizeof(u_int32_t);
190 p += *
static_cast<u_int32_t*
>(
static_cast<void*
>(p));
207 while (
close(n) < 0 && errno == EINTR) { }
222 char path[6 +
sizeof(pid_t) * 3 + 4];
224 snprintf(path,
sizeof(path),
"/proc/%ld/fd",
long(getpid()));
225 path[
sizeof(path) - 1] =
'\0';
227 sprintf(path,
"/proc/%ld/fd",
long(getpid()));
232 while (fd <= maxfd) {
235 while (
close(fd) < 0 && errno == EINTR) { }
Parse signed and unsigned type from string and check for trailing characters.
Cast a pointer we know is suitably aligned.
Implementation of closefrom() function.
WritableDatabase open()
Construct a WritableDatabase object for a new, empty InMemory database.
include <dirent.h>, with alternative implementation for windows.
std::enable_if< std::is_const< typename std::remove_pointer< U >::type >::value, T >::type alignment_cast(U ptr)
Cast a pointer we know is suitably aligned.
bool parse_signed(const char *p, T &res)
<unistd.h>, but with compat.
include <fcntl.h>, but working around broken platforms.