1 /* Copyright (C) 1994,1996,1997,1998,1999,2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <sys/types.h>
25 #include <sys/param.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,
35 __poll (fds
, nfds
, timeout
)
40 static int max_fd_size
;
42 fd_set
*rset
, *wset
, *xset
;
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
)
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
;
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
);
93 if (f
->events
& POLLIN
)
95 if (f
->events
& POLLOUT
)
97 if (f
->events
& POLLPRI
)
99 if (f
->fd
> maxfd
&& (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
)))
104 tv
.tv_sec
= timeout
/ 1000;
105 tv
.tv_usec
= (timeout
% 1000) * 1000;
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. */
132 /* Reset the return value. */
135 for (f
= fds
; f
< &fds
[nfds
]; ++f
)
136 if (f
->fd
!= -1 && (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
))
137 && (f
->revents
& POLLNVAL
) == 0)
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
,
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
);
166 /* Count it as being available. */
169 else if (errno
== EBADF
)
170 f
->revents
|= POLLNVAL
;
180 for (f
= fds
; f
< &fds
[nfds
]; ++f
)
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
;
196 weak_alias (__poll
, poll
)