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
//! The cgroup leaf as a mechanism: `cgroup.kill` names the tree in the kernel.
use std::os::fd::BorrowedFd;
use super::CgroupLeaf;
use crate::child::owned::teardown::Reach;
use crate::containment::dispatch::{TreeKillOutcome, Unkilled};
use crate::containment::drain_edge::DrainEdge;
#[cfg(feature = "tokio")]
use crate::containment::mechanism::AwaitableRelease;
use crate::containment::mechanism::{Contains, Release};
use crate::containment::treewalk::Listing;
use crate::containment::{Containment, Root, TerminateOutcome, TreeKill, TreeStop};
// The leaf's `Debug` hides its path; `subject` names it.
impl std::fmt::Debug for CgroupLeaf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CgroupLeaf").finish_non_exhaustive()
}
}
impl CgroupLeaf {
/// The steps of the leaf's drop that can be awaited instead of blocked on: its first step, the
/// one its drop takes first ([`remove_else_kill`](Self::remove_else_kill)), then the drain a
/// landed kill bounds, so that the drop finds it drained. A drain that cannot be watched is left
/// to the drop. The leaf stays where it is, so a future dropped mid-await leaves the whole of the
/// teardown to the drop. `child` is the child's pid, which names it in the debug lines.
#[cfg(feature = "tokio")]
pub(crate) async fn step_and_await_drain(&self, child: u32) {
// The leaf's drop makes this step again and reports what remains. A leaf that is occupied
// is the expected failure; one already gone is proof of a drain. Any other failure
// (EACCES, EIO) is the environment's, so it is logged, never asserted.
if let Err(e) = self.remove_else_kill() {
let occupied = matches!(e.raw_os_error(), Some(libc::EBUSY | libc::ENOTEMPTY));
if !occupied && !super::removed_after_drain(&e) {
log::debug!(
"child {child}: its leaf's first rmdir failed ({report})",
report = crate::error::Report(&e)
);
}
}
if !self.kill_landed() {
return;
}
if let Err(e) = DrainEdge::Cgroup(self).wait_async(None).await {
log::debug!(
"child {child}: its leaf's drain cannot be watched ({report}); the teardown waits for it on this thread, blocking",
report = crate::error::Report(&e)
);
}
}
}
impl Contains for CgroupLeaf {
/// `Ok` says the kernel was told to kill every member, however it was spawned (see
/// [`hard_kill`](CgroupLeaf::hard_kill)). The members `cgroup.procs` lists, in the leaf and in
/// every cgroup under it, are read before the kill, each proved through its pidfd and named by
/// its identity, so that a wait over it can wait for each member's own exit. The listing is kept
/// because the edge alone does not prove each listed member has exited.
///
/// Once the kill has landed, each listed member is placed through its pidfd
/// ([`reached_by_kill`](CgroupLeaf::reached_by_kill)): one still in the leaf was reached whatever
/// its credentials, and is waited for; one found outside is not waited for (see there); one that
/// cannot be placed is an error saying its exit cannot be observed.
fn kill(&self, root: Option<&dyn Root>) -> TreeKill<'_> {
let (listed, mut unobserved) = self.list_members(root.map(Root::pid));
let sent = self.send_kill(root);
let members = match sent {
TreeKillOutcome::Ran(Ok(())) => {
let (reached, unplaced) = self.reached_by_kill(listed);
unobserved.extend(unplaced);
reached
}
_ => listed.into_iter().map(|member| member.id).collect(),
};
TreeKill::listed(sent, members, unobserved, DrainEdge::Cgroup(self))
}
/// The bare `cgroup.kill`: no member is listed, for a caller that waits for none.
fn send_kill(&self, _root: Option<&dyn Root>) -> TreeKillOutcome {
TreeKillOutcome::Ran(self.hard_kill().map_err(Unkilled::from))
}
fn send_terminate(
&self,
_pid: u32,
_root: Option<&dyn Root>,
how: TreeStop,
) -> (TerminateOutcome, Option<Listing>) {
let signal = how.signal();
(TerminateOutcome::of(self.terminate(signal), Some(how.earlier())), None)
}
fn drain_edge(&self) -> Option<DrainEdge<'_>> {
Some(DrainEdge::Cgroup(self))
}
fn kind(&self) -> Containment {
Containment::CgroupV2
}
fn subject(&self) -> String {
format!("cgroup leaf {}", self.path().display())
}
/// Stops the leaf's `Drop` firing `cgroup.kill`.
fn disarm(&self) -> std::io::Result<()> {
CgroupLeaf::disarm(self);
Ok(())
}
/// Whether the task `pid` (which `pidfd` names, when there is one) is in this leaf or a cgroup
/// under it (see [`Subtree::holds`](super::Subtree::holds)): a `cgroup.kill` reaches it whatever
/// its credentials.
fn would_reach(&self, pid: u32, pidfd: Option<BorrowedFd<'_>>) -> Result<bool, crate::error::Error> {
self.subtree()?.holds(&*self.kernel, pid, pidfd)
}
/// A `cgroup.kill` that landed (or found the leaf gone) and where `child` is says it was in the
/// leaf (see [`Subtree::reached_by`](super::Subtree::reached_by)).
fn reached(&self, child: &crate::child::owned::OwnedChild) -> Result<bool, crate::error::Error> {
self.subtree()?.reached(child)
}
/// The leaf is killed through if it is armed and occupied, drained and removed: the release owns it.
fn release(self) -> (Release, Option<CgroupLeaf>) {
(Release::leaf(self), None)
}
/// The first step of the leaf's release and its drain, with the leaf left in place.
#[cfg(feature = "tokio")]
fn awaitable_release(&self) -> Option<AwaitableRelease<'_>> {
Some(AwaitableRelease::of_leaf(self))
}
}