From 49e767ccb9dcfd2161b89f0d7a980eb85f056c5d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 4 Dec 2016 11:39:55 -0500 Subject: Move portability code into util.h --- util.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index aef5fbb..bc611a8 100644 --- a/util.c +++ b/util.c @@ -58,3 +58,21 @@ int redirect(int fd, const char *path, int flags, ...) { return ret; } + +int dup_cloexec(int fd) { +#ifdef F_DUPFD_CLOEXEC + return fcntl(fd, F_DUPFD_CLOEXEC, 0); +#else + int ret = dup(fd); + if (ret < 0) { + return -1; + } + + if (fcntl(ret, F_SETFD, FD_CLOEXEC) == -1) { + close(ret); + return -1; + } + + return ret; +#endif +} -- cgit v1.2.3