poll.c 5.9 KB

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