2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A/* Copyright (c) 1988 AT&T */ 2N/A/* All Rights Reserved */ 2N/A#
pragma ident "%Z%%M% %I% %E% SMI" 2N/A * Emulation of select() system call using _pollsys() system call. 2N/A * polling for input only is most common. 2N/A * polling for exceptional conditions is very rare. 2N/A * Note that is it not feasible to emulate all error conditions, 2N/A * in particular conditions that would return EFAULT are far too 2N/A * difficult to check for in a library routine. 2N/A * This is the alternate large fd_set select. 2N/A * Must precede any include files 2N/A int j;
/* loop counter */ 2N/A * Rather than have a mammoth pollfd (65K) list on the stack 2N/A * we start with a small one and then malloc larger chunks 2N/A * on the heap if necessary. 2N/A * Check for invalid conditions at outset. 2N/A * Required for spec1170. 2N/A * SUSV3: We must behave as a cancellation point even if we fail early. 2N/A * If any input args are null, point them at the null array. 2N/A * For each fd, if any bits are set convert them into 2N/A * the appropriate pollfd struct. 2N/A * nused reflects the number of pollfd structs currently used 2N/A * less one. If realloc_fds returns NULL it is because malloc 2N/A * failed. We expect malloc() to have done the proper 2N/A for (j = 0, m =
1; b != 0; j++, b >>=
1, m <<=
1) {
2N/A if (
rv < 0) {
/* no need to set bit masks */ 2N/A * Clear out bit masks, just in case. 2N/A * On the assumption that usually only 2N/A * one bit mask is set, use three loops. 2N/A * Check for EINVAL error case first to avoid changing any bits 2N/A * if we're going to return an error. 2N/A * select will return EBADF immediately if any fd's 2N/A * are bad. poll will complete the poll on the 2N/A * rest of the fd's and include the error indication 2N/A * in the returned bits. This is a rare case so we 2N/A * accept this difference and return the error after 2N/A * doing more work than select would've done. 2N/A * We would like to make POLLHUP available to select, 2N/A * checking to see if we have pending data to be read. 2N/A * BUT until we figure out how not to break Xsun's 2N/A * dependencies on select's existing features... 2N/A * This is what we _thought_ would work ... sigh! 2N/A * if ((p->revents & POLLHUP) && 2N/A * !(p->revents & (POLLRDNORM|POLLRDBAND))) { 2N/A * Convert results of poll back into bits 2N/A * in the argument arrays. 2N/A * We assume POLLRDNORM, POLLWRNORM, and POLLRDBAND will only be set 2N/A * on return from poll if they were set on input, thus we don't 2N/A * worry about accidentally setting the corresponding bits in the 2N/A * zero array if the input bit masks were null. 2N/A * Must return number of bits set, not number of ready descriptors 2N/A * (as the man page says, and as poll() does). 2N/A /* have we moved into another word of the bit mask yet? */ 2N/A /* clear all output bits to start with */ 2N/A * In case we made "zero" read-only (e.g., with 2N/A * cc -R), avoid actually storing into it. 2N/A * Only set this bit on return if we asked about 2N/A rv++;
/* wasn't already set */ 2N/A * Only set this bit on return if we asked about 2N/A * output conditions. 2N/A rv++;
/* wasn't already set */ 2N/A * Only set this bit on return if we asked about 2N/A * output conditions. 2N/A rv++;
/* wasn't already set */ 2N/A /* check timeval validity */ 2N/A * Convert timeval to timespec. 2N/A * To preserve compatibility with past behavior, 2N/A * when select was built upon poll(2), which has a 2N/A * minimum non-zero timeout of 1 millisecond, force 2N/A * a minimum non-zero timeout of 500 microseconds. 2N/A * Reallocate buffers of pollfds for our list. We malloc a new buffer 2N/A * and, in the case where the old buffer does not match what is passed 2N/A * in orig, free the buffer after copying the contents.