Update from main archive 961219
[glibc.git] / sysdeps / mach / hurd / select.c
blobf51551a2f93cce56aa8a365e5f052931799a94a6
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <ansidecl.h>
20 #include <sys/types.h>
21 #include <hurd.h>
22 #include <hurd/fd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 /* All user select types. */
28 #define SELECT_ALL (SELECT_READ | SELECT_WRITE | SELECT_URG)
30 /* Used to record that a particular select rpc returned. Must be distinct
31 from SELECT_ALL (which better not have the high bit set). */
32 #define SELECT_RETURNED ((SELECT_ALL << 1) & ~SELECT_ALL)
34 /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
35 readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
36 (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
37 after waiting the interval specified therein. Returns the number of ready
38 descriptors, or -1 for errors. */
39 int
40 DEFUN(__select, (nfds, readfds, writefds, exceptfds, timeout),
41 int nfds AND fd_set *readfds AND fd_set *writefds AND
42 fd_set *exceptfds AND struct timeval *timeout)
44 int i;
45 mach_port_t portset;
46 int got;
47 error_t err;
48 fd_set rfds, wfds, xfds;
49 int firstfd, lastfd;
50 mach_msg_timeout_t to = (timeout != NULL ?
51 (timeout->tv_sec * 1000 +
52 timeout->tv_usec / 1000) :
53 0);
54 struct
56 struct hurd_userlink ulink;
57 struct hurd_fd *cell;
58 mach_port_t io_port;
59 int type;
60 mach_port_t reply_port;
61 } d[nfds];
63 /* Use local copies so we can't crash from user bogosity. */
64 if (readfds == NULL)
65 FD_ZERO (&rfds);
66 else
67 rfds = *readfds;
68 if (writefds == NULL)
69 FD_ZERO (&wfds);
70 else
71 wfds = *writefds;
72 if (exceptfds == NULL)
73 FD_ZERO (&xfds);
74 else
75 xfds = *exceptfds;
77 HURD_CRITICAL_BEGIN;
78 __mutex_lock (&_hurd_dtable_lock);
80 if (nfds > _hurd_dtablesize)
81 nfds = _hurd_dtablesize;
83 /* Collect the ports for interesting FDs. */
84 firstfd = lastfd = -1;
85 for (i = 0; i < nfds; ++i)
87 int type = 0;
88 if (readfds != NULL && FD_ISSET (i, &rfds))
89 type |= SELECT_READ;
90 if (writefds != NULL && FD_ISSET (i, &wfds))
91 type |= SELECT_WRITE;
92 if (exceptfds != NULL && FD_ISSET (i, &xfds))
93 type |= SELECT_URG;
94 d[i].type = type;
95 if (type)
97 d[i].cell = _hurd_dtable[i];
98 d[i].io_port = _hurd_port_get (&d[i].cell->port, &d[i].ulink);
99 if (d[i].io_port == MACH_PORT_NULL)
101 /* If one descriptor is bogus, we fail completely. */
102 while (i-- > 0)
103 _hurd_port_free (&d[i].cell->port, &d[i].ulink, d[i].io_port);
104 errno = EBADF;
105 break;
107 lastfd = i;
108 if (firstfd == -1)
109 firstfd = i;
113 __mutex_unlock (&_hurd_dtable_lock);
114 HURD_CRITICAL_END;
116 if (i < nfds)
117 return -1;
119 /* Send them all io_select request messages. */
121 if (firstfd == -1)
122 /* But not if there were no ports to deal with at all. */
123 portset = __mach_reply_port ();
124 else
126 err = 0;
127 got = 0;
128 portset = MACH_PORT_NULL;
130 for (i = firstfd; i <= lastfd; ++i)
131 if (d[i].type)
133 int type = d[i].type;
134 d[i].reply_port = __mach_reply_port ();
135 err = __io_select (d[i].io_port, d[i].reply_port,
136 /* Poll only if there's a single descriptor. */
137 (firstfd == lastfd) ? to : 0,
138 &type);
139 switch (err)
141 case MACH_RCV_TIMED_OUT:
142 /* No immediate response. This is normal. */
143 err = 0;
144 if (firstfd == lastfd)
145 /* When there's a single descriptor, we don't need a
146 portset, so just pretend we have one, but really
147 use the single reply port. */
148 portset = d[i].reply_port;
149 else if (got == 0)
150 /* We've got multiple reply ports, so we need a port set to
151 multiplex them. */
153 /* We will wait again for a reply later. */
154 if (portset == MACH_PORT_NULL)
155 /* Create the portset to receive all the replies on. */
156 err = __mach_port_allocate (__mach_task_self (),
157 MACH_PORT_RIGHT_PORT_SET,
158 &portset);
159 if (! err)
160 /* Put this reply port in the port set. */
161 __mach_port_move_member (__mach_task_self (),
162 d[i].reply_port, portset);
164 break;
166 default:
167 /* No other error should happen. Callers of select
168 don't expect to see errors, so we simulate
169 readiness of the erring object and the next call
170 hopefully will get the error again. */
171 type = SELECT_ALL;
172 /* FALLTHROUGH */
174 case 0:
175 /* We got an answer. */
176 if ((type & SELECT_ALL) == 0)
177 /* Bogus answer; treat like an error, as a fake positive. */
178 type = SELECT_ALL;
180 /* This port is already ready already. */
181 d[i].type &= type;
182 d[i].type |= SELECT_RETURNED;
183 ++got;
184 break;
186 _hurd_port_free (&d[i].cell->port, &d[i].ulink, d[i].io_port);
190 /* Now wait for reply messages. */
191 if (!err && got == 0)
193 /* Now wait for io_select_reply messages on PORT,
194 timing out as appropriate. */
196 union
198 mach_msg_header_t head;
199 struct
201 mach_msg_header_t head;
202 mach_msg_type_t err_type;
203 error_t err;
204 } error;
205 struct
207 mach_msg_header_t head;
208 mach_msg_type_t err_type;
209 error_t err;
210 mach_msg_type_t result_type;
211 int result;
212 } success;
213 } msg;
214 mach_msg_option_t options = (timeout == NULL ? 0 : MACH_RCV_TIMEOUT);
215 error_t msgerr;
216 while ((msgerr = __mach_msg (&msg.head,
217 MACH_RCV_MSG | options,
218 0, sizeof msg, portset, to,
219 MACH_PORT_NULL)) == MACH_MSG_SUCCESS)
221 /* We got a message. Decode it. */
222 #define IO_SELECT_REPLY_MSGID (21012 + 100) /* XXX */
223 const mach_msg_type_t inttype =
224 { MACH_MSG_TYPE_INTEGER_32, 32, 1, 1, 0, 0 };
225 if (msg.head.msgh_id == IO_SELECT_REPLY_MSGID &&
226 msg.head.msgh_size >= sizeof msg.error &&
227 !(msg.head.msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
228 *(int *) &msg.error.err_type == *(int *) &inttype)
230 /* This is a properly formatted message so far.
231 See if it is a success or a failure. */
232 if (msg.error.err == EINTR &&
233 msg.head.msgh_size == sizeof msg.error)
235 /* EINTR response; poll for further responses
236 and then return quickly. */
237 err = EINTR;
238 goto poll;
240 if (msg.error.err ||
241 msg.head.msgh_size != sizeof msg.success ||
242 *(int *) &msg.success.result_type != *(int *) &inttype ||
243 (msg.success.result & SELECT_ALL) == 0)
245 /* Error or bogus reply. Simulate readiness. */
246 __mach_msg_destroy (&msg.head);
247 msg.success.result = SELECT_ALL;
250 /* Look up the respondent's reply port and record its
251 readiness. */
253 int had = got;
254 if (firstfd != -1)
255 for (i = firstfd; i <= lastfd; ++i)
256 if (d[i].type
257 && d[i].reply_port == msg.head.msgh_local_port)
259 d[i].type &= msg.success.result;
260 d[i].type |= SELECT_RETURNED;
261 ++got;
263 assert (got > had);
267 if (msg.head.msgh_remote_port != MACH_PORT_NULL)
268 __mach_port_deallocate (__mach_task_self (),
269 msg.head.msgh_remote_port);
271 if (got)
272 poll:
274 /* Poll for another message. */
275 to = 0;
276 options |= MACH_RCV_TIMEOUT;
280 if (err == MACH_RCV_TIMED_OUT)
281 /* This is the normal value for ERR. We might have timed out and
282 read no messages. Otherwise, after receiving the first message,
283 we poll for more messages. We receive with a timeout of 0 to
284 effect a poll, so ERR is MACH_RCV_TIMED_OUT when the poll finds no
285 message waiting. */
286 err = 0;
288 if (got)
289 /* At least one descriptor is known to be ready now, so we will
290 return success. */
291 err = 0;
294 if (firstfd != -1)
295 for (i = firstfd; i <= lastfd; ++i)
296 if (d[i].type)
297 __mach_port_destroy (__mach_task_self (), d[i].reply_port);
298 if (firstfd == -1 || (firstfd != lastfd && portset != MACH_PORT_NULL))
299 /* Destroy PORTSET, but only if it's not actually the reply port for a
300 single descriptor (in which case it's destroyed in the previous loop;
301 not doing it here is just a bit more efficient). */
302 __mach_port_destroy (__mach_task_self (), portset);
304 if (err)
305 return __hurd_fail (err);
307 /* Below we recalculate GOT to include an increment for each operation
308 allowed on each fd. */
309 got = 0;
311 /* Set the user bitarrays. We only ever have to clear bits, as all desired
312 ones are initially set. */
313 if (firstfd != -1)
314 for (i = firstfd; i <= lastfd; ++i)
316 int type = d[i].type;
318 if ((type & SELECT_RETURNED) == 0)
319 type = 0;
321 if (type & SELECT_READ)
322 got++;
323 else if (readfds)
324 FD_CLR (i, readfds);
325 if (type & SELECT_WRITE)
326 got++;
327 else if (writefds)
328 FD_CLR (i, writefds);
329 if (type & SELECT_URG)
330 got++;
331 else if (exceptfds)
332 FD_CLR (i, exceptfds);
335 return got;
338 weak_alias (__select, select)