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 | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 11 | #include "lldb/Host/FileSystem.h" |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 12 | #include "lldb/Host/HostInfo.h" |
| 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 | |
| 16 | #include <functional> |
| 17 | #include <thread> |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 18 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 21 | #include <limits.h> |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <sys/types.h> |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 24 | #include <sys/stat.h> |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 25 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 26 | using namespace lldb; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 27 | using namespace lldb_private; |
| 28 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 29 | int PipePosix::kInvalidDescriptor = -1; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 30 | |
| 31 | enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE |
| 32 | |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 33 | // pipe2 is supported by Linux, FreeBSD v10 and higher. |
| 34 | // TODO: Add more platforms that support pipe2. |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 35 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 10) |
| 36 | #define PIPE2_SUPPORTED 1 |
| 37 | #else |
| 38 | #define PIPE2_SUPPORTED 0 |
| 39 | #endif |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 40 | |
| 41 | namespace |
| 42 | { |
| 43 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 44 | constexpr auto OPEN_WRITER_SLEEP_TIMEOUT_MSECS = 100; |
| 45 | |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 46 | #if defined(FD_CLOEXEC) && !PIPE2_SUPPORTED |
| 47 | bool SetCloexecFlag(int fd) |
| 48 | { |
| 49 | int flags = ::fcntl(fd, F_GETFD); |
| 50 | if (flags == -1) |
| 51 | return false; |
| 52 | return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0); |
| 53 | } |
| 54 | #endif |
| 55 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 56 | std::chrono::time_point<std::chrono::steady_clock> |
| 57 | Now() |
| 58 | { |
| 59 | return std::chrono::steady_clock::now(); |
| 60 | } |
| 61 | |
| 62 | Error |
| 63 | SelectIO(int handle, bool is_read, const std::function<Error(bool&)> &io_handler, const std::chrono::microseconds &timeout) |
| 64 | { |
| 65 | Error error; |
| 66 | fd_set fds; |
| 67 | bool done = false; |
| 68 | |
| 69 | using namespace std::chrono; |
| 70 | |
| 71 | const auto finish_time = Now() + timeout; |
| 72 | |
| 73 | while (!done) |
| 74 | { |
| 75 | struct timeval tv = {0, 0}; |
| 76 | if (timeout != microseconds::zero()) |
| 77 | { |
| 78 | const auto remaining_dur = duration_cast<microseconds>(finish_time - Now()); |
| 79 | if (remaining_dur.count() <= 0) |
| 80 | { |
| 81 | error.SetErrorString("timeout exceeded"); |
| 82 | break; |
| 83 | } |
| 84 | const auto dur_secs = duration_cast<seconds>(remaining_dur); |
| 85 | const auto dur_usecs = remaining_dur % seconds(1); |
| 86 | |
| 87 | tv.tv_sec = dur_secs.count(); |
| 88 | tv.tv_usec = dur_usecs.count(); |
| 89 | } |
| 90 | else |
| 91 | tv.tv_sec = 1; |
| 92 | |
| 93 | FD_ZERO(&fds); |
| 94 | FD_SET(handle, &fds); |
| 95 | |
| 96 | const auto retval = ::select(handle + 1, |
| 97 | (is_read) ? &fds : nullptr, |
| 98 | (is_read) ? nullptr : &fds, |
| 99 | nullptr, &tv); |
| 100 | if (retval == -1) |
| 101 | { |
| 102 | if (errno == EINTR) |
| 103 | continue; |
| 104 | error.SetErrorToErrno(); |
| 105 | break; |
| 106 | } |
| 107 | if (retval == 0) |
| 108 | { |
| 109 | error.SetErrorString("timeout exceeded"); |
| 110 | break; |
| 111 | } |
| 112 | if (!FD_ISSET(handle, &fds)) |
| 113 | { |
| 114 | error.SetErrorString("invalid state"); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | error = io_handler(done); |
| 119 | if (error.Fail()) |
| 120 | { |
| 121 | if (error.GetError() == EINTR) |
| 122 | continue; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | return error; |
| 127 | } |
| 128 | |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 131 | PipePosix::PipePosix() |
Chaoren Lin | ec53482 | 2015-05-01 16:49:23 +0000 | [diff] [blame] | 132 | : m_fds{ |
| 133 | PipePosix::kInvalidDescriptor, |
| 134 | PipePosix::kInvalidDescriptor |
| 135 | } {} |
Chaoren Lin | a52f484 | 2015-04-29 17:36:58 +0000 | [diff] [blame] | 136 | |
| 137 | PipePosix::PipePosix(int read_fd, int write_fd) |
| 138 | : m_fds{read_fd, write_fd} {} |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 139 | |
Chaoren Lin | ec53482 | 2015-05-01 16:49:23 +0000 | [diff] [blame] | 140 | PipePosix::PipePosix(PipePosix &&pipe_posix) |
| 141 | : PipeBase{std::move(pipe_posix)}, |
| 142 | m_fds{ |
| 143 | pipe_posix.ReleaseReadFileDescriptor(), |
| 144 | pipe_posix.ReleaseWriteFileDescriptor() |
| 145 | } {} |
| 146 | |
| 147 | PipePosix &PipePosix::operator=(PipePosix &&pipe_posix) |
| 148 | { |
| 149 | PipeBase::operator=(std::move(pipe_posix)); |
| 150 | m_fds[READ] = pipe_posix.ReleaseReadFileDescriptor(); |
| 151 | m_fds[WRITE] = pipe_posix.ReleaseWriteFileDescriptor(); |
| 152 | return *this; |
| 153 | } |
| 154 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 155 | PipePosix::~PipePosix() |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 156 | { |
| 157 | Close(); |
| 158 | } |
| 159 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 160 | Error |
| 161 | PipePosix::CreateNew(bool child_processes_inherit) |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 162 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 163 | if (CanRead() || CanWrite()) |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 164 | return Error(EINVAL, eErrorTypePOSIX); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 165 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 166 | Error error; |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 167 | #if PIPE2_SUPPORTED |
| 168 | if (::pipe2(m_fds, (child_processes_inherit) ? 0 : O_CLOEXEC) == 0) |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 169 | return error; |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 170 | #else |
| 171 | if (::pipe(m_fds) == 0) |
| 172 | { |
| 173 | #ifdef FD_CLOEXEC |
| 174 | if (!child_processes_inherit) |
| 175 | { |
| 176 | if (!SetCloexecFlag(m_fds[0]) || !SetCloexecFlag(m_fds[1])) |
| 177 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 178 | error.SetErrorToErrno(); |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 179 | Close(); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 180 | return error; |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | #endif |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 184 | return error; |
Oleksiy Vyalov | ff9a072 | 2014-11-21 16:18:57 +0000 | [diff] [blame] | 185 | } |
| 186 | #endif |
Zachary Turner | b2df30d | 2014-10-08 20:38:41 +0000 | [diff] [blame] | 187 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 188 | error.SetErrorToErrno(); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 189 | m_fds[READ] = PipePosix::kInvalidDescriptor; |
| 190 | m_fds[WRITE] = PipePosix::kInvalidDescriptor; |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 191 | return error; |
| 192 | } |
| 193 | |
| 194 | Error |
| 195 | PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit) |
| 196 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 197 | if (CanRead() || CanWrite()) |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 198 | return Error("Pipe is already opened"); |
| 199 | |
| 200 | Error error; |
| 201 | if (::mkfifo(name.data(), 0660) != 0) |
| 202 | error.SetErrorToErrno(); |
| 203 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 204 | return error; |
| 205 | } |
| 206 | |
| 207 | Error |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 208 | PipePosix::CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl<char>& name) |
| 209 | { |
| 210 | llvm::SmallString<PATH_MAX> named_pipe_path; |
| 211 | llvm::SmallString<PATH_MAX> pipe_spec((prefix + ".%%%%%%").str()); |
| 212 | FileSpec tmpdir_file_spec; |
| 213 | tmpdir_file_spec.Clear(); |
| 214 | if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
| 215 | { |
| 216 | tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str()); |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | tmpdir_file_spec.AppendPathComponent("/tmp"); |
| 221 | tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str()); |
| 222 | } |
| 223 | |
| 224 | // It's possible that another process creates the target path after we've |
| 225 | // verified it's available but before we create it, in which case we |
| 226 | // should try again. |
| 227 | Error error; |
| 228 | do { |
| 229 | llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath().c_str(), named_pipe_path); |
| 230 | error = CreateNew(named_pipe_path, child_process_inherit); |
| 231 | } while (error.GetError() == EEXIST); |
| 232 | |
| 233 | if (error.Success()) |
| 234 | name = named_pipe_path; |
| 235 | return error; |
| 236 | } |
| 237 | |
| 238 | Error |
Oleksiy Vyalov | 4771829 | 2015-01-14 01:31:27 +0000 | [diff] [blame] | 239 | PipePosix::OpenAsReader(llvm::StringRef name, bool child_process_inherit) |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 240 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 241 | if (CanRead() || CanWrite()) |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 242 | return Error("Pipe is already opened"); |
| 243 | |
| 244 | int flags = O_RDONLY | O_NONBLOCK; |
| 245 | if (!child_process_inherit) |
| 246 | flags |= O_CLOEXEC; |
| 247 | |
| 248 | Error error; |
| 249 | int fd = ::open(name.data(), flags); |
| 250 | if (fd != -1) |
| 251 | m_fds[READ] = fd; |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 252 | else |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 253 | error.SetErrorToErrno(); |
| 254 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 255 | return error; |
| 256 | } |
| 257 | |
| 258 | Error |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 259 | PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 260 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 261 | if (CanRead() || CanWrite()) |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 262 | return Error("Pipe is already opened"); |
| 263 | |
| 264 | int flags = O_WRONLY | O_NONBLOCK; |
| 265 | if (!child_process_inherit) |
| 266 | flags |= O_CLOEXEC; |
| 267 | |
| 268 | using namespace std::chrono; |
| 269 | const auto finish_time = Now() + timeout; |
| 270 | |
| 271 | while (!CanWrite()) |
| 272 | { |
| 273 | if (timeout != microseconds::zero()) |
| 274 | { |
| 275 | const auto dur = duration_cast<microseconds>(finish_time - Now()).count(); |
| 276 | if (dur <= 0) |
| 277 | return Error("timeout exceeded - reader hasn't opened so far"); |
| 278 | } |
| 279 | |
| 280 | errno = 0; |
| 281 | int fd = ::open(name.data(), flags); |
| 282 | if (fd == -1) |
| 283 | { |
| 284 | const auto errno_copy = errno; |
| 285 | // We may get ENXIO if a reader side of the pipe hasn't opened yet. |
| 286 | if (errno_copy != ENXIO) |
| 287 | return Error(errno_copy, eErrorTypePOSIX); |
| 288 | |
| 289 | std::this_thread::sleep_for(milliseconds(OPEN_WRITER_SLEEP_TIMEOUT_MSECS)); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | m_fds[WRITE] = fd; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return Error(); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | int |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 301 | PipePosix::GetReadFileDescriptor() const |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 302 | { |
| 303 | return m_fds[READ]; |
| 304 | } |
| 305 | |
| 306 | int |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 307 | PipePosix::GetWriteFileDescriptor() const |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 308 | { |
| 309 | return m_fds[WRITE]; |
| 310 | } |
| 311 | |
| 312 | int |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 313 | PipePosix::ReleaseReadFileDescriptor() |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 314 | { |
| 315 | const int fd = m_fds[READ]; |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 316 | m_fds[READ] = PipePosix::kInvalidDescriptor; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 317 | return fd; |
| 318 | } |
| 319 | |
| 320 | int |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 321 | PipePosix::ReleaseWriteFileDescriptor() |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 322 | { |
| 323 | const int fd = m_fds[WRITE]; |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 324 | m_fds[WRITE] = PipePosix::kInvalidDescriptor; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 325 | return fd; |
| 326 | } |
| 327 | |
| 328 | void |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 329 | PipePosix::Close() |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 330 | { |
| 331 | CloseReadFileDescriptor(); |
| 332 | CloseWriteFileDescriptor(); |
| 333 | } |
| 334 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 335 | Error |
| 336 | PipePosix::Delete(llvm::StringRef name) |
| 337 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 338 | return FileSystem::Unlink(FileSpec{name.data(), true}); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 341 | bool |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 342 | PipePosix::CanRead() const |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 343 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 344 | return m_fds[READ] != PipePosix::kInvalidDescriptor; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | bool |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 348 | PipePosix::CanWrite() const |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 349 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 350 | return m_fds[WRITE] != PipePosix::kInvalidDescriptor; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 353 | void |
| 354 | PipePosix::CloseReadFileDescriptor() |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 355 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 356 | if (CanRead()) |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 357 | { |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 358 | close(m_fds[READ]); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 359 | m_fds[READ] = PipePosix::kInvalidDescriptor; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 360 | } |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 363 | void |
| 364 | PipePosix::CloseWriteFileDescriptor() |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 365 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 366 | if (CanWrite()) |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 367 | { |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 368 | close(m_fds[WRITE]); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 369 | m_fds[WRITE] = PipePosix::kInvalidDescriptor; |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 370 | } |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 373 | Error |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 374 | PipePosix::ReadWithTimeout(void *buf, size_t size, const std::chrono::microseconds &timeout, size_t &bytes_read) |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 375 | { |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 376 | bytes_read = 0; |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 377 | if (!CanRead()) |
| 378 | return Error(EINVAL, eErrorTypePOSIX); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 379 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 380 | auto handle = GetReadFileDescriptor(); |
| 381 | return SelectIO(handle, |
| 382 | true, |
| 383 | [=, &bytes_read](bool &done) |
| 384 | { |
| 385 | Error error; |
| 386 | auto result = ::read(handle, |
| 387 | reinterpret_cast<char*>(buf) + bytes_read, |
| 388 | size - bytes_read); |
| 389 | if (result != -1) |
| 390 | { |
| 391 | bytes_read += result; |
| 392 | if (bytes_read == size || result == 0) |
| 393 | done = true; |
| 394 | } |
| 395 | else |
| 396 | error.SetErrorToErrno(); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 397 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 398 | return error; |
| 399 | }, |
| 400 | timeout); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 403 | Error |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 404 | PipePosix::Write(const void *buf, size_t size, size_t &bytes_written) |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 405 | { |
| 406 | bytes_written = 0; |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 407 | if (!CanWrite()) |
| 408 | return Error(EINVAL, eErrorTypePOSIX); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 409 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 410 | auto handle = GetWriteFileDescriptor(); |
| 411 | return SelectIO(handle, |
| 412 | false, |
| 413 | [=, &bytes_written](bool &done) |
| 414 | { |
| 415 | Error error; |
| 416 | auto result = ::write(handle, |
| 417 | reinterpret_cast<const char*>(buf) + bytes_written, |
| 418 | size - bytes_written); |
| 419 | if (result != -1) |
| 420 | { |
| 421 | bytes_written += result; |
| 422 | if (bytes_written == size) |
| 423 | done = true; |
| 424 | } |
| 425 | else |
| 426 | error.SetErrorToErrno(); |
Zachary Turner | 0b9d3ee | 2014-12-17 18:02:19 +0000 | [diff] [blame] | 427 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 428 | return error; |
| 429 | }, |
| 430 | std::chrono::microseconds::zero()); |
Greg Clayton | 100eb93 | 2014-07-02 21:10:39 +0000 | [diff] [blame] | 431 | } |