poll.c 5.9 KB

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