From cff85eaddcb85f887e41c73a53232c39d69904f3 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Fri, 16 Feb 2018 16:23:32 +0100 Subject: [PATCH] Cygwin: sockets: move common settings into set_socket_handle() Move setting address family, socket type and descriptor flags into fhandler_socket::set_socket_handle method. Signed-off-by: Corinna Vinschen --- winsup/cygwin/fhandler.h | 2 +- winsup/cygwin/fhandler_socket.cc | 37 +++++++++++++++++--------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index e1659db5a..99cacd31f 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -557,7 +557,7 @@ class fhandler_socket: public fhandler_base } status; #ifdef __INSIDE_CYGWIN_NET__ - int set_socket_handle (SOCKET sock); + int set_socket_handle (SOCKET sock, int af, int type, int flags); #endif public: diff --git a/winsup/cygwin/fhandler_socket.cc b/winsup/cygwin/fhandler_socket.cc index a30b49511..d7e9171f3 100644 --- a/winsup/cygwin/fhandler_socket.cc +++ b/winsup/cygwin/fhandler_socket.cc @@ -263,9 +263,9 @@ fhandler_socket::open (int flags, mode_t mode) } int -fhandler_socket::set_socket_handle (SOCKET sock) +fhandler_socket::set_socket_handle (SOCKET sock, int af, int type, int flags) { - DWORD flags; + DWORD hdl_flags; bool lsp_fixup = false; /* Usually sockets are inheritable IFS objects. Unfortunately some virus @@ -288,8 +288,8 @@ fhandler_socket::set_socket_handle (SOCKET sock) If that doesn't work for some reason, mark the sockets for duplication via WSADuplicateSocket/WSASocket. This requires to start the child process in SUSPENDED state so we only do this if really necessary. */ - if (!GetHandleInformation ((HANDLE) sock, &flags) - || !(flags & HANDLE_FLAG_INHERIT)) + if (!GetHandleInformation ((HANDLE) sock, &hdl_flags) + || !(hdl_flags & HANDLE_FLAG_INHERIT)) { int ret; SOCKET base_sock; @@ -303,7 +303,7 @@ fhandler_socket::set_socket_handle (SOCKET sock) debug_printf ("WSAIoctl: %u", WSAGetLastError ()); else if (base_sock != sock) { - if (GetHandleInformation ((HANDLE) base_sock, &flags) + if (GetHandleInformation ((HANDLE) base_sock, &hdl_flags) && (flags & HANDLE_FLAG_INHERIT)) { if (!DuplicateHandle (GetCurrentProcess (), (HANDLE) base_sock, @@ -312,19 +312,22 @@ fhandler_socket::set_socket_handle (SOCKET sock) debug_printf ("DuplicateHandle failed, %E"); else { - closesocket (sock); + ::closesocket (sock); sock = base_sock; lsp_fixup = false; } } } } + set_addr_family (af); + set_socket_type (type); + if (flags & SOCK_NONBLOCK) + set_nonblocking (true); + if (flags & SOCK_CLOEXEC) + set_close_on_exec (true); set_io_handle ((HANDLE) sock); if (!init_events ()) - { - closesocket (sock); - return -1; - } + return -1; if (lsp_fixup) init_fixup_before (); set_flags (O_RDWR | O_BINARY); @@ -360,6 +363,7 @@ int fhandler_socket::socket (int af, int type, int protocol, int flags) { SOCKET sock; + int ret; sock = ::socket (af == AF_LOCAL ? AF_INET : af, type, protocol); if (sock == INVALID_SOCKET) @@ -367,13 +371,12 @@ fhandler_socket::socket (int af, int type, int protocol, int flags) set_winsock_errno (); return -1; } - set_addr_family (af); - set_socket_type (type); - if (flags & SOCK_NONBLOCK) - set_nonblocking (true); - if (flags & SOCK_CLOEXEC) - set_close_on_exec (true); - return set_socket_handle (sock); + ret = set_socket_handle (sock, af, type, flags); + if (ret < 0) + ::closesocket (sock); + return ret; +} + } void -- 2.43.5