poll.c 5.8 KB

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