From 85f37eb9f6fd8b633d312dbd5471d06a75762956 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 7 Jun 2024 00:06:49 +0200 Subject: Add cloexec --- c/cloexec/.gitignore | 1 + c/cloexec/Makefile | 8 ++++++++ c/cloexec/cloexec.c | 26 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 c/cloexec/.gitignore create mode 100644 c/cloexec/Makefile create mode 100644 c/cloexec/cloexec.c (limited to 'c/cloexec') diff --git a/c/cloexec/.gitignore b/c/cloexec/.gitignore new file mode 100644 index 0000000..089a255 --- /dev/null +++ b/c/cloexec/.gitignore @@ -0,0 +1 @@ +cloexec diff --git a/c/cloexec/Makefile b/c/cloexec/Makefile new file mode 100644 index 0000000..1c57a62 --- /dev/null +++ b/c/cloexec/Makefile @@ -0,0 +1,8 @@ +include ../base.mk + +all: cloexec +cloexec: cloexec.c + $(CC) $(CFLAGS) -o $@ $< + +clean: + rm -f cloexec 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 +#include +#include +#include +#include + +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); +} -- cgit v1.2.3