Skip to content

Commit 98e7402

Browse files
committed
Cleanup API
1 parent 1acf1db commit 98e7402

File tree

14 files changed

+385
-299
lines changed

14 files changed

+385
-299
lines changed

minion-cli/src/main.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use minion::{self, Dominion};
1+
use minion::{self};
22
use std::time::Duration;
33
use structopt::StructOpt;
44

@@ -16,7 +16,7 @@ fn parse_env_item(src: &str) -> Result<EnvItem, &'static str> {
1616
})
1717
}
1818

19-
fn parse_path_exposition_item(src: &str) -> Result<minion::PathExpositionOptions, String> {
19+
fn parse_path_exposition_item(src: &str) -> Result<minion::SharedDir, String> {
2020
let parts = src.splitn(3, ':').collect::<Vec<_>>();
2121
if parts.len() != 3 {
2222
return Err(format!(
@@ -31,20 +31,20 @@ fn parse_path_exposition_item(src: &str) -> Result<minion::PathExpositionOptions
3131
amask.len()
3232
));
3333
}
34-
let access = match amask {
35-
"rwx" => minion::DesiredAccess::Full,
36-
"r-x" => minion::DesiredAccess::Readonly,
34+
let kind = match amask {
35+
"rwx" => minion::SharedDirKind::Full,
36+
"r-x" => minion::SharedDirKind::Readonly,
3737
_ => {
3838
return Err(format!(
3939
"unknown access mask {}. rwx or r-x expected",
4040
amask
4141
));
4242
}
4343
};
44-
Ok(minion::PathExpositionOptions {
44+
Ok(minion::SharedDir {
4545
src: parts[0].to_string().into(),
4646
dest: parts[2].to_string().into(),
47-
access,
47+
kind,
4848
})
4949
}
5050

@@ -92,7 +92,7 @@ struct ExecOpt {
9292
long = "expose",
9393
parse(try_from_str = parse_path_exposition_item)
9494
)]
95-
exposed_paths: Vec<minion::PathExpositionOptions>,
95+
exposed_paths: Vec<minion::SharedDir>,
9696

9797
/// Process working dir, relative to `isolation_root`
9898
#[structopt(short = "p", long = "pwd", default_value = "/")]
@@ -108,18 +108,18 @@ fn main() {
108108
eprintln!("Error: {}", err);
109109
std::process::exit(1);
110110
}
111-
let backend = minion::setup();
112-
113-
let dominion = backend.new_dominion(minion::DominionOptions {
114-
max_alive_process_count: options.num_processes.min(u32::max_value() as usize) as u32,
115-
memory_limit: options.memory_limit as u64,
116-
isolation_root: options.isolation_root.into(),
117-
exposed_paths: options.exposed_paths,
118-
cpu_time_limit: Duration::from_millis(u64::from(options.time_limit)),
119-
real_time_limit: Duration::from_millis(u64::from(options.time_limit * 3)),
120-
});
121-
122-
let dominion = dominion.unwrap();
111+
let backend = minion::erased::setup();
112+
113+
let sandbox = backend
114+
.new_sandbox(minion::SandboxOptions {
115+
max_alive_process_count: options.num_processes.min(u32::max_value() as usize) as u32,
116+
memory_limit: options.memory_limit as u64,
117+
isolation_root: options.isolation_root.into(),
118+
exposed_paths: options.exposed_paths,
119+
cpu_time_limit: Duration::from_millis(u64::from(options.time_limit)),
120+
real_time_limit: Duration::from_millis(u64::from(options.time_limit * 3)),
121+
})
122+
.unwrap();
123123

124124
let (stdin_fd, stdout_fd, stderr_fd);
125125
unsafe {
@@ -135,7 +135,7 @@ fn main() {
135135
.iter()
136136
.map(|v| format!("{}={}", &v.name, &v.value).into())
137137
.collect(),
138-
dominion: dominion.clone(),
138+
sandbox: sandbox.clone(),
139139
stdio: minion::StdioSpecification {
140140
stdin: unsafe { minion::InputSpecification::handle(stdin_fd) },
141141
stdout: unsafe { minion::OutputSpecification::handle(stdout_fd) },
@@ -150,10 +150,10 @@ fn main() {
150150
cp.wait_for_exit(None).unwrap();
151151
let exit_code = cp.get_exit_code().unwrap();
152152
println!("---> Child process exited with code {:?} <---", exit_code);
153-
if dominion.check_cpu_tle().unwrap() {
153+
if sandbox.check_cpu_tle().unwrap() {
154154
println!("Note: CPU time limit was exceeded");
155155
}
156-
if dominion.check_real_tle().unwrap() {
156+
if sandbox.check_real_tle().unwrap() {
157157
println!("Note: wall-clock time limit was exceeded");
158158
}
159159
}

minion-ffi/src/lib.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg_attr(minion_nightly, feature(unsafe_block_in_unsafe_fn))]
22
#![cfg_attr(minion_nightly, warn(unsafe_op_in_unsafe_fn))]
3-
use minion::{self, Dominion as _};
3+
use minion::{self};
44
use std::{
55
ffi::{CStr, OsStr, OsString},
66
mem::{self},
@@ -50,7 +50,7 @@ unsafe fn get_string(buf: *const c_char) -> OsString {
5050
s.to_os_string()
5151
}
5252

53-
pub struct Backend(Box<dyn minion::Backend>);
53+
pub struct Backend(Box<dyn minion::erased::Backend>);
5454

5555
/// # Safety
5656
/// Must be called once
@@ -69,7 +69,7 @@ pub unsafe extern "C" fn minion_lib_init() -> ErrorCode {
6969
#[no_mangle]
7070
#[must_use]
7171
pub extern "C" fn minion_backend_create(out: &mut *mut Backend) -> ErrorCode {
72-
let backend = Backend(minion::setup());
72+
let backend = Backend(minion::erased::setup());
7373
let backend = Box::new(backend);
7474
*out = Box::into_raw(backend);
7575
ErrorCode::Ok
@@ -93,7 +93,7 @@ pub struct TimeSpec {
9393
}
9494

9595
#[repr(C)]
96-
pub struct DominionOptions {
96+
pub struct SandboxOptions {
9797
pub cpu_time_limit: TimeSpec,
9898
pub real_time_limit: TimeSpec,
9999
pub process_limit: u32,
@@ -103,16 +103,16 @@ pub struct DominionOptions {
103103
}
104104

105105
#[derive(Clone)]
106-
pub struct Dominion(minion::DominionRef);
106+
pub struct Sandbox(Box<dyn minion::erased::Sandbox>);
107107

108108
/// # Safety
109109
/// `out` must be valid
110110
#[no_mangle]
111-
pub unsafe extern "C" fn minion_dominion_check_cpu_tle(
112-
dominion: &Dominion,
111+
pub unsafe extern "C" fn minion_sandbox_check_cpu_tle(
112+
sandbox: &Sandbox,
113113
out: *mut bool,
114114
) -> ErrorCode {
115-
match dominion.0.check_cpu_tle() {
115+
match sandbox.0.check_cpu_tle() {
116116
Ok(st) => {
117117
unsafe {
118118
out.write(st);
@@ -126,11 +126,11 @@ pub unsafe extern "C" fn minion_dominion_check_cpu_tle(
126126
/// # Safety
127127
/// `out` must be valid
128128
#[no_mangle]
129-
pub unsafe extern "C" fn minion_dominion_check_real_tle(
130-
dominion: &Dominion,
129+
pub unsafe extern "C" fn minion_sandbox_check_real_tle(
130+
sandbox: &Sandbox,
131131
out: *mut bool,
132132
) -> ErrorCode {
133-
match dominion.0.check_real_tle() {
133+
match sandbox.0.check_real_tle() {
134134
Ok(st) => {
135135
unsafe {
136136
out.write(st);
@@ -142,8 +142,8 @@ pub unsafe extern "C" fn minion_dominion_check_real_tle(
142142
}
143143

144144
#[no_mangle]
145-
pub extern "C" fn minion_dominion_kill(dominion: &Dominion) -> ErrorCode {
146-
match dominion.0.kill() {
145+
pub extern "C" fn minion_sandbox_kill(sandbox: &Sandbox) -> ErrorCode {
146+
match sandbox.0.kill() {
147147
Ok(_) => ErrorCode::Ok,
148148
Err(_) => ErrorCode::Unknown,
149149
}
@@ -153,29 +153,29 @@ pub extern "C" fn minion_dominion_kill(dominion: &Dominion) -> ErrorCode {
153153
/// Provided arguments must be well-formed
154154
#[no_mangle]
155155
#[must_use]
156-
pub unsafe extern "C" fn minion_dominion_create(
156+
pub unsafe extern "C" fn minion_sandbox_create(
157157
backend: &Backend,
158-
options: DominionOptions,
159-
out: &mut *mut Dominion,
158+
options: SandboxOptions,
159+
out: &mut *mut Sandbox,
160160
) -> ErrorCode {
161161
let mut exposed_paths = Vec::new();
162162
unsafe {
163163
let mut p = options.shared_directories;
164164
while !(*p).host_path.is_null() {
165-
let opt = minion::PathExpositionOptions {
165+
let opt = minion::SharedDir {
166166
src: get_string((*p).host_path).into(),
167167
dest: get_string((*p).sandbox_path).into(),
168-
access: match (*p).kind {
169-
SharedDirectoryAccessKind::Full => minion::DesiredAccess::Full,
170-
SharedDirectoryAccessKind::Readonly => minion::DesiredAccess::Readonly,
168+
kind: match (*p).kind {
169+
SharedDirectoryAccessKind::Full => minion::SharedDirKind::Full,
170+
SharedDirectoryAccessKind::Readonly => minion::SharedDirKind::Readonly,
171171
},
172172
};
173173
exposed_paths.push(opt);
174174
p = p.offset(1);
175175
}
176176
}
177177
let isolation_root = unsafe { get_string(options.isolation_root) }.into();
178-
let opts = minion::DominionOptions {
178+
let opts = minion::SandboxOptions {
179179
max_alive_process_count: options.process_limit as _,
180180
memory_limit: u64::from(options.memory_limit),
181181
cpu_time_limit: std::time::Duration::new(
@@ -189,20 +189,20 @@ pub unsafe extern "C" fn minion_dominion_create(
189189
isolation_root,
190190
exposed_paths,
191191
};
192-
let d = backend.0.new_dominion(opts);
192+
let d = backend.0.new_sandbox(opts);
193193
let d = d.unwrap();
194194

195-
let dw = Dominion(d);
195+
let dw = Sandbox(d);
196196
*out = Box::into_raw(Box::new(dw));
197197
ErrorCode::Ok
198198
}
199199

200200
/// # Safety
201-
/// `dominion` must be pointer, returned by `minion_dominion_create`.
201+
/// `sandbox` must be pointer, returned by `minion_sandbox_create`.
202202
#[no_mangle]
203203
#[must_use]
204-
pub unsafe extern "C" fn minion_dominion_free(dominion: *mut Dominion) -> ErrorCode {
205-
let b = unsafe { Box::from_raw(dominion) };
204+
pub unsafe extern "C" fn minion_sandbox_free(sandbox: *mut Sandbox) -> ErrorCode {
205+
let b = unsafe { Box::from_raw(sandbox) };
206206
mem::drop(b);
207207
ErrorCode::Ok
208208
}
@@ -242,7 +242,7 @@ pub struct ChildProcessOptions {
242242
pub argv: *const *const c_char,
243243
pub envp: *const EnvItem,
244244
pub stdio: StdioHandleSet,
245-
pub dominion: *mut Dominion,
245+
pub sandbox: *mut Sandbox,
246246
pub workdir: *const c_char,
247247
}
248248

@@ -269,7 +269,7 @@ pub static SHARED_DIRECTORY_ACCESS_FIN: SharedDirectoryAccess = SharedDirectoryA
269269
sandbox_path: std::ptr::null(),
270270
};
271271

272-
pub struct ChildProcess(Box<dyn minion::ChildProcess>);
272+
pub struct ChildProcess(Box<dyn minion::erased::ChildProcess>);
273273

274274
/// # Safety
275275
/// Provided `options` must be well-formed
@@ -314,7 +314,7 @@ pub unsafe extern "C" fn minion_cp_spawn(
314314
path: get_string(options.image_path).into(),
315315
arguments,
316316
environment,
317-
dominion: (*options.dominion).0.clone(),
317+
sandbox: (*options.sandbox).0.clone(),
318318
stdio,
319319
pwd: get_string(options.workdir).into(),
320320
}

minion-tests/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! master. Finally, test itself is executed inside another process.
99
//! I.e.
1010
//! Master: parses CLI, selects tests, spawns Workers
11-
//! Worker: sets up dominion, executes `minion-tests` in Test mode.
11+
//! Worker: sets up sandbox, executes `minion-tests` in Test mode.
1212
//! Test: just executes test selected by name
1313
#![feature(asm, test)]
1414
mod master;
@@ -27,7 +27,7 @@ pub trait TestCase: Send + Sync {
2727
/// If tests passed, does nothing otherwise
2828
/// panics.
2929
/// Executed on worker.
30-
fn check(&self, cp: &mut dyn minion::ChildProcess, d: minion::DominionRef);
30+
fn check(&self, cp: &mut dyn minion::erased::ChildProcess, sb: &dyn minion::erased::Sandbox);
3131
/// Overrides CPU time limit
3232
fn time_limit(&self) -> std::time::Duration {
3333
std::time::Duration::from_secs(1)

minion-tests/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod simple;
22

33
use crate::TestCase;
4-
use minion::ChildProcess;
4+
use minion::erased::ChildProcess;
55
use once_cell::sync::Lazy;
66
use std::io::Read;
77

minion-tests/src/tests/simple.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Tests that simple program that does nothing completes successfully.
2-
use minion::ChildProcess;
2+
use minion::erased::{ChildProcess, Sandbox};
33

44
pub(crate) struct TOk;
55
impl crate::TestCase for TOk {
@@ -12,7 +12,7 @@ impl crate::TestCase for TOk {
1212
fn test(&self) -> ! {
1313
std::process::exit(0)
1414
}
15-
fn check(&self, cp: &mut dyn ChildProcess, _: minion::DominionRef) {
15+
fn check(&self, cp: &mut dyn ChildProcess, _: &dyn Sandbox) {
1616
assert!(matches!(cp.get_exit_code(), Ok(Some(0))));
1717
super::assert_empty(&mut cp.stdout().unwrap());
1818
super::assert_empty(&mut cp.stderr().unwrap());
@@ -34,7 +34,7 @@ impl crate::TestCase for TTl {
3434
fn test(&self) -> ! {
3535
exceed_time_limit()
3636
}
37-
fn check(&self, cp: &mut dyn ChildProcess, _: minion::DominionRef) {
37+
fn check(&self, cp: &mut dyn ChildProcess, _: &dyn Sandbox) {
3838
super::assert_killed(cp);
3939
}
4040
}
@@ -53,7 +53,7 @@ impl crate::TestCase for TTlFork {
5353
nix::unistd::fork().unwrap();
5454
exceed_time_limit()
5555
}
56-
fn check(&self, cp: &mut dyn ChildProcess, _: minion::DominionRef) {
56+
fn check(&self, cp: &mut dyn ChildProcess, _: &dyn Sandbox) {
5757
super::assert_killed(cp);
5858
}
5959
fn process_count_limit(&self) -> u32 {
@@ -74,7 +74,7 @@ impl crate::TestCase for TIdle {
7474
nix::unistd::sleep(1_000_000_000);
7575
std::process::exit(0)
7676
}
77-
fn check(&self, cp: &mut dyn ChildProcess, _: minion::DominionRef) {
77+
fn check(&self, cp: &mut dyn ChildProcess, _: &dyn Sandbox) {
7878
super::assert_killed(cp);
7979
}
8080
}
@@ -90,7 +90,7 @@ impl crate::TestCase for TRet1 {
9090
fn test(&self) -> ! {
9191
std::process::exit(1);
9292
}
93-
fn check(&self, cp: &mut dyn ChildProcess, _: minion::DominionRef) {
93+
fn check(&self, cp: &mut dyn ChildProcess, _: &dyn Sandbox) {
9494
super::assert_exit_code(cp, 1);
9595
}
9696
}
@@ -125,7 +125,7 @@ impl crate::TestCase for TOom {
125125
}
126126
}
127127

128-
fn check(&self, _cp: &mut dyn ChildProcess, _d: minion::DominionRef) {
128+
fn check(&self, _cp: &mut dyn ChildProcess, _sb: &dyn Sandbox) {
129129
// TODO this test is broken
130130
// super::assert_exit_code(cp, -9);
131131
// assert!(!d.check_cpu_tle().unwrap());
@@ -164,7 +164,7 @@ impl crate::TestCase for TSecurity {
164164
assert!(matches!(err, nix::Error::Sys(nix::errno::Errno::EPERM)));
165165
std::process::exit(24)
166166
}
167-
fn check(&self, cp: &mut dyn minion::ChildProcess, _d: minion::DominionRef) {
167+
fn check(&self, cp: &mut dyn ChildProcess, _sb: &dyn Sandbox) {
168168
super::assert_exit_code(cp, 24);
169169
super::assert_empty(&mut cp.stdout().unwrap());
170170
super::assert_empty(&mut cp.stderr().unwrap());

0 commit comments

Comments
 (0)