poll.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.
  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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <alloca.h>
  17. #include <sys/poll.h>
  18. #include <sys/types.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <sys/time.h>
  22. #include <sys/param.h>
  23. #include <unistd.h>
  24. /* Poll the file descriptors described by the NFDS structures starting at
  25. FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
  26. an event to occur; if TIMEOUT is -1, block until an event occurs.
  27. Returns the number of file descriptors with events, zero if timed out,
  28. or -1 for errors. */
  29. int poll(struct pollfd *fds, nfds_t nfds, int timeout)
  30. {
  31. static int max_fd_size;
  32. struct timeval tv;
  33. fd_set *rset, *wset, *xset;
  34. struct pollfd *f;
  35. int ready;
  36. int maxfd = 0;
  37. int bytes;
  38. if (!max_fd_size)
  39. max_fd_size = getdtablesize ();
  40. bytes = howmany (max_fd_size, __NFDBITS);
  41. rset = alloca (bytes);
  42. wset = alloca (bytes);
  43. xset = alloca (bytes);
  44. /* We can't call FD_ZERO, since FD_ZERO only works with sets
  45. of exactly __FD_SETSIZE size. */
  46. bzero (rset, bytes);
  47. bzero (wset, bytes);
  48. bzero (xset, bytes);
  49. for (f = fds; f < &fds[nfds]; ++f)
  50. {
  51. f->revents = 0;
  52. if (f->fd >= 0)
  53. {
  54. if (f->fd >= max_fd_size)
  55. {
  56. /* The user provides a file descriptor number which is higher
  57. than the maximum we got from the `getdtablesize' call.
  58. Maybe this is ok so enlarge the arrays. */
  59. fd_set *nrset, *nwset, *nxset;
  60. int nbytes;
  61. max_fd_size = roundup (f->fd, __NFDBITS);
  62. nbytes = howmany (max_fd_size, __NFDBITS);
  63. nrset = alloca (nbytes);
  64. nwset = alloca (nbytes);
  65. nxset = alloca (nbytes);
  66. bzero ((char *) nrset + bytes, nbytes - bytes);
  67. bzero ((char *) nwset + bytes, nbytes - bytes);
  68. bzero ((char *) nxset + bytes, nbytes - bytes);
  69. rset = memcpy (nrset, rset, bytes);
  70. wset = memcpy (nwset, wset, bytes);
  71. xset = memcpy (nxset, xset, bytes);
  72. bytes = nbytes;
  73. }
  74. if (f->events & POLLIN)
  75. FD_SET (f->fd, rset);
  76. if (f->events & POLLOUT)
  77. FD_SET (f->fd, wset);
  78. if (f->events & POLLPRI)
  79. FD_SET (f->fd, xset);
  80. if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
  81. maxfd = f->fd;
  82. }
  83. }
  84. tv.tv_sec = timeout / 1000;
  85. tv.tv_usec = (timeout % 1000) * 1000;
  86. while (1)
  87. {
  88. ready = select (maxfd + 1, rset, wset, xset,
  89. timeout == -1 ? NULL : &tv);
  90. /* It might be that one or more of the file descriptors is invalid.
  91. We now try to find and mark them and then try again. */
  92. if (ready == -1 && errno == EBADF)
  93. {
  94. fd_set *sngl_rset = alloca (bytes);
  95. fd_set *sngl_wset = alloca (bytes);
  96. fd_set *sngl_xset = alloca (bytes);
  97. struct timeval sngl_tv;
  98. /* Clear the original set. */
  99. bzero (rset, bytes);
  100. bzero (wset, bytes);
  101. bzero (xset, bytes);
  102. /* This means we don't wait for input. */
  103. sngl_tv.tv_sec = 0;
  104. sngl_tv.tv_usec = 0;
  105. maxfd = -1;
  106. /* Reset the return value. */
  107. ready = 0;
  108. for (f = fds; f < &fds[nfds]; ++f)
  109. if (f->fd != -1 && (f->events & (POLLIN|POLLOUT|POLLPRI))
  110. && (f->revents & POLLNVAL) == 0)
  111. {
  112. int n;
  113. bzero (sngl_rset, bytes);
  114. bzero (sngl_wset, bytes);
  115. bzero (sngl_xset, bytes);
  116. if (f->events & POLLIN)
  117. FD_SET (f->fd, sngl_rset);
  118. if (f->events & POLLOUT)
  119. FD_SET (f->fd, sngl_wset);
  120. if (f->events & POLLPRI)
  121. FD_SET (f->fd, sngl_xset);
  122. n = select (f->fd + 1, sngl_rset, sngl_wset, sngl_xset,
  123. &sngl_tv);
  124. if (n != -1)
  125. {
  126. /* This descriptor is ok. */
  127. if (f->events & POLLIN)
  128. FD_SET (f->fd, rset);
  129. if (f->events & POLLOUT)
  130. FD_SET (f->fd, wset);
  131. if (f->events & POLLPRI)
  132. FD_SET (f->fd, xset);
  133. if (f->fd > maxfd)
  134. maxfd = f->fd;
  135. if (n > 0)
  136. /* Count it as being available. */
  137. ++ready;
  138. }
  139. else if (errno == EBADF)
  140. f->revents |= POLLNVAL;
  141. }
  142. /* Try again. */
  143. continue;
  144. }
  145. break;
  146. }
  147. if (ready > 0)
  148. for (f = fds; f < &fds[nfds]; ++f)
  149. {
  150. if (f->fd >= 0)
  151. {
  152. if (FD_ISSET (f->fd, rset))
  153. f->revents |= POLLIN;
  154. if (FD_ISSET (f->fd, wset))
  155. f->revents |= POLLOUT;
  156. if (FD_ISSET (f->fd, xset))
  157. f->revents |= POLLPRI;
  158. }
  159. }
  160. return ready;
  161. }