pub struct Sandbox { /* private fields */ }Expand description
Build sandbox manager.
Provides methods to create, execute commands in, and destroy sandboxes. The sandbox implementation is platform-specific but the interface is uniform.
§Example
let config = Config::load(None, false)?;
let sandbox = Sandbox::new(&config);
if sandbox.enabled() {
sandbox.create(0)?; // Create sandbox 0
// Execute a script in the sandbox
let child = sandbox.execute(
0,
Path::new("/path/to/script"),
vec![("KEY".to_string(), "value".to_string())],
None,
None,
)?;
let output = child.wait_with_output()?;
sandbox.destroy(0)?;
}Implementations§
Source§impl Sandbox
impl Sandbox
pub fn mount_bindfs( &self, src: &Path, dest: &Path, opts: &[&str], ) -> Result<Option<ExitStatus>>
pub fn mount_devfs( &self, _src: &Path, dest: &Path, opts: &[&str], ) -> Result<Option<ExitStatus>>
pub fn mount_fdfs( &self, _src: &Path, dest: &Path, opts: &[&str], ) -> Result<Option<ExitStatus>>
pub fn mount_nfs( &self, src: &Path, dest: &Path, opts: &[&str], ) -> Result<Option<ExitStatus>>
pub fn mount_procfs( &self, _src: &Path, dest: &Path, opts: &[&str], ) -> Result<Option<ExitStatus>>
pub fn mount_tmpfs( &self, _src: &Path, dest: &Path, opts: &[&str], ) -> Result<Option<ExitStatus>>
pub fn unmount_bindfs(&self, dest: &Path) -> Result<Option<ExitStatus>>
pub fn unmount_devfs(&self, dest: &Path) -> Result<Option<ExitStatus>>
pub fn unmount_fdfs(&self, dest: &Path) -> Result<Option<ExitStatus>>
pub fn unmount_nfs(&self, dest: &Path) -> Result<Option<ExitStatus>>
pub fn unmount_procfs(&self, dest: &Path) -> Result<Option<ExitStatus>>
pub fn unmount_tmpfs(&self, dest: &Path) -> Result<Option<ExitStatus>>
Sourcepub fn kill_processes(&self, sandbox: &Path)
pub fn kill_processes(&self, sandbox: &Path)
Kill all processes using files within a sandbox path.
Source§impl Sandbox
impl Sandbox
Sourcepub fn enabled(&self) -> bool
pub fn enabled(&self) -> bool
Return whether sandboxes have been enabled.
This is based on whether a valid sandboxes section has been
specified in the config file.
Sourcepub fn command(&self, id: usize, cmd: &Path) -> Command
pub fn command(&self, id: usize, cmd: &Path) -> Command
Create a Command that runs in the sandbox (via chroot) if enabled, or directly if sandboxes are disabled.
Sourcepub fn kill_processes_by_id(&self, id: usize)
pub fn kill_processes_by_id(&self, id: usize)
Kill all processes in a sandbox by id. This is used for graceful shutdown on Ctrl+C.
Sourcepub fn create(&self, id: usize) -> Result<()>
pub fn create(&self, id: usize) -> Result<()>
Create a single sandbox by id. If the sandbox already exists and is valid (has lock), this is a no-op.
Sourcepub fn execute(
&self,
id: usize,
script: &Path,
envs: Vec<(String, String)>,
stdin_data: Option<&str>,
status_fd: Option<i32>,
) -> Result<Child>
pub fn execute( &self, id: usize, script: &Path, envs: Vec<(String, String)>, stdin_data: Option<&str>, status_fd: Option<i32>, ) -> Result<Child>
Execute a script file with supplied environment variables and optional stdin data. If status_fd is provided, it will be passed to the child process via the bob_status_fd environment variable.
Sourcepub fn execute_script(
&self,
id: usize,
content: &str,
envs: Vec<(String, String)>,
) -> Result<Child>
pub fn execute_script( &self, id: usize, content: &str, envs: Vec<(String, String)>, ) -> Result<Child>
Execute inline script content via /bin/sh.
Sourcepub fn create_all(&self, count: usize) -> Result<()>
pub fn create_all(&self, count: usize) -> Result<()>
Create all sandboxes.
Sourcepub fn destroy_all(&self, count: usize) -> Result<()>
pub fn destroy_all(&self, count: usize) -> Result<()>
Destroy all sandboxes. Continue on errors to ensure all sandboxes are attempted, printing each error as it occurs.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Sandbox
impl !RefUnwindSafe for Sandbox
impl Send for Sandbox
impl Sync for Sandbox
impl Unpin for Sandbox
impl !UnwindSafe for Sandbox
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more