]> git.sesse.net Git - vlc/blob - compat/poll.c
direct3d11: D3D11_CREATE_DEVICE_DEBUG requires VisualStudio or Windows SDK
[vlc] / compat / poll.c
1 /*****************************************************************************
2  * poll.c: poll() emulation
3  *****************************************************************************
4  * Copyright © 2007-2012 Rémi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #ifdef _WIN32
30 # ifdef FD_SETSIZE
31 /* Too late for #undef FD_SETSIZE to work: fd_set is already defined. */
32 #  error Header inclusion order compromised!
33 # endif
34 # define FD_SETSIZE 0
35 # include <winsock2.h>
36 #else
37 # include <sys/time.h>
38 # include <sys/select.h>
39 # include <fcntl.h>
40 #endif
41
42 int (poll) (struct pollfd *fds, unsigned nfds, int timeout)
43 {
44 #ifdef _WIN32
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);
49
50     if (rdset == NULL || wrset == NULL || exset == NULL)
51     {
52         free (rdset);
53         free (wrset);
54         free (exset);
55         errno = ENOMEM;
56         return -1;
57     }
58 /* Winsock FD_SET uses FD_SETSIZE in its expansion */
59 # undef FD_SETSIZE
60 # define FD_SETSIZE (nfds)
61 #else
62     fd_set rdset[1], wrset[1], exset[1];
63 #endif
64     struct timeval tv = { 0, 0 };
65     int val = -1;
66
67     FD_ZERO (rdset);
68     FD_ZERO (wrset);
69     FD_ZERO (exset);
70     for (unsigned i = 0; i < nfds; i++)
71     {
72         int fd = fds[i].fd;
73         if (val < fd)
74             val = fd;
75
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
84          * memory read.
85          *
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
89          * size are allocated.
90          * Note that Vista has a much nicer WSAPoll(), but Mingw does not
91          * support it yet.
92          */
93 #ifndef _WIN32
94         if ((unsigned)fd >= FD_SETSIZE)
95         {
96             errno = EINVAL;
97             return -1;
98         }
99 #endif
100         if (fds[i].events & POLLIN)
101             FD_SET (fd, rdset);
102         if (fds[i].events & POLLOUT)
103             FD_SET (fd, wrset);
104         if (fds[i].events & POLLPRI)
105             FD_SET (fd, exset);
106     }
107
108     if (timeout >= 0)
109     {
110         div_t d = div (timeout, 1000);
111         tv.tv_sec = d.quot;
112         tv.tv_usec = d.rem * 1000;
113     }
114
115     val = select (val + 1, rdset, wrset, exset,
116                   (timeout >= 0) ? &tv : NULL);
117     if (val == -1)
118     {
119 #ifndef _WIN32
120         if (errno != EBADF)
121 #else
122         if (WSAGetLastError () != WSAENOTSOCK)
123 #endif
124             return -1;
125
126         val = 0;
127
128         for (unsigned i = 0; i < nfds; i++)
129 #ifndef _WIN32
130             if (fcntl (fds[i].fd, F_GETFD) == -1)
131 #else
132             if (getsockopt (fds[i].fd, SOL_SOCKET, SO_REUSEADDR,
133                             &(DWORD){ 0 }, &(int){ sizeof (DWORD) }) != 0)
134 #endif
135             {
136                 fds[i].revents = POLLNVAL;
137                 val++;
138             }
139             else
140                 fds[i].revents = 0;
141
142         return val ? val : -1;
143     }
144
145     for (unsigned i = 0; i < nfds; i++)
146     {
147         int fd = fds[i].fd;
148         fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
149                        | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
150                        | (FD_ISSET (fd, exset) ? POLLPRI : 0);
151     }
152 #ifdef _WIN32
153     free (exset);
154     free (wrset);
155     free (rdset);
156 #endif
157     return val;
158 }