180814287SRaphael Isemann //===-- PipeWindows.cpp ---------------------------------------------------===//
2b2df30d6SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b2df30d6SZachary Turner //
7b2df30d6SZachary Turner //===----------------------------------------------------------------------===//
8b2df30d6SZachary Turner 
9b2df30d6SZachary Turner #include "lldb/Host/windows/PipeWindows.h"
10b2df30d6SZachary Turner 
114536c458SOleksiy Vyalov #include "llvm/ADT/SmallString.h"
124536c458SOleksiy Vyalov #include "llvm/Support/Process.h"
13b2df30d6SZachary Turner #include "llvm/Support/raw_ostream.h"
14b2df30d6SZachary Turner 
15b2df30d6SZachary Turner #include <fcntl.h>
16b2df30d6SZachary Turner #include <io.h>
17aa60e3c8SZachary Turner #include <rpc.h>
18b2df30d6SZachary Turner 
19b2df30d6SZachary Turner #include <atomic>
20b2df30d6SZachary Turner #include <string>
21b2df30d6SZachary Turner 
220b9d3eefSZachary Turner using namespace lldb;
23b2df30d6SZachary Turner using namespace lldb_private;
24b2df30d6SZachary Turner 
25*93c1b3caSPavel Labath static std::atomic<uint32_t> g_pipe_serial(0);
26*93c1b3caSPavel Labath static constexpr llvm::StringLiteral g_pipe_name_prefix = "\\\\.\\Pipe\\";
27e55850beSAaron Smith 
PipeWindows()28e55850beSAaron Smith PipeWindows::PipeWindows()
29e55850beSAaron Smith     : m_read(INVALID_HANDLE_VALUE), m_write(INVALID_HANDLE_VALUE),
30e55850beSAaron Smith       m_read_fd(PipeWindows::kInvalidDescriptor),
31e55850beSAaron Smith       m_write_fd(PipeWindows::kInvalidDescriptor) {
32e55850beSAaron Smith   ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
33e55850beSAaron Smith   ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
34b2df30d6SZachary Turner }
35b2df30d6SZachary Turner 
PipeWindows(pipe_t read,pipe_t write)36e55850beSAaron Smith PipeWindows::PipeWindows(pipe_t read, pipe_t write)
37e55850beSAaron Smith     : m_read((HANDLE)read), m_write((HANDLE)write),
38e55850beSAaron Smith       m_read_fd(PipeWindows::kInvalidDescriptor),
39e55850beSAaron Smith       m_write_fd(PipeWindows::kInvalidDescriptor) {
40e55850beSAaron Smith   assert(read != LLDB_INVALID_PIPE || write != LLDB_INVALID_PIPE);
41b2df30d6SZachary Turner 
42e55850beSAaron Smith   // Don't risk in passing file descriptors and getting handles from them by
43e55850beSAaron Smith   // _get_osfhandle since the retrieved handles are highly likely unrecognized
44e55850beSAaron Smith   // in the current process and usually crashes the program.  Pass handles
45e55850beSAaron Smith   // instead since the handle can be inherited.
46e55850beSAaron Smith 
47e55850beSAaron Smith   if (read != LLDB_INVALID_PIPE) {
48e55850beSAaron Smith     m_read_fd = _open_osfhandle((intptr_t)read, _O_RDONLY);
49e55850beSAaron Smith     // Make sure the fd and native handle are consistent.
50e55850beSAaron Smith     if (m_read_fd < 0)
51e55850beSAaron Smith       m_read = INVALID_HANDLE_VALUE;
52e55850beSAaron Smith   }
53e55850beSAaron Smith 
54e55850beSAaron Smith   if (write != LLDB_INVALID_PIPE) {
55e55850beSAaron Smith     m_write_fd = _open_osfhandle((intptr_t)write, _O_WRONLY);
56e55850beSAaron Smith     if (m_write_fd < 0)
57e55850beSAaron Smith       m_write = INVALID_HANDLE_VALUE;
58e55850beSAaron Smith   }
59e55850beSAaron Smith 
600b9d3eefSZachary Turner   ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
610b9d3eefSZachary Turner   ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
62b2df30d6SZachary Turner }
63b2df30d6SZachary Turner 
~PipeWindows()64b9c1b51eSKate Stone PipeWindows::~PipeWindows() { Close(); }
65b2df30d6SZachary Turner 
CreateNew(bool child_process_inherit)6697206d57SZachary Turner Status PipeWindows::CreateNew(bool child_process_inherit) {
67e55850beSAaron Smith   // Create an anonymous pipe with the specified inheritance.
68e55850beSAaron Smith   SECURITY_ATTRIBUTES sa{sizeof(SECURITY_ATTRIBUTES), 0,
69e55850beSAaron Smith                          child_process_inherit ? TRUE : FALSE};
70e55850beSAaron Smith   BOOL result = ::CreatePipe(&m_read, &m_write, &sa, 1024);
71e55850beSAaron Smith   if (result == FALSE)
72e55850beSAaron Smith     return Status(::GetLastError(), eErrorTypeWin32);
73e55850beSAaron Smith 
74e55850beSAaron Smith   m_read_fd = _open_osfhandle((intptr_t)m_read, _O_RDONLY);
75e55850beSAaron Smith   ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
76e55850beSAaron Smith   m_read_overlapped.hEvent = ::CreateEventA(nullptr, TRUE, FALSE, nullptr);
77e55850beSAaron Smith 
78e55850beSAaron Smith   m_write_fd = _open_osfhandle((intptr_t)m_write, _O_WRONLY);
79e55850beSAaron Smith   ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
80e55850beSAaron Smith 
81e55850beSAaron Smith   return Status();
82e55850beSAaron Smith }
83e55850beSAaron Smith 
CreateNewNamed(bool child_process_inherit)84e55850beSAaron Smith Status PipeWindows::CreateNewNamed(bool child_process_inherit) {
8505097246SAdrian Prantl   // Even for anonymous pipes, we open a named pipe.  This is because you
8605097246SAdrian Prantl   // cannot get overlapped i/o on Windows without using a named pipe.  So we
8705097246SAdrian Prantl   // synthesize a unique name.
88b2df30d6SZachary Turner   uint32_t serial = g_pipe_serial.fetch_add(1);
89b2df30d6SZachary Turner   std::string pipe_name;
90b2df30d6SZachary Turner   llvm::raw_string_ostream pipe_name_stream(pipe_name);
910b9d3eefSZachary Turner   pipe_name_stream << "lldb.pipe." << ::GetCurrentProcessId() << "." << serial;
92b2df30d6SZachary Turner   pipe_name_stream.flush();
93b2df30d6SZachary Turner 
940b9d3eefSZachary Turner   return CreateNew(pipe_name.c_str(), child_process_inherit);
95b2df30d6SZachary Turner }
96b2df30d6SZachary Turner 
CreateNew(llvm::StringRef name,bool child_process_inherit)9797206d57SZachary Turner Status PipeWindows::CreateNew(llvm::StringRef name,
9897206d57SZachary Turner                               bool child_process_inherit) {
990b9d3eefSZachary Turner   if (name.empty())
10097206d57SZachary Turner     return Status(ERROR_INVALID_PARAMETER, eErrorTypeWin32);
1010b9d3eefSZachary Turner 
1020b9d3eefSZachary Turner   if (CanRead() || CanWrite())
10397206d57SZachary Turner     return Status(ERROR_ALREADY_EXISTS, eErrorTypeWin32);
1040b9d3eefSZachary Turner 
1057a6ebb5bSPavel Labath   std::string pipe_path = g_pipe_name_prefix.str();
106e06444d9SPavel Labath   pipe_path.append(name.str());
1070b9d3eefSZachary Turner 
10805097246SAdrian Prantl   // Always open for overlapped i/o.  We implement blocking manually in Read
10905097246SAdrian Prantl   // and Write.
1100b9d3eefSZachary Turner   DWORD read_mode = FILE_FLAG_OVERLAPPED;
111b9c1b51eSKate Stone   m_read = ::CreateNamedPipeA(
112b9c1b51eSKate Stone       pipe_path.c_str(), PIPE_ACCESS_INBOUND | read_mode,
113b9c1b51eSKate Stone       PIPE_TYPE_BYTE | PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);
1140b9d3eefSZachary Turner   if (INVALID_HANDLE_VALUE == m_read)
11597206d57SZachary Turner     return Status(::GetLastError(), eErrorTypeWin32);
1160b9d3eefSZachary Turner   m_read_fd = _open_osfhandle((intptr_t)m_read, _O_RDONLY);
1170b9d3eefSZachary Turner   ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
1180b9d3eefSZachary Turner   m_read_overlapped.hEvent = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
1190b9d3eefSZachary Turner 
120e55850beSAaron Smith   // Open the write end of the pipe. Note that closing either the read or
121e55850beSAaron Smith   // write end of the pipe could directly close the pipe itself.
12297206d57SZachary Turner   Status result = OpenNamedPipe(name, child_process_inherit, false);
123b9c1b51eSKate Stone   if (!result.Success()) {
12466f2e12fSChaoren Lin     CloseReadFileDescriptor();
1250b9d3eefSZachary Turner     return result;
1260b9d3eefSZachary Turner   }
1270b9d3eefSZachary Turner 
1280b9d3eefSZachary Turner   return result;
1290b9d3eefSZachary Turner }
1300b9d3eefSZachary Turner 
CreateWithUniqueName(llvm::StringRef prefix,bool child_process_inherit,llvm::SmallVectorImpl<char> & name)13197206d57SZachary Turner Status PipeWindows::CreateWithUniqueName(llvm::StringRef prefix,
132b9c1b51eSKate Stone                                          bool child_process_inherit,
133b9c1b51eSKate Stone                                          llvm::SmallVectorImpl<char> &name) {
1344536c458SOleksiy Vyalov   llvm::SmallString<128> pipe_name;
13597206d57SZachary Turner   Status error;
136aa60e3c8SZachary Turner   ::UUID unique_id;
137aa60e3c8SZachary Turner   RPC_CSTR unique_string;
138aa60e3c8SZachary Turner   RPC_STATUS status = ::UuidCreate(&unique_id);
139aa60e3c8SZachary Turner   if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
140aa60e3c8SZachary Turner     status = ::UuidToStringA(&unique_id, &unique_string);
141b9c1b51eSKate Stone   if (status == RPC_S_OK) {
1424536c458SOleksiy Vyalov     pipe_name = prefix;
1434536c458SOleksiy Vyalov     pipe_name += "-";
144aa60e3c8SZachary Turner     pipe_name += reinterpret_cast<char *>(unique_string);
145aa60e3c8SZachary Turner     ::RpcStringFreeA(&unique_string);
146aa60e3c8SZachary Turner     error = CreateNew(pipe_name, child_process_inherit);
147b9c1b51eSKate Stone   } else {
148aa60e3c8SZachary Turner     error.SetError(status, eErrorTypeWin32);
149aa60e3c8SZachary Turner   }
1504536c458SOleksiy Vyalov   if (error.Success())
1514536c458SOleksiy Vyalov     name = pipe_name;
1524536c458SOleksiy Vyalov   return error;
1534536c458SOleksiy Vyalov }
1544536c458SOleksiy Vyalov 
OpenAsReader(llvm::StringRef name,bool child_process_inherit)15597206d57SZachary Turner Status PipeWindows::OpenAsReader(llvm::StringRef name,
156b9c1b51eSKate Stone                                  bool child_process_inherit) {
157e55850beSAaron Smith   if (CanRead())
15897206d57SZachary Turner     return Status(ERROR_ALREADY_EXISTS, eErrorTypeWin32);
1590b9d3eefSZachary Turner 
1600b9d3eefSZachary Turner   return OpenNamedPipe(name, child_process_inherit, true);
1610b9d3eefSZachary Turner }
1620b9d3eefSZachary Turner 
16397206d57SZachary Turner Status
OpenAsWriterWithTimeout(llvm::StringRef name,bool child_process_inherit,const std::chrono::microseconds & timeout)16497206d57SZachary Turner PipeWindows::OpenAsWriterWithTimeout(llvm::StringRef name,
16597206d57SZachary Turner                                      bool child_process_inherit,
166b9c1b51eSKate Stone                                      const std::chrono::microseconds &timeout) {
167e55850beSAaron Smith   if (CanWrite())
16897206d57SZachary Turner     return Status(ERROR_ALREADY_EXISTS, eErrorTypeWin32);
1690b9d3eefSZachary Turner 
1700b9d3eefSZachary Turner   return OpenNamedPipe(name, child_process_inherit, false);
1710b9d3eefSZachary Turner }
1720b9d3eefSZachary Turner 
OpenNamedPipe(llvm::StringRef name,bool child_process_inherit,bool is_read)17397206d57SZachary Turner Status PipeWindows::OpenNamedPipe(llvm::StringRef name,
174b9c1b51eSKate Stone                                   bool child_process_inherit, bool is_read) {
1750b9d3eefSZachary Turner   if (name.empty())
17697206d57SZachary Turner     return Status(ERROR_INVALID_PARAMETER, eErrorTypeWin32);
1770b9d3eefSZachary Turner 
1780b9d3eefSZachary Turner   assert(is_read ? !CanRead() : !CanWrite());
1790b9d3eefSZachary Turner 
1805a8ad459SZachary Turner   SECURITY_ATTRIBUTES attributes = {};
1810b9d3eefSZachary Turner   attributes.bInheritHandle = child_process_inherit;
1820b9d3eefSZachary Turner 
1837a6ebb5bSPavel Labath   std::string pipe_path = g_pipe_name_prefix.str();
184e06444d9SPavel Labath   pipe_path.append(name.str());
1850b9d3eefSZachary Turner 
186b9c1b51eSKate Stone   if (is_read) {
187b9c1b51eSKate Stone     m_read = ::CreateFileA(pipe_path.c_str(), GENERIC_READ, 0, &attributes,
188b9c1b51eSKate Stone                            OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
1890b9d3eefSZachary Turner     if (INVALID_HANDLE_VALUE == m_read)
19097206d57SZachary Turner       return Status(::GetLastError(), eErrorTypeWin32);
1910b9d3eefSZachary Turner 
192b2df30d6SZachary Turner     m_read_fd = _open_osfhandle((intptr_t)m_read, _O_RDONLY);
1930b9d3eefSZachary Turner 
1940b9d3eefSZachary Turner     ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
1950b9d3eefSZachary Turner     m_read_overlapped.hEvent = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
196b9c1b51eSKate Stone   } else {
197b9c1b51eSKate Stone     m_write = ::CreateFileA(pipe_path.c_str(), GENERIC_WRITE, 0, &attributes,
198b9c1b51eSKate Stone                             OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
1990b9d3eefSZachary Turner     if (INVALID_HANDLE_VALUE == m_write)
20097206d57SZachary Turner       return Status(::GetLastError(), eErrorTypeWin32);
2010b9d3eefSZachary Turner 
202b2df30d6SZachary Turner     m_write_fd = _open_osfhandle((intptr_t)m_write, _O_WRONLY);
203b2df30d6SZachary Turner 
2040b9d3eefSZachary Turner     ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
205b2df30d6SZachary Turner   }
2060b9d3eefSZachary Turner 
20797206d57SZachary Turner   return Status();
208b2df30d6SZachary Turner }
209b2df30d6SZachary Turner 
GetReadFileDescriptor() const210b9c1b51eSKate Stone int PipeWindows::GetReadFileDescriptor() const { return m_read_fd; }
211b2df30d6SZachary Turner 
GetWriteFileDescriptor() const212b9c1b51eSKate Stone int PipeWindows::GetWriteFileDescriptor() const { return m_write_fd; }
213b2df30d6SZachary Turner 
ReleaseReadFileDescriptor()214b9c1b51eSKate Stone int PipeWindows::ReleaseReadFileDescriptor() {
2150b9d3eefSZachary Turner   if (!CanRead())
216e55850beSAaron Smith     return PipeWindows::kInvalidDescriptor;
217b2df30d6SZachary Turner   int result = m_read_fd;
218e55850beSAaron Smith   m_read_fd = PipeWindows::kInvalidDescriptor;
2190b9d3eefSZachary Turner   if (m_read_overlapped.hEvent)
2200b9d3eefSZachary Turner     ::CloseHandle(m_read_overlapped.hEvent);
221b2df30d6SZachary Turner   m_read = INVALID_HANDLE_VALUE;
2220b9d3eefSZachary Turner   ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
223b2df30d6SZachary Turner   return result;
224b2df30d6SZachary Turner }
225b2df30d6SZachary Turner 
ReleaseWriteFileDescriptor()226b9c1b51eSKate Stone int PipeWindows::ReleaseWriteFileDescriptor() {
2270b9d3eefSZachary Turner   if (!CanWrite())
228e55850beSAaron Smith     return PipeWindows::kInvalidDescriptor;
229b2df30d6SZachary Turner   int result = m_write_fd;
230e55850beSAaron Smith   m_write_fd = PipeWindows::kInvalidDescriptor;
231b2df30d6SZachary Turner   m_write = INVALID_HANDLE_VALUE;
2320b9d3eefSZachary Turner   ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
233b2df30d6SZachary Turner   return result;
234b2df30d6SZachary Turner }
235b2df30d6SZachary Turner 
CloseReadFileDescriptor()236b9c1b51eSKate Stone void PipeWindows::CloseReadFileDescriptor() {
2370b9d3eefSZachary Turner   if (!CanRead())
2380b9d3eefSZachary Turner     return;
239b2df30d6SZachary Turner 
2400b9d3eefSZachary Turner   if (m_read_overlapped.hEvent)
2410b9d3eefSZachary Turner     ::CloseHandle(m_read_overlapped.hEvent);
242e55850beSAaron Smith 
2430b9d3eefSZachary Turner   _close(m_read_fd);
244b2df30d6SZachary Turner   m_read = INVALID_HANDLE_VALUE;
245e55850beSAaron Smith   m_read_fd = PipeWindows::kInvalidDescriptor;
2460b9d3eefSZachary Turner   ZeroMemory(&m_read_overlapped, sizeof(m_read_overlapped));
2470b9d3eefSZachary Turner }
2480b9d3eefSZachary Turner 
CloseWriteFileDescriptor()249b9c1b51eSKate Stone void PipeWindows::CloseWriteFileDescriptor() {
2500b9d3eefSZachary Turner   if (!CanWrite())
2510b9d3eefSZachary Turner     return;
2520b9d3eefSZachary Turner 
2530b9d3eefSZachary Turner   _close(m_write_fd);
2540b9d3eefSZachary Turner   m_write = INVALID_HANDLE_VALUE;
255e55850beSAaron Smith   m_write_fd = PipeWindows::kInvalidDescriptor;
2560b9d3eefSZachary Turner   ZeroMemory(&m_write_overlapped, sizeof(m_write_overlapped));
257b2df30d6SZachary Turner }
2580b9d3eefSZachary Turner 
Close()259b9c1b51eSKate Stone void PipeWindows::Close() {
26066f2e12fSChaoren Lin   CloseReadFileDescriptor();
26166f2e12fSChaoren Lin   CloseWriteFileDescriptor();
262b2df30d6SZachary Turner }
263b2df30d6SZachary Turner 
Delete(llvm::StringRef name)26497206d57SZachary Turner Status PipeWindows::Delete(llvm::StringRef name) { return Status(); }
265d5f8b6a6SOleksiy Vyalov 
CanRead() const266b9c1b51eSKate Stone bool PipeWindows::CanRead() const { return (m_read != INVALID_HANDLE_VALUE); }
2670b9d3eefSZachary Turner 
CanWrite() const268b9c1b51eSKate Stone bool PipeWindows::CanWrite() const { return (m_write != INVALID_HANDLE_VALUE); }
269b2df30d6SZachary Turner 
270b2df30d6SZachary Turner HANDLE
GetReadNativeHandle()271b9c1b51eSKate Stone PipeWindows::GetReadNativeHandle() { return m_read; }
272b2df30d6SZachary Turner 
273b2df30d6SZachary Turner HANDLE
GetWriteNativeHandle()274b9c1b51eSKate Stone PipeWindows::GetWriteNativeHandle() { return m_write; }
275b2df30d6SZachary Turner 
ReadWithTimeout(void * buf,size_t size,const std::chrono::microseconds & duration,size_t & bytes_read)27697206d57SZachary Turner Status PipeWindows::ReadWithTimeout(void *buf, size_t size,
277b9c1b51eSKate Stone                                     const std::chrono::microseconds &duration,
278b9c1b51eSKate Stone                                     size_t &bytes_read) {
2790b9d3eefSZachary Turner   if (!CanRead())
28097206d57SZachary Turner     return Status(ERROR_INVALID_HANDLE, eErrorTypeWin32);
2810b9d3eefSZachary Turner 
2820b9d3eefSZachary Turner   bytes_read = 0;
2830b9d3eefSZachary Turner   DWORD sys_bytes_read = size;
284b9c1b51eSKate Stone   BOOL result = ::ReadFile(m_read, buf, sys_bytes_read, &sys_bytes_read,
285b9c1b51eSKate Stone                            &m_read_overlapped);
2860b9d3eefSZachary Turner   if (!result && GetLastError() != ERROR_IO_PENDING)
28797206d57SZachary Turner     return Status(::GetLastError(), eErrorTypeWin32);
2880b9d3eefSZachary Turner 
289b9c1b51eSKate Stone   DWORD timeout = (duration == std::chrono::microseconds::zero())
290b9c1b51eSKate Stone                       ? INFINITE
291b9c1b51eSKate Stone                       : duration.count() * 1000;
2920b9d3eefSZachary Turner   DWORD wait_result = ::WaitForSingleObject(m_read_overlapped.hEvent, timeout);
293b9c1b51eSKate Stone   if (wait_result != WAIT_OBJECT_0) {
294b9c1b51eSKate Stone     // The operation probably failed.  However, if it timed out, we need to
29505097246SAdrian Prantl     // cancel the I/O. Between the time we returned from WaitForSingleObject
29605097246SAdrian Prantl     // and the time we call CancelIoEx, the operation may complete.  If that
29705097246SAdrian Prantl     // hapens, CancelIoEx will fail and return ERROR_NOT_FOUND. If that
29805097246SAdrian Prantl     // happens, the original operation should be considered to have been
299b9c1b51eSKate Stone     // successful.
3000b9d3eefSZachary Turner     bool failed = true;
3010b9d3eefSZachary Turner     DWORD failure_error = ::GetLastError();
302b9c1b51eSKate Stone     if (wait_result == WAIT_TIMEOUT) {
3030b9d3eefSZachary Turner       BOOL cancel_result = CancelIoEx(m_read, &m_read_overlapped);
3040b9d3eefSZachary Turner       if (!cancel_result && GetLastError() == ERROR_NOT_FOUND)
3050b9d3eefSZachary Turner         failed = false;
306b2df30d6SZachary Turner     }
3070b9d3eefSZachary Turner     if (failed)
30897206d57SZachary Turner       return Status(failure_error, eErrorTypeWin32);
3090b9d3eefSZachary Turner   }
3100b9d3eefSZachary Turner 
31105097246SAdrian Prantl   // Now we call GetOverlappedResult setting bWait to false, since we've
31205097246SAdrian Prantl   // already waited as long as we're willing to.
3130b9d3eefSZachary Turner   if (!GetOverlappedResult(m_read, &m_read_overlapped, &sys_bytes_read, FALSE))
31497206d57SZachary Turner     return Status(::GetLastError(), eErrorTypeWin32);
3150b9d3eefSZachary Turner 
3160b9d3eefSZachary Turner   bytes_read = sys_bytes_read;
31797206d57SZachary Turner   return Status();
3180b9d3eefSZachary Turner }
3190b9d3eefSZachary Turner 
Write(const void * buf,size_t num_bytes,size_t & bytes_written)32097206d57SZachary Turner Status PipeWindows::Write(const void *buf, size_t num_bytes,
321b9c1b51eSKate Stone                           size_t &bytes_written) {
3220b9d3eefSZachary Turner   if (!CanWrite())
32397206d57SZachary Turner     return Status(ERROR_INVALID_HANDLE, eErrorTypeWin32);
3240b9d3eefSZachary Turner 
3250b9d3eefSZachary Turner   DWORD sys_bytes_written = 0;
326b9c1b51eSKate Stone   BOOL write_result = ::WriteFile(m_write, buf, num_bytes, &sys_bytes_written,
327b9c1b51eSKate Stone                                   &m_write_overlapped);
3280b9d3eefSZachary Turner   if (!write_result && GetLastError() != ERROR_IO_PENDING)
32997206d57SZachary Turner     return Status(::GetLastError(), eErrorTypeWin32);
3300b9d3eefSZachary Turner 
331b9c1b51eSKate Stone   BOOL result = GetOverlappedResult(m_write, &m_write_overlapped,
332b9c1b51eSKate Stone                                     &sys_bytes_written, TRUE);
3330b9d3eefSZachary Turner   if (!result)
33497206d57SZachary Turner     return Status(::GetLastError(), eErrorTypeWin32);
33597206d57SZachary Turner   return Status();
336b2df30d6SZachary Turner }
337