Skip to content

Commit

Permalink
Pass through poll_oneof
Browse files Browse the repository at this point in the history
it's better than nothing
  • Loading branch information
kateinoigakukun committed May 23, 2023
1 parent 132aeec commit 9e70c75
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/wasi_snapshot_preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,49 @@ pub(crate) unsafe fn poll_oneoff<S: Storage>(
out: *mut Event,
nsubscriptions: u32,
) -> Result<Size, Error> {
Err(wasi::ERRNO_NOTSUP.into())
let in_ = slice::from_raw_parts(in_, nsubscriptions as usize);
let mut new_in = Vec::<Subscription>::new();
for sub in in_ {
match sub.u.tag {
0 /* EVENTTYPE_CLOCK */ => {
new_in.push(*sub);
}
1 | 2 /* EVENTTYPE_FD_READ | EVENTTYPE_FD_WRITE */ => {
let mut new_sub = *sub;
let fd = if sub.u.tag == 1 {
sub.u.u.fd_read.file_descriptor
} else {
sub.u.u.fd_write.file_descriptor
};
let fd = fs.get_backing_fd(fd)?;
let new_fd = match fd {
BackingFd::Virtual(_) => return Err(wasi::ERRNO_NOTSUP.into()),
BackingFd::Wasi(fd) => fd,
};

if sub.u.tag == 1 {
new_sub.u.u.fd_read.file_descriptor = new_fd;
} else {
new_sub.u.u.fd_write.file_descriptor = new_fd;
}

new_in.push(new_sub);
}
_ => return Err(wasi::ERRNO_INVAL.into()),
}
}
let mut rp0 = MaybeUninit::<Fdstat>::uninit();
let ret = wasi::wasi_snapshot_preview1::poll_oneoff(
new_in.as_ptr() as i32,
out as i32,
nsubscriptions as i32,
rp0.as_mut_ptr() as i32,
);

match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
_ => Err(Error(ret as u16)),
}
}

fn read_bytes<R: std::io::Read>(mut src: R, iovs: wasi::IovecArray) -> Result<usize, wasi::Errno> {
Expand Down
13 changes: 13 additions & 0 deletions tests/run-make/polling/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-include ../tools.mk

objs = $(TMPDIR)/main.c.o

check: $(objs)
$(CC) $(LDFLAGS) $(objs) $(LIB_WASI_VFS) -o $(TMPDIR)/main.wasm
$(WASI_VFS_CLI) pack $(TMPDIR)/main.wasm --mapdir /dev::./dev -o $(TMPDIR)/main.packed.wasm
$(WASI_RUN) --mapdir /dev::./dev $(TMPDIR)/main.wasm
# TODO(katei): Currently, poll_oneoff is not supported for vfs fd
# $(WASI_RUN) --mapdir /dev::./dev $(TMPDIR)/main.packed.wasm

clean:
rm -rf $(PROG) $(objs)
1 change: 1 addition & 0 deletions tests/run-make/polling/dev/polling
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
20 changes: 20 additions & 0 deletions tests/run-make/polling/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(int argc, char *argv[]) {
int ret = clock_nanosleep(CLOCK_MONOTONIC, 0, 0, 0);
assert(ret == 0);

// Check ioctl(fd, FIONREAD, ...)
int fd = open("/dev/polling", O_RDONLY);
assert(fd >= 0);
int val = 0;
ret = ioctl(fd, FIONREAD, &val);
assert(ret == 0);
assert(val == 6); // "hello\n"

return 0;
}

0 comments on commit 9e70c75

Please sign in to comment.