123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
- # pragma GCC diagnostic ignored "-Wtype-limits"
- #endif
- #include <config.h>
- #include <alloca.h>
- #include <sys/types.h>
- #include <poll.h>
- #include <errno.h>
- #include <limits.h>
- #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
- # define WINDOWS_NATIVE
- # include <winsock2.h>
- # include <windows.h>
- # include <io.h>
- # include <stdio.h>
- # include <conio.h>
- # include "msvc-nothrow.h"
- #else
- # include <sys/time.h>
- # include <unistd.h>
- #endif
- #include <sys/select.h>
- #include <sys/socket.h>
- #ifdef HAVE_SYS_IOCTL_H
- # include <sys/ioctl.h>
- #endif
- #ifdef HAVE_SYS_FILIO_H
- # include <sys/filio.h>
- #endif
- #include <time.h>
- #include "assure.h"
- #ifndef INFTIM
- # define INFTIM (-1)
- #endif
- #ifndef MSG_PEEK
- # define MSG_PEEK 0
- #endif
- #ifdef WINDOWS_NATIVE
- static BOOL IsConsoleHandle (HANDLE h)
- {
- DWORD mode;
- return GetConsoleMode (h, &mode) != 0;
- }
- static BOOL
- IsSocketHandle (HANDLE h)
- {
- WSANETWORKEVENTS ev;
- if (IsConsoleHandle (h))
- return FALSE;
-
- ev.lNetworkEvents = 0xDEADBEEF;
- WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
- return ev.lNetworkEvents != 0xDEADBEEF;
- }
- typedef struct _FILE_PIPE_LOCAL_INFORMATION {
- ULONG NamedPipeType;
- ULONG NamedPipeConfiguration;
- ULONG MaximumInstances;
- ULONG CurrentInstances;
- ULONG InboundQuota;
- ULONG ReadDataAvailable;
- ULONG OutboundQuota;
- ULONG WriteQuotaAvailable;
- ULONG NamedPipeState;
- ULONG NamedPipeEnd;
- } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION;
- typedef struct _IO_STATUS_BLOCK
- {
- union {
- DWORD Status;
- PVOID Pointer;
- } u;
- ULONG_PTR Information;
- } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
- typedef enum _FILE_INFORMATION_CLASS {
- FilePipeLocalInformation = 24
- } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;
- typedef DWORD (WINAPI *PNtQueryInformationFile)
- (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS);
- # ifndef PIPE_BUF
- # define PIPE_BUF 512
- # endif
- static int
- windows_compute_revents (HANDLE h, int *p_sought)
- {
- int i, ret, happened;
- INPUT_RECORD *irbuffer;
- DWORD avail, nbuffer;
- BOOL bRet;
- IO_STATUS_BLOCK iosb;
- FILE_PIPE_LOCAL_INFORMATION fpli;
- static PNtQueryInformationFile NtQueryInformationFile;
- static BOOL once_only;
- switch (GetFileType (h))
- {
- case FILE_TYPE_PIPE:
- if (!once_only)
- {
- NtQueryInformationFile = (PNtQueryInformationFile)
- GetProcAddress (GetModuleHandle ("ntdll.dll"),
- "NtQueryInformationFile");
- once_only = TRUE;
- }
- happened = 0;
- if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0)
- {
- if (avail)
- happened |= *p_sought & (POLLIN | POLLRDNORM);
- }
- else if (GetLastError () == ERROR_BROKEN_PIPE)
- happened |= POLLHUP;
- else
- {
-
- memset (&iosb, 0, sizeof (iosb));
- memset (&fpli, 0, sizeof (fpli));
- if (!NtQueryInformationFile
- || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
- FilePipeLocalInformation)
- || fpli.WriteQuotaAvailable >= PIPE_BUF
- || (fpli.OutboundQuota < PIPE_BUF &&
- fpli.WriteQuotaAvailable == fpli.OutboundQuota))
- happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
- }
- return happened;
- case FILE_TYPE_CHAR:
- ret = WaitForSingleObject (h, 0);
- if (!IsConsoleHandle (h))
- return ret == WAIT_OBJECT_0 ? *p_sought & ~(POLLPRI | POLLRDBAND) : 0;
- nbuffer = avail = 0;
- bRet = GetNumberOfConsoleInputEvents (h, &nbuffer);
- if (bRet)
- {
-
- *p_sought &= POLLIN | POLLRDNORM;
- if (nbuffer == 0)
- return POLLHUP;
- if (!*p_sought)
- return 0;
- irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD));
- bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail);
- if (!bRet || avail == 0)
- return POLLHUP;
- for (i = 0; i < avail; i++)
- if (irbuffer[i].EventType == KEY_EVENT)
- return *p_sought;
- return 0;
- }
- else
- {
-
- *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND;
- return *p_sought;
- }
- default:
- ret = WaitForSingleObject (h, 0);
- if (ret == WAIT_OBJECT_0)
- return *p_sought & ~(POLLPRI | POLLRDBAND);
- return *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
- }
- }
- static int
- windows_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents)
- {
- int happened = 0;
- if ((lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) == FD_ACCEPT)
- happened |= (POLLIN | POLLRDNORM) & sought;
- else if (lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
- {
- int r, error;
- char data[64];
- WSASetLastError (0);
- r = recv (h, data, sizeof (data), MSG_PEEK);
- error = WSAGetLastError ();
- WSASetLastError (0);
- if (r > 0 || error == WSAENOTCONN)
- happened |= (POLLIN | POLLRDNORM) & sought;
-
- else if (r == 0 || error == WSAESHUTDOWN || error == WSAECONNRESET
- || error == WSAECONNABORTED || error == WSAENETRESET)
- happened |= POLLHUP;
- else
- happened |= POLLERR;
- }
- if (lNetworkEvents & (FD_WRITE | FD_CONNECT))
- happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
- if (lNetworkEvents & FD_OOB)
- happened |= (POLLPRI | POLLRDBAND) & sought;
- return happened;
- }
- #else
- static int
- compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds)
- {
- int happened = 0;
- if (FD_ISSET (fd, rfds))
- {
- int r;
- int socket_errno;
- # if defined __MACH__ && defined __APPLE__
-
- r = recv (fd, NULL, 0, MSG_PEEK);
- socket_errno = (r < 0) ? errno : 0;
- if (r == 0 || socket_errno == ENOTSOCK)
- ioctl (fd, FIONREAD, &r);
- # else
- char data[64];
- r = recv (fd, data, sizeof (data), MSG_PEEK);
- socket_errno = (r < 0) ? errno : 0;
- # endif
- if (r == 0)
- happened |= POLLHUP;
-
- else if (r > 0 || ( socket_errno == ENOTCONN))
- happened |= (POLLIN | POLLRDNORM) & sought;
-
- else if (socket_errno == ESHUTDOWN || socket_errno == ECONNRESET
- || socket_errno == ECONNABORTED || socket_errno == ENETRESET)
- happened |= POLLHUP;
-
- else if (socket_errno == ENOTSOCK)
- happened |= (POLLIN | POLLRDNORM) & sought;
- else
- happened |= POLLERR;
- }
- if (FD_ISSET (fd, wfds))
- happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
- if (FD_ISSET (fd, efds))
- happened |= (POLLPRI | POLLRDBAND) & sought;
- return happened;
- }
- #endif
- int
- poll (struct pollfd *pfd, nfds_t nfd, int timeout)
- {
- #ifndef WINDOWS_NATIVE
- fd_set rfds, wfds, efds;
- struct timeval tv;
- struct timeval *ptv;
- int maxfd, rc;
- nfds_t i;
- if (nfd < 0)
- {
- errno = EINVAL;
- return -1;
- }
-
-
- if (!pfd && nfd)
- {
- errno = EFAULT;
- return -1;
- }
-
- if (timeout == 0)
- {
- ptv = &tv;
- ptv->tv_sec = 0;
- ptv->tv_usec = 0;
- }
- else if (timeout > 0)
- {
- ptv = &tv;
- ptv->tv_sec = timeout / 1000;
- ptv->tv_usec = (timeout % 1000) * 1000;
- }
- else if (timeout == INFTIM)
-
- ptv = NULL;
- else
- {
- errno = EINVAL;
- return -1;
- }
-
- maxfd = -1;
- FD_ZERO (&rfds);
- FD_ZERO (&wfds);
- FD_ZERO (&efds);
- for (i = 0; i < nfd; i++)
- {
- if (pfd[i].fd < 0)
- continue;
- if (maxfd < pfd[i].fd)
- {
- maxfd = pfd[i].fd;
- if (FD_SETSIZE <= maxfd)
- {
- errno = EINVAL;
- return -1;
- }
- }
- if (pfd[i].events & (POLLIN | POLLRDNORM))
- FD_SET (pfd[i].fd, &rfds);
-
- if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
- FD_SET (pfd[i].fd, &wfds);
- if (pfd[i].events & (POLLPRI | POLLRDBAND))
- FD_SET (pfd[i].fd, &efds);
- }
-
- rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv);
- if (rc < 0)
- return rc;
-
- rc = 0;
- for (i = 0; i < nfd; i++)
- {
- pfd[i].revents = (pfd[i].fd < 0
- ? 0
- : compute_revents (pfd[i].fd, pfd[i].events,
- &rfds, &wfds, &efds));
- rc += pfd[i].revents != 0;
- }
- return rc;
- #else
- static struct timeval tv0;
- static HANDLE hEvent;
- WSANETWORKEVENTS ev;
- HANDLE h, handle_array[FD_SETSIZE + 2];
- DWORD ret, wait_timeout, nhandles;
- fd_set rfds, wfds, xfds;
- BOOL poll_again;
- MSG msg;
- int rc = 0;
- nfds_t i;
- if (nfd < 0 || timeout < -1)
- {
- errno = EINVAL;
- return -1;
- }
- if (!hEvent)
- hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
- restart:
- handle_array[0] = hEvent;
- nhandles = 1;
- FD_ZERO (&rfds);
- FD_ZERO (&wfds);
- FD_ZERO (&xfds);
-
- for (i = 0; i < nfd; i++)
- {
- int sought = pfd[i].events;
- pfd[i].revents = 0;
- if (pfd[i].fd < 0)
- continue;
- if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND
- | POLLPRI | POLLRDBAND)))
- continue;
- h = (HANDLE) _get_osfhandle (pfd[i].fd);
- assure (h != NULL);
- if (IsSocketHandle (h))
- {
- int requested = FD_CLOSE;
-
- if (sought & (POLLIN | POLLRDNORM))
- {
- requested |= FD_READ | FD_ACCEPT;
- FD_SET ((SOCKET) h, &rfds);
- }
- if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND))
- {
- requested |= FD_WRITE | FD_CONNECT;
- FD_SET ((SOCKET) h, &wfds);
- }
- if (sought & (POLLPRI | POLLRDBAND))
- {
- requested |= FD_OOB;
- FD_SET ((SOCKET) h, &xfds);
- }
- if (requested)
- WSAEventSelect ((SOCKET) h, hEvent, requested);
- }
- else
- {
-
- pfd[i].revents = windows_compute_revents (h, &sought);
- if (sought)
- handle_array[nhandles++] = h;
- if (pfd[i].revents)
- timeout = 0;
- }
- }
- if (select (0, &rfds, &wfds, &xfds, &tv0) > 0)
- {
-
- poll_again = FALSE;
- wait_timeout = 0;
- }
- else
- {
- poll_again = TRUE;
- if (timeout == INFTIM)
- wait_timeout = INFINITE;
- else
- wait_timeout = timeout;
- }
- for (;;)
- {
- ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE,
- wait_timeout, QS_ALLINPUT);
- if (ret == WAIT_OBJECT_0 + nhandles)
- {
-
- BOOL bRet;
- while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0)
- {
- TranslateMessage (&msg);
- DispatchMessage (&msg);
- }
- }
- else
- break;
- }
- if (poll_again)
- select (0, &rfds, &wfds, &xfds, &tv0);
-
- handle_array[nhandles] = NULL;
- nhandles = 1;
- for (i = 0; i < nfd; i++)
- {
- int happened;
- if (pfd[i].fd < 0)
- continue;
- if (!(pfd[i].events & (POLLIN | POLLRDNORM |
- POLLOUT | POLLWRNORM | POLLWRBAND)))
- continue;
- h = (HANDLE) _get_osfhandle (pfd[i].fd);
- if (h != handle_array[nhandles])
- {
-
- WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev);
- WSAEventSelect ((SOCKET) h, 0, 0);
-
- if (FD_ISSET ((SOCKET) h, &rfds)
- && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT)))
- ev.lNetworkEvents |= FD_READ | FD_ACCEPT;
- if (FD_ISSET ((SOCKET) h, &wfds))
- ev.lNetworkEvents |= FD_WRITE | FD_CONNECT;
- if (FD_ISSET ((SOCKET) h, &xfds))
- ev.lNetworkEvents |= FD_OOB;
- happened = windows_compute_revents_socket ((SOCKET) h, pfd[i].events,
- ev.lNetworkEvents);
- }
- else
- {
-
- int sought = pfd[i].events;
- happened = windows_compute_revents (h, &sought);
- nhandles++;
- }
- if ((pfd[i].revents |= happened) != 0)
- rc++;
- }
- if (!rc && timeout == INFTIM)
- {
- SleepEx (1, TRUE);
- goto restart;
- }
- return rc;
- #endif
- }
|