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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Which cooperative shutdown signal a spawned child can be sent, and how far it reaches.
//!
//! Tree ownership and cooperative-signal mechanism are independent axes.
//! [`Containment`](crate::containment::Containment) / `Attached` answer "who tears this tree
//! down"; [`GracefulMechanism`] answers "what cooperative signal `terminate()` sends to this
//! child, and how far it goes". On Windows the two are correlated only because containment is
//! currently the only thing that sets `CREATE_NEW_PROCESS_GROUP` — a coincidence deliberately
//! encoded nowhere.
//!
//! The mechanism is a **spawn-time** fact, derived on Windows from the creation-flag word the
//! spawn actually passed to `CreateProcessW`. That word is a sound negative and an unsound
//! positive: a detaching or window-suppressing flag being present is enough to know the child is
//! not in this process's console, but no flag can establish that it is. A GUI-subsystem image
//! never attaches to its spawner's console whatever the flags say, and any child may
//! `FreeConsole`/`AllocConsole`/`AttachConsole` after it starts. So the mechanism says which
//! signal would be sent and whether this process has a route to send it — never that a given
//! call will arrive.
//!
//! One child has no such word at all: a UAC-elevated one, created on cosca's behalf by a system
//! service. It reports [`GracefulMechanism::Unknown`], which
//! [`Child::terminate`](crate::Child::terminate) refuses.
/// Which cooperative signal [`Child::terminate`](crate::Child::terminate) sends to a child, how
/// far that signal reaches when it is delivered, and whether this process has a route to deliver
/// it. Queried via [`Child::graceful_mechanism`](crate::Child::graceful_mechanism).
///
/// A spawn-time fact, not a delivery guarantee: see the module doc for what the value does and
/// does not claim.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GracefulMechanism {
/// Unix: an identity-bound `SIGTERM` to this process alone.
Process,
/// Windows: a console control event to the child's own console process group, whose
/// delivery **from this process** the creation flags do not exclude. Not a guarantee, and
/// not established per call.
ConsoleGroup,
/// Windows: **no in-process route.** This process's console is not the one the child would
/// receive an event in; whether the child leads a group of its own is not knowable from
/// here. Not "unreachable": a process attached to the child's own console can deliver an
/// event.
OtherConsoleGroup,
/// Windows: **cosca did not create this child.** A system service creates the UAC-elevated
/// child, with creation flags this process never sees, so whether it leads a console process
/// group of its own is unknowable from here — and a pid that leads none addresses its
/// leader's whole group instead. Refused, not attempted.
Unknown,
/// Windows: the child leads no process group of its own, and group leadership is fixed at
/// creation — so no console control event can be addressed to it ALONE by any process,
/// ever; one aimed at its pid reaches its leader's whole group instead.
None,
}
// Gated on `tokio` as well as Windows: the async `Child` is the only handle that can stop
// pinning its child's pid, so it is the only caller this distinction exists for.
#[cfg(all(windows, feature = "tokio"))]
impl GracefulMechanism {
/// Whether the signal this mechanism names is addressed to a **bare pid** — no handle, no
/// identity check — so a caller whose handle has stopped pinning that pid must refuse rather
/// than fire it at whatever the OS has since reissued it to. The mechanisms that send
/// nothing at all are false: their refusal is permanent and pid-independent, and reporting
/// it as a pinning problem would name the wrong cause and the wrong error variant.
///
/// Matched exhaustively, like [`signal`], so a future variant must decide this too.
pub(crate) fn addresses_a_bare_pid(self) -> bool {
match self {
GracefulMechanism::ConsoleGroup | GracefulMechanism::OtherConsoleGroup => true,
// Identity-bound (`crate::wait::terminate`), and unconstructible on Windows anyway.
GracefulMechanism::Process => false,
GracefulMechanism::Unknown | GracefulMechanism::None => false,
}
}
}
impl std::fmt::Display for GracefulMechanism {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
GracefulMechanism::Process => "process",
GracefulMechanism::ConsoleGroup => "own console group",
GracefulMechanism::OtherConsoleGroup => "own console group in another console",
GracefulMechanism::Unknown => "unknown",
GracefulMechanism::None => "none",
};
f.write_str(s)
}
}
/// Send the cooperative signal `mechanism` names to the child with identity `id`. Signal-only:
/// does not wait or reap. The crate's single dispatch point — every cooperative signal to an
/// owned child funnels through here.
///
/// Matched exhaustively with no wildcard, so a future variant is a compile error at the one
/// place this decision is made.
pub(crate) fn signal(mechanism: GracefulMechanism, id: crate::identity::ProcessId) -> Result<(), crate::error::Error> {
match mechanism {
GracefulMechanism::Process => crate::wait::terminate(id),
// One arm for both. This process's console list is readable, but absence from it does
// not imply undeliverable (see `containment::windows::terminate`), and refusing
// `OtherConsoleGroup` would refuse a child that re-attached itself to our console after
// it started.
#[cfg(windows)]
GracefulMechanism::ConsoleGroup | GracefulMechanism::OtherConsoleGroup => {
crate::containment::windows::terminate(id.pid())
}
#[cfg(not(windows))]
GracefulMechanism::ConsoleGroup | GracefulMechanism::OtherConsoleGroup => {
debug_assert!(false, "a console-group mechanism is unconstructible off Windows");
Err(crate::error::Error::Unsupported {
op: "graceful terminate".into(),
platform: std::env::consts::OS,
detail: "internal invariant: a console-group mechanism reached signal off Windows".into(),
})
}
GracefulMechanism::Unknown => Err(crate::error::Error::Unsupported {
op: "graceful terminate".into(),
platform: std::env::consts::OS,
detail: "cosca did not create this child — a system elevation service did — so \
whether it leads a console process group of its own is unknown, and an \
event addressed to its pid would reach its leader's whole group instead. \
Use kill() for a hard teardown."
.into(),
}),
GracefulMechanism::None => Err(crate::error::Error::Unsupported {
op: "graceful terminate".into(),
platform: std::env::consts::OS,
detail: "this child leads no console process group of its own, so no console control \
event can be addressed to it by any process; group leadership is fixed at \
creation. Spawn it with contain() to give it one, or use kill() for a hard \
teardown."
.into(),
}),
}
}
#[cfg(test)]
#[path = "graceful_tests.rs"]
mod graceful_tests;