select.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-12-28 Bernard first version
  9. */
  10. #include <dfs.h>
  11. #include <dfs_fs.h>
  12. #include <dfs_posix.h>
  13. #include <dfs_poll.h>
  14. #include <dfs_select.h>
  15. static void fdszero(fd_set *set, int nfds)
  16. {
  17. fd_mask *m;
  18. int n;
  19. /*
  20. The 'sizeof(fd_set)' of the system space may differ from user space,
  21. so the actual size of the 'fd_set' is determined here with the parameter 'nfds'
  22. */
  23. m = (fd_mask *)set;
  24. for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8))
  25. {
  26. rt_memset(m, 0, sizeof(fd_mask));
  27. m ++;
  28. }
  29. }
  30. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
  31. {
  32. int fd;
  33. int npfds;
  34. int msec;
  35. int ndx;
  36. int ret;
  37. struct pollfd *pollset = RT_NULL;
  38. /* How many pollfd structures do we need to allocate? */
  39. for (fd = 0, npfds = 0; fd < nfds; fd++)
  40. {
  41. /* Check if any monitor operation is requested on this fd */
  42. if ((readfds && FD_ISSET(fd, readfds)) ||
  43. (writefds && FD_ISSET(fd, writefds)) ||
  44. (exceptfds && FD_ISSET(fd, exceptfds)))
  45. {
  46. npfds++;
  47. }
  48. }
  49. /* Allocate the descriptor list for poll() */
  50. if (npfds > 0)
  51. {
  52. pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd));
  53. if (!pollset)
  54. {
  55. return -1;
  56. }
  57. }
  58. /* Initialize the descriptor list for poll() */
  59. for (fd = 0, ndx = 0; fd < nfds; fd++)
  60. {
  61. int incr = 0;
  62. /* The readfs set holds the set of FDs that the caller can be assured
  63. * of reading from without blocking. Note that POLLHUP is included as
  64. * a read-able condition. POLLHUP will be reported at the end-of-file
  65. * or when a connection is lost. In either case, the read() can then
  66. * be performed without blocking.
  67. */
  68. if (readfds && FD_ISSET(fd, readfds))
  69. {
  70. pollset[ndx].fd = fd;
  71. pollset[ndx].events |= POLLIN;
  72. incr = 1;
  73. }
  74. if (writefds && FD_ISSET(fd, writefds))
  75. {
  76. pollset[ndx].fd = fd;
  77. pollset[ndx].events |= POLLOUT;
  78. incr = 1;
  79. }
  80. if (exceptfds && FD_ISSET(fd, exceptfds))
  81. {
  82. pollset[ndx].fd = fd;
  83. incr = 1;
  84. }
  85. ndx += incr;
  86. }
  87. RT_ASSERT(ndx == npfds);
  88. /* Convert the timeout to milliseconds */
  89. if (timeout)
  90. {
  91. msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  92. }
  93. else
  94. {
  95. msec = -1;
  96. }
  97. /* Then let poll do all of the real work. */
  98. ret = poll(pollset, npfds, msec);
  99. /* Now set up the return values */
  100. if (readfds)
  101. {
  102. fdszero(readfds, nfds);
  103. }
  104. if (writefds)
  105. {
  106. fdszero(writefds, nfds);
  107. }
  108. if (exceptfds)
  109. {
  110. fdszero(exceptfds, nfds);
  111. }
  112. /* Convert the poll descriptor list back into selects 3 bitsets */
  113. if (ret > 0)
  114. {
  115. ret = 0;
  116. for (ndx = 0; ndx < npfds; ndx++)
  117. {
  118. /* Check for read conditions. Note that POLLHUP is included as a
  119. * read condition. POLLHUP will be reported when no more data will
  120. * be available (such as when a connection is lost). In either
  121. * case, the read() can then be performed without blocking.
  122. */
  123. if (readfds)
  124. {
  125. if (pollset[ndx].revents & (POLLIN | POLLHUP))
  126. {
  127. FD_SET(pollset[ndx].fd, readfds);
  128. ret++;
  129. }
  130. }
  131. /* Check for write conditions */
  132. if (writefds)
  133. {
  134. if (pollset[ndx].revents & POLLOUT)
  135. {
  136. FD_SET(pollset[ndx].fd, writefds);
  137. ret++;
  138. }
  139. }
  140. /* Check for exceptions */
  141. if (exceptfds)
  142. {
  143. if (pollset[ndx].revents & POLLERR)
  144. {
  145. FD_SET(pollset[ndx].fd, exceptfds);
  146. ret++;
  147. }
  148. }
  149. }
  150. }
  151. if (pollset) rt_free(pollset);
  152. return ret;
  153. }