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