gaol/
sandbox.rs

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