From: Noah Watkins Date: Sun, 21 Jul 2013 01:41:40 +0000 (-0700) Subject: pipe: use pipe2 feature test; check fcntl retval X-Git-Tag: v0.73~40^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F793%2Fhead;p=ceph.git pipe: use pipe2 feature test; check fcntl retval Signed-off-by: Noah Watkins --- diff --git a/configure.ac b/configure.ac index c344e7d6134..66d8f2161c9 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 9c01b322f89..9ec9e36e586 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 }