Index: runtime/bin/socket_linux.cc |
diff --git a/runtime/bin/socket_linux.cc b/runtime/bin/socket_linux.cc |
index 295072836b7d7662bda38ec18450d33425100479..e3d3103eebeb52606d01940d5136e971e0b1feb9 100644 |
--- a/runtime/bin/socket_linux.cc |
+++ b/runtime/bin/socket_linux.cc |
@@ -28,6 +28,13 @@ |
namespace dart { |
namespace bin { |
+static void SaveErrorAndClose(intptr_t fd) { |
+ int err = errno; |
+ VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ errno = err; |
+} |
+ |
+ |
SocketAddress::SocketAddress(struct sockaddr* sa) { |
ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, |
@@ -69,7 +76,7 @@ static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
if ((result == 0) || (errno == EINPROGRESS)) { |
return fd; |
} |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
@@ -93,7 +100,7 @@ intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
intptr_t result = TEMP_FAILURE_RETRY( |
bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
if ((result != 0) && (errno != EINPROGRESS)) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
@@ -322,7 +329,7 @@ intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
if (NO_RETRY_EXPECTED( |
bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
return fd; |
@@ -408,7 +415,7 @@ intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
if (NO_RETRY_EXPECTED( |
bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
@@ -418,14 +425,12 @@ intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
// Don't close the socket until we have created a new socket, ensuring |
// that we do not get the bad port number again. |
intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
- int err = errno; |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
- errno = err; |
+ SaveErrorAndClose(fd); |
return new_fd; |
} |
if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
@@ -463,8 +468,14 @@ intptr_t ServerSocket::Accept(intptr_t fd) { |
socket = kTemporaryFailure; |
} |
} else { |
- FDUtils::SetCloseOnExec(socket); |
- FDUtils::SetNonBlocking(socket); |
+ if (!FDUtils::SetCloseOnExec(socket)) { |
+ SaveErrorAndClose(socket); |
+ return -1; |
+ } |
+ if (!FDUtils::SetNonBlocking(socket)) { |
+ SaveErrorAndClose(socket); |
+ return -1; |
+ } |
} |
return socket; |
} |