From 1edaad1b20a2c9396947ff1d811f4d082ec16830 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 5 May 2024 15:47:13 +0900 Subject: [PATCH] Add wasi select, FD_SET, FD_ZERO, FD_ISSET --- src/wasi.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/wasi.rs b/src/wasi.rs index 6b8177aa86cb2..6ab7285f03359 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -178,6 +178,11 @@ s! { pub st_ctim: timespec, __reserved: [c_longlong; 3], } + + pub struct fd_set { + __nfds: usize, + __fds: [c_int; FD_SETSIZE as usize], + } } // Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash, @@ -446,6 +451,28 @@ pub const NOEXPR: ::nl_item = 0x50001; pub const YESSTR: ::nl_item = 0x50002; pub const NOSTR: ::nl_item = 0x50003; +f! { + pub fn FD_ISSET(fd: ::c_int, set: *const fd_set) -> bool { + let set = &*set; + let n = set.__nfds; + return set.__fds[..n].iter().any(|p| *p == fd) + } + + pub fn FD_SET(fd: ::c_int, set: *mut fd_set) -> () { + let set = &mut *set; + let n = set.__nfds; + if !set.__fds[..n].iter().any(|p| *p == fd) { + set.__nfds = n + 1; + set.__fds[n] = fd; + } + } + + pub fn FD_ZERO(set: *mut fd_set) -> () { + (*set).__nfds = 0; + return + } +} + #[cfg_attr( feature = "rustc-dep-of-std", link( @@ -741,6 +768,14 @@ extern "C" { pub fn nl_langinfo(item: ::nl_item) -> *mut ::c_char; pub fn nl_langinfo_l(item: ::nl_item, loc: ::locale_t) -> *mut ::c_char; + pub fn select( + nfds: c_int, + readfds: *mut fd_set, + writefds: *mut fd_set, + errorfds: *mut fd_set, + timeout: *const timeval, + ) -> c_int; + pub fn __wasilibc_register_preopened_fd(fd: c_int, path: *const c_char) -> c_int; pub fn __wasilibc_fd_renumber(fd: c_int, newfd: c_int) -> c_int; pub fn __wasilibc_unlinkat(fd: c_int, path: *const c_char) -> c_int;