Eliminate const on buffersize to eliminate a compiler warning.
[catagits/fcgi2.git] / libfcgi / os_unix.c
index b43664a..7912c9b 100755 (executable)
@@ -17,7 +17,7 @@
  */
 
 #ifndef lint
-static const char rcsid[] = "$Id: os_unix.c,v 1.20 2000/09/20 15:45:29 robs Exp $";
+static const char rcsid[] = "$Id: os_unix.c,v 1.26 2001/06/18 14:24:28 robs Exp $";
 #endif /* not lint */
 
 #include "fcgi_config.h"
@@ -60,12 +60,8 @@ static const char rcsid[] = "$Id: os_unix.c,v 1.20 2000/09/20 15:45:29 robs Exp
 #include "fcgimisc.h"
 #include "fcgios.h"
 
-#ifndef FALSE
-#define FALSE 0
-#endif
-
-#ifndef TRUE
-#define TRUE 1
+#ifndef INADDR_NONE
+#define INADDR_NONE ((unsigned long) -1)
 #endif
 
 /*
@@ -105,7 +101,6 @@ static fd_set writeFdSetPost;
 static int numWrPosted = 0;
 static int volatile maxFd = -1;
 
-\f
 /*
  *--------------------------------------------------------------
  *
@@ -146,7 +141,6 @@ int OS_LibInit(int stdioFds[3])
     return 0;
 }
 
-\f
 /*
  *--------------------------------------------------------------
  *
@@ -173,7 +167,6 @@ void OS_LibShutdown()
     return;
 }
 
-\f
 /*
  *----------------------------------------------------------------------
  *
@@ -220,13 +213,11 @@ static int OS_BuildSockAddrUn(const char *bindPath,
 #endif
     return 0;
 }
-\f
 union SockAddrUnion {
     struct  sockaddr_un        unixVariant;
     struct  sockaddr_in        inetVariant;
 };
 
-\f
 /*
  * OS_CreateLocalIpcFd --
  *
@@ -249,6 +240,7 @@ int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
     int listenSock, servLen;
     union   SockAddrUnion sa;
     int            tcp = FALSE;
+    unsigned long tcp_ia;
     char    *tp;
     short   port;
     char    host[MAXPATHLEN];
@@ -262,12 +254,26 @@ int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
            tcp = TRUE;
         }
     }
-    if(tcp && (*host && strcmp(host, "localhost") != 0)) {
-       fprintf(stderr, "To start a service on a TCP port can not "
-                       "specify a host name.\n"
-                       "You should either use \"localhost:<port>\" or "
-                       " just use \":<port>.\"\n");
-       exit(1);
+    if(tcp) {
+      if (!*host || !strcmp(host,"*")) {
+       tcp_ia = htonl(INADDR_ANY);
+      } else {
+       tcp_ia = inet_addr(host);
+       if (tcp_ia == INADDR_NONE) {
+         struct hostent * hep;
+         hep = gethostbyname(host);
+         if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
+           fprintf(stderr, "Cannot resolve host name %s -- exiting!\n", host);
+           exit(1);
+         }
+         if (hep->h_addr_list[1]) {
+           fprintf(stderr, "Host %s has multiple addresses ---\n", host);
+           fprintf(stderr, "you must choose one explicitly!!!\n");
+           exit(1);
+         }
+         tcp_ia = ((struct in_addr *) (hep->h_addr))->s_addr;
+       }
+      }
     }
 
     if(tcp) {
@@ -293,7 +299,7 @@ int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
     if(tcp) {
        memset((char *) &sa.inetVariant, 0, sizeof(sa.inetVariant));
        sa.inetVariant.sin_family = AF_INET;
-       sa.inetVariant.sin_addr.s_addr = htonl(INADDR_ANY);
+       sa.inetVariant.sin_addr.s_addr = tcp_ia;
        sa.inetVariant.sin_port = htons(port);
        servLen = sizeof(sa.inetVariant);
     } else {
@@ -312,7 +318,6 @@ int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
     return listenSock;
 }
 
-\f
 /*
  *----------------------------------------------------------------------
  *
@@ -386,7 +391,6 @@ int OS_FcgiConnect(char *bindPath)
     }
 }
 
-\f
 /*
  *--------------------------------------------------------------
  *
@@ -407,7 +411,7 @@ int OS_Read(int fd, char * buf, size_t len)
 {
     return(read(fd, buf, len));
 }
-\f
+
 /*
  *--------------------------------------------------------------
  *
@@ -429,7 +433,6 @@ int OS_Write(int fd, char * buf, size_t len)
     return(write(fd, buf, len));
 }
 
-\f
 /*
  *----------------------------------------------------------------------
  *
@@ -495,7 +498,6 @@ int OS_SpawnChild(char *appPath, int listenFd)
     return 0;
 }
 
-\f
 /*
  *--------------------------------------------------------------
  *
@@ -552,7 +554,6 @@ static void GrowAsyncTable(void)
 
 }
 
-\f
 /*
  *--------------------------------------------------------------
  *
@@ -603,7 +604,7 @@ int OS_AsyncRead(int fd, int offset, void *buf, int len,
     FD_SET(fd, &readFdSet);
     return 0;
 }
-\f
+
 /*
  *--------------------------------------------------------------
  *
@@ -652,7 +653,7 @@ int OS_AsyncWrite(int fd, int offset, void *buf, int len,
     FD_SET(fd, &writeFdSet);
     return 0;
 }
-\f
+
 /*
  *--------------------------------------------------------------
  *
@@ -671,6 +672,9 @@ int OS_AsyncWrite(int fd, int offset, void *buf, int len,
  */
 int OS_Close(int fd)
 {
+    if (fd == -1)
+        return 0;
+
     if (asyncIoInUse) {
         int index = AIO_RD_IX(fd);
 
@@ -693,7 +697,7 @@ int OS_Close(int fd)
     }
     return close(fd);
 }
-\f
+
 /*
  *--------------------------------------------------------------
  *
@@ -718,7 +722,6 @@ int OS_CloseRead(int fd)
     return shutdown(fd, 0);
 }
 
-\f
 /*
  *--------------------------------------------------------------
  *
@@ -849,7 +852,6 @@ char * str_dup(const char * str)
     return sdup;
 }
 
-\f
 /*
  *----------------------------------------------------------------------
  *
@@ -889,7 +891,6 @@ static int ClientAddrOK(struct sockaddr_in *saPtr, const char *clientList)
     return result;
 }
 
-\f
 /*
  *----------------------------------------------------------------------
  *
@@ -928,7 +929,7 @@ static int AcquireLock(int sock, int fail_on_intr)
     return 0;
 #endif
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -967,7 +968,6 @@ static int ReleaseLock(int sock)
 #endif
 }
 
-\f
 /**********************************************************************
  * Determine if the errno resulting from a failed accept() warrants a
  * retry or exit().  Based on Apache's http_main.c accept() handling
@@ -1123,7 +1123,7 @@ int OS_Accept(int listen_sock, int fail_on_intr, const char *webServerAddrs)
 
     return (socket);
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1144,7 +1144,6 @@ int OS_IpcClose(int ipcFd)
     return OS_Close(ipcFd);
 }
 
-\f
 /*
  *----------------------------------------------------------------------
  *
@@ -1172,6 +1171,8 @@ int OS_IsFcgi(int sock)
     int len = sizeof(sa);
 #endif
 
+    errno = 0;
+
     if (getpeername(sock, (struct sockaddr *)&sa, &len) != 0 && errno == ENOTCONN) {
         return TRUE;
     }
@@ -1179,7 +1180,7 @@ int OS_IsFcgi(int sock)
         return FALSE;
     }
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *