xref: /freebsd-src/contrib/llvm-project/lldb/source/Host/common/Socket.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15ffd83dbSDimitry Andric //===-- Socket.cpp --------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Host/Socket.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Host/Config.h"
120b57cec5SDimitry Andric #include "lldb/Host/Host.h"
130b57cec5SDimitry Andric #include "lldb/Host/SocketAddress.h"
140b57cec5SDimitry Andric #include "lldb/Host/common/TCPSocket.h"
150b57cec5SDimitry Andric #include "lldb/Host/common/UDPSocket.h"
1681ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
170b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
2006c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
210b57cec5SDimitry Andric #include "llvm/Support/Errno.h"
220b57cec5SDimitry Andric #include "llvm/Support/Error.h"
2304eeddc0SDimitry Andric #include "llvm/Support/Regex.h"
240b57cec5SDimitry Andric #include "llvm/Support/WindowsError.h"
250b57cec5SDimitry Andric 
26480093f4SDimitry Andric #if LLDB_ENABLE_POSIX
270b57cec5SDimitry Andric #include "lldb/Host/posix/DomainSocket.h"
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric #include <arpa/inet.h>
300b57cec5SDimitry Andric #include <netdb.h>
310b57cec5SDimitry Andric #include <netinet/in.h>
320b57cec5SDimitry Andric #include <netinet/tcp.h>
330b57cec5SDimitry Andric #include <sys/socket.h>
340b57cec5SDimitry Andric #include <sys/un.h>
350b57cec5SDimitry Andric #include <unistd.h>
360b57cec5SDimitry Andric #endif
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric #ifdef __linux__
390b57cec5SDimitry Andric #include "lldb/Host/linux/AbstractSocket.h"
400b57cec5SDimitry Andric #endif
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric #ifdef __ANDROID__
430b57cec5SDimitry Andric #include <arpa/inet.h>
440b57cec5SDimitry Andric #include <asm-generic/errno-base.h>
45fe6060f1SDimitry Andric #include <cerrno>
460b57cec5SDimitry Andric #include <fcntl.h>
47fe6060f1SDimitry Andric #include <linux/tcp.h>
480b57cec5SDimitry Andric #include <sys/syscall.h>
490b57cec5SDimitry Andric #include <unistd.h>
500b57cec5SDimitry Andric #endif // __ANDROID__
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric using namespace lldb;
530b57cec5SDimitry Andric using namespace lldb_private;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric #if defined(_WIN32)
560b57cec5SDimitry Andric typedef const char *set_socket_option_arg_type;
570b57cec5SDimitry Andric typedef char *get_socket_option_arg_type;
580b57cec5SDimitry Andric const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET;
590b57cec5SDimitry Andric #else  // #if defined(_WIN32)
600b57cec5SDimitry Andric typedef const void *set_socket_option_arg_type;
610b57cec5SDimitry Andric typedef void *get_socket_option_arg_type;
620b57cec5SDimitry Andric const NativeSocket Socket::kInvalidSocketValue = -1;
630b57cec5SDimitry Andric #endif // #if defined(_WIN32)
640b57cec5SDimitry Andric 
65349cc55cSDimitry Andric static bool IsInterrupted() {
660b57cec5SDimitry Andric #if defined(_WIN32)
670b57cec5SDimitry Andric   return ::WSAGetLastError() == WSAEINTR;
680b57cec5SDimitry Andric #else
690b57cec5SDimitry Andric   return errno == EINTR;
700b57cec5SDimitry Andric #endif
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric Socket::Socket(SocketProtocol protocol, bool should_close,
740b57cec5SDimitry Andric                bool child_processes_inherit)
759dba64beSDimitry Andric     : IOObject(eFDTypeSocket), m_protocol(protocol),
760b57cec5SDimitry Andric       m_socket(kInvalidSocketValue),
779dba64beSDimitry Andric       m_child_processes_inherit(child_processes_inherit),
789dba64beSDimitry Andric       m_should_close_fd(should_close) {}
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric Socket::~Socket() { Close(); }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric llvm::Error Socket::Initialize() {
830b57cec5SDimitry Andric #if defined(_WIN32)
840b57cec5SDimitry Andric   auto wVersion = WINSOCK_VERSION;
850b57cec5SDimitry Andric   WSADATA wsaData;
860b57cec5SDimitry Andric   int err = ::WSAStartup(wVersion, &wsaData);
870b57cec5SDimitry Andric   if (err == 0) {
880b57cec5SDimitry Andric     if (wsaData.wVersion < wVersion) {
890b57cec5SDimitry Andric       WSACleanup();
90*0fca6ea1SDimitry Andric       return llvm::createStringError("WSASock version is not expected.");
910b57cec5SDimitry Andric     }
920b57cec5SDimitry Andric   } else {
930b57cec5SDimitry Andric     return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric #endif
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   return llvm::Error::success();
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric void Socket::Terminate() {
1010b57cec5SDimitry Andric #if defined(_WIN32)
1020b57cec5SDimitry Andric   ::WSACleanup();
1030b57cec5SDimitry Andric #endif
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric std::unique_ptr<Socket> Socket::Create(const SocketProtocol protocol,
1070b57cec5SDimitry Andric                                        bool child_processes_inherit,
1080b57cec5SDimitry Andric                                        Status &error) {
1090b57cec5SDimitry Andric   error.Clear();
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   std::unique_ptr<Socket> socket_up;
1120b57cec5SDimitry Andric   switch (protocol) {
1130b57cec5SDimitry Andric   case ProtocolTcp:
1140b57cec5SDimitry Andric     socket_up =
1159dba64beSDimitry Andric         std::make_unique<TCPSocket>(true, child_processes_inherit);
1160b57cec5SDimitry Andric     break;
1170b57cec5SDimitry Andric   case ProtocolUdp:
1180b57cec5SDimitry Andric     socket_up =
1199dba64beSDimitry Andric         std::make_unique<UDPSocket>(true, child_processes_inherit);
1200b57cec5SDimitry Andric     break;
1210b57cec5SDimitry Andric   case ProtocolUnixDomain:
122480093f4SDimitry Andric #if LLDB_ENABLE_POSIX
1230b57cec5SDimitry Andric     socket_up =
1249dba64beSDimitry Andric         std::make_unique<DomainSocket>(true, child_processes_inherit);
1250b57cec5SDimitry Andric #else
1260b57cec5SDimitry Andric     error.SetErrorString(
1270b57cec5SDimitry Andric         "Unix domain sockets are not supported on this platform.");
1280b57cec5SDimitry Andric #endif
1290b57cec5SDimitry Andric     break;
1300b57cec5SDimitry Andric   case ProtocolUnixAbstract:
1310b57cec5SDimitry Andric #ifdef __linux__
1320b57cec5SDimitry Andric     socket_up =
1339dba64beSDimitry Andric         std::make_unique<AbstractSocket>(child_processes_inherit);
1340b57cec5SDimitry Andric #else
1350b57cec5SDimitry Andric     error.SetErrorString(
1360b57cec5SDimitry Andric         "Abstract domain sockets are not supported on this platform.");
1370b57cec5SDimitry Andric #endif
1380b57cec5SDimitry Andric     break;
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   if (error.Fail())
1420b57cec5SDimitry Andric     socket_up.reset();
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   return socket_up;
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
1475ffd83dbSDimitry Andric llvm::Expected<std::unique_ptr<Socket>>
1485ffd83dbSDimitry Andric Socket::TcpConnect(llvm::StringRef host_and_port,
1495ffd83dbSDimitry Andric                    bool child_processes_inherit) {
15081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Connection);
1515ffd83dbSDimitry Andric   LLDB_LOG(log, "host_and_port = {0}", host_and_port);
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   Status error;
1540b57cec5SDimitry Andric   std::unique_ptr<Socket> connect_socket(
1550b57cec5SDimitry Andric       Create(ProtocolTcp, child_processes_inherit, error));
1560b57cec5SDimitry Andric   if (error.Fail())
1575ffd83dbSDimitry Andric     return error.ToError();
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   error = connect_socket->Connect(host_and_port);
1600b57cec5SDimitry Andric   if (error.Success())
1615ffd83dbSDimitry Andric     return std::move(connect_socket);
1620b57cec5SDimitry Andric 
1635ffd83dbSDimitry Andric   return error.ToError();
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
1665ffd83dbSDimitry Andric llvm::Expected<std::unique_ptr<TCPSocket>>
1675ffd83dbSDimitry Andric Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit,
168349cc55cSDimitry Andric                   int backlog) {
16981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Connection);
1705ffd83dbSDimitry Andric   LLDB_LOG(log, "host_and_port = {0}", host_and_port);
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   std::unique_ptr<TCPSocket> listen_socket(
1730b57cec5SDimitry Andric       new TCPSocket(true, child_processes_inherit));
1740b57cec5SDimitry Andric 
175349cc55cSDimitry Andric   Status error = listen_socket->Listen(host_and_port, backlog);
1765ffd83dbSDimitry Andric   if (error.Fail())
1775ffd83dbSDimitry Andric     return error.ToError();
1785ffd83dbSDimitry Andric 
1795ffd83dbSDimitry Andric   return std::move(listen_socket);
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric 
1825ffd83dbSDimitry Andric llvm::Expected<std::unique_ptr<UDPSocket>>
1835ffd83dbSDimitry Andric Socket::UdpConnect(llvm::StringRef host_and_port,
1845ffd83dbSDimitry Andric                    bool child_processes_inherit) {
1855ffd83dbSDimitry Andric   return UDPSocket::Connect(host_and_port, child_processes_inherit);
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
188349cc55cSDimitry Andric llvm::Expected<Socket::HostAndPort> Socket::DecodeHostAndPort(llvm::StringRef host_and_port) {
189349cc55cSDimitry Andric   static llvm::Regex g_regex("([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
190349cc55cSDimitry Andric   HostAndPort ret;
1919dba64beSDimitry Andric   llvm::SmallVector<llvm::StringRef, 3> matches;
192349cc55cSDimitry Andric   if (g_regex.match(host_and_port, &matches)) {
193349cc55cSDimitry Andric     ret.hostname = matches[1].str();
1940b57cec5SDimitry Andric     // IPv6 addresses are wrapped in [] when specified with ports
195349cc55cSDimitry Andric     if (ret.hostname.front() == '[' && ret.hostname.back() == ']')
196349cc55cSDimitry Andric       ret.hostname = ret.hostname.substr(1, ret.hostname.size() - 2);
197349cc55cSDimitry Andric     if (to_integer(matches[2], ret.port, 10))
198349cc55cSDimitry Andric       return ret;
199349cc55cSDimitry Andric   } else {
200349cc55cSDimitry Andric     // If this was unsuccessful, then check if it's simply an unsigned 16-bit
2010b57cec5SDimitry Andric     // integer, representing a port with an empty host.
202349cc55cSDimitry Andric     if (to_integer(host_and_port, ret.port, 10))
203349cc55cSDimitry Andric       return ret;
2040b57cec5SDimitry Andric   }
2050b57cec5SDimitry Andric 
206349cc55cSDimitry Andric   return llvm::createStringError(llvm::inconvertibleErrorCode(),
207349cc55cSDimitry Andric                                  "invalid host:port specification: '%s'",
2080b57cec5SDimitry Andric                                  host_and_port.str().c_str());
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric IOObject::WaitableHandle Socket::GetWaitableHandle() {
2120b57cec5SDimitry Andric   // TODO: On Windows, use WSAEventSelect
2130b57cec5SDimitry Andric   return m_socket;
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric Status Socket::Read(void *buf, size_t &num_bytes) {
2170b57cec5SDimitry Andric   Status error;
2180b57cec5SDimitry Andric   int bytes_received = 0;
2190b57cec5SDimitry Andric   do {
2200b57cec5SDimitry Andric     bytes_received = ::recv(m_socket, static_cast<char *>(buf), num_bytes, 0);
2210b57cec5SDimitry Andric   } while (bytes_received < 0 && IsInterrupted());
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   if (bytes_received < 0) {
2240b57cec5SDimitry Andric     SetLastError(error);
2250b57cec5SDimitry Andric     num_bytes = 0;
2260b57cec5SDimitry Andric   } else
2270b57cec5SDimitry Andric     num_bytes = bytes_received;
2280b57cec5SDimitry Andric 
22981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Communication);
2300b57cec5SDimitry Andric   if (log) {
2319dba64beSDimitry Andric     LLDB_LOGF(log,
2329dba64beSDimitry Andric               "%p Socket::Read() (socket = %" PRIu64
2330b57cec5SDimitry Andric               ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64
2340b57cec5SDimitry Andric               " (error = %s)",
2350b57cec5SDimitry Andric               static_cast<void *>(this), static_cast<uint64_t>(m_socket), buf,
2360b57cec5SDimitry Andric               static_cast<uint64_t>(num_bytes),
2370b57cec5SDimitry Andric               static_cast<int64_t>(bytes_received), error.AsCString());
2380b57cec5SDimitry Andric   }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   return error;
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric Status Socket::Write(const void *buf, size_t &num_bytes) {
2449dba64beSDimitry Andric   const size_t src_len = num_bytes;
2450b57cec5SDimitry Andric   Status error;
2460b57cec5SDimitry Andric   int bytes_sent = 0;
2470b57cec5SDimitry Andric   do {
2480b57cec5SDimitry Andric     bytes_sent = Send(buf, num_bytes);
2490b57cec5SDimitry Andric   } while (bytes_sent < 0 && IsInterrupted());
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric   if (bytes_sent < 0) {
2520b57cec5SDimitry Andric     SetLastError(error);
2530b57cec5SDimitry Andric     num_bytes = 0;
2540b57cec5SDimitry Andric   } else
2550b57cec5SDimitry Andric     num_bytes = bytes_sent;
2560b57cec5SDimitry Andric 
25781ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Communication);
2580b57cec5SDimitry Andric   if (log) {
2599dba64beSDimitry Andric     LLDB_LOGF(log,
2609dba64beSDimitry Andric               "%p Socket::Write() (socket = %" PRIu64
2610b57cec5SDimitry Andric               ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64
2620b57cec5SDimitry Andric               " (error = %s)",
2630b57cec5SDimitry Andric               static_cast<void *>(this), static_cast<uint64_t>(m_socket), buf,
2649dba64beSDimitry Andric               static_cast<uint64_t>(src_len),
2650b57cec5SDimitry Andric               static_cast<int64_t>(bytes_sent), error.AsCString());
2660b57cec5SDimitry Andric   }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   return error;
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric Status Socket::Close() {
2720b57cec5SDimitry Andric   Status error;
2730b57cec5SDimitry Andric   if (!IsValid() || !m_should_close_fd)
2740b57cec5SDimitry Andric     return error;
2750b57cec5SDimitry Andric 
27681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Connection);
2779dba64beSDimitry Andric   LLDB_LOGF(log, "%p Socket::Close (fd = %" PRIu64 ")",
2780b57cec5SDimitry Andric             static_cast<void *>(this), static_cast<uint64_t>(m_socket));
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric #if defined(_WIN32)
28104eeddc0SDimitry Andric   bool success = closesocket(m_socket) == 0;
2820b57cec5SDimitry Andric #else
28304eeddc0SDimitry Andric   bool success = ::close(m_socket) == 0;
2840b57cec5SDimitry Andric #endif
2850b57cec5SDimitry Andric   // A reference to a FD was passed in, set it to an invalid value
2860b57cec5SDimitry Andric   m_socket = kInvalidSocketValue;
2870b57cec5SDimitry Andric   if (!success) {
2880b57cec5SDimitry Andric     SetLastError(error);
2890b57cec5SDimitry Andric   }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   return error;
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric int Socket::GetOption(int level, int option_name, int &option_value) {
2950b57cec5SDimitry Andric   get_socket_option_arg_type option_value_p =
2960b57cec5SDimitry Andric       reinterpret_cast<get_socket_option_arg_type>(&option_value);
2970b57cec5SDimitry Andric   socklen_t option_value_size = sizeof(int);
2980b57cec5SDimitry Andric   return ::getsockopt(m_socket, level, option_name, option_value_p,
2990b57cec5SDimitry Andric                       &option_value_size);
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric int Socket::SetOption(int level, int option_name, int option_value) {
3030b57cec5SDimitry Andric   set_socket_option_arg_type option_value_p =
3040b57cec5SDimitry Andric       reinterpret_cast<get_socket_option_arg_type>(&option_value);
3050b57cec5SDimitry Andric   return ::setsockopt(m_socket, level, option_name, option_value_p,
3060b57cec5SDimitry Andric                       sizeof(option_value));
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric size_t Socket::Send(const void *buf, const size_t num_bytes) {
3100b57cec5SDimitry Andric   return ::send(m_socket, static_cast<const char *>(buf), num_bytes, 0);
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric void Socket::SetLastError(Status &error) {
3140b57cec5SDimitry Andric #if defined(_WIN32)
3150b57cec5SDimitry Andric   error.SetError(::WSAGetLastError(), lldb::eErrorTypeWin32);
3160b57cec5SDimitry Andric #else
3170b57cec5SDimitry Andric   error.SetErrorToErrno();
3180b57cec5SDimitry Andric #endif
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric NativeSocket Socket::CreateSocket(const int domain, const int type,
3220b57cec5SDimitry Andric                                   const int protocol,
3230b57cec5SDimitry Andric                                   bool child_processes_inherit, Status &error) {
3240b57cec5SDimitry Andric   error.Clear();
3250b57cec5SDimitry Andric   auto socket_type = type;
3260b57cec5SDimitry Andric #ifdef SOCK_CLOEXEC
3270b57cec5SDimitry Andric   if (!child_processes_inherit)
3280b57cec5SDimitry Andric     socket_type |= SOCK_CLOEXEC;
3290b57cec5SDimitry Andric #endif
3300b57cec5SDimitry Andric   auto sock = ::socket(domain, socket_type, protocol);
3310b57cec5SDimitry Andric   if (sock == kInvalidSocketValue)
3320b57cec5SDimitry Andric     SetLastError(error);
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   return sock;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric NativeSocket Socket::AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
3380b57cec5SDimitry Andric                                   socklen_t *addrlen,
3390b57cec5SDimitry Andric                                   bool child_processes_inherit, Status &error) {
3400b57cec5SDimitry Andric   error.Clear();
3410b57cec5SDimitry Andric #if defined(ANDROID_USE_ACCEPT_WORKAROUND)
3420b57cec5SDimitry Andric   // Hack:
3430b57cec5SDimitry Andric   // This enables static linking lldb-server to an API 21 libc, but still
3440b57cec5SDimitry Andric   // having it run on older devices. It is necessary because API 21 libc's
3450b57cec5SDimitry Andric   // implementation of accept() uses the accept4 syscall(), which is not
3460b57cec5SDimitry Andric   // available in older kernels. Using an older libc would fix this issue, but
3470b57cec5SDimitry Andric   // introduce other ones, as the old libraries were quite buggy.
3480b57cec5SDimitry Andric   int fd = syscall(__NR_accept, sockfd, addr, addrlen);
3490b57cec5SDimitry Andric   if (fd >= 0 && !child_processes_inherit) {
3500b57cec5SDimitry Andric     int flags = ::fcntl(fd, F_GETFD);
3510b57cec5SDimitry Andric     if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)
3520b57cec5SDimitry Andric       return fd;
3530b57cec5SDimitry Andric     SetLastError(error);
3540b57cec5SDimitry Andric     close(fd);
3550b57cec5SDimitry Andric   }
3560b57cec5SDimitry Andric   return fd;
3570b57cec5SDimitry Andric #elif defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
3580b57cec5SDimitry Andric   int flags = 0;
3590b57cec5SDimitry Andric   if (!child_processes_inherit) {
3600b57cec5SDimitry Andric     flags |= SOCK_CLOEXEC;
3610b57cec5SDimitry Andric   }
3629dba64beSDimitry Andric   NativeSocket fd = llvm::sys::RetryAfterSignal(
3639dba64beSDimitry Andric       static_cast<NativeSocket>(-1), ::accept4, sockfd, addr, addrlen, flags);
3640b57cec5SDimitry Andric #else
3659dba64beSDimitry Andric   NativeSocket fd = llvm::sys::RetryAfterSignal(
3669dba64beSDimitry Andric       static_cast<NativeSocket>(-1), ::accept, sockfd, addr, addrlen);
3670b57cec5SDimitry Andric #endif
3680b57cec5SDimitry Andric   if (fd == kInvalidSocketValue)
3690b57cec5SDimitry Andric     SetLastError(error);
3700b57cec5SDimitry Andric   return fd;
3710b57cec5SDimitry Andric }
372349cc55cSDimitry Andric 
373349cc55cSDimitry Andric llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &OS,
374349cc55cSDimitry Andric                                             const Socket::HostAndPort &HP) {
375349cc55cSDimitry Andric   return OS << '[' << HP.hostname << ']' << ':' << HP.port;
376349cc55cSDimitry Andric }
377