diff options
| author | Yabin Cui <yabinc@google.com> | 2019-01-18 11:11:57 -0800 |
|---|---|---|
| committer | Yabin Cui <yabinc@google.com> | 2019-01-18 14:25:13 -0800 |
| commit | e2841044a0eba0da363b06b37ee5265a48ef9238 (patch) | |
| tree | de82572e7d08e1e75c2c1ebf71d58a0af0e959dd /base/include/android-base | |
| parent | 77d895b73948d0944f988bba873bbedebc6356c6 (diff) | |
| download | system_core-e2841044a0eba0da363b06b37ee5265a48ef9238.tar.gz system_core-e2841044a0eba0da363b06b37ee5265a48ef9238.tar.bz2 system_core-e2841044a0eba0da363b06b37ee5265a48ef9238.zip | |
base: support optional flags in android::base::Pipe.
Bug: none
Test: build and use it manually.
Test: run libbase_tests.
Change-Id: I50b5251e8da23ddce94711807859612bf890406b
Diffstat (limited to 'base/include/android-base')
| -rw-r--r-- | base/include/android-base/unique_fd.h | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h index c8d12cf89..2c890b42d 100644 --- a/base/include/android-base/unique_fd.h +++ b/base/include/android-base/unique_fd.h @@ -161,22 +161,35 @@ using unique_fd = unique_fd_impl<DefaultCloser>; // Inline functions, so that they can be used header-only. template <typename Closer> -inline bool Pipe(unique_fd_impl<Closer>* read, unique_fd_impl<Closer>* write) { +inline bool Pipe(unique_fd_impl<Closer>* read, unique_fd_impl<Closer>* write, + int flags = O_CLOEXEC) { int pipefd[2]; #if defined(__linux__) - if (pipe2(pipefd, O_CLOEXEC) != 0) { + if (pipe2(pipefd, flags) != 0) { return false; } #else // defined(__APPLE__) + if (flags & ~(O_CLOEXEC | O_NONBLOCK)) { + return false; + } if (pipe(pipefd) != 0) { return false; } - if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 || fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) { - close(pipefd[0]); - close(pipefd[1]); - return false; + if (flags & O_CLOEXEC) { + if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 || fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) { + close(pipefd[0]); + close(pipefd[1]); + return false; + } + } + if (flags & O_NONBLOCK) { + if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0 || fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) { + close(pipefd[0]); + close(pipefd[1]); + return false; + } } #endif |
