use super::*;
#[cfg(unix)]
pub(super) fn daemon_fds(dir: &tempfile::TempDir) -> (DaemonFds, [std::fs::File; 2]) {
use std::os::fd::AsRawFd as _;
let listener = std::fs::File::create(dir.path().join("listener.stand-in")).unwrap();
let pidfile = std::fs::File::create(dir.path().join("pidfile.stand-in")).unwrap();
let fds = DaemonFds {
listener: listener.as_raw_fd(),
pidfile: pidfile.as_raw_fd(),
};
(fds, [listener, pidfile])
}
#[cfg(unix)]
#[derive(Debug, Default)]
pub(super) struct AdoptingRunner;
#[cfg(unix)]
pub(super) const STAND_IN_SPAWN_PID: u32 = 7000;
#[cfg(unix)]
#[derive(Debug)]
pub(super) struct StandInProc {
pid: u32,
}
#[cfg(unix)]
impl RunningProcess for StandInProc {
fn pid(&self) -> u32 {
self.pid
}
async fn wait(&mut self) -> ExitOutcome {
core::future::pending().await
}
fn signal(&mut self, _sig: crate::runner::StopSignal) -> Result<(), RunnerError> {
Ok(())
}
fn kill_tree(&mut self) -> Result<(), RunnerError> {
Ok(())
}
}
#[cfg(unix)]
fn stand_in_io() -> ProcIo {
let (_logs_tx, logs) = mpsc::channel(1);
let (_from_child_tx, from_child) = mpsc::channel(1);
ProcIo {
logs,
from_child,
to_child: closed_tx(),
log_ctl: closed_tx(),
to_stdin: closed_tx(),
}
}
#[cfg(unix)]
fn closed_tx<T>() -> mpsc::Sender<T> {
let (tx, rx) = mpsc::channel(1);
drop(rx);
tx
}
#[cfg(unix)]
impl ProcessRunner for AdoptingRunner {
type Proc = StandInProc;
fn spawn(&self, _spec: &SpawnSpec) -> Result<(Self::Proc, ProcIo), RunnerError> {
Ok((
StandInProc {
pid: STAND_IN_SPAWN_PID,
},
stand_in_io(),
))
}
fn adopt(&self, spec: crate::runner::AdoptSpec) -> Result<(Self::Proc, ProcIo), RunnerError> {
Ok((StandInProc { pid: spec.pid }, stand_in_io()))
}
}
#[cfg(unix)]
pub(super) fn carried(
name: &str,
id: u32,
pid: Option<u32>,
mutate: impl FnOnce(&mut ProcessEntry),
) -> CarriedSheep {
carried_marked(name, id, pid, false, None, false, mutate)
}
#[cfg(unix)]
pub(super) fn carried_marked(
name: &str,
id: u32,
pid: Option<u32>,
pending_delete: bool,
manual: Option<PendingManual>,
ready_failed: bool,
mutate: impl FnOnce(&mut ProcessEntry),
) -> CarriedSheep {
let spec = app_with(name, |app| app.autorestart = false);
let mut entry = ProcessEntry {
pid,
..plain_entry(id, spec)
};
mutate(&mut entry);
CarriedSheep::from_entry(
&entry,
0,
CarriedFds::none(),
pending_delete,
manual,
ready_failed,
None,
)
}
#[cfg(unix)]
pub(super) fn without_handles(carried: CarriedSheep) -> crate::handover::adopt::AdoptedSheep {
crate::handover::adopt::AdoptedSheep {
carried,
out_pipe: None,
err_pipe: None,
out_log: None,
err_log: None,
stdin_pipe: None,
channel: None,
dog_link: None,
}
}
#[cfg(unix)]
pub(super) const fn counters(next_id: u32) -> Counters {
Counters {
next_id,
next_deadline: 0,
next_action_stamp: 0,
}
}
#[cfg(unix)]
pub(super) fn adoptable_child(script: &str) -> u32 {
use std::os::unix::process::CommandExt as _;
std::process::Command::new("/bin/sh")
.args(["-c", script])
.process_group(0)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("a test host can spawn a shell")
.id()
}
#[cfg(unix)]
pub(super) fn respawnable(mutate: impl FnOnce(&mut AppConfig)) -> impl FnOnce(&mut ProcessEntry) {
move |entry: &mut ProcessEntry| {
entry.spec = app_with("web", |app| {
app.script = "/bin/sh".to_owned();
app.args = vec!["-c".to_owned(), "sleep 30".to_owned()];
mutate(app);
});
}
}
#[cfg(unix)]
pub(super) async fn flock_until(
sup: &SupervisorHandle,
done: impl Fn(&[ProcessInfo]) -> bool,
what: &str,
) -> Vec<ProcessInfo> {
tokio::time::timeout(Duration::from_secs(20), async {
loop {
let info = sup.list().await;
if done(&info) {
return info;
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
})
.await
.unwrap_or_else(|_| panic!("{what}"))
}