Implement sock_accept
With the addition of `sock_accept()` in `wasi-0.11.0`, wasmtime can now
implement basic networking for pre-opened sockets.
For Windows `AsHandle` was replaced with `AsRawHandleOrSocket` to cope
with the duality of Handles and Sockets.
For Unix a `wasi_cap_std_sync::net::Socket` enum was created to handle
the {Tcp,Unix}{Listener,Stream} more efficiently in
`WasiCtxBuilder::preopened_socket()`.
The addition of that many `WasiFile` implementors was mainly necessary,
because of the difference in the `num_ready_bytes()` function.
A known issue is Windows now busy polling on sockets, because except
for `stdin`, nothing is querying the status of windows handles/sockets.
Another know issue on Windows, is that there is no crate providing
support for `fcntl(fd, F_GETFL, 0)` on a socket.
Signed-off-by: Harald Hoyer <harald@profian.com>
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -3135,6 +3135,7 @@ dependencies = [
|
||||
"cap-std",
|
||||
"cap-time-ext",
|
||||
"fs-set-times",
|
||||
"io-extras",
|
||||
"io-lifetimes",
|
||||
"lazy_static",
|
||||
"rustix",
|
||||
@@ -3196,6 +3197,7 @@ dependencies = [
|
||||
"anyhow",
|
||||
"cap-std",
|
||||
"cap-tempfile",
|
||||
"io-extras",
|
||||
"io-lifetimes",
|
||||
"lazy_static",
|
||||
"rustix",
|
||||
|
||||
Submodule crates/wasi-common/WASI updated: 5ab83a68d4...0ba0c5e2e3
@@ -31,6 +31,7 @@ rustix = "0.31.0"
|
||||
winapi = "0.3"
|
||||
lazy_static = "1.4"
|
||||
atty = "0.2.14"
|
||||
io-extras = "0.12.0"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.1.0"
|
||||
|
||||
@@ -25,6 +25,9 @@ impl WasiFile for File {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn datasync(&self) -> Result<(), Error> {
|
||||
self.0.sync_data()?;
|
||||
Ok(())
|
||||
@@ -171,8 +174,19 @@ impl AsHandle for File {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandleOrSocket for File {
|
||||
#[inline]
|
||||
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
|
||||
self.0.as_raw_handle_or_socket()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
use io_lifetimes::{AsFd, BorrowedFd};
|
||||
|
||||
#[cfg(unix)]
|
||||
impl AsFd for File {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
|
||||
@@ -36,17 +36,20 @@
|
||||
pub mod clocks;
|
||||
pub mod dir;
|
||||
pub mod file;
|
||||
pub mod net;
|
||||
pub mod sched;
|
||||
pub mod stdio;
|
||||
|
||||
pub use cap_std::ambient_authority;
|
||||
pub use cap_std::fs::Dir;
|
||||
pub use cap_std::net::TcpListener;
|
||||
pub use clocks::clocks_ctx;
|
||||
pub use sched::sched_ctx;
|
||||
|
||||
use crate::net::Socket;
|
||||
use cap_rand::RngCore;
|
||||
use std::path::Path;
|
||||
use wasi_common::{table::Table, Error, WasiCtx, WasiFile};
|
||||
use wasi_common::{file::FileCaps, table::Table, Error, WasiCtx, WasiFile};
|
||||
|
||||
pub struct WasiCtxBuilder(WasiCtx);
|
||||
|
||||
@@ -120,6 +123,18 @@ impl WasiCtxBuilder {
|
||||
self.0.push_preopened_dir(dir, guest_path)?;
|
||||
Ok(self)
|
||||
}
|
||||
pub fn preopened_socket(mut self, fd: u32, socket: impl Into<Socket>) -> Result<Self, Error> {
|
||||
let socket: Socket = socket.into();
|
||||
let file: Box<dyn WasiFile> = socket.into();
|
||||
|
||||
let caps = FileCaps::FDSTAT_SET_FLAGS
|
||||
| FileCaps::FILESTAT_GET
|
||||
| FileCaps::READ
|
||||
| FileCaps::POLL_READWRITE;
|
||||
|
||||
self.0.insert_file(fd, file, caps);
|
||||
Ok(self)
|
||||
}
|
||||
pub fn build(self) -> WasiCtx {
|
||||
self.0
|
||||
}
|
||||
|
||||
420
crates/wasi-common/cap-std-sync/src/net.rs
Normal file
420
crates/wasi-common/cap-std-sync/src/net.rs
Normal file
@@ -0,0 +1,420 @@
|
||||
#[cfg(windows)]
|
||||
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
|
||||
#[cfg(unix)]
|
||||
use io_lifetimes::AsFilelike;
|
||||
use io_lifetimes::AsSocketlike;
|
||||
#[cfg(unix)]
|
||||
use io_lifetimes::{AsFd, BorrowedFd};
|
||||
#[cfg(windows)]
|
||||
use io_lifetimes::{AsSocket, BorrowedSocket};
|
||||
use std::any::Any;
|
||||
use std::convert::TryInto;
|
||||
use std::io;
|
||||
#[cfg(unix)]
|
||||
use system_interface::fs::FileIoExt;
|
||||
#[cfg(unix)]
|
||||
use system_interface::fs::GetSetFdFlags;
|
||||
use system_interface::io::IsReadWrite;
|
||||
use system_interface::io::ReadReady;
|
||||
use wasi_common::{
|
||||
file::{Advice, FdFlags, FileType, Filestat, WasiFile},
|
||||
Error, ErrorExt,
|
||||
};
|
||||
|
||||
pub enum Socket {
|
||||
TcpListener(cap_std::net::TcpListener),
|
||||
TcpStream(cap_std::net::TcpStream),
|
||||
#[cfg(unix)]
|
||||
UnixStream(cap_std::os::unix::net::UnixStream),
|
||||
#[cfg(unix)]
|
||||
UnixListener(cap_std::os::unix::net::UnixListener),
|
||||
}
|
||||
|
||||
impl From<cap_std::net::TcpListener> for Socket {
|
||||
fn from(listener: cap_std::net::TcpListener) -> Self {
|
||||
Self::TcpListener(listener)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<cap_std::net::TcpStream> for Socket {
|
||||
fn from(stream: cap_std::net::TcpStream) -> Self {
|
||||
Self::TcpStream(stream)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
impl From<cap_std::os::unix::net::UnixListener> for Socket {
|
||||
fn from(listener: cap_std::os::unix::net::UnixListener) -> Self {
|
||||
Self::UnixListener(listener)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
impl From<cap_std::os::unix::net::UnixStream> for Socket {
|
||||
fn from(stream: cap_std::os::unix::net::UnixStream) -> Self {
|
||||
Self::UnixStream(stream)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
impl From<Socket> for Box<dyn WasiFile> {
|
||||
fn from(listener: Socket) -> Self {
|
||||
match listener {
|
||||
Socket::TcpListener(l) => Box::new(crate::net::TcpListener::from_cap_std(l)),
|
||||
Socket::UnixListener(l) => Box::new(crate::net::UnixListener::from_cap_std(l)),
|
||||
Socket::TcpStream(l) => Box::new(crate::net::TcpStream::from_cap_std(l)),
|
||||
Socket::UnixStream(l) => Box::new(crate::net::UnixStream::from_cap_std(l)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl From<Socket> for Box<dyn WasiFile> {
|
||||
fn from(listener: Socket) -> Self {
|
||||
match listener {
|
||||
Socket::TcpListener(l) => Box::new(crate::net::TcpListener::from_cap_std(l)),
|
||||
Socket::TcpStream(l) => Box::new(crate::net::TcpStream::from_cap_std(l)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! wasi_listen_write_impl {
|
||||
($ty:ty, $stream:ty) => {
|
||||
#[async_trait::async_trait]
|
||||
impl WasiFile for $ty {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
async fn sock_accept(&mut self, fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
let (stream, _) = self.0.accept()?;
|
||||
let mut stream = <$stream>::from_cap_std(stream);
|
||||
stream.set_fdflags(fdflags).await?;
|
||||
Ok(Box::new(stream))
|
||||
}
|
||||
async fn datasync(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn sync(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn get_filetype(&self) -> Result<FileType, Error> {
|
||||
Ok(FileType::SocketStream)
|
||||
}
|
||||
#[cfg(unix)]
|
||||
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
|
||||
let fdflags = self.0.as_filelike().get_fd_flags()?;
|
||||
Ok(from_sysif_fdflags(fdflags))
|
||||
}
|
||||
#[cfg(windows)]
|
||||
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
|
||||
// There does not seem to be a way for windows to call s.th. like `fcntl()`
|
||||
// `rustix::fs::fcntl` is only available for Unix
|
||||
// `rustix::io::ioctl_fionbio` only sets the flags, but does not read
|
||||
Ok(FdFlags::empty())
|
||||
}
|
||||
async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
|
||||
if fdflags == wasi_common::file::FdFlags::NONBLOCK {
|
||||
self.0.set_nonblocking(true)?;
|
||||
} else if fdflags.is_empty() {
|
||||
self.0.set_nonblocking(false)?;
|
||||
} else {
|
||||
return Err(
|
||||
Error::invalid_argument().context("cannot set anything else than NONBLOCK")
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn set_times(
|
||||
&self,
|
||||
_atime: Option<wasi_common::SystemTimeSpec>,
|
||||
_mtime: Option<wasi_common::SystemTimeSpec>,
|
||||
) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn read_vectored<'a>(
|
||||
&self,
|
||||
_bufs: &mut [io::IoSliceMut<'a>],
|
||||
) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn read_vectored_at<'a>(
|
||||
&self,
|
||||
_bufs: &mut [io::IoSliceMut<'a>],
|
||||
_offset: u64,
|
||||
) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn write_vectored<'a>(&self, _bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn write_vectored_at<'a>(
|
||||
&self,
|
||||
_bufs: &[io::IoSlice<'a>],
|
||||
_offset: u64,
|
||||
) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn peek(&self, _buf: &mut [u8]) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn num_ready_bytes(&self) -> Result<u64, Error> {
|
||||
Ok(1)
|
||||
}
|
||||
fn isatty(&self) -> bool {
|
||||
false
|
||||
}
|
||||
async fn readable(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn writable(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl AsSocket for $ty {
|
||||
#[inline]
|
||||
fn as_socket(&self) -> BorrowedSocket<'_> {
|
||||
self.0.as_socket()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandleOrSocket for $ty {
|
||||
#[inline]
|
||||
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
|
||||
self.0.as_raw_handle_or_socket()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
impl AsFd for $ty {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub struct TcpListener(cap_std::net::TcpListener);
|
||||
|
||||
impl TcpListener {
|
||||
pub fn from_cap_std(cap_std: cap_std::net::TcpListener) -> Self {
|
||||
TcpListener(cap_std)
|
||||
}
|
||||
}
|
||||
wasi_listen_write_impl!(TcpListener, TcpStream);
|
||||
|
||||
#[cfg(unix)]
|
||||
pub struct UnixListener(cap_std::os::unix::net::UnixListener);
|
||||
|
||||
#[cfg(unix)]
|
||||
impl UnixListener {
|
||||
pub fn from_cap_std(cap_std: cap_std::os::unix::net::UnixListener) -> Self {
|
||||
UnixListener(cap_std)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
wasi_listen_write_impl!(UnixListener, UnixStream);
|
||||
|
||||
macro_rules! wasi_stream_write_impl {
|
||||
($ty:ty, $std_ty:ty) => {
|
||||
#[async_trait::async_trait]
|
||||
impl WasiFile for $ty {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn datasync(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn sync(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn get_filetype(&self) -> Result<FileType, Error> {
|
||||
Ok(FileType::SocketStream)
|
||||
}
|
||||
#[cfg(unix)]
|
||||
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
|
||||
let fdflags = self.0.as_filelike().get_fd_flags()?;
|
||||
Ok(from_sysif_fdflags(fdflags))
|
||||
}
|
||||
#[cfg(windows)]
|
||||
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
|
||||
// There does not seem to be a way for windows to call s.th. like `fcntl(fd, F_GETFL, 0)`
|
||||
// on a socket.
|
||||
// `rustix::fs::fcntl` is only available for Unix.
|
||||
// `rustix::io::ioctl_fionbio` only sets the flags, but does not read.
|
||||
Ok(FdFlags::empty())
|
||||
}
|
||||
async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
|
||||
if fdflags == wasi_common::file::FdFlags::NONBLOCK {
|
||||
self.0.set_nonblocking(true)?;
|
||||
} else if fdflags.is_empty() {
|
||||
self.0.set_nonblocking(false)?;
|
||||
} else {
|
||||
return Err(
|
||||
Error::invalid_argument().context("cannot set anything else than NONBLOCK")
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn set_times(
|
||||
&self,
|
||||
_atime: Option<wasi_common::SystemTimeSpec>,
|
||||
_mtime: Option<wasi_common::SystemTimeSpec>,
|
||||
) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn read_vectored<'a>(
|
||||
&self,
|
||||
bufs: &mut [io::IoSliceMut<'a>],
|
||||
) -> Result<u64, Error> {
|
||||
use std::io::Read;
|
||||
let n = Read::read_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?;
|
||||
Ok(n.try_into()?)
|
||||
}
|
||||
async fn read_vectored_at<'a>(
|
||||
&self,
|
||||
_bufs: &mut [io::IoSliceMut<'a>],
|
||||
_offset: u64,
|
||||
) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
|
||||
use std::io::Write;
|
||||
let n = Write::write_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?;
|
||||
Ok(n.try_into()?)
|
||||
}
|
||||
async fn write_vectored_at<'a>(
|
||||
&self,
|
||||
_bufs: &[io::IoSlice<'a>],
|
||||
_offset: u64,
|
||||
) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
|
||||
let n = self.0.peek(buf)?;
|
||||
Ok(n.try_into()?)
|
||||
}
|
||||
async fn num_ready_bytes(&self) -> Result<u64, Error> {
|
||||
let val = self.as_socketlike_view::<$std_ty>().num_ready_bytes()?;
|
||||
Ok(val)
|
||||
}
|
||||
fn isatty(&self) -> bool {
|
||||
false
|
||||
}
|
||||
async fn readable(&self) -> Result<(), Error> {
|
||||
let (readable, _writeable) = self.0.is_read_write()?;
|
||||
if readable {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::io())
|
||||
}
|
||||
}
|
||||
async fn writable(&self) -> Result<(), Error> {
|
||||
let (_readable, writeable) = self.0.is_read_write()?;
|
||||
if writeable {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::io())
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(unix)]
|
||||
impl AsFd for $ty {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl AsSocket for $ty {
|
||||
/// Borrows the socket.
|
||||
fn as_socket(&self) -> BorrowedSocket<'_> {
|
||||
self.0.as_socket()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandleOrSocket for TcpStream {
|
||||
#[inline]
|
||||
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
|
||||
self.0.as_raw_handle_or_socket()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub struct TcpStream(cap_std::net::TcpStream);
|
||||
|
||||
impl TcpStream {
|
||||
pub fn from_cap_std(socket: cap_std::net::TcpStream) -> Self {
|
||||
TcpStream(socket)
|
||||
}
|
||||
}
|
||||
|
||||
wasi_stream_write_impl!(TcpStream, std::net::TcpStream);
|
||||
|
||||
#[cfg(unix)]
|
||||
pub struct UnixStream(cap_std::os::unix::net::UnixStream);
|
||||
|
||||
#[cfg(unix)]
|
||||
impl UnixStream {
|
||||
pub fn from_cap_std(socket: cap_std::os::unix::net::UnixStream) -> Self {
|
||||
UnixStream(socket)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
wasi_stream_write_impl!(UnixStream, std::os::unix::net::UnixStream);
|
||||
|
||||
pub fn filetype_from(ft: &cap_std::fs::FileType) -> FileType {
|
||||
use cap_fs_ext::FileTypeExt;
|
||||
if ft.is_block_device() {
|
||||
FileType::SocketDgram
|
||||
} else {
|
||||
FileType::SocketStream
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_sysif_fdflags(f: system_interface::fs::FdFlags) -> wasi_common::file::FdFlags {
|
||||
let mut out = wasi_common::file::FdFlags::empty();
|
||||
if f.contains(system_interface::fs::FdFlags::NONBLOCK) {
|
||||
out |= wasi_common::file::FdFlags::NONBLOCK;
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -90,6 +90,18 @@ fn wasi_file_fd(f: &dyn WasiFile) -> Option<BorrowedFd<'_>> {
|
||||
let a = f.as_any();
|
||||
if a.is::<crate::file::File>() {
|
||||
Some(a.downcast_ref::<crate::file::File>().unwrap().as_fd())
|
||||
} else if a.is::<crate::net::TcpStream>() {
|
||||
Some(a.downcast_ref::<crate::net::TcpStream>().unwrap().as_fd())
|
||||
} else if a.is::<crate::net::TcpListener>() {
|
||||
Some(a.downcast_ref::<crate::net::TcpListener>().unwrap().as_fd())
|
||||
} else if a.is::<crate::net::UnixStream>() {
|
||||
Some(a.downcast_ref::<crate::net::UnixStream>().unwrap().as_fd())
|
||||
} else if a.is::<crate::net::UnixListener>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::net::UnixListener>()
|
||||
.unwrap()
|
||||
.as_fd(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stdin>() {
|
||||
Some(a.downcast_ref::<crate::stdio::Stdin>().unwrap().as_fd())
|
||||
} else if a.is::<crate::stdio::Stdout>() {
|
||||
|
||||
@@ -9,9 +9,8 @@
|
||||
// taken the time to improve it. See bug #2880.
|
||||
|
||||
use anyhow::Context;
|
||||
use io_lifetimes::AsHandle;
|
||||
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
|
||||
use std::ops::Deref;
|
||||
use std::os::windows::io::{AsRawHandle, RawHandle};
|
||||
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
|
||||
use std::sync::Mutex;
|
||||
use std::thread;
|
||||
@@ -33,7 +32,7 @@ pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
|
||||
pub async fn poll_oneoff_<'a>(
|
||||
poll: &mut Poll<'a>,
|
||||
file_is_stdin: impl Fn(&dyn WasiFile) -> bool,
|
||||
file_to_handle: impl Fn(&dyn WasiFile) -> Option<RawHandle>,
|
||||
file_to_handle: impl Fn(&dyn WasiFile) -> Option<RawHandleOrSocket>,
|
||||
) -> Result<(), Error> {
|
||||
if poll.is_empty() {
|
||||
return Ok(());
|
||||
@@ -140,35 +139,43 @@ pub fn wasi_file_is_stdin(f: &dyn WasiFile) -> bool {
|
||||
f.as_any().is::<crate::stdio::Stdin>()
|
||||
}
|
||||
|
||||
pub fn wasi_file_raw_handle(f: &dyn WasiFile) -> Option<RawHandle> {
|
||||
pub fn wasi_file_raw_handle(f: &dyn WasiFile) -> Option<RawHandleOrSocket> {
|
||||
let a = f.as_any();
|
||||
if a.is::<crate::file::File>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::file::File>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::net::TcpStream>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::net::TcpStream>()
|
||||
.unwrap()
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::net::TcpListener>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::net::TcpListener>()
|
||||
.unwrap()
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stdin>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stdin>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stdout>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stdout>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stderr>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stderr>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -8,6 +8,8 @@ use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use system_interface::io::ReadReady;
|
||||
|
||||
#[cfg(windows)]
|
||||
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
|
||||
#[cfg(unix)]
|
||||
use io_lifetimes::{AsFd, BorrowedFd};
|
||||
#[cfg(windows)]
|
||||
@@ -124,6 +126,10 @@ impl WasiFile for Stdin {
|
||||
async fn writable(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
|
||||
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
impl AsHandle for Stdin {
|
||||
@@ -131,6 +137,13 @@ impl AsHandle for Stdin {
|
||||
self.0.as_handle()
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandleOrSocket for Stdin {
|
||||
#[inline]
|
||||
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
|
||||
self.0.as_raw_handle_or_socket()
|
||||
}
|
||||
}
|
||||
#[cfg(unix)]
|
||||
impl AsFd for Stdin {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
@@ -244,6 +257,9 @@ macro_rules! wasi_file_write_impl {
|
||||
async fn writable(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
impl AsHandle for $ty {
|
||||
@@ -257,6 +273,13 @@ macro_rules! wasi_file_write_impl {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
impl AsRawHandleOrSocket for $ty {
|
||||
#[inline]
|
||||
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
|
||||
self.0.as_raw_handle_or_socket()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,9 @@ pub use anyhow::{Context, Error};
|
||||
/// the crate. Not all values are represented presently.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ErrorKind {
|
||||
/// Errno::WouldBlk: Would block
|
||||
#[error("WouldBlk: Would block")]
|
||||
WouldBlk,
|
||||
/// Errno::Noent: No such file or directory
|
||||
#[error("Noent: No such file or directory")]
|
||||
Noent,
|
||||
|
||||
@@ -5,6 +5,7 @@ use std::any::Any;
|
||||
#[wiggle::async_trait]
|
||||
pub trait WasiFile: Send + Sync {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
async fn sock_accept(&mut self, fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error>;
|
||||
async fn datasync(&self) -> Result<(), Error>; // write op
|
||||
async fn sync(&self) -> Result<(), Error>; // file op
|
||||
async fn get_filetype(&self) -> Result<FileType, Error>; // file op
|
||||
|
||||
@@ -189,6 +189,10 @@ impl<R: Read + Any + Send + Sync> WasiFile for ReadPipe<R> {
|
||||
async fn writable(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
|
||||
async fn sock_accept(&mut self, fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
}
|
||||
|
||||
/// A virtual pipe write end.
|
||||
@@ -348,4 +352,8 @@ impl<W: Write + Any + Send + Sync> WasiFile for WritePipe<W> {
|
||||
async fn writable(&self) -> Result<(), Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
|
||||
async fn sock_accept(&mut self, fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
Err(Error::badf())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ impl From<ErrorKind> for types::Errno {
|
||||
fn from(e: ErrorKind) -> types::Errno {
|
||||
use types::Errno;
|
||||
match e {
|
||||
ErrorKind::WouldBlk => Errno::Again,
|
||||
ErrorKind::Noent => Errno::Noent,
|
||||
ErrorKind::TooBig => Errno::TooBig,
|
||||
ErrorKind::Badf => Errno::Badf,
|
||||
@@ -114,6 +115,7 @@ impl TryFrom<std::io::Error> for types::Errno {
|
||||
fn raw_error_code(err: &std::io::Error) -> Option<types::Errno> {
|
||||
use rustix::io::Error;
|
||||
match Error::from_io_error(err) {
|
||||
Some(Error::AGAIN) => Some(types::Errno::Again),
|
||||
Some(Error::PIPE) => Some(types::Errno::Pipe),
|
||||
Some(Error::PERM) => Some(types::Errno::Perm),
|
||||
Some(Error::NOENT) => Some(types::Errno::Noent),
|
||||
@@ -147,6 +149,7 @@ impl TryFrom<std::io::Error> for types::Errno {
|
||||
fn raw_error_code(err: &std::io::Error) -> Option<types::Errno> {
|
||||
use winapi::shared::winerror;
|
||||
match err.raw_os_error().map(|code| code as u32) {
|
||||
Some(winerror::WSAEWOULDBLOCK) => Some(types::Errno::Again),
|
||||
Some(winerror::ERROR_BAD_ENVIRONMENT) => Some(types::Errno::TooBig),
|
||||
Some(winerror::ERROR_FILE_NOT_FOUND) => Some(types::Errno::Noent),
|
||||
Some(winerror::ERROR_PATH_NOT_FOUND) => Some(types::Errno::Noent),
|
||||
@@ -1148,6 +1151,27 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn sock_accept(
|
||||
&mut self,
|
||||
fd: types::Fd,
|
||||
flags: types::Fdflags,
|
||||
) -> Result<types::Fd, Error> {
|
||||
let table = self.table();
|
||||
let f = table
|
||||
.get_file_mut(u32::from(fd))?
|
||||
.get_cap_mut(FileCaps::READ)?;
|
||||
|
||||
let file = f.sock_accept(FdFlags::from(flags)).await?;
|
||||
let file_caps = FileCaps::READ
|
||||
| FileCaps::WRITE
|
||||
| FileCaps::FDSTAT_SET_FLAGS
|
||||
| FileCaps::POLL_READWRITE
|
||||
| FileCaps::FILESTAT_GET;
|
||||
|
||||
let fd = table.push(Box::new(FileEntry::new(file_caps, file)))?;
|
||||
Ok(types::Fd::from(fd))
|
||||
}
|
||||
|
||||
async fn sock_recv<'a>(
|
||||
&mut self,
|
||||
_fd: types::Fd,
|
||||
|
||||
@@ -25,6 +25,7 @@ rustix = "0.31.0"
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = "0.3"
|
||||
lazy_static = "1.4"
|
||||
io-extras = "0.12.0"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.1.0"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::block_on_dummy_executor;
|
||||
#[cfg(windows)]
|
||||
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
|
||||
#[cfg(not(windows))]
|
||||
use io_lifetimes::AsFd;
|
||||
#[cfg(windows)]
|
||||
use io_lifetimes::{AsHandle, BorrowedHandle};
|
||||
use std::any::Any;
|
||||
use std::io;
|
||||
use wasi_common::{
|
||||
@@ -21,6 +21,54 @@ impl File {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TcpListener(wasi_cap_std_sync::net::TcpListener);
|
||||
|
||||
impl TcpListener {
|
||||
pub(crate) fn from_inner(listener: wasi_cap_std_sync::net::TcpListener) -> Self {
|
||||
TcpListener(listener)
|
||||
}
|
||||
pub fn from_cap_std(listener: cap_std::net::TcpListener) -> Self {
|
||||
Self::from_inner(wasi_cap_std_sync::net::TcpListener::from_cap_std(listener))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TcpStream(wasi_cap_std_sync::net::TcpStream);
|
||||
|
||||
impl TcpStream {
|
||||
pub(crate) fn from_inner(stream: wasi_cap_std_sync::net::TcpStream) -> Self {
|
||||
TcpStream(stream)
|
||||
}
|
||||
pub fn from_cap_std(stream: cap_std::net::TcpStream) -> Self {
|
||||
Self::from_inner(wasi_cap_std_sync::net::TcpStream::from_cap_std(stream))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub struct UnixListener(wasi_cap_std_sync::net::UnixListener);
|
||||
|
||||
#[cfg(unix)]
|
||||
impl UnixListener {
|
||||
pub(crate) fn from_inner(listener: wasi_cap_std_sync::net::UnixListener) -> Self {
|
||||
UnixListener(listener)
|
||||
}
|
||||
pub fn from_cap_std(listener: cap_std::os::unix::net::UnixListener) -> Self {
|
||||
Self::from_inner(wasi_cap_std_sync::net::UnixListener::from_cap_std(listener))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub struct UnixStream(wasi_cap_std_sync::net::UnixStream);
|
||||
|
||||
#[cfg(unix)]
|
||||
impl UnixStream {
|
||||
fn from_inner(stream: wasi_cap_std_sync::net::UnixStream) -> Self {
|
||||
UnixStream(stream)
|
||||
}
|
||||
pub fn from_cap_std(stream: cap_std::os::unix::net::UnixStream) -> Self {
|
||||
Self::from_inner(wasi_cap_std_sync::net::UnixStream::from_cap_std(stream))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Stdin(wasi_cap_std_sync::stdio::Stdin);
|
||||
|
||||
pub fn stdin() -> Stdin {
|
||||
@@ -175,17 +223,28 @@ macro_rules! wasi_file_impl {
|
||||
use wasi_common::ErrorExt;
|
||||
Err(Error::badf())
|
||||
}
|
||||
|
||||
async fn sock_accept(&mut self, fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
|
||||
block_on_dummy_executor(|| self.0.sock_accept(fdflags))
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
impl AsHandle for $ty {
|
||||
fn as_handle(&self) -> BorrowedHandle<'_> {
|
||||
self.0.as_handle()
|
||||
impl AsRawHandleOrSocket for $ty {
|
||||
#[inline]
|
||||
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
|
||||
self.0.as_raw_handle_or_socket()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
wasi_file_impl!(File);
|
||||
wasi_file_impl!(TcpListener);
|
||||
wasi_file_impl!(TcpStream);
|
||||
#[cfg(unix)]
|
||||
wasi_file_impl!(UnixListener);
|
||||
#[cfg(unix)]
|
||||
wasi_file_impl!(UnixStream);
|
||||
wasi_file_impl!(Stdin);
|
||||
wasi_file_impl!(Stdout);
|
||||
wasi_file_impl!(Stderr);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
mod dir;
|
||||
mod file;
|
||||
pub mod net;
|
||||
pub mod sched;
|
||||
pub mod stdio;
|
||||
|
||||
@@ -12,6 +13,9 @@ use wasi_common::{Error, Table, WasiCtx, WasiFile};
|
||||
|
||||
pub use dir::Dir;
|
||||
pub use file::File;
|
||||
pub use net::*;
|
||||
use wasi_cap_std_sync::net::Socket;
|
||||
use wasi_common::file::FileCaps;
|
||||
|
||||
use crate::sched::sched_ctx;
|
||||
|
||||
@@ -91,6 +95,19 @@ impl WasiCtxBuilder {
|
||||
self.0.push_preopened_dir(dir, guest_path)?;
|
||||
Ok(self)
|
||||
}
|
||||
pub fn preopened_socket(mut self, fd: u32, socket: impl Into<Socket>) -> Result<Self, Error> {
|
||||
let socket: Socket = socket.into();
|
||||
let file: Box<dyn WasiFile> = socket.into();
|
||||
|
||||
let caps = FileCaps::FDSTAT_SET_FLAGS
|
||||
| FileCaps::FILESTAT_GET
|
||||
| FileCaps::READ
|
||||
| FileCaps::POLL_READWRITE;
|
||||
|
||||
self.0.insert_file(fd, file, caps);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn build(self) -> WasiCtx {
|
||||
self.0
|
||||
}
|
||||
|
||||
6
crates/wasi-common/tokio/src/net.rs
Normal file
6
crates/wasi-common/tokio/src/net.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub use crate::file::TcpListener;
|
||||
pub use crate::file::TcpStream;
|
||||
#[cfg(unix)]
|
||||
pub use crate::file::UnixListener;
|
||||
#[cfg(unix)]
|
||||
pub use crate::file::UnixStream;
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::block_on_dummy_executor;
|
||||
use io_lifetimes::AsHandle;
|
||||
use std::os::windows::io::{AsRawHandle, RawHandle};
|
||||
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
|
||||
use wasi_cap_std_sync::sched::windows::poll_oneoff_;
|
||||
use wasi_common::{file::WasiFile, sched::Poll, Error};
|
||||
|
||||
@@ -16,35 +15,43 @@ pub fn wasi_file_is_stdin(f: &dyn WasiFile) -> bool {
|
||||
f.as_any().is::<crate::stdio::Stdin>()
|
||||
}
|
||||
|
||||
fn wasi_file_raw_handle(f: &dyn WasiFile) -> Option<RawHandle> {
|
||||
fn wasi_file_raw_handle(f: &dyn WasiFile) -> Option<RawHandleOrSocket> {
|
||||
let a = f.as_any();
|
||||
if a.is::<crate::file::File>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::file::File>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::net::TcpListener>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::net::TcpListener>()
|
||||
.unwrap()
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::net::TcpStream>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::net::TcpStream>()
|
||||
.unwrap()
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stdin>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stdin>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stdout>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stdout>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else if a.is::<crate::stdio::Stderr>() {
|
||||
Some(
|
||||
a.downcast_ref::<crate::stdio::Stderr>()
|
||||
.unwrap()
|
||||
.as_handle()
|
||||
.as_raw_handle(),
|
||||
.as_raw_handle_or_socket(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
|
||||
Reference in New Issue
Block a user