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)))
128 if (maxfd > FD_SETSIZE)
136 /* examine fd sets */
137 rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv);
141 /* establish results */
143 for (i = 0; i < nfd; i++)
148 int happened = 0, sought = pfd[i].events;
149 if (FD_ISSET (pfd[i].fd, &rfds))
153 #if defined __MACH__ && defined __APPLE__
154 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
155 for some kinds of descriptors. Detect if this descriptor is a
156 connected socket, a server socket, or something else using a
157 0-byte recv, and use ioctl(2) to detect POLLHUP. */
158 r = recv (pfd[i].fd, NULL, 0, MSG_PEEK);
159 if (r == 0 || errno == ENOTSOCK)
160 ioctl(pfd[i].fd, FIONREAD, &r);
163 r = recv (pfd[i].fd, data, sizeof (data), MSG_PEEK);
168 /* If the event happened on an unconnected server socket,
170 else if (r > 0 || ( /* (r == -1) && */ errno == ENOTCONN))
171 happened |= (POLLIN | POLLRDNORM) & sought;
173 /* Distinguish hung-up sockets from other errors. */
174 else if (errno == ESHUTDOWN || errno == ECONNRESET
175 || errno == ECONNABORTED || errno == ENETRESET)
182 if (FD_ISSET (pfd[i].fd, &wfds))
183 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought;
185 if (FD_ISSET (pfd[i].fd, &efds))
186 happened |= (POLLPRI | POLLRDBAND) & sought;
190 pfd[i].revents = happened;