17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5a5c23cccSraf * Common Development and Distribution License (the "License").
6a5c23cccSraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21a5c23cccSraf
227c478bd9Sstevel@tonic-gate /*
238cd45542Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * Emulation of select() system call using poll() system call.
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * Assumptions:
347c478bd9Sstevel@tonic-gate * polling for input only is most common.
357c478bd9Sstevel@tonic-gate * polling for exceptional conditions is very rare.
367c478bd9Sstevel@tonic-gate *
377c478bd9Sstevel@tonic-gate * Note that is it not feasible to emulate all error conditions,
387c478bd9Sstevel@tonic-gate * in particular conditions that would return EFAULT are far too
397c478bd9Sstevel@tonic-gate * difficult to check for in a library routine.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427257d1b4Sraf #pragma weak _select = select
437c478bd9Sstevel@tonic-gate
447257d1b4Sraf #include "lint.h"
457c478bd9Sstevel@tonic-gate #include <values.h>
468cd45542Sraf #include <pthread.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
48*2209d3c8SRichard Lowe #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <sys/select.h>
527c478bd9Sstevel@tonic-gate #include <sys/poll.h>
537c478bd9Sstevel@tonic-gate #include <alloca.h>
547c478bd9Sstevel@tonic-gate #include "libc.h"
557c478bd9Sstevel@tonic-gate
56*2209d3c8SRichard Lowe /*
57*2209d3c8SRichard Lowe * STACK_PFD_LIM
58*2209d3c8SRichard Lowe *
59*2209d3c8SRichard Lowe * The limit at which pselect allocates pollfd structures in the heap,
60*2209d3c8SRichard Lowe * rather than on the stack. These limits match the historical behaviour
61*2209d3c8SRichard Lowe * with the * _large_fdset implementations.
62*2209d3c8SRichard Lowe *
63*2209d3c8SRichard Lowe * BULK_ALLOC_LIM
64*2209d3c8SRichard Lowe *
65*2209d3c8SRichard Lowe * The limit below which we'll just allocate nfds pollfds, rather than
66*2209d3c8SRichard Lowe * counting how many we actually need.
67*2209d3c8SRichard Lowe */
68*2209d3c8SRichard Lowe #if defined(_LP64)
69*2209d3c8SRichard Lowe #define STACK_PFD_LIM FD_SETSIZE
70*2209d3c8SRichard Lowe #define BULK_ALLOC_LIM 8192
71*2209d3c8SRichard Lowe #else
72*2209d3c8SRichard Lowe #define STACK_PFD_LIM 1024
73*2209d3c8SRichard Lowe #define BULK_ALLOC_LIM 1024
74*2209d3c8SRichard Lowe #endif
75*2209d3c8SRichard Lowe
76*2209d3c8SRichard Lowe /*
77*2209d3c8SRichard Lowe * The previous _large_fdset implementations are, unfortunately, baked into
78*2209d3c8SRichard Lowe * the ABI.
79*2209d3c8SRichard Lowe */
80*2209d3c8SRichard Lowe #pragma weak select_large_fdset = select
81*2209d3c8SRichard Lowe #pragma weak pselect_large_fdset = pselect
82*2209d3c8SRichard Lowe
83*2209d3c8SRichard Lowe #define fd_set_size(nfds) (((nfds) + (NFDBITS - 1)) / NFDBITS)
84*2209d3c8SRichard Lowe
85*2209d3c8SRichard Lowe static nfds_t
fd_sets_count(int limit,fd_set * in,fd_set * out,fd_set * ex)86*2209d3c8SRichard Lowe fd_sets_count(int limit, fd_set *in, fd_set *out, fd_set *ex)
87*2209d3c8SRichard Lowe {
88*2209d3c8SRichard Lowe nfds_t total = 0;
89*2209d3c8SRichard Lowe
90*2209d3c8SRichard Lowe if (limit <= 0)
91*2209d3c8SRichard Lowe return (0);
92*2209d3c8SRichard Lowe
93*2209d3c8SRichard Lowe for (int i = 0; i < fd_set_size(limit); i++) {
94*2209d3c8SRichard Lowe long v = (in->fds_bits[i] | out->fds_bits[i] | ex->fds_bits[i]);
95*2209d3c8SRichard Lowe
96*2209d3c8SRichard Lowe while (v != 0) {
97*2209d3c8SRichard Lowe v &= v - 1;
98*2209d3c8SRichard Lowe total++;
99*2209d3c8SRichard Lowe }
100*2209d3c8SRichard Lowe }
101*2209d3c8SRichard Lowe
102*2209d3c8SRichard Lowe return (total);
103*2209d3c8SRichard Lowe }
104*2209d3c8SRichard Lowe
1057c478bd9Sstevel@tonic-gate int
pselect(int nfds,fd_set * in0,fd_set * out0,fd_set * ex0,const timespec_t * tsp,const sigset_t * sigmask)1067c478bd9Sstevel@tonic-gate pselect(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
107*2209d3c8SRichard Lowe const timespec_t *tsp, const sigset_t *sigmask)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate long *in, *out, *ex;
1107c478bd9Sstevel@tonic-gate ulong_t m; /* bit mask */
1117c478bd9Sstevel@tonic-gate int j; /* loop counter */
1127c478bd9Sstevel@tonic-gate ulong_t b; /* bits to test */
1137c478bd9Sstevel@tonic-gate int n, rv;
1147c478bd9Sstevel@tonic-gate struct pollfd *pfd;
1157c478bd9Sstevel@tonic-gate struct pollfd *p;
1167c478bd9Sstevel@tonic-gate int lastj = -1;
117*2209d3c8SRichard Lowe nfds_t npfds = 0;
118*2209d3c8SRichard Lowe boolean_t heap_pfds = B_FALSE;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /* "zero" is read-only, it could go in the text segment */
1217c478bd9Sstevel@tonic-gate static fd_set zero = { 0 };
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * Check for invalid conditions at outset.
1257c478bd9Sstevel@tonic-gate * Required for spec1170.
1267c478bd9Sstevel@tonic-gate * SUSV3: We must behave as a cancellation point even if we fail early.
1277c478bd9Sstevel@tonic-gate */
1287c478bd9Sstevel@tonic-gate if (nfds < 0 || nfds > FD_SETSIZE) {
1298cd45542Sraf pthread_testcancel();
1307c478bd9Sstevel@tonic-gate errno = EINVAL;
1317c478bd9Sstevel@tonic-gate return (-1);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if (tsp != NULL) {
1357c478bd9Sstevel@tonic-gate /* check timespec validity */
1367c478bd9Sstevel@tonic-gate if (tsp->tv_nsec < 0 || tsp->tv_nsec >= NANOSEC ||
1377c478bd9Sstevel@tonic-gate tsp->tv_sec < 0) {
1388cd45542Sraf pthread_testcancel();
1397c478bd9Sstevel@tonic-gate errno = EINVAL;
1407c478bd9Sstevel@tonic-gate return (-1);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate * If any input args are null, point them at the null array.
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate if (in0 == NULL)
1487c478bd9Sstevel@tonic-gate in0 = &zero;
1497c478bd9Sstevel@tonic-gate if (out0 == NULL)
1507c478bd9Sstevel@tonic-gate out0 = &zero;
1517c478bd9Sstevel@tonic-gate if (ex0 == NULL)
1527c478bd9Sstevel@tonic-gate ex0 = &zero;
1537c478bd9Sstevel@tonic-gate
154*2209d3c8SRichard Lowe if (nfds <= BULK_ALLOC_LIM) {
155*2209d3c8SRichard Lowe p = pfd = alloca(nfds * sizeof (struct pollfd));
156*2209d3c8SRichard Lowe } else {
157*2209d3c8SRichard Lowe npfds = fd_sets_count(nfds, in0, out0, ex0);
158*2209d3c8SRichard Lowe
159*2209d3c8SRichard Lowe if (npfds > STACK_PFD_LIM) {
160*2209d3c8SRichard Lowe p = pfd = malloc(npfds * sizeof (struct pollfd));
161*2209d3c8SRichard Lowe if (p == NULL)
162*2209d3c8SRichard Lowe return (-1);
163*2209d3c8SRichard Lowe heap_pfds = B_TRUE;
164*2209d3c8SRichard Lowe } else {
165*2209d3c8SRichard Lowe p = pfd = alloca(npfds * sizeof (struct pollfd));
166*2209d3c8SRichard Lowe }
167*2209d3c8SRichard Lowe }
168*2209d3c8SRichard Lowe
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * For each fd, if any bits are set convert them into
1717c478bd9Sstevel@tonic-gate * the appropriate pollfd struct.
1727c478bd9Sstevel@tonic-gate */
1737c478bd9Sstevel@tonic-gate in = (long *)in0->fds_bits;
1747c478bd9Sstevel@tonic-gate out = (long *)out0->fds_bits;
1757c478bd9Sstevel@tonic-gate ex = (long *)ex0->fds_bits;
1767c478bd9Sstevel@tonic-gate for (n = 0; n < nfds; n += NFDBITS) {
1777c478bd9Sstevel@tonic-gate b = (ulong_t)(*in | *out | *ex);
1787c478bd9Sstevel@tonic-gate for (j = 0, m = 1; b != 0; j++, b >>= 1, m <<= 1) {
1797c478bd9Sstevel@tonic-gate if (b & 1) {
1807c478bd9Sstevel@tonic-gate p->fd = n + j;
1817c478bd9Sstevel@tonic-gate if (p->fd >= nfds)
1827c478bd9Sstevel@tonic-gate goto done;
1837c478bd9Sstevel@tonic-gate p->events = 0;
1847c478bd9Sstevel@tonic-gate if (*in & m)
1857c478bd9Sstevel@tonic-gate p->events |= POLLRDNORM;
1867c478bd9Sstevel@tonic-gate if (*out & m)
1877c478bd9Sstevel@tonic-gate p->events |= POLLWRNORM;
1887c478bd9Sstevel@tonic-gate if (*ex & m)
1897c478bd9Sstevel@tonic-gate p->events |= POLLRDBAND;
1907c478bd9Sstevel@tonic-gate p++;
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate in++;
1947c478bd9Sstevel@tonic-gate out++;
1957c478bd9Sstevel@tonic-gate ex++;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate done:
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * Now do the poll.
2007c478bd9Sstevel@tonic-gate */
201*2209d3c8SRichard Lowe npfds = (int)(p - pfd);
2027c478bd9Sstevel@tonic-gate do {
203*2209d3c8SRichard Lowe rv = _pollsys(pfd, npfds, tsp, sigmask);
2047c478bd9Sstevel@tonic-gate } while (rv < 0 && errno == EAGAIN);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate if (rv < 0) /* no need to set bit masks */
207*2209d3c8SRichard Lowe goto out;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if (rv == 0) {
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate * Clear out bit masks, just in case.
2127c478bd9Sstevel@tonic-gate * On the assumption that usually only
2137c478bd9Sstevel@tonic-gate * one bit mask is set, use three loops.
2147c478bd9Sstevel@tonic-gate */
2157c478bd9Sstevel@tonic-gate if (in0 != &zero) {
2167c478bd9Sstevel@tonic-gate in = (long *)in0->fds_bits;
2177c478bd9Sstevel@tonic-gate for (n = 0; n < nfds; n += NFDBITS)
2187c478bd9Sstevel@tonic-gate *in++ = 0;
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate if (out0 != &zero) {
2217c478bd9Sstevel@tonic-gate out = (long *)out0->fds_bits;
2227c478bd9Sstevel@tonic-gate for (n = 0; n < nfds; n += NFDBITS)
2237c478bd9Sstevel@tonic-gate *out++ = 0;
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate if (ex0 != &zero) {
2267c478bd9Sstevel@tonic-gate ex = (long *)ex0->fds_bits;
2277c478bd9Sstevel@tonic-gate for (n = 0; n < nfds; n += NFDBITS)
2287c478bd9Sstevel@tonic-gate *ex++ = 0;
2297c478bd9Sstevel@tonic-gate }
230*2209d3c8SRichard Lowe rv = 0;
231*2209d3c8SRichard Lowe goto out;
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate * Check for EINVAL error case first to avoid changing any bits
2367c478bd9Sstevel@tonic-gate * if we're going to return an error.
2377c478bd9Sstevel@tonic-gate */
238*2209d3c8SRichard Lowe for (p = pfd, n = npfds; n-- > 0; p++) {
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate * select will return EBADF immediately if any fd's
2417c478bd9Sstevel@tonic-gate * are bad. poll will complete the poll on the
2427c478bd9Sstevel@tonic-gate * rest of the fd's and include the error indication
2437c478bd9Sstevel@tonic-gate * in the returned bits. This is a rare case so we
2447c478bd9Sstevel@tonic-gate * accept this difference and return the error after
2457c478bd9Sstevel@tonic-gate * doing more work than select would've done.
2467c478bd9Sstevel@tonic-gate */
2477c478bd9Sstevel@tonic-gate if (p->revents & POLLNVAL) {
2487c478bd9Sstevel@tonic-gate errno = EBADF;
249*2209d3c8SRichard Lowe rv = -1;
250*2209d3c8SRichard Lowe goto out;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * We would like to make POLLHUP available to select,
2547c478bd9Sstevel@tonic-gate * checking to see if we have pending data to be read.
2557c478bd9Sstevel@tonic-gate * BUT until we figure out how not to break Xsun's
2567c478bd9Sstevel@tonic-gate * dependencies on select's existing features...
2577c478bd9Sstevel@tonic-gate * This is what we _thought_ would work ... sigh!
2587c478bd9Sstevel@tonic-gate */
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate * if ((p->revents & POLLHUP) &&
2617c478bd9Sstevel@tonic-gate * !(p->revents & (POLLRDNORM|POLLRDBAND))) {
2627c478bd9Sstevel@tonic-gate * errno = EINTR;
263*2209d3c8SRichard Lowe * rv = -1;
264*2209d3c8SRichard Lowe * goto out;
2657c478bd9Sstevel@tonic-gate * }
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * Convert results of poll back into bits
2717c478bd9Sstevel@tonic-gate * in the argument arrays.
2727c478bd9Sstevel@tonic-gate *
2737c478bd9Sstevel@tonic-gate * We assume POLLRDNORM, POLLWRNORM, and POLLRDBAND will only be set
2747c478bd9Sstevel@tonic-gate * on return from poll if they were set on input, thus we don't
2757c478bd9Sstevel@tonic-gate * worry about accidentally setting the corresponding bits in the
2767c478bd9Sstevel@tonic-gate * zero array if the input bit masks were null.
2777c478bd9Sstevel@tonic-gate *
2787c478bd9Sstevel@tonic-gate * Must return number of bits set, not number of ready descriptors
2797c478bd9Sstevel@tonic-gate * (as the man page says, and as poll() does).
2807c478bd9Sstevel@tonic-gate */
2817c478bd9Sstevel@tonic-gate rv = 0;
282*2209d3c8SRichard Lowe for (p = pfd, n = npfds; n-- > 0; p++) {
2837c478bd9Sstevel@tonic-gate j = (int)(p->fd / NFDBITS);
2847c478bd9Sstevel@tonic-gate /* have we moved into another word of the bit mask yet? */
2857c478bd9Sstevel@tonic-gate if (j != lastj) {
2867c478bd9Sstevel@tonic-gate /* clear all output bits to start with */
2877c478bd9Sstevel@tonic-gate in = (long *)&in0->fds_bits[j];
2887c478bd9Sstevel@tonic-gate out = (long *)&out0->fds_bits[j];
2897c478bd9Sstevel@tonic-gate ex = (long *)&ex0->fds_bits[j];
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate * In case we made "zero" read-only (e.g., with
2927c478bd9Sstevel@tonic-gate * cc -R), avoid actually storing into it.
2937c478bd9Sstevel@tonic-gate */
2947c478bd9Sstevel@tonic-gate if (in0 != &zero)
2957c478bd9Sstevel@tonic-gate *in = 0;
2967c478bd9Sstevel@tonic-gate if (out0 != &zero)
2977c478bd9Sstevel@tonic-gate *out = 0;
2987c478bd9Sstevel@tonic-gate if (ex0 != &zero)
2997c478bd9Sstevel@tonic-gate *ex = 0;
3007c478bd9Sstevel@tonic-gate lastj = j;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate if (p->revents) {
3037c478bd9Sstevel@tonic-gate m = 1L << (p->fd % NFDBITS);
3047c478bd9Sstevel@tonic-gate if (p->revents & POLLRDNORM) {
3057c478bd9Sstevel@tonic-gate *in |= m;
3067c478bd9Sstevel@tonic-gate rv++;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate if (p->revents & POLLWRNORM) {
3097c478bd9Sstevel@tonic-gate *out |= m;
3107c478bd9Sstevel@tonic-gate rv++;
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate if (p->revents & POLLRDBAND) {
3137c478bd9Sstevel@tonic-gate *ex |= m;
3147c478bd9Sstevel@tonic-gate rv++;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate * Only set this bit on return if we asked about
3187c478bd9Sstevel@tonic-gate * input conditions.
3197c478bd9Sstevel@tonic-gate */
3207c478bd9Sstevel@tonic-gate if ((p->revents & (POLLHUP|POLLERR)) &&
3217c478bd9Sstevel@tonic-gate (p->events & POLLRDNORM)) {
3227c478bd9Sstevel@tonic-gate if ((*in & m) == 0)
3237c478bd9Sstevel@tonic-gate rv++; /* wasn't already set */
3247c478bd9Sstevel@tonic-gate *in |= m;
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate * Only set this bit on return if we asked about
3287c478bd9Sstevel@tonic-gate * output conditions.
3297c478bd9Sstevel@tonic-gate */
3307c478bd9Sstevel@tonic-gate if ((p->revents & (POLLHUP|POLLERR)) &&
3317c478bd9Sstevel@tonic-gate (p->events & POLLWRNORM)) {
3327c478bd9Sstevel@tonic-gate if ((*out & m) == 0)
3337c478bd9Sstevel@tonic-gate rv++; /* wasn't already set */
3347c478bd9Sstevel@tonic-gate *out |= m;
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate * Only set this bit on return if we asked about
3387c478bd9Sstevel@tonic-gate * output conditions.
3397c478bd9Sstevel@tonic-gate */
3407c478bd9Sstevel@tonic-gate if ((p->revents & (POLLHUP|POLLERR)) &&
3417c478bd9Sstevel@tonic-gate (p->events & POLLRDBAND)) {
3427c478bd9Sstevel@tonic-gate if ((*ex & m) == 0)
3437c478bd9Sstevel@tonic-gate rv++; /* wasn't already set */
3447c478bd9Sstevel@tonic-gate *ex |= m;
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate }
348*2209d3c8SRichard Lowe out:
349*2209d3c8SRichard Lowe if (heap_pfds)
350*2209d3c8SRichard Lowe free(pfd);
3517c478bd9Sstevel@tonic-gate return (rv);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate int
select(int nfds,fd_set * in0,fd_set * out0,fd_set * ex0,struct timeval * tv)3557c478bd9Sstevel@tonic-gate select(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0, struct timeval *tv)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate timespec_t ts;
3587c478bd9Sstevel@tonic-gate timespec_t *tsp;
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate if (tv == NULL)
3617c478bd9Sstevel@tonic-gate tsp = NULL;
3627c478bd9Sstevel@tonic-gate else {
3637c478bd9Sstevel@tonic-gate /* check timeval validity */
3647c478bd9Sstevel@tonic-gate if (tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) {
3657c478bd9Sstevel@tonic-gate errno = EINVAL;
3667c478bd9Sstevel@tonic-gate return (-1);
3677c478bd9Sstevel@tonic-gate }
368a5c23cccSraf /*
369a5c23cccSraf * Convert timeval to timespec.
370a5c23cccSraf * To preserve compatibility with past behavior,
371a5c23cccSraf * when select was built upon poll(2), which has a
372a5c23cccSraf * minimum non-zero timeout of 1 millisecond, force
373a5c23cccSraf * a minimum non-zero timeout of 500 microseconds.
374a5c23cccSraf */
3757c478bd9Sstevel@tonic-gate ts.tv_sec = tv->tv_sec;
3767c478bd9Sstevel@tonic-gate ts.tv_nsec = tv->tv_usec * 1000;
377a5c23cccSraf if (ts.tv_nsec != 0 && ts.tv_nsec < 500000)
378a5c23cccSraf ts.tv_nsec = 500000;
3797c478bd9Sstevel@tonic-gate tsp = &ts;
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate return (pselect(nfds, in0, out0, ex0, tsp, NULL));
3837c478bd9Sstevel@tonic-gate }
384