#[path = "child/graceful.rs"]
mod graceful;
#[path = "child/suspended.rs"]
mod suspended;
pub use suspended::SuspendedChild;
use std::collections::BTreeMap;
use std::process::ExitStatus;
use crate::containment::dispatch::Swept;
use crate::containment::Containment;
use crate::error::Error;
use crate::identity::ProcessId;
use crate::pipe::ParentEnd;
use crate::stdio::Fd;
pub(super) type FdPipes = BTreeMap<Fd, ParentEnd>;
fn try_take_reader(ends: &mut FdPipes, fd: Fd) -> std::io::Result<Option<super::stdio::OwnedRead>> {
match ends.remove(&fd) {
None => Ok(None),
Some(ParentEnd::Reader(r)) => super::stdio::register_reader(r).map(Some),
Some(end) => {
ends.insert(fd, end);
Ok(None)
}
}
}
fn try_take_writer(ends: &mut FdPipes, fd: Fd) -> std::io::Result<Option<super::stdio::OwnedWrite>> {
match ends.remove(&fd) {
None => Ok(None),
Some(ParentEnd::Writer(w)) => super::stdio::register_writer(w).map(Some),
Some(end) => {
ends.insert(fd, end);
Ok(None)
}
}
}
fn logged<T>(taken: std::io::Result<Option<T>>, fd: Fd, what: &str, consequence: &str) -> Option<T> {
taken.unwrap_or_else(|e| {
log::warn!(
"{fd} {what} dropped: tokio conversion failed ({report}); {consequence}",
report = crate::error::Report(&e)
);
None
})
}
const BROKEN_PIPE: &str = "the child will see a broken pipe on writes";
const EOF_ON_READS: &str = "the child will see EOF on reads";
#[derive(Debug)]
pub struct Child {
pub(crate) inner: crate::Child,
}
impl Child {
pub(super) fn from_sync(inner: crate::Child) -> Child {
Child { inner }
}
pub(super) async fn tear_down_awaiting(self) -> crate::child::drop_teardown::Torn {
self.inner
.tear_down_or_hand_back_awaiting(crate::child::owned::teardown::Disposition::HandBack)
.await
}
pub fn id(&self) -> Option<ProcessId> {
self.inner.id()
}
pub fn pid(&self) -> u32 {
self.inner.pid()
}
pub fn is_alive(&self) -> crate::identity::Liveness {
self.inner.is_alive()
}
pub fn containment(&self) -> Containment {
self.inner.containment()
}
pub fn graceful_mechanism(&self) -> crate::graceful::GracefulMechanism {
self.inner.graceful_mechanism()
}
pub fn elevation(&self) -> Option<crate::elevation::ElevationReport> {
self.inner.elevation()
}
pub fn stdin(&mut self) -> Option<super::stdio::ChildStdin> {
logged(self.stdin_or_error(), Fd::STDIN, "write end", EOF_ON_READS)
}
pub(super) fn stdin_or_error(&mut self) -> std::io::Result<Option<super::stdio::ChildStdin>> {
Ok(try_take_writer(&mut self.inner.pipes, Fd::STDIN)?.map(|inner| super::stdio::ChildStdin { inner }))
}
pub fn stdout(&mut self) -> Option<super::stdio::ChildStdout> {
logged(self.stdout_or_error(), Fd::STDOUT, "read end", BROKEN_PIPE)
}
pub(super) fn stdout_or_error(&mut self) -> std::io::Result<Option<super::stdio::ChildStdout>> {
Ok(try_take_reader(&mut self.inner.pipes, Fd::STDOUT)?.map(|inner| super::stdio::ChildStdout { inner }))
}
pub fn stderr(&mut self) -> Option<super::stdio::ChildStderr> {
logged(self.stderr_or_error(), Fd::STDERR, "read end", BROKEN_PIPE)
}
pub(super) fn stderr_or_error(&mut self) -> std::io::Result<Option<super::stdio::ChildStderr>> {
Ok(try_take_reader(&mut self.inner.pipes, Fd::STDERR)?.map(|inner| super::stdio::ChildStderr { inner }))
}
#[cfg(unix)]
pub fn fd_read_end(&mut self, fd: impl Into<crate::stdio::Fd>) -> Option<::tokio::net::unix::pipe::Receiver> {
let fd = fd.into();
logged(try_take_reader(&mut self.inner.pipes, fd), fd, "read end", BROKEN_PIPE)
}
#[cfg(unix)]
pub fn fd_write_end(&mut self, fd: impl Into<crate::stdio::Fd>) -> Option<::tokio::net::unix::pipe::Sender> {
let fd = fd.into();
logged(
try_take_writer(&mut self.inner.pipes, fd),
fd,
"write end",
EOF_ON_READS,
)
}
#[cfg(windows)]
pub fn fd_read_end(&mut self, fd: impl Into<Fd>) -> Option<super::stdio::ChildStdout> {
let fd = fd.into();
logged(try_take_reader(&mut self.inner.pipes, fd), fd, "read end", BROKEN_PIPE)
.map(|inner| super::stdio::ChildStdout { inner })
}
#[cfg(windows)]
pub fn fd_write_end(&mut self, fd: impl Into<Fd>) -> Option<super::stdio::ChildStdin> {
let fd = fd.into();
logged(
try_take_writer(&mut self.inner.pipes, fd),
fd,
"write end",
EOF_ON_READS,
)
.map(|inner| super::stdio::ChildStdin { inner })
}
#[cfg(windows)]
pub fn test_job_handle_contains_self(&self) -> bool {
self.inner.test_job_handle_contains_self()
}
#[cfg(windows)]
pub fn test_job_handle_contains(&self, pid: u32) -> bool {
self.inner.test_job_handle_contains(pid)
}
pub async fn wait(&mut self) -> Result<ExitStatus, Error> {
#[cfg(test)]
if let Some(error) = crate::child::spawn::failure::seams::take_wait_failure() {
return Err(error);
}
drop(self.inner.pipes.remove(&Fd::STDIN));
self.inner.wait_async().await
}
pub fn try_wait(&mut self) -> Result<Option<ExitStatus>, Error> {
self.inner.try_wait()
}
pub fn kill(&mut self) -> Result<(), Error> {
self.inner.kill()
}
pub async fn kill_tree(&self) -> Result<(), Error> {
self.kill_tree_after("kill_tree", None)
.wait_async()
.await
.map_err(|failure| failure.error)
}
pub fn send_kill_tree(&self) -> Result<(), Error> {
self.inner
.send_kill_tree_after("send_kill_tree", None)
.map_err(|failure| failure.error)
}
pub(crate) fn kill_tree_after(&self, op: &'static str, earlier: Option<&str>) -> Swept<'_> {
self.inner.kill_tree_after(op, earlier)
}
pub fn terminate_tree(&self) -> Result<(), Error> {
self.inner.terminate_tree()
}
pub async fn wait_tree(&self) -> Result<crate::containment::TreeDrain, Error> {
self.inner.tree.wait_drained_async(None).await
}
pub async fn wait_tree_timeout(
&self,
timeout: std::time::Duration,
) -> Result<crate::containment::TreeDrain, Error> {
self.inner
.tree
.wait_drained_async(super::wait::deadline_from(timeout))
.await
}
pub fn detach(&mut self) -> Result<(), Error> {
self.inner.disarm()
}
}
pub(crate) fn async_policy() -> crate::child::spawn::policy::SpawnPolicy {
crate::child::spawn::policy::SpawnPolicy {
teardown_warn_after: slow_teardown_after(),
unreaped_drop: crate::child::owned::unreaped::DropKind::LetGo,
}
}
#[cfg_attr(
all(test, not(target_os = "linux")),
allow(dead_code, reason = "a test opts in with its own threshold off Linux")
)]
const LATE: std::time::Duration = std::time::Duration::from_secs(1);
pub(crate) fn slow_teardown_after() -> Option<std::time::Duration> {
#[cfg(test)]
return fault::slow_teardown_after();
#[cfg(not(test))]
Some(LATE)
}
#[cfg(test)]
#[path = "child_tests.rs"]
mod child_tests;
#[cfg(all(test, windows))]
#[path = "child/disarm_refused_tests.rs"]
mod disarm_refused_tests;
#[cfg(test)]
#[path = "child_pipe_conversion_tests.rs"]
mod child_pipe_conversion_tests;
#[cfg(test)]
#[path = "child_wait_tree_tests.rs"]
mod child_wait_tree_tests;
#[cfg(test)]
pub(crate) mod fault {
use std::cell::Cell;
thread_local! {
static SLOW_TEARDOWN_AFTER: Cell<Option<std::time::Duration>> = const { Cell::new(None) };
}
#[cfg(target_os = "linux")]
pub(crate) fn warn_slow_teardowns() -> WarnSlowTeardowns {
warn_slow_teardowns_after(super::LATE)
}
pub(crate) fn warn_slow_teardowns_after(after: std::time::Duration) -> WarnSlowTeardowns {
SLOW_TEARDOWN_AFTER.with(|w| w.set(Some(after)));
WarnSlowTeardowns(())
}
#[must_use = "the warn is off again as soon as the guard is dropped"]
pub(crate) struct WarnSlowTeardowns(());
impl Drop for WarnSlowTeardowns {
fn drop(&mut self) {
SLOW_TEARDOWN_AFTER.with(|w| w.set(None));
}
}
pub(super) fn slow_teardown_after() -> Option<std::time::Duration> {
SLOW_TEARDOWN_AFTER.with(Cell::get)
}
}