X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=libfcgi%2Fos_unix.c;h=7912c9b62f76d283391eeeb7bd3341a547d0d21c;hb=5215a48a265b9583234d1d3c105b0c1cfcf96d21;hp=9ba8962076513e803de53c65410f437e48e584fe;hpb=8db9dd8f3a09bd0b1591dbefc785ad2487d8fd4c;p=catagits%2Ffcgi2.git diff --git a/libfcgi/os_unix.c b/libfcgi/os_unix.c index 9ba8962..7912c9b 100755 --- a/libfcgi/os_unix.c +++ b/libfcgi/os_unix.c @@ -17,11 +17,13 @@ */ #ifndef lint -static const char rcsid[] = "$Id: os_unix.c,v 1.15 1999/08/15 17:57:17 roberts 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" +#include + #ifdef HAVE_NETINET_IN_H #include #endif @@ -38,7 +40,6 @@ static const char rcsid[] = "$Id: os_unix.c,v 1.15 1999/08/15 17:57:17 roberts E #include #include #include -#include #include #ifdef HAVE_NETDB_H @@ -59,12 +60,8 @@ static const char rcsid[] = "$Id: os_unix.c,v 1.15 1999/08/15 17:57:17 roberts E #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 /* @@ -89,6 +86,7 @@ typedef struct { #define AIO_RD_IX(fd) (fd * 2) #define AIO_WR_IX(fd) ((fd * 2) + 1) +static int asyncIoInUse = FALSE; static int asyncIoTableSize = 16; static AioInfo *asyncIoTable = NULL; @@ -103,7 +101,6 @@ static fd_set writeFdSetPost; static int numWrPosted = 0; static int volatile maxFd = -1; - /* *-------------------------------------------------------------- * @@ -144,7 +141,6 @@ int OS_LibInit(int stdioFds[3]) return 0; } - /* *-------------------------------------------------------------- * @@ -171,7 +167,6 @@ void OS_LibShutdown() return; } - /* *---------------------------------------------------------------------- * @@ -218,13 +213,11 @@ static int OS_BuildSockAddrUn(const char *bindPath, #endif return 0; } - union SockAddrUnion { struct sockaddr_un unixVariant; struct sockaddr_in inetVariant; }; - /* * OS_CreateLocalIpcFd -- * @@ -247,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]; @@ -260,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:\" or " - " just use \":.\"\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) { @@ -291,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 { @@ -310,7 +318,6 @@ int OS_CreateLocalIpcFd(const char *bindPath, int backlog) return listenSock; } - /* *---------------------------------------------------------------------- * @@ -369,7 +376,7 @@ int OS_FcgiConnect(char *bindPath) resultSock = socket(AF_UNIX, SOCK_STREAM, 0); } - assert(resultSock >= 0); + ASSERT(resultSock >= 0); connectStatus = connect(resultSock, (struct sockaddr *) &sa.unixVariant, servLen); if(connectStatus >= 0) { @@ -384,7 +391,6 @@ int OS_FcgiConnect(char *bindPath) } } - /* *-------------------------------------------------------------- * @@ -405,7 +411,7 @@ int OS_Read(int fd, char * buf, size_t len) { return(read(fd, buf, len)); } - + /* *-------------------------------------------------------------- * @@ -427,7 +433,6 @@ int OS_Write(int fd, char * buf, size_t len) return(write(fd, buf, len)); } - /* *---------------------------------------------------------------------- * @@ -493,7 +498,6 @@ int OS_SpawnChild(char *appPath, int listenFd) return 0; } - /* *-------------------------------------------------------------- * @@ -520,6 +524,7 @@ int OS_AsyncReadStdin(void *buf, int len, OS_AsyncProc procPtr, { int index = AIO_RD_IX(STDIN_FILENO); + asyncIoInUse = TRUE; ASSERT(asyncIoTable[index].inUse == 0); asyncIoTable[index].procPtr = procPtr; asyncIoTable[index].clientData = clientData; @@ -549,7 +554,6 @@ static void GrowAsyncTable(void) } - /* *-------------------------------------------------------------- * @@ -580,6 +584,7 @@ int OS_AsyncRead(int fd, int offset, void *buf, int len, int index = AIO_RD_IX(fd); ASSERT(asyncIoTable != NULL); + asyncIoInUse = TRUE; if(fd > maxFd) maxFd = fd; @@ -599,7 +604,7 @@ int OS_AsyncRead(int fd, int offset, void *buf, int len, FD_SET(fd, &readFdSet); return 0; } - + /* *-------------------------------------------------------------- * @@ -628,6 +633,8 @@ int OS_AsyncWrite(int fd, int offset, void *buf, int len, { int index = AIO_WR_IX(fd); + asyncIoInUse = TRUE; + if(fd > maxFd) maxFd = fd; @@ -646,7 +653,7 @@ int OS_AsyncWrite(int fd, int offset, void *buf, int len, FD_SET(fd, &writeFdSet); return 0; } - + /* *-------------------------------------------------------------- * @@ -665,25 +672,32 @@ int OS_AsyncWrite(int fd, int offset, void *buf, int len, */ int OS_Close(int fd) { - int index = AIO_RD_IX(fd); + if (fd == -1) + return 0; - FD_CLR(fd, &readFdSet); - FD_CLR(fd, &readFdSetPost); - if(asyncIoTable[index].inUse != 0) { - asyncIoTable[index].inUse = 0; - } + if (asyncIoInUse) { + int index = AIO_RD_IX(fd); + + FD_CLR(fd, &readFdSet); + FD_CLR(fd, &readFdSetPost); + if (asyncIoTable[index].inUse != 0) { + asyncIoTable[index].inUse = 0; + } - FD_CLR(fd, &writeFdSet); - FD_CLR(fd, &writeFdSetPost); - index = AIO_WR_IX(fd); - if(asyncIoTable[index].inUse != 0) { - asyncIoTable[index].inUse = 0; + FD_CLR(fd, &writeFdSet); + FD_CLR(fd, &writeFdSetPost); + index = AIO_WR_IX(fd); + if (asyncIoTable[index].inUse != 0) { + asyncIoTable[index].inUse = 0; + } + + if (maxFd == fd) { + maxFd--; + } } - if(maxFd == fd) - maxFd--; return close(fd); } - + /* *-------------------------------------------------------------- * @@ -708,7 +722,6 @@ int OS_CloseRead(int fd) return shutdown(fd, 0); } - /* *-------------------------------------------------------------- * @@ -735,6 +748,7 @@ int OS_DoIo(struct timeval *tmo) fd_set readFdSetCpy; fd_set writeFdSetCpy; + asyncIoInUse = TRUE; FD_ZERO(&readFdSetCpy); FD_ZERO(&writeFdSetCpy); @@ -824,7 +838,20 @@ int OS_DoIo(struct timeval *tmo) return 0; } - +/* + * Not all systems have strdup(). + * @@@ autoconf should determine whether or not this is needed, but for now.. + */ +char * str_dup(const char * str) +{ + char * sdup = (char *) malloc(strlen(str) + 1); + + if (sdup) + strcpy(sdup, str); + + return sdup; +} + /* *---------------------------------------------------------------------- * @@ -842,34 +869,28 @@ static int ClientAddrOK(struct sockaddr_in *saPtr, const char *clientList) { int result = FALSE; char *clientListCopy, *cur, *next; - char *newString = NULL; - int strLen; - if(clientList == NULL || *clientList == '\0') { + if (clientList == NULL || *clientList == '\0') { return TRUE; } - strLen = strlen(clientList); - clientListCopy = (char *)malloc(strLen + 1); - assert(newString != NULL); - memcpy(newString, clientList, strLen); - newString[strLen] = '\000'; + clientListCopy = str_dup(clientList); - for(cur = clientListCopy; cur != NULL; cur = next) { + for (cur = clientListCopy; cur != NULL; cur = next) { next = strchr(cur, ','); - if(next != NULL) { + if (next != NULL) { *next++ = '\0'; - } - if(inet_addr(cur) == saPtr->sin_addr.s_addr) { + } + if (inet_addr(cur) == saPtr->sin_addr.s_addr) { result = TRUE; break; } } + free(clientListCopy); return result; } - /* *---------------------------------------------------------------------- * @@ -908,7 +929,7 @@ static int AcquireLock(int sock, int fail_on_intr) return 0; #endif } - + /* *---------------------------------------------------------------------- * @@ -947,7 +968,6 @@ static int ReleaseLock(int sock) #endif } - /********************************************************************** * Determine if the errno resulting from a failed accept() warrants a * retry or exit(). Based on Apache's http_main.c accept() handling @@ -1103,7 +1123,7 @@ int OS_Accept(int listen_sock, int fail_on_intr, const char *webServerAddrs) return (socket); } - + /* *---------------------------------------------------------------------- * @@ -1124,7 +1144,6 @@ int OS_IpcClose(int ipcFd) return OS_Close(ipcFd); } - /* *---------------------------------------------------------------------- * @@ -1152,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; } @@ -1159,7 +1180,7 @@ int OS_IsFcgi(int sock) return FALSE; } } - + /* *---------------------------------------------------------------------- *