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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! `cosca`: unified cross-platform subprocess management.
//!
//! Build a process with [`Command`] (stdio, env, tree containment, elevation),
//! spawn it into an owned [`Child`], or attach to an already-running process by
//! pid via [`Process`]. Sync by default; async counterparts live in the `tokio`
//! module behind the `tokio` feature.
//!
//! # Platform requirements
//!
//! **Linux 5.6 or newer**, with `pidfd_open` and `openat2` not blocked by a seccomp profile.
//! Each requirement comes from a different syscall:
//!
//! - `pidfd_open` needs 5.3, and cosca requires a pidfd for every child it spawns. A refusal is
//! [`Error::Unsupported`](error::Error::Unsupported); a transient failure such as `EMFILE` is
//! [`Error::Io`](error::Error::Io) naming the syscall. `main` does not require a pidfd
//! at spawn yet ([#341](https://github.com/bindreams/cosca/issues/341)).
//! - `waitid(P_PIDFD)` needs 5.4, and is what a pidfd-based reap needs. On `main` only the cgroup
//! leaf reaps that way; an owned child's waits and reaps still go by pid
//! ([#341](https://github.com/bindreams/cosca/issues/341)).
//! - `openat2` with `RESOLVE_BENEATH | RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS` needs 5.6. The
//! checked `/proc` view uses it to read a process's identity. A refusal is
//! [`Error::Unsupported`](error::Error::Unsupported) naming `openat2`, from a by-pid identity
//! read, wait or kill, and from a spawn inside
//! [`Error::MayHaveStarted`](error::Error::MayHaveStarted): a spawn reads the identity once the
//! child runs. [`ProcessId::current`](identity::ProcessId::current)
//! needs only a readable `/proc/self/stat`.
//! - The elevation shim (`cosca::init`) runs on Linux. Other platforms have no shim; `init` reports
//! that and does not start the program. It needs `/proc` mounted: it reaches cosca's socket through
//! `/proc/thread-self/fd`. Without it the shim refuses, saying so, and the program is not started.
//!
//! [`Containment::CgroupV2`] additionally needs `cgroup.kill` (Linux 5.14); without it `CgroupV2`
//! is not used and containment falls back as documented on [`Containment`]. It also assumes kernel
//! commit `b69bb476dee9` ("cgroup: fix race between fork and cgroup.kill"), in mainline from 6.14
//! or in a stable kernel that carries it. cosca does not probe for it; the cost of its absence is
//! described under [`Command::kill_on_drop`].
//!
//! Linux 5.6 on its own oopses under systemd 254 or newer: `systemd-udevd` writes a cgroup's
//! `memory.pressure`, and that write dereferences NULL in the 5.6 kernel. cosca does not touch
//! pressure-stall information; the CI guest boots 5.6 with `psi=0` for that reason.
//!
//! # A child under a debugger
//!
//! A debugger that traces a [`Child`] owns its signal delivery: a graceful signal, such as the
//! one [`Child::terminate_tree`] sends, reaches the child only if the debugger passes it on. It
//! cannot cancel the `SIGKILL` of [`Child::kill`], but it can delay the death:
//!
//! - On Linux only if it traces exits (`PTRACE_O_TRACEEXIT`): it then holds the dying child at an
//! exit stop until it continues it ([ptrace(2)], BUGS). Otherwise the child dies at once.
//! - On macOS a running child stops for the debugger on `SIGKILL` too, and a stopped child just
//! holds it. Either way the child dies once the debugger resumes it, detaches or exits.
//!
//! [`Child::wait`] returns the child's exit once the debugger lets go of it: when it detaches, when
//! it collects the exit, which hands the child back, or when it exits itself. The debugger's exit
//! kills the child on macOS; on Linux it detaches it, or kills it if the debugger asked to
//! (`PTRACE_O_EXITKILL`).
//!
//! [ptrace(2)]: https://man7.org/linux/man-pages/man2/ptrace.2.html
// `SpawnLockGuard` is `#[must_use]`, but only this lint keeps `let _ = spawn_lock();` (a lock released
// at once) flagged, as rustc's `let_underscore_lock` did when the guard was a `MutexGuard`. Discard a
// result on purpose with `_ = expr;`. Crate-level, not in `[lints]`, which would extend it to the
// tests and test binaries.
pub use Job;
pub use ;
pub use ;
pub use ;
pub use ;
pub use GracefulMechanism;
pub use ;
pub use ;
// Lets `tests/common/locked.rs`, which names `cosca::test_spawn_lock`, compile into the lib's tests.
extern crate self as cosca;
/// Test-only: the same process-wide lock production spawns take internally
/// (`child::spawn::spawn_lock`), exposed so this crate's OWN integration tests
/// (`tests/*.rs`, a separate compilation unit that cannot name a `pub(crate)` item) can
/// serialize a raw `std::process::Command` spawn against it too — closing the same
/// fork-bystander-inherits-a-live-marker window `containment::fdmarker`'s module docs
/// describe, for a raw spawn that bypasses `cosca::Command` entirely. `#[doc(hidden)]`: not
/// public API, present only for this crate's own `tests/` binaries to link against.
///
/// Returns a guard: hold it for the raw spawn. Re-taking the lock on the same thread (a
/// `cosca::Command::spawn` under it, say) panics under `debug_assertions` instead of deadlocking.
/// Holds [`test_spawn_lock`]'s lock until dropped.
] SpawnLockGuard);
// Off Windows only the `Exact` completion (`resolve::exact`) is consumed, so the lib build sees
// the search policy as dead. The module is deliberately NOT cfg-gated: keeping it platform-independent is what makes
// its policy testable from a POSIX host, which is where most of this work happens. The allow
// goes away when the POSIX and default spawn paths route through it too.
pub use Command;
// Declared here, not in `test_groups.rs`: integration roots include that file by `#[path]`.
pub use ;
pub use ExitStatus;
/// Captured result of a finished process.
/// Start building a command from an argument vector.
/// Start building a command from a single command-line string.