Skip to content

Commit

Permalink
feat: Add Executor#run_with_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doe committed Feb 20, 2024
1 parent cd8abe2 commit a13cbf7
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions hakoniwa/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,19 @@ pub struct Executor {

/// Where the stdin read from.
stdin: Stdio,

/// Process ID.
#[doc(hidden)]
pub pid: Option<Pid>,
}

impl Executor {
/// This [exit_code][ExecutorResult::exit_code] used when [SandboxSetupError](ExecutorResultStatus::SandboxSetupError).
pub const EXITCODE_FAILURE: i32 = 125;

/// Hook types.
const HOOK_TYPE_AFTER_FORK: &'static str = "after_fork";

/// Constructor.
pub(crate) fn new<SA: AsRef<str>>(prog: &str, argv: &[SA]) -> Self {
let uid = Uid::current().as_raw();
Expand Down Expand Up @@ -508,13 +515,22 @@ impl Executor {

/// Run it in a container, and return an [ExecutorResult].
pub fn run(&mut self) -> ExecutorResult {
match self._run() {
match self._run(HashMap::new()) {
Ok(val) => val,
Err(err) => ExecutorResult::failure(&err.to_string()),
}
}

/// Similar to [Executor::run()], but with hooks.
#[doc(hidden)]
pub fn run_with_hooks(&mut self, hooks: HashMap<&str, &dyn Fn(&Self)>) -> ExecutorResult {
match self._run(hooks) {
Ok(val) => val,
Err(err) => ExecutorResult::failure(&err.to_string()),
}
}

fn _run(&mut self) -> Result<ExecutorResult> {
fn _run(&mut self, hooks: HashMap<&str, &dyn Fn(&Self)>) -> Result<ExecutorResult> {
// Create pipes.
let mut out_pipe = contrib::nix::io::pipe().map_err(|err| {
Error::_ExecutorRunError(format!("create stdout pipe failed: {}", err))
Expand All @@ -540,7 +556,7 @@ impl Executor {
Self::stream_writer((in_pipe.0.as_raw_fd(), in_pipe.1.as_raw_fd()), &self.stdin)?;

// Run & Wait.
let mut result = match self.__run(&out_pipe, &err_pipe, in_pipe) {
let mut result = match self.__run(&out_pipe, &err_pipe, in_pipe, hooks) {
Ok(val) => val,
Err(err) => {
let err = format!("hakoniwa: {}\n", err);
Expand Down Expand Up @@ -574,6 +590,7 @@ impl Executor {
out_pipe: &contrib::nix::io::Pipe,
err_pipe: &contrib::nix::io::Pipe,
in_pipe: contrib::nix::io::Pipe,
hooks: HashMap<&str, &dyn Fn(&Self)>,
) -> Result<ExecutorResult> {
self.lookup_executable()?;
self.log_before_forkexec();
Expand All @@ -594,7 +611,10 @@ impl Executor {

// Fork & Exec.
let result = match unsafe { unistd::fork() } {
Ok(ForkResult::Parent { child, .. }) => self.run_in_parent(child, cpr_pipe, in_pipe),
Ok(ForkResult::Parent { child, .. }) => {
self.pid = Some(child);
self.run_in_parent(child, cpr_pipe, in_pipe, hooks)
}
Ok(ForkResult::Child) => self.run_in_child(&cpr_pipe, out_pipe, err_pipe, &in_pipe),
Err(err) => ExecutorResult::failure(&format!("fork failed: {}", err)),
};
Expand All @@ -608,7 +628,13 @@ impl Executor {
child: Pid,
(mut cpr_reader, mut cpr_writer): contrib::nix::io::Pipe,
(mut in_reader, mut in_writer): contrib::nix::io::Pipe,
hooks: HashMap<&str, &dyn Fn(&Self)>,
) -> ExecutorResult {
// Run after_fork hook.
if let Some(hook) = hooks.get(Self::HOOK_TYPE_AFTER_FORK) {
hook(self);
};

// Close unused pipes.
in_reader.close();
in_writer.close();
Expand Down

0 comments on commit a13cbf7

Please sign in to comment.