1 /*****************************************************************************
2 * poll.c: poll() emulation
3 *****************************************************************************
4 * Copyright © 2007-2012 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
31 /* Too late for #undef FD_SETSIZE to work: fd_set is already defined. */
32 # error Header inclusion order compromised!
35 # include <winsock2.h>
37 # include <sys/time.h>
38 # include <sys/select.h>
42 int (poll) (struct pollfd *fds, unsigned nfds, int timeout)
45 size_t setsize = sizeof (fd_set) + nfds * sizeof (SOCKET);
46 fd_set *rdset = malloc (setsize);
47 fd_set *wrset = malloc (setsize);
48 fd_set *exset = malloc (setsize);
50 if (rdset == NULL || wrset == NULL || exset == NULL)
58 /* Winsock FD_SET uses FD_SETSIZE in its expansion */
60 # define FD_SETSIZE (nfds)
62 fd_set rdset[1], wrset[1], exset[1];
64 struct timeval tv = { 0, 0 };
70 for (unsigned i = 0; i < nfds; i++)
76 /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
77 * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
78 * uses poll() rather than select(). Most POSIX systems implement
79 * fd_set has a bit field with no sanity checks. This is especially bad
80 * on systems (such as BSD) that have no process open files limit by
81 * default, such that it is quite feasible to get fd >= FD_SETSIZE.
82 * The next instructions will result in a buffer overflow if run on
83 * a POSIX system, and the later FD_ISSET would perform an undefined
86 * With Winsock, fd_set is a table of integers. This is awfully slow.
87 * However, FD_SET and FD_ISSET silently and safely discard excess
88 * entries. Here, overflow cannot happen anyway: fd_set of adequate
90 * Note that Vista has a much nicer WSAPoll(), but Mingw does not
94 if ((unsigned)fd >= FD_SETSIZE)
100 if (fds[i].events & POLLIN)
102 if (fds[i].events & POLLOUT)
104 if (fds[i].events & POLLPRI)
110 div_t d = div (timeout, 1000);
112 tv.tv_usec = d.rem * 1000;
115 val = select (val + 1, rdset, wrset, exset,
116 (timeout >= 0) ? &tv : NULL);
122 if (WSAGetLastError () != WSAENOTSOCK)
128 for (unsigned i = 0; i < nfds; i++)
130 if (fcntl (fds[i].fd, F_GETFD) == -1)
132 if (getsockopt (fds[i].fd, SOL_SOCKET, SO_REUSEADDR,
133 &(DWORD){ 0 }, &(int){ sizeof (DWORD) }) != 0)
136 fds[i].revents = POLLNVAL;
142 return val ? val : -1;
145 for (unsigned i = 0; i < nfds; i++)
148 fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
149 | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
150 | (FD_ISSET (fd, exset) ? POLLPRI : 0);