*/
#ifndef lint
-static const char rcsid[] = "$Id: os_unix.c,v 1.11 1999/08/02 19:22:00 skimo 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 <sys/types.h>
+
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
-#include <sys/types.h>
#include <sys/un.h>
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
-
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> /* for getpeername */
#endif
#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
/*
#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;
-static int isFastCGI = FALSE;
static int libInitialized = FALSE;
static fd_set readFdSet;
static int numWrPosted = 0;
static int volatile maxFd = -1;
-\f
/*
*--------------------------------------------------------------
*
return 0;
}
-\f
/*
*--------------------------------------------------------------
*
return;
}
-\f
/*
*----------------------------------------------------------------------
*
*----------------------------------------------------------------------
*/
-static int OS_BuildSockAddrUn(char *bindPath,
+static int OS_BuildSockAddrUn(const char *bindPath,
struct sockaddr_un *servAddrPtr,
int *servAddrLen)
{
#endif
return 0;
}
-\f
union SockAddrUnion {
struct sockaddr_un unixVariant;
struct sockaddr_in inetVariant;
};
-\f
/*
* OS_CreateLocalIpcFd --
*
*
*----------------------------------------------------------------------
*/
-int OS_CreateLocalIpcFd(char *bindPath)
+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];
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) {
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 {
}
}
if(bind(listenSock, (struct sockaddr *) &sa.unixVariant, servLen) < 0
- || listen(listenSock, 5) < 0) {
+ || listen(listenSock, backlog) < 0) {
perror("bind/listen");
exit(errno);
}
return listenSock;
}
-\f
/*
*----------------------------------------------------------------------
*
resultSock = socket(AF_UNIX, SOCK_STREAM, 0);
}
- assert(resultSock >= 0);
+ ASSERT(resultSock >= 0);
connectStatus = connect(resultSock, (struct sockaddr *) &sa.unixVariant,
servLen);
if(connectStatus >= 0) {
}
}
-\f
/*
*--------------------------------------------------------------
*
{
return(read(fd, buf, len));
}
-\f
+
/*
*--------------------------------------------------------------
*
return(write(fd, buf, len));
}
-\f
/*
*----------------------------------------------------------------------
*
return 0;
}
-\f
/*
*--------------------------------------------------------------
*
{
int index = AIO_RD_IX(STDIN_FILENO);
+ asyncIoInUse = TRUE;
ASSERT(asyncIoTable[index].inUse == 0);
asyncIoTable[index].procPtr = procPtr;
asyncIoTable[index].clientData = clientData;
}
-\f
/*
*--------------------------------------------------------------
*
int index = AIO_RD_IX(fd);
ASSERT(asyncIoTable != NULL);
+ asyncIoInUse = TRUE;
if(fd > maxFd)
maxFd = fd;
FD_SET(fd, &readFdSet);
return 0;
}
-\f
+
/*
*--------------------------------------------------------------
*
{
int index = AIO_WR_IX(fd);
+ asyncIoInUse = TRUE;
+
if(fd > maxFd)
maxFd = fd;
FD_SET(fd, &writeFdSet);
return 0;
}
-\f
+
/*
*--------------------------------------------------------------
*
*/
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, &writeFdSet);
- FD_CLR(fd, &writeFdSetPost);
- index = AIO_WR_IX(fd);
- if(asyncIoTable[index].inUse != 0) {
- asyncIoTable[index].inUse = 0;
+ 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;
+ }
+
+ if (maxFd == fd) {
+ maxFd--;
+ }
}
- if(maxFd == fd)
- maxFd--;
return close(fd);
}
-\f
+
/*
*--------------------------------------------------------------
*
return shutdown(fd, 0);
}
-\f
/*
*--------------------------------------------------------------
*
fd_set readFdSetCpy;
fd_set writeFdSetCpy;
+ asyncIoInUse = TRUE;
FD_ZERO(&readFdSetCpy);
FD_ZERO(&writeFdSetCpy);
return 0;
}
-\f
+/*
+ * 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;
+}
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-static int ClientAddrOK(struct sockaddr_in *saPtr, char *clientList)
+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;
}
-\f
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-static int AcquireLock(int blocking)
+static int AcquireLock(int sock, int fail_on_intr)
{
#ifdef USE_LOCKING
- struct flock lock;
- lock.l_type = F_WRLCK;
- lock.l_start = 0;
- lock.l_whence = SEEK_SET;
- lock.l_len = 0;
-
- if(fcntl(FCGI_LISTENSOCK_FILENO,
- blocking ? F_SETLKW : F_SETLK, &lock) < 0) {
- if (errno != EINTR)
- return -1;
- }
-#endif /* USE_LOCKING */
+ do {
+ struct flock lock;
+ lock.l_type = F_WRLCK;
+ lock.l_start = 0;
+ lock.l_whence = SEEK_SET;
+ lock.l_len = 0;
+
+ if (fcntl(sock, F_SETLKW, &lock) != -1)
+ return 0;
+ } while (errno == EINTR && !fail_on_intr);
+
+ return -1;
+
+#else
return 0;
+#endif
}
-\f
+
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-static int ReleaseLock(void)
+static int ReleaseLock(int sock)
{
#ifdef USE_LOCKING
- struct flock lock;
- lock.l_type = F_UNLCK;
- lock.l_start = 0;
- lock.l_whence = SEEK_SET;
- lock.l_len = 0;
+ do {
+ struct flock lock;
+ lock.l_type = F_UNLCK;
+ lock.l_start = 0;
+ lock.l_whence = SEEK_SET;
+ lock.l_len = 0;
+
+ if (fcntl(sock, F_SETLK, &lock) != -1)
+ return 0;
+ } while (errno == EINTR);
- if(fcntl(FCGI_LISTENSOCK_FILENO, F_SETLK, &lock) < 0) {
- return -1;
- }
-#endif /* USE_LOCKING */
+ return -1;
+
+#else
return 0;
+#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
/*
*----------------------------------------------------------------------
*
- * OS_FcgiIpcAccept --
+ * OS_Accept --
*
* Accepts a new FastCGI connection. This routine knows whether
* we're dealing with TCP based sockets or NT Named Pipes for IPC.
*
*----------------------------------------------------------------------
*/
-int OS_FcgiIpcAccept(char *clientAddrList)
+int OS_Accept(int listen_sock, int fail_on_intr, const char *webServerAddrs)
{
int socket;
union {
struct sockaddr_un un;
struct sockaddr_in in;
} sa;
-#ifdef HAVE_SOCKLEN
- socklen_t len;
-#else
- int len;
-#endif
- while (1) {
- if (AcquireLock(TRUE) < 0)
- return (-1);
+ for (;;) {
+ if (AcquireLock(listen_sock, fail_on_intr))
+ return -1;
- while (1) {
+ for (;;) {
do {
- len = sizeof(sa);
- socket = accept(FCGI_LISTENSOCK_FILENO, (struct sockaddr *) &sa.un, &len);
- } while (socket < 0 && errno == EINTR);
+#ifdef HAVE_SOCKLEN
+ socklen_t len = sizeof(sa);
+#else
+ int len = sizeof(sa);
+#endif
+ socket = accept(listen_sock, (struct sockaddr *)&sa, &len);
+ } while (socket < 0 && errno == EINTR && !fail_on_intr);
if (socket < 0) {
if (!is_reasonable_accept_errno(errno)) {
int errnoSave = errno;
-
- ReleaseLock();
+ ReleaseLock(listen_sock);
errno = errnoSave;
return (-1);
}
errno = 0;
}
- else {
+ else { /* socket >= 0 */
int set = 1;
if (sa.in.sin_family != AF_INET)
#endif
/* Check that the client IP address is approved */
- if (ClientAddrOK(&sa.in, clientAddrList))
+ if (ClientAddrOK(&sa.in, webServerAddrs))
break;
close(socket);
- }
- } /* while(1) - accept */
+ } /* socket >= 0 */
+ } /* for(;;) */
- if (ReleaseLock() < 0)
+ if (ReleaseLock(listen_sock))
return (-1);
if (sa.in.sin_family != AF_UNIX || is_af_unix_keeper(socket))
return (socket);
}
-\f
+
/*
*----------------------------------------------------------------------
*
return OS_Close(ipcFd);
}
-\f
/*
*----------------------------------------------------------------------
*
*
*----------------------------------------------------------------------
*/
-int OS_IsFcgi()
+int OS_IsFcgi(int sock)
{
union {
struct sockaddr_in in;
int len = sizeof(sa);
#endif
- if (getpeername(FCGI_LISTENSOCK_FILENO, (struct sockaddr *)&sa, &len) != 0
- && errno == ENOTCONN)
- isFastCGI = TRUE;
- else
- isFastCGI = FALSE;
+ errno = 0;
- return (isFastCGI);
+ if (getpeername(sock, (struct sockaddr *)&sa, &len) != 0 && errno == ENOTCONN) {
+ return TRUE;
+ }
+ else {
+ return FALSE;
+ }
}
-\f
+
/*
*----------------------------------------------------------------------
*