15#include "llvm/Config/config.h"
26#include <sys/socket.h>
39#if defined(HAVE_UNISTD_H)
46WSABalancer::WSABalancer() {
48 ::memset(&WsaData, 0,
sizeof(WsaData));
49 if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0) {
54WSABalancer::~WSABalancer() { WSACleanup(); }
59 return std::error_code(::WSAGetLastError(), std::system_category());
66 struct sockaddr_un
Addr;
68 Addr.sun_family = AF_UNIX;
69 strncpy(
Addr.sun_path, SocketPath.
str().c_str(),
sizeof(
Addr.sun_path) - 1);
75 SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
76 if (Socket == INVALID_SOCKET) {
78 int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
82 "Create socket failed");
86 if (::connect(Socket, (
struct sockaddr *)&
Addr,
sizeof(
Addr)) == -1)
88 "Connect socket failed");
91 return _open_osfhandle(Socket, 0);
97ListeningSocket::ListeningSocket(
int SocketFD,
StringRef SocketPath,
99 : FD(SocketFD), SocketPath(SocketPath), PipeFD{PipeFD[0], PipeFD[1]} {}
102 : FD(LS.FD.
load()), SocketPath(LS.SocketPath),
103 PipeFD{LS.PipeFD[0], LS.PipeFD[1]} {
106 LS.SocketPath.clear();
128 return llvm::make_error<StringError>(
129 std::make_error_code(std::errc::file_exists),
130 "Socket address unavailable");
132 ::close(std::move(*MaybeFD));
135 return llvm::make_error<StringError>(
136 std::make_error_code(std::errc::address_in_use),
137 "Socket address unavailable");
142 SOCKET Socket = socket(AF_UNIX, SOCK_STREAM, 0);
143 if (Socket == INVALID_SOCKET)
145 int Socket = socket(AF_UNIX, SOCK_STREAM, 0);
149 "socket create failed");
152 if (::bind(Socket, (
struct sockaddr *)&
Addr,
sizeof(
Addr)) == -1) {
156 return llvm::make_error<StringError>(EC,
"Bind error");
160 if (::listen(Socket, MaxBacklog) == -1)
167 if (::_pipe(PipeFD, 1, 0) == -1)
169 if (::pipe(PipeFD) == -1)
175 return ListeningSocket{_open_osfhandle(Socket, 0), SocketPath, PipeFD};
190static std::error_code
192 const std::function<
int()> &getActiveFD,
193 const std::optional<int> &CancelFD = std::nullopt) {
195 FD[0].events = POLLIN;
197 SOCKET WinServerSock = _get_osfhandle(getActiveFD());
198 FD[0].fd = WinServerSock;
200 FD[0].fd = getActiveFD();
203 if (CancelFD.has_value()) {
204 FD[1].events = POLLIN;
205 FD[1].fd = CancelFD.value();
211 auto Start = std::chrono::steady_clock::now();
212 auto RemainingTimeout = Timeout;
217 if (PollStatus != 0 && Timeout != std::chrono::milliseconds(-1)) {
218 auto TotalElapsedTime =
219 std::chrono::duration_cast<std::chrono::milliseconds>(
220 std::chrono::steady_clock::now() - Start);
222 if (TotalElapsedTime >= Timeout)
223 return std::make_error_code(std::errc::operation_would_block);
225 RemainingTimeout = Timeout - TotalElapsedTime;
228 PollStatus = WSAPoll(FD, FDCount, RemainingTimeout.count());
229 }
while (PollStatus == SOCKET_ERROR &&
232 PollStatus = ::poll(FD, FDCount, RemainingTimeout.count());
233 }
while (PollStatus == -1 &&
239 if (getActiveFD() == -1 || (CancelFD.has_value() && FD[1].revents & POLLIN))
240 return std::make_error_code(std::errc::operation_canceled);
242 if (PollStatus == SOCKET_ERROR)
244 if (PollStatus == -1)
248 return std::make_error_code(std::errc::timed_out);
249 if (FD[0].revents & POLLNVAL)
250 return std::make_error_code(std::errc::bad_file_descriptor);
251 return std::error_code();
256 auto getActiveFD = [
this]() ->
int {
return FD; };
257 std::error_code TimeoutErr =
manageTimeout(Timeout, getActiveFD, PipeFD[0]);
259 return llvm::make_error<StringError>(TimeoutErr,
"Timeout error");
263 SOCKET WinAcceptSock =
::accept(_get_osfhandle(FD), NULL, NULL);
264 AcceptFD = _open_osfhandle(WinAcceptSock, 0);
266 AcceptFD =
::accept(FD, NULL, NULL);
271 "Socket accept failed");
272 return std::make_unique<raw_socket_stream>(AcceptFD);
276 int ObservedFD = FD.load();
278 if (ObservedFD == -1)
283 if (!FD.compare_exchange_strong(ObservedFD, -1))
287 ::unlink(SocketPath.c_str());
291 ssize_t written =
::write(PipeFD[1], &Byte, 1);
328 return std::make_unique<raw_socket_stream>(*FD);
332 const std::chrono::milliseconds &Timeout) {
333 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)