]> git.sesse.net Git - vlc/blob - src/network/poll.c
Win32: fix poll() with more than 64 sockets
[vlc] / src / network / poll.c
1 /*****************************************************************************
2  * poll.c: I/O event multiplexing
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
7  * Author: Rémi Denis-Courmont
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <stdlib.h>
30 #include <vlc_network.h>
31
32
33 #ifdef HAVE_MAEMO
34 # include <signal.h>
35 # include <errno.h>
36 # include <poll.h>
37
38 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
39 {
40     struct timespec tsbuf, *ts;
41     sigset_t set;
42     int canc, ret;
43
44     if (timeout != -1)
45     {
46         div_t d = div (timeout, 1000);
47         tsbuf.tv_sec = d.quot;
48         tsbuf.tv_nsec = d.rem * 1000000;
49         ts = &tsbuf;
50     }
51     else
52         ts = NULL;
53
54     pthread_sigmask (SIG_BLOCK, NULL, &set);
55     sigdelset (&set, SIGRTMIN);
56
57     canc = vlc_savecancel ();
58     ret = ppoll (fds, nfds, ts, &set);
59     vlc_restorecancel (canc);
60
61     vlc_testcancel ();
62     return ret;
63 }
64
65 #elif defined (HAVE_POLL)
66 struct pollfd;
67
68 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
69 {
70     (void)fds; (void)nfds; (void)timeout;
71     abort ();
72 }
73
74 #elif defined (WIN32)
75
76 #include <string.h>
77 #include <errno.h>
78
79 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
80 {
81     size_t setsize = sizeof (fd_set) + (nfds - FD_SETSIZE) * sizeof (SOCKET);
82     fd_set *rdset = malloc (setsize);
83     fd_set *wrset = malloc (setsize);
84     fd_set *exset = malloc (setsize);
85     struct timeval tv = { 0, 0 };
86     int val;
87
88     if (unlikely(rdset == NULL || wrset == NULL || exset == NULL))
89     {
90         free (rdset);
91         free (wrset);
92         free (exset);
93         errno = ENOMEM;
94         return -1;
95     }
96
97 resume:
98     val = -1;
99     vlc_testcancel ();
100
101     FD_ZERO (rdset);
102     FD_ZERO (wrset);
103     FD_ZERO (exset);
104     for (unsigned i = 0; i < nfds; i++)
105     {
106         int fd = fds[i].fd;
107         if (val < fd)
108             val = fd;
109
110         /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
111          * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
112          * uses poll() rather than select(). Most POSIX systems implement
113          * fd_set has a bit field with no sanity checks. This is especially bad
114          * on systems (such as BSD) that have no process open files limit by
115          * default, such that it is quite feasible to get fd >= FD_SETSIZE.
116          * The next instructions will result in a buffer overflow if run on
117          * a POSIX system, and the later FD_ISSET would perform an undefined
118          * memory read.
119          *
120          * With Winsock, fd_set is a table of integers. This is awfully slow.
121          * However, FD_SET and FD_ISSET silently and safely discard excess
122          * entries. Here, overflow cannot happen anyway: fd_set of adequate
123          * size are allocated.
124          * Note that Vista has a much nicer WSAPoll(), but Mingw does not
125          * support it yet.
126          */
127         if (fds[i].events & POLLIN)
128             FD_SET ((SOCKET)fd, rdset);
129         if (fds[i].events & POLLOUT)
130             FD_SET ((SOCKET)fd, wrset);
131         if (fds[i].events & POLLPRI)
132             FD_SET ((SOCKET)fd, exset);
133     }
134
135 #ifndef HAVE_ALERTABLE_SELECT
136 # warning FIXME! Fix cancellation and remove this crap.
137     if ((timeout < 0) || (timeout > 50))
138     {
139         tv.tv_sec = 0;
140         tv.tv_usec = 50000;
141     }
142     else
143 #endif
144     if (timeout >= 0)
145     {
146         div_t d = div (timeout, 1000);
147         tv.tv_sec = d.quot;
148         tv.tv_usec = d.rem * 1000;
149     }
150
151     val = select (val + 1, rdset, wrset, exset,
152                   /*(timeout >= 0) ?*/ &tv /*: NULL*/);
153
154 #ifndef HAVE_ALERTABLE_SELECT
155     if (val == 0)
156     {
157         if (timeout > 0)
158             timeout -= (timeout > 50) ? 50 : timeout;
159         if (timeout != 0)
160             goto resume;
161     }
162 #endif
163
164     if (val == -1)
165         return -1;
166
167     for (unsigned i = 0; i < nfds; i++)
168     {
169         int fd = fds[i].fd;
170         fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
171                        | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
172                        | (FD_ISSET (fd, exset) ? POLLPRI : 0);
173     }
174     free (exset);
175     free (wrset);
176     free (rdset);
177     return val;
178 }
179
180 #else
181 # error poll() not implemented!
182 #endif