fix copyright information in these files
[catagits/fcgi2.git] / libfcgi / os_unix.c
index 2ab9b82..41b717a 100755 (executable)
@@ -1,23 +1,17 @@
 /*
  * os_unix.c --
  *
- *      Description of file.
- *
- *
- *  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
+ *
+ * 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_unix.c,v 1.31 2001/09/06 20:07:53 robs Exp $";
+static const char rcsid[] = "$Id: os_unix.c,v 1.38 2003/06/22 00:16:43 robs Exp $";
 #endif /* not lint */
 
 #include "fcgi_config.h"
@@ -103,12 +97,6 @@ static int volatile maxFd = -1;
 static int shutdownPending = FALSE;
 static int shutdownNow = FALSE;
 
-void OS_Shutdown()
-{
-    shutdownNow = TRUE;
-    OS_ShutdownPending();
-}
-
 void OS_ShutdownPending()
 {
     shutdownPending = TRUE;
@@ -187,7 +175,7 @@ int OS_LibInit(int stdioFds[3])
     FD_ZERO(&readFdSetPost);
     FD_ZERO(&writeFdSetPost);
 
-    OS_InstallSignalHandlers(FALSE);
+    OS_InstallSignalHandlers(TRUE);
 
     libInitialized = TRUE;
 
@@ -211,8 +199,6 @@ int OS_LibInit(int stdioFds[3])
  */
 void OS_LibShutdown()
 {
-    OS_Shutdown();
-
     if(!libInitialized)
         return;
 
@@ -293,11 +279,11 @@ union SockAddrUnion {
 int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
 {
     int listenSock, servLen;
-    union   SockAddrUnion sa;
+    union   SockAddrUnion sa;  
     int            tcp = FALSE;
-    unsigned long tcp_ia;
+    unsigned long tcp_ia = 0;
     char    *tp;
-    short   port;
+    short   port = 0;
     char    host[MAXPATHLEN];
 
     strcpy(host, bindPath);
@@ -400,7 +386,7 @@ int OS_FcgiConnect(char *bindPath)
     int connectStatus;
     char    *tp;
     char    host[MAXPATHLEN];
-    short   port;
+    short   port = 0;
     int            tcp = FALSE;
 
     strcpy(host, bindPath);
@@ -646,7 +632,7 @@ int OS_AsyncRead(int fd, int offset, void *buf, int len,
     if(fd > maxFd)
         maxFd = fd;
 
-    if(index >= asyncIoTableSize) {
+    while (index >= asyncIoTableSize) {
         GrowAsyncTable();
     }
 
@@ -695,7 +681,7 @@ int OS_AsyncWrite(int fd, int offset, void *buf, int len,
     if(fd > maxFd)
         maxFd = fd;
 
-    if(index >= asyncIoTableSize) {
+    while (index >= asyncIoTableSize) {
         GrowAsyncTable();
     }
 
@@ -727,7 +713,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)
 {
     if (fd == -1)
         return 0;
@@ -752,6 +738,37 @@ int OS_Close(int fd)
             maxFd--;
         }
     }
+
+    /*
+     * 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(fd, 1) == 0)
+        {
+            struct timeval tv;
+            fd_set rfds;
+            int rv;
+            char trash[1024];
+
+            FD_ZERO(&rfds);
+
+            do 
+            {
+                FD_SET(fd, &rfds);
+                tv.tv_sec = 2;
+                tv.tv_usec = 0;
+                rv = select(fd + 1, &rfds, NULL, NULL, &tv);
+            }
+            while (rv > 0 && read(fd, trash, sizeof(trash)) > 0);
+        }
+    }
+
     return close(fd);
 }
 
@@ -1209,9 +1226,9 @@ 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)
 {
-    return OS_Close(ipcFd);
+    return OS_Close(ipcFd, shutdown);
 }
 
 /*