aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Hsieh <victorhsieh@google.com>2021-04-20 20:27:56 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-04-20 20:27:56 +0000
commita7d368fa1553cd495833e2097c3a942812ec8b6e (patch)
treed517c4f707c2e19d52276880fce3844b9ea4f8e3
parent76a7414f787e40a93c0ecc8aa72cd509715ec21b (diff)
parent07e83986ab75dc5925d011732d1fe9db4836f86b (diff)
downloadminijail-a7d368fa1553cd495833e2097c3a942812ec8b6e.tar.gz
Make run/run_remap more generic am: f16b78b423 am: 4f4c093869 am: a3f4caae38 am: 07e83986ab
Original change: https://android-review.googlesource.com/c/platform/external/minijail/+/1675960 Change-Id: I30150f67f85a547e0d53969709548d0770b1ac70
-rw-r--r--rust/minijail/src/lib.rs32
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();
}
}