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