1 /* Emulation for poll(2)
2 Contributed by Paolo Bonzini.
4 Copyright 2001, 2002, 2003, 2006, 2007 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>
28 #include <sys/socket.h>
29 #include <sys/select.h>
32 #ifdef HAVE_SYS_IOCTL_H
33 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_FILIO_H
36 #include <sys/filio.h>
47 #define EOVERFLOW EINVAL
51 poll (pfd, nfd, timeout)
56 fd_set rfds, wfds, efds;
57 struct timeval tv, *ptv;
62 if (nfd > sysconf (_SC_OPEN_MAX))
67 #else /* !_SC_OPEN_MAX */
74 #endif /* OPEN_MAX -- else, no check is needed */
75 #endif /* !_SC_OPEN_MAX */
77 /* EFAULT is not necessary to implement, but let's do it in the
85 /* convert timeout number into a timeval structure */
89 /* return immediately or after timeout */
90 ptv->tv_sec = timeout / 1000;
91 ptv->tv_usec = (timeout % 1000) * 1000;
93 else if (timeout == INFTIM)
102 /* create fd sets and determine max fd */
107 for (i = 0; i < nfd; i++)
112 if (pfd[i].events & (POLLIN | POLLRDNORM))
113 FD_SET (pfd[i].fd, &rfds);
115 /* see select(2): "the only exceptional condition detectable
116 is out-of-band data received on a socket", hence we push
117 POLLWRBAND events onto wfds instead of efds. */
118 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND))
119 FD_SET (pfd[i].fd, &wfds);
120 if (pfd[i].events & (POLLPRI | POLLRDBAND))
121 FD_SET (pfd[i].fd, &efds);
122 if (pfd[i].fd >= maxfd
123 && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI
124 | POLLRDNORM | POLLRDBAND
125 | POLLWRNORM | POLLWRBAND)))
129 /* Windows use a linear array of sockets (of size FD_SETSIZE). The
130 descriptor value is not used to address the array. */
131 #if defined __CYGWIN__ || (!defined _WIN32 && !defined __WIN32__)
132 if (maxfd > FD_SETSIZE)
141 /* examine fd sets */
142 rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv);
146 /* establish results */
148 for (i = 0; i < nfd; i++)
153 int happened = 0, sought = pfd[i].events;
154 if (FD_ISSET (pfd[i].fd, &rfds))
158 #if defined __MACH__ && defined __APPLE__
159 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
160 for some kinds of descriptors. Detect if this descriptor is a
161 connected socket, a server socket, or something else using a
162 0-byte recv, and use ioctl(2) to detect POLLHUP. */
163 r = recv (pfd[i].fd, NULL, 0, MSG_PEEK);
164 if (r == 0 || errno == ENOTSOCK)
165 ioctl(pfd[i].fd, FIONREAD, &r);
168 r = recv (pfd[i].fd, data, sizeof (data), MSG_PEEK);
173 /* If the event happened on an unconnected server socket,
175 else if (r > 0 || ( /* (r == -1) && */ errno == ENOTCONN))
176 happened |= (POLLIN | POLLRDNORM) & sought;
178 /* Distinguish hung-up sockets from other errors. */
179 else if (errno == ESHUTDOWN || errno == ECONNRESET
180 || errno == ECONNABORTED || errno == ENETRESET)
187 if (FD_ISSET (pfd[i].fd, &wfds))
188 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
190 if (FD_ISSET (pfd[i].fd, &efds))
191 happened |= (POLLPRI | POLLRDBAND) & sought;
195 pfd[i].revents = happened;