Check unix domain socket functionality
[catagits/fcgi2.git] / libfcgi / os_win32.c
index 7f5add8..cb3550f 100755 (executable)
@@ -1,23 +1,19 @@
 /*
  * os_win32.c --
  *
- *
- *  Copyright (c) 1995 Open Market, Inc.
- *  All rights reserved.
- *
- *  This file contains proprietary and confidential information and
- *  remains the unpublished property of Open Market, Inc. Use,
- *  disclosure, or reproduction is prohibited except as permitted by
- *  express written license agreement with Open Market, Inc.
- *
  *  Bill Snapper
  *  snapper@openmarket.com
  *
  * (Special thanks to Karen and Bill.  They made my job much easier and
  *  significantly more enjoyable.)
+ *
+ * Copyright (c) 1996 Open Market, Inc.
+ *
+ * See the file "LICENSE" for information on usage and redistribution
+ * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  */
 #ifndef lint
-static const char rcsid[] = "$Id: os_win32.c,v 1.32 2002/03/04 22:16:38 robs Exp $";
+static const char rcsid[] = "$Id: os_win32.c,v 1.35 2004/01/31 17:47:07 robs Exp $";
 #endif /* not lint */
 
 #define WIN32_LEAN_AND_MEAN 
@@ -28,6 +24,7 @@ static const char rcsid[] = "$Id: os_win32.c,v 1.32 2002/03/04 22:16:38 robs Exp
 #include <stdio.h>
 #include <sys/timeb.h>
 #include <process.h>
+#include <signal.h>
 
 #define DLLAPI  __declspec(dllexport)
 
@@ -272,12 +269,14 @@ void OS_ShutdownPending(void)
 static void ShutdownRequestThread(void * arg)
 {
     HANDLE shutdownEvent = (HANDLE) arg;
-    DWORD rv; 
     
     WaitForSingleObject(shutdownEvent, INFINITE);
 
     shutdownPending = TRUE;
 
+    // emulate the unix behaviour
+    raise(SIGTERM);
+
     if (listenType == FD_PIPE_SYNC)
     {
         // Its a hassle to get ConnectNamedPipe to return early,
@@ -1353,7 +1352,7 @@ int OS_AsyncWrite(int fd, int offset, void *buf, int len,
  *
  *--------------------------------------------------------------
  */
-int OS_Close(int fd)
+int OS_Close(int fd, int shutdown_ok)
 {
     int ret = 0;
 
@@ -1373,14 +1372,44 @@ int OS_Close(int fd)
 
     case FD_SOCKET_SYNC:
        case FD_SOCKET_ASYNC:
-           /*
-            * Closing a socket that has an async read outstanding causes a
-            * tcp reset and possible data loss.  The shutdown call seems to
-            * prevent this.
-            */
-           shutdown(fdTable[fd].fid.sock, SD_SEND);
-           if (closesocket(fdTable[fd].fid.sock) == SOCKET_ERROR) ret = -1;
-           break;
+
+        /*
+         * shutdown() the send side and then read() from client until EOF
+         * or a timeout expires.  This is done to minimize the potential
+         * that a TCP RST will be sent by our TCP stack in response to 
+         * receipt of additional data from the client.  The RST would
+         * cause the client to discard potentially useful response data.
+         */
+
+        if (shutdown_ok)
+        {
+            if (shutdown(fdTable[fd].fid.sock, SD_SEND) == 0)
+            {
+                struct timeval tv;
+                fd_set rfds;
+                int sock = fdTable[fd].fid.sock;
+                int rv;
+                char trash[1024];
+   
+                FD_ZERO(&rfds);
+
+                do 
+                {
+#pragma warning( disable : 4127 ) 
+                   FD_SET((unsigned) sock, &rfds);
+#pragma warning( default : 4127 )
+                    
+                   tv.tv_sec = 2;
+                   tv.tv_usec = 0;
+                   rv = select(sock + 1, &rfds, NULL, NULL, &tv);
+                }
+                while (rv > 0 && recv(sock, trash, sizeof(trash), 0) > 0);
+            }
+        }
+        
+        closesocket(fdTable[fd].fid.sock);
+
+        break;
 
        default:
 
@@ -1768,7 +1797,7 @@ int OS_Accept(int listen_sock, int fail_on_intr, const char *webServerAddrs)
  *
  *----------------------------------------------------------------------
  */
-int OS_IpcClose(int ipcFd)
+int OS_IpcClose(int ipcFd, int shutdown)
 {
     if (ipcFd == -1) return 0;
 
@@ -1793,7 +1822,7 @@ int OS_IpcClose(int ipcFd)
 
     case FD_SOCKET_SYNC:
 
-           OS_Close(ipcFd);
+           OS_Close(ipcFd, shutdown);
            break;
 
     case FD_UNUSED: