xref: /onnv-gate/usr/src/lib/libc/port/gen/select_large_fdset.c (revision 6515:10dab2b883e0)
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 2008 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 _pollsys() 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  * This is the alternate large fd_set select.
44  *
45  */
46 
47 /*
48  * Must precede any include files
49  */
50 #ifdef FD_SETSIZE
51 #undef FD_SETSIZE
52 #endif
53 #define	FD_SETSIZE 65536
54 
55 /*
56  * We do not #redefine the name since the only users of this
57  * are external to the libraries and commands.
58  *
59  *  #pragma weak pselect_large_fdset = _pselect_large_fdset
60  *  #pragma weak select_large_fdset = _select_large_fdset
61  */
62 
63 #include "synonyms.h"
64 #include <values.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <pthread.h>
68 #include <errno.h>
69 #include <sys/time.h>
70 #include <sys/types.h>
71 #include <sys/poll.h>
72 #include <string.h>
73 #include <stdlib.h>
74 #include "libc.h"
75 
76 #define	DEFAULT_POLL_SIZE 64
77 
78 static struct pollfd *realloc_fds(int *, struct pollfd **, struct pollfd *);
79 
80 int
81 pselect_large_fdset(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
82 	const timespec_t *tsp, const sigset_t *sigmask)
83 {
84 	long *in, *out, *ex;
85 	ulong_t m;	/* bit mask */
86 	int j;		/* loop counter */
87 	ulong_t b;	/* bits to test */
88 	int n, rv;
89 	int lastj = -1;
90 	int nused;
91 
92 	/*
93 	 * Rather than have a mammoth pollfd (65K) list on the stack
94 	 * we start with a small one and then malloc larger chunks
95 	 * on the heap if necessary.
96 	 */
97 
98 	struct pollfd pfd[DEFAULT_POLL_SIZE];
99 	struct pollfd *p;
100 	struct pollfd *pfd_list;
101 	int nfds_on_list;
102 
103 	fd_set zero;
104 
105 	/*
106 	 * Check for invalid conditions at outset.
107 	 * Required for spec1170.
108 	 * SUSV3: We must behave as a cancellation point even if we fail early.
109 	 */
110 	if (nfds >= 0 && nfds <= FD_SETSIZE) {
111 		if (tsp != NULL) {
112 			if (tsp->tv_nsec < 0 || tsp->tv_nsec >= NANOSEC ||
113 			    tsp->tv_sec < 0) {
114 				pthread_testcancel();
115 				errno = EINVAL;
116 				return (-1);
117 			}
118 		}
119 	} else {
120 		pthread_testcancel();
121 		errno = EINVAL;
122 		return (-1);
123 	}
124 
125 	/*
126 	 * If any input args are null, point them at the null array.
127 	 */
128 	(void) memset(&zero, 0, sizeof (fd_set));
129 	if (in0 == NULL)
130 		in0 = &zero;
131 	if (out0 == NULL)
132 		out0 = &zero;
133 	if (ex0 == NULL)
134 		ex0 = &zero;
135 
136 	nfds_on_list = DEFAULT_POLL_SIZE;
137 	pfd_list = pfd;
138 	p = pfd_list;
139 	(void) memset(pfd, 0, sizeof (pfd));
140 	/*
141 	 * For each fd, if any bits are set convert them into
142 	 * the appropriate pollfd struct.
143 	 */
144 	in = (long *)in0->fds_bits;
145 	out = (long *)out0->fds_bits;
146 	ex = (long *)ex0->fds_bits;
147 	nused = 0;
148 	/*
149 	 * nused reflects the number of pollfd structs currently used
150 	 * less one. If realloc_fds returns NULL it is because malloc
151 	 * failed. We expect malloc() to have done the proper
152 	 * thing with errno.
153 	 */
154 	for (n = 0; n < nfds; n += NFDBITS) {
155 		b = (ulong_t)(*in | *out | *ex);
156 		for (j = 0, m = 1; b != 0; j++, b >>= 1, m <<= 1) {
157 			if (b & 1) {
158 				p->fd = n + j;
159 				if (p->fd < nfds) {
160 					p->events = 0;
161 					if (*in & m)
162 						p->events |= POLLRDNORM;
163 					if (*out & m)
164 						p->events |= POLLWRNORM;
165 					if (*ex & m)
166 						p->events |= POLLRDBAND;
167 					if (nused < (nfds_on_list - 1)) {
168 						p++;
169 					} else if ((p = realloc_fds(
170 					    &nfds_on_list, &pfd_list, pfd))
171 					    == NULL) {
172 						if (pfd_list != pfd)
173 							(void) free(pfd_list);
174 						pthread_testcancel();
175 						return (-1);
176 					}
177 					nused++;
178 				} else
179 					goto done;
180 			}
181 		}
182 		in++;
183 		out++;
184 		ex++;
185 	}
186 done:
187 	/*
188 	 * Now do the poll.
189 	 */
190 	do {
191 		rv = _pollsys(pfd_list, (nfds_t)nused, tsp, sigmask);
192 	} while (rv < 0 && errno == EAGAIN);
193 
194 	if (rv < 0) {		/* no need to set bit masks */
195 		if (pfd_list != pfd)
196 			(void) free(pfd_list);
197 		return (rv);
198 	} else if (rv == 0) {
199 		/*
200 		 * Clear out bit masks, just in case.
201 		 * On the assumption that usually only
202 		 * one bit mask is set, use three loops.
203 		 */
204 		if (in0 != &zero) {
205 			in = (long *)in0->fds_bits;
206 			for (n = 0; n < nfds; n += NFDBITS)
207 				*in++ = 0;
208 		}
209 		if (out0 != &zero) {
210 			out = (long *)out0->fds_bits;
211 			for (n = 0; n < nfds; n += NFDBITS)
212 				*out++ = 0;
213 		}
214 		if (ex0 != &zero) {
215 			ex = (long *)ex0->fds_bits;
216 			for (n = 0; n < nfds; n += NFDBITS)
217 				*ex++ = 0;
218 		}
219 		if (pfd_list != pfd)
220 			(void) free(pfd_list);
221 		return (0);
222 	}
223 
224 	/*
225 	 * Check for EINVAL error case first to avoid changing any bits
226 	 * if we're going to return an error.
227 	 */
228 	for (p = pfd_list, j = nused; j-- > 0; p++) {
229 		/*
230 		 * select will return EBADF immediately if any fd's
231 		 * are bad.  poll will complete the poll on the
232 		 * rest of the fd's and include the error indication
233 		 * in the returned bits.  This is a rare case so we
234 		 * accept this difference and return the error after
235 		 * doing more work than select would've done.
236 		 */
237 		if (p->revents & POLLNVAL) {
238 			errno = EBADF;
239 			if (pfd_list != pfd)
240 				(void) free(pfd_list);
241 			return (-1);
242 		}
243 		/*
244 		 * We would like to make POLLHUP available to select,
245 		 * checking to see if we have pending data to be read.
246 		 * BUT until we figure out how not to break Xsun's
247 		 * dependencies on select's existing features...
248 		 * This is what we _thought_ would work ... sigh!
249 		 */
250 		/*
251 		 * if ((p->revents & POLLHUP) &&
252 		 *	!(p->revents & (POLLRDNORM|POLLRDBAND))) {
253 		 *	errno = EINTR;
254 		 *	return (-1);
255 		 * }
256 		 */
257 	}
258 
259 	/*
260 	 * Convert results of poll back into bits
261 	 * in the argument arrays.
262 	 *
263 	 * We assume POLLRDNORM, POLLWRNORM, and POLLRDBAND will only be set
264 	 * on return from poll if they were set on input, thus we don't
265 	 * worry about accidentally setting the corresponding bits in the
266 	 * zero array if the input bit masks were null.
267 	 *
268 	 * Must return number of bits set, not number of ready descriptors
269 	 * (as the man page says, and as poll() does).
270 	 */
271 	rv = 0;
272 	for (p = pfd_list; nused-- > 0; p++) {
273 		j = (int)(p->fd / NFDBITS);
274 		/* have we moved into another word of the bit mask yet? */
275 		if (j != lastj) {
276 			/* clear all output bits to start with */
277 			in = (long *)&in0->fds_bits[j];
278 			out = (long *)&out0->fds_bits[j];
279 			ex = (long *)&ex0->fds_bits[j];
280 			/*
281 			 * In case we made "zero" read-only (e.g., with
282 			 * cc -R), avoid actually storing into it.
283 			 */
284 			if (in0 != &zero)
285 				*in = 0;
286 			if (out0 != &zero)
287 				*out = 0;
288 			if (ex0 != &zero)
289 				*ex = 0;
290 			lastj = j;
291 		}
292 		if (p->revents) {
293 			m = 1L << (p->fd % NFDBITS);
294 			if (p->revents & POLLRDNORM) {
295 				*in |= m;
296 				rv++;
297 			}
298 			if (p->revents & POLLWRNORM) {
299 				*out |= m;
300 				rv++;
301 			}
302 			if (p->revents & POLLRDBAND) {
303 				*ex |= m;
304 				rv++;
305 			}
306 			/*
307 			 * Only set this bit on return if we asked about
308 			 * input conditions.
309 			 */
310 			if ((p->revents & (POLLHUP|POLLERR)) &&
311 			    (p->events & POLLRDNORM)) {
312 				if ((*in & m) == 0)
313 					rv++;	/* wasn't already set */
314 				*in |= m;
315 			}
316 			/*
317 			 * Only set this bit on return if we asked about
318 			 * output conditions.
319 			 */
320 			if ((p->revents & (POLLHUP|POLLERR)) &&
321 			    (p->events & POLLWRNORM)) {
322 				if ((*out & m) == 0)
323 					rv++;	/* wasn't already set */
324 				*out |= m;
325 			}
326 			/*
327 			 * Only set this bit on return if we asked about
328 			 * output conditions.
329 			 */
330 			if ((p->revents & (POLLHUP|POLLERR)) &&
331 			    (p->events & POLLRDBAND)) {
332 				if ((*ex & m) == 0)
333 					rv++;   /* wasn't already set */
334 				*ex |= m;
335 			}
336 		}
337 	}
338 	if (pfd_list != pfd)
339 		(void) free(pfd_list);
340 	return (rv);
341 }
342 
343 int
344 select_large_fdset(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
345 	struct timeval *tv)
346 {
347 	timespec_t ts;
348 	timespec_t *tsp;
349 
350 	if (tv == NULL)
351 		tsp = NULL;
352 	else {
353 		/* check timeval validity */
354 		if (tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) {
355 			errno = EINVAL;
356 			return (-1);
357 		}
358 		/*
359 		 * Convert timeval to timespec.
360 		 * To preserve compatibility with past behavior,
361 		 * when select was built upon poll(2), which has a
362 		 * minimum non-zero timeout of 1 millisecond, force
363 		 * a minimum non-zero timeout of 500 microseconds.
364 		 */
365 		ts.tv_sec = tv->tv_sec;
366 		ts.tv_nsec = tv->tv_usec * 1000;
367 		if (ts.tv_nsec != 0 && ts.tv_nsec < 500000)
368 			ts.tv_nsec = 500000;
369 		tsp = &ts;
370 	}
371 
372 	return (pselect_large_fdset(nfds, in0, out0, ex0, tsp, NULL));
373 }
374 
375 /*
376  * Reallocate buffers of pollfds for our list. We malloc a new buffer
377  * and, in the case where the old buffer does not match what is passed
378  * in orig, free the buffer after copying the contents.
379  */
380 struct pollfd *
381 realloc_fds(int *num, struct pollfd **list_head, struct pollfd *orig)
382 {
383 	struct pollfd *b;
384 	int nta;
385 	int n2;
386 
387 	n2 = *num * 2;
388 	nta = n2 * sizeof (struct pollfd);
389 	b = malloc(nta);
390 	if (b) {
391 		(void) memset(b, 0, (size_t)nta);
392 		(void) memcpy(b, *list_head, nta / 2);
393 		if (*list_head != orig)
394 			(void) free(*list_head);
395 		*list_head = b;
396 		b += *num;
397 		*num = n2;
398 	}
399 	return (b);
400 }
401