From 59a55fdaaf2c66aae7de151949ba46981da4696e Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Sat, 20 Jul 2013 18:41:40 -0700 Subject: [PATCH] pipe: use pipe2 feature test; check fcntl retval Signed-off-by: Noah Watkins --- configure.ac | 1 + src/common/pipe.c | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index c344e7d6134b..66d8f2161c99 100644 --- a/configure.ac +++ b/configure.ac @@ -538,6 +538,7 @@ AC_CHECK_FUNC([fallocate], AC_CHECK_HEADERS([sys/prctl.h]) AC_CHECK_FUNCS([prctl]) +AC_CHECK_FUNCS([pipe2]) # Checks for typedefs, structures, and compiler characteristics. #AC_HEADER_STDBOOL diff --git a/src/common/pipe.c b/src/common/pipe.c index 9c01b322f89d..9ec9e36e586b 100644 --- a/src/common/pipe.c +++ b/src/common/pipe.c @@ -11,6 +11,7 @@ * Foundation. See file COPYING. * */ +#include "acconfig.h" #include "common/pipe.h" @@ -20,24 +21,40 @@ int pipe_cloexec(int pipefd[2]) { -#if defined(O_CLOEXEC) && !defined(__FreeBSD__) int ret; + +#if defined(HAVE_PIPE2) && defined(O_CLOEXEC) ret = pipe2(pipefd, O_CLOEXEC); - if (ret) { - ret = -errno; - return ret; - } + if (ret == -1) + return -errno; return 0; #else - /* The old-fashioned, race-condition prone way that we have to fall back on if - * O_CLOEXEC does not exist. */ - int ret = pipe(pipefd); - if (ret) { + ret = pipe(pipefd); + if (ret == -1) + return -errno; + + /* + * The old-fashioned, race-condition prone way that we have to fall + * back on if O_CLOEXEC does not exist. + */ + ret = fcntl(pipefd[0], F_SETFD, FD_CLOEXEC); + if (ret == -1) { + ret = -errno; + goto out; + } + + ret = fcntl(pipefd[1], F_SETFD, FD_CLOEXEC); + if (ret == -1) { ret = -errno; - return ret; + goto out; } - fcntl(pipefd[0], F_SETFD, FD_CLOEXEC); - fcntl(pipefd[1], F_SETFD, FD_CLOEXEC); + return 0; + +out: + TEMP_FAILURE_RETRY(close(pipefd[0])); + TEMP_FAILURE_RETRY(close(pipefd[1])); + + return ret; #endif } -- 2.47.3