1
2 /***
3 This file is part of PulseAudio.
4
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 /***
22 Based on work for the GNU C Library.
23 Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
24 ***/
25
26 /* Poll the file descriptors described by the NFDS structures starting at
27 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
28 an event to occur; if TIMEOUT is -1, block until an event occurs.
29 Returns the number of file descriptors with events, zero if timed out,
30 or -1 for errors. */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
38 #endif
39
40 #include <errno.h>
41 #include <fcntl.h>
42
43 #ifdef HAVE_SYS_SELECT_H
44 #include <sys/select.h>
45 #endif
46
47 #include <pulsecore/socket.h>
48 #include <pulsecore/core-util.h>
49 #include <pulse/util.h>
50
51 #include "poll.h"
52
53 /* Mac OSX fails to implement poll() in a working way since 10.4. IOW, for
54 * several years. We need to enable a dirty workaround and emulate that call
55 * with select(), just like for Windows. sic! */
56
57 #if !defined(HAVE_POLL_H) || defined(OS_IS_DARWIN)
58
pa_poll(struct pollfd * fds,unsigned long int nfds,int timeout)59 int pa_poll (struct pollfd *fds, unsigned long int nfds, int timeout) {
60 struct timeval tv;
61 fd_set rset, wset, xset;
62 struct pollfd *f;
63 int ready;
64 int maxfd = 0;
65 #ifdef OS_IS_WIN32
66 char data[64];
67 #endif
68
69 FD_ZERO (&rset);
70 FD_ZERO (&wset);
71 FD_ZERO (&xset);
72
73 if (nfds == 0) {
74 if (timeout >= 0) {
75 pa_msleep(timeout);
76 return 0;
77 }
78
79 #ifdef OS_IS_WIN32
80 /*
81 * Windows does not support signals properly so waiting for them would
82 * mean a deadlock.
83 */
84 pa_msleep(100);
85 return 0;
86 #else
87 return select(0, NULL, NULL, NULL, NULL);
88 #endif
89 }
90
91 for (f = fds; f < &fds[nfds]; ++f) {
92 if (f->fd != -1) {
93 if (f->events & POLLIN)
94 FD_SET (f->fd, &rset);
95 if (f->events & POLLOUT)
96 FD_SET (f->fd, &wset);
97 if (f->events & POLLPRI)
98 FD_SET (f->fd, &xset);
99 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
100 maxfd = f->fd;
101 }
102 }
103
104 tv.tv_sec = timeout / 1000;
105 tv.tv_usec = (timeout % 1000) * 1000;
106
107 ready = select(maxfd + 1, &rset, &wset, &xset, (timeout == -1 ? NULL : &tv));
108
109 if ((ready == -1) && (errno == EBADF)) {
110 ready = 0;
111 maxfd = -1;
112
113 #ifdef OS_IS_WIN32
114 /*
115 * Windows has no fcntl(), so we have to trick around with more
116 * select() calls to find out what went wrong
117 */
118
119 FD_ZERO (&rset);
120 FD_ZERO (&wset);
121 FD_ZERO (&xset);
122
123 for (f = fds; f < &fds[nfds]; ++f) {
124 if (f->fd != -1) {
125 fd_set sngl_rset, sngl_wset, sngl_xset;
126
127 FD_ZERO (&sngl_rset);
128 FD_ZERO (&sngl_wset);
129 FD_ZERO (&sngl_xset);
130
131 if (f->events & POLLIN)
132 FD_SET (f->fd, &sngl_rset);
133 if (f->events & POLLOUT)
134 FD_SET (f->fd, &sngl_wset);
135 if (f->events & POLLPRI)
136 FD_SET (f->fd, &sngl_xset);
137 if (f->events & (POLLIN|POLLOUT|POLLPRI)) {
138 struct timeval singl_tv;
139
140 singl_tv.tv_sec = 0;
141 singl_tv.tv_usec = 0;
142
143 if (select(f->fd, &rset, &wset, &xset, &singl_tv) != -1) {
144 if (f->events & POLLIN)
145 FD_SET (f->fd, &rset);
146 if (f->events & POLLOUT)
147 FD_SET (f->fd, &wset);
148 if (f->events & POLLPRI)
149 FD_SET (f->fd, &xset);
150 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
151 maxfd = f->fd;
152 ++ready;
153 } else if (errno == EBADF)
154 f->revents |= POLLNVAL;
155 }
156 }
157 }
158
159 #else /* !OS_IS_WIN32 */
160
161 for (f = fds; f < &fds[nfds]; f++)
162 if (f->fd != -1) {
163 /* use fcntl() to find out whether the descriptor is valid */
164 if (fcntl(f->fd, F_GETFL) != -1) {
165 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI))) {
166 maxfd = f->fd;
167 ready++;
168 }
169 } else {
170 FD_CLR(f->fd, &rset);
171 FD_CLR(f->fd, &wset);
172 FD_CLR(f->fd, &xset);
173 }
174 }
175
176 #endif
177
178 if (ready) {
179 /* Linux alters the tv struct... but it shouldn't matter here ...
180 * as we're going to be a little bit out anyway as we've just eaten
181 * more than a couple of cpu cycles above */
182 ready = select(maxfd + 1, &rset, &wset, &xset, (timeout == -1 ? NULL : &tv));
183 }
184 }
185
186 #ifdef OS_IS_WIN32
187 errno = WSAGetLastError();
188 #endif
189
190 if (ready > 0) {
191 ready = 0;
192 for (f = fds; f < &fds[nfds]; ++f) {
193 f->revents = 0;
194 if (f->fd != -1) {
195 if (FD_ISSET (f->fd, &rset)) {
196 /* support for POLLHUP. An hung up descriptor does not
197 increase the return value! */
198 #ifdef OS_IS_DARWIN
199 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
200 * for some kinds of descriptors. Detect if this descriptor is a
201 * connected socket, a server socket, or something else using a
202 * 0-byte recv, and use ioctl(2) to detect POLLHUP. */
203 int r = recv(f->fd, NULL, 0, MSG_PEEK);
204 if (r == 0 || (r < 0 && errno == ENOTSOCK))
205 ioctl(f->fd, FIONREAD, &r);
206
207 if (r == 0)
208 f->revents |= POLLHUP;
209 #else /* !OS_IS_DARWIN */
210 if (recv (f->fd, data, 64, MSG_PEEK) == -1) {
211 if (errno == ESHUTDOWN || errno == ECONNRESET ||
212 errno == ECONNABORTED || errno == ENETRESET) {
213 fprintf(stderr, "Hangup\n");
214 f->revents |= POLLHUP;
215 }
216 }
217 #endif
218
219 if (f->revents == 0)
220 f->revents |= POLLIN;
221 }
222 if (FD_ISSET (f->fd, &wset))
223 f->revents |= POLLOUT;
224 if (FD_ISSET (f->fd, &xset))
225 f->revents |= POLLPRI;
226 }
227 if (f->revents)
228 ready++;
229 }
230 }
231
232 return ready;
233 }
234
235 #endif /* HAVE_SYS_POLL_H */
236