Zachary Turner | b2df30d | 2014-10-08 20:38:41 +0000 | [diff] [blame] | 1 | //===-- PipePosix.cpp -------------------------------------------*- C++ -*-===// |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | b2df30d | 2014-10-08 20:38:41 +0000 | [diff] [blame] | 10 | #include "lldb/Host/posix/PipePosix.h" |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 11 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | ee1f578 | 2016-08-10 22:43:48 +0000 | [diff] [blame] | 12 | #include "lldb/Utility/SelectHelper.h" |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallString.h" |
| 14 | #include "llvm/Support/FileSystem.h" |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 15 | |
Omair Javaid | 2040548 | 2015-08-09 19:04:41 +0000 | [diff] [blame] | 16 | #if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)) |
Stephane Sezer | 4d640f2 | 2015-09-09 01:17:24 +0000 | [diff] [blame] | 17 | #ifndef _GLIBCXX_USE_NANOSLEEP |
Omair Javaid | 2040548 | 2015-08-09 19:04:41 +0000 | [diff] [blame] | 18 | #define _GLIBCXX_USE_NANOSLEEP |
| 19 | #endif |
Stephane Sezer | 4d640f2 | 2015-09-09 01:17:24 +0000 | [diff] [blame] | 20 | #endif |
Omair Javaid | 2040548 | 2015-08-09 19:04:41 +0000 | [diff] [blame] | 21 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 22 | #include <functional> |
| 23 | #include <thread> |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 24 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 27 | #include <limits.h> |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 28 | #include <sys/stat.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 31 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 32 | using namespace lldb; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 33 | using namespace lldb_private; |
| 34 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 35 | int PipePosix::kInvalidDescriptor = -1; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 36 | |
| 37 | enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE |
| 38 | |
Ed Maste | 37366e5 | 2015-09-14 15:12:49 +0000 | [diff] [blame] | 39 | // pipe2 is supported by a limited set of platforms |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 40 | // TODO: Add more platforms that support pipe2. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 10) || \ |
| 42 | defined(__NetBSD__) |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 43 | #define PIPE2_SUPPORTED 1 |
| 44 | #else |
| 45 | #define PIPE2_SUPPORTED 0 |
| 46 | #endif |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | namespace { |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 49 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 50 | constexpr auto OPEN_WRITER_SLEEP_TIMEOUT_MSECS = 100; |
| 51 | |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 52 | #if defined(FD_CLOEXEC) && !PIPE2_SUPPORTED |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | bool SetCloexecFlag(int fd) { |
| 54 | int flags = ::fcntl(fd, F_GETFD); |
| 55 | if (flags == -1) |
| 56 | return false; |
| 57 | return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0); |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 58 | } |
| 59 | #endif |
| 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | std::chrono::time_point<std::chrono::steady_clock> Now() { |
| 62 | return std::chrono::steady_clock::now(); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 63 | } |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 66 | PipePosix::PipePosix() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | : m_fds{PipePosix::kInvalidDescriptor, PipePosix::kInvalidDescriptor} {} |
Chaoren Lin | a52f484 | 2015-04-29 17:36:58 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | PipePosix::PipePosix(int read_fd, int write_fd) : m_fds{read_fd, write_fd} {} |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 70 | |
Chaoren Lin | ec53482 | 2015-05-01 16:49:23 +0000 | [diff] [blame] | 71 | PipePosix::PipePosix(PipePosix &&pipe_posix) |
| 72 | : PipeBase{std::move(pipe_posix)}, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | m_fds{pipe_posix.ReleaseReadFileDescriptor(), |
| 74 | pipe_posix.ReleaseWriteFileDescriptor()} {} |
Chaoren Lin | ec53482 | 2015-05-01 16:49:23 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | PipePosix &PipePosix::operator=(PipePosix &&pipe_posix) { |
| 77 | PipeBase::operator=(std::move(pipe_posix)); |
| 78 | m_fds[READ] = pipe_posix.ReleaseReadFileDescriptor(); |
| 79 | m_fds[WRITE] = pipe_posix.ReleaseWriteFileDescriptor(); |
| 80 | return *this; |
Chaoren Lin | ec53482 | 2015-05-01 16:49:23 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | PipePosix::~PipePosix() { Close(); } |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 84 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 85 | Status PipePosix::CreateNew(bool child_processes_inherit) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | if (CanRead() || CanWrite()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 87 | return Status(EINVAL, eErrorTypePOSIX); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 88 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 89 | Status error; |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 90 | #if PIPE2_SUPPORTED |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | if (::pipe2(m_fds, (child_processes_inherit) ? 0 : O_CLOEXEC) == 0) |
| 92 | return error; |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 93 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | if (::pipe(m_fds) == 0) { |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 95 | #ifdef FD_CLOEXEC |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | if (!child_processes_inherit) { |
| 97 | if (!SetCloexecFlag(m_fds[0]) || !SetCloexecFlag(m_fds[1])) { |
| 98 | error.SetErrorToErrno(); |
| 99 | Close(); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 100 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | } |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 102 | } |
| 103 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | return error; |
| 105 | } |
| 106 | #endif |
Zachary Turner | b2df30d | 2014-10-08 20:38:41 +0000 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | error.SetErrorToErrno(); |
| 109 | m_fds[READ] = PipePosix::kInvalidDescriptor; |
| 110 | m_fds[WRITE] = PipePosix::kInvalidDescriptor; |
| 111 | return error; |
| 112 | } |
| 113 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 114 | Status PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | if (CanRead() || CanWrite()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 116 | return Status("Pipe is already opened"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 118 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | if (::mkfifo(name.data(), 0660) != 0) |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 120 | error.SetErrorToErrno(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | |
| 122 | return error; |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 125 | Status PipePosix::CreateWithUniqueName(llvm::StringRef prefix, |
| 126 | bool child_process_inherit, |
| 127 | llvm::SmallVectorImpl<char> &name) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | llvm::SmallString<PATH_MAX> named_pipe_path; |
| 129 | llvm::SmallString<PATH_MAX> pipe_spec((prefix + ".%%%%%%").str()); |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame] | 130 | FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir(); |
| 131 | if (!tmpdir_file_spec) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | tmpdir_file_spec.AppendPathComponent("/tmp"); |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame] | 133 | tmpdir_file_spec.AppendPathComponent(pipe_spec); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | // It's possible that another process creates the target path after we've |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 136 | // verified it's available but before we create it, in which case we should |
| 137 | // try again. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 138 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | do { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 140 | llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | named_pipe_path); |
| 142 | error = CreateNew(named_pipe_path, child_process_inherit); |
| 143 | } while (error.GetError() == EEXIST); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | if (error.Success()) |
| 146 | name = named_pipe_path; |
| 147 | return error; |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 150 | Status PipePosix::OpenAsReader(llvm::StringRef name, |
| 151 | bool child_process_inherit) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | if (CanRead() || CanWrite()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 153 | return Status("Pipe is already opened"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | |
| 155 | int flags = O_RDONLY | O_NONBLOCK; |
| 156 | if (!child_process_inherit) |
| 157 | flags |= O_CLOEXEC; |
| 158 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 159 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | int fd = ::open(name.data(), flags); |
| 161 | if (fd != -1) |
| 162 | m_fds[READ] = fd; |
| 163 | else |
| 164 | error.SetErrorToErrno(); |
| 165 | |
| 166 | return error; |
| 167 | } |
| 168 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 169 | Status |
| 170 | PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name, |
| 171 | bool child_process_inherit, |
| 172 | const std::chrono::microseconds &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | if (CanRead() || CanWrite()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 174 | return Status("Pipe is already opened"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | |
| 176 | int flags = O_WRONLY | O_NONBLOCK; |
| 177 | if (!child_process_inherit) |
| 178 | flags |= O_CLOEXEC; |
| 179 | |
| 180 | using namespace std::chrono; |
| 181 | const auto finish_time = Now() + timeout; |
| 182 | |
| 183 | while (!CanWrite()) { |
| 184 | if (timeout != microseconds::zero()) { |
| 185 | const auto dur = duration_cast<microseconds>(finish_time - Now()).count(); |
| 186 | if (dur <= 0) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 187 | return Status("timeout exceeded - reader hasn't opened so far"); |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | errno = 0; |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 191 | int fd = ::open(name.data(), flags); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 192 | if (fd == -1) { |
| 193 | const auto errno_copy = errno; |
| 194 | // We may get ENXIO if a reader side of the pipe hasn't opened yet. |
| 195 | if (errno_copy != ENXIO) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 196 | return Status(errno_copy, eErrorTypePOSIX); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | std::this_thread::sleep_for( |
| 199 | milliseconds(OPEN_WRITER_SLEEP_TIMEOUT_MSECS)); |
| 200 | } else { |
| 201 | m_fds[WRITE] = fd; |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 202 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 203 | } |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 204 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 205 | return Status(); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | int PipePosix::GetReadFileDescriptor() const { return m_fds[READ]; } |
| 209 | |
| 210 | int PipePosix::GetWriteFileDescriptor() const { return m_fds[WRITE]; } |
| 211 | |
| 212 | int PipePosix::ReleaseReadFileDescriptor() { |
| 213 | const int fd = m_fds[READ]; |
| 214 | m_fds[READ] = PipePosix::kInvalidDescriptor; |
| 215 | return fd; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 218 | int PipePosix::ReleaseWriteFileDescriptor() { |
| 219 | const int fd = m_fds[WRITE]; |
| 220 | m_fds[WRITE] = PipePosix::kInvalidDescriptor; |
| 221 | return fd; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | void PipePosix::Close() { |
| 225 | CloseReadFileDescriptor(); |
| 226 | CloseWriteFileDescriptor(); |
| 227 | } |
| 228 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 229 | Status PipePosix::Delete(llvm::StringRef name) { |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 230 | return llvm::sys::fs::remove(name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | bool PipePosix::CanRead() const { |
| 234 | return m_fds[READ] != PipePosix::kInvalidDescriptor; |
| 235 | } |
| 236 | |
| 237 | bool PipePosix::CanWrite() const { |
| 238 | return m_fds[WRITE] != PipePosix::kInvalidDescriptor; |
| 239 | } |
| 240 | |
| 241 | void PipePosix::CloseReadFileDescriptor() { |
| 242 | if (CanRead()) { |
| 243 | close(m_fds[READ]); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 244 | m_fds[READ] = PipePosix::kInvalidDescriptor; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 245 | } |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | void PipePosix::CloseWriteFileDescriptor() { |
| 249 | if (CanWrite()) { |
| 250 | close(m_fds[WRITE]); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 251 | m_fds[WRITE] = PipePosix::kInvalidDescriptor; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | } |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 255 | Status PipePosix::ReadWithTimeout(void *buf, size_t size, |
| 256 | const std::chrono::microseconds &timeout, |
| 257 | size_t &bytes_read) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | bytes_read = 0; |
| 259 | if (!CanRead()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 260 | return Status(EINVAL, eErrorTypePOSIX); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | const int fd = GetReadFileDescriptor(); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 263 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | SelectHelper select_helper; |
| 265 | select_helper.SetTimeout(timeout); |
| 266 | select_helper.FDSetRead(fd); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 267 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 268 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | while (error.Success()) { |
| 270 | error = select_helper.Select(); |
| 271 | if (error.Success()) { |
| 272 | auto result = ::read(fd, reinterpret_cast<char *>(buf) + bytes_read, |
| 273 | size - bytes_read); |
| 274 | if (result != -1) { |
| 275 | bytes_read += result; |
| 276 | if (bytes_read == size || result == 0) |
| 277 | break; |
| 278 | } else { |
| 279 | error.SetErrorToErrno(); |
| 280 | break; |
| 281 | } |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 282 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | } |
| 284 | return error; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 287 | Status PipePosix::Write(const void *buf, size_t size, size_t &bytes_written) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | bytes_written = 0; |
| 289 | if (!CanWrite()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 290 | return Status(EINVAL, eErrorTypePOSIX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | |
| 292 | const int fd = GetWriteFileDescriptor(); |
| 293 | SelectHelper select_helper; |
| 294 | select_helper.SetTimeout(std::chrono::seconds(0)); |
| 295 | select_helper.FDSetWrite(fd); |
| 296 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 297 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | while (error.Success()) { |
| 299 | error = select_helper.Select(); |
| 300 | if (error.Success()) { |
| 301 | auto result = |
| 302 | ::write(fd, reinterpret_cast<const char *>(buf) + bytes_written, |
| 303 | size - bytes_written); |
| 304 | if (result != -1) { |
| 305 | bytes_written += result; |
| 306 | if (bytes_written == size) |
| 307 | break; |
| 308 | } else { |
| 309 | error.SetErrorToErrno(); |
| 310 | } |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 311 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | } |
| 313 | return error; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 314 | } |