1433d6423SLionel Sambuc /* $NetBSD: poll.c,v 1.3 2008/04/29 05:46:08 martin Exp $ */
2433d6423SLionel Sambuc
3433d6423SLionel Sambuc /*-
4433d6423SLionel Sambuc * Copyright (c) 2003 The NetBSD Foundation, Inc.
5433d6423SLionel Sambuc * All rights reserved.
6433d6423SLionel Sambuc *
7433d6423SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8433d6423SLionel Sambuc * by Charles Blundell.
9433d6423SLionel Sambuc *
10433d6423SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11433d6423SLionel Sambuc * modification, are permitted provided that the following conditions
12433d6423SLionel Sambuc * are met:
13433d6423SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14433d6423SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15433d6423SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16433d6423SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17433d6423SLionel Sambuc * documentation and/or other materials provided with the distribution.
18433d6423SLionel Sambuc *
19433d6423SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20433d6423SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21433d6423SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22433d6423SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23433d6423SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24433d6423SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25433d6423SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26433d6423SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27433d6423SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28433d6423SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29433d6423SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30433d6423SLionel Sambuc */
31433d6423SLionel Sambuc
32433d6423SLionel Sambuc #include <sys/cdefs.h>
33433d6423SLionel Sambuc #include "namespace.h"
34433d6423SLionel Sambuc #include <lib.h>
35433d6423SLionel Sambuc
36433d6423SLionel Sambuc #include <sys/types.h>
37433d6423SLionel Sambuc #include <sys/time.h>
38433d6423SLionel Sambuc #include <unistd.h>
39433d6423SLionel Sambuc #include <sys/poll.h>
40433d6423SLionel Sambuc #include <errno.h>
41433d6423SLionel Sambuc
42433d6423SLionel Sambuc int
poll(struct pollfd * p,nfds_t nfds,int timout)43433d6423SLionel Sambuc poll(struct pollfd *p, nfds_t nfds, int timout)
44433d6423SLionel Sambuc {
45433d6423SLionel Sambuc fd_set rd, wr, except;
46433d6423SLionel Sambuc struct timeval tv;
47433d6423SLionel Sambuc nfds_t i;
48433d6423SLionel Sambuc int highfd, rval;
49433d6423SLionel Sambuc
50433d6423SLionel Sambuc /*
51433d6423SLionel Sambuc * select cannot tell us much wrt POLL*BAND, POLLPRI, POLLHUP or
52433d6423SLionel Sambuc * POLLNVAL.
53433d6423SLionel Sambuc */
54433d6423SLionel Sambuc FD_ZERO(&rd);
55433d6423SLionel Sambuc FD_ZERO(&wr);
56433d6423SLionel Sambuc FD_ZERO(&except);
57433d6423SLionel Sambuc
58433d6423SLionel Sambuc highfd = -1;
59433d6423SLionel Sambuc for (i = 0; i < nfds; i++) {
606956dd2bSDavid van Moolenbroek p[i].revents = 0;
61433d6423SLionel Sambuc if (p[i].fd < 0)
62433d6423SLionel Sambuc continue;
63433d6423SLionel Sambuc if (p[i].fd >= FD_SETSIZE) {
64433d6423SLionel Sambuc errno = EINVAL;
65433d6423SLionel Sambuc return -1;
66433d6423SLionel Sambuc }
67433d6423SLionel Sambuc if (p[i].fd > highfd)
68433d6423SLionel Sambuc highfd = p[i].fd;
69433d6423SLionel Sambuc
70*44fdeb7aSDavid van Moolenbroek if (p[i].events & (POLLIN|POLLRDNORM))
71433d6423SLionel Sambuc FD_SET(p[i].fd, &rd);
72433d6423SLionel Sambuc if (p[i].events & (POLLOUT|POLLWRNORM|POLLWRBAND))
73433d6423SLionel Sambuc FD_SET(p[i].fd, &wr);
74*44fdeb7aSDavid van Moolenbroek if (p[i].events & (POLLRDBAND|POLLPRI))
75433d6423SLionel Sambuc FD_SET(p[i].fd, &except);
76433d6423SLionel Sambuc }
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc tv.tv_sec = timout / 1000;
79433d6423SLionel Sambuc tv.tv_usec = (timout % 1000) * 1000;
80433d6423SLionel Sambuc
81433d6423SLionel Sambuc rval = select(highfd + 1, &rd, &wr, &except,
82433d6423SLionel Sambuc timout == -1 ? NULL : &tv);
83433d6423SLionel Sambuc if (rval <= 0)
84433d6423SLionel Sambuc return rval;
85433d6423SLionel Sambuc
86433d6423SLionel Sambuc rval = 0;
87433d6423SLionel Sambuc for (i = 0; i < nfds; i++) {
886956dd2bSDavid van Moolenbroek if (p[i].fd < 0)
896956dd2bSDavid van Moolenbroek continue;
90433d6423SLionel Sambuc if (FD_ISSET(p[i].fd, &rd))
91*44fdeb7aSDavid van Moolenbroek p[i].revents |= p[i].events & (POLLIN|POLLRDNORM);
92433d6423SLionel Sambuc if (FD_ISSET(p[i].fd, &wr))
93*44fdeb7aSDavid van Moolenbroek p[i].revents |=
94*44fdeb7aSDavid van Moolenbroek p[i].events & (POLLOUT|POLLWRNORM|POLLWRBAND);
95433d6423SLionel Sambuc if (FD_ISSET(p[i].fd, &except))
96*44fdeb7aSDavid van Moolenbroek p[i].revents |= p[i].events & (POLLRDBAND|POLLPRI);
97*44fdeb7aSDavid van Moolenbroek /* XXX: POLLERR/POLLHUP/POLLNVAL? */
98433d6423SLionel Sambuc if (p[i].revents != 0)
99433d6423SLionel Sambuc rval++;
100433d6423SLionel Sambuc }
101433d6423SLionel Sambuc return rval;
102433d6423SLionel Sambuc }
103