return (int) (m - 1);
}
-int close_all_fds(int except[], size_t n_except) {
+int close_all_fds_full(int except[], size_t n_except, bool allow_alloc) {
static bool have_close_range = true; /* Assume we live in the future */
_cleanup_closedir_ DIR *d = NULL;
int r = 0;
/* Fallback for when close_range() is not supported */
opendir_fallback:
- d = opendir("/proc/self/fd");
+ d = allow_alloc ? opendir("/proc/self/fd") : NULL;
if (d) {
struct dirent *de;
return r;
}
- /* Fallback for when /proc isn't available (for example in chroots) by brute-forcing through the file
- * descriptor table. */
+ /* Fallback for when /proc isn't available (for example in chroots) or when we cannot allocate by
+ * brute-forcing through the file descriptor table. */
int max_fd = get_max_fd();
if (max_fd < 0)
int fd_nonblock(int fd, bool nonblock);
int fd_cloexec(int fd, bool cloexec);
-int close_all_fds(int except[], size_t n_except);
+int close_all_fds_full(int except[], size_t n_except, bool allow_alloc);
+static inline int close_all_fds(int except[], size_t n_except) {
+ return close_all_fds_full(except, n_except, true);
+}
int same_fd(int a, int b);
log_close();
/* Make sure nobody waits for us on a socket anymore */
- (void) close_all_fds(NULL, 0);
+ (void) close_all_fds_full(NULL, 0, false);
sync();