subscribe to mut fds

This commit is contained in:
Pat Hickey
2021-04-26 16:06:56 -07:00
parent 867d2c9a35
commit a532d0f379
4 changed files with 39 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
use crate::file::{FileCaps, FileEntryExt, TableFileExt};
use crate::file::{FileCaps, FileEntryExt, FileEntryMutExt, TableFileExt};
use crate::sched::{
subscription::{RwEventFlags, SubscriptionResult},
Poll,
@@ -7,6 +7,7 @@ use crate::snapshots::preview_1::types as snapshot1_types;
use crate::snapshots::preview_1::wasi_snapshot_preview1::WasiSnapshotPreview1 as Snapshot1;
use crate::{Error, ErrorExt, WasiCtx};
use cap_std::time::Duration;
use std::collections::HashSet;
use std::convert::{TryFrom, TryInto};
use std::io::{IoSlice, IoSliceMut};
use std::ops::Deref;
@@ -778,6 +779,7 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
}
let table = self.table();
let mut subscribed_fds = HashSet::new();
let mut poll = Poll::new();
let subs = subs.as_array(nsubscriptions);
@@ -816,15 +818,27 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
},
types::SubscriptionU::FdRead(readsub) => {
let fd = readsub.file_descriptor;
if subscribed_fds.contains(&fd) {
Err(Error::invalid_argument()
.context("Fd can be subscribed to at most once per poll_oneoff"))?;
} else {
subscribed_fds.insert(fd);
}
let file = table
.get_file(u32::from(fd))?
.get_file_mut(u32::from(fd))?
.get_cap(FileCaps::POLL_READWRITE)?;
poll.subscribe_read(file, sub.userdata.into());
}
types::SubscriptionU::FdWrite(writesub) => {
let fd = writesub.file_descriptor;
if subscribed_fds.contains(&fd) {
Err(Error::invalid_argument()
.context("Fd can be subscribed to at most once per poll_oneoff"))?;
} else {
subscribed_fds.insert(fd);
}
let file = table
.get_file(u32::from(fd))?
.get_file_mut(u32::from(fd))?
.get_cap(FileCaps::POLL_READWRITE)?;
poll.subscribe_write(file, sub.userdata.into());
}

View File

@@ -13,6 +13,7 @@ use crate::{
use anyhow::Context;
use cap_std::time::{Duration, SystemClock};
use std::cell::{Ref, RefMut};
use std::collections::HashSet;
use std::convert::{TryFrom, TryInto};
use std::io::{IoSlice, IoSliceMut};
use std::ops::{Deref, DerefMut};
@@ -970,6 +971,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
let table = self.table();
let mut subscribed_fds = HashSet::new();
let mut poll = Poll::new();
let subs = subs.as_array(nsubscriptions);
@@ -1008,15 +1010,27 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
},
types::SubscriptionU::FdRead(readsub) => {
let fd = readsub.file_descriptor;
if subscribed_fds.contains(&fd) {
return Err(Error::invalid_argument()
.context("Fd can be subscribed to at most once per poll_oneoff"));
} else {
subscribed_fds.insert(fd);
}
let file = table
.get_file(u32::from(fd))?
.get_file_mut(u32::from(fd))?
.get_cap(FileCaps::POLL_READWRITE)?;
poll.subscribe_read(file, sub.userdata.into());
}
types::SubscriptionU::FdWrite(writesub) => {
let fd = writesub.file_descriptor;
if subscribed_fds.contains(&fd) {
return Err(Error::invalid_argument()
.context("Fd can be subscribed to at most once per poll_oneoff"));
} else {
subscribed_fds.insert(fd);
}
let file = table
.get_file(u32::from(fd))?
.get_file_mut(u32::from(fd))?
.get_cap(FileCaps::POLL_READWRITE)?;
poll.subscribe_write(file, sub.userdata.into());
}