aboutsummaryrefslogtreecommitdiff
path: root/c/cloexec/cloexec.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-06-07 00:06:49 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-06-07 00:06:49 +0200
commit85f37eb9f6fd8b633d312dbd5471d06a75762956 (patch)
tree4d5748ee166ef19773a007befecf0c220d51bb82 /c/cloexec/cloexec.c
parentcdf2238a33d63947ad70b0c26a3b4f4fe1f18454 (diff)
Add cloexec
Diffstat (limited to 'c/cloexec/cloexec.c')
-rw-r--r--c/cloexec/cloexec.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/c/cloexec/cloexec.c b/c/cloexec/cloexec.c
new file mode 100644
index 0000000..ba899ee
--- /dev/null
+++ b/c/cloexec/cloexec.c
@@ -0,0 +1,26 @@
+#define _GNU_SOURCE
+#include <assert.h>
+#include <err.h>
+#include <paths.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ int fd, nfd;
+
+ if ((fd = open(_PATH_DEVNULL, O_RDONLY | O_CLOEXEC)) == -1)
+ err(1, "open: %s", _PATH_DEVNULL);
+
+ if ((nfd = dup(fd)) == -1)
+ err(1, "dup");
+ assert((fcntl(nfd, F_GETFD) & FD_CLOEXEC) == 0);
+ close(nfd);
+ if (dup3(fd, nfd, O_CLOEXEC) == -1)
+ err(1, "dup");
+ assert((fcntl(nfd, F_GETFD) & FD_CLOEXEC) != 0);
+
+ close(nfd);
+ close(fd);
+}