mod.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //! sys/select.h implementation
  2. use core::mem;
  3. use cbitset::BitSet;
  4. use crate::{
  5. fs::File,
  6. header::{
  7. errno,
  8. sys_epoll::{
  9. epoll_create1, epoll_ctl, epoll_data, epoll_event, epoll_wait, EPOLLERR, EPOLLIN,
  10. EPOLLOUT, EPOLL_CLOEXEC, EPOLL_CTL_ADD,
  11. },
  12. sys_time::timeval,
  13. },
  14. platform::{self, types::*},
  15. };
  16. // fd_set is also defined in C because cbindgen is incompatible with mem::size_of booo
  17. pub const FD_SETSIZE: usize = 1024;
  18. type bitset = BitSet<[u64; FD_SETSIZE / (8 * mem::size_of::<u64>())]>;
  19. #[repr(C)]
  20. pub struct fd_set {
  21. pub fds_bits: bitset,
  22. }
  23. pub fn select_epoll(
  24. nfds: c_int,
  25. readfds: Option<&mut fd_set>,
  26. writefds: Option<&mut fd_set>,
  27. exceptfds: Option<&mut fd_set>,
  28. timeout: Option<&mut timeval>,
  29. ) -> c_int {
  30. if nfds < 0 || nfds > FD_SETSIZE as i32 {
  31. unsafe { platform::errno = errno::EINVAL };
  32. return -1;
  33. };
  34. let ep = {
  35. let epfd = epoll_create1(EPOLL_CLOEXEC);
  36. if epfd < 0 {
  37. return -1;
  38. }
  39. File::new(epfd)
  40. };
  41. let mut read_bitset: Option<&mut bitset> = readfds.map(|fd_set| &mut fd_set.fds_bits);
  42. let mut write_bitset: Option<&mut bitset> = writefds.map(|fd_set| &mut fd_set.fds_bits);
  43. let mut except_bitset: Option<&mut bitset> = exceptfds.map(|fd_set| &mut fd_set.fds_bits);
  44. // Keep track of the number of file descriptors that do not support epoll
  45. let mut not_epoll = 0;
  46. for fd in 0..nfds {
  47. let mut events = 0;
  48. if let Some(ref fd_set) = read_bitset {
  49. if fd_set.contains(fd as usize) {
  50. events |= EPOLLIN;
  51. }
  52. }
  53. if let Some(ref fd_set) = write_bitset {
  54. if fd_set.contains(fd as usize) {
  55. events |= EPOLLOUT;
  56. }
  57. }
  58. if let Some(ref fd_set) = except_bitset {
  59. if fd_set.contains(fd as usize) {
  60. events |= EPOLLERR;
  61. }
  62. }
  63. if events > 0 {
  64. let mut event = epoll_event {
  65. events,
  66. data: epoll_data { fd },
  67. ..Default::default()
  68. };
  69. if epoll_ctl(*ep, EPOLL_CTL_ADD, fd, &mut event) < 0 {
  70. if unsafe { platform::errno == errno::EPERM } {
  71. not_epoll += 1;
  72. } else {
  73. return -1;
  74. }
  75. } else {
  76. if let Some(ref mut fd_set) = read_bitset {
  77. if fd_set.contains(fd as usize) {
  78. fd_set.remove(fd as usize);
  79. }
  80. }
  81. if let Some(ref mut fd_set) = write_bitset {
  82. if fd_set.contains(fd as usize) {
  83. fd_set.remove(fd as usize);
  84. }
  85. }
  86. if let Some(ref mut fd_set) = except_bitset {
  87. if fd_set.contains(fd as usize) {
  88. fd_set.remove(fd as usize);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. let mut events: [epoll_event; 32] = unsafe { mem::zeroed() };
  95. let epoll_timeout = if not_epoll > 0 {
  96. // Do not wait if any non-epoll file descriptors were found
  97. 0
  98. } else {
  99. match timeout {
  100. Some(timeout) => {
  101. //TODO: Check for overflow
  102. ((timeout.tv_sec as c_int) * 1000) + ((timeout.tv_usec as c_int) / 1000)
  103. }
  104. None => -1,
  105. }
  106. };
  107. let res = epoll_wait(
  108. *ep,
  109. events.as_mut_ptr(),
  110. events.len() as c_int,
  111. epoll_timeout,
  112. );
  113. if res < 0 {
  114. return -1;
  115. }
  116. let mut count = not_epoll;
  117. for event in events.iter().take(res as usize) {
  118. let fd = unsafe { event.data.fd };
  119. // TODO: Error status when fd does not match?
  120. if fd >= 0 && fd < FD_SETSIZE as c_int {
  121. if event.events & EPOLLIN > 0 {
  122. if let Some(ref mut fd_set) = read_bitset {
  123. fd_set.insert(fd as usize);
  124. count += 1;
  125. }
  126. }
  127. if event.events & EPOLLOUT > 0 {
  128. if let Some(ref mut fd_set) = write_bitset {
  129. fd_set.insert(fd as usize);
  130. count += 1;
  131. }
  132. }
  133. if event.events & EPOLLERR > 0 {
  134. if let Some(ref mut fd_set) = except_bitset {
  135. fd_set.insert(fd as usize);
  136. count += 1;
  137. }
  138. }
  139. }
  140. }
  141. count
  142. }
  143. #[no_mangle]
  144. pub unsafe extern "C" fn select(
  145. nfds: c_int,
  146. readfds: *mut fd_set,
  147. writefds: *mut fd_set,
  148. exceptfds: *mut fd_set,
  149. timeout: *mut timeval,
  150. ) -> c_int {
  151. trace_expr!(
  152. select_epoll(
  153. nfds,
  154. if readfds.is_null() {
  155. None
  156. } else {
  157. Some(&mut *readfds)
  158. },
  159. if writefds.is_null() {
  160. None
  161. } else {
  162. Some(&mut *writefds)
  163. },
  164. if exceptfds.is_null() {
  165. None
  166. } else {
  167. Some(&mut *exceptfds)
  168. },
  169. if timeout.is_null() {
  170. None
  171. } else {
  172. Some(&mut *timeout)
  173. }
  174. ),
  175. "select({}, {:p}, {:p}, {:p}, {:p})",
  176. nfds,
  177. readfds,
  178. writefds,
  179. exceptfds,
  180. timeout
  181. )
  182. }