aboutsummaryrefslogtreecommitdiff
path: root/src/shared/misc.c
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2023-08-30 13:17:07 +0200
committerWilliam Hubbs <w.d.hubbs@gmail.com>2023-09-12 22:53:12 -0500
commitc199c5cf6e39d6e21b92bddba7beeebcc3afca79 (patch)
tree2c6f39ffe0f23aace0e70e8766fc731af3ba0f80 /src/shared/misc.c
parent5bfb592d75f2e2ccfc94cb510c5eca755767dfbb (diff)
misc: add cloexec_fds_from() helper function
Move logic to set file descriptors to a cloexec_fds_from() function in misc.c so it can be shared by both supervisor-daemon and start-stop-daemon, and hide the details behind.
Diffstat (limited to 'src/shared/misc.c')
-rw-r--r--src/shared/misc.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/shared/misc.c b/src/shared/misc.c
index c987ce8c..429407d4 100644
--- a/src/shared/misc.c
+++ b/src/shared/misc.c
@@ -15,6 +15,11 @@
* except according to the terms contained in the LICENSE file.
*/
+#ifdef HAVE_CLOSE_RANGE
+/* For close_range() */
+# define _GNU_SOURCE
+#endif
+
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
@@ -500,3 +505,25 @@ pid_t get_pid(const char *applet,const char *pidfile)
return pid;
}
+
+#ifndef HAVE_CLOSE_RANGE
+static inline int close_range(int first RC_UNUSED,
+ int last RC_UNUSED,
+ unsigned int flags RC_UNUSED)
+{
+ return -1;
+}
+#endif
+#ifndef CLOSE_RANGE_CLOEXEC
+# define CLOSE_RANGE_CLOEXEC (1U << 2)
+#endif
+
+void
+cloexec_fds_from(int first)
+{
+ int i;
+ if (close_range(first, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0) {
+ for (i = getdtablesize() - 1; i >= first; --i)
+ fcntl(i, F_SETFD, FD_CLOEXEC);
+ }
+}