4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
31 * Emulation of select() system call using poll() system call.
34 * polling for input only is most common.
35 * polling for exceptional conditions is very rare.
37 * Note that is it not feasible to emulate all error conditions,
38 * in particular conditions that would return EFAULT are far too
39 * difficult to check for in a library routine.
42 #pragma weak _select = select
50 #include <sys/types.h>
51 #include <sys/select.h>
59 * The limit at which pselect allocates pollfd structures in the heap,
60 * rather than on the stack. These limits match the historical behaviour
61 * with the * _large_fdset implementations.
65 * The limit below which we'll just allocate nfds pollfds, rather than
66 * counting how many we actually need.
69 #define STACK_PFD_LIM FD_SETSIZE
70 #define BULK_ALLOC_LIM 8192
72 #define STACK_PFD_LIM 1024
73 #define BULK_ALLOC_LIM 1024
77 * The previous _large_fdset implementations are, unfortunately, baked into
80 #pragma weak select_large_fdset = select
81 #pragma weak pselect_large_fdset = pselect
83 #define fd_set_size(nfds) (((nfds) + (NFDBITS - 1)) / NFDBITS)
86 fd_sets_count(int limit
, fd_set
*in
, fd_set
*out
, fd_set
*ex
)
93 for (int i
= 0; i
< fd_set_size(limit
); i
++) {
94 long v
= (in
->fds_bits
[i
] | out
->fds_bits
[i
] | ex
->fds_bits
[i
]);
106 pselect(int nfds
, fd_set
*in0
, fd_set
*out0
, fd_set
*ex0
,
107 const timespec_t
*tsp
, const sigset_t
*sigmask
)
110 ulong_t m
; /* bit mask */
111 int j
; /* loop counter */
112 ulong_t b
; /* bits to test */
118 boolean_t heap_pfds
= B_FALSE
;
120 /* "zero" is read-only, it could go in the text segment */
121 static fd_set zero
= { 0 };
124 * Check for invalid conditions at outset.
125 * Required for spec1170.
126 * SUSV3: We must behave as a cancellation point even if we fail early.
128 if (nfds
< 0 || nfds
> FD_SETSIZE
) {
129 pthread_testcancel();
135 /* check timespec validity */
136 if (tsp
->tv_nsec
< 0 || tsp
->tv_nsec
>= NANOSEC
||
138 pthread_testcancel();
145 * If any input args are null, point them at the null array.
154 if (nfds
<= BULK_ALLOC_LIM
) {
155 p
= pfd
= alloca(nfds
* sizeof (struct pollfd
));
157 npfds
= fd_sets_count(nfds
, in0
, out0
, ex0
);
159 if (npfds
> STACK_PFD_LIM
) {
160 p
= pfd
= malloc(npfds
* sizeof (struct pollfd
));
165 p
= pfd
= alloca(npfds
* sizeof (struct pollfd
));
170 * For each fd, if any bits are set convert them into
171 * the appropriate pollfd struct.
173 in
= (long *)in0
->fds_bits
;
174 out
= (long *)out0
->fds_bits
;
175 ex
= (long *)ex0
->fds_bits
;
176 for (n
= 0; n
< nfds
; n
+= NFDBITS
) {
177 b
= (ulong_t
)(*in
| *out
| *ex
);
178 for (j
= 0, m
= 1; b
!= 0; j
++, b
>>= 1, m
<<= 1) {
185 p
->events
|= POLLRDNORM
;
187 p
->events
|= POLLWRNORM
;
189 p
->events
|= POLLRDBAND
;
201 npfds
= (int)(p
- pfd
);
203 rv
= _pollsys(pfd
, npfds
, tsp
, sigmask
);
204 } while (rv
< 0 && errno
== EAGAIN
);
206 if (rv
< 0) /* no need to set bit masks */
211 * Clear out bit masks, just in case.
212 * On the assumption that usually only
213 * one bit mask is set, use three loops.
216 in
= (long *)in0
->fds_bits
;
217 for (n
= 0; n
< nfds
; n
+= NFDBITS
)
221 out
= (long *)out0
->fds_bits
;
222 for (n
= 0; n
< nfds
; n
+= NFDBITS
)
226 ex
= (long *)ex0
->fds_bits
;
227 for (n
= 0; n
< nfds
; n
+= NFDBITS
)
235 * Check for EINVAL error case first to avoid changing any bits
236 * if we're going to return an error.
238 for (p
= pfd
, n
= npfds
; n
-- > 0; p
++) {
240 * select will return EBADF immediately if any fd's
241 * are bad. poll will complete the poll on the
242 * rest of the fd's and include the error indication
243 * in the returned bits. This is a rare case so we
244 * accept this difference and return the error after
245 * doing more work than select would've done.
247 if (p
->revents
& POLLNVAL
) {
253 * We would like to make POLLHUP available to select,
254 * checking to see if we have pending data to be read.
255 * BUT until we figure out how not to break Xsun's
256 * dependencies on select's existing features...
257 * This is what we _thought_ would work ... sigh!
260 * if ((p->revents & POLLHUP) &&
261 * !(p->revents & (POLLRDNORM|POLLRDBAND))) {
270 * Convert results of poll back into bits
271 * in the argument arrays.
273 * We assume POLLRDNORM, POLLWRNORM, and POLLRDBAND will only be set
274 * on return from poll if they were set on input, thus we don't
275 * worry about accidentally setting the corresponding bits in the
276 * zero array if the input bit masks were null.
278 * Must return number of bits set, not number of ready descriptors
279 * (as the man page says, and as poll() does).
282 for (p
= pfd
, n
= npfds
; n
-- > 0; p
++) {
283 j
= (int)(p
->fd
/ NFDBITS
);
284 /* have we moved into another word of the bit mask yet? */
286 /* clear all output bits to start with */
287 in
= (long *)&in0
->fds_bits
[j
];
288 out
= (long *)&out0
->fds_bits
[j
];
289 ex
= (long *)&ex0
->fds_bits
[j
];
291 * In case we made "zero" read-only (e.g., with
292 * cc -R), avoid actually storing into it.
303 m
= 1L << (p
->fd
% NFDBITS
);
304 if (p
->revents
& POLLRDNORM
) {
308 if (p
->revents
& POLLWRNORM
) {
312 if (p
->revents
& POLLRDBAND
) {
317 * Only set this bit on return if we asked about
320 if ((p
->revents
& (POLLHUP
|POLLERR
)) &&
321 (p
->events
& POLLRDNORM
)) {
323 rv
++; /* wasn't already set */
327 * Only set this bit on return if we asked about
330 if ((p
->revents
& (POLLHUP
|POLLERR
)) &&
331 (p
->events
& POLLWRNORM
)) {
333 rv
++; /* wasn't already set */
337 * Only set this bit on return if we asked about
340 if ((p
->revents
& (POLLHUP
|POLLERR
)) &&
341 (p
->events
& POLLRDBAND
)) {
343 rv
++; /* wasn't already set */
355 select(int nfds
, fd_set
*in0
, fd_set
*out0
, fd_set
*ex0
, struct timeval
*tv
)
363 /* check timeval validity */
364 if (tv
->tv_usec
< 0 || tv
->tv_usec
>= MICROSEC
) {
369 * Convert timeval to timespec.
370 * To preserve compatibility with past behavior,
371 * when select was built upon poll(2), which has a
372 * minimum non-zero timeout of 1 millisecond, force
373 * a minimum non-zero timeout of 500 microseconds.
375 ts
.tv_sec
= tv
->tv_sec
;
376 ts
.tv_nsec
= tv
->tv_usec
* 1000;
377 if (ts
.tv_nsec
!= 0 && ts
.tv_nsec
< 500000)
382 return (pselect(nfds
, in0
, out0
, ex0
, tsp
, NULL
));