poll.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <sys/syscall.h>
  16. #include <sys/poll.h>
  17. #include <bits/kernel-features.h>
  18. #include <cancel.h>
  19. #if defined __ASSUME_POLL_SYSCALL && defined __NR_poll
  20. #define __NR___poll_nocancel __NR_poll
  21. static _syscall3(int, __NC(poll), struct pollfd *, fds,
  22. unsigned long int, nfds, int, timeout)
  23. #else /* !__NR_poll */
  24. #include <alloca.h>
  25. #include <sys/types.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/time.h>
  29. #include <sys/param.h>
  30. #include <unistd.h>
  31. #include <sys/select.h>
  32. /* uClinux 2.0 doesn't have poll, emulate it using select */
  33. /* Poll the file descriptors described by the NFDS structures starting at
  34. FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
  35. an event to occur; if TIMEOUT is -1, block until an event occurs.
  36. Returns the number of file descriptors with events, zero if timed out,
  37. or -1 for errors. */
  38. int __NC(poll)(struct pollfd *fds, nfds_t nfds, int timeout)
  39. {
  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;
  47. if (!max_fd_size)
  48. max_fd_size = getdtablesize ();
  49. bytes = howmany (max_fd_size, __NFDBITS);
  50. rset = alloca (bytes);
  51. wset = alloca (bytes);
  52. xset = alloca (bytes);
  53. /* We can't call FD_ZERO, since FD_ZERO only works with sets
  54. of exactly __FD_SETSIZE size. */
  55. memset (rset, 0, bytes);
  56. memset (wset, 0, bytes);
  57. memset (xset, 0, bytes);
  58. for (f = fds; f < &fds[nfds]; ++f)
  59. {
  60. f->revents = 0;
  61. if (f->fd >= 0)
  62. {
  63. if (f->fd >= max_fd_size)
  64. {
  65. /* The user provides a file descriptor number which is higher
  66. than the maximum we got from the `getdtablesize' call.
  67. Maybe this is ok so enlarge the arrays. */
  68. fd_set *nrset, *nwset, *nxset;
  69. int nbytes;
  70. max_fd_size = roundup (f->fd, __NFDBITS);
  71. nbytes = howmany (max_fd_size, __NFDBITS);
  72. nrset = alloca (nbytes);
  73. nwset = alloca (nbytes);
  74. nxset = alloca (nbytes);
  75. memset ((char *) nrset + bytes, 0, nbytes - bytes);
  76. memset ((char *) nwset + bytes, 0, nbytes - bytes);
  77. memset ((char *) nxset + bytes, 0, nbytes - bytes);
  78. rset = memcpy (nrset, rset, bytes);
  79. wset = memcpy (nwset, wset, bytes);
  80. xset = memcpy (nxset, xset, bytes);
  81. bytes = nbytes;
  82. }
  83. if (f->events & POLLIN)
  84. FD_SET (f->fd, rset);
  85. if (f->events & POLLOUT)
  86. FD_SET (f->fd, wset);
  87. if (f->events & POLLPRI)
  88. FD_SET (f->fd, xset);
  89. if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
  90. maxfd = f->fd;
  91. }
  92. }
  93. tv.tv_sec = timeout / 1000;
  94. tv.tv_usec = (timeout % 1000) * 1000;
  95. while (1)
  96. {
  97. ready = __NC(select) (maxfd + 1, rset, wset, xset,
  98. timeout == -1 ? NULL : &tv);
  99. /* It might be that one or more of the file descriptors is invalid.
  100. We now try to find and mark them and then try again. */
  101. if (ready == -1 && errno == EBADF)
  102. {
  103. fd_set *sngl_rset = alloca (bytes);
  104. fd_set *sngl_wset = alloca (bytes);
  105. fd_set *sngl_xset = alloca (bytes);
  106. struct timeval sngl_tv;
  107. /* Clear the original set. */
  108. memset (rset, 0, bytes);
  109. memset (wset, 0, bytes);
  110. memset (xset, 0, bytes);
  111. /* This means we don't wait for input. */
  112. sngl_tv.tv_sec = 0;
  113. sngl_tv.tv_usec = 0;
  114. maxfd = -1;
  115. /* Reset the return value. */
  116. ready = 0;
  117. for (f = fds; f < &fds[nfds]; ++f)
  118. if (f->fd != -1 && (f->events & (POLLIN|POLLOUT|POLLPRI))
  119. && (f->revents & POLLNVAL) == 0)
  120. {
  121. int n;
  122. memset (sngl_rset, 0, bytes);
  123. memset (sngl_wset, 0, bytes);
  124. memset (sngl_xset, 0, bytes);
  125. if (f->events & POLLIN)
  126. FD_SET (f->fd, sngl_rset);
  127. if (f->events & POLLOUT)
  128. FD_SET (f->fd, sngl_wset);
  129. if (f->events & POLLPRI)
  130. FD_SET (f->fd, sngl_xset);
  131. n = __NC(select) (f->fd + 1, sngl_rset, sngl_wset, sngl_xset,
  132. &sngl_tv);
  133. if (n != -1)
  134. {
  135. /* This descriptor is ok. */
  136. if (f->events & POLLIN)
  137. FD_SET (f->fd, rset);
  138. if (f->events & POLLOUT)
  139. FD_SET (f->fd, wset);
  140. if (f->events & POLLPRI)
  141. FD_SET (f->fd, xset);
  142. if (f->fd > maxfd)
  143. maxfd = f->fd;
  144. if (n > 0)
  145. /* Count it as being available. */
  146. ++ready;
  147. }
  148. else if (errno == EBADF)
  149. f->revents |= POLLNVAL;
  150. }
  151. /* Try again. */
  152. continue;
  153. }
  154. break;
  155. }
  156. if (ready > 0)
  157. for (f = fds; f < &fds[nfds]; ++f)
  158. {
  159. if (f->fd >= 0)
  160. {
  161. if (FD_ISSET (f->fd, rset))
  162. f->revents |= POLLIN;
  163. if (FD_ISSET (f->fd, wset))
  164. f->revents |= POLLOUT;
  165. if (FD_ISSET (f->fd, xset))
  166. f->revents |= POLLPRI;
  167. }
  168. }
  169. return ready;
  170. }
  171. #endif
  172. CANCELLABLE_SYSCALL(int, poll, (struct pollfd *fds, nfds_t nfds, int timeout),
  173. (fds, nfds, timeout))
  174. lt_libc_hidden(poll)