2.9
[glibc/nacl-glibc.git] / sysdeps / unix / bsd / poll.c
blob0bddf0b275c3385cb0d3884462006de27f62ee48
1 /* Copyright (C) 1994,1996,1997,1998,1999,2001,2002
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <sys/poll.h>
22 #include <sys/types.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/time.h>
26 #include <sys/param.h>
27 #include <unistd.h>
29 /* Poll the file descriptors described by the NFDS structures starting at
30 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
31 an event to occur; if TIMEOUT is -1, block until an event occurs.
32 Returns the number of file descriptors with events, zero if timed out,
33 or -1 for errors. */
35 int
36 __poll (fds, nfds, timeout)
37 struct pollfd *fds;
38 nfds_t nfds;
39 int timeout;
41 static int max_fd_size;
42 struct timeval tv;
43 fd_set *rset, *wset, *xset;
44 struct pollfd *f;
45 int ready;
46 int maxfd = 0;
47 int bytes;
49 if (!max_fd_size)
50 max_fd_size = __getdtablesize ();
52 bytes = howmany (max_fd_size, __NFDBITS);
53 rset = alloca (bytes);
54 wset = alloca (bytes);
55 xset = alloca (bytes);
57 /* We can't call FD_ZERO, since FD_ZERO only works with sets
58 of exactly __FD_SETSIZE size. */
59 __bzero (rset, bytes);
60 __bzero (wset, bytes);
61 __bzero (xset, bytes);
63 for (f = fds; f < &fds[nfds]; ++f)
65 f->revents = 0;
66 if (f->fd >= 0)
68 if (f->fd >= max_fd_size)
70 /* The user provides a file descriptor number which is higher
71 than the maximum we got from the `getdtablesize' call.
72 Maybe this is ok so enlarge the arrays. */
73 fd_set *nrset, *nwset, *nxset;
74 int nbytes;
76 max_fd_size = roundup (f->fd, __NFDBITS);
77 nbytes = howmany (max_fd_size, __NFDBITS);
79 nrset = alloca (nbytes);
80 nwset = alloca (nbytes);
81 nxset = alloca (nbytes);
83 __bzero ((char *) nrset + bytes, nbytes - bytes);
84 __bzero ((char *) nwset + bytes, nbytes - bytes);
85 __bzero ((char *) nxset + bytes, nbytes - bytes);
87 rset = memcpy (nrset, rset, bytes);
88 wset = memcpy (nwset, wset, bytes);
89 xset = memcpy (nxset, xset, bytes);
91 bytes = nbytes;
94 if (f->events & POLLIN)
95 FD_SET (f->fd, rset);
96 if (f->events & POLLOUT)
97 FD_SET (f->fd, wset);
98 if (f->events & POLLPRI)
99 FD_SET (f->fd, xset);
100 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
101 maxfd = f->fd;
105 tv.tv_sec = timeout / 1000;
106 tv.tv_usec = (timeout % 1000) * 1000;
108 while (1)
110 ready = __select (maxfd + 1, rset, wset, xset,
111 timeout == -1 ? NULL : &tv);
113 /* It might be that one or more of the file descriptors is invalid.
114 We now try to find and mark them and then try again. */
115 if (ready == -1 && errno == EBADF)
117 fd_set *sngl_rset = alloca (bytes);
118 fd_set *sngl_wset = alloca (bytes);
119 fd_set *sngl_xset = alloca (bytes);
120 struct timeval sngl_tv;
122 /* Clear the original set. */
123 __bzero (rset, bytes);
124 __bzero (wset, bytes);
125 __bzero (xset, bytes);
127 /* This means we don't wait for input. */
128 sngl_tv.tv_sec = 0;
129 sngl_tv.tv_usec = 0;
131 maxfd = -1;
133 /* Reset the return value. */
134 ready = 0;
136 for (f = fds; f < &fds[nfds]; ++f)
137 if (f->fd != -1 && (f->events & (POLLIN|POLLOUT|POLLPRI))
138 && (f->revents & POLLNVAL) == 0)
140 int n;
142 __bzero (sngl_rset, bytes);
143 __bzero (sngl_wset, bytes);
144 __bzero (sngl_xset, bytes);
146 if (f->events & POLLIN)
147 FD_SET (f->fd, sngl_rset);
148 if (f->events & POLLOUT)
149 FD_SET (f->fd, sngl_wset);
150 if (f->events & POLLPRI)
151 FD_SET (f->fd, sngl_xset);
153 n = __select (f->fd + 1, sngl_rset, sngl_wset, sngl_xset,
154 &sngl_tv);
155 if (n != -1)
157 /* This descriptor is ok. */
158 if (f->events & POLLIN)
159 FD_SET (f->fd, rset);
160 if (f->events & POLLOUT)
161 FD_SET (f->fd, wset);
162 if (f->events & POLLPRI)
163 FD_SET (f->fd, xset);
164 if (f->fd > maxfd)
165 maxfd = f->fd;
166 if (n > 0)
167 /* Count it as being available. */
168 ++ready;
170 else if (errno == EBADF)
171 f->revents |= POLLNVAL;
173 /* Try again. */
174 continue;
177 break;
180 if (ready > 0)
181 for (f = fds; f < &fds[nfds]; ++f)
183 if (f->fd >= 0)
185 if (FD_ISSET (f->fd, rset))
186 f->revents |= POLLIN;
187 if (FD_ISSET (f->fd, wset))
188 f->revents |= POLLOUT;
189 if (FD_ISSET (f->fd, xset))
190 f->revents |= POLLPRI;
194 return ready;
196 #ifndef __poll
197 libc_hidden_def (__poll)
198 weak_alias (__poll, poll)
199 #endif