2N/A/*
2N/A * CDDL HEADER START
2N/A *
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 *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
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 *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Emulation of select() system call using _pollsys() system call.
2N/A *
2N/A * Assumptions:
2N/A * polling for input only is most common.
2N/A * polling for exceptional conditions is very rare.
2N/A *
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 *
2N/A * This is the alternate large fd_set select.
2N/A *
2N/A */
2N/A
2N/A/*
2N/A * Must precede any include files
2N/A */
2N/A#ifdef FD_SETSIZE
2N/A#undef FD_SETSIZE
2N/A#endif
2N/A#define FD_SETSIZE 65536
2N/A
2N/A#include "lint.h"
2N/A#include <values.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <pthread.h>
2N/A#include <errno.h>
2N/A#include <sys/time.h>
2N/A#include <sys/types.h>
2N/A#include <sys/poll.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include "libc.h"
2N/A
2N/A#define DEFAULT_POLL_SIZE 64
2N/A
2N/Astatic struct pollfd *realloc_fds(int *, struct pollfd **, struct pollfd *);
2N/A
2N/Aint
2N/Apselect_large_fdset(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
2N/A const timespec_t *tsp, const sigset_t *sigmask)
2N/A{
2N/A long *in, *out, *ex;
2N/A ulong_t m; /* bit mask */
2N/A int j; /* loop counter */
2N/A ulong_t b; /* bits to test */
2N/A int n, rv;
2N/A int lastj = -1;
2N/A int nused;
2N/A
2N/A /*
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 */
2N/A
2N/A struct pollfd pfd[DEFAULT_POLL_SIZE];
2N/A struct pollfd *p;
2N/A struct pollfd *pfd_list;
2N/A int nfds_on_list;
2N/A
2N/A fd_set zero;
2N/A
2N/A /*
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 */
2N/A if (nfds >= 0 && nfds <= FD_SETSIZE) {
2N/A if (tsp != NULL) {
2N/A if (tsp->tv_nsec < 0 || tsp->tv_nsec >= NANOSEC ||
2N/A tsp->tv_sec < 0) {
2N/A pthread_testcancel();
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A }
2N/A } else {
2N/A pthread_testcancel();
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * If any input args are null, point them at the null array.
2N/A */
2N/A (void) memset(&zero, 0, sizeof (fd_set));
2N/A if (in0 == NULL)
2N/A in0 = &zero;
2N/A if (out0 == NULL)
2N/A out0 = &zero;
2N/A if (ex0 == NULL)
2N/A ex0 = &zero;
2N/A
2N/A nfds_on_list = DEFAULT_POLL_SIZE;
2N/A pfd_list = pfd;
2N/A p = pfd_list;
2N/A (void) memset(pfd, 0, sizeof (pfd));
2N/A /*
2N/A * For each fd, if any bits are set convert them into
2N/A * the appropriate pollfd struct.
2N/A */
2N/A in = (long *)in0->fds_bits;
2N/A out = (long *)out0->fds_bits;
2N/A ex = (long *)ex0->fds_bits;
2N/A nused = 0;
2N/A /*
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 * thing with errno.
2N/A */
2N/A for (n = 0; n < nfds; n += NFDBITS) {
2N/A b = (ulong_t)(*in | *out | *ex);
2N/A for (j = 0, m = 1; b != 0; j++, b >>= 1, m <<= 1) {
2N/A if (b & 1) {
2N/A p->fd = n + j;
2N/A if (p->fd < nfds) {
2N/A p->events = 0;
2N/A if (*in & m)
2N/A p->events |= POLLRDNORM;
2N/A if (*out & m)
2N/A p->events |= POLLWRNORM;
2N/A if (*ex & m)
2N/A p->events |= POLLRDBAND;
2N/A if (nused < (nfds_on_list - 1)) {
2N/A p++;
2N/A } else if ((p = realloc_fds(
2N/A &nfds_on_list, &pfd_list, pfd))
2N/A == NULL) {
2N/A if (pfd_list != pfd)
2N/A free(pfd_list);
2N/A pthread_testcancel();
2N/A return (-1);
2N/A }
2N/A nused++;
2N/A } else
2N/A goto done;
2N/A }
2N/A }
2N/A in++;
2N/A out++;
2N/A ex++;
2N/A }
2N/Adone:
2N/A /*
2N/A * Now do the poll.
2N/A */
2N/A do {
2N/A rv = _pollsys(pfd_list, (nfds_t)nused, tsp, sigmask);
2N/A } while (rv < 0 && errno == EAGAIN);
2N/A
2N/A if (rv < 0) { /* no need to set bit masks */
2N/A if (pfd_list != pfd)
2N/A free(pfd_list);
2N/A return (rv);
2N/A } else if (rv == 0) {
2N/A /*
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 */
2N/A if (in0 != &zero) {
2N/A in = (long *)in0->fds_bits;
2N/A for (n = 0; n < nfds; n += NFDBITS)
2N/A *in++ = 0;
2N/A }
2N/A if (out0 != &zero) {
2N/A out = (long *)out0->fds_bits;
2N/A for (n = 0; n < nfds; n += NFDBITS)
2N/A *out++ = 0;
2N/A }
2N/A if (ex0 != &zero) {
2N/A ex = (long *)ex0->fds_bits;
2N/A for (n = 0; n < nfds; n += NFDBITS)
2N/A *ex++ = 0;
2N/A }
2N/A if (pfd_list != pfd)
2N/A free(pfd_list);
2N/A return (0);
2N/A }
2N/A
2N/A /*
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 */
2N/A for (p = pfd_list, j = nused; j-- > 0; p++) {
2N/A /*
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 */
2N/A if (p->revents & POLLNVAL) {
2N/A errno = EBADF;
2N/A if (pfd_list != pfd)
2N/A free(pfd_list);
2N/A return (-1);
2N/A }
2N/A /*
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 */
2N/A /*
2N/A * if ((p->revents & POLLHUP) &&
2N/A * !(p->revents & (POLLRDNORM|POLLRDBAND))) {
2N/A * errno = EINTR;
2N/A * return (-1);
2N/A * }
2N/A */
2N/A }
2N/A
2N/A /*
2N/A * Convert results of poll back into bits
2N/A * in the argument arrays.
2N/A *
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 *
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 */
2N/A rv = 0;
2N/A for (p = pfd_list; nused-- > 0; p++) {
2N/A j = (int)(p->fd / NFDBITS);
2N/A /* have we moved into another word of the bit mask yet? */
2N/A if (j != lastj) {
2N/A /* clear all output bits to start with */
2N/A in = (long *)&in0->fds_bits[j];
2N/A out = (long *)&out0->fds_bits[j];
2N/A ex = (long *)&ex0->fds_bits[j];
2N/A /*
2N/A * In case we made "zero" read-only (e.g., with
2N/A * cc -R), avoid actually storing into it.
2N/A */
2N/A if (in0 != &zero)
2N/A *in = 0;
2N/A if (out0 != &zero)
2N/A *out = 0;
2N/A if (ex0 != &zero)
2N/A *ex = 0;
2N/A lastj = j;
2N/A }
2N/A if (p->revents) {
2N/A m = 1L << (p->fd % NFDBITS);
2N/A if (p->revents & POLLRDNORM) {
2N/A *in |= m;
2N/A rv++;
2N/A }
2N/A if (p->revents & POLLWRNORM) {
2N/A *out |= m;
2N/A rv++;
2N/A }
2N/A if (p->revents & POLLRDBAND) {
2N/A *ex |= m;
2N/A rv++;
2N/A }
2N/A /*
2N/A * Only set this bit on return if we asked about
2N/A * input conditions.
2N/A */
2N/A if ((p->revents & (POLLHUP|POLLERR)) &&
2N/A (p->events & POLLRDNORM)) {
2N/A if ((*in & m) == 0)
2N/A rv++; /* wasn't already set */
2N/A *in |= m;
2N/A }
2N/A /*
2N/A * Only set this bit on return if we asked about
2N/A * output conditions.
2N/A */
2N/A if ((p->revents & (POLLHUP|POLLERR)) &&
2N/A (p->events & POLLWRNORM)) {
2N/A if ((*out & m) == 0)
2N/A rv++; /* wasn't already set */
2N/A *out |= m;
2N/A }
2N/A /*
2N/A * Only set this bit on return if we asked about
2N/A * output conditions.
2N/A */
2N/A if ((p->revents & (POLLHUP|POLLERR)) &&
2N/A (p->events & POLLRDBAND)) {
2N/A if ((*ex & m) == 0)
2N/A rv++; /* wasn't already set */
2N/A *ex |= m;
2N/A }
2N/A }
2N/A }
2N/A if (pfd_list != pfd)
2N/A free(pfd_list);
2N/A return (rv);
2N/A}
2N/A
2N/Aint
2N/Aselect_large_fdset(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
2N/A struct timeval *tv)
2N/A{
2N/A timespec_t ts;
2N/A timespec_t *tsp;
2N/A
2N/A if (tv == NULL)
2N/A tsp = NULL;
2N/A else {
2N/A /* check timeval validity */
2N/A if (tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A /*
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 */
2N/A ts.tv_sec = tv->tv_sec;
2N/A ts.tv_nsec = tv->tv_usec * 1000;
2N/A if (ts.tv_nsec != 0 && ts.tv_nsec < 500000)
2N/A ts.tv_nsec = 500000;
2N/A tsp = &ts;
2N/A }
2N/A
2N/A return (pselect_large_fdset(nfds, in0, out0, ex0, tsp, NULL));
2N/A}
2N/A
2N/A/*
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.
2N/A */
2N/Astruct pollfd *
2N/Arealloc_fds(int *num, struct pollfd **list_head, struct pollfd *orig)
2N/A{
2N/A struct pollfd *b;
2N/A int nta;
2N/A int n2;
2N/A
2N/A n2 = *num * 2;
2N/A nta = n2 * sizeof (struct pollfd);
2N/A b = malloc(nta);
2N/A if (b) {
2N/A (void) memset(b, 0, (size_t)nta);
2N/A (void) memcpy(b, *list_head, nta / 2);
2N/A if (*list_head != orig)
2N/A free(*list_head);
2N/A *list_head = b;
2N/A b += *num;
2N/A *num = n2;
2N/A }
2N/A return (b);
2N/A}