From 4e1588c270ccc94e91f1517d69b60b3b39cc0829 Mon Sep 17 00:00:00 2001 From: Oleksiy Vyalov Date: Fri, 10 Apr 2015 02:31:37 +0000 Subject: [PATCH] Wrap socket error handling with SetLastError and IsInterrupted internal functions which can properly treat Windows and POSIX errors. http://reviews.llvm.org/D8939 llvm-svn: 234559 --- lldb/source/Host/common/Socket.cpp | 70 +++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp index ff1ca7d..2059fec 100644 --- a/lldb/source/Host/common/Socket.cpp +++ b/lldb/source/Host/common/Socket.cpp @@ -80,6 +80,25 @@ NativeSocket Accept(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrl return ::accept (sockfd, addr, addrlen); #endif } + +void SetLastError(Error &error) +{ +#if defined(_WIN32) + error.SetError(::WSAGetLastError(), lldb::eErrorTypeWin32); +#else + error.SetErrorToErrno(); +#endif +} + +bool IsInterrupted() +{ +#if defined(_WIN32) + return ::WSAGetLastError() == WSAEINTR; +#else + return errno == EINTR; +#endif +} + } Socket::Socket(NativeSocket socket, SocketProtocol protocol, bool should_close) @@ -116,8 +135,7 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh sock = CreateSocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, child_processes_inherit); if (sock == kInvalidSocketValue) { - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -143,9 +161,8 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr); if (inet_pton_result <= 0) { - // TODO: On Windows, use WSAGetLastError() if (inet_pton_result == -1) - error.SetErrorToErrno(); + SetLastError(error); else error.SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str()); @@ -155,8 +172,7 @@ Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (-1 == ::connect (sock, (const struct sockaddr *)&sa, sizeof(sa))) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -184,7 +200,7 @@ Error Socket::TcpListen( listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit); if (listen_sock == kInvalidSocketValue) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -209,16 +225,14 @@ Error Socket::TcpListen( int err = ::bind (listen_sock, anyaddr, anyaddr.GetLength()); if (err == -1) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); return error; } err = ::listen (listen_sock, backlog); if (err == -1) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -289,8 +303,7 @@ Error Socket::BlockingAccept(llvm::StringRef host_and_port, bool child_processes if (sock == kInvalidSocketValue) { - // TODO: On Windows, use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); break; } @@ -354,8 +367,7 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (final_recv_fd == kInvalidSocketValue) { // Socket creation failed... - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); } else { @@ -368,8 +380,7 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (::bind (final_recv_fd, addr, addr.GetLength()) == -1) { // Bind failed... - // TODO: On Windows use WSAGetLastError() - error.SetErrorToErrno(); + SetLastError (error); } } @@ -420,8 +431,7 @@ Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inh if (final_send_fd == kInvalidSocketValue) { - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -442,7 +452,7 @@ Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inher int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit); if (fd == kInvalidSocketValue) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -457,7 +467,7 @@ Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inher if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -481,7 +491,7 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri listen_fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit); if (listen_fd == kInvalidSocketValue) { - error.SetErrorToErrno(); + SetLastError (error); return error; } @@ -511,7 +521,7 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri if (!success) { - error.SetErrorToErrno(); + SetLastError (error); return error; } // We are done with the listen port @@ -585,12 +595,11 @@ Error Socket::Read (void *buf, size_t &num_bytes) do { bytes_received = ::recv (m_socket, static_cast(buf), num_bytes, 0); - // TODO: Use WSAGetLastError on windows. - } while (bytes_received < 0 && errno == EINTR); + } while (bytes_received < 0 && IsInterrupted ()); if (bytes_received < 0) { - error.SetErrorToErrno(); + SetLastError (error); num_bytes = 0; } else @@ -628,13 +637,11 @@ Error Socket::Write (const void *buf, size_t &num_bytes) } else bytes_sent = ::send (m_socket, static_cast(buf), num_bytes, 0); - // TODO: Use WSAGetLastError on windows. - } while (bytes_sent < 0 && errno == EINTR); + } while (bytes_sent < 0 && IsInterrupted ()); if (bytes_sent < 0) { - // TODO: On Windows, use WSAGEtLastError. - error.SetErrorToErrno(); + SetLastError (error); num_bytes = 0; } else @@ -680,8 +687,7 @@ Error Socket::Close() m_socket = kInvalidSocketValue; if (!success) { - // TODO: On Windows, use WSAGetLastError(). - error.SetErrorToErrno(); + SetLastError (error); } return error; -- 2.7.4