1 /* Emulation for poll(2)
2 Contributed by Paolo Bonzini.
4 Copyright 2001, 2002, 2003, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of gnulib.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/select.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_FILIO_H
45 #include <sys/filio.h>
55 /* BeOS does not have MSG_PEEK. */
62 /* Declare data structures for ntdll functions. */
63 typedef struct _FILE_PIPE_LOCAL_INFORMATION {
65 ULONG NamedPipeConfiguration;
66 ULONG MaximumInstances;
67 ULONG CurrentInstances;
69 ULONG ReadDataAvailable;
71 ULONG WriteQuotaAvailable;
74 } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION;
76 typedef struct _IO_STATUS_BLOCK
82 ULONG_PTR Information;
83 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
85 #define FilePipeLocalInformation 24
87 typedef NTSTATUS (NTAPI *PNtQueryInformationFile)
88 (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS);
94 /* Compute revents values for file handle H. */
97 win32_compute_revents (HANDLE h, int sought)
100 INPUT_RECORD *irbuffer;
101 DWORD avail, nbuffer;
102 IO_STATUS_BLOCK iosb;
103 FILE_PIPE_LOCAL_INFORMATION fpli;
104 static PNtQueryInformationFile NtQueryInformationFile;
106 ret = WaitForSingleObject (h, 0);
107 if (ret != WAIT_OBJECT_0)
108 return sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
110 switch (GetFileType (h))
113 if (!NtQueryInformationFile)
114 NtQueryInformationFile = (PNtQueryInformationFile)
115 GetProcAddress (GetModuleHandle ("ntdll.dll"),
116 "NtQueryInformationFile");
119 if (!PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL))
123 happened |= sought & (POLLIN | POLLRDNORM);
125 memset (&iosb, 0, sizeof (iosb));
126 memset (&fpli, 0, sizeof (fpli));
128 /* If NtQueryInformationFile fails, optimistically assume the pipe is
129 writable. This could happen on Win9x, because NtQueryInformationFile
130 is not available, or if we inherit a pipe that doesn't permit
131 FILE_READ_ATTRIBUTES access on the write end (I think this should
132 not happen since WinXP SP2; WINE seems fine too). Otherwise,
133 ensure that enough space is available for atomic writes. */
134 if (NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
135 FilePipeLocalInformation)
136 || fpli.WriteQuotaAvailable >= PIPE_BUF
137 || (fpli.OutboundQuota < PIPE_BUF &&
138 fpli.WriteQuotaAvailable == fpli.OutboundQuota))
139 happened |= sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
145 bRet = GetNumberOfConsoleInputEvents (h, &nbuffer);
146 if (!bRet || nbuffer == 0)
149 irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD));
150 bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail);
151 if (!bRet || avail == 0)
154 for (i = 0; i < avail; i++)
155 if (irbuffer[i].EventType == KEY_EVENT)
156 return sought & ~(POLLPRI | POLLRDBAND);
158 return sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
161 return sought & ~(POLLPRI | POLLRDBAND);
165 /* Convert fd_sets returned by select into revents values. */
168 win32_compute_revents_socket (SOCKET h, int sought,
169 fd_set *rfds, fd_set *wfds, fd_set *efds)
173 if (FD_ISSET (h, rfds))
179 r = recv (h, data, sizeof (data), MSG_PEEK);
180 error = WSAGetLastError ();
186 /* If the event happened on an unconnected server socket,
188 else if (r > 0 || ( /* (r == -1) && */ error == ENOTCONN))
189 happened |= (POLLIN | POLLRDNORM) & sought;
191 /* Distinguish hung-up sockets from other errors. */
192 else if (error == WSAESHUTDOWN || error == WSAECONNRESET
193 || error == WSAECONNABORTED || error == WSAENETRESET)
200 if (FD_ISSET (h, wfds))
201 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
203 if (FD_ISSET (h, efds))
204 happened |= (POLLPRI | POLLRDBAND) & sought;
211 /* Convert select(2) returned fd_sets into poll(2) revents values. */
213 compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds)
216 if (FD_ISSET (fd, rfds))
221 #if defined __MACH__ && defined __APPLE__
222 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
223 for some kinds of descriptors. Detect if this descriptor is a
224 connected socket, a server socket, or something else using a
225 0-byte recv, and use ioctl(2) to detect POLLHUP. */
226 r = recv (fd, NULL, 0, MSG_PEEK);
227 socket_errno = (r < 0) ? errno : 0;
228 if (r == 0 || socket_errno == ENOTSOCK)
229 ioctl (fd, FIONREAD, &r);
232 r = recv (fd, data, sizeof (data), MSG_PEEK);
233 socket_errno = (r < 0) ? errno : 0;
238 /* If the event happened on an unconnected server socket,
240 else if (r > 0 || ( /* (r == -1) && */ socket_errno == ENOTCONN))
241 happened |= (POLLIN | POLLRDNORM) & sought;
243 /* Distinguish hung-up sockets from other errors. */
244 else if (socket_errno == ESHUTDOWN || socket_errno == ECONNRESET
245 || socket_errno == ECONNABORTED || socket_errno == ENETRESET)
252 if (FD_ISSET (fd, wfds))
253 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
255 if (FD_ISSET (fd, efds))
256 happened |= (POLLPRI | POLLRDBAND) & sought;
263 poll (pfd, nfd, timeout)
269 fd_set rfds, wfds, efds;
276 static int sc_open_max = -1;
279 || (nfd > sc_open_max
280 && (sc_open_max != -1
281 || nfd > (sc_open_max = sysconf (_SC_OPEN_MAX)))))
286 #else /* !_SC_OPEN_MAX */
288 if (nfd < 0 || nfd > OPEN_MAX)
293 #endif /* OPEN_MAX -- else, no check is needed */
294 #endif /* !_SC_OPEN_MAX */
296 /* EFAULT is not necessary to implement, but let's do it in the
304 /* convert timeout number into a timeval structure */
311 else if (timeout > 0)
314 ptv->tv_sec = timeout / 1000;
315 ptv->tv_usec = (timeout % 1000) * 1000;
317 else if (timeout == INFTIM)
326 /* create fd sets and determine max fd */
331 for (i = 0; i < nfd; i++)
336 if (pfd[i].events & (POLLIN | POLLRDNORM))
337 FD_SET (pfd[i].fd, &rfds);
339 /* see select(2): "the only exceptional condition detectable
340 is out-of-band data received on a socket", hence we push
341 POLLWRBAND events onto wfds instead of efds. */
342 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
343 FD_SET (pfd[i].fd, &wfds);
344 if (pfd[i].events & (POLLPRI | POLLRDBAND))
345 FD_SET (pfd[i].fd, &efds);
346 if (pfd[i].fd >= maxfd
347 && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI
348 | POLLRDNORM | POLLRDBAND
349 | POLLWRNORM | POLLWRBAND)))
353 /* Windows use a linear array of sockets (of size FD_SETSIZE). The
354 descriptor value is not used to address the array. */
355 #if defined __CYGWIN__ || (!defined _WIN32 && !defined __WIN32__)
356 if (maxfd > FD_SETSIZE)
365 /* examine fd sets */
366 rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv);
370 /* establish results */
372 for (i = 0; i < nfd; i++)
377 int happened = compute_revents (pfd[i].fd, pfd[i].events,
378 &rfds, &wfds, &efds);
381 pfd[i].revents = happened;
388 fd_set rfds, wfds, efds;
389 static struct timeval tv0;
390 struct timeval tv = { 0, 0 };
392 static HANDLE hEvent;
393 HANDLE handle_array[FD_SET_SIZE + 2];
394 DWORD ret, wait_timeout, nhandles;
402 if (nfd < 0 || nfd > FD_SET_SIZE || timeout < 0)
409 hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
411 handle_array[0] = hEvent;
415 /* Classify socket handles and create fd sets. */
419 for (i = 0; i < nfd; i++)
424 h = (HANDLE) _get_osfhandle (i);
426 optlen = sizeof(sockbuf);
427 if ((getsockopt ((SOCKET) h, SOL_SOCKET, SO_TYPE, sockbuf, &optlen)
429 || WSAGetLastError() != WSAENOTSOCK)
433 /* see above; socket handles are mapped onto select. */
434 if (pfd[i].events & (POLLIN | POLLRDNORM))
436 FD_SET (pfd[i].fd, &rfds);
437 ev |= FD_READ | FD_ACCEPT;
439 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
441 FD_SET (pfd[i].fd, &wfds);
442 ev |= FD_WRITE | FD_CONNECT;
444 if (pfd[i].events & (POLLPRI | POLLRDBAND))
446 FD_SET (pfd[i].fd, &efds);
451 WSAEventSelect ((SOCKET) h, hEvent, ev);
457 if (pfd[i].events & (POLLIN | POLLRDNORM |
458 POLLOUT | POLLWRNORM | POLLWRBAND))
459 handle_array[nhandles++] = h;
463 if (timeout == INFTIM)
464 wait_timeout = INFINITE;
466 wait_timeout = timeout;
470 ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE,
471 wait_timeout, QS_ALLINPUT);
473 if (ret == WAIT_OBJECT_0 + nhandles)
475 /* new input of some other kind */
476 while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0)
478 TranslateMessage (&msg);
479 DispatchMessage (&msg);
486 /* Now check if the sockets have some event set. */
487 select (nsock + 1, rfds, wfds, efds, &tv0);
489 /* Place a sentinel at the end of the array. */
490 handle_array[nhandles] = NULL;
492 for (i = 0; i < nfd; i++)
502 h = (HANDLE) _get_osfhandle (i);
503 if (h != handle_array[nhandles])
506 WSAEventSelect (h, 0, 0);
507 happened = win32_compute_revents_socket ((SOCKET) h, pfd[i].events,
508 &rfds, &wfds, &efds);
514 happened = win32_compute_revents (h, pfd[i].events);
519 pfd[i].revents = happened;