poll.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <sys/syscall.h>
  17. #include <sys/poll.h>
  18. #ifdef __NR_poll
  19. _syscall3(int, poll, struct pollfd *, fds,
  20. unsigned long int, nfds, int, timeout)
  21. #elif defined(__NR_ppoll) && defined __UCLIBC_LINUX_SPECIFIC__
  22. int poll(struct pollfd *fds, nfds_t nfds, int timeout)
  23. {
  24. struct timespec *ts = NULL, tval;
  25. if (timeout > 0) {
  26. tval.tv_sec = timeout / 1000;
  27. tval.tv_nsec = (timeout % 1000) * 1000000;
  28. ts = &tval;
  29. } else if (timeout == 0) {
  30. tval.tv_sec = 0;
  31. tval.tv_nsec = 0;
  32. ts = &tval;
  33. }
  34. return ppoll(fds, nfds, ts, NULL);
  35. }
  36. #else
  37. /* ugh, this arch lacks poll, so we need to emulate this crap ... */
  38. #include <alloca.h>
  39. #include <sys/types.h>
  40. #include <errno.h>
  41. #include <string.h>
  42. #include <sys/time.h>
  43. #include <sys/param.h>
  44. #include <unistd.h>
  45. /* uClinux 2.0 doesn't have poll, emulate it using select */
  46. /* Poll the file descriptors described by the NFDS structures starting at
  47. FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
  48. an event to occur; if TIMEOUT is -1, block until an event occurs.
  49. Returns the number of file descriptors with events, zero if timed out,
  50. or -1 for errors. */
  51. int poll(struct pollfd *fds, nfds_t nfds, int timeout)
  52. {
  53. static int max_fd_size;
  54. struct timeval tv;
  55. fd_set *rset, *wset, *xset;
  56. struct pollfd *f;
  57. int ready;
  58. int maxfd = 0;
  59. int bytes;
  60. if (!max_fd_size)
  61. max_fd_size = getdtablesize ();
  62. bytes = howmany (max_fd_size, __NFDBITS);
  63. rset = alloca (bytes);
  64. wset = alloca (bytes);
  65. xset = alloca (bytes);
  66. /* We can't call FD_ZERO, since FD_ZERO only works with sets
  67. of exactly __FD_SETSIZE size. */
  68. memset (rset, 0, bytes);
  69. memset (wset, 0, bytes);
  70. memset (xset, 0, bytes);
  71. for (f = fds; f < &fds[nfds]; ++f)
  72. {
  73. f->revents = 0;
  74. if (f->fd >= 0)
  75. {
  76. if (f->fd >= max_fd_size)
  77. {
  78. /* The user provides a file descriptor number which is higher
  79. than the maximum we got from the `getdtablesize' call.
  80. Maybe this is ok so enlarge the arrays. */
  81. fd_set *nrset, *nwset, *nxset;
  82. int nbytes;
  83. max_fd_size = roundup (f->fd, __NFDBITS);
  84. nbytes = howmany (max_fd_size, __NFDBITS);
  85. nrset = alloca (nbytes);
  86. nwset = alloca (nbytes);
  87. nxset = alloca (nbytes);
  88. memset ((char *) nrset + bytes, 0, nbytes - bytes);
  89. memset ((char *) nwset + bytes, 0, nbytes - bytes);
  90. memset ((char *) nxset + bytes, 0, nbytes - bytes);
  91. rset = memcpy (nrset, rset, bytes);
  92. wset = memcpy (nwset, wset, bytes);
  93. xset = memcpy (nxset, xset, bytes);
  94. bytes = nbytes;
  95. }
  96. if (f->events & POLLIN)
  97. FD_SET (f->fd, rset);
  98. if (f->events & POLLOUT)
  99. FD_SET (f->fd, wset);
  100. if (f->events & POLLPRI)
  101. FD_SET (f->fd, xset);
  102. if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
  103. maxfd = f->fd;
  104. }
  105. }
  106. tv.tv_sec = timeout / 1000;
  107. tv.tv_usec = (timeout % 1000) * 1000;
  108. while (1)
  109. {
  110. ready = select (maxfd + 1, rset, wset, xset,
  111. 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)
  115. {
  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;
  120. /* Clear the original set. */
  121. memset (rset, 0, bytes);
  122. memset (wset, 0, bytes);
  123. memset (xset, 0, bytes);
  124. /* This means we don't wait for input. */
  125. sngl_tv.tv_sec = 0;
  126. sngl_tv.tv_usec = 0;
  127. maxfd = -1;
  128. /* Reset the return value. */
  129. ready = 0;
  130. for (f = fds; f < &fds[nfds]; ++f)
  131. if (f->fd != -1 && (f->events & (POLLIN|POLLOUT|POLLPRI))
  132. && (f->revents & POLLNVAL) == 0)
  133. {
  134. int n;
  135. memset (sngl_rset, 0, bytes);
  136. memset (sngl_wset, 0, bytes);
  137. memset (sngl_xset, 0, bytes);
  138. if (f->events & POLLIN)
  139. FD_SET (f->fd, sngl_rset);
  140. if (f->events & POLLOUT)
  141. FD_SET (f->fd, sngl_wset);
  142. if (f->events & POLLPRI)
  143. FD_SET (f->fd, sngl_xset);
  144. n = select (f->fd + 1, sngl_rset, sngl_wset, sngl_xset,
  145. &sngl_tv);
  146. if (n != -1)
  147. {
  148. /* This descriptor is ok. */
  149. if (f->events & POLLIN)
  150. FD_SET (f->fd, rset);
  151. if (f->events & POLLOUT)
  152. FD_SET (f->fd, wset);
  153. if (f->events & POLLPRI)
  154. FD_SET (f->fd, xset);
  155. if (f->fd > maxfd)
  156. maxfd = f->fd;
  157. if (n > 0)
  158. /* Count it as being available. */
  159. ++ready;
  160. }
  161. else if (errno == EBADF)
  162. f->revents |= POLLNVAL;
  163. }
  164. /* Try again. */
  165. continue;
  166. }
  167. break;
  168. }
  169. if (ready > 0)
  170. for (f = fds; f < &fds[nfds]; ++f)
  171. {
  172. if (f->fd >= 0)
  173. {
  174. if (FD_ISSET (f->fd, rset))
  175. f->revents |= POLLIN;
  176. if (FD_ISSET (f->fd, wset))
  177. f->revents |= POLLOUT;
  178. if (FD_ISSET (f->fd, xset))
  179. f->revents |= POLLPRI;
  180. }
  181. }
  182. return ready;
  183. }
  184. #endif
  185. #ifndef __LINUXTHREADS_OLD__
  186. libc_hidden_def(poll)
  187. #else
  188. libc_hidden_weak(poll)
  189. strong_alias(poll,__libc_poll)
  190. #endif