]> code.delx.au - pulseaudio/blob - src/pulsecore/poll.c
hack around another OS X bug: recv() with MSG_PEEK does not work
[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
44 #ifdef HAVE_SYS_SELECT_H
45 #include <sys/select.h>
46 #endif
47
48 #include "winsock.h"
49
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 ((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));
112
113 if ((ready == -1) && (errno == EBADF)) {
114 ready = 0;
115
116 FD_ZERO (&rset);
117 FD_ZERO (&wset);
118 FD_ZERO (&xset);
119
120 maxfd = -1;
121
122 for (f = fds; f < &fds[nfds]; ++f) {
123 if (f->fd != -1) {
124 fd_set sngl_rset, sngl_wset, sngl_xset;
125
126 FD_ZERO (&sngl_rset);
127 FD_ZERO (&sngl_wset);
128 FD_ZERO (&sngl_xset);
129
130 if (f->events & POLLIN)
131 FD_SET (f->fd, &sngl_rset);
132 if (f->events & POLLOUT)
133 FD_SET (f->fd, &sngl_wset);
134 if (f->events & POLLPRI)
135 FD_SET (f->fd, &sngl_xset);
136 if (f->events & (POLLIN|POLLOUT|POLLPRI)) {
137 struct timeval singl_tv;
138
139 singl_tv.tv_sec = 0;
140 singl_tv.tv_usec = 0;
141
142 if (select((SELECT_TYPE_ARG1) f->fd, SELECT_TYPE_ARG234 &rset,
143 SELECT_TYPE_ARG234 &wset, SELECT_TYPE_ARG234 &xset,
144 SELECT_TYPE_ARG5 &singl_tv) != -1) {
145 if (f->events & POLLIN)
146 FD_SET (f->fd, &rset);
147 if (f->events & POLLOUT)
148 FD_SET (f->fd, &wset);
149 if (f->events & POLLPRI)
150 FD_SET (f->fd, &xset);
151 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
152 maxfd = f->fd;
153 ++ready;
154 } else if (errno == EBADF)
155 f->revents |= POLLNVAL;
156 }
157 }
158 }
159
160 if (ready) {
161 /* Linux alters the tv struct... but it shouldn't matter here ...
162 * as we're going to be a little bit out anyway as we've just eaten
163 * more than a couple of cpu cycles above */
164 ready = select ((SELECT_TYPE_ARG1) maxfd + 1, SELECT_TYPE_ARG234 &rset,
165 SELECT_TYPE_ARG234 &wset, SELECT_TYPE_ARG234 &xset,
166 SELECT_TYPE_ARG5 (timeout == -1 ? NULL : &tv));
167 }
168 }
169
170 #ifdef OS_IS_WIN32
171 errno = WSAGetLastError();
172 #endif
173
174 if (ready > 0) {
175 int r;
176
177 ready = 0;
178 for (f = fds; f < &fds[nfds]; ++f) {
179 f->revents = 0;
180 if (f->fd != -1) {
181 if (FD_ISSET (f->fd, &rset)) {
182 /* support for POLLHUP. An hung up descriptor does not
183 increase the return value! */
184 #ifdef OS_IS_DARWIN
185 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK
186 * for some kinds of descriptors. Detect if this descriptor is a
187 * connected socket, a server socket, or something else using a
188 * 0-byte recv, and use ioctl(2) to detect POLLHUP. */
189 r = recv(f->fd, NULL, 0, MSG_PEEK);
190 if (r == 0 || (r < 0 && errno == ENOTSOCK))
191 ioctl(f->fd, FIONREAD, &r);
192
193 if (r == 0)
194 f->revents |= POLLHUP;
195 #else /* !OS_IS_DARWIN */
196 if (recv (f->fd, data, 64, MSG_PEEK) == -1) {
197 if (errno == ESHUTDOWN || errno == ECONNRESET ||
198 errno == ECONNABORTED || errno == ENETRESET) {
199 fprintf(stderr, "Hangup\n");
200 f->revents |= POLLHUP;
201 }
202 }
203 #endif
204
205 if (f->revents == 0)
206 f->revents |= POLLIN;
207 }
208 if (FD_ISSET (f->fd, &wset))
209 f->revents |= POLLOUT;
210 if (FD_ISSET (f->fd, &xset))
211 f->revents |= POLLPRI;
212 }
213 if (f->revents)
214 ready++;
215 }
216 }
217
218 return ready;
219 }
220
221 #endif /* HAVE_SYS_POLL_H */