xref: /titanic_44/usr/src/lib/libc/port/gen/select.c (revision 8a40a695ee676a322b094e9afe5375567bfb51e3)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 /*
33  * Emulation of select() system call using poll() system call.
34  *
35  * Assumptions:
36  *	polling for input only is most common.
37  *	polling for exceptional conditions is very rare.
38  *
39  * Note that is it not feasible to emulate all error conditions,
40  * in particular conditions that would return EFAULT are far too
41  * difficult to check for in a library routine.
42  *
43  */
44 
45 #pragma weak pselect = _pselect
46 #pragma weak select = _select
47 
48 #include "synonyms.h"
49 #include <values.h>
50 #include <errno.h>
51 #include <sys/time.h>
52 #include <sys/types.h>
53 #include <sys/select.h>
54 #include <sys/poll.h>
55 #include <alloca.h>
56 #include "libc.h"
57 
58 int
59 pselect(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
60 	const timespec_t *tsp, const sigset_t *sigmask)
61 {
62 	long *in, *out, *ex;
63 	ulong_t m;	/* bit mask */
64 	int j;		/* loop counter */
65 	ulong_t b;	/* bits to test */
66 	int n, rv;
67 	struct pollfd *pfd;
68 	struct pollfd *p;
69 	int lastj = -1;
70 
71 	/* "zero" is read-only, it could go in the text segment */
72 	static fd_set zero = { 0 };
73 
74 	/*
75 	 * Check for invalid conditions at outset.
76 	 * Required for spec1170.
77 	 * SUSV3: We must behave as a cancellation point even if we fail early.
78 	 */
79 	if (nfds < 0 || nfds > FD_SETSIZE) {
80 		_private_testcancel();
81 		errno = EINVAL;
82 		return (-1);
83 	}
84 	p = pfd = (struct pollfd *)alloca(nfds * sizeof (struct pollfd));
85 
86 	if (tsp != NULL) {
87 		/* check timespec validity */
88 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= NANOSEC ||
89 		    tsp->tv_sec < 0) {
90 			_private_testcancel();
91 			errno = EINVAL;
92 			return (-1);
93 		}
94 	}
95 
96 	/*
97 	 * If any input args are null, point them at the null array.
98 	 */
99 	if (in0 == NULL)
100 		in0 = &zero;
101 	if (out0 == NULL)
102 		out0 = &zero;
103 	if (ex0 == NULL)
104 		ex0 = &zero;
105 
106 	/*
107 	 * For each fd, if any bits are set convert them into
108 	 * the appropriate pollfd struct.
109 	 */
110 	in = (long *)in0->fds_bits;
111 	out = (long *)out0->fds_bits;
112 	ex = (long *)ex0->fds_bits;
113 	for (n = 0; n < nfds; n += NFDBITS) {
114 		b = (ulong_t)(*in | *out | *ex);
115 		for (j = 0, m = 1; b != 0; j++, b >>= 1, m <<= 1) {
116 			if (b & 1) {
117 				p->fd = n + j;
118 				if (p->fd >= nfds)
119 					goto done;
120 				p->events = 0;
121 				if (*in & m)
122 					p->events |= POLLRDNORM;
123 				if (*out & m)
124 					p->events |= POLLWRNORM;
125 				if (*ex & m)
126 					p->events |= POLLRDBAND;
127 				p++;
128 			}
129 		}
130 		in++;
131 		out++;
132 		ex++;
133 	}
134 done:
135 	/*
136 	 * Now do the poll.
137 	 */
138 	n = (int)(p - pfd);		/* number of pollfd's */
139 	do {
140 		rv = _pollsys(pfd, (nfds_t)n, tsp, sigmask);
141 	} while (rv < 0 && errno == EAGAIN);
142 
143 	if (rv < 0)		/* no need to set bit masks */
144 		return (rv);
145 
146 	if (rv == 0) {
147 		/*
148 		 * Clear out bit masks, just in case.
149 		 * On the assumption that usually only
150 		 * one bit mask is set, use three loops.
151 		 */
152 		if (in0 != &zero) {
153 			in = (long *)in0->fds_bits;
154 			for (n = 0; n < nfds; n += NFDBITS)
155 				*in++ = 0;
156 		}
157 		if (out0 != &zero) {
158 			out = (long *)out0->fds_bits;
159 			for (n = 0; n < nfds; n += NFDBITS)
160 				*out++ = 0;
161 		}
162 		if (ex0 != &zero) {
163 			ex = (long *)ex0->fds_bits;
164 			for (n = 0; n < nfds; n += NFDBITS)
165 				*ex++ = 0;
166 		}
167 		return (0);
168 	}
169 
170 	/*
171 	 * Check for EINVAL error case first to avoid changing any bits
172 	 * if we're going to return an error.
173 	 */
174 	for (p = pfd, j = n; j-- > 0; p++) {
175 		/*
176 		 * select will return EBADF immediately if any fd's
177 		 * are bad.  poll will complete the poll on the
178 		 * rest of the fd's and include the error indication
179 		 * in the returned bits.  This is a rare case so we
180 		 * accept this difference and return the error after
181 		 * doing more work than select would've done.
182 		 */
183 		if (p->revents & POLLNVAL) {
184 			errno = EBADF;
185 			return (-1);
186 		}
187 		/*
188 		 * We would like to make POLLHUP available to select,
189 		 * checking to see if we have pending data to be read.
190 		 * BUT until we figure out how not to break Xsun's
191 		 * dependencies on select's existing features...
192 		 * This is what we _thought_ would work ... sigh!
193 		 */
194 		/*
195 		 * if ((p->revents & POLLHUP) &&
196 		 *	!(p->revents & (POLLRDNORM|POLLRDBAND))) {
197 		 *	errno = EINTR;
198 		 *	return (-1);
199 		 * }
200 		 */
201 	}
202 
203 	/*
204 	 * Convert results of poll back into bits
205 	 * in the argument arrays.
206 	 *
207 	 * We assume POLLRDNORM, POLLWRNORM, and POLLRDBAND will only be set
208 	 * on return from poll if they were set on input, thus we don't
209 	 * worry about accidentally setting the corresponding bits in the
210 	 * zero array if the input bit masks were null.
211 	 *
212 	 * Must return number of bits set, not number of ready descriptors
213 	 * (as the man page says, and as poll() does).
214 	 */
215 	rv = 0;
216 	for (p = pfd; n-- > 0; p++) {
217 		j = (int)(p->fd / NFDBITS);
218 		/* have we moved into another word of the bit mask yet? */
219 		if (j != lastj) {
220 			/* clear all output bits to start with */
221 			in = (long *)&in0->fds_bits[j];
222 			out = (long *)&out0->fds_bits[j];
223 			ex = (long *)&ex0->fds_bits[j];
224 			/*
225 			 * In case we made "zero" read-only (e.g., with
226 			 * cc -R), avoid actually storing into it.
227 			 */
228 			if (in0 != &zero)
229 				*in = 0;
230 			if (out0 != &zero)
231 				*out = 0;
232 			if (ex0 != &zero)
233 				*ex = 0;
234 			lastj = j;
235 		}
236 		if (p->revents) {
237 			m = 1L << (p->fd % NFDBITS);
238 			if (p->revents & POLLRDNORM) {
239 				*in |= m;
240 				rv++;
241 			}
242 			if (p->revents & POLLWRNORM) {
243 				*out |= m;
244 				rv++;
245 			}
246 			if (p->revents & POLLRDBAND) {
247 				*ex |= m;
248 				rv++;
249 			}
250 			/*
251 			 * Only set this bit on return if we asked about
252 			 * input conditions.
253 			 */
254 			if ((p->revents & (POLLHUP|POLLERR)) &&
255 			    (p->events & POLLRDNORM)) {
256 				if ((*in & m) == 0)
257 					rv++;	/* wasn't already set */
258 				*in |= m;
259 			}
260 			/*
261 			 * Only set this bit on return if we asked about
262 			 * output conditions.
263 			 */
264 			if ((p->revents & (POLLHUP|POLLERR)) &&
265 			    (p->events & POLLWRNORM)) {
266 				if ((*out & m) == 0)
267 					rv++;	/* wasn't already set */
268 				*out |= m;
269 			}
270 			/*
271 			 * Only set this bit on return if we asked about
272 			 * output conditions.
273 			 */
274 			if ((p->revents & (POLLHUP|POLLERR)) &&
275 			    (p->events & POLLRDBAND)) {
276 				if ((*ex & m) == 0)
277 					rv++;	/* wasn't already set */
278 				*ex |= m;
279 			}
280 		}
281 	}
282 	return (rv);
283 }
284 
285 int
286 select(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0, struct timeval *tv)
287 {
288 	timespec_t ts;
289 	timespec_t *tsp;
290 
291 	if (tv == NULL)
292 		tsp = NULL;
293 	else {
294 		/* check timeval validity */
295 		if (tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) {
296 			errno = EINVAL;
297 			return (-1);
298 		}
299 		/*
300 		 * Convert timeval to timespec.
301 		 * To preserve compatibility with past behavior,
302 		 * when select was built upon poll(2), which has a
303 		 * minimum non-zero timeout of 1 millisecond, force
304 		 * a minimum non-zero timeout of 500 microseconds.
305 		 */
306 		ts.tv_sec = tv->tv_sec;
307 		ts.tv_nsec = tv->tv_usec * 1000;
308 		if (ts.tv_nsec != 0 && ts.tv_nsec < 500000)
309 			ts.tv_nsec = 500000;
310 		tsp = &ts;
311 	}
312 
313 	return (pselect(nfds, in0, out0, ex0, tsp, NULL));
314 }
315