[glib: 10/12] glib/win32: teach reopen_noninherited() about SOCKET




commit a34a4e7844a9e4b1c9fb727826a5bff0b7abe241
Author: Marc-André Lureau <marcandre lureau redhat com>
Date:   Fri Apr 8 16:15:56 2022 +0400

    glib/win32: teach reopen_noninherited() about SOCKET
    
    Signed-off-by: Marc-André Lureau <marcandre lureau redhat com>

 glib/gwin32.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
---
diff --git a/glib/gwin32.c b/glib/gwin32.c
index c0e1d7f3fb..0a34ed98d9 100644
--- a/glib/gwin32.c
+++ b/glib/gwin32.c
@@ -40,6 +40,7 @@
 #include <fcntl.h>
 
 #define STRICT                 /* Strict typing, please */
+#include <winsock2.h>
 #include <windows.h>
 #undef STRICT
 #ifndef G_WITH_CYGWIN
@@ -1501,9 +1502,37 @@ g_win32_reopen_noninherited (int fd,
       return -1;
     }
 
-  if (DuplicateHandle (GetCurrentProcess (), h,
-                       GetCurrentProcess (), &duph,
-                       0, FALSE, DUPLICATE_SAME_ACCESS) == 0)
+  if (g_win32_handle_is_socket (h))
+    {
+      WSAPROTOCOL_INFO info;
+
+      if (WSADuplicateSocket ((SOCKET) h,
+                              GetCurrentProcessId (),
+                              &info))
+        {
+          gchar *emsg = g_win32_error_message (WSAGetLastError ());
+          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                       "WSADuplicateSocket() failed: %s", emsg);
+          g_free (emsg);
+          return -1;
+        }
+
+      duph = (HANDLE) WSASocket (FROM_PROTOCOL_INFO,
+                                 FROM_PROTOCOL_INFO,
+                                 FROM_PROTOCOL_INFO,
+                                 &info, 0, 0);
+      if (duph == (HANDLE) INVALID_SOCKET)
+        {
+          gchar *emsg = g_win32_error_message (WSAGetLastError ());
+          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                       "WSASocket() failed: %s", emsg);
+          g_free (emsg);
+          return -1;
+        }
+    }
+  else if (DuplicateHandle (GetCurrentProcess (), h,
+                            GetCurrentProcess (), &duph,
+                            0, FALSE, DUPLICATE_SAME_ACCESS) == 0)
     {
       char *emsg = g_win32_error_message (GetLastError ());
       g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]