Use sysconf (_SC_OPEN_MAX) to find the upper limit. Or use 1024
if that fails.
https://bugs.kde.org/show_bug.cgi?id=467714
465435 m_libcfile.c:66 (vgPlain_safe_fd): Assertion 'newfd >= VG_(fd_hard_limit)' failed.
466104 aligned_alloc problems, part 1
467482 Build failure on aarch64 Alpine
+467714 fdleak_* and rlimit tests fail when parent process has more than
+ 64 descriptors opened
467839 Gdbserver: Improve compatibility of library directory name
n-i-bz FreeBSD rfork syscall fail with EINVAL or ENOSYS rather than VG_(unimplemented)
#define _FDLEAK_H_
#include <stdlib.h>
+#include <unistd.h>
#include <stdio.h>
#define DO(op) \
* - For Ubuntu 8.04, see also
* https://bugs.launchpad.net/ubuntu/+source/seahorse/+bug/235184
*/
-#define CLOSE_INHERITED_FDS { int i; for (i = 3; i < 64; i++) close(i); }
+static inline void close_inherited () {
+ int i; int max_fds = sysconf (_SC_OPEN_MAX);
+ if (max_fds < 0)
+ max_fds = 1024; /* Fallback if sysconf fails, returns -1. */
+
+ /* Only leave 0 (stdin), 1 (stdout) and 2 (stderr) open. */
+ for (i = 3; i < max_fds; i++)
+ close(i);
+}
+#define CLOSE_INHERITED_FDS close_inherited ()
#endif /* _FDLEAK_H_ */