xref: /src/contrib/llvm-project/llvm/lib/Support/raw_socket_stream.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
199aabd70SDimitry Andric //===-- llvm/Support/raw_socket_stream.cpp - Socket streams --*- C++ -*-===//
299aabd70SDimitry Andric //
399aabd70SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
499aabd70SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
599aabd70SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
699aabd70SDimitry Andric //
799aabd70SDimitry Andric //===----------------------------------------------------------------------===//
899aabd70SDimitry Andric //
999aabd70SDimitry Andric // This file contains raw_ostream implementations for streams to communicate
1099aabd70SDimitry Andric // via UNIX sockets
1199aabd70SDimitry Andric //
1299aabd70SDimitry Andric //===----------------------------------------------------------------------===//
1399aabd70SDimitry Andric 
1499aabd70SDimitry Andric #include "llvm/Support/raw_socket_stream.h"
1599aabd70SDimitry Andric #include "llvm/Config/config.h"
1699aabd70SDimitry Andric #include "llvm/Support/Error.h"
17ac9a064cSDimitry Andric #include "llvm/Support/FileSystem.h"
18ac9a064cSDimitry Andric 
19ac9a064cSDimitry Andric #include <atomic>
20ac9a064cSDimitry Andric #include <fcntl.h>
21ac9a064cSDimitry Andric #include <functional>
22ac9a064cSDimitry Andric #include <thread>
2399aabd70SDimitry Andric 
2499aabd70SDimitry Andric #ifndef _WIN32
25ac9a064cSDimitry Andric #include <poll.h>
2699aabd70SDimitry Andric #include <sys/socket.h>
2799aabd70SDimitry Andric #include <sys/un.h>
2899aabd70SDimitry Andric #else
2999aabd70SDimitry Andric #include "llvm/Support/Windows/WindowsSupport.h"
3099aabd70SDimitry Andric // winsock2.h must be included before afunix.h. Briefly turn off clang-format to
3199aabd70SDimitry Andric // avoid error.
3299aabd70SDimitry Andric // clang-format off
3399aabd70SDimitry Andric #include <winsock2.h>
3499aabd70SDimitry Andric #include <afunix.h>
3599aabd70SDimitry Andric // clang-format on
3699aabd70SDimitry Andric #include <io.h>
3799aabd70SDimitry Andric #endif // _WIN32
3899aabd70SDimitry Andric 
3999aabd70SDimitry Andric #if defined(HAVE_UNISTD_H)
4099aabd70SDimitry Andric #include <unistd.h>
4199aabd70SDimitry Andric #endif
4299aabd70SDimitry Andric 
4399aabd70SDimitry Andric using namespace llvm;
4499aabd70SDimitry Andric 
4599aabd70SDimitry Andric #ifdef _WIN32
WSABalancer()4699aabd70SDimitry Andric WSABalancer::WSABalancer() {
4799aabd70SDimitry Andric   WSADATA WsaData;
4899aabd70SDimitry Andric   ::memset(&WsaData, 0, sizeof(WsaData));
4999aabd70SDimitry Andric   if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0) {
5099aabd70SDimitry Andric     llvm::report_fatal_error("WSAStartup failed");
5199aabd70SDimitry Andric   }
5299aabd70SDimitry Andric }
5399aabd70SDimitry Andric 
~WSABalancer()5499aabd70SDimitry Andric WSABalancer::~WSABalancer() { WSACleanup(); }
5599aabd70SDimitry Andric #endif // _WIN32
5699aabd70SDimitry Andric 
getLastSocketErrorCode()5799aabd70SDimitry Andric static std::error_code getLastSocketErrorCode() {
5899aabd70SDimitry Andric #ifdef _WIN32
5999aabd70SDimitry Andric   return std::error_code(::WSAGetLastError(), std::system_category());
6099aabd70SDimitry Andric #else
61ac9a064cSDimitry Andric   return errnoAsErrorCode();
6299aabd70SDimitry Andric #endif
6399aabd70SDimitry Andric }
6499aabd70SDimitry Andric 
setSocketAddr(StringRef SocketPath)65ac9a064cSDimitry Andric static sockaddr_un setSocketAddr(StringRef SocketPath) {
6699aabd70SDimitry Andric   struct sockaddr_un Addr;
6799aabd70SDimitry Andric   memset(&Addr, 0, sizeof(Addr));
6899aabd70SDimitry Andric   Addr.sun_family = AF_UNIX;
6999aabd70SDimitry Andric   strncpy(Addr.sun_path, SocketPath.str().c_str(), sizeof(Addr.sun_path) - 1);
70ac9a064cSDimitry Andric   return Addr;
71ac9a064cSDimitry Andric }
7299aabd70SDimitry Andric 
getSocketFD(StringRef SocketPath)73ac9a064cSDimitry Andric static Expected<int> getSocketFD(StringRef SocketPath) {
7499aabd70SDimitry Andric #ifdef _WIN32
75ac9a064cSDimitry Andric   SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
76ac9a064cSDimitry Andric   if (Socket == INVALID_SOCKET) {
7799aabd70SDimitry Andric #else
78ac9a064cSDimitry Andric   int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
79ac9a064cSDimitry Andric   if (Socket == -1) {
8099aabd70SDimitry Andric #endif // _WIN32
8199aabd70SDimitry Andric     return llvm::make_error<StringError>(getLastSocketErrorCode(),
8299aabd70SDimitry Andric                                          "Create socket failed");
8399aabd70SDimitry Andric   }
8499aabd70SDimitry Andric 
85ac9a064cSDimitry Andric   struct sockaddr_un Addr = setSocketAddr(SocketPath);
86ac9a064cSDimitry Andric   if (::connect(Socket, (struct sockaddr *)&Addr, sizeof(Addr)) == -1)
8799aabd70SDimitry Andric     return llvm::make_error<StringError>(getLastSocketErrorCode(),
8899aabd70SDimitry Andric                                          "Connect socket failed");
89ac9a064cSDimitry Andric 
9099aabd70SDimitry Andric #ifdef _WIN32
91ac9a064cSDimitry Andric   return _open_osfhandle(Socket, 0);
9299aabd70SDimitry Andric #else
93ac9a064cSDimitry Andric   return Socket;
9499aabd70SDimitry Andric #endif // _WIN32
9599aabd70SDimitry Andric }
9699aabd70SDimitry Andric 
97ac9a064cSDimitry Andric ListeningSocket::ListeningSocket(int SocketFD, StringRef SocketPath,
98ac9a064cSDimitry Andric                                  int PipeFD[2])
99ac9a064cSDimitry Andric     : FD(SocketFD), SocketPath(SocketPath), PipeFD{PipeFD[0], PipeFD[1]} {}
100ac9a064cSDimitry Andric 
101ac9a064cSDimitry Andric ListeningSocket::ListeningSocket(ListeningSocket &&LS)
102ac9a064cSDimitry Andric     : FD(LS.FD.load()), SocketPath(LS.SocketPath),
103ac9a064cSDimitry Andric       PipeFD{LS.PipeFD[0], LS.PipeFD[1]} {
104ac9a064cSDimitry Andric 
105ac9a064cSDimitry Andric   LS.FD = -1;
106ac9a064cSDimitry Andric   LS.SocketPath.clear();
107ac9a064cSDimitry Andric   LS.PipeFD[0] = -1;
108ac9a064cSDimitry Andric   LS.PipeFD[1] = -1;
109ac9a064cSDimitry Andric }
110ac9a064cSDimitry Andric 
111ac9a064cSDimitry Andric Expected<ListeningSocket> ListeningSocket::createUnix(StringRef SocketPath,
112ac9a064cSDimitry Andric                                                       int MaxBacklog) {
113ac9a064cSDimitry Andric 
114ac9a064cSDimitry Andric   // Handle instances where the target socket address already exists and
115ac9a064cSDimitry Andric   // differentiate between a preexisting file with and without a bound socket
116ac9a064cSDimitry Andric   //
117ac9a064cSDimitry Andric   // ::bind will return std::errc:address_in_use if a file at the socket address
118ac9a064cSDimitry Andric   // already exists (e.g., the file was not properly unlinked due to a crash)
119ac9a064cSDimitry Andric   // even if another socket has not yet binded to that address
120ac9a064cSDimitry Andric   if (llvm::sys::fs::exists(SocketPath)) {
121ac9a064cSDimitry Andric     Expected<int> MaybeFD = getSocketFD(SocketPath);
122ac9a064cSDimitry Andric     if (!MaybeFD) {
123ac9a064cSDimitry Andric 
124ac9a064cSDimitry Andric       // Regardless of the error, notify the caller that a file already exists
125ac9a064cSDimitry Andric       // at the desired socket address and that there is no bound socket at that
126ac9a064cSDimitry Andric       // address. The file must be removed before ::bind can use the address
127ac9a064cSDimitry Andric       consumeError(MaybeFD.takeError());
128ac9a064cSDimitry Andric       return llvm::make_error<StringError>(
129ac9a064cSDimitry Andric           std::make_error_code(std::errc::file_exists),
130ac9a064cSDimitry Andric           "Socket address unavailable");
131ac9a064cSDimitry Andric     }
132ac9a064cSDimitry Andric     ::close(std::move(*MaybeFD));
133ac9a064cSDimitry Andric 
134ac9a064cSDimitry Andric     // Notify caller that the provided socket address already has a bound socket
135ac9a064cSDimitry Andric     return llvm::make_error<StringError>(
136ac9a064cSDimitry Andric         std::make_error_code(std::errc::address_in_use),
137ac9a064cSDimitry Andric         "Socket address unavailable");
138ac9a064cSDimitry Andric   }
139ac9a064cSDimitry Andric 
140ac9a064cSDimitry Andric #ifdef _WIN32
141ac9a064cSDimitry Andric   WSABalancer _;
142ac9a064cSDimitry Andric   SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
143ac9a064cSDimitry Andric   if (Socket == INVALID_SOCKET)
144ac9a064cSDimitry Andric #else
145ac9a064cSDimitry Andric   int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
146ac9a064cSDimitry Andric   if (Socket == -1)
147ac9a064cSDimitry Andric #endif
148ac9a064cSDimitry Andric     return llvm::make_error<StringError>(getLastSocketErrorCode(),
149ac9a064cSDimitry Andric                                          "socket create failed");
150ac9a064cSDimitry Andric 
151ac9a064cSDimitry Andric   struct sockaddr_un Addr = setSocketAddr(SocketPath);
152ac9a064cSDimitry Andric   if (::bind(Socket, (struct sockaddr *)&Addr, sizeof(Addr)) == -1) {
153ac9a064cSDimitry Andric     // Grab error code from call to ::bind before calling ::close
154ac9a064cSDimitry Andric     std::error_code EC = getLastSocketErrorCode();
155ac9a064cSDimitry Andric     ::close(Socket);
156ac9a064cSDimitry Andric     return llvm::make_error<StringError>(EC, "Bind error");
157ac9a064cSDimitry Andric   }
158ac9a064cSDimitry Andric 
159ac9a064cSDimitry Andric   // Mark socket as passive so incoming connections can be accepted
160ac9a064cSDimitry Andric   if (::listen(Socket, MaxBacklog) == -1)
161ac9a064cSDimitry Andric     return llvm::make_error<StringError>(getLastSocketErrorCode(),
162ac9a064cSDimitry Andric                                          "Listen error");
163ac9a064cSDimitry Andric 
164ac9a064cSDimitry Andric   int PipeFD[2];
165ac9a064cSDimitry Andric #ifdef _WIN32
166ac9a064cSDimitry Andric   // Reserve 1 byte for the pipe and use default textmode
167ac9a064cSDimitry Andric   if (::_pipe(PipeFD, 1, 0) == -1)
168ac9a064cSDimitry Andric #else
169ac9a064cSDimitry Andric   if (::pipe(PipeFD) == -1)
170ac9a064cSDimitry Andric #endif // _WIN32
171ac9a064cSDimitry Andric     return llvm::make_error<StringError>(getLastSocketErrorCode(),
172ac9a064cSDimitry Andric                                          "pipe failed");
173ac9a064cSDimitry Andric 
174ac9a064cSDimitry Andric #ifdef _WIN32
175ac9a064cSDimitry Andric   return ListeningSocket{_open_osfhandle(Socket, 0), SocketPath, PipeFD};
176ac9a064cSDimitry Andric #else
177ac9a064cSDimitry Andric   return ListeningSocket{Socket, SocketPath, PipeFD};
178ac9a064cSDimitry Andric #endif // _WIN32
179ac9a064cSDimitry Andric }
180ac9a064cSDimitry Andric 
181ac9a064cSDimitry Andric // If a file descriptor being monitored by ::poll is closed by another thread,
182ac9a064cSDimitry Andric // the result is unspecified. In the case ::poll does not unblock and return,
183ac9a064cSDimitry Andric // when ActiveFD is closed, you can provide another file descriptor via CancelFD
184ac9a064cSDimitry Andric // that when written to will cause poll to return. Typically CancelFD is the
185ac9a064cSDimitry Andric // read end of a unidirectional pipe.
186ac9a064cSDimitry Andric //
187ac9a064cSDimitry Andric // Timeout should be -1 to block indefinitly
188ac9a064cSDimitry Andric //
189ac9a064cSDimitry Andric // getActiveFD is a callback to handle ActiveFD's of std::atomic<int> and int
190ac9a064cSDimitry Andric static std::error_code
191ac9a064cSDimitry Andric manageTimeout(const std::chrono::milliseconds &Timeout,
192ac9a064cSDimitry Andric               const std::function<int()> &getActiveFD,
193ac9a064cSDimitry Andric               const std::optional<int> &CancelFD = std::nullopt) {
194ac9a064cSDimitry Andric   struct pollfd FD[2];
195ac9a064cSDimitry Andric   FD[0].events = POLLIN;
196ac9a064cSDimitry Andric #ifdef _WIN32
197ac9a064cSDimitry Andric   SOCKET WinServerSock = _get_osfhandle(getActiveFD());
198ac9a064cSDimitry Andric   FD[0].fd = WinServerSock;
199ac9a064cSDimitry Andric #else
200ac9a064cSDimitry Andric   FD[0].fd = getActiveFD();
201ac9a064cSDimitry Andric #endif
202ac9a064cSDimitry Andric   uint8_t FDCount = 1;
203ac9a064cSDimitry Andric   if (CancelFD.has_value()) {
204ac9a064cSDimitry Andric     FD[1].events = POLLIN;
205ac9a064cSDimitry Andric     FD[1].fd = CancelFD.value();
206ac9a064cSDimitry Andric     FDCount++;
207ac9a064cSDimitry Andric   }
208ac9a064cSDimitry Andric 
209ac9a064cSDimitry Andric   // Keep track of how much time has passed in case ::poll or WSAPoll are
210ac9a064cSDimitry Andric   // interupted by a signal and need to be recalled
211ac9a064cSDimitry Andric   auto Start = std::chrono::steady_clock::now();
212ac9a064cSDimitry Andric   auto RemainingTimeout = Timeout;
213ac9a064cSDimitry Andric   int PollStatus = 0;
214ac9a064cSDimitry Andric   do {
215ac9a064cSDimitry Andric     // If Timeout is -1 then poll should block and RemainingTimeout does not
216ac9a064cSDimitry Andric     // need to be recalculated
217ac9a064cSDimitry Andric     if (PollStatus != 0 && Timeout != std::chrono::milliseconds(-1)) {
218ac9a064cSDimitry Andric       auto TotalElapsedTime =
219ac9a064cSDimitry Andric           std::chrono::duration_cast<std::chrono::milliseconds>(
220ac9a064cSDimitry Andric               std::chrono::steady_clock::now() - Start);
221ac9a064cSDimitry Andric 
222ac9a064cSDimitry Andric       if (TotalElapsedTime >= Timeout)
223ac9a064cSDimitry Andric         return std::make_error_code(std::errc::operation_would_block);
224ac9a064cSDimitry Andric 
225ac9a064cSDimitry Andric       RemainingTimeout = Timeout - TotalElapsedTime;
226ac9a064cSDimitry Andric     }
227ac9a064cSDimitry Andric #ifdef _WIN32
228ac9a064cSDimitry Andric     PollStatus = WSAPoll(FD, FDCount, RemainingTimeout.count());
229ac9a064cSDimitry Andric   } while (PollStatus == SOCKET_ERROR &&
230ac9a064cSDimitry Andric            getLastSocketErrorCode() == std::errc::interrupted);
231ac9a064cSDimitry Andric #else
232ac9a064cSDimitry Andric     PollStatus = ::poll(FD, FDCount, RemainingTimeout.count());
233ac9a064cSDimitry Andric   } while (PollStatus == -1 &&
234ac9a064cSDimitry Andric            getLastSocketErrorCode() == std::errc::interrupted);
235ac9a064cSDimitry Andric #endif
236ac9a064cSDimitry Andric 
237ac9a064cSDimitry Andric   // If ActiveFD equals -1 or CancelFD has data to be read then the operation
238ac9a064cSDimitry Andric   // has been canceled by another thread
239ac9a064cSDimitry Andric   if (getActiveFD() == -1 || (CancelFD.has_value() && FD[1].revents & POLLIN))
240ac9a064cSDimitry Andric     return std::make_error_code(std::errc::operation_canceled);
241ac9a064cSDimitry Andric #if _WIN32
242ac9a064cSDimitry Andric   if (PollStatus == SOCKET_ERROR)
243ac9a064cSDimitry Andric #else
244ac9a064cSDimitry Andric   if (PollStatus == -1)
245ac9a064cSDimitry Andric #endif
246ac9a064cSDimitry Andric     return getLastSocketErrorCode();
247ac9a064cSDimitry Andric   if (PollStatus == 0)
248ac9a064cSDimitry Andric     return std::make_error_code(std::errc::timed_out);
249ac9a064cSDimitry Andric   if (FD[0].revents & POLLNVAL)
250ac9a064cSDimitry Andric     return std::make_error_code(std::errc::bad_file_descriptor);
251ac9a064cSDimitry Andric   return std::error_code();
252ac9a064cSDimitry Andric }
253ac9a064cSDimitry Andric 
254ac9a064cSDimitry Andric Expected<std::unique_ptr<raw_socket_stream>>
accept(const std::chrono::milliseconds & Timeout)255ac9a064cSDimitry Andric ListeningSocket::accept(const std::chrono::milliseconds &Timeout) {
256ac9a064cSDimitry Andric   auto getActiveFD = [this]() -> int { return FD; };
257ac9a064cSDimitry Andric   std::error_code TimeoutErr = manageTimeout(Timeout, getActiveFD, PipeFD[0]);
258ac9a064cSDimitry Andric   if (TimeoutErr)
259ac9a064cSDimitry Andric     return llvm::make_error<StringError>(TimeoutErr, "Timeout error");
260ac9a064cSDimitry Andric 
261ac9a064cSDimitry Andric   int AcceptFD;
262ac9a064cSDimitry Andric #ifdef _WIN32
263ac9a064cSDimitry Andric   SOCKET WinAcceptSock = ::accept(_get_osfhandle(FD), NULL, NULL);
264ac9a064cSDimitry Andric   AcceptFD = _open_osfhandle(WinAcceptSock, 0);
265ac9a064cSDimitry Andric #else
266ac9a064cSDimitry Andric   AcceptFD = ::accept(FD, NULL, NULL);
267ac9a064cSDimitry Andric #endif
268ac9a064cSDimitry Andric 
269ac9a064cSDimitry Andric   if (AcceptFD == -1)
270ac9a064cSDimitry Andric     return llvm::make_error<StringError>(getLastSocketErrorCode(),
271ac9a064cSDimitry Andric                                          "Socket accept failed");
272ac9a064cSDimitry Andric   return std::make_unique<raw_socket_stream>(AcceptFD);
273ac9a064cSDimitry Andric }
274ac9a064cSDimitry Andric 
shutdown()275ac9a064cSDimitry Andric void ListeningSocket::shutdown() {
276ac9a064cSDimitry Andric   int ObservedFD = FD.load();
277ac9a064cSDimitry Andric 
278ac9a064cSDimitry Andric   if (ObservedFD == -1)
279ac9a064cSDimitry Andric     return;
280ac9a064cSDimitry Andric 
281ac9a064cSDimitry Andric   // If FD equals ObservedFD set FD to -1; If FD doesn't equal ObservedFD then
282ac9a064cSDimitry Andric   // another thread is responsible for shutdown so return
283ac9a064cSDimitry Andric   if (!FD.compare_exchange_strong(ObservedFD, -1))
284ac9a064cSDimitry Andric     return;
285ac9a064cSDimitry Andric 
286ac9a064cSDimitry Andric   ::close(ObservedFD);
287ac9a064cSDimitry Andric   ::unlink(SocketPath.c_str());
288ac9a064cSDimitry Andric 
289ac9a064cSDimitry Andric   // Ensure ::poll returns if shutdown is called by a separate thread
290ac9a064cSDimitry Andric   char Byte = 'A';
291ac9a064cSDimitry Andric   ssize_t written = ::write(PipeFD[1], &Byte, 1);
292ac9a064cSDimitry Andric 
293ac9a064cSDimitry Andric   // Ignore any write() error
294ac9a064cSDimitry Andric   (void)written;
295ac9a064cSDimitry Andric }
296ac9a064cSDimitry Andric 
~ListeningSocket()297ac9a064cSDimitry Andric ListeningSocket::~ListeningSocket() {
298ac9a064cSDimitry Andric   shutdown();
299ac9a064cSDimitry Andric 
300ac9a064cSDimitry Andric   // Close the pipe's FDs in the destructor instead of within
301ac9a064cSDimitry Andric   // ListeningSocket::shutdown to avoid unnecessary synchronization issues that
302ac9a064cSDimitry Andric   // would occur as PipeFD's values would have to be changed to -1
303ac9a064cSDimitry Andric   //
304ac9a064cSDimitry Andric   // The move constructor sets PipeFD to -1
305ac9a064cSDimitry Andric   if (PipeFD[0] != -1)
306ac9a064cSDimitry Andric     ::close(PipeFD[0]);
307ac9a064cSDimitry Andric   if (PipeFD[1] != -1)
308ac9a064cSDimitry Andric     ::close(PipeFD[1]);
309ac9a064cSDimitry Andric }
310ac9a064cSDimitry Andric 
311ac9a064cSDimitry Andric //===----------------------------------------------------------------------===//
312ac9a064cSDimitry Andric //  raw_socket_stream
313ac9a064cSDimitry Andric //===----------------------------------------------------------------------===//
314ac9a064cSDimitry Andric 
raw_socket_stream(int SocketFD)31599aabd70SDimitry Andric raw_socket_stream::raw_socket_stream(int SocketFD)
31699aabd70SDimitry Andric     : raw_fd_stream(SocketFD, true) {}
31799aabd70SDimitry Andric 
~raw_socket_stream()318ac9a064cSDimitry Andric raw_socket_stream::~raw_socket_stream() {}
319ac9a064cSDimitry Andric 
32099aabd70SDimitry Andric Expected<std::unique_ptr<raw_socket_stream>>
createConnectedUnix(StringRef SocketPath)32199aabd70SDimitry Andric raw_socket_stream::createConnectedUnix(StringRef SocketPath) {
32299aabd70SDimitry Andric #ifdef _WIN32
32399aabd70SDimitry Andric   WSABalancer _;
32499aabd70SDimitry Andric #endif // _WIN32
325ac9a064cSDimitry Andric   Expected<int> FD = getSocketFD(SocketPath);
32699aabd70SDimitry Andric   if (!FD)
32799aabd70SDimitry Andric     return FD.takeError();
32899aabd70SDimitry Andric   return std::make_unique<raw_socket_stream>(*FD);
32999aabd70SDimitry Andric }
33099aabd70SDimitry Andric 
read(char * Ptr,size_t Size,const std::chrono::milliseconds & Timeout)331ac9a064cSDimitry Andric ssize_t raw_socket_stream::read(char *Ptr, size_t Size,
332ac9a064cSDimitry Andric                                 const std::chrono::milliseconds &Timeout) {
333ac9a064cSDimitry Andric   auto getActiveFD = [this]() -> int { return this->get_fd(); };
334ac9a064cSDimitry Andric   std::error_code Err = manageTimeout(Timeout, getActiveFD);
335ac9a064cSDimitry Andric   // Mimic raw_fd_stream::read error handling behavior
336ac9a064cSDimitry Andric   if (Err) {
337ac9a064cSDimitry Andric     raw_fd_stream::error_detected(Err);
338ac9a064cSDimitry Andric     return -1;
339ac9a064cSDimitry Andric   }
340ac9a064cSDimitry Andric   return raw_fd_stream::read(Ptr, Size);
34199aabd70SDimitry Andric }
342