X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fpoll.c;h=49c41591efee5f1bd556f1811245f494710668cd;hb=3ac9b4577c78342bdd022b31cf56de2b12f7297b;hp=ffa6ab0b115a29dcf00deeda4bb915c7355cf9f8;hpb=96ee65902e40c618aa07b6652152ecd5674815f2;p=gnulib.git diff --git a/lib/poll.c b/lib/poll.c index ffa6ab0b1..49c41591e 100644 --- a/lib/poll.c +++ b/lib/poll.c @@ -1,7 +1,7 @@ /* Emulation for poll(2) Contributed by Paolo Bonzini. - Copyright 2001, 2002, 2003, 2006, 2007, 2008 Free Software Foundation, Inc. + Copyright 2001-2003, 2006-2010 Free Software Foundation, Inc. This file is part of gnulib. @@ -19,45 +19,70 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +/* Tell gcc not to warn about the (nfd < 0) tests, below. */ +#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__ +# pragma GCC diagnostic ignored "-Wtype-limits" +#endif + #include +#include #include #include "poll.h" #include #include - -#ifdef __MSVCRT__ -#include -#include -#include -#include -#include +#include + +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# define WIN32_NATIVE +# include +# include +# include +# include +# include #else -#include -#include -#include +# include +# include +# include +# include #endif #ifdef HAVE_SYS_IOCTL_H -#include +# include #endif #ifdef HAVE_SYS_FILIO_H -#include +# include #endif -#include #include #ifndef INFTIM -#define INFTIM (-1) +# define INFTIM (-1) #endif /* BeOS does not have MSG_PEEK. */ #ifndef MSG_PEEK -#define MSG_PEEK 0 +# define MSG_PEEK 0 #endif -#ifdef __MSVCRT__ +#ifdef WIN32_NATIVE + +#define IsConsoleHandle(h) (((long) (h) & 3) == 3) + +static BOOL +IsSocketHandle (HANDLE h) +{ + WSANETWORKEVENTS ev; + + if (IsConsoleHandle (h)) + return FALSE; + + /* Under Wine, it seems that getsockopt returns 0 for pipes too. + WSAEnumNetworkEvents instead distinguishes the two correctly. */ + ev.lNetworkEvents = 0xDEADBEEF; + WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); + return ev.lNetworkEvents != 0xDEADBEEF; +} /* Declare data structures for ntdll functions. */ typedef struct _FILE_PIPE_LOCAL_INFORMATION { @@ -75,102 +100,132 @@ typedef struct _FILE_PIPE_LOCAL_INFORMATION { typedef struct _IO_STATUS_BLOCK { - union u { - NTSTATUS Status; + union { + DWORD Status; PVOID Pointer; - }; + } u; ULONG_PTR Information; } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; -#define FilePipeLocalInformation 24 +typedef enum _FILE_INFORMATION_CLASS { + FilePipeLocalInformation = 24 +} FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS; -typedef NTSTATUS (NTAPI *PNtQueryInformationFile) - (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS); +typedef DWORD (WINAPI *PNtQueryInformationFile) + (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS); -#ifndef PIPE_BUF -#define PIPE_BUF 512 -#endif +# ifndef PIPE_BUF +# define PIPE_BUF 512 +# endif -/* Compute revents values for file handle H. */ +/* Compute revents values for file handle H. If some events cannot happen + for the handle, eliminate them from *P_SOUGHT. */ static int -win32_compute_revents (HANDLE h, int sought) +win32_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; - - ret = WaitForSingleObject (h, 0); - if (ret != WAIT_OBJECT_0) - return sought & (POLLOUT | POLLWRNORM | POLLWRBAND); + static BOOL once_only; switch (GetFileType (h)) { case FILE_TYPE_PIPE: - if (!NtQueryInformationFile) - NtQueryInformationFile = (PNtQueryInformationFile) - GetProcAddress (GetModuleHandle ("ntdll.dll"), - "NtQueryInformationFile"); + if (!once_only) + { + NtQueryInformationFile = (PNtQueryInformationFile) + GetProcAddress (GetModuleHandle ("ntdll.dll"), + "NtQueryInformationFile"); + once_only = TRUE; + } happened = 0; - if (!PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL)) - return POLLERR; - - if (avail) - happened |= sought & (POLLIN | POLLRDNORM); - - memset (&iosb, 0, sizeof (iosb)); - memset (&fpli, 0, sizeof (fpli)); - - /* If NtQueryInformationFile fails, optimistically assume the pipe is - writable. This could happen on Win9x, because NtQueryInformationFile - is not available, or if we inherit a pipe that doesn't permit - FILE_READ_ATTRIBUTES access on the write end (I think this should - not happen since WinXP SP2; WINE seems fine too). Otherwise, - ensure that enough space is available for atomic writes. */ - if (NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli), - FilePipeLocalInformation) - || fpli.WriteQuotaAvailable >= PIPE_BUF - || (fpli.OutboundQuota < PIPE_BUF && - fpli.WriteQuotaAvailable == fpli.OutboundQuota)) - happened |= sought & (POLLOUT | POLLWRNORM | POLLWRBAND); + if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0) + { + if (avail) + happened |= *p_sought & (POLLIN | POLLRDNORM); + } + else + { + /* It was the write-end of the pipe. Check if it is writable. + If NtQueryInformationFile fails, optimistically assume the pipe is + writable. This could happen on Win9x, where NtQueryInformationFile + is not available, or if we inherit a pipe that doesn't permit + FILE_READ_ATTRIBUTES access on the write end (I think this should + not happen since WinXP SP2; WINE seems fine too). Otherwise, + ensure that enough space is available for atomic writes. */ + 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 || nbuffer == 0) - return POLLHUP; - - 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 sought & ~(POLLPRI | POLLRDBAND); - - return sought & (POLLOUT | POLLWRNORM | POLLWRBAND); + if (bRet) + { + /* Input buffer. */ + *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 + { + /* Screen buffer. */ + *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND; + return *p_sought; + } default: - return sought & ~(POLLPRI | POLLRDBAND); + ret = WaitForSingleObject (h, 0); + if (ret == WAIT_OBJECT_0) + return *p_sought & ~(POLLPRI | POLLRDBAND); + + return *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND); } } /* Convert fd_sets returned by select into revents values. */ static int -win32_compute_revents_socket (SOCKET h, int sought, - fd_set *rfds, fd_set *wfds, fd_set *efds) +win32_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents) { int happened = 0; - if (FD_ISSET (h, rfds)) + 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; @@ -180,16 +235,11 @@ win32_compute_revents_socket (SOCKET h, int sought, error = WSAGetLastError (); WSASetLastError (0); - if (r == 0) - happened |= POLLHUP; - - /* If the event happened on an unconnected server socket, - that's fine. */ - else if (r > 0 || ( /* (r == -1) && */ error == ENOTCONN)) + if (r > 0 || error == WSAENOTCONN) happened |= (POLLIN | POLLRDNORM) & sought; /* Distinguish hung-up sockets from other errors. */ - else if (error == WSAESHUTDOWN || error == WSAECONNRESET + else if (r == 0 || error == WSAESHUTDOWN || error == WSAECONNRESET || error == WSAECONNABORTED || error == WSAENETRESET) happened |= POLLHUP; @@ -197,10 +247,10 @@ win32_compute_revents_socket (SOCKET h, int sought, happened |= POLLERR; } - if (FD_ISSET (h, wfds)) + if (lNetworkEvents & (FD_WRITE | FD_CONNECT)) happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; - if (FD_ISSET (h, efds)) + if (lNetworkEvents & FD_OOB) happened |= (POLLPRI | POLLRDBAND) & sought; return happened; @@ -212,13 +262,13 @@ win32_compute_revents_socket (SOCKET h, int sought, static int compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds) { - int happened; + int happened = 0; if (FD_ISSET (fd, rfds)) { int r; int socket_errno; -#if defined __MACH__ && defined __APPLE__ +# if defined __MACH__ && defined __APPLE__ /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK for some kinds of descriptors. Detect if this descriptor is a connected socket, a server socket, or something else using a @@ -226,27 +276,27 @@ compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds) r = recv (fd, NULL, 0, MSG_PEEK); socket_errno = (r < 0) ? errno : 0; if (r == 0 || socket_errno == ENOTSOCK) - ioctl (fd, FIONREAD, &r); -#else + ioctl (fd, FIONREAD, &r); +# else char data[64]; r = recv (fd, data, sizeof (data), MSG_PEEK); socket_errno = (r < 0) ? errno : 0; -#endif +# endif if (r == 0) - happened |= POLLHUP; + happened |= POLLHUP; /* If the event happened on an unconnected server socket, that's fine. */ else if (r > 0 || ( /* (r == -1) && */ socket_errno == ENOTCONN)) - happened |= (POLLIN | POLLRDNORM) & sought; + happened |= (POLLIN | POLLRDNORM) & sought; /* Distinguish hung-up sockets from other errors. */ else if (socket_errno == ESHUTDOWN || socket_errno == ECONNRESET - || socket_errno == ECONNABORTED || socket_errno == ENETRESET) - happened |= POLLHUP; + || socket_errno == ECONNABORTED || socket_errno == ENETRESET) + happened |= POLLHUP; else - happened |= POLLERR; + happened |= POLLERR; } if (FD_ISSET (fd, wfds)) @@ -265,33 +315,33 @@ poll (pfd, nfd, timeout) nfds_t nfd; int timeout; { -#ifndef __MSVCRT__ +#ifndef WIN32_NATIVE fd_set rfds, wfds, efds; struct timeval tv; struct timeval *ptv; int maxfd, rc; nfds_t i; -#ifdef _SC_OPEN_MAX +# ifdef _SC_OPEN_MAX static int sc_open_max = -1; if (nfd < 0 || (nfd > sc_open_max && (sc_open_max != -1 - || nfd > (sc_open_max = sysconf (_SC_OPEN_MAX))))) + || nfd > (sc_open_max = sysconf (_SC_OPEN_MAX))))) { errno = EINVAL; return -1; } -#else /* !_SC_OPEN_MAX */ -#ifdef OPEN_MAX +# else /* !_SC_OPEN_MAX */ +# ifdef OPEN_MAX if (nfd < 0 || nfd > OPEN_MAX) { errno = EINVAL; return -1; } -#endif /* OPEN_MAX -- else, no check is needed */ -#endif /* !_SC_OPEN_MAX */ +# endif /* OPEN_MAX -- else, no check is needed */ +# endif /* !_SC_OPEN_MAX */ /* EFAULT is not necessary to implement, but let's do it in the simplest case. */ @@ -331,35 +381,30 @@ poll (pfd, nfd, timeout) for (i = 0; i < nfd; i++) { if (pfd[i].fd < 0) - continue; + continue; if (pfd[i].events & (POLLIN | POLLRDNORM)) - FD_SET (pfd[i].fd, &rfds); + FD_SET (pfd[i].fd, &rfds); /* see select(2): "the only exceptional condition detectable is out-of-band data received on a socket", hence we push POLLWRBAND events onto wfds instead of efds. */ if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) - FD_SET (pfd[i].fd, &wfds); + FD_SET (pfd[i].fd, &wfds); if (pfd[i].events & (POLLPRI | POLLRDBAND)) - FD_SET (pfd[i].fd, &efds); + FD_SET (pfd[i].fd, &efds); if (pfd[i].fd >= maxfd - && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI - | POLLRDNORM | POLLRDBAND - | POLLWRNORM | POLLWRBAND))) - { - maxfd = pfd[i].fd; - - /* Windows use a linear array of sockets (of size FD_SETSIZE). The - descriptor value is not used to address the array. */ -#if defined __CYGWIN__ || (!defined _WIN32 && !defined __WIN32__) - if (maxfd > FD_SETSIZE) - { - errno = EOVERFLOW; - return -1; - } -#endif - } + && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI + | POLLRDNORM | POLLRDBAND + | POLLWRNORM | POLLWRBAND))) + { + maxfd = pfd[i].fd; + if (maxfd > FD_SETSIZE) + { + errno = EOVERFLOW; + return -1; + } + } } /* examine fd sets */ @@ -376,30 +421,27 @@ poll (pfd, nfd, timeout) { int happened = compute_revents (pfd[i].fd, pfd[i].events, &rfds, &wfds, &efds); - if (happened) - { - pfd[i].revents = happened; - rc++; - } + if (happened) + { + pfd[i].revents = happened; + rc++; + } } return rc; #else - fd_set rfds, wfds, efds; static struct timeval tv0; - struct timeval tv = { 0, 0 }; - struct timeval *ptv; static HANDLE hEvent; - HANDLE handle_array[FD_SET_SIZE + 2]; + WSANETWORKEVENTS ev; + HANDLE h, handle_array[FD_SETSIZE + 2]; DWORD ret, wait_timeout, nhandles; - int nsock; - BOOL bRet; + fd_set rfds, wfds, xfds; + BOOL poll_again; MSG msg; - char sockbuf[256]; - int rc; + int rc = 0; nfds_t i; - if (nfd < 0 || nfd > FD_SET_SIZE || timeout < 0) + if (nfd < 0 || timeout < -1) { errno = EINVAL; return -1; @@ -410,81 +452,98 @@ poll (pfd, nfd, timeout) handle_array[0] = hEvent; nhandles = 1; - nsock = 0; - - /* Classify socket handles and create fd sets. */ FD_ZERO (&rfds); FD_ZERO (&wfds); - FD_ZERO (&efds); + FD_ZERO (&xfds); + + /* Classify socket handles and create fd sets. */ 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 (i); + h = (HANDLE) _get_osfhandle (pfd[i].fd); assert (h != NULL); - optlen = sizeof(sockbuf); - if ((getsockopt ((SOCKET) h, SOL_SOCKET, SO_TYPE, sockbuf, &optlen) - != SOCKET_ERROR) - || WSAGetLastError() != WSAENOTSOCK) + if (IsSocketHandle (h)) { - int ev = 0; + int requested = FD_CLOSE; /* see above; socket handles are mapped onto select. */ - if (pfd[i].events & (POLLIN | POLLRDNORM)) - { - FD_SET (pfd[i].fd, &rfds); - ev |= FD_READ | FD_ACCEPT; - } - if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) + if (sought & (POLLIN | POLLRDNORM)) { - FD_SET (pfd[i].fd, &wfds); - ev |= FD_WRITE | FD_CONNECT; + requested |= FD_READ | FD_ACCEPT; + FD_SET ((SOCKET) h, &rfds); } - if (pfd[i].events & (POLLPRI | POLLRDBAND)) + if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND)) { - FD_SET (pfd[i].fd, &efds); - ev |= FD_OOB; + requested |= FD_WRITE | FD_CONNECT; + FD_SET ((SOCKET) h, &wfds); } - if (ev) + if (sought & (POLLPRI | POLLRDBAND)) { - WSAEventSelect ((SOCKET) h, hEvent, ev); - nsock++; + requested |= FD_OOB; + FD_SET ((SOCKET) h, &xfds); } + + if (requested) + WSAEventSelect ((SOCKET) h, hEvent, requested); } else { - if (pfd[i].events & (POLLIN | POLLRDNORM | - POLLOUT | POLLWRNORM | POLLWRBAND)) + /* Poll now. If we get an event, do not poll again. Also, + screen buffer handles are waitable, and they'll block until + a character is available. win32_compute_revents eliminates + bits for the "wrong" direction. */ + pfd[i].revents = win32_compute_revents (h, &sought); + if (sought) handle_array[nhandles++] = h; + if (pfd[i].revents) + wait_timeout = 0; } } - if (timeout == INFTIM) - wait_timeout = INFINITE; + if (select (0, &rfds, &wfds, &xfds, &tv0) > 0) + { + /* Do MsgWaitForMultipleObjects anyway to dispatch messages, but + no need to call select again. */ + poll_again = FALSE; + wait_timeout = 0; + } else - wait_timeout = timeout; + { + poll_again = TRUE; + if (timeout == INFTIM) + wait_timeout = INFINITE; + else + wait_timeout = timeout; + } for (;;) { ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE, - wait_timeout, QS_ALLINPUT); + wait_timeout, QS_ALLINPUT); if (ret == WAIT_OBJECT_0 + nhandles) - { + { /* new input of some other kind */ + BOOL bRet; while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0) { TranslateMessage (&msg); DispatchMessage (&msg); } - } + } else - break; + break; } - /* Now check if the sockets have some event set. */ - select (nsock + 1, rfds, wfds, efds, &tv0); + if (poll_again) + select (0, &rfds, &wfds, &xfds, &tv0); /* Place a sentinel at the end of the array. */ handle_array[nhandles] = NULL; @@ -494,31 +553,41 @@ poll (pfd, nfd, timeout) int happened; if (pfd[i].fd < 0) - { - pfd[i].revents = 0; - continue; - } + continue; + if (!(pfd[i].events & (POLLIN | POLLRDNORM | + POLLOUT | POLLWRNORM | POLLWRBAND))) + continue; - h = (HANDLE) _get_osfhandle (i); + h = (HANDLE) _get_osfhandle (pfd[i].fd); if (h != handle_array[nhandles]) { /* It's a socket. */ - WSAEventSelect (h, 0, 0); + WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); + WSAEventSelect ((SOCKET) h, 0, 0); + + /* If we're lucky, WSAEnumNetworkEvents already provided a way + to distinguish FD_READ and FD_ACCEPT; this saves a recv later. */ + 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 = win32_compute_revents_socket ((SOCKET) h, pfd[i].events, - &rfds, &wfds, &efds); + ev.lNetworkEvents); } else { /* Not a socket. */ + int sought = pfd[i].events; + happened = win32_compute_revents (h, &sought); nhandles++; - happened = win32_compute_revents (h, pfd[i].events); } - if (happened) - { - pfd[i].revents = happened; - rc++; - } + if ((pfd[i].revents |= happened) != 0) + rc++; } return rc;