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
22 #include <sys/types.h>
26 #include <sys/param.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,
36 __poll (fds
, nfds
, timeout
)
41 static int max_fd_size
;
43 fd_set
*rset
, *wset
, *xset
;
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
)
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
;
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
);
94 if (f
->events
& POLLIN
)
96 if (f
->events
& POLLOUT
)
98 if (f
->events
& POLLPRI
)
100 if (f
->fd
> maxfd
&& (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
)))
105 tv
.tv_sec
= timeout
/ 1000;
106 tv
.tv_usec
= (timeout
% 1000) * 1000;
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. */
133 /* Reset the return value. */
136 for (f
= fds
; f
< &fds
[nfds
]; ++f
)
137 if (f
->fd
!= -1 && (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
))
138 && (f
->revents
& POLLNVAL
) == 0)
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
,
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
);
167 /* Count it as being available. */
170 else if (errno
== EBADF
)
171 f
->revents
|= POLLNVAL
;
181 for (f
= fds
; f
< &fds
[nfds
]; ++f
)
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
;
197 libc_hidden_def (__poll
)
198 weak_alias (__poll
, poll
)