1*433d6423SLionel Sambuc /* $NetBSD: poll.c,v 1.3 2008/04/29 05:46:08 martin Exp $ */ 2*433d6423SLionel Sambuc 3*433d6423SLionel Sambuc /*- 4*433d6423SLionel Sambuc * Copyright (c) 2003 The NetBSD Foundation, Inc. 5*433d6423SLionel Sambuc * All rights reserved. 6*433d6423SLionel Sambuc * 7*433d6423SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation 8*433d6423SLionel Sambuc * by Charles Blundell. 9*433d6423SLionel Sambuc * 10*433d6423SLionel Sambuc * Redistribution and use in source and binary forms, with or without 11*433d6423SLionel Sambuc * modification, are permitted provided that the following conditions 12*433d6423SLionel Sambuc * are met: 13*433d6423SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 14*433d6423SLionel Sambuc * notice, this list of conditions and the following disclaimer. 15*433d6423SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 16*433d6423SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 17*433d6423SLionel Sambuc * documentation and/or other materials provided with the distribution. 18*433d6423SLionel Sambuc * 19*433d6423SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*433d6423SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*433d6423SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*433d6423SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*433d6423SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*433d6423SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*433d6423SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*433d6423SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*433d6423SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*433d6423SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*433d6423SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 30*433d6423SLionel Sambuc */ 31*433d6423SLionel Sambuc 32*433d6423SLionel Sambuc #include <sys/cdefs.h> 33*433d6423SLionel Sambuc #include "namespace.h" 34*433d6423SLionel Sambuc #include <lib.h> 35*433d6423SLionel Sambuc 36*433d6423SLionel Sambuc #include <sys/types.h> 37*433d6423SLionel Sambuc #include <sys/time.h> 38*433d6423SLionel Sambuc #include <unistd.h> 39*433d6423SLionel Sambuc #include <sys/poll.h> 40*433d6423SLionel Sambuc #include <errno.h> 41*433d6423SLionel Sambuc 42*433d6423SLionel Sambuc int 43*433d6423SLionel Sambuc poll(struct pollfd *p, nfds_t nfds, int timout) 44*433d6423SLionel Sambuc { 45*433d6423SLionel Sambuc fd_set rd, wr, except; 46*433d6423SLionel Sambuc struct timeval tv; 47*433d6423SLionel Sambuc nfds_t i; 48*433d6423SLionel Sambuc int highfd, rval; 49*433d6423SLionel Sambuc 50*433d6423SLionel Sambuc /* 51*433d6423SLionel Sambuc * select cannot tell us much wrt POLL*BAND, POLLPRI, POLLHUP or 52*433d6423SLionel Sambuc * POLLNVAL. 53*433d6423SLionel Sambuc */ 54*433d6423SLionel Sambuc FD_ZERO(&rd); 55*433d6423SLionel Sambuc FD_ZERO(&wr); 56*433d6423SLionel Sambuc FD_ZERO(&except); 57*433d6423SLionel Sambuc 58*433d6423SLionel Sambuc highfd = -1; 59*433d6423SLionel Sambuc for (i = 0; i < nfds; i++) { 60*433d6423SLionel Sambuc if (p[i].fd < 0) 61*433d6423SLionel Sambuc continue; 62*433d6423SLionel Sambuc if (p[i].fd >= FD_SETSIZE) { 63*433d6423SLionel Sambuc errno = EINVAL; 64*433d6423SLionel Sambuc return -1; 65*433d6423SLionel Sambuc } 66*433d6423SLionel Sambuc if (p[i].fd > highfd) 67*433d6423SLionel Sambuc highfd = p[i].fd; 68*433d6423SLionel Sambuc 69*433d6423SLionel Sambuc if (p[i].events & (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI)) 70*433d6423SLionel Sambuc FD_SET(p[i].fd, &rd); 71*433d6423SLionel Sambuc if (p[i].events & (POLLOUT|POLLWRNORM|POLLWRBAND)) 72*433d6423SLionel Sambuc FD_SET(p[i].fd, &wr); 73*433d6423SLionel Sambuc FD_SET(p[i].fd, &except); 74*433d6423SLionel Sambuc } 75*433d6423SLionel Sambuc 76*433d6423SLionel Sambuc tv.tv_sec = timout / 1000; 77*433d6423SLionel Sambuc tv.tv_usec = (timout % 1000) * 1000; 78*433d6423SLionel Sambuc 79*433d6423SLionel Sambuc rval = select(highfd + 1, &rd, &wr, &except, 80*433d6423SLionel Sambuc timout == -1 ? NULL : &tv); 81*433d6423SLionel Sambuc if (rval <= 0) 82*433d6423SLionel Sambuc return rval; 83*433d6423SLionel Sambuc 84*433d6423SLionel Sambuc rval = 0; 85*433d6423SLionel Sambuc for (i = 0; i < nfds; i++) { 86*433d6423SLionel Sambuc p[i].revents = 0; 87*433d6423SLionel Sambuc if (FD_ISSET(p[i].fd, &rd)) 88*433d6423SLionel Sambuc p[i].revents |= POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI; 89*433d6423SLionel Sambuc if (FD_ISSET(p[i].fd, &wr)) 90*433d6423SLionel Sambuc p[i].revents |= POLLOUT|POLLWRNORM|POLLWRBAND; 91*433d6423SLionel Sambuc if (FD_ISSET(p[i].fd, &except)) 92*433d6423SLionel Sambuc p[i].revents |= POLLERR; 93*433d6423SLionel Sambuc /* XXX: POLLHUP/POLLNVAL? */ 94*433d6423SLionel Sambuc if (p[i].revents != 0) 95*433d6423SLionel Sambuc rval++; 96*433d6423SLionel Sambuc } 97*433d6423SLionel Sambuc return rval; 98*433d6423SLionel Sambuc } 99