15#include "llvm/Config/config.h"
25#include <sys/socket.h>
38#if defined(HAVE_UNISTD_H)
45WSABalancer::WSABalancer() {
47 ::memset(&WsaData, 0,
sizeof(WsaData));
48 if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0) {
53WSABalancer::~WSABalancer() { WSACleanup(); }
58 return std::error_code(::WSAGetLastError(), std::system_category());
65 struct sockaddr_un
Addr;
67 Addr.sun_family = AF_UNIX;
68 strncpy(
Addr.sun_path, SocketPath.
str().c_str(),
sizeof(
Addr.sun_path) - 1);
74 SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
75 if (Socket == INVALID_SOCKET) {
77 int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
81 "Create socket failed");
85 if (::connect(Socket, (
struct sockaddr *)&
Addr,
sizeof(
Addr)) == -1)
87 "Connect socket failed");
90 return _open_osfhandle(Socket, 0);
96ListeningSocket::ListeningSocket(
int SocketFD,
StringRef SocketPath,
98 : FD(SocketFD), SocketPath(SocketPath), PipeFD{PipeFD[0], PipeFD[1]} {}
101 : FD(LS.FD.
load()), SocketPath(LS.SocketPath),
102 PipeFD{LS.PipeFD[0], LS.PipeFD[1]} {
105 LS.SocketPath.clear();
127 return llvm::make_error<StringError>(
128 std::make_error_code(std::errc::file_exists),
129 "Socket address unavailable");
131 ::close(std::move(*MaybeFD));
134 return llvm::make_error<StringError>(
135 std::make_error_code(std::errc::address_in_use),
136 "Socket address unavailable");
141 SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
142 if (Socket == INVALID_SOCKET)
144 int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
148 "socket create failed");
151 if (::bind(Socket, (
struct sockaddr *)&
Addr,
sizeof(
Addr)) == -1) {
155 return llvm::make_error<StringError>(EC,
"Bind error");
159 if (::listen(Socket, MaxBacklog) == -1)
166 if (::_pipe(PipeFD, 1, 0) == -1)
168 if (::pipe(PipeFD) == -1)
174 return ListeningSocket{_open_osfhandle(Socket, 0), SocketPath, PipeFD};
189static std::error_code
191 const std::function<
int()> &getActiveFD,
192 const std::optional<int> &CancelFD = std::nullopt) {
194 FD[0].events = POLLIN;
196 SOCKET WinServerSock = _get_osfhandle(getActiveFD());
197 FD[0].fd = WinServerSock;
199 FD[0].fd = getActiveFD();
202 if (CancelFD.has_value()) {
203 FD[1].events = POLLIN;
204 FD[1].fd = CancelFD.value();
210 auto Start = std::chrono::steady_clock::now();
211 auto RemainingTimeout = Timeout;
216 if (PollStatus != 0 && Timeout != std::chrono::milliseconds(-1)) {
217 auto TotalElapsedTime =
218 std::chrono::duration_cast<std::chrono::milliseconds>(
219 std::chrono::steady_clock::now() - Start);
221 if (TotalElapsedTime >= Timeout)
222 return std::make_error_code(std::errc::operation_would_block);
224 RemainingTimeout = Timeout - TotalElapsedTime;
227 PollStatus = WSAPoll(FD, FDCount, RemainingTimeout.count());
228 }
while (PollStatus == SOCKET_ERROR &&
231 PollStatus = ::poll(FD, FDCount, RemainingTimeout.count());
232 }
while (PollStatus == -1 &&
238 if (getActiveFD() == -1 || (CancelFD.has_value() && FD[1].revents & POLLIN))
239 return std::make_error_code(std::errc::operation_canceled);
241 if (PollStatus == SOCKET_ERROR)
243 if (PollStatus == -1)
247 return std::make_error_code(std::errc::timed_out);
248 if (FD[0].revents & POLLNVAL)
249 return std::make_error_code(std::errc::bad_file_descriptor);
250 return std::error_code();
255 auto getActiveFD = [
this]() ->
int {
return FD; };
256 std::error_code TimeoutErr =
manageTimeout(Timeout, getActiveFD, PipeFD[0]);
258 return llvm::make_error<StringError>(TimeoutErr,
"Timeout error");
262 SOCKET WinAcceptSock =
::accept(_get_osfhandle(FD), NULL, NULL);
263 AcceptFD = _open_osfhandle(WinAcceptSock, 0);
265 AcceptFD =
::accept(FD, NULL, NULL);
270 "Socket accept failed");
271 return std::make_unique<raw_socket_stream>(AcceptFD);
275 int ObservedFD = FD.load();
277 if (ObservedFD == -1)
282 if (!FD.compare_exchange_strong(ObservedFD, -1))
286 ::unlink(SocketPath.c_str());
290 ssize_t written =
::write(PipeFD[1], &Byte, 1);
327 return std::make_unique<raw_socket_stream>(*FD);
331 const std::chrono::milliseconds &Timeout) {
332 auto getActiveFD = [
this]() ->
int {
return this->
get_fd(); };
AMDGPU Mark last scratch load
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
Manages a passive (i.e., listening) UNIX domain socket.
static Expected< ListeningSocket > createUnix(StringRef SocketPath, int MaxBacklog=llvm::hardware_concurrency().compute_thread_count())
Creates a listening socket bound to the specified file system path.
void shutdown()
Closes the FD, unlinks the socket file, and writes to PipeFD.
Expected< std::unique_ptr< raw_socket_stream > > accept(const std::chrono::milliseconds &Timeout=std::chrono::milliseconds(-1))
Accepts an incoming connection on the listening socket.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
int get_fd() const
Return the file descriptor.
void error_detected(std::error_code EC)
Set the flag indicating that an output error has been encountered.
A raw_ostream of a file for reading/writing/seeking.
ssize_t read(char *Ptr, size_t Size)
This reads the Size bytes into a buffer pointed by Ptr.
static Expected< std::unique_ptr< raw_socket_stream > > createConnectedUnix(StringRef SocketPath)
Create a raw_socket_stream connected to the UNIX domain socket at SocketPath.
raw_socket_stream(int SocketFD)
ssize_t read(char *Ptr, size_t Size, const std::chrono::milliseconds &Timeout=std::chrono::milliseconds(-1))
Attempt to read from the raw_socket_stream's file descriptor.
bool exists(const basic_file_status &status)
Does file exist?
This is an optimization pass for GlobalISel generic memory operations.
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
void consumeError(Error Err)
Consume a Error without doing anything.
static Expected< int > getSocketFD(StringRef SocketPath)
static std::error_code getLastSocketErrorCode()
static std::error_code manageTimeout(const std::chrono::milliseconds &Timeout, const std::function< int()> &getActiveFD, const std::optional< int > &CancelFD=std::nullopt)
static sockaddr_un setSocketAddr(StringRef SocketPath)