use std::fs;
use std::io;
use std::os::fd::OwnedFd;
use std::os::unix::process::ExitStatusExt as _;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use command_fds::{CommandFdExt, FdMapping};
use nix::sys::signal::{self, Signal};
use nix::unistd::Pid;
use shep_core::signals::OperatorSignal;
use tokio::io::{
AsyncBufReadExt as _, AsyncRead, AsyncWrite, AsyncWriteExt as _, BufReader, Lines,
};
use tokio::net::UnixStream;
use tokio::process::{Child, Command};
use tokio::sync::mpsc;
use crate::boot::DIR_MODE;
use crate::channel::{CHANNEL_VERSION, ChildMessage, ShepherdMessage};
use crate::runner::{
ExitOutcome, FlushError, LogCtl, LogLine, Preflight, ProcIo, ProcessRunner, ReopenError,
RunnerError, RunningProcess, SpawnSpec, StdinWrite, StopSignal, check_log_ancestry,
open_log_path,
};
const CHANNEL_CAPACITY: usize = 32;
#[derive(Debug, Default)]
pub struct TokioRunner;
impl TokioRunner {
#[must_use]
pub fn new() -> Self {
Self
}
}
#[derive(Debug)]
pub struct TokioProc {
pid: u32,
child: Child,
}
impl RunningProcess for TokioProc {
fn pid(&self) -> u32 {
self.pid
}
async fn wait(&mut self) -> ExitOutcome {
match self.child.wait().await {
Ok(status) => ExitOutcome {
code: status.code(),
signal: status.signal(),
},
Err(error) => {
tracing::error!(pid = self.pid, %error, "process wait() failed");
ExitOutcome {
code: None,
signal: None,
}
}
}
}
fn signal(&mut self, sig: StopSignal) -> Result<(), RunnerError> {
signal_group(self.pid, to_nix_signal(sig))
}
fn kill_tree(&mut self) -> Result<(), RunnerError> {
signal_group(self.pid, Signal::SIGKILL)
}
fn signal_process(&mut self, sig: OperatorSignal) -> Result<(), RunnerError> {
let pid = i32::try_from(self.pid)
.ok()
.filter(|pid| *pid > 0)
.ok_or_else(|| {
RunnerError::SignalFailed(format!(
"pid {} is not a signallable process id",
self.pid
))
})?;
signal::kill(Pid::from_raw(pid), to_nix_operator_signal(sig))
.map_err(|error| RunnerError::SignalFailed(error.to_string()))
}
}
pub(crate) fn signal_group(pid: u32, sig: Signal) -> Result<(), RunnerError> {
let pid = i32::try_from(pid)
.ok()
.filter(|pid| *pid > 0)
.ok_or_else(|| {
RunnerError::SignalFailed(format!("pid {pid} is not a signallable process id"))
})?;
signal::kill(Pid::from_raw(-pid), sig)
.map_err(|error| RunnerError::SignalFailed(error.to_string()))
}
fn to_nix_signal(sig: StopSignal) -> Signal {
match sig {
StopSignal::Term => Signal::SIGTERM,
StopSignal::Int => Signal::SIGINT,
StopSignal::Quit => Signal::SIGQUIT,
StopSignal::Usr2 => Signal::SIGUSR2,
StopSignal::Kill => Signal::SIGKILL,
}
}
fn to_nix_operator_signal(sig: OperatorSignal) -> Signal {
match sig {
OperatorSignal::Hup => Signal::SIGHUP,
OperatorSignal::Int => Signal::SIGINT,
OperatorSignal::Quit => Signal::SIGQUIT,
OperatorSignal::Term => Signal::SIGTERM,
OperatorSignal::Usr1 => Signal::SIGUSR1,
OperatorSignal::Usr2 => Signal::SIGUSR2,
OperatorSignal::Winch => Signal::SIGWINCH,
OperatorSignal::Cont => Signal::SIGCONT,
OperatorSignal::Kill => Signal::SIGKILL,
}
}
fn what_exec_will_find(spec: &SpawnSpec) -> Preflight {
if spec.program.is_empty() {
return Preflight::Unknown;
}
let program = Path::new(&spec.program);
if spec.program.contains('/') {
let full = if program.is_absolute() {
program.to_path_buf()
} else {
match &spec.cwd {
Some(cwd) => cwd.join(program),
None => return Preflight::Unknown,
}
};
if !definitely_absent(&full) {
return Preflight::Unknown;
}
return Preflight::Impossible(format!("no such file: {}", full.display()));
}
let Some(path) = spec.env.get("PATH").filter(|value| !value.is_empty()) else {
return Preflight::Unknown;
};
for dir in path.split(':').filter(|dir| !dir.is_empty()) {
match fs::metadata(Path::new(dir).join(program)) {
Ok(_) => return Preflight::Unknown,
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(_) => return Preflight::Unknown,
}
}
Preflight::Doubtful(format!(
"`{}` is not on the shepherd's PATH ({})",
spec.program,
summarise_path(path)
))
}
const PATH_ENTRIES_IN_MESSAGE: usize = 4;
fn summarise_path(path: &str) -> String {
let entries: Vec<&str> = path.split(':').filter(|dir| !dir.is_empty()).collect();
if entries.len() <= PATH_ENTRIES_IN_MESSAGE {
return path.to_string();
}
format!(
"{} and {} more entries",
entries[..PATH_ENTRIES_IN_MESSAGE].join(":"),
entries.len() - PATH_ENTRIES_IN_MESSAGE,
)
}
fn definitely_absent(path: &Path) -> bool {
matches!(fs::metadata(path), Err(err) if err.kind() == io::ErrorKind::NotFound)
}
impl ProcessRunner for TokioRunner {
type Proc = TokioProc;
fn preflight(&self, spec: &SpawnSpec) -> Preflight {
what_exec_will_find(spec)
}
fn spawn(&self, spec: &SpawnSpec) -> Result<(Self::Proc, ProcIo), RunnerError> {
let mut command = Command::new(&spec.program);
command.args(&spec.args);
if let Some(cwd) = &spec.cwd {
command.current_dir(cwd);
}
command.env_clear();
command.envs(&spec.env);
command.stdin(if spec.stdin {
Stdio::piped()
} else {
Stdio::null()
});
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
command.process_group(0);
if let Some(creds) = spec.credentials {
if let Some(gid) = creds.gid {
command.gid(gid);
}
command.uid(creds.uid);
}
let (from_child_tx, from_child_rx) = mpsc::channel(CHANNEL_CAPACITY);
let (to_child_tx, to_child_rx) = mpsc::channel(CHANNEL_CAPACITY);
if spec.channel {
command.env("SHEP_CHANNEL_FD", "3");
command.env("SHEP_CHANNEL_VERSION", CHANNEL_VERSION);
let (daemon_end, child_end) = UnixStream::pair().map_err(|error| {
RunnerError::SpawnFailed(format!("shepherd channel socketpair: {error}"))
})?;
let std_child_end = child_end.into_std().map_err(|error| {
RunnerError::SpawnFailed(format!("shepherd channel into_std: {error}"))
})?;
std_child_end.set_nonblocking(false).map_err(|error| {
RunnerError::SpawnFailed(format!("shepherd channel set_nonblocking: {error}"))
})?;
let child_fd = OwnedFd::from(std_child_end);
command
.as_std_mut()
.fd_mappings(vec![FdMapping {
parent_fd: child_fd,
child_fd: 3,
}])
.map_err(|error| {
RunnerError::SpawnFailed(format!("shepherd channel fd mapping: {error}"))
})?;
spawn_channel_pumps(daemon_end, from_child_tx, to_child_rx);
} else {
drop(from_child_tx);
drop(to_child_rx);
}
let mut child = command
.spawn()
.map_err(|error| RunnerError::SpawnFailed(error.to_string()))?;
drop(command);
let pid = child.id().ok_or_else(|| {
RunnerError::SpawnFailed("child exited before its pid could be read".to_string())
})?;
let (logs_tx, logs_rx) = mpsc::channel(CHANNEL_CAPACITY);
let (log_ctl_tx, log_ctl_rx) = mpsc::channel(CHANNEL_CAPACITY);
spawn_log_pump(
child.stdout.take(),
child.stderr.take(),
spec.out_file.clone(),
spec.err_file.clone(),
logs_tx,
log_ctl_rx,
);
let (to_stdin_tx, to_stdin_rx) = mpsc::channel(CHANNEL_CAPACITY);
if spec.stdin {
spawn_stdin_pump(child.stdin.take(), to_stdin_rx);
} else {
drop(to_stdin_rx);
}
let io = ProcIo {
logs: logs_rx,
from_child: from_child_rx,
to_child: to_child_tx,
log_ctl: log_ctl_tx,
to_stdin: to_stdin_tx,
};
Ok((TokioProc { pid, child }, io))
}
}
#[derive(Debug)]
struct LogFile {
path: PathBuf,
handle: Option<tokio::fs::File>,
}
impl LogFile {
async fn open(path: PathBuf) -> Self {
let handle = open_append(&path).await.ok();
Self { path, handle }
}
async fn append(&mut self, line: &str) {
let Some(handle) = self.handle.as_mut() else {
return;
};
let mut buf = String::with_capacity(line.len() + 1);
buf.push_str(line);
buf.push('\n');
if let Err(error) = handle.write_all(buf.as_bytes()).await {
tracing::error!(path = ?self.path, %error, "log file append failed");
}
}
async fn flush(&mut self) -> Result<(), FlushError> {
let Some(handle) = self.handle.as_mut() else {
return Ok(());
};
handle.flush().await.map_err(|error| FlushError {
message: format!("{}: {error}", self.path.display()),
})
}
async fn reopen(&mut self) -> Result<(), ReopenError> {
if let Some(handle) = self.handle.as_mut()
&& let Err(error) = handle.flush().await
{
tracing::error!(path = ?self.path, %error, "log file flush failed");
}
drop(self.handle.take());
match open_append(&self.path).await {
Ok(handle) => {
self.handle = Some(handle);
Ok(())
}
Err(error) => Err(ReopenError {
message: format!("{}: {error}", self.path.display()),
}),
}
}
}
#[derive(Debug)]
struct LogFiles {
out: LogFile,
err: LogFile,
}
impl LogFiles {
fn stream(&mut self, err: bool) -> &mut LogFile {
if err { &mut self.err } else { &mut self.out }
}
async fn serve(&mut self, ctl: LogCtl) {
match ctl {
LogCtl::Reopen { done } => {
let mut failures = Vec::new();
if let Err(error) = self.out.reopen().await {
failures.push(error.message);
}
if let Err(error) = self.err.reopen().await {
failures.push(error.message);
}
let result = if failures.is_empty() {
Ok(())
} else {
Err(ReopenError {
message: failures.join(", "),
})
};
let _ = done.send(result);
}
LogCtl::Flush { done } => {
let mut failures = Vec::new();
if let Err(error) = self.out.flush().await {
failures.push(error.message);
}
if let Err(error) = self.err.flush().await {
failures.push(error.message);
}
let result = if failures.is_empty() {
Ok(())
} else {
Err(FlushError {
message: failures.join(", "),
})
};
let _ = done.send(result);
}
}
}
}
enum AfterLine {
KeepReading,
StreamEnded,
LogsClosed,
}
async fn deliver_line(
result: io::Result<Option<String>>,
err: bool,
files: &mut LogFiles,
logs_tx: &mpsc::Sender<LogLine>,
ctl_rx: &mut mpsc::Receiver<LogCtl>,
) -> AfterLine {
match result {
Ok(Some(line)) => {
files.stream(err).append(&line).await;
let Some(slot) = reserve_slot(logs_tx, files, ctl_rx).await else {
return AfterLine::LogsClosed;
};
slot.send(LogLine { err, line });
AfterLine::KeepReading
}
Ok(None) => AfterLine::StreamEnded, Err(error) => {
tracing::error!(path = ?files.stream(err).path, %error, "log stream read failed");
AfterLine::StreamEnded
}
}
}
async fn reserve_slot<'tx>(
logs_tx: &'tx mpsc::Sender<LogLine>,
files: &mut LogFiles,
ctl_rx: &mut mpsc::Receiver<LogCtl>,
) -> Option<mpsc::Permit<'tx, LogLine>> {
loop {
tokio::select! {
slot = logs_tx.reserve() => return slot.ok(),
ctl = ctl_rx.recv() => match ctl {
Some(ctl) => files.serve(ctl).await,
None => return logs_tx.reserve().await.ok(),
},
}
}
}
async fn next_line<R>(lines: &mut Option<Lines<BufReader<R>>>) -> io::Result<Option<String>>
where
R: AsyncRead + Unpin,
{
match lines {
Some(lines) => lines.next_line().await,
None => core::future::pending().await,
}
}
fn spawn_log_pump<O, E>(
stdout: Option<O>,
stderr: Option<E>,
out_path: PathBuf,
err_path: PathBuf,
logs_tx: mpsc::Sender<LogLine>,
mut ctl_rx: mpsc::Receiver<LogCtl>,
) where
O: AsyncRead + Unpin + Send + 'static,
E: AsyncRead + Unpin + Send + 'static,
{
tokio::spawn(async move {
let mut files = LogFiles {
out: LogFile::open(out_path).await,
err: LogFile::open(err_path).await,
};
let mut out_lines = stdout.map(|reader| BufReader::new(reader).lines());
let mut err_lines = stderr.map(|reader| BufReader::new(reader).lines());
while out_lines.is_some() || err_lines.is_some() {
tokio::select! {
result = next_line(&mut out_lines) => {
match deliver_line(result, false, &mut files, &logs_tx, &mut ctl_rx).await {
AfterLine::KeepReading => {}
AfterLine::StreamEnded => out_lines = None,
AfterLine::LogsClosed => break,
}
}
result = next_line(&mut err_lines) => {
match deliver_line(result, true, &mut files, &logs_tx, &mut ctl_rx).await {
AfterLine::KeepReading => {}
AfterLine::StreamEnded => err_lines = None,
AfterLine::LogsClosed => break,
}
}
ctl = ctl_rx.recv() => {
match ctl {
Some(ctl) => files.serve(ctl).await,
None => break, }
}
() = logs_tx.closed() => break,
}
}
});
}
async fn open_append(path: &Path) -> io::Result<tokio::fs::File> {
check_log_ancestry(path)
.inspect_err(|error| tracing::error!(?path, %error, "log ancestry check failed"))?;
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
&& let Err(error) = tokio::fs::DirBuilder::new()
.mode(DIR_MODE)
.recursive(true)
.create(parent)
.await
{
tracing::error!(?path, %error, "log directory create failed");
return Err(error);
}
let mut options = tokio::fs::OpenOptions::new();
options.create(true).append(true);
open_log_path(&mut options, path)
.await
.inspect_err(|error| tracing::error!(?path, %error, "log file open failed"))
}
fn spawn_stdin_pump<W>(stdin: Option<W>, mut rx: mpsc::Receiver<StdinWrite>)
where
W: AsyncWrite + Unpin + Send + 'static,
{
tokio::spawn(async move {
let Some(mut stdin) = stdin else {
while let Some(StdinWrite { done, .. }) = rx.recv().await {
let _ = done.send(Err(RunnerError::WriteFailed(
"this child has no stdin pipe".to_string(),
)));
}
return;
};
while let Some(StdinWrite { line, done }) = rx.recv().await {
if done.is_closed() {
continue;
}
let mut bytes = line.into_bytes();
bytes.push(b'\n');
let result = match stdin.write_all(&bytes).await {
Ok(()) => stdin.flush().await,
Err(error) => Err(error),
};
let _ = done.send(result.map_err(|error| RunnerError::WriteFailed(error.to_string())));
}
});
}
fn spawn_channel_pumps(
daemon_end: UnixStream,
from_child_tx: mpsc::Sender<ChildMessage>,
mut to_child_rx: mpsc::Receiver<ShepherdMessage>,
) {
let (read_half, mut write_half) = daemon_end.into_split();
tokio::spawn(async move {
let mut lines = BufReader::new(read_half).lines();
loop {
match lines.next_line().await {
Ok(Some(line)) => match serde_json::from_str::<ChildMessage>(&line) {
Ok(msg) => {
if from_child_tx.send(msg).await.is_err() {
break; }
}
Err(error) => {
tracing::warn!(%line, %error, "malformed shepherd-channel frame");
}
},
Ok(None) => break, Err(error) => {
tracing::error!(%error, "shepherd-channel read failed");
break;
}
}
}
});
tokio::spawn(async move {
while let Some(msg) = to_child_rx.recv().await {
let mut line = match serde_json::to_string(&msg) {
Ok(json) => json,
Err(error) => {
tracing::error!(%error, "shepherd message encode failed");
continue;
}
};
line.push('\n');
if write_half.write_all(line.as_bytes()).await.is_err() {
break; }
}
});
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use std::fs;
use std::os::unix::fs::PermissionsExt as _;
use std::time::Duration;
use tokio::io::DuplexStream;
use tokio::sync::oneshot;
use tokio::time::timeout;
use super::*;
const PUMP_DEADLINE: Duration = Duration::from_secs(5);
const STREAM_BUFFER: usize = 4096;
struct PumpHarness {
dir: tempfile::TempDir,
out_path: PathBuf,
err_path: PathBuf,
out_writer: DuplexStream,
err_writer: DuplexStream,
logs: mpsc::Receiver<LogLine>,
ctl: mpsc::Sender<LogCtl>,
}
impl PumpHarness {
fn start() -> Self {
let dir = tempfile::tempdir().unwrap();
let out_path = dir.path().join("out.log");
let err_path = dir.path().join("err.log");
let (out_writer, out_reader) = tokio::io::duplex(STREAM_BUFFER);
let (err_writer, err_reader) = tokio::io::duplex(STREAM_BUFFER);
let (logs_tx, logs) = mpsc::channel(CHANNEL_CAPACITY);
let (ctl, ctl_rx) = mpsc::channel(CHANNEL_CAPACITY);
spawn_log_pump(
Some(out_reader),
Some(err_reader),
out_path.clone(),
err_path.clone(),
logs_tx,
ctl_rx,
);
Self {
dir,
out_path,
err_path,
out_writer,
err_writer,
logs,
ctl,
}
}
async fn feed(&mut self, err: bool, line: &str) {
let writer = if err {
&mut self.err_writer
} else {
&mut self.out_writer
};
writer
.write_all(format!("{line}\n").as_bytes())
.await
.unwrap();
let observed = timeout(PUMP_DEADLINE, self.logs.recv())
.await
.expect("the pump must forward a line it has read")
.expect("the pump must not end while its streams are open");
assert_eq!(
observed,
LogLine {
err,
line: line.to_string()
}
);
}
async fn reopen(&self) {
let outcome = self.reopen_for_answer().await;
assert_eq!(outcome, Ok(()), "this reopen must have worked");
}
async fn reopen_for_answer(&self) -> Result<(), ReopenError> {
let (done, ack) = oneshot::channel();
self.ctl
.send(LogCtl::Reopen { done })
.await
.expect("the pump must still be reading its control channel");
timeout(PUMP_DEADLINE, ack)
.await
.expect("a reopen must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement")
}
async fn flush(&self) {
let (done, ack) = oneshot::channel();
self.ctl
.send(LogCtl::Flush { done })
.await
.expect("the pump must still be reading its control channel");
let outcome = timeout(PUMP_DEADLINE, ack)
.await
.expect("a flush must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
assert_eq!(outcome, Ok(()), "this flush must have worked");
}
}
async fn assert_file_settles(path: &Path, expected: &str) {
let settled = timeout(PUMP_DEADLINE, async {
while fs::read_to_string(path).unwrap_or_default() != expected {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await;
assert!(
settled.is_ok(),
"{}: expected {expected:?}, found {:?}",
path.display(),
fs::read_to_string(path)
);
}
#[tokio::test]
async fn a_reopen_moves_both_streams_onto_the_recreated_paths() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
let rotated_out = pump.dir.path().join("out.log.1");
let rotated_err = pump.dir.path().join("err.log.1");
fs::rename(&pump.out_path, &rotated_out).unwrap();
fs::rename(&pump.err_path, &rotated_err).unwrap();
assert!(
!pump.out_path.exists(),
"sanity: the rename really moved it"
);
pump.reopen().await;
assert_eq!(fs::read_to_string(&rotated_out).unwrap(), "before-out\n");
assert_eq!(fs::read_to_string(&rotated_err).unwrap(), "before-err\n");
assert_eq!(fs::read_to_string(&pump.out_path).unwrap(), "");
assert_eq!(fs::read_to_string(&pump.err_path).unwrap(), "");
pump.feed(false, "after-out").await;
pump.feed(true, "after-err").await;
pump.reopen().await;
assert_eq!(fs::read_to_string(&pump.out_path).unwrap(), "after-out\n");
assert_eq!(fs::read_to_string(&pump.err_path).unwrap(), "after-err\n");
assert_eq!(fs::read_to_string(&rotated_out).unwrap(), "before-out\n");
assert_eq!(fs::read_to_string(&rotated_err).unwrap(), "before-err\n");
}
#[tokio::test]
async fn a_reopened_handle_still_appends_so_a_truncation_leaves_no_hole() {
let mut pump = PumpHarness::start();
pump.feed(false, "first").await;
fs::rename(&pump.out_path, pump.dir.path().join("out.log.1")).unwrap();
pump.reopen().await;
pump.feed(false, "second").await;
assert_file_settles(&pump.out_path, "second\n").await;
fs::File::create(&pump.out_path).unwrap();
assert_eq!(fs::metadata(&pump.out_path).unwrap().len(), 0);
pump.feed(false, "third").await;
assert_file_settles(&pump.out_path, "third\n").await;
}
#[tokio::test]
async fn opening_a_symlinked_log_path_is_refused_rather_than_followed() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("someone-elses.conf");
let link = dir.path().join("web-out.log");
fs::write(&target, b"original").unwrap();
std::os::unix::fs::symlink(&target, &link).unwrap();
let error = open_append(&link)
.await
.expect_err("a symlinked log path must not be opened for appending");
assert_eq!(
fs::read(&target).unwrap(),
b"original",
"a refused open must not have reached the symlink's target at all"
);
assert_eq!(
error.to_string(),
crate::runner::SYMLINK_REFUSED,
"the operator who reads this needs the word symlink, not ELOOP's \
own wording about levels of them"
);
}
#[tokio::test]
async fn a_reopen_is_answered_while_both_streams_are_idle() {
let pump = PumpHarness::start();
pump.reopen().await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
pump.reopen().await;
assert!(pump.out_path.exists(), "stdout's path must be back");
assert!(pump.err_path.exists(), "stderr's path must be back");
}
#[tokio::test]
async fn a_reopen_is_answered_while_the_logs_channel_is_full() {
let mut pump = PumpHarness::start();
let flooded = CHANNEL_CAPACITY + 1;
let mut written = String::new();
for n in 0..flooded {
let line = format!("line-{n}\n");
pump.out_writer.write_all(line.as_bytes()).await.unwrap();
written.push_str(&line);
}
assert_file_settles(&pump.out_path, &written).await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
pump.reopen().await;
assert!(pump.out_path.exists(), "stdout's path must be back");
assert!(pump.err_path.exists(), "stderr's path must be back");
for n in 0..flooded {
let observed = timeout(PUMP_DEADLINE, pump.logs.recv())
.await
.expect("the pump must resume once `logs` has room")
.expect("the pump must not end while its streams are open");
assert_eq!(observed.line, format!("line-{n}"));
}
}
async fn let_the_pump_settle() {
for _ in 0..16 {
tokio::task::yield_now().await;
}
}
#[tokio::test]
async fn a_stream_that_has_ended_is_still_reopened() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
drop(pump.out_writer);
let_the_pump_settle().await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
let (done, ack) = oneshot::channel();
pump.ctl
.send(LogCtl::Reopen { done })
.await
.expect("a pump with one live stream still reads its control channel");
let outcome = timeout(PUMP_DEADLINE, ack)
.await
.expect("a reopen must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
assert_eq!(
outcome,
Ok(()),
"an ended stream's path is as openable as a live one's"
);
assert!(
pump.out_path.exists(),
"the ended stream's path must come back too"
);
assert!(
pump.err_path.exists(),
"the live stream's path must be back"
);
}
#[tokio::test]
async fn a_send_fails_once_the_pump_has_ended() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
drop(pump.out_writer);
drop(pump.err_writer);
let ended = timeout(PUMP_DEADLINE, pump.logs.recv())
.await
.expect("the pump must end once both streams reach EOF");
assert!(ended.is_none(), "a pump that has ended sends nothing more");
let (done, ack) = oneshot::channel();
assert!(
pump.ctl.send(LogCtl::Reopen { done }).await.is_err(),
"a reopen aimed at an ended pump must fail to send"
);
assert!(ack.await.is_err());
}
#[tokio::test]
async fn a_reopen_that_cannot_open_a_path_again_answers_with_the_failure() {
let pump = PumpHarness::start();
pump.reopen().await;
fs::rename(&pump.out_path, pump.dir.path().join("out.log.1")).unwrap();
fs::create_dir(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
let error = pump
.reopen_for_answer()
.await
.expect_err("a reopen that could not open stdout's path must say so");
assert!(
error.message.contains(pump.out_path.to_str().unwrap()),
"the failure must name the path it could not open: {error}"
);
assert!(
!error.message.contains(pump.err_path.to_str().unwrap()),
"stderr's path opened fine and must not be reported: {error}"
);
assert!(
pump.err_path.exists(),
"stderr must be reopened even though stdout's open failed"
);
}
#[tokio::test]
async fn a_flush_lands_both_streams_and_keeps_writing_afterwards() {
let mut pump = PumpHarness::start();
pump.feed(false, "out-before").await;
pump.feed(true, "err-before").await;
pump.flush().await;
assert_eq!(fs::read_to_string(&pump.out_path).unwrap(), "out-before\n");
assert_eq!(fs::read_to_string(&pump.err_path).unwrap(), "err-before\n");
pump.feed(false, "out-after").await;
assert_file_settles(&pump.out_path, "out-before\nout-after\n").await;
}
#[tokio::test]
async fn a_flush_reports_the_write_its_file_never_took() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("out.log");
fs::write(&path, "").unwrap();
let mut log = LogFile {
path: path.clone(),
handle: Some(tokio::fs::File::open(&path).await.unwrap()),
};
log.append("never-lands").await;
let error = log
.flush()
.await
.expect_err("a write that never reached the file must fail the flush");
assert!(
error.message.starts_with(&format!("{}: ", path.display())),
"the failure must name the file it belongs to: {error}"
);
}
#[tokio::test]
async fn dropping_the_logs_receiver_ends_a_pump_with_no_line_to_carry_the_news() {
let pump = PumpHarness::start();
pump.reopen().await;
drop(pump.logs);
timeout(PUMP_DEADLINE, pump.ctl.closed())
.await
.expect("a pump whose `logs` receiver is gone must end");
}
#[tokio::test]
async fn dropping_the_control_sender_ends_the_pump() {
let mut pump = PumpHarness::start();
pump.feed(false, "still-here").await;
drop(pump.ctl);
let after = timeout(PUMP_DEADLINE, pump.logs.recv())
.await
.expect("the pump must end once nothing can control it");
assert!(after.is_none(), "a pump that has ended sends nothing more");
}
const STDIN_BUFFER: usize = 4;
#[tokio::test]
async fn a_line_whose_caller_stopped_waiting_is_dropped_rather_than_written_later() {
use tokio::io::AsyncReadExt as _;
let (mut child_end, daemon_end) = tokio::io::duplex(STDIN_BUFFER);
let (to_stdin, rx) = mpsc::channel(CHANNEL_CAPACITY);
spawn_stdin_pump(Some(daemon_end), rx);
let (first_done, first_ack) = oneshot::channel();
to_stdin
.send(StdinWrite {
line: "AAAAAAAA".to_string(),
done: first_done,
})
.await
.unwrap();
let (second_done, second_ack) = oneshot::channel();
to_stdin
.send(StdinWrite {
line: "BBBB".to_string(),
done: second_done,
})
.await
.unwrap();
drop(second_ack);
let (third_done, third_ack) = oneshot::channel();
to_stdin
.send(StdinWrite {
line: "CCCC".to_string(),
done: third_done,
})
.await
.unwrap();
drop(to_stdin);
let mut written = Vec::new();
timeout(PUMP_DEADLINE, child_end.read_to_end(&mut written))
.await
.expect("the pump must drain and close the pipe")
.expect("reading the pipe must succeed");
assert_eq!(
String::from_utf8(written).unwrap(),
"AAAAAAAA\nCCCC\n",
"the abandoned line must not reach the app, and the live one must"
);
assert!(
timeout(PUMP_DEADLINE, first_ack)
.await
.expect("the first write must be acknowledged")
.expect("its sender must outlive the write")
.is_ok()
);
assert!(
timeout(PUMP_DEADLINE, third_ack)
.await
.expect("the third write must be acknowledged")
.expect("its sender must outlive the write")
.is_ok()
);
}
fn preflight_spec(program: &str, cwd: Option<PathBuf>, path: Option<&str>) -> SpawnSpec {
let mut env = BTreeMap::new();
if let Some(path) = path {
env.insert("PATH".to_string(), path.to_string());
}
SpawnSpec {
name: "web".to_string(),
program: program.to_string(),
args: Vec::new(),
cwd,
env,
out_file: PathBuf::from("/dev/null"),
err_file: PathBuf::from("/dev/null"),
channel: false,
stdin: false,
credentials: None,
}
}
#[test]
fn a_program_that_is_really_there_is_nothing_to_report() {
let dir = tempfile::tempdir().unwrap();
let bin = dir.path().join("srv");
fs::write(&bin, "#!/bin/sh\n").unwrap();
let path_dir = dir.path().to_string_lossy().into_owned();
for spec in [
preflight_spec(&bin.to_string_lossy(), None, None),
preflight_spec("./srv", Some(dir.path().to_path_buf()), None),
preflight_spec("srv", None, Some(&path_dir)),
preflight_spec("srv", None, Some(&format!("/nonexistent:{path_dir}"))),
preflight_spec("./srv", None, None),
preflight_spec("srv", None, None),
preflight_spec("srv", None, Some("")),
preflight_spec("", None, None),
] {
assert_eq!(
what_exec_will_find(&spec),
Preflight::Unknown,
"reported something about a spec it cannot be certain about: {spec:?}"
);
}
}
#[test]
fn an_absent_path_is_impossible_and_names_the_path_that_was_tried() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(
what_exec_will_find(&preflight_spec(
"./proto-enum-api",
Some(dir.path().to_path_buf()),
None,
)),
Preflight::Impossible(format!(
"no such file: {}/./proto-enum-api",
dir.path().display()
)),
);
assert_eq!(
what_exec_will_find(&preflight_spec("/nonexistent/srv", None, None)),
Preflight::Impossible("no such file: /nonexistent/srv".to_string()),
);
}
#[test]
fn a_filesystem_error_that_is_not_absence_is_never_impossible() {
let dir = tempfile::tempdir().unwrap();
let wall = dir.path().join("wall");
fs::write(&wall, "not a directory\n").unwrap();
let through_a_file = wall.join("srv");
let kind = fs::metadata(&through_a_file).unwrap_err().kind();
assert_ne!(
kind,
io::ErrorKind::NotFound,
"the provocation must be an error OTHER than not-found, or this \
case proves nothing: {kind:?}"
);
assert_eq!(
what_exec_will_find(&preflight_spec(
&through_a_file.to_string_lossy(),
None,
None
)),
Preflight::Unknown,
"a path shep could not read is a suspicion, not a certainty, and \
must never refuse a batch"
);
if nix::unistd::Uid::effective().is_root() {
return;
}
let locked = dir.path().join("locked");
fs::create_dir(&locked).unwrap();
fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)).unwrap();
let behind_the_wall = locked.join("srv");
let observed = fs::metadata(&behind_the_wall)
.map(|_| ())
.map_err(|e| e.kind());
let verdict = what_exec_will_find(&preflight_spec(
&behind_the_wall.to_string_lossy(),
None,
None,
));
fs::set_permissions(&locked, fs::Permissions::from_mode(0o755)).unwrap();
assert_eq!(
observed,
Err(io::ErrorKind::PermissionDenied),
"the chmod must actually bite, or the assertion below is vacuous"
);
assert_eq!(
verdict,
Preflight::Unknown,
"a permission error on the way to the program must not take the \
rest of the flock down with it"
);
}
#[test]
fn a_bare_command_off_the_path_is_only_ever_doubtful() {
let found = what_exec_will_find(&preflight_spec("node", None, Some("/nonexistent")));
assert_eq!(
found,
Preflight::Doubtful("`node` is not on the shepherd's PATH (/nonexistent)".to_string()),
);
assert!(
!matches!(found, Preflight::Impossible(_)),
"a claim about an environment must never refuse a batch"
);
}
#[test]
fn a_long_path_is_summarised_and_a_startup_units_own_path_is_not() {
let fallback = "/usr/local/bin:/usr/bin:/bin";
assert_eq!(
summarise_path(fallback),
fallback,
"the PATH a unit actually gets must print in full"
);
let long = "/a:/b:/c:/d:/e:/f";
assert_eq!(summarise_path(long), "/a:/b:/c:/d and 2 more entries");
assert_eq!(summarise_path("/a:/b:/c:/d"), "/a:/b:/c:/d");
}
#[test]
fn a_zero_pid_is_refused_before_it_can_reach_the_daemons_own_group() {
let err = signal_group(0, Signal::SIGCONT).unwrap_err();
assert_eq!(
err.to_string(),
"signal delivery failed: pid 0 is not a signallable process id"
);
}
}