[PATCH v8 03/14] libbb: implement close_cloexec_fds

Nadav Tasher tashernadav at gmail.com
Sun Mar 9 23:55:25 UTC 2025


This commit implements the close_cloexec_fds() function,
which manually closes all FDs that have the FD_CLOEXEC flag.

Signed-off-by: Nadav Tasher <tashernadav at gmail.com>
---
 include/libbb.h |  1 +
 libbb/xfuncs.c  | 10 ++++++++++
 2 files changed, 11 insertions(+)

diff --git a/include/libbb.h b/include/libbb.h
index bc9a7f907..ed2af5af2 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -581,6 +581,7 @@ int ndelay_off(int fd) FAST_FUNC;
 void close_on_exec_on(int fd) FAST_FUNC;
 void xdup2(int, int) FAST_FUNC;
 void xmove_fd(int, int) FAST_FUNC;
+void close_cloexec_fds(void) FAST_FUNC;
 
 
 DIR *xopendir(const char *path) FAST_FUNC;
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index b03af8542..552650131 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -46,6 +46,16 @@ void FAST_FUNC close_on_exec_on(int fd)
 	fcntl(fd, F_SETFD, FD_CLOEXEC);
 }
 
+void FAST_FUNC close_cloexec_fds(void)
+{
+	for (int fd = 0; fd < 1024; ++fd) {
+		/* if the FD has the FD_CLOEXEC flag, we need to
+		 * close it. */
+		if (fcntl(fd, F_GETFD) & FD_CLOEXEC)
+			close(fd);
+	}
+}
+
 char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
 {
 #ifndef IFNAMSIZ
-- 
2.43.0



More information about the busybox mailing list