3 This file is part of PulseAudio.
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
24 Based on work for the GNU C Library.
25 Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
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,
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
45 #ifdef HAVE_SYS_SELECT_H
46 #include <sys/select.h>
49 #include <pulsecore/socket.h>
50 #include <pulsecore/core-util.h>
51 #include <pulse/util.h>
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! */
59 #if !defined(HAVE_POLL_H) || defined(OS_IS_DARWIN)
61 int pa_poll (struct pollfd
*fds
, unsigned long int nfds
, int timeout
) {
63 fd_set rset
, wset
, xset
;
83 * Windows does not support signals properly so waiting for them would
89 return select(0, NULL
, NULL
, NULL
, NULL
);
93 for (f
= fds
; f
< &fds
[nfds
]; ++f
) {
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
)))
106 tv
.tv_sec
= timeout
/ 1000;
107 tv
.tv_usec
= (timeout
% 1000) * 1000;
109 ready
= select((SELECT_TYPE_ARG1
) maxfd
+ 1, SELECT_TYPE_ARG234
&rset
,
110 SELECT_TYPE_ARG234
&wset
, SELECT_TYPE_ARG234
&xset
,
111 SELECT_TYPE_ARG5 (timeout
== -1 ? NULL
: &tv
));
113 if ((ready
== -1) && (errno
== EBADF
)) {
119 * Windows has no fcntl(), so we have to trick around with more
120 * select() calls to find out what went wrong
127 for (f
= fds
; f
< &fds
[nfds
]; ++f
) {
129 fd_set sngl_rset
, sngl_wset
, sngl_xset
;
131 FD_ZERO (&sngl_rset
);
132 FD_ZERO (&sngl_wset
);
133 FD_ZERO (&sngl_xset
);
135 if (f
->events
& POLLIN
)
136 FD_SET (f
->fd
, &sngl_rset
);
137 if (f
->events
& POLLOUT
)
138 FD_SET (f
->fd
, &sngl_wset
);
139 if (f
->events
& POLLPRI
)
140 FD_SET (f
->fd
, &sngl_xset
);
141 if (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
)) {
142 struct timeval singl_tv
;
145 singl_tv
.tv_usec
= 0;
147 if (select((SELECT_TYPE_ARG1
) f
->fd
, SELECT_TYPE_ARG234
&rset
,
148 SELECT_TYPE_ARG234
&wset
, SELECT_TYPE_ARG234
&xset
,
149 SELECT_TYPE_ARG5
&singl_tv
) != -1) {
150 if (f
->events
& POLLIN
)
151 FD_SET (f
->fd
, &rset
);
152 if (f
->events
& POLLOUT
)
153 FD_SET (f
->fd
, &wset
);
154 if (f
->events
& POLLPRI
)
155 FD_SET (f
->fd
, &xset
);
156 if (f
->fd
> maxfd
&& (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
)))
159 } else if (errno
== EBADF
)
160 f
->revents
|= POLLNVAL
;
165 #else /* !OS_IS_WIN32 */
167 for (f
= fds
; f
< &fds
[nfds
]; f
++)
169 /* use fcntl() to find out whether the descriptor is valid */
170 if (fcntl(f
->fd
, F_GETFL
) != -1) {
171 if (f
->fd
> maxfd
&& (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
))) {
176 FD_CLR(f
->fd
, &rset
);
177 FD_CLR(f
->fd
, &wset
);
178 FD_CLR(f
->fd
, &xset
);
185 /* Linux alters the tv struct... but it shouldn't matter here ...
186 * as we're going to be a little bit out anyway as we've just eaten
187 * more than a couple of cpu cycles above */
188 ready
= select((SELECT_TYPE_ARG1
) maxfd
+ 1, SELECT_TYPE_ARG234
&rset
,
189 SELECT_TYPE_ARG234
&wset
, SELECT_TYPE_ARG234
&xset
,
190 SELECT_TYPE_ARG5 (timeout
== -1 ? NULL
: &tv
));
195 errno
= WSAGetLastError();
202 for (f
= fds
; f
< &fds
[nfds
]; ++f
) {
205 if (FD_ISSET (f
->fd
, &rset
)) {
206 /* support for POLLHUP. An hung up descriptor does not
207 increase the return value! */
209 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
210 * for some kinds of descriptors. Detect if this descriptor is a
211 * connected socket, a server socket, or something else using a
212 * 0-byte recv, and use ioctl(2) to detect POLLHUP. */
213 r
= recv(f
->fd
, NULL
, 0, MSG_PEEK
);
214 if (r
== 0 || (r
< 0 && errno
== ENOTSOCK
))
215 ioctl(f
->fd
, FIONREAD
, &r
);
218 f
->revents
|= POLLHUP
;
219 #else /* !OS_IS_DARWIN */
220 if (recv (f
->fd
, data
, 64, MSG_PEEK
) == -1) {
221 if (errno
== ESHUTDOWN
|| errno
== ECONNRESET
||
222 errno
== ECONNABORTED
|| errno
== ENETRESET
) {
223 fprintf(stderr
, "Hangup\n");
224 f
->revents
|= POLLHUP
;
230 f
->revents
|= POLLIN
;
232 if (FD_ISSET (f
->fd
, &wset
))
233 f
->revents
|= POLLOUT
;
234 if (FD_ISSET (f
->fd
, &xset
))
235 f
->revents
|= POLLPRI
;
245 #endif /* HAVE_SYS_POLL_H */