diff options
author | Victor Hsieh <victorhsieh@google.com> | 2021-04-15 16:20:23 -0700 |
---|---|---|
committer | Treehugger Robot <treehugger-gerrit@google.com> | 2021-04-20 17:40:03 +0000 |
commit | f16b78b4235cdc5dc22226c041c219d9ebfc18b0 (patch) | |
tree | d517c4f707c2e19d52276880fce3844b9ea4f8e3 | |
parent | c71826854946d4f24e23304b63aab0f2c1639932 (diff) | |
download | minijail-f16b78b4235cdc5dc22226c041c219d9ebfc18b0.tar.gz |
Make run/run_remap more generic
For example, this allows the caller to pass &[String] instead of only
&[&str].
Bug: None
Test: cargo test
Change-Id: I0e47a5a5f04a0983572031bcf609032077bc943f
-rw-r--r-- | rust/minijail/src/lib.rs | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/rust/minijail/src/lib.rs b/rust/minijail/src/lib.rs index d4f5787..8500a1f 100644 --- a/rust/minijail/src/lib.rs +++ b/rust/minijail/src/lib.rs @@ -640,11 +640,11 @@ impl Minijail { /// FDs 0, 1, and 2 are overwritten with /dev/null FDs unless they are included in the /// inheritable_fds list. This function may abort in the child on error because a partially /// entered jail isn't recoverable. - pub fn run<P: AsRef<Path>>( + pub fn run<P: AsRef<Path>, S: AsRef<str>>( &self, cmd: P, inheritable_fds: &[RawFd], - args: &[&str], + args: &[S], ) -> Result<pid_t> { self.run_remap( cmd, @@ -658,11 +658,11 @@ impl Minijail { /// Behaves the same as `run()` except `inheritable_fds` is a list of fd /// mappings rather than just a list of fds to preserve. - pub fn run_remap<P: AsRef<Path>>( + pub fn run_remap<P: AsRef<Path>, S: AsRef<str>>( &self, cmd: P, inheritable_fds: &[(RawFd, RawFd)], - args: &[&str], + args: &[S], ) -> Result<pid_t> { let cmd_os = cmd .as_ref() @@ -674,8 +674,9 @@ impl Minijail { // into a null terminated array, suitable for use as an argv parameter to `execve`. let mut args_cstr = Vec::with_capacity(args.len()); let mut args_array = Vec::with_capacity(args.len()); - for &arg in args { - let arg_cstr = CString::new(arg).map_err(|_| Error::StrToCString(arg.to_owned()))?; + for arg in args { + let arg_cstr = CString::new(arg.as_ref()) + .map_err(|_| Error::StrToCString(arg.as_ref().to_owned()))?; args_array.push(arg_cstr.as_ptr()); args_cstr.push(arg_cstr); } @@ -850,6 +851,7 @@ mod tests { use super::*; const SHELL: &str = "/bin/sh"; + const EMPTY_STRING_SLICE: &[&str] = &[]; #[test] fn create_and_free() { @@ -911,7 +913,7 @@ mod tests { #[test] fn wait_success() { let j = Minijail::new().unwrap(); - j.run("/bin/true", &[1, 2], &[]).unwrap(); + j.run("/bin/true", &[1, 2], &EMPTY_STRING_SLICE).unwrap(); expect_result!(j.wait(), Ok(())); } @@ -930,21 +932,22 @@ mod tests { #[test] fn wait_returncode() { let j = Minijail::new().unwrap(); - j.run("/bin/false", &[1, 2], &[]).unwrap(); + j.run("/bin/false", &[1, 2], &EMPTY_STRING_SLICE).unwrap(); expect_result!(j.wait(), Err(Error::ReturnCode(1))); } #[test] fn wait_noaccess() { let j = Minijail::new().unwrap(); - j.run("/dev/null", &[1, 2], &[]).unwrap(); + j.run("/dev/null", &[1, 2], &EMPTY_STRING_SLICE).unwrap(); expect_result!(j.wait(), Err(Error::NoAccess)); } #[test] fn wait_nocommand() { let j = Minijail::new().unwrap(); - j.run("/bin/does not exist", &[1, 2], &[]).unwrap(); + j.run("/bin/does not exist", &[1, 2], &EMPTY_STRING_SLICE) + .unwrap(); expect_result!(j.wait(), Err(Error::NoCommand)); } @@ -984,6 +987,13 @@ mod tests { #[test] fn run() { let j = Minijail::new().unwrap(); - j.run("/bin/true", &[], &[]).unwrap(); + j.run("/bin/true", &[], &EMPTY_STRING_SLICE).unwrap(); + } + + #[test] + fn run_string_vec() { + let j = Minijail::new().unwrap(); + let args = vec!["ignored".to_string()]; + j.run(Path::new("/bin/true"), &[], &args).unwrap(); } } |