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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 #include <sys/types.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. */
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
)
48 fd_set rfds
, wfds
, xfds
;
50 mach_msg_timeout_t to
= (timeout
!= NULL
?
51 (timeout
->tv_sec
* 1000 +
52 timeout
->tv_usec
/ 1000) :
56 struct hurd_userlink ulink
;
60 mach_port_t reply_port
;
63 /* Use local copies so we can't crash from user bogosity. */
72 if (exceptfds
== NULL
)
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
)
88 if (readfds
!= NULL
&& FD_ISSET (i
, &rfds
))
90 if (writefds
!= NULL
&& FD_ISSET (i
, &wfds
))
92 if (exceptfds
!= NULL
&& FD_ISSET (i
, &xfds
))
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. */
103 _hurd_port_free (&d
[i
].cell
->port
, &d
[i
].ulink
, d
[i
].io_port
);
113 __mutex_unlock (&_hurd_dtable_lock
);
119 /* Send them all io_select request messages. */
122 portset
= MACH_PORT_NULL
;
123 for (i
= firstfd
; i
<= lastfd
; ++i
)
126 int type
= d
[i
].type
;
127 d
[i
].reply_port
= __mach_reply_port ();
128 err
= __io_select (d
[i
].io_port
, d
[i
].reply_port
,
129 /* Poll only when there's a single descriptor. */
130 (firstfd
== lastfd
) ? to
: 0,
134 case MACH_RCV_TIMED_OUT
:
135 /* No immediate response. This is normal. */
137 if (firstfd
== lastfd
)
138 /* When there's a single descriptor, we don't need a portset,
139 so just pretend we have one, but really use the single reply
141 portset
= d
[i
].reply_port
;
143 /* We've got multiple reply ports, so we need a port set to
146 /* We will wait again for a reply later. */
147 if (portset
== MACH_PORT_NULL
)
148 /* Create the portset to receive all the replies on. */
149 err
= __mach_port_allocate (__mach_task_self (),
150 MACH_PORT_RIGHT_PORT_SET
,
153 /* Put this reply port in the port set. */
154 __mach_port_move_member (__mach_task_self (),
155 d
[i
].reply_port
, portset
);
160 /* No other error should happen. Callers of select don't
161 expect to see errors, so we simulate readiness of the erring
162 object and the next call hopefully will get the error again. */
167 /* We got an answer. */
168 if ((type
& SELECT_ALL
) == 0)
169 /* Bogus answer; treat like an error, as a fake positive. */
172 /* This port is already ready already. */
174 d
[i
].type
|= SELECT_RETURNED
;
178 _hurd_port_free (&d
[i
].cell
->port
, &d
[i
].ulink
, d
[i
].io_port
);
181 /* Now wait for reply messages. */
182 if (!err
&& got
== 0)
184 /* Now wait for io_select_reply messages on PORT,
185 timing out as appropriate. */
189 mach_msg_header_t head
;
192 mach_msg_header_t head
;
193 mach_msg_type_t err_type
;
198 mach_msg_header_t head
;
199 mach_msg_type_t err_type
;
201 mach_msg_type_t result_type
;
205 mach_msg_option_t options
= (timeout
== NULL
? 0 : MACH_RCV_TIMEOUT
);
207 while ((msgerr
= __mach_msg (&msg
.head
,
208 MACH_RCV_MSG
| options
,
209 0, sizeof msg
, portset
, to
,
210 MACH_PORT_NULL
)) == MACH_MSG_SUCCESS
)
212 /* We got a message. Decode it. */
213 #define IO_SELECT_REPLY_MSGID (21012 + 100) /* XXX */
214 const mach_msg_type_t inttype
=
215 { MACH_MSG_TYPE_INTEGER_32
, 32, 1, 1, 0, 0 };
216 if (msg
.head
.msgh_id
== IO_SELECT_REPLY_MSGID
&&
217 msg
.head
.msgh_size
>= sizeof msg
.error
&&
218 !(msg
.head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
219 *(int *) &msg
.error
.err_type
== *(int *) &inttype
)
221 /* This is a properly formatted message so far.
222 See if it is a success or a failure. */
223 if (msg
.error
.err
== EINTR
&&
224 msg
.head
.msgh_size
== sizeof msg
.error
)
226 /* EINTR response; poll for further responses
227 and then return quickly. */
232 msg
.head
.msgh_size
!= sizeof msg
.success
||
233 *(int *) &msg
.success
.result_type
!= *(int *) &inttype
||
234 (msg
.success
.result
& SELECT_ALL
) == 0)
236 /* Error or bogus reply. Simulate readiness. */
237 __mach_msg_destroy (&msg
);
238 msg
.success
.result
= SELECT_ALL
;
241 /* Look up the respondant's reply port and record its
245 for (i
= firstfd
; i
<= lastfd
; ++i
)
246 if (d
[i
].type
&& d
[i
].reply_port
== msg
.head
.msgh_local_port
)
248 d
[i
].type
&= msg
.success
.result
;
249 d
[i
].type
|= SELECT_RETURNED
;
256 if (msg
.head
.msgh_remote_port
!= MACH_PORT_NULL
)
257 __mach_port_deallocate (__mach_task_self (),
258 msg
.head
.msgh_remote_port
);
263 /* Poll for another message. */
265 options
|= MACH_RCV_TIMEOUT
;
269 if (err
== MACH_RCV_TIMED_OUT
)
270 /* This is the normal value for ERR. We might have timed out and
271 read no messages. Otherwise, after receiving the first message,
272 we poll for more messages. We receive with a timeout of 0 to
273 effect a poll, so ERR is MACH_RCV_TIMED_OUT when the poll finds no
278 /* At least one descriptor is known to be ready now, so we will
283 for (i
= firstfd
; i
<= lastfd
; ++i
)
285 __mach_port_destroy (__mach_task_self (), d
[i
].reply_port
);
286 if (firstfd
!= lastfd
&& portset
!= MACH_PORT_NULL
)
287 /* Destroy PORTSET, but only if it's not actually the reply port for a
288 single descriptor (in which case it's destroyed in the previous loop;
289 not doing it here is just a bit more efficient). */
290 __mach_port_destroy (__mach_task_self (), portset
);
293 return __hurd_fail (err
);
295 /* Below we recalculate GOT to include an increment for each operation
296 allowed on each fd. */
299 /* Set the user bitarrays. We only ever have to clear bits, as all desired
300 ones are initially set. */
301 for (i
= firstfd
; i
<= lastfd
; ++i
)
303 int type
= d
[i
].type
;
305 if ((type
& SELECT_RETURNED
) == 0)
308 if (type
& SELECT_READ
)
312 if (type
& SELECT_WRITE
)
315 FD_CLR (i
, writefds
);
316 if (type
& SELECT_URG
)
319 FD_CLR (i
, exceptfds
);
325 weak_alias (__select
, select
)