1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! Async mirror of the foreign [`Process`](crate::Process). Introspection delegates
//! synchronously; only the death-watch and the graceful pair are async (`tokio::wait`).
//! NO stdio (its pipes belong to its real parent); every operation re-verifies identity; nothing here
//! reaps (the real parent collects the zombie).
use std::time::Duration;
use crate::error::Error;
use crate::identity::{ProcessId, RawPid};
use crate::process::Recursive;
#[path = "process/graceful.rs"]
mod graceful;
/// An async handle to a foreign process identified by `(pid, start_token)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Process {
inner: crate::process::Process,
}
impl From<crate::process::Process> for Process {
fn from(inner: crate::process::Process) -> Process {
Process { inner }
}
}
impl Process {
/// A handle to the process with this identity. Infallible — see
/// [`Process::from_id`](crate::Process::from_id) for why construction does not check
/// that the process is still there.
pub fn from_id(id: ProcessId) -> Process {
Process::from(crate::process::Process::from_id(id))
}
/// Resolve the process currently holding `pid`. Fallible, unlike
/// [`from_id`](Self::from_id): a bare pid has to be resolved into an identity first.
pub fn from_pid(pid: RawPid) -> crate::identity::Resolved<Process> {
crate::process::Process::from_pid(pid).map(Process::from)
}
/// This process's own handle. Infallible.
pub fn current() -> Process {
Process::from(crate::process::Process::current())
}
/// The stable identity (`(pid, start_token)`).
pub fn id(&self) -> ProcessId {
self.inner.id()
}
/// Whether this exact identity still resolves (zombie-inclusive; see
/// [`ProcessId::exists`]).
pub fn exists(&self) -> crate::identity::Existence {
self.inner.exists()
}
/// Whether the process is still running (zombie-exclusive; see [`ProcessId::is_alive`]).
pub fn is_alive(&self) -> crate::identity::Liveness {
self.inner.is_alive()
}
/// The parent process, by identity (see [`Process::parent`](crate::Process::parent) for
/// the identity-guard contract).
pub fn parent(&self) -> Result<Option<Process>, Error> {
Ok(self.inner.parent()?.map(Process::from))
}
/// The process's children (see [`Process::children`](crate::Process::children)).
pub fn children(&self, recursive: Recursive) -> Result<Vec<Process>, Error> {
Ok(self.inner.children(recursive)?.into_iter().map(Process::from).collect())
}
/// Resolve when the process exits. Death-watch — yields no `ExitStatus` (only the real
/// parent gets one). Non-reaping and signal-free; `Err` only on a watch failure (on Linux a
/// refused `pidfd_open` is `Unsupported`, see [`Error::Unsupported`](crate::error::Error::Unsupported)). Dropping the future cancels the watch on every
/// platform (the Windows watcher is released via its cancel event).
///
/// # Runtime
///
/// Needs a runtime with the IO driver enabled on Unix (the `#[tokio::main]` /
/// `#[tokio::test]` defaults) — missing it, tokio panics rather than returning a typed
/// error. On Windows the watch runs on the blocking pool (one thread per in-flight wait).
pub async fn wait(&self) -> Result<(), Error> {
crate::tokio::wait::wait_exit(self.inner.id()).await
}
/// Wait up to `timeout` for the process to exit. `Ok(true)` = exited; `Ok(false)` =
/// still alive at expiry. `Duration::ZERO` polls once. Non-reaping; cancellation and
/// runtime requirements as on [`wait`](Process::wait) (Unix additionally needs the time
/// driver). Errors as for [`wait`](Process::wait).
pub async fn wait_timeout(&self, timeout: Duration) -> Result<bool, Error> {
crate::tokio::wait::grace_wait(self.inner.id(), timeout).await
}
/// Hard-kill the process by identity (see [`Process::kill`](crate::Process::kill) for
/// the per-OS race-freedom contract). On Linux a refused `pidfd_open` is `Unsupported` (see
/// [`Error::Unsupported`](crate::error::Error::Unsupported)).
pub fn kill(&self) -> Result<(), Error> {
self.inner.kill()
}
/// Send `SIGTERM` (signal-only, identity-bound). Unix only; Windows returns
/// `Unsupported`, as does Linux when `pidfd_open` is refused (see [`Error::Unsupported`](crate::error::Error::Unsupported)).
pub fn terminate(&self) -> Result<(), Error> {
self.inner.terminate()
}
/// Best-effort hard identity-walk sweep of the tree (all platforms; the `TreeWalk`
/// contract — see [`Process::kill_tree`](crate::Process::kill_tree)).
pub fn kill_tree(&self) -> Result<(), Error> {
self.inner.kill_tree()
}
/// Best-effort graceful (`SIGTERM`) identity-walk sweep. Unix only; Windows returns
/// `Unsupported` (see [`Process::terminate_tree`](crate::Process::terminate_tree)).
pub fn terminate_tree(&self) -> Result<(), Error> {
self.inner.terminate_tree()
}
}
#[cfg(test)]
#[path = "process_tests.rs"]
mod process_tests;