Set default charset to UTF-8 for libc.pot.
[glibc.git] / sysdeps / unix / bsd / poll.c
blob244c285072521ba89d0d9eaad7fbbf158cf94155
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <alloca.h>
20 #include <sys/poll.h>
21 #include <sys/types.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <sys/param.h>
26 #include <unistd.h>
28 /* Poll the file descriptors described by the NFDS structures starting at
29 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
30 an event to occur; if TIMEOUT is -1, block until an event occurs.
31 Returns the number of file descriptors with events, zero if timed out,
32 or -1 for errors. */
34 int
35 __poll (fds, nfds, timeout)
36 struct pollfd *fds;
37 nfds_t nfds;
38 int timeout;
40 static int max_fd_size;
41 struct timeval tv;
42 fd_set *rset, *wset, *xset;
43 struct pollfd *f;
44 int ready;
45 int maxfd = 0;
46 int bytes;
48 if (!max_fd_size)
49 max_fd_size = __getdtablesize ();
51 bytes = howmany (max_fd_size, __NFDBITS);
52 rset = alloca (bytes);
53 wset = alloca (bytes);
54 xset = alloca (bytes);
56 /* We can't call FD_ZERO, since FD_ZERO only works with sets
57 of exactly __FD_SETSIZE size. */
58 __bzero (rset, bytes);
59 __bzero (wset, bytes);
60 __bzero (xset, bytes);
62 for (f = fds; f < &fds[nfds]; ++f)
64 f->revents = 0;
65 if (f->fd >= 0)
67 if (f->fd >= max_fd_size)
69 /* The user provides a file descriptor number which is higher
70 than the maximum we got from the `getdtablesize' call.
71 Maybe this is ok so enlarge the arrays. */
72 fd_set *nrset, *nwset, *nxset;
73 int nbytes;
75 max_fd_size = roundup (f->fd, __NFDBITS);
76 nbytes = howmany (max_fd_size, __NFDBITS);
78 nrset = alloca (nbytes);
79 nwset = alloca (nbytes);
80 nxset = alloca (nbytes);
82 __bzero ((char *) nrset + bytes, nbytes - bytes);
83 __bzero ((char *) nwset + bytes, nbytes - bytes);
84 __bzero ((char *) nxset + bytes, nbytes - bytes);
86 rset = memcpy (nrset, rset, bytes);
87 wset = memcpy (nwset, wset, bytes);
88 xset = memcpy (nxset, xset, bytes);
90 bytes = nbytes;
93 if (f->events & POLLIN)
94 FD_SET (f->fd, rset);
95 if (f->events & POLLOUT)
96 FD_SET (f->fd, wset);
97 if (f->events & POLLPRI)
98 FD_SET (f->fd, xset);
99 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
100 maxfd = f->fd;
104 tv.tv_sec = timeout / 1000;
105 tv.tv_usec = (timeout % 1000) * 1000;
107 while (1)
109 ready = __select (maxfd + 1, rset, wset, xset,
110 timeout == -1 ? NULL : &tv);
112 /* It might be that one or more of the file descriptors is invalid.
113 We now try to find and mark them and then try again. */
114 if (ready == -1 && errno == EBADF)
116 fd_set *sngl_rset = alloca (bytes);
117 fd_set *sngl_wset = alloca (bytes);
118 fd_set *sngl_xset = alloca (bytes);
119 struct timeval sngl_tv;
121 /* Clear the original set. */
122 __bzero (rset, bytes);
123 __bzero (wset, bytes);
124 __bzero (xset, bytes);
126 /* This means we don't wait for input. */
127 sngl_tv.tv_sec = 0;
128 sngl_tv.tv_usec = 0;
130 maxfd = -1;
132 /* Reset the return value. */
133 ready = 0;
135 for (f = fds; f < &fds[nfds]; ++f)
136 if (f->fd != -1 && (f->events & (POLLIN|POLLOUT|POLLPRI))
137 && (f->revents & POLLNVAL) == 0)
139 int n;
141 __bzero (sngl_rset, bytes);
142 __bzero (sngl_wset, bytes);
143 __bzero (sngl_xset, bytes);
145 if (f->events & POLLIN)
146 FD_SET (f->fd, sngl_rset);
147 if (f->events & POLLOUT)
148 FD_SET (f->fd, sngl_wset);
149 if (f->events & POLLPRI)
150 FD_SET (f->fd, sngl_xset);
152 n = __select (f->fd + 1, sngl_rset, sngl_wset, sngl_xset,
153 &sngl_tv);
154 if (n != -1)
156 /* This descriptor is ok. */
157 if (f->events & POLLIN)
158 FD_SET (f->fd, rset);
159 if (f->events & POLLOUT)
160 FD_SET (f->fd, wset);
161 if (f->events & POLLPRI)
162 FD_SET (f->fd, xset);
163 if (f->fd > maxfd)
164 maxfd = f->fd;
165 if (n > 0)
166 /* Count it as being available. */
167 ++ready;
169 else if (errno == EBADF)
170 f->revents |= POLLNVAL;
172 /* Try again. */
173 continue;
176 break;
179 if (ready > 0)
180 for (f = fds; f < &fds[nfds]; ++f)
182 if (f->fd >= 0)
184 if (FD_ISSET (f->fd, rset))
185 f->revents |= POLLIN;
186 if (FD_ISSET (f->fd, wset))
187 f->revents |= POLLOUT;
188 if (FD_ISSET (f->fd, xset))
189 f->revents |= POLLPRI;
193 return ready;
195 #ifndef __poll
196 libc_hidden_def (__poll)
197 weak_alias (__poll, poll)
198 #endif