use super::codes;
use super::protocol::{Command, Frame, NotExecuted};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct LoopState {
pub(crate) armed: bool,
pub(crate) conn_open: bool,
pub(crate) owner_watched: bool,
pub(crate) exec_pending: bool,
pub(crate) test_hooks: bool,
}
impl LoopState {
pub(crate) fn new(test_hooks: bool) -> LoopState {
LoopState {
armed: true,
conn_open: true,
owner_watched: true,
exec_pending: true,
test_hooks,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Control {
Nothing,
Byte(u8),
Eof,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ExecEvent {
Nothing,
Eof,
Report,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Events {
pub(crate) control: Control,
pub(crate) owner_exited: bool,
pub(crate) child_exited: bool,
pub(crate) exec: ExecEvent,
pub(crate) signaled: bool,
pub(crate) forced_failure: bool,
}
impl Events {
pub(crate) const NONE: Events = Events {
control: Control::Nothing,
owner_exited: false,
child_exited: false,
exec: ExecEvent::Nothing,
signaled: false,
forced_failure: false,
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ToChild {
Kill,
Term,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct Actions {
pub(crate) signal: Option<ToChild>,
pub(crate) reap: bool,
pub(crate) lost: bool,
pub(crate) pong: bool,
pub(crate) violation: bool,
}
pub(crate) fn decide(state: &mut LoopState, events: &Events) -> Actions {
let mut actions = Actions::default();
if events.forced_failure {
actions.lost = true;
return actions;
}
if events.exec != ExecEvent::Nothing {
state.exec_pending = false;
}
match events.control {
Control::Nothing => {}
Control::Eof => {
state.conn_open = false;
if state.armed {
actions.signal = Some(ToChild::Kill);
}
}
Control::Byte(byte) => match Command::decode(byte) {
Ok(Command::Kill) => actions.signal = Some(ToChild::Kill),
Ok(Command::Terminate) => actions.signal = Some(ToChild::Term),
Ok(Command::Disarm) => state.armed = false,
Ok(Command::Ping) if state.test_hooks => actions.pong = true,
Ok(Command::Allow | Command::Deny | Command::Ping) | Err(_) => {
actions.signal = Some(ToChild::Kill);
actions.violation = true;
}
},
}
if events.owner_exited && state.owner_watched {
state.owner_watched = false;
if state.armed {
actions.signal = Some(ToChild::Kill);
}
}
if events.signaled {
actions.signal = Some(ToChild::Kill);
}
actions.reap = events.child_exited;
actions
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Verdict {
pub(crate) frame: Frame,
pub(crate) exit_code: i32,
}
pub(crate) fn conclude(report: Option<NotExecuted>, reaped: Option<i32>, lost: bool) -> Verdict {
if let Some(not_executed) = report {
return Verdict {
frame: Frame::NotExecuted(not_executed),
exit_code: codes::NOT_EXECUTED,
};
}
let Some(status) = reaped else {
return Verdict {
frame: Frame::StatusLost,
exit_code: codes::STATUS_LOST,
};
};
if lost {
return Verdict {
frame: Frame::Lost(status),
exit_code: codes::SUPERVISION,
};
}
Verdict {
frame: Frame::Status(status),
exit_code: exit_code_of(status),
}
}
fn exit_code_of(wait_status: i32) -> i32 {
match wait_status & 0x7f {
0 => (wait_status >> 8) & 0xff,
signal => 128 + signal,
}
}
#[cfg(test)]
#[path = "step_tests.rs"]
mod step_tests;