/
executor.rs
89 lines (78 loc) · 2.56 KB
/
executor.rs
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
use chibiwasm::wasi::file::File;
use containerd_shim_wasm::sandbox::oci;
use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use libcontainer::workload::{Executor, ExecutorError};
use nix::unistd::{dup, dup2};
use oci_spec::runtime::Spec;
use std::{
os::fd::RawFd,
sync::{Arc, Mutex},
};
const EXECUTOR_NAME: &str = "chibiwasm";
pub struct ChibiwasmExecutor {
pub stdin: Option<RawFd>,
pub stdout: Option<RawFd>,
pub stderr: Option<RawFd>,
}
impl Executor for ChibiwasmExecutor {
fn exec(&self, spec: &Spec) -> Result<(), ExecutorError> {
let args = oci::get_args(spec);
if args.is_empty() {
return Err(ExecutorError::InvalidArg);
}
if let Some(stdin) = self.stdin {
dup(STDIN_FILENO).unwrap();
dup2(stdin, STDIN_FILENO).unwrap();
}
if let Some(stdout) = self.stdout {
dup(STDOUT_FILENO).unwrap();
dup2(stdout, STDOUT_FILENO).unwrap();
}
if let Some(stderr) = self.stderr {
dup(STDERR_FILENO).unwrap();
dup2(stderr, STDERR_FILENO).unwrap();
}
let mut iterator = args
.first()
.expect("args must have at least one argument.")
.split('#');
let mut cmd = iterator.next().unwrap().to_string();
let stripped = cmd.strip_prefix(std::path::MAIN_SEPARATOR);
if let Some(strpd) = stripped {
cmd = strpd.to_string();
}
let method = iterator.next().unwrap_or("_start");
let mod_path = cmd;
let io = vec![
Arc::new(Mutex::new(
File::from_raw_fd(self.stdin.unwrap_or(0) as u32),
)),
Arc::new(Mutex::new(File::from_raw_fd(
self.stdout.unwrap_or(1) as u32
))),
Arc::new(Mutex::new(File::from_raw_fd(
self.stderr.unwrap_or(2) as u32
))),
];
let mut runtime = chibiwasm::Runtime::from_file(
&mod_path,
Some(Box::new(
chibiwasm::wasi::preview1::WasiSnapshotPreview1::with_io(io),
)),
)
.expect("failed to create chibiwasm runtime");
match runtime.call(method.into(), vec![]) {
Ok(_) => std::process::exit(0),
Err(e) => {
eprintln!("failed call: {}. error: {}", method, e);
std::process::exit(137)
}
};
}
fn can_handle(&self, _spec: &Spec) -> bool {
true
}
fn name(&self) -> &'static str {
EXECUTOR_NAME
}
}