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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Linux cgroup v2 leaf containment.
//!
//! When cgroup v2 is mounted, delegated, and writable with `cgroup.kill`
//! support, the spawned child is placed in a freshly created leaf sub-cgroup.
//! Teardown writes `"1"` to `cgroup.kill` — the kernel atomically kills every
//! process in the leaf (fork-proof). Falls back to the process-group mechanism
//! when any precondition fails (see `try_create_leaf`).
//!
//! # Delegation prerequisite
//! cgroup v2 requires "no internal processes": a non-root cgroup may not
//! contain processes AND child cgroups simultaneously. This implementation
//! creates the leaf as a direct child of the supervisor's cgroup, which works
//! correctly only when the supervisor's cgroup is already an inner node (i.e.
//! it contains no processes itself). This is the normal case in properly
//! delegated slices (systemd user slices, container environments). On hosts
//! where the supervisor IS a leaf (root cgroup, or an undelegated slice), the
//! child's `cgroup.procs` write will fail with EBUSY/EINVAL; the placement
//! step handles this gracefully by falling back to the process-group
//! mechanism without aborting the spawn.
//!
//! # Async-signal-safety
//! `place_self_in_cgroup_pre_exec` is called from the spawn's in-child hook
//! (after `fork`, before `exec`). The only async-signal-safe operations there
//! are raw `libc::write`, `libc::close` and `libc::send` — no allocation, no
//! `format!`, no `String`. Its outcome crosses back through a socket pair
//! (`ReportChannel`).
//!
//! # Placement reports: when one is final, and what cosca may kill
//! Every decision about a leaf rests on these rules. `take_placement`,
//! `abandon_before_verdict` and `Drop` each apply them; none has a rule of its own.
//!
//! **Messages.** In its `pre_exec` the child sends, in order: an *intent*, carrying its pid,
//! before it touches the leaf; then, after its `cgroup.procs` write, its *report* — `Placed` or
//! the write's errno. It sends nothing else, and nothing after `exec`. The child's handle is the
//! pidfd the spawn handshake holds for it, which the abandonment is given: unlike a pid, it
//! cannot come to name another process, whoever reaps the child. A child that sent an intent has
//! its pidfd in the parent, because the handshake's go-ahead comes only once the parent holds it.
//!
//! **One verdict.** The parent ends the exchange exactly once, by one of two acts:
//! - *Decide.* `take_placement` reads the report — waiting for it, or for the child's exit; then it
//! sends *proceed* and closes the channel. A child whose send fails because the parent has
//! decided finds *proceed* queued, and carries on.
//! - *Abandon.* A spawn that failed before its verdict shuts the channel for reading. Every
//! message sent before that is still read; every send after it fails with no *proceed* queued,
//! and the child exits without `exec`. After abandonment no child execs, in or out of a leaf.
//!
//! **Final.** After either act, what was received is all there will ever be. Before it, a report
//! is final once received, or once the child has exited (seen through a pidfd or `waitid`) or
//! the leaf is removed (a removed leaf admits no member). Otherwise it is in flight, whatever
//! `spawn` returned — std's `spawn` can return before the child has run any `pre_exec`.
//!
//! **Absence.** Nothing of the child's is in the leaf only when there is proof of it:
//! - a final report other than `Placed` — the child either never entered, or exited before
//! reporting (its placement may have been interrupted between the write and the send). Either
//! way it never exec'd, so it never ran a program that could fork into the leaf; or
//! - the leaf's own removal — `rmdir` succeeds only on a leaf with no live member.
//!
//! **Kill.** cosca kills through a leaf whenever it lacks proof of absence, and never when it has
//! it: an occupant of a leaf that provably holds nothing of the child's is not cosca's to kill.
//! The one exception is a tree its caller opted out of teardown (`detach`, `kill_on_drop(false)`):
//! that leaf is disarmed, and its `Drop` never kills. A child cosca gives up on is also killed
//! itself, through its pidfd, whatever the leaf's kill returned — it may have left the leaf, or
//! never entered it — and, when it exec'd outside the leaf, as the process group it leads. An
//! elevation front is sent nothing: an exited one is reaped, and one its leaf's kill reached is
//! waited for (see `abandon_before_verdict`).
//!
//! **Ownership.** The child is this process's own, but another reaper in the process may reap it
//! (see `Command::contain`), so cosca looks at it through its handle before anything it sends by
//! number. It leads its own process group (`process_group(0)`), and anything it forks after `exec`
//! starts in that group. A process group's id cannot name another group while its leader is
//! unreaped. cosca reaps a child only when no handle owns it — an abandoned spawn whose runtime
//! dropped it — and, but for an elevation front, only after signalling it directly. It does so
//! through an [`OwnedChild`](crate::child::owned::OwnedChild) over the child's pidfd, never by
//! its pid.
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
/// Test-only fault seams for the leaf-creation steps a temp directory cannot reach.
///
/// Thread-local with take semantics — arm and call on one thread, then assert the flag was
/// consumed — so parallel tests in this binary cannot arm each other's faults, matching
/// `treewalk::fault` and `fdmarker::fault`.
pub
pub
// Selection only: the cgroup lane runs this module's tests whole.
default_labels!;