180814287SRaphael Isemann //===-- Socket.cpp --------------------------------------------------------===//
298688922SZachary 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
698688922SZachary Turner //
798688922SZachary Turner //===----------------------------------------------------------------------===//
898688922SZachary Turner
998688922SZachary Turner #include "lldb/Host/Socket.h"
1098688922SZachary Turner
1198688922SZachary Turner #include "lldb/Host/Config.h"
1298688922SZachary Turner #include "lldb/Host/Host.h"
1398688922SZachary Turner #include "lldb/Host/SocketAddress.h"
14e98628ceSOleksiy Vyalov #include "lldb/Host/common/TCPSocket.h"
15e98628ceSOleksiy Vyalov #include "lldb/Host/common/UDPSocket.h"
16*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
176f9e6901SZachary Turner #include "lldb/Utility/Log.h"
18e98628ceSOleksiy Vyalov
1911827799SChris Bieneman #include "llvm/ADT/STLExtras.h"
202819136fSMichal Gorny #include "llvm/Support/Errno.h"
21f8a74c18SAaron Smith #include "llvm/Support/Error.h"
2275e164f6Sserge-sans-paille #include "llvm/Support/Regex.h"
23f8a74c18SAaron Smith #include "llvm/Support/WindowsError.h"
2411827799SChris Bieneman
253011d55fSJonas Devlieghere #if LLDB_ENABLE_POSIX
26e98628ceSOleksiy Vyalov #include "lldb/Host/posix/DomainSocket.h"
27e98628ceSOleksiy Vyalov
28e98628ceSOleksiy Vyalov #include <arpa/inet.h>
29e98628ceSOleksiy Vyalov #include <netdb.h>
30e98628ceSOleksiy Vyalov #include <netinet/in.h>
31e98628ceSOleksiy Vyalov #include <netinet/tcp.h>
32e98628ceSOleksiy Vyalov #include <sys/socket.h>
33e98628ceSOleksiy Vyalov #include <sys/un.h>
34b6dbe9a9SPavel Labath #include <unistd.h>
35e98628ceSOleksiy Vyalov #endif
3698688922SZachary Turner
37179c51e0SOleksiy Vyalov #ifdef __linux__
38179c51e0SOleksiy Vyalov #include "lldb/Host/linux/AbstractSocket.h"
39179c51e0SOleksiy Vyalov #endif
40179c51e0SOleksiy Vyalov
41e705c8b5SPavel Labath #ifdef __ANDROID__
428da0bf3bSShawn Best #include <arpa/inet.h>
43b9c1b51eSKate Stone #include <asm-generic/errno-base.h>
4476e47d48SRaphael Isemann #include <cerrno>
4528753189STamas Berghammer #include <fcntl.h>
4676e47d48SRaphael Isemann #include <linux/tcp.h>
47b9c1b51eSKate Stone #include <sys/syscall.h>
48b9c1b51eSKate Stone #include <unistd.h>
49e705c8b5SPavel Labath #endif // __ANDROID__
508da0bf3bSShawn Best
5198688922SZachary Turner using namespace lldb;
5298688922SZachary Turner using namespace lldb_private;
5398688922SZachary Turner
5498688922SZachary Turner #if defined(_WIN32)
5598688922SZachary Turner typedef const char *set_socket_option_arg_type;
5698688922SZachary Turner typedef char *get_socket_option_arg_type;
5798688922SZachary Turner const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET;
5898688922SZachary Turner #else // #if defined(_WIN32)
5998688922SZachary Turner typedef const void *set_socket_option_arg_type;
6098688922SZachary Turner typedef void *get_socket_option_arg_type;
6198688922SZachary Turner const NativeSocket Socket::kInvalidSocketValue = -1;
6298688922SZachary Turner #endif // #if defined(_WIN32)
6398688922SZachary Turner
IsInterrupted()6493c1b3caSPavel Labath static bool IsInterrupted() {
654e1588c2SOleksiy Vyalov #if defined(_WIN32)
664e1588c2SOleksiy Vyalov return ::WSAGetLastError() == WSAEINTR;
674e1588c2SOleksiy Vyalov #else
684e1588c2SOleksiy Vyalov return errno == EINTR;
694e1588c2SOleksiy Vyalov #endif
704e1588c2SOleksiy Vyalov }
71477e42a6SOleksiy Vyalov
Socket(SocketProtocol protocol,bool should_close,bool child_processes_inherit)7211827799SChris Bieneman Socket::Socket(SocketProtocol protocol, bool should_close,
7311827799SChris Bieneman bool child_processes_inherit)
7411751271SLawrence D'Anna : IOObject(eFDTypeSocket), m_protocol(protocol),
7511827799SChris Bieneman m_socket(kInvalidSocketValue),
7611751271SLawrence D'Anna m_child_processes_inherit(child_processes_inherit),
7711751271SLawrence D'Anna m_should_close_fd(should_close) {}
7898688922SZachary Turner
~Socket()79b9c1b51eSKate Stone Socket::~Socket() { Close(); }
8098688922SZachary Turner
Initialize()81f8a74c18SAaron Smith llvm::Error Socket::Initialize() {
82f8a74c18SAaron Smith #if defined(_WIN32)
83f8a74c18SAaron Smith auto wVersion = WINSOCK_VERSION;
84f8a74c18SAaron Smith WSADATA wsaData;
85f8a74c18SAaron Smith int err = ::WSAStartup(wVersion, &wsaData);
86f8a74c18SAaron Smith if (err == 0) {
87f8a74c18SAaron Smith if (wsaData.wVersion < wVersion) {
88f8a74c18SAaron Smith WSACleanup();
89f8a74c18SAaron Smith return llvm::make_error<llvm::StringError>(
90f8a74c18SAaron Smith "WSASock version is not expected.", llvm::inconvertibleErrorCode());
91f8a74c18SAaron Smith }
92f8a74c18SAaron Smith } else {
93f8a74c18SAaron Smith return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
94f8a74c18SAaron Smith }
95f8a74c18SAaron Smith #endif
96f8a74c18SAaron Smith
97f8a74c18SAaron Smith return llvm::Error::success();
98f8a74c18SAaron Smith }
99f8a74c18SAaron Smith
Terminate()100f8a74c18SAaron Smith void Socket::Terminate() {
101f8a74c18SAaron Smith #if defined(_WIN32)
102f8a74c18SAaron Smith ::WSACleanup();
103f8a74c18SAaron Smith #endif
104f8a74c18SAaron Smith }
105f8a74c18SAaron Smith
Create(const SocketProtocol protocol,bool child_processes_inherit,Status & error)106b9c1b51eSKate Stone std::unique_ptr<Socket> Socket::Create(const SocketProtocol protocol,
107b9c1b51eSKate Stone bool child_processes_inherit,
10897206d57SZachary Turner Status &error) {
109db4d9865SOleksiy Vyalov error.Clear();
110db4d9865SOleksiy Vyalov
111db4d9865SOleksiy Vyalov std::unique_ptr<Socket> socket_up;
112b9c1b51eSKate Stone switch (protocol) {
113db4d9865SOleksiy Vyalov case ProtocolTcp:
11411827799SChris Bieneman socket_up =
115a8f3ae7cSJonas Devlieghere std::make_unique<TCPSocket>(true, child_processes_inherit);
116db4d9865SOleksiy Vyalov break;
117db4d9865SOleksiy Vyalov case ProtocolUdp:
11811827799SChris Bieneman socket_up =
119a8f3ae7cSJonas Devlieghere std::make_unique<UDPSocket>(true, child_processes_inherit);
120db4d9865SOleksiy Vyalov break;
121db4d9865SOleksiy Vyalov case ProtocolUnixDomain:
1223011d55fSJonas Devlieghere #if LLDB_ENABLE_POSIX
12311827799SChris Bieneman socket_up =
124a8f3ae7cSJonas Devlieghere std::make_unique<DomainSocket>(true, child_processes_inherit);
125db4d9865SOleksiy Vyalov #else
126b9c1b51eSKate Stone error.SetErrorString(
127b9c1b51eSKate Stone "Unix domain sockets are not supported on this platform.");
128db4d9865SOleksiy Vyalov #endif
129db4d9865SOleksiy Vyalov break;
130db4d9865SOleksiy Vyalov case ProtocolUnixAbstract:
131db4d9865SOleksiy Vyalov #ifdef __linux__
13211827799SChris Bieneman socket_up =
133a8f3ae7cSJonas Devlieghere std::make_unique<AbstractSocket>(child_processes_inherit);
134db4d9865SOleksiy Vyalov #else
135b9c1b51eSKate Stone error.SetErrorString(
136b9c1b51eSKate Stone "Abstract domain sockets are not supported on this platform.");
137db4d9865SOleksiy Vyalov #endif
138db4d9865SOleksiy Vyalov break;
139db4d9865SOleksiy Vyalov }
140db4d9865SOleksiy Vyalov
141db4d9865SOleksiy Vyalov if (error.Fail())
142db4d9865SOleksiy Vyalov socket_up.reset();
143db4d9865SOleksiy Vyalov
144db4d9865SOleksiy Vyalov return socket_up;
145db4d9865SOleksiy Vyalov }
146db4d9865SOleksiy Vyalov
147c9e6b701SPavel Labath llvm::Expected<std::unique_ptr<Socket>>
TcpConnect(llvm::StringRef host_and_port,bool child_processes_inherit)148c9e6b701SPavel Labath Socket::TcpConnect(llvm::StringRef host_and_port,
149c9e6b701SPavel Labath bool child_processes_inherit) {
150a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Connection);
151c9e6b701SPavel Labath LLDB_LOG(log, "host_and_port = {0}", host_and_port);
15298688922SZachary Turner
15397206d57SZachary Turner Status error;
154b9c1b51eSKate Stone std::unique_ptr<Socket> connect_socket(
155b9c1b51eSKate Stone Create(ProtocolTcp, child_processes_inherit, error));
156e98628ceSOleksiy Vyalov if (error.Fail())
157c9e6b701SPavel Labath return error.ToError();
15898688922SZachary Turner
159e98628ceSOleksiy Vyalov error = connect_socket->Connect(host_and_port);
160e98628ceSOleksiy Vyalov if (error.Success())
161c9e6b701SPavel Labath return std::move(connect_socket);
16298688922SZachary Turner
163c9e6b701SPavel Labath return error.ToError();
16498688922SZachary Turner }
16598688922SZachary Turner
166c9e6b701SPavel Labath llvm::Expected<std::unique_ptr<TCPSocket>>
TcpListen(llvm::StringRef host_and_port,bool child_processes_inherit,int backlog)167c9e6b701SPavel Labath Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit,
1684373f359SMichał Górny int backlog) {
169a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Connection);
170c9e6b701SPavel Labath LLDB_LOG(log, "host_and_port = {0}", host_and_port);
17198688922SZachary Turner
172b9c1b51eSKate Stone std::unique_ptr<TCPSocket> listen_socket(
17311827799SChris Bieneman new TCPSocket(true, child_processes_inherit));
17498688922SZachary Turner
1755f1c8d8aSMichał Górny Status error = listen_socket->Listen(host_and_port, backlog);
176c9e6b701SPavel Labath if (error.Fail())
177c9e6b701SPavel Labath return error.ToError();
178c9e6b701SPavel Labath
179c9e6b701SPavel Labath return std::move(listen_socket);
18098688922SZachary Turner }
18198688922SZachary Turner
182c9e6b701SPavel Labath llvm::Expected<std::unique_ptr<UDPSocket>>
UdpConnect(llvm::StringRef host_and_port,bool child_processes_inherit)183c9e6b701SPavel Labath Socket::UdpConnect(llvm::StringRef host_and_port,
184c9e6b701SPavel Labath bool child_processes_inherit) {
185c9e6b701SPavel Labath return UDPSocket::Connect(host_and_port, child_processes_inherit);
18698688922SZachary Turner }
18798688922SZachary Turner
DecodeHostAndPort(llvm::StringRef host_and_port)188073c5d0eSMichał Górny llvm::Expected<Socket::HostAndPort> Socket::DecodeHostAndPort(llvm::StringRef host_and_port) {
1895f1c8d8aSMichał Górny static llvm::Regex g_regex("([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
190073c5d0eSMichał Górny HostAndPort ret;
1913af3f1e8SJonas Devlieghere llvm::SmallVector<llvm::StringRef, 3> matches;
1925f1c8d8aSMichał Górny if (g_regex.match(host_and_port, &matches)) {
193073c5d0eSMichał Górny ret.hostname = matches[1].str();
19411827799SChris Bieneman // IPv6 addresses are wrapped in [] when specified with ports
195073c5d0eSMichał Górny if (ret.hostname.front() == '[' && ret.hostname.back() == ']')
196073c5d0eSMichał Górny ret.hostname = ret.hostname.substr(1, ret.hostname.size() - 2);
197073c5d0eSMichał Górny if (to_integer(matches[2], ret.port, 10))
198073c5d0eSMichał Górny return ret;
1995f1c8d8aSMichał Górny } else {
200073c5d0eSMichał Górny // If this was unsuccessful, then check if it's simply an unsigned 16-bit
20105097246SAdrian Prantl // integer, representing a port with an empty host.
202073c5d0eSMichał Górny if (to_integer(host_and_port, ret.port, 10))
203073c5d0eSMichał Górny return ret;
20498688922SZachary Turner }
20598688922SZachary Turner
2065f1c8d8aSMichał Górny return llvm::createStringError(llvm::inconvertibleErrorCode(),
2075f1c8d8aSMichał Górny "invalid host:port specification: '%s'",
2081a0c0ffaSAdrian Prantl host_and_port.str().c_str());
20998688922SZachary Turner }
21098688922SZachary Turner
GetWaitableHandle()211b9c1b51eSKate Stone IOObject::WaitableHandle Socket::GetWaitableHandle() {
21298688922SZachary Turner // TODO: On Windows, use WSAEventSelect
21398688922SZachary Turner return m_socket;
21498688922SZachary Turner }
21598688922SZachary Turner
Read(void * buf,size_t & num_bytes)21697206d57SZachary Turner Status Socket::Read(void *buf, size_t &num_bytes) {
21797206d57SZachary Turner Status error;
21898688922SZachary Turner int bytes_received = 0;
219b9c1b51eSKate Stone do {
22098688922SZachary Turner bytes_received = ::recv(m_socket, static_cast<char *>(buf), num_bytes, 0);
2214e1588c2SOleksiy Vyalov } while (bytes_received < 0 && IsInterrupted());
22298688922SZachary Turner
223b9c1b51eSKate Stone if (bytes_received < 0) {
2244e1588c2SOleksiy Vyalov SetLastError(error);
22598688922SZachary Turner num_bytes = 0;
226b9c1b51eSKate Stone } else
22798688922SZachary Turner num_bytes = bytes_received;
22898688922SZachary Turner
229a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Communication);
230b9c1b51eSKate Stone if (log) {
23163e5fb76SJonas Devlieghere LLDB_LOGF(log,
23263e5fb76SJonas Devlieghere "%p Socket::Read() (socket = %" PRIu64
233b9c1b51eSKate Stone ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64
234b9c1b51eSKate Stone " (error = %s)",
235b9c1b51eSKate Stone static_cast<void *>(this), static_cast<uint64_t>(m_socket), buf,
23698688922SZachary Turner static_cast<uint64_t>(num_bytes),
237b9c1b51eSKate Stone static_cast<int64_t>(bytes_received), error.AsCString());
23898688922SZachary Turner }
23998688922SZachary Turner
24098688922SZachary Turner return error;
24198688922SZachary Turner }
24298688922SZachary Turner
Write(const void * buf,size_t & num_bytes)24397206d57SZachary Turner Status Socket::Write(const void *buf, size_t &num_bytes) {
244f8b47628SRaphael Isemann const size_t src_len = num_bytes;
24597206d57SZachary Turner Status error;
24698688922SZachary Turner int bytes_sent = 0;
247b9c1b51eSKate Stone do {
248e98628ceSOleksiy Vyalov bytes_sent = Send(buf, num_bytes);
2494e1588c2SOleksiy Vyalov } while (bytes_sent < 0 && IsInterrupted());
25098688922SZachary Turner
251b9c1b51eSKate Stone if (bytes_sent < 0) {
2524e1588c2SOleksiy Vyalov SetLastError(error);
25398688922SZachary Turner num_bytes = 0;
254b9c1b51eSKate Stone } else
25598688922SZachary Turner num_bytes = bytes_sent;
25698688922SZachary Turner
257a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Communication);
258b9c1b51eSKate Stone if (log) {
25963e5fb76SJonas Devlieghere LLDB_LOGF(log,
26063e5fb76SJonas Devlieghere "%p Socket::Write() (socket = %" PRIu64
261b9c1b51eSKate Stone ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64
262b9c1b51eSKate Stone " (error = %s)",
263b9c1b51eSKate Stone static_cast<void *>(this), static_cast<uint64_t>(m_socket), buf,
264f8b47628SRaphael Isemann static_cast<uint64_t>(src_len),
265b9c1b51eSKate Stone static_cast<int64_t>(bytes_sent), error.AsCString());
26698688922SZachary Turner }
26798688922SZachary Turner
26898688922SZachary Turner return error;
26998688922SZachary Turner }
27098688922SZachary Turner
PreDisconnect()27197206d57SZachary Turner Status Socket::PreDisconnect() {
27297206d57SZachary Turner Status error;
27398688922SZachary Turner return error;
27498688922SZachary Turner }
27598688922SZachary Turner
Close()27697206d57SZachary Turner Status Socket::Close() {
27797206d57SZachary Turner Status error;
27898688922SZachary Turner if (!IsValid() || !m_should_close_fd)
27998688922SZachary Turner return error;
28098688922SZachary Turner
281a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Connection);
28263e5fb76SJonas Devlieghere LLDB_LOGF(log, "%p Socket::Close (fd = %" PRIu64 ")",
283b0717666SAlexandre Ganea static_cast<void *>(this), static_cast<uint64_t>(m_socket));
28498688922SZachary Turner
28598688922SZachary Turner #if defined(_WIN32)
2864f6d3a37SShafik Yaghmour bool success = closesocket(m_socket) == 0;
28798688922SZachary Turner #else
2884f6d3a37SShafik Yaghmour bool success = ::close(m_socket) == 0;
28998688922SZachary Turner #endif
29098688922SZachary Turner // A reference to a FD was passed in, set it to an invalid value
29198688922SZachary Turner m_socket = kInvalidSocketValue;
292b9c1b51eSKate Stone if (!success) {
2934e1588c2SOleksiy Vyalov SetLastError(error);
29498688922SZachary Turner }
29598688922SZachary Turner
29698688922SZachary Turner return error;
29798688922SZachary Turner }
29898688922SZachary Turner
GetOption(int level,int option_name,int & option_value)299b9c1b51eSKate Stone int Socket::GetOption(int level, int option_name, int &option_value) {
300b9c1b51eSKate Stone get_socket_option_arg_type option_value_p =
301b9c1b51eSKate Stone reinterpret_cast<get_socket_option_arg_type>(&option_value);
30298688922SZachary Turner socklen_t option_value_size = sizeof(int);
303b9c1b51eSKate Stone return ::getsockopt(m_socket, level, option_name, option_value_p,
304b9c1b51eSKate Stone &option_value_size);
30598688922SZachary Turner }
30698688922SZachary Turner
SetOption(int level,int option_name,int option_value)307b9c1b51eSKate Stone int Socket::SetOption(int level, int option_name, int option_value) {
308b9c1b51eSKate Stone set_socket_option_arg_type option_value_p =
309b9c1b51eSKate Stone reinterpret_cast<get_socket_option_arg_type>(&option_value);
310b9c1b51eSKate Stone return ::setsockopt(m_socket, level, option_name, option_value_p,
311b9c1b51eSKate Stone sizeof(option_value));
31298688922SZachary Turner }
31398688922SZachary Turner
Send(const void * buf,const size_t num_bytes)314b9c1b51eSKate Stone size_t Socket::Send(const void *buf, const size_t num_bytes) {
315e98628ceSOleksiy Vyalov return ::send(m_socket, static_cast<const char *>(buf), num_bytes, 0);
31698688922SZachary Turner }
31798688922SZachary Turner
SetLastError(Status & error)31897206d57SZachary Turner void Socket::SetLastError(Status &error) {
319e98628ceSOleksiy Vyalov #if defined(_WIN32)
320e98628ceSOleksiy Vyalov error.SetError(::WSAGetLastError(), lldb::eErrorTypeWin32);
321e98628ceSOleksiy Vyalov #else
322e98628ceSOleksiy Vyalov error.SetErrorToErrno();
323e98628ceSOleksiy Vyalov #endif
32498688922SZachary Turner }
325014bb7daSVince Harron
CreateSocket(const int domain,const int type,const int protocol,bool child_processes_inherit,Status & error)326b9c1b51eSKate Stone NativeSocket Socket::CreateSocket(const int domain, const int type,
327e98628ceSOleksiy Vyalov const int protocol,
32897206d57SZachary Turner bool child_processes_inherit, Status &error) {
329e98628ceSOleksiy Vyalov error.Clear();
33011827799SChris Bieneman auto socket_type = type;
331e98628ceSOleksiy Vyalov #ifdef SOCK_CLOEXEC
332e98628ceSOleksiy Vyalov if (!child_processes_inherit)
33311827799SChris Bieneman socket_type |= SOCK_CLOEXEC;
334e98628ceSOleksiy Vyalov #endif
33511827799SChris Bieneman auto sock = ::socket(domain, socket_type, protocol);
336e98628ceSOleksiy Vyalov if (sock == kInvalidSocketValue)
337e98628ceSOleksiy Vyalov SetLastError(error);
338e98628ceSOleksiy Vyalov
339e98628ceSOleksiy Vyalov return sock;
340014bb7daSVince Harron }
341014bb7daSVince Harron
AcceptSocket(NativeSocket sockfd,struct sockaddr * addr,socklen_t * addrlen,bool child_processes_inherit,Status & error)342b9c1b51eSKate Stone NativeSocket Socket::AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
343e98628ceSOleksiy Vyalov socklen_t *addrlen,
34497206d57SZachary Turner bool child_processes_inherit, Status &error) {
345e98628ceSOleksiy Vyalov error.Clear();
346095d6b8fSPavel Labath #if defined(ANDROID_USE_ACCEPT_WORKAROUND)
347ef97630fSPavel Labath // Hack:
34805097246SAdrian Prantl // This enables static linking lldb-server to an API 21 libc, but still
34905097246SAdrian Prantl // having it run on older devices. It is necessary because API 21 libc's
350ef97630fSPavel Labath // implementation of accept() uses the accept4 syscall(), which is not
351ef97630fSPavel Labath // available in older kernels. Using an older libc would fix this issue, but
352ef97630fSPavel Labath // introduce other ones, as the old libraries were quite buggy.
353e98628ceSOleksiy Vyalov int fd = syscall(__NR_accept, sockfd, addr, addrlen);
354b9c1b51eSKate Stone if (fd >= 0 && !child_processes_inherit) {
355e98628ceSOleksiy Vyalov int flags = ::fcntl(fd, F_GETFD);
356e98628ceSOleksiy Vyalov if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)
357e98628ceSOleksiy Vyalov return fd;
358e98628ceSOleksiy Vyalov SetLastError(error);
359e98628ceSOleksiy Vyalov close(fd);
360014bb7daSVince Harron }
361e98628ceSOleksiy Vyalov return fd;
362d4abad9aSEugene Zemtsov #elif defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
363e98628ceSOleksiy Vyalov int flags = 0;
364e98628ceSOleksiy Vyalov if (!child_processes_inherit) {
365e98628ceSOleksiy Vyalov flags |= SOCK_CLOEXEC;
366014bb7daSVince Harron }
3675534a675SMartin Storsjo NativeSocket fd = llvm::sys::RetryAfterSignal(
3685534a675SMartin Storsjo static_cast<NativeSocket>(-1), ::accept4, sockfd, addr, addrlen, flags);
369e98628ceSOleksiy Vyalov #else
3705534a675SMartin Storsjo NativeSocket fd = llvm::sys::RetryAfterSignal(
3715534a675SMartin Storsjo static_cast<NativeSocket>(-1), ::accept, sockfd, addr, addrlen);
372e98628ceSOleksiy Vyalov #endif
373e98628ceSOleksiy Vyalov if (fd == kInvalidSocketValue)
374e98628ceSOleksiy Vyalov SetLastError(error);
375e98628ceSOleksiy Vyalov return fd;
376014bb7daSVince Harron }
377073c5d0eSMichał Górny
operator <<(llvm::raw_ostream & OS,const Socket::HostAndPort & HP)378073c5d0eSMichał Górny llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &OS,
379073c5d0eSMichał Górny const Socket::HostAndPort &HP) {
380073c5d0eSMichał Górny return OS << '[' << HP.hostname << ']' << ':' << HP.port;
381073c5d0eSMichał Górny }
382