Merge pull request #18060 from frazenshtein/master

Pass WSA_FLAG_NO_HANDLE_INHERIT flag to the WSASocketA() to avoid handle leaking on the Windows in case of using CreateProcess() on the server
pull/18894/head
Jan Tattermusch 6 years ago committed by GitHub
commit 7820b44c20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc
  2. 4
      src/core/lib/iomgr/endpoint_pair_windows.cc
  3. 1
      src/core/lib/iomgr/iomgr_windows.cc
  4. 19
      src/core/lib/iomgr/socket_windows.cc
  5. 8
      src/core/lib/iomgr/socket_windows.h
  6. 2
      src/core/lib/iomgr/tcp_client_windows.cc
  7. 4
      src/core/lib/iomgr/tcp_server_windows.cc

@ -445,7 +445,8 @@ class SockToPolledFdMap {
*/
static ares_socket_t Socket(int af, int type, int protocol, void* user_data) {
SockToPolledFdMap* map = static_cast<SockToPolledFdMap*>(user_data);
SOCKET s = WSASocket(af, type, protocol, nullptr, 0, WSA_FLAG_OVERLAPPED);
SOCKET s = WSASocket(af, type, protocol, nullptr, 0,
grpc_get_default_wsa_socket_flags());
if (s == INVALID_SOCKET) {
return s;
}

@ -41,7 +41,7 @@ static void create_sockets(SOCKET sv[2]) {
int addr_len = sizeof(addr);
lst_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
WSA_FLAG_OVERLAPPED);
grpc_get_default_wsa_socket_flags());
GPR_ASSERT(lst_sock != INVALID_SOCKET);
memset(&addr, 0, sizeof(addr));
@ -54,7 +54,7 @@ static void create_sockets(SOCKET sv[2]) {
SOCKET_ERROR);
cli_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
WSA_FLAG_OVERLAPPED);
grpc_get_default_wsa_socket_flags());
GPR_ASSERT(cli_sock != INVALID_SOCKET);
GPR_ASSERT(WSAConnect(cli_sock, (grpc_sockaddr*)&addr, addr_len, NULL, NULL,

@ -61,6 +61,7 @@ static void iomgr_platform_init(void) {
winsock_init();
grpc_iocp_init();
grpc_pollset_global_init();
grpc_wsa_socket_flags_init();
}
static void iomgr_platform_flush(void) { grpc_iocp_flush(); }

@ -39,6 +39,8 @@
#include "src/core/lib/iomgr/sockaddr_windows.h"
#include "src/core/lib/iomgr/socket_windows.h"
static DWORD s_wsa_socket_flags;
grpc_winsocket* grpc_winsocket_create(SOCKET socket, const char* name) {
char* final_name;
grpc_winsocket* r = (grpc_winsocket*)gpr_malloc(sizeof(grpc_winsocket));
@ -181,4 +183,21 @@ int grpc_ipv6_loopback_available(void) {
return g_ipv6_loopback_available;
}
DWORD grpc_get_default_wsa_socket_flags() { return s_wsa_socket_flags; }
void grpc_wsa_socket_flags_init() {
s_wsa_socket_flags = WSA_FLAG_OVERLAPPED;
/* WSA_FLAG_NO_HANDLE_INHERIT may be not supported on the older Windows
versions, see
https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
for details. */
SOCKET sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
s_wsa_socket_flags | WSA_FLAG_NO_HANDLE_INHERIT);
if (sock != INVALID_SOCKET) {
/* Windows 7, Windows 2008 R2 with SP1 or later */
s_wsa_socket_flags |= WSA_FLAG_NO_HANDLE_INHERIT;
closesocket(sock);
}
}
#endif /* GRPC_WINSOCK_SOCKET */

@ -32,6 +32,10 @@
#include "src/core/lib/iomgr/closure.h"
#include "src/core/lib/iomgr/iomgr_internal.h"
#ifndef WSA_FLAG_NO_HANDLE_INHERIT
#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
#endif
/* This holds the data for an outstanding read or write on a socket.
The mutex to protect the concurrent access to that data is the one
inside the winsocket wrapper. */
@ -114,6 +118,10 @@ void grpc_socket_become_ready(grpc_winsocket* winsocket,
The value is probed once, and cached for the life of the process. */
int grpc_ipv6_loopback_available(void);
void grpc_wsa_socket_flags_init();
DWORD grpc_get_default_wsa_socket_flags();
#endif
#endif /* GRPC_CORE_LIB_IOMGR_SOCKET_WINDOWS_H */

@ -148,7 +148,7 @@ static void tcp_connect(grpc_closure* on_done, grpc_endpoint** endpoint,
}
sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
WSA_FLAG_OVERLAPPED);
grpc_get_default_wsa_socket_flags());
if (sock == INVALID_SOCKET) {
error = GRPC_WSA_ERROR(WSAGetLastError(), "WSASocket");
goto failure;

@ -255,7 +255,7 @@ static grpc_error* start_accept_locked(grpc_tcp_listener* port) {
}
sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
WSA_FLAG_OVERLAPPED);
grpc_get_default_wsa_socket_flags());
if (sock == INVALID_SOCKET) {
error = GRPC_WSA_ERROR(WSAGetLastError(), "WSASocket");
goto failure;
@ -493,7 +493,7 @@ static grpc_error* tcp_server_add_port(grpc_tcp_server* s,
}
sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
WSA_FLAG_OVERLAPPED);
grpc_get_default_wsa_socket_flags());
if (sock == INVALID_SOCKET) {
error = GRPC_WSA_ERROR(WSAGetLastError(), "WSASocket");
goto done;

Loading…
Cancel
Save