7 * Copyright (c) 1995 Open Market, Inc.
10 * This file contains proprietary and confidential information and
11 * remains the unpublished property of Open Market, Inc. Use,
12 * disclosure, or reproduction is prohibited except as permitted by
13 * express written license agreement with Open Market, Inc.
16 * snapper@openmarket.com
20 static const char rcsid[] = "$Id: os_unix.c,v 1.31 2001/09/06 20:07:53 robs Exp $";
23 #include "fcgi_config.h"
25 #include <sys/types.h>
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 #include <fcntl.h> /* for fcntl */
36 #include <memory.h> /* for memchr() */
37 #include <netinet/tcp.h>
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h> /* for getpeername */
63 #define INADDR_NONE ((unsigned long) -1)
67 * This structure holds an entry for each oustanding async I/O operation.
70 OS_AsyncProc procPtr; /* callout completion procedure */
71 ClientData clientData; /* caller private data */
80 * Entries in the async I/O table are allocated 2 per file descriptor.
82 * Read Entry Index = fd * 2
83 * Write Entry Index = (fd * 2) + 1
85 #define AIO_RD_IX(fd) (fd * 2)
86 #define AIO_WR_IX(fd) ((fd * 2) + 1)
88 static int asyncIoInUse = FALSE;
89 static int asyncIoTableSize = 16;
90 static AioInfo *asyncIoTable = NULL;
92 static int libInitialized = FALSE;
94 static fd_set readFdSet;
95 static fd_set writeFdSet;
97 static fd_set readFdSetPost;
98 static int numRdPosted = 0;
99 static fd_set writeFdSetPost;
100 static int numWrPosted = 0;
101 static int volatile maxFd = -1;
103 static int shutdownPending = FALSE;
104 static int shutdownNow = FALSE;
109 OS_ShutdownPending();
112 void OS_ShutdownPending()
114 shutdownPending = TRUE;
117 static void OS_Sigusr1Handler(int signo)
119 OS_ShutdownPending();
122 static void OS_SigpipeHandler(int signo)
127 static void installSignalHandler(int signo, const struct sigaction * act, int force)
131 sigaction(signo, NULL, &sa);
133 if (force || sa.sa_handler == SIG_DFL)
135 sigaction(signo, act, NULL);
139 static void OS_InstallSignalHandlers(int force)
143 sigemptyset(&sa.sa_mask);
146 sa.sa_handler = OS_SigpipeHandler;
147 installSignalHandler(SIGPIPE, &sa, force);
149 sa.sa_handler = OS_Sigusr1Handler;
150 installSignalHandler(SIGUSR1, &sa, force);
154 *--------------------------------------------------------------
158 * Set up the OS library for use.
160 * NOTE: This function is really only needed for application
161 * asynchronous I/O. It will most likely change in the
162 * future to setup the multi-threaded environment.
165 * Returns 0 if success, -1 if not.
168 * Async I/O table allocated and initialized.
170 *--------------------------------------------------------------
172 int OS_LibInit(int stdioFds[3])
177 asyncIoTable = (AioInfo *)malloc(asyncIoTableSize * sizeof(AioInfo));
178 if(asyncIoTable == NULL) {
182 memset((char *) asyncIoTable, 0,
183 asyncIoTableSize * sizeof(AioInfo));
186 FD_ZERO(&writeFdSet);
187 FD_ZERO(&readFdSetPost);
188 FD_ZERO(&writeFdSetPost);
190 OS_InstallSignalHandlers(FALSE);
192 libInitialized = TRUE;
198 *--------------------------------------------------------------
202 * Shutdown the OS library.
208 * Memory freed, fds closed.
210 *--------------------------------------------------------------
212 void OS_LibShutdown()
221 libInitialized = FALSE;
226 *----------------------------------------------------------------------
228 * OS_BuildSockAddrUn --
230 * Using the pathname bindPath, fill in the sockaddr_un structure
231 * *servAddrPtr and the length of this structure *servAddrLen.
233 * The format of the sockaddr_un structure changed incompatibly in
234 * 4.3BSD Reno. Digital UNIX supports both formats, other systems
235 * support one or the other.
238 * 0 for normal return, -1 for failure (bindPath too long).
240 *----------------------------------------------------------------------
243 static int OS_BuildSockAddrUn(const char *bindPath,
244 struct sockaddr_un *servAddrPtr,
247 int bindPathLen = strlen(bindPath);
249 #ifdef HAVE_SOCKADDR_UN_SUN_LEN /* 4.3BSD Reno and later: BSDI, DEC */
250 if(bindPathLen >= sizeof(servAddrPtr->sun_path)) {
253 #else /* 4.3 BSD Tahoe: Solaris, HPUX, DEC, ... */
254 if(bindPathLen > sizeof(servAddrPtr->sun_path)) {
258 memset((char *) servAddrPtr, 0, sizeof(*servAddrPtr));
259 servAddrPtr->sun_family = AF_UNIX;
260 memcpy(servAddrPtr->sun_path, bindPath, bindPathLen);
261 #ifdef HAVE_SOCKADDR_UN_SUN_LEN /* 4.3BSD Reno and later: BSDI, DEC */
262 *servAddrLen = sizeof(servAddrPtr->sun_len)
263 + sizeof(servAddrPtr->sun_family)
265 servAddrPtr->sun_len = *servAddrLen;
266 #else /* 4.3 BSD Tahoe: Solaris, HPUX, DEC, ... */
267 *servAddrLen = sizeof(servAddrPtr->sun_family) + bindPathLen;
271 union SockAddrUnion {
272 struct sockaddr_un unixVariant;
273 struct sockaddr_in inetVariant;
277 * OS_CreateLocalIpcFd --
279 * This procedure is responsible for creating the listener socket
280 * on Unix for local process communication. It will create a
281 * domain socket or a TCP/IP socket bound to "localhost" and return
282 * a file descriptor to it to the caller.
285 * Listener socket created. This call returns either a valid
286 * file descriptor or -1 on error.
291 *----------------------------------------------------------------------
293 int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
295 int listenSock, servLen;
296 union SockAddrUnion sa;
298 unsigned long tcp_ia;
301 char host[MAXPATHLEN];
303 strcpy(host, bindPath);
304 if((tp = strchr(host, ':')) != 0) {
306 if((port = atoi(tp)) == 0) {
313 if (!*host || !strcmp(host,"*")) {
314 tcp_ia = htonl(INADDR_ANY);
316 tcp_ia = inet_addr(host);
317 if (tcp_ia == INADDR_NONE) {
318 struct hostent * hep;
319 hep = gethostbyname(host);
320 if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
321 fprintf(stderr, "Cannot resolve host name %s -- exiting!\n", host);
324 if (hep->h_addr_list[1]) {
325 fprintf(stderr, "Host %s has multiple addresses ---\n", host);
326 fprintf(stderr, "you must choose one explicitly!!!\n");
329 tcp_ia = ((struct in_addr *) (hep->h_addr))->s_addr;
335 listenSock = socket(AF_INET, SOCK_STREAM, 0);
336 if(listenSock >= 0) {
338 if(setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR,
339 (char *) &flag, sizeof(flag)) < 0) {
340 fprintf(stderr, "Can't set SO_REUSEADDR.\n");
345 listenSock = socket(AF_UNIX, SOCK_STREAM, 0);
352 * Bind the listening socket.
355 memset((char *) &sa.inetVariant, 0, sizeof(sa.inetVariant));
356 sa.inetVariant.sin_family = AF_INET;
357 sa.inetVariant.sin_addr.s_addr = tcp_ia;
358 sa.inetVariant.sin_port = htons(port);
359 servLen = sizeof(sa.inetVariant);
362 if(OS_BuildSockAddrUn(bindPath, &sa.unixVariant, &servLen)) {
363 fprintf(stderr, "Listening socket's path name is too long.\n");
367 if(bind(listenSock, (struct sockaddr *) &sa.unixVariant, servLen) < 0
368 || listen(listenSock, backlog) < 0) {
369 perror("bind/listen");
377 *----------------------------------------------------------------------
381 * Create the socket and connect to the remote application if
384 * This was lifted from the cgi-fcgi application and was abstracted
385 * out because Windows NT does not have a domain socket and must
386 * use a named pipe which has a different API altogether.
389 * -1 if fail or a valid file descriptor if connection succeeds.
392 * Remote connection established.
394 *----------------------------------------------------------------------
396 int OS_FcgiConnect(char *bindPath)
398 union SockAddrUnion sa;
399 int servLen, resultSock;
402 char host[MAXPATHLEN];
406 strcpy(host, bindPath);
407 if((tp = strchr(host, ':')) != 0) {
409 if((port = atoi(tp)) == 0) {
417 if((hp = gethostbyname((*host ? host : "localhost"))) == NULL) {
418 fprintf(stderr, "Unknown host: %s\n", bindPath);
421 sa.inetVariant.sin_family = AF_INET;
422 memcpy(&sa.inetVariant.sin_addr, hp->h_addr, hp->h_length);
423 sa.inetVariant.sin_port = htons(port);
424 servLen = sizeof(sa.inetVariant);
425 resultSock = socket(AF_INET, SOCK_STREAM, 0);
427 if(OS_BuildSockAddrUn(bindPath, &sa.unixVariant, &servLen)) {
428 fprintf(stderr, "Listening socket's path name is too long.\n");
431 resultSock = socket(AF_UNIX, SOCK_STREAM, 0);
434 ASSERT(resultSock >= 0);
435 connectStatus = connect(resultSock, (struct sockaddr *) &sa.unixVariant,
437 if(connectStatus >= 0) {
441 * Most likely (errno == ENOENT || errno == ECONNREFUSED)
442 * and no FCGI application server is running.
450 *--------------------------------------------------------------
454 * Pass through to the unix read function.
457 * Returns number of byes read, 0, or -1 failure: errno
458 * contains actual error.
463 *--------------------------------------------------------------
465 int OS_Read(int fd, char * buf, size_t len)
467 if (shutdownNow) return -1;
468 return(read(fd, buf, len));
472 *--------------------------------------------------------------
476 * Pass through to unix write function.
479 * Returns number of byes read, 0, or -1 failure: errno
480 * contains actual error.
485 *--------------------------------------------------------------
487 int OS_Write(int fd, char * buf, size_t len)
489 if (shutdownNow) return -1;
490 return(write(fd, buf, len));
494 *----------------------------------------------------------------------
498 * Spawns a new FastCGI listener process.
501 * 0 if success, -1 if error.
504 * Child process spawned.
506 *----------------------------------------------------------------------
508 int OS_SpawnChild(char *appPath, int listenFd)
517 if(forkResult == 0) {
519 * Close STDIN unconditionally. It's used by the parent
520 * process for CGI communication. The FastCGI applciation
521 * will be replacing this with the FastCGI listenFd IF
522 * STDIN_FILENO is the same as FCGI_LISTENSOCK_FILENO
523 * (which it is on Unix). Regardless, STDIN, STDOUT, and
524 * STDERR will be closed as the FastCGI process uses a
525 * multiplexed socket in their place.
530 * If the listenFd is already the value of FCGI_LISTENSOCK_FILENO
531 * we're set. If not, change it so the child knows where to
532 * get the listen socket from.
534 if(listenFd != FCGI_LISTENSOCK_FILENO) {
535 dup2(listenFd, FCGI_LISTENSOCK_FILENO);
539 close(STDOUT_FILENO);
540 close(STDERR_FILENO);
543 * We're a child. Exec the application.
545 * XXX: entire environment passes through
547 execl(appPath, appPath, NULL);
549 * XXX: Can't do this as we've already closed STDERR!!!
559 *--------------------------------------------------------------
561 * OS_AsyncReadStdin --
563 * This initiates an asynchronous read on the standard
566 * The abstraction is necessary because Windows NT does not
567 * have a clean way of "select"ing a file descriptor for
571 * -1 if error, 0 otherwise.
574 * Asynchronous bit is set in the readfd variable and
575 * request is enqueued.
577 *--------------------------------------------------------------
579 int OS_AsyncReadStdin(void *buf, int len, OS_AsyncProc procPtr,
580 ClientData clientData)
582 int index = AIO_RD_IX(STDIN_FILENO);
585 ASSERT(asyncIoTable[index].inUse == 0);
586 asyncIoTable[index].procPtr = procPtr;
587 asyncIoTable[index].clientData = clientData;
588 asyncIoTable[index].fd = STDIN_FILENO;
589 asyncIoTable[index].len = len;
590 asyncIoTable[index].offset = 0;
591 asyncIoTable[index].buf = buf;
592 asyncIoTable[index].inUse = 1;
593 FD_SET(STDIN_FILENO, &readFdSet);
594 if(STDIN_FILENO > maxFd)
595 maxFd = STDIN_FILENO;
599 static void GrowAsyncTable(void)
601 int oldTableSize = asyncIoTableSize;
603 asyncIoTableSize = asyncIoTableSize * 2;
604 asyncIoTable = (AioInfo *)realloc(asyncIoTable, asyncIoTableSize * sizeof(AioInfo));
605 if(asyncIoTable == NULL) {
609 memset((char *) &asyncIoTable[oldTableSize], 0,
610 oldTableSize * sizeof(AioInfo));
615 *--------------------------------------------------------------
619 * This initiates an asynchronous read on the file
620 * handle which may be a socket or named pipe.
622 * We also must save the ProcPtr and ClientData, so later
623 * when the io completes, we know who to call.
625 * We don't look at any results here (the ReadFile may
626 * return data if it is cached) but do all completion
627 * processing in OS_Select when we get the io completion
628 * port done notifications. Then we call the callback.
631 * -1 if error, 0 otherwise.
634 * Asynchronous I/O operation is queued for completion.
636 *--------------------------------------------------------------
638 int OS_AsyncRead(int fd, int offset, void *buf, int len,
639 OS_AsyncProc procPtr, ClientData clientData)
641 int index = AIO_RD_IX(fd);
643 ASSERT(asyncIoTable != NULL);
649 if(index >= asyncIoTableSize) {
653 ASSERT(asyncIoTable[index].inUse == 0);
654 asyncIoTable[index].procPtr = procPtr;
655 asyncIoTable[index].clientData = clientData;
656 asyncIoTable[index].fd = fd;
657 asyncIoTable[index].len = len;
658 asyncIoTable[index].offset = offset;
659 asyncIoTable[index].buf = buf;
660 asyncIoTable[index].inUse = 1;
661 FD_SET(fd, &readFdSet);
666 *--------------------------------------------------------------
670 * This initiates an asynchronous write on the "fake" file
671 * descriptor (which may be a file, socket, or named pipe).
672 * We also must save the ProcPtr and ClientData, so later
673 * when the io completes, we know who to call.
675 * We don't look at any results here (the WriteFile generally
676 * completes immediately) but do all completion processing
677 * in OS_DoIo when we get the io completion port done
678 * notifications. Then we call the callback.
681 * -1 if error, 0 otherwise.
684 * Asynchronous I/O operation is queued for completion.
686 *--------------------------------------------------------------
688 int OS_AsyncWrite(int fd, int offset, void *buf, int len,
689 OS_AsyncProc procPtr, ClientData clientData)
691 int index = AIO_WR_IX(fd);
698 if(index >= asyncIoTableSize) {
702 ASSERT(asyncIoTable[index].inUse == 0);
703 asyncIoTable[index].procPtr = procPtr;
704 asyncIoTable[index].clientData = clientData;
705 asyncIoTable[index].fd = fd;
706 asyncIoTable[index].len = len;
707 asyncIoTable[index].offset = offset;
708 asyncIoTable[index].buf = buf;
709 asyncIoTable[index].inUse = 1;
710 FD_SET(fd, &writeFdSet);
715 *--------------------------------------------------------------
719 * Closes the descriptor. This is a pass through to the
723 * 0 for success, -1 on failure
728 *--------------------------------------------------------------
736 int index = AIO_RD_IX(fd);
738 FD_CLR(fd, &readFdSet);
739 FD_CLR(fd, &readFdSetPost);
740 if (asyncIoTable[index].inUse != 0) {
741 asyncIoTable[index].inUse = 0;
744 FD_CLR(fd, &writeFdSet);
745 FD_CLR(fd, &writeFdSetPost);
746 index = AIO_WR_IX(fd);
747 if (asyncIoTable[index].inUse != 0) {
748 asyncIoTable[index].inUse = 0;
759 *--------------------------------------------------------------
763 * Cancel outstanding asynchronous reads and prevent subsequent
764 * reads from completing.
767 * Socket or file is shutdown. Return values mimic Unix shutdown:
768 * 0 success, -1 failure
770 *--------------------------------------------------------------
772 int OS_CloseRead(int fd)
774 if(asyncIoTable[AIO_RD_IX(fd)].inUse != 0) {
775 asyncIoTable[AIO_RD_IX(fd)].inUse = 0;
776 FD_CLR(fd, &readFdSet);
779 return shutdown(fd, 0);
783 *--------------------------------------------------------------
787 * This function was formerly OS_Select. It's purpose is
788 * to pull I/O completion events off the queue and dispatch
789 * them to the appropriate place.
795 * Handlers are called.
797 *--------------------------------------------------------------
799 int OS_DoIo(struct timeval *tmo)
801 int fd, len, selectStatus;
802 OS_AsyncProc procPtr;
803 ClientData clientData;
806 fd_set writeFdSetCpy;
809 FD_ZERO(&readFdSetCpy);
810 FD_ZERO(&writeFdSetCpy);
812 for(fd = 0; fd <= maxFd; fd++) {
813 if(FD_ISSET(fd, &readFdSet)) {
814 FD_SET(fd, &readFdSetCpy);
816 if(FD_ISSET(fd, &writeFdSet)) {
817 FD_SET(fd, &writeFdSetCpy);
822 * If there were no completed events from a prior call, see if there's
825 if(numRdPosted == 0 && numWrPosted == 0) {
826 selectStatus = select((maxFd+1), &readFdSetCpy, &writeFdSetCpy,
828 if(selectStatus < 0) {
832 for(fd = 0; fd <= maxFd; fd++) {
834 * Build up a list of completed events. We'll work off of
835 * this list as opposed to looping through the read and write
836 * fd sets since they can be affected by a callbacl routine.
838 if(FD_ISSET(fd, &readFdSetCpy)) {
840 FD_SET(fd, &readFdSetPost);
841 FD_CLR(fd, &readFdSet);
844 if(FD_ISSET(fd, &writeFdSetCpy)) {
846 FD_SET(fd, &writeFdSetPost);
847 FD_CLR(fd, &writeFdSet);
852 if(numRdPosted == 0 && numWrPosted == 0)
855 for(fd = 0; fd <= maxFd; fd++) {
857 * Do reads and dispatch callback.
859 if(FD_ISSET(fd, &readFdSetPost)
860 && asyncIoTable[AIO_RD_IX(fd)].inUse) {
863 FD_CLR(fd, &readFdSetPost);
864 aioPtr = &asyncIoTable[AIO_RD_IX(fd)];
866 len = read(aioPtr->fd, aioPtr->buf, aioPtr->len);
868 procPtr = aioPtr->procPtr;
869 aioPtr->procPtr = NULL;
870 clientData = aioPtr->clientData;
873 (*procPtr)(clientData, len);
877 * Do writes and dispatch callback.
879 if(FD_ISSET(fd, &writeFdSetPost) &&
880 asyncIoTable[AIO_WR_IX(fd)].inUse) {
883 FD_CLR(fd, &writeFdSetPost);
884 aioPtr = &asyncIoTable[AIO_WR_IX(fd)];
886 len = write(aioPtr->fd, aioPtr->buf, aioPtr->len);
888 procPtr = aioPtr->procPtr;
889 aioPtr->procPtr = NULL;
890 clientData = aioPtr->clientData;
892 (*procPtr)(clientData, len);
899 * Not all systems have strdup().
900 * @@@ autoconf should determine whether or not this is needed, but for now..
902 static char * str_dup(const char * str)
904 char * sdup = (char *) malloc(strlen(str) + 1);
913 *----------------------------------------------------------------------
917 * Checks if a client address is in a list of allowed addresses
920 * TRUE if address list is empty or client address is present
921 * in the list, FALSE otherwise.
923 *----------------------------------------------------------------------
925 static int ClientAddrOK(struct sockaddr_in *saPtr, const char *clientList)
928 char *clientListCopy, *cur, *next;
930 if (clientList == NULL || *clientList == '\0') {
934 clientListCopy = str_dup(clientList);
936 for (cur = clientListCopy; cur != NULL; cur = next) {
937 next = strchr(cur, ',');
941 if (inet_addr(cur) == saPtr->sin_addr.s_addr) {
947 free(clientListCopy);
952 *----------------------------------------------------------------------
956 * On platforms that implement concurrent calls to accept
957 * on a shared listening ipcFd, returns 0. On other platforms,
958 * acquires an exclusive lock across all processes sharing a
959 * listening ipcFd, blocking until the lock has been acquired.
962 * 0 for successful call, -1 in case of system error (fatal).
965 * This process now has the exclusive lock.
967 *----------------------------------------------------------------------
969 static int AcquireLock(int sock, int fail_on_intr)
974 lock.l_type = F_WRLCK;
976 lock.l_whence = SEEK_SET;
979 if (fcntl(sock, F_SETLKW, &lock) != -1)
981 } while (errno == EINTR
983 && ! shutdownPending);
993 *----------------------------------------------------------------------
997 * On platforms that implement concurrent calls to accept
998 * on a shared listening ipcFd, does nothing. On other platforms,
999 * releases an exclusive lock acquired by AcquireLock.
1002 * 0 for successful call, -1 in case of system error (fatal).
1005 * This process no longer holds the lock.
1007 *----------------------------------------------------------------------
1009 static int ReleaseLock(int sock)
1014 lock.l_type = F_UNLCK;
1016 lock.l_whence = SEEK_SET;
1019 if (fcntl(sock, F_SETLK, &lock) != -1)
1021 } while (errno == EINTR);
1030 /**********************************************************************
1031 * Determine if the errno resulting from a failed accept() warrants a
1032 * retry or exit(). Based on Apache's http_main.c accept() handling
1033 * and Stevens' Unix Network Programming Vol 1, 2nd Ed, para. 15.6.
1035 static int is_reasonable_accept_errno (const int error)
1039 /* EPROTO on certain older kernels really means ECONNABORTED, so
1040 * we need to ignore it for them. See discussion in new-httpd
1041 * archives nh.9701 search for EPROTO. Also see nh.9603, search
1042 * for EPROTO: There is potentially a bug in Solaris 2.x x<6, and
1043 * other boxes that implement tcp sockets in userland (i.e. on top of
1044 * STREAMS). On these systems, EPROTO can actually result in a fatal
1045 * loop. See PR#981 for example. It's hard to handle both uses of
1052 /* Linux generates the rest of these, other tcp stacks (i.e.
1053 * bsd) tend to hide them behind getsockopt() interfaces. They
1054 * occur when the net goes sour or the client disconnects after the
1055 * three-way handshake has been done in the kernel but before
1056 * userland has picked up the socket. */
1076 /**********************************************************************
1077 * This works around a problem on Linux 2.0.x and SCO Unixware (maybe
1078 * others?). When a connect() is made to a Unix Domain socket, but its
1079 * not accept()ed before the web server gets impatient and close()s, an
1080 * accept() results in a valid file descriptor, but no data to read.
1081 * This causes a block on the first read() - which never returns!
1083 * Another approach to this is to write() to the socket to provoke a
1084 * SIGPIPE, but this is a pain because of the FastCGI protocol, the fact
1085 * that whatever is written has to be universally ignored by all FastCGI
1086 * web servers, and a SIGPIPE handler has to be installed which returns
1087 * (or SIGPIPE is ignored).
1089 * READABLE_UNIX_FD_DROP_DEAD_TIMEVAL = 2,0 by default.
1091 * Making it shorter is probably safe, but I'll leave that to you. Making
1092 * it 0,0 doesn't work reliably. The shorter you can reliably make it,
1093 * the faster your application will be able to recover (waiting 2 seconds
1094 * may _cause_ the problem when there is a very high demand). At any rate,
1095 * this is better than perma-blocking.
1097 static int is_af_unix_keeper(const int fd)
1099 struct timeval tval = { READABLE_UNIX_FD_DROP_DEAD_TIMEVAL };
1103 FD_SET(fd, &read_fds);
1105 return select(fd + 1, &read_fds, NULL, NULL, &tval) >= 0 && FD_ISSET(fd, &read_fds);
1109 *----------------------------------------------------------------------
1113 * Accepts a new FastCGI connection. This routine knows whether
1114 * we're dealing with TCP based sockets or NT Named Pipes for IPC.
1117 * -1 if the operation fails, otherwise this is a valid IPC fd.
1120 * New IPC connection is accepted.
1122 *----------------------------------------------------------------------
1124 int OS_Accept(int listen_sock, int fail_on_intr, const char *webServerAddrs)
1128 struct sockaddr_un un;
1129 struct sockaddr_in in;
1133 if (AcquireLock(listen_sock, fail_on_intr))
1139 socklen_t len = sizeof(sa);
1141 int len = sizeof(sa);
1143 if (shutdownPending) break;
1144 /* There's a window here */
1146 socket = accept(listen_sock, (struct sockaddr *)&sa, &len);
1150 && ! shutdownPending);
1153 if (shutdownPending || ! is_reasonable_accept_errno(errno)) {
1154 int errnoSave = errno;
1156 ReleaseLock(listen_sock);
1158 if (! shutdownPending) {
1166 else { /* socket >= 0 */
1169 if (sa.in.sin_family != AF_INET)
1173 /* No replies to outgoing data, so disable Nagle */
1174 setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&set, sizeof(set));
1177 /* Check that the client IP address is approved */
1178 if (ClientAddrOK(&sa.in, webServerAddrs))
1185 if (ReleaseLock(listen_sock))
1188 if (sa.in.sin_family != AF_UNIX || is_af_unix_keeper(socket))
1192 } /* while(1) - lock */
1198 *----------------------------------------------------------------------
1202 * OS IPC routine to close an IPC connection.
1208 * IPC connection is closed.
1210 *----------------------------------------------------------------------
1212 int OS_IpcClose(int ipcFd)
1214 return OS_Close(ipcFd);
1218 *----------------------------------------------------------------------
1222 * Determines whether this process is a FastCGI process or not.
1225 * Returns 1 if FastCGI, 0 if not.
1230 *----------------------------------------------------------------------
1232 int OS_IsFcgi(int sock)
1235 struct sockaddr_in in;
1236 struct sockaddr_un un;
1239 socklen_t len = sizeof(sa);
1241 int len = sizeof(sa);
1246 if (getpeername(sock, (struct sockaddr *)&sa, &len) != 0 && errno == ENOTCONN) {
1255 *----------------------------------------------------------------------
1259 * Sets selected flag bits in an open file descriptor.
1261 *----------------------------------------------------------------------
1263 void OS_SetFlags(int fd, int flags)
1266 if((val = fcntl(fd, F_GETFL, 0)) < 0) {
1270 if(fcntl(fd, F_SETFL, val) < 0) {