system/EventPipe: use AtScopeExit() in PoorSocketPair()

This commit is contained in:
Max Kellermann 2017-08-11 08:30:37 +02:00
parent 424e1363ed
commit 699f41e114
1 changed files with 8 additions and 14 deletions

View File

@ -21,6 +21,7 @@
#include "EventPipe.hxx" #include "EventPipe.hxx"
#include "system/fd_util.h" #include "system/fd_util.h"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
#include "util/ScopeExit.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <assert.h> #include <assert.h>
@ -107,6 +108,10 @@ static bool PoorSocketPair(int fd[2])
if (listen_socket == INVALID_SOCKET) if (listen_socket == INVALID_SOCKET)
return false; return false;
AtScopeExit(listen_socket) {
closesocket(listen_socket);
};
sockaddr_in address; sockaddr_in address;
std::memset(&address, 0, sizeof(address)); std::memset(&address, 0, sizeof(address));
address.sin_family = AF_INET; address.sin_family = AF_INET;
@ -116,31 +121,24 @@ static bool PoorSocketPair(int fd[2])
reinterpret_cast<sockaddr*>(&address), reinterpret_cast<sockaddr*>(&address),
sizeof(address)); sizeof(address));
if (ret < 0) { if (ret < 0)
SafeCloseSocket(listen_socket);
return false; return false;
}
ret = listen(listen_socket, 1); ret = listen(listen_socket, 1);
if (ret < 0) { if (ret < 0)
SafeCloseSocket(listen_socket);
return false; return false;
}
int address_len = sizeof(address); int address_len = sizeof(address);
ret = getsockname(listen_socket, ret = getsockname(listen_socket,
reinterpret_cast<sockaddr*>(&address), reinterpret_cast<sockaddr*>(&address),
&address_len); &address_len);
if (ret < 0) { if (ret < 0)
SafeCloseSocket(listen_socket);
return false; return false;
}
SOCKET socket0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKET socket0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket0 == INVALID_SOCKET) { if (socket0 == INVALID_SOCKET) {
SafeCloseSocket(listen_socket);
return false; return false;
} }
@ -149,20 +147,16 @@ static bool PoorSocketPair(int fd[2])
sizeof(address)); sizeof(address));
if (ret < 0) { if (ret < 0) {
SafeCloseSocket(listen_socket);
SafeCloseSocket(socket0); SafeCloseSocket(socket0);
return false; return false;
} }
SOCKET socket1 = accept(listen_socket, nullptr, nullptr); SOCKET socket1 = accept(listen_socket, nullptr, nullptr);
if (socket1 == INVALID_SOCKET) { if (socket1 == INVALID_SOCKET) {
SafeCloseSocket(listen_socket);
SafeCloseSocket(socket0); SafeCloseSocket(socket0);
return false; return false;
} }
SafeCloseSocket(listen_socket);
u_long non_block = 1; u_long non_block = 1;
if (ioctlsocket(socket0, FIONBIO, &non_block) < 0 if (ioctlsocket(socket0, FIONBIO, &non_block) < 0
|| ioctlsocket(socket1, FIONBIO, &non_block) < 0) { || ioctlsocket(socket1, FIONBIO, &non_block) < 0) {