Skip to content

Commit

Permalink
print errno name (#3138)
Browse files Browse the repository at this point in the history
b/321999529

Change-Id: Ie46505ea50979446b1ef128d34b6487a304e2d4b
(cherry picked from commit 48244d4)
  • Loading branch information
haozheng-cobalt authored and anonymous1-me committed May 31, 2024
1 parent 197ef16 commit 709eaa9
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 1 deletion.
28 changes: 28 additions & 0 deletions starboard/nplb/posix_compliance/posix_socket_send_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "starboard/nplb/posix_compliance/posix_socket_helpers.h"
#include "starboard/thread.h"
Expand Down Expand Up @@ -76,8 +77,14 @@ TEST(PosixSocketSendTest, RainyDayUnconnectedSocket) {
ssize_t bytes_written = send(socket_fd, buf, sizeof(buf), kSendFlags);
EXPECT_FALSE(bytes_written >= 0);

<<<<<<< HEAD
// TODO: check errno: EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET ||
// errno == EPIPE);
=======
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))

EXPECT_TRUE(close(socket_fd) == 0);
}
Expand Down Expand Up @@ -109,8 +116,17 @@ TEST(PosixSocketSendTest, RainyDaySendToClosedSocket) {
void* thread_result;
EXPECT_TRUE(pthread_join(send_thread, &thread_result) == 0);

<<<<<<< HEAD
// TODO: errno: EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno
// == EPIPE);
=======
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN || // errno on Windows
errno == EINPROGRESS || // errno on Evergreen
errno == ENETUNREACH // errno on raspi
);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))

// Clean up the server socket.
EXPECT_TRUE(close(server_socket_fd) == 0);
Expand Down Expand Up @@ -143,9 +159,15 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketUntilBlocking) {

if (result < 0) {
// If we didn't get a socket, it should be pending.
<<<<<<< HEAD
// TODO: export errno
// EXPECT_TRUE(errno == EINPROGRESS || errno == EAGAIN || errno ==
// EWOULDBLOCK);
=======
EXPECT_TRUE(errno == EINPROGRESS || errno == EAGAIN ||
errno == EWOULDBLOCK);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))
break;
}

Expand Down Expand Up @@ -191,10 +213,16 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketConnectionReset) {
result = send(client_socket_fd, buff, sizeof(buff), kSendFlags);

if (result < 0) {
<<<<<<< HEAD
// TODO: errno:
// EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno ==
// EPIPE);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
=======
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ECONNABORTED);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))
break;
}

Expand Down
28 changes: 28 additions & 0 deletions starboard/nplb/posix_compliance/posix_socket_sendto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

#include "starboard/nplb/posix_compliance/posix_socket_helpers.h"
Expand Down Expand Up @@ -79,8 +80,14 @@ TEST(PosixSocketSendtoTest, RainyDayUnconnectedSocket) {
sendto(socket_fd, buf, sizeof(buf), kSendFlags, NULL, 0);
EXPECT_FALSE(bytes_written >= 0);

<<<<<<< HEAD
// TODO: check errno: EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET ||
// errno == EPIPE);
=======
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))

EXPECT_TRUE(close(socket_fd) == 0);
}
Expand Down Expand Up @@ -112,8 +119,17 @@ TEST(PosixSocketSendtoTest, RainyDaySendToClosedSocket) {
void* thread_result;
EXPECT_TRUE(pthread_join(send_thread, &thread_result) == 0);

<<<<<<< HEAD
// TODO: errno: EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno
// == EPIPE);
=======
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ENOTCONN || // errno on Windows
errno == EINPROGRESS || // errno on Evergreen
errno == ENETUNREACH // errno on raspi
);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))

// Clean up the server socket.
EXPECT_TRUE(close(server_socket_fd) == 0);
Expand Down Expand Up @@ -146,9 +162,15 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketUntilBlocking) {

if (result < 0) {
// If we didn't get a socket, it should be pending.
<<<<<<< HEAD
// TODO: export errno
// EXPECT_TRUE(errno == EINPROGRESS || errno == EAGAIN || errno ==
// EWOULDBLOCK);
=======
EXPECT_TRUE(errno == EINPROGRESS || errno == EAGAIN ||
errno == EWOULDBLOCK);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))
break;
}

Expand Down Expand Up @@ -194,10 +216,16 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketConnectionReset) {
result = sendto(client_socket_fd, buff, sizeof(buff), kSendFlags, NULL, 0);

if (result < 0) {
<<<<<<< HEAD
// TODO: errno:
// EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno ==
// EPIPE);
SB_DLOG(INFO) << "Failed to send, errno = " << errno;
=======
EXPECT_TRUE(errno == ECONNRESET || errno == ENETRESET || errno == EPIPE ||
errno == ECONNABORTED);
SB_DLOG(INFO) << "Failed to send, errno = " << strerror(errno);
>>>>>>> 48244d436bb (print errno name (#3138))
break;
}

Expand Down
140 changes: 139 additions & 1 deletion starboard/shared/win32/posix_emu/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// We specifically do not include <sys/socket.h> since the define causes a loop

#include <fcntl.h>
#include <io.h> // Needed for file-specific `_close`.
#include <io.h> // Needed for file-specific `_close`.
#include <string.h>
#include <unistd.h> // Our version that declares generic `close`.
#include <winsock2.h>
#undef NO_ERROR // http://b/302733082#comment15
Expand Down Expand Up @@ -91,6 +92,143 @@ static FileOrSocket handle_db_get(int fd, bool erase) {
return handle;
}

<<<<<<< HEAD
=======
// WSAGetLastError should be called immediately to retrieve the extended error
// code for the failing function call.
// https://learn.microsoft.com/en-us/windows/win32/winsock/error-codes-errno-h-errno-and-wsagetlasterror-2
static void set_errno() {
int winsockError = WSAGetLastError();
int sockError = 0;

// The error codes returned by Windows Sockets are similar to UNIX socket
// error code constants, but the constants are all prefixed with WSA. So in
// Winsock applications the WSAEWOULDBLOCK error code would be returned, while
// in UNIX applications the EWOULDBLOCK error code would be returned. The
// errno values in a WIN32 are a subset of the values for errno in UNIX
// systems.
switch (winsockError) {
case WSAEINTR: // Interrupted function call
sockError = EINTR;
break;
case WSAEBADF: // WSAEBADF
sockError = EBADF;
break;
case WSAEACCES: // WSAEACCES
sockError = EACCES;
break;
case WSAEFAULT: // Bad address
sockError = EFAULT;
break;
case WSAEINVAL: // Invalid argument
sockError = EINVAL;
break;
case WSAEMFILE: // Too many open files
sockError = EMFILE;
break;
case WSAEWOULDBLOCK: // Operation would block
sockError = EWOULDBLOCK;
break;
case WSAEINPROGRESS: // Operation now in progress
sockError = EINPROGRESS;
break;
case WSAEALREADY: // Operation already in progress
sockError = EALREADY;
break;
case WSAENOTSOCK: // Socket operation on non-socket
sockError = ENOTSOCK;
break;
case WSAEDESTADDRREQ: // Destination address required
sockError = EDESTADDRREQ;
break;
case WSAEMSGSIZE: // Message too long
sockError = EMSGSIZE;
break;
case WSAEPROTOTYPE: // Protocol wrong type for socket
sockError = EPROTOTYPE;
break;
case WSAENOPROTOOPT: // Bad protocol option
sockError = ENOPROTOOPT;
break;
case WSAEPROTONOSUPPORT: // Protocol not supported
sockError = EPROTONOSUPPORT;
break;
case WSAEOPNOTSUPP: // Operation not supported
sockError = EOPNOTSUPP;
break;
case WSAEAFNOSUPPORT: // Address family not supported by protocol family
sockError = EAFNOSUPPORT;
break;
case WSAEADDRINUSE: // Address already in use
sockError = EADDRINUSE;
break;
case WSAEADDRNOTAVAIL: // Cannot assign requested address
sockError = EADDRNOTAVAIL;
break;
case WSAENETDOWN: // Network is down
sockError = ENETDOWN;
break;
case WSAENETUNREACH: // Network is unreachable
sockError = ENETUNREACH;
break;
case WSAENETRESET: // Network dropped connection on reset
sockError = ENETRESET;
break;
case WSAECONNABORTED: // Software caused connection abort
sockError = ECONNABORTED;
break;
case WSAECONNRESET: // Connection reset by peer
sockError = ECONNRESET;
break;
case WSAENOBUFS: // No buffer space available
sockError = ENOBUFS;
break;
case WSAEISCONN: // Socket is already connected
sockError = EISCONN;
break;
case WSAENOTCONN: // Socket is not connected
sockError = ENOTCONN;
break;
case WSAETIMEDOUT: // Connection timed out
sockError = ETIMEDOUT;
break;
case WSAECONNREFUSED: // Connection refused
sockError = ECONNREFUSED;
break;
case WSAELOOP: // WSAELOOP
sockError = ELOOP;
break;
case WSAENAMETOOLONG: // WSAENAMETOOLONG
sockError = ENAMETOOLONG;
break;
case WSAEHOSTUNREACH: // No route to host
sockError = EHOSTUNREACH;
break;
case WSAENOTEMPTY: // WSAENOTEMPTY
sockError = ENOTEMPTY;
break;
case WSAHOST_NOT_FOUND: // Host not found
sockError = HOST_NOT_FOUND;
break;
case WSATRY_AGAIN: // Non-authoritative host not found
sockError = TRY_AGAIN;
break;
case WSANO_RECOVERY: // This is a non-recoverable error
sockError = NO_RECOVERY;
break;
case WSANO_DATA: // Valid name, no data record of requested type
sockError = NO_DATA;
break;
default:
SB_DLOG(WARNING) << "Unknown socket error.";
break;
}

_set_errno(sockError);
SB_DLOG(INFO) << "Encounter socket error: " << strerror(sockError);
}

>>>>>>> 48244d436bb (print errno name (#3138))
///////////////////////////////////////////////////////////////////////////////
// Implementations below exposed externally in pure C for emulation.
///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 709eaa9

Please sign in to comment.