1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Creation and destruction of sandboxes.
use platform::process::{self, Process};
use profile::Profile;
use std::collections::HashMap;
use std::convert::AsRef;
use std::env;
use std::ffi::{CString, OsStr};
use std::io;
pub use platform::{ChildSandbox, Sandbox};
/// All platform-specific sandboxes implement this trait.
///
/// A new sandbox can be created with `Sandbox::new()`, which all platform-specific sandboxes
/// implement.
pub trait SandboxMethods {
/// Returns this sandbox profile.
fn profile(&self) -> &Profile;
/// Spawns a child process eligible for sandboxing.
fn start(&self, command: &mut Command) -> io::Result<Process>;
}
/// All platform-specific sandboxes in the child process implement this trait.
pub trait ChildSandboxMethods {
/// Activates the restrictions in this child process from here on out. Be sure to check the
/// return value!
fn activate(&self) -> Result<(),()>;
}
fn cstring<T>(path: T) -> CString
where T: AsRef<OsStr>
{
let path = path.as_ref();
let bytes = if cfg!(windows) {
path.to_str().unwrap().as_bytes()
} else {
use std::os::unix::ffi::OsStrExt;
path.as_bytes()
};
CString::new(bytes).unwrap()
}
pub struct Command {
/// A path to the executable.
pub module_path: CString,
/// The arguments to pass.
pub args: Vec<CString>,
/// The environment of the process.
pub env: HashMap<CString,CString>,
}
impl Command {
/// Constructs a new `Command` for launching the executable at path `module_path` with no
/// arguments and no environment by default. Builder methods are provided to change these
/// defaults and otherwise configure the process.
pub fn new<T>(module_path: T) -> Command where T: AsRef<OsStr> {
Command {
module_path: cstring(module_path),
args: Vec::new(),
env: HashMap::new(),
}
}
/// Constructs a new `Command` for launching the current executable.
pub fn me() -> io::Result<Command> {
Ok(Command::new(try!(env::current_exe())))
}
/// Adds an argument to pass to the program.
pub fn arg<'a,T>(&'a mut self, arg: T) -> &'a mut Command where T: AsRef<OsStr> {
self.args.push(cstring(arg));
self
}
/// Adds multiple arguments to pass to the program.
pub fn args<'a,T>(&'a mut self, args: &[T]) -> &'a mut Command where T: AsRef<OsStr> {
self.args.extend(args.iter().map(cstring));
self
}
/// Inserts or updates an environment variable mapping.
pub fn env<'a,T,U>(&'a mut self, key: T, val: U) -> &'a mut Command
where T: AsRef<OsStr>, U: AsRef<OsStr> {
self.env.insert(cstring(key), cstring(val));
self
}
/// Executes the command as a child process, which is returned.
pub fn spawn(&self) -> io::Result<Process> {
process::spawn(self)
}
}