xref: /onnv-gate/usr/src/lib/libc/port/gen/select.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51778Sraf  * Common Development and Distribution License (the "License").
61778Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211778Sraf 
220Sstevel@tonic-gate /*
236515Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*6812Sraf 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Emulation of select() system call using poll() system call.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * Assumptions:
360Sstevel@tonic-gate  *	polling for input only is most common.
370Sstevel@tonic-gate  *	polling for exceptional conditions is very rare.
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * Note that is it not feasible to emulate all error conditions,
400Sstevel@tonic-gate  * in particular conditions that would return EFAULT are far too
410Sstevel@tonic-gate  * difficult to check for in a library routine.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
44*6812Sraf #pragma weak _select = select
450Sstevel@tonic-gate 
46*6812Sraf #include "lint.h"
470Sstevel@tonic-gate #include <values.h>
486515Sraf #include <pthread.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <sys/time.h>
510Sstevel@tonic-gate #include <sys/types.h>
520Sstevel@tonic-gate #include <sys/select.h>
530Sstevel@tonic-gate #include <sys/poll.h>
540Sstevel@tonic-gate #include <alloca.h>
550Sstevel@tonic-gate #include "libc.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int
pselect(int nfds,fd_set * in0,fd_set * out0,fd_set * ex0,const timespec_t * tsp,const sigset_t * sigmask)580Sstevel@tonic-gate pselect(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
590Sstevel@tonic-gate 	const timespec_t *tsp, const sigset_t *sigmask)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	long *in, *out, *ex;
620Sstevel@tonic-gate 	ulong_t m;	/* bit mask */
630Sstevel@tonic-gate 	int j;		/* loop counter */
640Sstevel@tonic-gate 	ulong_t b;	/* bits to test */
650Sstevel@tonic-gate 	int n, rv;
660Sstevel@tonic-gate 	struct pollfd *pfd;
670Sstevel@tonic-gate 	struct pollfd *p;
680Sstevel@tonic-gate 	int lastj = -1;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/* "zero" is read-only, it could go in the text segment */
710Sstevel@tonic-gate 	static fd_set zero = { 0 };
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/*
740Sstevel@tonic-gate 	 * Check for invalid conditions at outset.
750Sstevel@tonic-gate 	 * Required for spec1170.
760Sstevel@tonic-gate 	 * SUSV3: We must behave as a cancellation point even if we fail early.
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 	if (nfds < 0 || nfds > FD_SETSIZE) {
796515Sraf 		pthread_testcancel();
800Sstevel@tonic-gate 		errno = EINVAL;
810Sstevel@tonic-gate 		return (-1);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	p = pfd = (struct pollfd *)alloca(nfds * sizeof (struct pollfd));
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (tsp != NULL) {
860Sstevel@tonic-gate 		/* check timespec validity */
870Sstevel@tonic-gate 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= NANOSEC ||
880Sstevel@tonic-gate 		    tsp->tv_sec < 0) {
896515Sraf 			pthread_testcancel();
900Sstevel@tonic-gate 			errno = EINVAL;
910Sstevel@tonic-gate 			return (-1);
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/*
960Sstevel@tonic-gate 	 * If any input args are null, point them at the null array.
970Sstevel@tonic-gate 	 */
980Sstevel@tonic-gate 	if (in0 == NULL)
990Sstevel@tonic-gate 		in0 = &zero;
1000Sstevel@tonic-gate 	if (out0 == NULL)
1010Sstevel@tonic-gate 		out0 = &zero;
1020Sstevel@tonic-gate 	if (ex0 == NULL)
1030Sstevel@tonic-gate 		ex0 = &zero;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * For each fd, if any bits are set convert them into
1070Sstevel@tonic-gate 	 * the appropriate pollfd struct.
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	in = (long *)in0->fds_bits;
1100Sstevel@tonic-gate 	out = (long *)out0->fds_bits;
1110Sstevel@tonic-gate 	ex = (long *)ex0->fds_bits;
1120Sstevel@tonic-gate 	for (n = 0; n < nfds; n += NFDBITS) {
1130Sstevel@tonic-gate 		b = (ulong_t)(*in | *out | *ex);
1140Sstevel@tonic-gate 		for (j = 0, m = 1; b != 0; j++, b >>= 1, m <<= 1) {
1150Sstevel@tonic-gate 			if (b & 1) {
1160Sstevel@tonic-gate 				p->fd = n + j;
1170Sstevel@tonic-gate 				if (p->fd >= nfds)
1180Sstevel@tonic-gate 					goto done;
1190Sstevel@tonic-gate 				p->events = 0;
1200Sstevel@tonic-gate 				if (*in & m)
1210Sstevel@tonic-gate 					p->events |= POLLRDNORM;
1220Sstevel@tonic-gate 				if (*out & m)
1230Sstevel@tonic-gate 					p->events |= POLLWRNORM;
1240Sstevel@tonic-gate 				if (*ex & m)
1250Sstevel@tonic-gate 					p->events |= POLLRDBAND;
1260Sstevel@tonic-gate 				p++;
1270Sstevel@tonic-gate 			}
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 		in++;
1300Sstevel@tonic-gate 		out++;
1310Sstevel@tonic-gate 		ex++;
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate done:
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * Now do the poll.
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	n = (int)(p - pfd);		/* number of pollfd's */
1380Sstevel@tonic-gate 	do {
1390Sstevel@tonic-gate 		rv = _pollsys(pfd, (nfds_t)n, tsp, sigmask);
1400Sstevel@tonic-gate 	} while (rv < 0 && errno == EAGAIN);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (rv < 0)		/* no need to set bit masks */
1430Sstevel@tonic-gate 		return (rv);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (rv == 0) {
1460Sstevel@tonic-gate 		/*
1470Sstevel@tonic-gate 		 * Clear out bit masks, just in case.
1480Sstevel@tonic-gate 		 * On the assumption that usually only
1490Sstevel@tonic-gate 		 * one bit mask is set, use three loops.
1500Sstevel@tonic-gate 		 */
1510Sstevel@tonic-gate 		if (in0 != &zero) {
1520Sstevel@tonic-gate 			in = (long *)in0->fds_bits;
1530Sstevel@tonic-gate 			for (n = 0; n < nfds; n += NFDBITS)
1540Sstevel@tonic-gate 				*in++ = 0;
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 		if (out0 != &zero) {
1570Sstevel@tonic-gate 			out = (long *)out0->fds_bits;
1580Sstevel@tonic-gate 			for (n = 0; n < nfds; n += NFDBITS)
1590Sstevel@tonic-gate 				*out++ = 0;
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 		if (ex0 != &zero) {
1620Sstevel@tonic-gate 			ex = (long *)ex0->fds_bits;
1630Sstevel@tonic-gate 			for (n = 0; n < nfds; n += NFDBITS)
1640Sstevel@tonic-gate 				*ex++ = 0;
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 		return (0);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * Check for EINVAL error case first to avoid changing any bits
1710Sstevel@tonic-gate 	 * if we're going to return an error.
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	for (p = pfd, j = n; j-- > 0; p++) {
1740Sstevel@tonic-gate 		/*
1750Sstevel@tonic-gate 		 * select will return EBADF immediately if any fd's
1760Sstevel@tonic-gate 		 * are bad.  poll will complete the poll on the
1770Sstevel@tonic-gate 		 * rest of the fd's and include the error indication
1780Sstevel@tonic-gate 		 * in the returned bits.  This is a rare case so we
1790Sstevel@tonic-gate 		 * accept this difference and return the error after
1800Sstevel@tonic-gate 		 * doing more work than select would've done.
1810Sstevel@tonic-gate 		 */
1820Sstevel@tonic-gate 		if (p->revents & POLLNVAL) {
1830Sstevel@tonic-gate 			errno = EBADF;
1840Sstevel@tonic-gate 			return (-1);
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 		/*
1870Sstevel@tonic-gate 		 * We would like to make POLLHUP available to select,
1880Sstevel@tonic-gate 		 * checking to see if we have pending data to be read.
1890Sstevel@tonic-gate 		 * BUT until we figure out how not to break Xsun's
1900Sstevel@tonic-gate 		 * dependencies on select's existing features...
1910Sstevel@tonic-gate 		 * This is what we _thought_ would work ... sigh!
1920Sstevel@tonic-gate 		 */
1930Sstevel@tonic-gate 		/*
1940Sstevel@tonic-gate 		 * if ((p->revents & POLLHUP) &&
1950Sstevel@tonic-gate 		 *	!(p->revents & (POLLRDNORM|POLLRDBAND))) {
1960Sstevel@tonic-gate 		 *	errno = EINTR;
1970Sstevel@tonic-gate 		 *	return (-1);
1980Sstevel@tonic-gate 		 * }
1990Sstevel@tonic-gate 		 */
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * Convert results of poll back into bits
2040Sstevel@tonic-gate 	 * in the argument arrays.
2050Sstevel@tonic-gate 	 *
2060Sstevel@tonic-gate 	 * We assume POLLRDNORM, POLLWRNORM, and POLLRDBAND will only be set
2070Sstevel@tonic-gate 	 * on return from poll if they were set on input, thus we don't
2080Sstevel@tonic-gate 	 * worry about accidentally setting the corresponding bits in the
2090Sstevel@tonic-gate 	 * zero array if the input bit masks were null.
2100Sstevel@tonic-gate 	 *
2110Sstevel@tonic-gate 	 * Must return number of bits set, not number of ready descriptors
2120Sstevel@tonic-gate 	 * (as the man page says, and as poll() does).
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	rv = 0;
2150Sstevel@tonic-gate 	for (p = pfd; n-- > 0; p++) {
2160Sstevel@tonic-gate 		j = (int)(p->fd / NFDBITS);
2170Sstevel@tonic-gate 		/* have we moved into another word of the bit mask yet? */
2180Sstevel@tonic-gate 		if (j != lastj) {
2190Sstevel@tonic-gate 			/* clear all output bits to start with */
2200Sstevel@tonic-gate 			in = (long *)&in0->fds_bits[j];
2210Sstevel@tonic-gate 			out = (long *)&out0->fds_bits[j];
2220Sstevel@tonic-gate 			ex = (long *)&ex0->fds_bits[j];
2230Sstevel@tonic-gate 			/*
2240Sstevel@tonic-gate 			 * In case we made "zero" read-only (e.g., with
2250Sstevel@tonic-gate 			 * cc -R), avoid actually storing into it.
2260Sstevel@tonic-gate 			 */
2270Sstevel@tonic-gate 			if (in0 != &zero)
2280Sstevel@tonic-gate 				*in = 0;
2290Sstevel@tonic-gate 			if (out0 != &zero)
2300Sstevel@tonic-gate 				*out = 0;
2310Sstevel@tonic-gate 			if (ex0 != &zero)
2320Sstevel@tonic-gate 				*ex = 0;
2330Sstevel@tonic-gate 			lastj = j;
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 		if (p->revents) {
2360Sstevel@tonic-gate 			m = 1L << (p->fd % NFDBITS);
2370Sstevel@tonic-gate 			if (p->revents & POLLRDNORM) {
2380Sstevel@tonic-gate 				*in |= m;
2390Sstevel@tonic-gate 				rv++;
2400Sstevel@tonic-gate 			}
2410Sstevel@tonic-gate 			if (p->revents & POLLWRNORM) {
2420Sstevel@tonic-gate 				*out |= m;
2430Sstevel@tonic-gate 				rv++;
2440Sstevel@tonic-gate 			}
2450Sstevel@tonic-gate 			if (p->revents & POLLRDBAND) {
2460Sstevel@tonic-gate 				*ex |= m;
2470Sstevel@tonic-gate 				rv++;
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 			/*
2500Sstevel@tonic-gate 			 * Only set this bit on return if we asked about
2510Sstevel@tonic-gate 			 * input conditions.
2520Sstevel@tonic-gate 			 */
2530Sstevel@tonic-gate 			if ((p->revents & (POLLHUP|POLLERR)) &&
2540Sstevel@tonic-gate 			    (p->events & POLLRDNORM)) {
2550Sstevel@tonic-gate 				if ((*in & m) == 0)
2560Sstevel@tonic-gate 					rv++;	/* wasn't already set */
2570Sstevel@tonic-gate 				*in |= m;
2580Sstevel@tonic-gate 			}
2590Sstevel@tonic-gate 			/*
2600Sstevel@tonic-gate 			 * Only set this bit on return if we asked about
2610Sstevel@tonic-gate 			 * output conditions.
2620Sstevel@tonic-gate 			 */
2630Sstevel@tonic-gate 			if ((p->revents & (POLLHUP|POLLERR)) &&
2640Sstevel@tonic-gate 			    (p->events & POLLWRNORM)) {
2650Sstevel@tonic-gate 				if ((*out & m) == 0)
2660Sstevel@tonic-gate 					rv++;	/* wasn't already set */
2670Sstevel@tonic-gate 				*out |= m;
2680Sstevel@tonic-gate 			}
2690Sstevel@tonic-gate 			/*
2700Sstevel@tonic-gate 			 * Only set this bit on return if we asked about
2710Sstevel@tonic-gate 			 * output conditions.
2720Sstevel@tonic-gate 			 */
2730Sstevel@tonic-gate 			if ((p->revents & (POLLHUP|POLLERR)) &&
2740Sstevel@tonic-gate 			    (p->events & POLLRDBAND)) {
2750Sstevel@tonic-gate 				if ((*ex & m) == 0)
2760Sstevel@tonic-gate 					rv++;	/* wasn't already set */
2770Sstevel@tonic-gate 				*ex |= m;
2780Sstevel@tonic-gate 			}
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 	return (rv);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate int
select(int nfds,fd_set * in0,fd_set * out0,fd_set * ex0,struct timeval * tv)2850Sstevel@tonic-gate select(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0, struct timeval *tv)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	timespec_t ts;
2880Sstevel@tonic-gate 	timespec_t *tsp;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if (tv == NULL)
2910Sstevel@tonic-gate 		tsp = NULL;
2920Sstevel@tonic-gate 	else {
2930Sstevel@tonic-gate 		/* check timeval validity */
2940Sstevel@tonic-gate 		if (tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) {
2950Sstevel@tonic-gate 			errno = EINVAL;
2960Sstevel@tonic-gate 			return (-1);
2970Sstevel@tonic-gate 		}
2981778Sraf 		/*
2991778Sraf 		 * Convert timeval to timespec.
3001778Sraf 		 * To preserve compatibility with past behavior,
3011778Sraf 		 * when select was built upon poll(2), which has a
3021778Sraf 		 * minimum non-zero timeout of 1 millisecond, force
3031778Sraf 		 * a minimum non-zero timeout of 500 microseconds.
3041778Sraf 		 */
3050Sstevel@tonic-gate 		ts.tv_sec = tv->tv_sec;
3060Sstevel@tonic-gate 		ts.tv_nsec = tv->tv_usec * 1000;
3071778Sraf 		if (ts.tv_nsec != 0 && ts.tv_nsec < 500000)
3081778Sraf 			ts.tv_nsec = 500000;
3090Sstevel@tonic-gate 		tsp = &ts;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (pselect(nfds, in0, out0, ex0, tsp, NULL));
3130Sstevel@tonic-gate }
314