From 3d2a9d35450876fe28708340e2b4ed3831d9d9af Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 10 Nov 2009 16:53:20 +0100 Subject: fd_util: added function pipe_cloexec() Same as pipe_cloexec_nonblock(), but doesn't set non-blocking mode. --- src/fd_util.c | 26 ++++++++++++++++++++++++-- src/fd_util.h | 7 +++++++ 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/fd_util.c b/src/fd_util.c index 8f142e34a..f296aab97 100644 --- a/src/fd_util.c +++ b/src/fd_util.c @@ -139,6 +139,30 @@ creat_cloexec(const char *path_fs, int mode) return fd; } +int +pipe_cloexec(int fd[2]) +{ +#ifdef WIN32 + return _pipe(event_pipe, 512, _O_BINARY); +#else + int ret; + +#ifdef HAVE_PIPE2 + ret = pipe2(fd, O_CLOEXEC); + if (ret >= 0 || errno != ENOSYS) + return ret; +#endif + + ret = pipe(fd); + if (ret >= 0) { + fd_set_cloexec(fd[0], true); + fd_set_cloexec(fd[1], true); + } + + return ret; +#endif +} + int pipe_cloexec_nonblock(int fd[2]) { @@ -158,10 +182,8 @@ pipe_cloexec_nonblock(int fd[2]) fd_set_cloexec(fd[0], true); fd_set_cloexec(fd[1], true); -#ifndef WIN32 fd_set_nonblock(fd[0]); fd_set_nonblock(fd[1]); -#endif } return ret; diff --git a/src/fd_util.h b/src/fd_util.h index 68a7d86ab..8b94a3a27 100644 --- a/src/fd_util.h +++ b/src/fd_util.h @@ -55,6 +55,13 @@ open_cloexec(const char *path_fs, int flags); int creat_cloexec(const char *path_fs, int mode); +/** + * Wrapper for pipe(), which sets the CLOEXEC flag (atomically if + * supported by the OS). + */ +int +pipe_cloexec(int fd[2]); + /** * Wrapper for pipe(), which sets the CLOEXEC flag (atomically if * supported by the OS). -- cgit v1.2.3