Path: blob/main/crates/wasi-common/src/sync/mod.rs
1693 views
//! The `wasi-cap-std-sync` crate provides impl of `WasiFile` and `WasiDir` in1//! terms of `cap_std::fs::{File, Dir}`. These types provide sandboxed access2//! to the local filesystem on both Unix and Windows.3//!4//! All syscalls are hidden behind the `cap-std` hierarchy, with the lone5//! exception of the `sched` implementation, which is provided for both unix6//! and windows in separate modules.7//!8//! Any `wasi_common::{WasiCtx, WasiCtxBuilder}` is interoperable with the9//! implementations provided in `wasi_common::sync`. However, for convenience,10//! this module provides its own `WasiCtxBuilder` that hooks up to all of the11//! crate's components, i.e. it fills in all of the arguments to12//! `WasiCtx::builder(...)`, presents `preopen_dir` in terms of13//! `cap_std::fs::Dir`, and provides convenience methods for inheriting the14//! parent process's stdio, args, and env.1516pub mod clocks;17pub mod dir;18pub mod file;19pub mod net;20pub mod sched;21pub mod stdio;2223pub use cap_std::ambient_authority;24pub use cap_std::fs::Dir;25pub use cap_std::net::TcpListener;26pub use clocks::clocks_ctx;27pub use sched::sched_ctx;2829use self::net::Socket;30use crate::{Error, WasiCtx, WasiFile, file::FileAccessMode, table::Table};31use cap_rand::{Rng, RngCore, SeedableRng};32use std::mem;33use std::path::Path;3435pub struct WasiCtxBuilder {36ctx: WasiCtx,37built: bool,38}3940impl WasiCtxBuilder {41pub fn new() -> Self {42WasiCtxBuilder {43ctx: WasiCtx::new(random_ctx(), clocks_ctx(), sched_ctx(), Table::new()),44built: false,45}46}47pub fn env(&mut self, var: &str, value: &str) -> Result<&mut Self, crate::StringArrayError> {48self.ctx.push_env(var, value)?;49Ok(self)50}51pub fn envs(&mut self, env: &[(String, String)]) -> Result<&mut Self, crate::StringArrayError> {52for (k, v) in env {53self.ctx.push_env(k, v)?;54}55Ok(self)56}57pub fn inherit_env(&mut self) -> Result<&mut Self, crate::StringArrayError> {58for (key, value) in std::env::vars() {59self.ctx.push_env(&key, &value)?;60}61Ok(self)62}63pub fn arg(&mut self, arg: &str) -> Result<&mut Self, crate::StringArrayError> {64self.ctx.push_arg(arg)?;65Ok(self)66}67pub fn args(&mut self, arg: &[String]) -> Result<&mut Self, crate::StringArrayError> {68for a in arg {69self.ctx.push_arg(&a)?;70}71Ok(self)72}73pub fn inherit_args(&mut self) -> Result<&mut Self, crate::StringArrayError> {74for arg in std::env::args() {75self.ctx.push_arg(&arg)?;76}77Ok(self)78}79pub fn stdin(&mut self, f: Box<dyn WasiFile>) -> &mut Self {80self.ctx.set_stdin(f);81self82}83pub fn stdout(&mut self, f: Box<dyn WasiFile>) -> &mut Self {84self.ctx.set_stdout(f);85self86}87pub fn stderr(&mut self, f: Box<dyn WasiFile>) -> &mut Self {88self.ctx.set_stderr(f);89self90}91pub fn inherit_stdin(&mut self) -> &mut Self {92self.stdin(Box::new(crate::sync::stdio::stdin()))93}94pub fn inherit_stdout(&mut self) -> &mut Self {95self.stdout(Box::new(crate::sync::stdio::stdout()))96}97pub fn inherit_stderr(&mut self) -> &mut Self {98self.stderr(Box::new(crate::sync::stdio::stderr()))99}100pub fn inherit_stdio(&mut self) -> &mut Self {101self.inherit_stdin().inherit_stdout().inherit_stderr()102}103pub fn preopened_dir(104&mut self,105dir: Dir,106guest_path: impl AsRef<Path>,107) -> Result<&mut Self, Error> {108let dir = Box::new(crate::sync::dir::Dir::from_cap_std(dir));109self.ctx.push_preopened_dir(dir, guest_path)?;110Ok(self)111}112pub fn preopened_socket(113&mut self,114fd: u32,115socket: impl Into<Socket>,116) -> Result<&mut Self, Error> {117let socket: Socket = socket.into();118let file: Box<dyn WasiFile> = socket.into();119self.ctx120.insert_file(fd, file, FileAccessMode::READ | FileAccessMode::WRITE);121Ok(self)122}123pub fn build(&mut self) -> WasiCtx {124assert!(!self.built);125let WasiCtxBuilder { ctx, .. } = mem::replace(self, Self::new());126self.built = true;127ctx128}129}130131pub fn random_ctx() -> Box<dyn RngCore + Send + Sync> {132let mut rng = cap_rand::thread_rng(cap_rand::ambient_authority());133Box::new(cap_rand::rngs::StdRng::from_seed(rng.r#gen()))134}135136#[cfg(feature = "wasmtime")]137super::define_wasi!(block_on);138139140