select.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * File : select.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2016-12-28 Bernard first version
  23. */
  24. #include <dfs.h>
  25. #include <dfs_fs.h>
  26. #include <dfs_posix.h>
  27. #include <dfs_poll.h>
  28. #include <dfs_select.h>
  29. static void fdzero(fd_set *set, int nfds)
  30. {
  31. fd_mask *m;
  32. int n;
  33. /*
  34. The 'sizeof(fd_set)' of the system space may differ from user space,
  35. so the actual size of the 'fd_set' is determined here with the parameter 'nfds'
  36. */
  37. m = (fd_mask*)set;
  38. for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8))
  39. {
  40. rt_memset(m, 0, sizeof(fd_mask));
  41. m ++;
  42. }
  43. }
  44. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
  45. {
  46. int fd;
  47. int npfds;
  48. int msec;
  49. int ndx;
  50. int ret;
  51. struct pollfd *pollset = RT_NULL;
  52. /* How many pollfd structures do we need to allocate? */
  53. for (fd = 0, npfds = 0; fd < nfds; fd++)
  54. {
  55. /* Check if any monitor operation is requested on this fd */
  56. if ((readfds && FD_ISSET(fd, readfds)) ||
  57. (writefds && FD_ISSET(fd, writefds)) ||
  58. (exceptfds && FD_ISSET(fd, exceptfds)))
  59. {
  60. npfds++;
  61. }
  62. }
  63. /* Allocate the descriptor list for poll() */
  64. if (npfds > 0)
  65. {
  66. pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd));
  67. if (!pollset)
  68. {
  69. return -1;
  70. }
  71. }
  72. /* Initialize the descriptor list for poll() */
  73. for (fd = 0, ndx = 0; fd < nfds; fd++)
  74. {
  75. int incr = 0;
  76. /* The readfs set holds the set of FDs that the caller can be assured
  77. * of reading from without blocking. Note that POLLHUP is included as
  78. * a read-able condition. POLLHUP will be reported at the end-of-file
  79. * or when a connection is lost. In either case, the read() can then
  80. * be performed without blocking.
  81. */
  82. if (readfds && FD_ISSET(fd, readfds))
  83. {
  84. pollset[ndx].fd = fd;
  85. pollset[ndx].events |= POLLIN;
  86. incr = 1;
  87. }
  88. if (writefds && FD_ISSET(fd, writefds))
  89. {
  90. pollset[ndx].fd = fd;
  91. pollset[ndx].events |= POLLOUT;
  92. incr = 1;
  93. }
  94. if (exceptfds && FD_ISSET(fd, exceptfds))
  95. {
  96. pollset[ndx].fd = fd;
  97. incr = 1;
  98. }
  99. ndx += incr;
  100. }
  101. RT_ASSERT(ndx == npfds);
  102. /* Convert the timeout to milliseconds */
  103. if (timeout)
  104. {
  105. msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  106. }
  107. else
  108. {
  109. msec = -1;
  110. }
  111. /* Then let poll do all of the real work. */
  112. ret = poll(pollset, npfds, msec);
  113. /* Now set up the return values */
  114. if (readfds)
  115. {
  116. fdzero(readfds, nfds);
  117. }
  118. if (writefds)
  119. {
  120. fdzero(writefds, nfds);
  121. }
  122. if (exceptfds)
  123. {
  124. fdzero(exceptfds, nfds);
  125. }
  126. /* Convert the poll descriptor list back into selects 3 bitsets */
  127. if (ret > 0)
  128. {
  129. ret = 0;
  130. for (ndx = 0; ndx < npfds; ndx++)
  131. {
  132. /* Check for read conditions. Note that POLLHUP is included as a
  133. * read condition. POLLHUP will be reported when no more data will
  134. * be available (such as when a connection is lost). In either
  135. * case, the read() can then be performed without blocking.
  136. */
  137. if (readfds)
  138. {
  139. if (pollset[ndx].revents & (POLLIN | POLLHUP))
  140. {
  141. FD_SET(pollset[ndx].fd, readfds);
  142. ret++;
  143. }
  144. }
  145. /* Check for write conditions */
  146. if (writefds)
  147. {
  148. if (pollset[ndx].revents & POLLOUT)
  149. {
  150. FD_SET(pollset[ndx].fd, writefds);
  151. ret++;
  152. }
  153. }
  154. /* Check for exceptions */
  155. if (exceptfds)
  156. {
  157. if (pollset[ndx].revents & POLLERR)
  158. {
  159. FD_SET(pollset[ndx].fd, exceptfds);
  160. ret++;
  161. }
  162. }
  163. }
  164. }
  165. if (pollset) rt_free(pollset);
  166. return ret;
  167. }