MINOR: fd: add a new my_closefrom() function to close all FDs
This is a naive implementation of closefrom() which closes all FDs
starting from the one passed in argument. closefrom() is not provided
on all operating systems, and other versions will follow.
diff --git a/src/fd.c b/src/fd.c
index 9434c63..92d093c 100644
--- a/src/fd.c
+++ b/src/fd.c
@@ -149,6 +149,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
+#include <sys/resource.h>
#include <common/compat.h>
#include <common/config.h>
@@ -460,6 +461,27 @@
fdlist_process_cached_events(&fd_cache);
}
+/* This is a portable implementation of closefrom(). It closes all open file
+ * descriptors starting at <start> and above. This is a naive version for use
+ * when the operating system provides no alternative.
+ */
+void my_closefrom(int start)
+{
+ struct rlimit limit;
+ int nbfds;
+
+ if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
+ nbfds = limit.rlim_cur;
+ else
+ nbfds = 0;
+
+ if (nbfds <= 0)
+ nbfds = 1024; /* safe limit */
+
+ while (start < nbfds)
+ close(start++);
+}
+
/* disable the specified poller */
void disable_poller(const char *poller_name)
{