Windows: child process fixes
authorBert Belder <bertbelder@gmail.com>
Wed, 9 Feb 2011 02:30:03 +0000 (03:30 +0100)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 9 Feb 2011 05:21:39 +0000 (21:21 -0800)
src/node_child_process_win32.cc
src/node_file.cc
src/node_net.cc
src/platform_win32_winsock.cc
src/platform_win32_winsock.h

index 4dba282..2734f25 100644 (file)
@@ -321,8 +321,8 @@ void ChildProcess::close_stdio_handles(ChildProcess *child) {
   // take some time and would deadlock if done in the main thread.
   for (int i = 0; i < 3; i++) {
     if (!child->got_custom_fds_[i]) {
-      wsa_disconnect_ex((SOCKET)child->stdio_handles_[i], NULL, 0, 0);
-      closesocket((SOCKET)child->stdio_handles_[i]);
+      shutdown(reinterpret_cast<SOCKET>(child->stdio_handles_[i]), SD_BOTH);
+      closesocket(reinterpret_cast<SOCKET>(child->stdio_handles_[i]));
     }
   }
 }
@@ -742,6 +742,10 @@ Handle<Value> ChildProcess::Spawn(const Arguments& args) {
           SOCKET_ERROR)
         wsa_perror("ioctlsocket");
 
+      // Make parent handle non-inheritable
+      if (!SetHandleInformation(parent_handle, HANDLE_FLAG_INHERIT, 0))
+        winapi_perror("SetHandleInformation");
+
       // Make child handle inheritable
       if (!SetHandleInformation(child_handle, HANDLE_FLAG_INHERIT,
           HANDLE_FLAG_INHERIT))
@@ -873,4 +877,4 @@ void ChildProcess::Initialize(Handle<Object> target) {
 
 }  // namespace node
 
-NODE_MODULE(node_child_process, node::ChildProcess::Initialize);
\ No newline at end of file
+NODE_MODULE(node_child_process, node::ChildProcess::Initialize);
index e85ccc7..40b8362 100644 (file)
@@ -56,8 +56,8 @@ static inline bool SetCloseOnExec(int fd) {
 #ifdef __POSIX__
   return (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1);
 #else // __MINGW32__
-  /* no-op on windows */
-  return false;
+  return SetHandleInformation(reinterpret_cast<HANDLE>(_get_osfhandle(fd)),
+                              HANDLE_FLAG_INHERIT, 0) != 0;
 #endif
 }
 
index 17f4687..5a2d308 100644 (file)
@@ -92,14 +92,15 @@ static Persistent<FunctionTemplate> recv_msg_template;
   }
 
 
-#ifdef __POSIX__
-
 static inline bool SetCloseOnExec(int fd) {
+#ifdef __POSIX__
   return (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1);
+#else // __MINGW32__
+  return SetHandleInformation(reinterpret_cast<HANDLE>(_get_osfhandle(fd)),
+                              HANDLE_FLAG_INHERIT, 0) != 0;
+#endif
 }
 
-#endif // __POSIX__
-
 
 static inline bool SetNonBlock(int fd) {
 #ifdef __MINGW32__
@@ -115,12 +116,11 @@ static inline bool SetSockFlags(int fd) {
 #ifdef __MINGW32__
   BOOL flags = TRUE;
   setsockopt(_get_osfhandle(fd), SOL_SOCKET, SO_REUSEADDR, (const char *)&flags, sizeof(flags));
-  return SetNonBlock(fd);
 #else // __POSIX__
   int flags = 1;
   setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags));
-  return SetNonBlock(fd) && SetCloseOnExec(fd);
 #endif
+  return SetNonBlock(fd) && SetCloseOnExec(fd);
 }
 
 
index cecb5a4..df193d9 100644 (file)
@@ -130,14 +130,6 @@ void wsa_perror(const char *prefix) {
 \r
 \r
 /*\r
- * Wrapper for DisconnectEx extension function\r
- */\r
-BOOL wsa_disconnect_ex(SOCKET socket, OVERLAPPED *overlapped, DWORD flags, DWORD reserved) {\r
-  return wsexf.DisconnectEx(socket, overlapped, flags, reserved);\r
-}\r
-\r
-\r
-/*\r
  * Retrieves a pointer to a WSAPROTOCOL_INFOW structure\r
  * related to a certain winsock protocol from the cache\r
  */\r
@@ -422,6 +414,7 @@ inline static void wsa_get_extension_function(SOCKET socket, GUID guid, void **t
  * Retrieves the needed winsock extension function pointers for the tcp/ip subsystem,\r
  * storing them in the `wsexf` cache\r
  */\r
+/*\r
 inline static void wsa_init_extension_functions() {\r
   SOCKET dummy = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);\r
 \r
@@ -431,14 +424,15 @@ inline static void wsa_init_extension_functions() {
     return;\r
   }\r
 \r
-  //wsa_get_extension_function(dummy, WSAID_CONNECTEX,            (void**)&wsexf.ConnectEx           );\r
-  //wsa_get_extension_function(dummy, WSAID_ACCEPTEX,             (void**)&wsexf.AcceptEx            );\r
-  //wsa_get_extension_function(dummy, WSAID_GETACCEPTEXSOCKADDRS, (void**)&wsexf.GetAcceptExSockAddrs);\r
+  wsa_get_extension_function(dummy, WSAID_CONNECTEX,            (void**)&wsexf.ConnectEx           );\r
+  wsa_get_extension_function(dummy, WSAID_ACCEPTEX,             (void**)&wsexf.AcceptEx            );\r
+  wsa_get_extension_function(dummy, WSAID_GETACCEPTEXSOCKADDRS, (void**)&wsexf.GetAcceptExSockAddrs);\r
   wsa_get_extension_function(dummy, WSAID_DISCONNECTEX,         (void**)&wsexf.DisconnectEx        );\r
-  //wsa_get_extension_function(dummy, WSAID_TRANSMITFILE,         (void**)&wsexf.TransmitFile        );\r
+  wsa_get_extension_function(dummy, WSAID_TRANSMITFILE,         (void**)&wsexf.TransmitFile        );\r
 \r
   closesocket(dummy);\r
 }\r
+*/\r
 \r
 \r
 /*\r
@@ -451,7 +445,7 @@ void wsa_init() {
   }\r
 \r
   wsa_init_proto_info_cache();\r
-  wsa_init_extension_functions();\r
+  //wsa_init_extension_functions();\r
 }\r
 \r
 \r
index f292682..f78329d 100644 (file)
@@ -15,7 +15,6 @@ void wsa_init();
 void wsa_perror(const char* prefix = "");\r
 \r
 SOCKET wsa_sync_socket(int af, int type, int proto);\r
-BOOL wsa_disconnect_ex(SOCKET socket, OVERLAPPED *overlapped, DWORD flags, DWORD reserved);\r
 \r
 int wsa_socketpair(int af, int type, int proto, SOCKET sock[2]);\r
 int wsa_sync_async_socketpair(int af, int type, int proto, SOCKET *syncSocket, SOCKET *asyncSocket);\r