use std::fmt;
use crate::error::Error;
use crate::identity::ProcessId;
use crate::signal::{Sent, Sig};
pub(crate) mod child;
pub(crate) mod drive;
#[cfg(test)]
pub(crate) mod fake;
#[cfg(target_os = "linux")]
pub(crate) mod linux;
#[cfg(target_os = "macos")]
pub(crate) mod macos;
#[cfg(unix)]
pub(crate) mod poll;
#[cfg(windows)]
pub(crate) mod windows;
#[cfg(unix)]
use std::os::fd::BorrowedFd;
#[cfg(windows)]
use std::os::windows::io::BorrowedHandle;
pub(crate) trait OsProcess: Send + Sync + fmt::Debug {
fn signal(&self, sig: Sig) -> Result<Sent, Error>;
fn exited(&self) -> Result<Exit<'_>, Error>;
fn exit_wait(&self) -> Result<ExitWait<'_>, Error> {
Ok(Box::new(drive::ProcessExit(self)))
}
#[cfg(any(target_os = "linux", windows))]
fn os_handle(&self) -> child::OsHandle<'_>;
}
pub(crate) type ExitWait<'a> = Box<dyn drive::Waitable<Output = ()> + Send + 'a>;
#[derive(Debug)]
pub(crate) enum Exit<'a> {
Exited,
Running(WaitFor<'a>),
#[cfg_attr(
not(target_os = "macos"),
allow(dead_code, reason = "only macOS's exit edge precedes the state the exit is read in")
)]
Recheck,
}
#[derive(Debug)]
pub(crate) enum WaitFor<'a> {
#[cfg(unix)]
Readable(BorrowedFd<'a>),
#[cfg(windows)]
Signalled(BorrowedHandle<'a>),
}
#[cfg(unix)]
macro_rules! exit_unobserved {
() => {
"its exit cannot be observed"
};
}
#[cfg(target_os = "macos")]
pub(crate) const EXIT_CONSEQUENCE: &str = exit_unobserved!();
#[cfg(unix)]
pub(crate) const OPEN_CONSEQUENCE: &str = concat!(exit_unobserved!(), " and no signal was sent");
pub(crate) const PIDLESS_CHILD: &str = "the child (pid unknown)";
pub(crate) fn child_named(pid: Option<u32>) -> String {
pid.map_or_else(|| PIDLESS_CHILD.to_owned(), |pid| format!("child {pid}"))
}
#[cfg(test)]
pub(crate) fn open(id: ProcessId) -> Result<Option<Box<dyn OsProcess>>, Error> {
open_with(crate::identity::HostKernel, id).map_err(OpenFailure::into_open_error)
}
#[derive(Debug)]
pub(crate) enum OpenFailure {
#[cfg(target_os = "linux")]
Call(crate::identity::FailedCall),
Finished(Error),
}
impl From<Error> for OpenFailure {
fn from(error: Error) -> Self {
OpenFailure::Finished(error)
}
}
impl OpenFailure {
pub(crate) fn into_open_error(self) -> Error {
match self {
#[cfg(target_os = "linux")]
OpenFailure::Call(call) => linux::open::open_error(call),
OpenFailure::Finished(error) => error,
}
}
}
pub(crate) fn open_with<'k, K: crate::identity::Kernel + Send + 'k>(
kernel: K,
id: ProcessId,
) -> Result<Option<Box<dyn OsProcess + 'k>>, OpenFailure> {
#[cfg(target_os = "linux")]
{
Ok(linux::open::open_verified_with(&kernel, id)?
.map(|pidfd| Box::new(linux::LinuxProcess::new(kernel, Some(id.pid()), pidfd)) as Box<dyn OsProcess + 'k>))
}
#[cfg(windows)]
{
Ok(windows::WindowsProcess::open(kernel, id)?.map(|p| Box::new(p) as Box<dyn OsProcess + 'k>))
}
#[cfg(target_os = "macos")]
{
Ok(macos::MacosProcess::open(kernel, id)?.map(|p| Box::new(p) as Box<dyn OsProcess + 'k>))
}
}
pub(crate) fn kill(p: &dyn OsProcess) -> Result<(), Error> {
p.signal(Sig::Kill).map(|_: Sent| ())
}
#[cfg(unix)]
pub(crate) fn terminate(p: &dyn OsProcess) -> Result<(), Error> {
p.signal(Sig::Term).map(|_: Sent| ())
}
#[cfg(windows)]
pub(crate) fn terminate(_p: &dyn OsProcess) -> Result<(), Error> {
Err(Error::terminate_unsupported())
}