https://github.com/slydiman updated https://github.com/llvm/llvm-project/pull/106640
>From fe58bf1f08553b75f089692077abdff0ccaf96bb Mon Sep 17 00:00:00 2001 From: Dmitry Vasilyev <dvassil...@accesssoftek.com> Date: Fri, 30 Aug 2024 01:29:58 +0400 Subject: [PATCH 1/2] [lldb][NFC] Move few static helpers to the class Socket --- lldb/include/lldb/Host/Socket.h | 4 ++++ lldb/source/Host/common/Socket.cpp | 24 +++++++++++++++++----- lldb/source/Host/common/TCPSocket.cpp | 29 +++++---------------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lldb/include/lldb/Host/Socket.h b/lldb/include/lldb/Host/Socket.h index 304a91bdf6741b..141566c00497b8 100644 --- a/lldb/include/lldb/Host/Socket.h +++ b/lldb/include/lldb/Host/Socket.h @@ -34,9 +34,11 @@ namespace lldb_private { #if defined(_WIN32) typedef SOCKET NativeSocket; typedef lldb::pipe_t shared_fd_t; +typedef const char *set_socket_option_arg_type; #else typedef int NativeSocket; typedef NativeSocket shared_fd_t; +typedef const void *set_socket_option_arg_type; #endif class Socket; class TCPSocket; @@ -138,6 +140,8 @@ class Socket : public IOObject { virtual size_t Send(const void *buf, const size_t num_bytes); + static int CloseSocket(NativeSocket sockfd); + static Status GetLastError(); static void SetLastError(Status &error); static NativeSocket CreateSocket(const int domain, const int type, const int protocol, diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp index 1a506aa95b2465..62473a79e290e8 100644 --- a/lldb/source/Host/common/Socket.cpp +++ b/lldb/source/Host/common/Socket.cpp @@ -386,11 +386,7 @@ Status Socket::Close() { LLDB_LOGF(log, "%p Socket::Close (fd = %" PRIu64 ")", static_cast<void *>(this), static_cast<uint64_t>(m_socket)); -#if defined(_WIN32) - bool success = closesocket(m_socket) == 0; -#else - bool success = ::close(m_socket) == 0; -#endif + bool success = CloseSocket(m_socket) == 0; // A reference to a FD was passed in, set it to an invalid value m_socket = kInvalidSocketValue; if (!success) { @@ -427,6 +423,24 @@ void Socket::SetLastError(Status &error) { #endif } +Status Socket::GetLastError() { + std::error_code EC; +#ifdef _WIN32 + EC = llvm::mapWindowsError(WSAGetLastError()); +#else + EC = std::error_code(errno, std::generic_category()); +#endif + return EC; +} + +int Socket::CloseSocket(NativeSocket sockfd) { +#ifdef _WIN32 + return ::closesocket(sockfd); +#else + return ::close(sockfd); +#endif +} + NativeSocket Socket::CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit, Status &error) { diff --git a/lldb/source/Host/common/TCPSocket.cpp b/lldb/source/Host/common/TCPSocket.cpp index fc005814308d90..cba511b9f7f966 100644 --- a/lldb/source/Host/common/TCPSocket.cpp +++ b/lldb/source/Host/common/TCPSocket.cpp @@ -33,28 +33,9 @@ #include <winsock2.h> #endif -#ifdef _WIN32 -#define CLOSE_SOCKET closesocket -typedef const char *set_socket_option_arg_type; -#else -#include <unistd.h> -#define CLOSE_SOCKET ::close -typedef const void *set_socket_option_arg_type; -#endif - using namespace lldb; using namespace lldb_private; -static Status GetLastSocketError() { - std::error_code EC; -#ifdef _WIN32 - EC = llvm::mapWindowsError(WSAGetLastError()); -#else - EC = std::error_code(errno, std::generic_category()); -#endif - return EC; -} - static const int kType = SOCK_STREAM; TCPSocket::TCPSocket(bool should_close, bool child_processes_inherit) @@ -213,7 +194,7 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) { reinterpret_cast<set_socket_option_arg_type>(&option_value); if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p, sizeof(option_value)) == -1) { - CLOSE_SOCKET(fd); + CloseSocket(fd); continue; } @@ -229,8 +210,8 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) { err = ::listen(fd, backlog); if (err == -1) { - error = GetLastSocketError(); - CLOSE_SOCKET(fd); + error = GetLastError(); + CloseSocket(fd); continue; } @@ -251,7 +232,7 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) { void TCPSocket::CloseListenSockets() { for (auto socket : m_listen_sockets) - CLOSE_SOCKET(socket.first); + CloseSocket(socket.first); m_listen_sockets.clear(); } @@ -280,7 +261,7 @@ llvm::Expected<std::vector<MainLoopBase::ReadHandleUP>> TCPSocket::Accept( const lldb_private::SocketAddress &AddrIn = m_listen_sockets[fd]; if (!AddrIn.IsAnyAddr() && AcceptAddr != AddrIn) { - CLOSE_SOCKET(sock); + CloseSocket(sock); LLDB_LOG(log, "rejecting incoming connection from {0} (expecting {1})", AcceptAddr.GetIPAddress(), AddrIn.GetIPAddress()); return; >From 492c5fe2d78fc13f57fc10b9780e3e0e63250046 Mon Sep 17 00:00:00 2001 From: Dmitry Vasilyev <dvassil...@accesssoftek.com> Date: Wed, 4 Sep 2024 13:17:31 +0400 Subject: [PATCH 2/2] Made Socket::SetOption() static. Fixed a typo in Socket::SetOption(). --- lldb/include/lldb/Host/Socket.h | 15 +++++++++++---- lldb/source/Host/common/Socket.cpp | 12 +++++++----- lldb/source/Host/common/TCPSocket.cpp | 6 +----- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lldb/include/lldb/Host/Socket.h b/lldb/include/lldb/Host/Socket.h index 141566c00497b8..764a048976eb41 100644 --- a/lldb/include/lldb/Host/Socket.h +++ b/lldb/include/lldb/Host/Socket.h @@ -34,11 +34,9 @@ namespace lldb_private { #if defined(_WIN32) typedef SOCKET NativeSocket; typedef lldb::pipe_t shared_fd_t; -typedef const char *set_socket_option_arg_type; #else typedef int NativeSocket; typedef NativeSocket shared_fd_t; -typedef const void *set_socket_option_arg_type; #endif class Socket; class TCPSocket; @@ -114,8 +112,17 @@ class Socket : public IOObject { static llvm::Expected<std::unique_ptr<UDPSocket>> UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit); - int GetOption(int level, int option_name, int &option_value); - int SetOption(int level, int option_name, int option_value); + static int GetOption(NativeSocket sockfd, int level, int option_name, + int &option_value); + int GetOption(int level, int option_name, int &option_value) { + return GetOption(m_socket, level, option_name, option_value); + }; + + static int SetOption(NativeSocket sockfd, int level, int option_name, + int option_value); + int SetOption(int level, int option_name, int option_value) { + return SetOption(m_socket, level, option_name, option_value); + }; NativeSocket GetNativeSocket() const { return m_socket; } SocketProtocol GetSocketProtocol() const { return m_protocol; } diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp index 62473a79e290e8..1a63571b94c6f1 100644 --- a/lldb/source/Host/common/Socket.cpp +++ b/lldb/source/Host/common/Socket.cpp @@ -396,18 +396,20 @@ Status Socket::Close() { return error; } -int Socket::GetOption(int level, int option_name, int &option_value) { +int Socket::GetOption(NativeSocket sockfd, int level, int option_name, + int &option_value) { get_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); socklen_t option_value_size = sizeof(int); - return ::getsockopt(m_socket, level, option_name, option_value_p, + return ::getsockopt(sockfd, level, option_name, option_value_p, &option_value_size); } -int Socket::SetOption(int level, int option_name, int option_value) { +int Socket::SetOption(NativeSocket sockfd, int level, int option_name, + int option_value) { set_socket_option_arg_type option_value_p = - reinterpret_cast<get_socket_option_arg_type>(&option_value); - return ::setsockopt(m_socket, level, option_name, option_value_p, + reinterpret_cast<set_socket_option_arg_type>(&option_value); + return ::setsockopt(sockfd, level, option_name, option_value_p, sizeof(option_value)); } diff --git a/lldb/source/Host/common/TCPSocket.cpp b/lldb/source/Host/common/TCPSocket.cpp index cba511b9f7f966..4f1518ef697ff0 100644 --- a/lldb/source/Host/common/TCPSocket.cpp +++ b/lldb/source/Host/common/TCPSocket.cpp @@ -189,11 +189,7 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) { continue; // enable local address reuse - int option_value = 1; - set_socket_option_arg_type option_value_p = - reinterpret_cast<set_socket_option_arg_type>(&option_value); - if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p, - sizeof(option_value)) == -1) { + if (SetOption(fd, SOL_SOCKET, SO_REUSEADDR, 1) == -1) { CloseSocket(fd); continue; } _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits