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, see <http://www.gnu.org/licenses/>.
22 Based on work for the GNU C Library.
23 Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
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,
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
43 #ifdef HAVE_SYS_SELECT_H
44 #include <sys/select.h>
47 #include <pulsecore/socket.h>
48 #include <pulsecore/core-util.h>
49 #include <pulse/util.h>
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! */
57 #if !defined(HAVE_POLL_H) || defined(OS_IS_DARWIN)
59 int pa_poll (struct pollfd *fds, unsigned long int nfds, int timeout) {
61 fd_set rset, wset, xset;
81 * Windows does not support signals properly so waiting for them would
87 return select(0, NULL, NULL, NULL, NULL);
91 for (f = fds; f < &fds[nfds]; ++f) {
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)))
104 tv.tv_sec = timeout / 1000;
105 tv.tv_usec = (timeout % 1000) * 1000;
107 ready = select(maxfd + 1, &rset, &wset, &xset, (timeout == -1 ? NULL : &tv));
109 if ((ready == -1) && (errno == EBADF)) {
115 * Windows has no fcntl(), so we have to trick around with more
116 * select() calls to find out what went wrong
123 for (f = fds; f < &fds[nfds]; ++f) {
125 fd_set sngl_rset, sngl_wset, sngl_xset;
127 FD_ZERO (&sngl_rset);
128 FD_ZERO (&sngl_wset);
129 FD_ZERO (&sngl_xset);
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;
141 singl_tv.tv_usec = 0;
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)))
153 } else if (errno == EBADF)
154 f->revents |= POLLNVAL;
159 #else /* !OS_IS_WIN32 */
161 for (f = fds; f < &fds[nfds]; f++)
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))) {
170 FD_CLR(f->fd, &rset);
171 FD_CLR(f->fd, &wset);
172 FD_CLR(f->fd, &xset);
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));
187 errno = WSAGetLastError();
192 for (f = fds; f < &fds[nfds]; ++f) {
195 if (FD_ISSET (f->fd, &rset)) {
196 /* support for POLLHUP. An hung up descriptor does not
197 increase the return value! */
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);
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;
220 f->revents |= POLLIN;
222 if (FD_ISSET (f->fd, &wset))
223 f->revents |= POLLOUT;
224 if (FD_ISSET (f->fd, &xset))
225 f->revents |= POLLPRI;
235 #endif /* HAVE_SYS_POLL_H */