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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! `cosca`: unified cross-platform subprocess management.
//!
//! Build a process with [`Command`] (stdio, env, tree containment, elevation),
//! spawn it into an owned [`Child`], or open an already-running process with
//! [`Process`]. Sync by default; async counterparts live in the `tokio`
//! module behind the `tokio` feature.
//!
//! # Which type do I use?
//!
//! With the `tokio` feature, `cosca::tokio::Command`, `cosca::tokio::Child` and
//! `cosca::tokio::Process` are the async twins of [`Command`], [`Child`] and [`Process`];
//! [`ProcessId`](identity::ProcessId) is shared.
//!
//! ## A process you spawn: [`Command`] and [`Child`]
//!
//! [`Command::spawn`] makes a [`Child`], and the caller is its parent: the child's exit status,
//! its stdio pipes and, with [`contain`](Command::contain), its whole process tree are the handle's.
//! `contain` takes the strongest mechanism the host offers and fails the spawn when it cannot set
//! one up; [`contain_with`](Command::contain_with)(`BestEffort`) takes a weaker one instead
//! ([`ContainMode`]). [`kill_tree`](Child::kill_tree) kills the tree and waits for it. A drop kills
//! the contained tree, then the child, unless the handle opted out with
//! [`kill_on_drop(false)`](Command::kill_on_drop).
//!
//! ```no_run
//! fn serve() -> Result<(), cosca::error::Error> {
//! let mut cmd = cosca::run(["my-server", "--port", "8080"]);
//! let child = cmd.contain().spawn()?;
//! // ... later:
//! child.kill_tree()?;
//! let status = child.wait()?;
//! println!("my-server exited: {status}");
//! Ok(())
//! }
//! ```
//!
//! ## A process you did not spawn: [`Process`]
//!
//! [`Process::open_pid`] and [`Process::open`] hold a running process by identity. Once opened,
//! [`kill`](Process::kill), the tree acts such as [`kill_tree`](Process::kill_tree), and
//! [`wait`](Process::wait) reach that process or report it gone, never a later holder of its pid.
//! `Process::wait` reports no exit status; [`Child::wait`] does. A pid whose identity cannot be
//! read is an `Err`, not `Ok(None)`: usually [`Error::Unassessable`](error::Error::Unassessable),
//! and [`Process::open`]'s `# Errors` lists the rest.
//!
//! ```no_run
//! use cosca::error::Report;
//! use cosca::Process;
//!
//! fn stop(pid: u32) {
//! match Process::open_pid(pid) {
//! Ok(Some(process)) => {
//! if let Err(e) = process.kill().and_then(|()| process.wait()) {
//! eprintln!("could not stop pid {pid}: {}", Report::new(&e));
//! }
//! }
//! // No process has this pid.
//! Ok(None) => println!("pid {pid} is not running"),
//! Err(e) => eprintln!("could not open pid {pid}: {}", Report::new(&e)),
//! }
//! }
//! ```
//!
//! ## Naming a process exactly: [`ProcessId`](identity::ProcessId)
//!
//! A `ProcessId` is a pid plus a per-process key from the kernel, compared exactly, so the id of a
//! process that has exited does not equal the id of a later holder of its pid (except on Linux
//! below 6.9: see [`ProcessId`](identity::ProcessId)).
//! [`ProcessId::of`](identity::ProcessId::of) reads the id of a pid and answers
//! [`Found`](identity::Resolved::Found), [`Gone`](identity::Resolved::Gone) (no such process) or
//! [`Unknown`](identity::Resolved::Unknown) (the OS refused to say). [`Child::id`] gives a spawned
//! child's, or `None` when the OS refused to report it at spawn. An id is `Copy`, `Eq` and `Hash`.
//! It holds nothing and acts on nothing: [`exists`](identity::ProcessId::exists) and
//! [`is_alive`](identity::ProcessId::is_alive) read the OS at that moment, and [`Process::open`]
//! turns an id into a handle to act through.
//!
//! To keep an id past this process, [`to_record`](identity::ProcessId::to_record) makes an
//! [`identity::ProcessIdRecord`] (serializable with the `serde` feature), and the only way back is
//! `ProcessId::try_from`. It refuses a record that cannot be compared on this host, so a record
//! cannot silently alias onto an unrelated process: [`RecordErrorKind`](error::RecordErrorKind)
//! lists each refusal. A restored id says nothing about whether its process is still there:
//! opening it does.
//!
//! ```no_run
//! use cosca::error::Error;
//! use cosca::identity::{ProcessId, ProcessIdRecord, Resolved};
//! use cosca::Process;
//!
//! // The id of a child this program spawned.
//! fn save_child(child: &cosca::Child) -> Result<Option<ProcessIdRecord>, Error> {
//! match child.id() {
//! Some(id) => Ok(Some(id.to_record()?)),
//! // The OS refused to report its identity at spawn: there is no id to keep.
//! None => Ok(None),
//! }
//! }
//!
//! // The id of a process this program did not spawn.
//! fn save_pid(pid: u32) -> Result<Option<ProcessIdRecord>, Error> {
//! match ProcessId::of(pid) {
//! Resolved::Found(id) => Ok(Some(id.to_record()?)),
//! // No process has this pid.
//! Resolved::Gone => Ok(None),
//! // The OS refused to say: a process may well be there.
//! Resolved::Unknown => {
//! eprintln!("could not read the identity of pid {pid}");
//! Ok(None)
//! }
//! }
//! }
//!
//! // Later, possibly in another run of the program, from a stored record.
//! fn stop(record: &ProcessIdRecord) -> Result<(), Error> {
//! let id = ProcessId::try_from(record)?;
//! match Process::open(id)? {
//! Some(process) => process.kill(),
//! // That process is gone, and its pid may name another now.
//! None => Ok(()),
//! }
//! }
//! ```
//!
//! # Asking a child to stop: [`stop`]
//!
//! [`Child::terminate`], [`graceful_shutdown`](Child::graceful_shutdown) and their `_tree` forms ask the
//! child to stop: `SIGTERM` on Unix, `Ctrl+Break` on Windows, as [`stop::default`] says.
//! [`Command::graceful_stop`] replaces what is sent with an action composed from the functions of the
//! [`stop`] module, whose docs say what a stop reaches on each platform and how to choose one.
//!
//! # Platform requirements
//!
//! **Linux 5.6 or newer**, with `pidfd_open` and `openat2` not blocked by a seccomp profile or an
//! LSM.
//!
//! Each requirement comes from a different syscall:
//!
//! - `pidfd_open` needs 5.3, and cosca requires it to read the identity of any process by its pid,
//! [`ProcessId::current`](identity::ProcessId::current) and
//! [`to_record`](identity::ProcessId::to_record) / `restore` included. A refusal is
//! [`Error::Unsupported`](error::Error::Unsupported); a transient failure such as `EMFILE` is
//! [`Error::Io`](error::Error::Io) naming the syscall (an identity read that meets one is
//! unknown, and a member of a process group whose pidfd cannot be opened is not signalled).
//! `ProcessId::current` panics on either.
//! - `clone3` (5.3), or `clone` with `CLONE_PIDFD` (5.2) where `clone3` is refused, makes every child
//! cosca spawns, and gives its pidfd as the child is made, so no child exists without one. A spawn
//! whose `clone3` and `clone` are both refused with `ENOSYS` or `EPERM` is
//! [`Error::Unsupported`](error::Error::Unsupported) naming `spawn` and the running kernel, and makes
//! no child. The one exception is a kernel before 5.2, whose `clone` ignores `CLONE_PIDFD`: the
//! spawn is the same `Unsupported`, but a child was made, and it is left (cosca never reaps by
//! pid). Such kernels are below the 5.6 floor.
//! - On Linux 6.9 and newer pidfds have an inode of their own, one per process and never reused,
//! which is a process's identity: `pidfd_open` by pid, then `fstat`, with no `/proc`. Before 6.9
//! the identity is the `starttime` in `/proc/<pid>/stat`. cosca finds out which by asking the
//! pidfd's filesystem, and never parses a kernel version.
//! - `waitid(P_PIDFD)` needs 5.4, and is how an owned child is waited for and reaped, by its
//! pidfd and not its pid.
//! - `openat2` with `RESOLVE_BENEATH | RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS` needs 5.6. The
//! checked `/proc` view uses it to read a process's start time and to list processes.
//! Before Linux 6.9 a refusal (what counts as one is under
//! [`Error::Unsupported`](error::Error::Unsupported)) is that error 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. On 6.9+ a by-pid identity read, an open and a spawn need no view; listing a
//! process group or the process table, and a start time (`created_at`), still do.
//! [`ProcessId::current`](identity::ProcessId::current) needs only a readable `/proc/self/stat`
//! before 6.9, and on 6.9+ no `/proc` at all.
//! - The elevation shim (`cosca::init_helpers`) runs on Linux. macOS has no shim; `init_helpers` reports that
//! and does not start the program. Windows has none. 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.
//!
//! 32-bit Linux is unsupported: cosca does not compile there. Before Linux 6.14 a 32-bit kernel gives
//! only the low 32 bits of a pidfs inode number, so a process's identity (its pidfd's inode on 6.9+)
//! would not be unique.
//!
//! Linux on an architecture other than x86_64, aarch64, riscv64, s390x, powerpc64 and loongarch64 is
//! unsupported too, and cosca does not compile there: its raw `clone`, which gives a child its pidfd
//! from the fork, is checked to return 0 in the child only on those.
//!
//! [`Containment::CgroupV2`] additionally needs `cgroup.kill` (Linux 5.14); without it `CgroupV2`
//! is not used: on an older kernel a spawn gets a process group under both
//! [`ContainMode::Strongest`] and [`ContainMode::BestEffort`], as documented on
//! [`ContainMode`]. 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.
//!
//! **macOS 14.4 or newer**, for `proc_signal_with_audittoken`. It signals a process by an audit token
//! that names its pid and `exec` version, so the kernel never signals a process that took a reused
//! pid. Earlier versions are unsupported, with no fallback.
//!
//! # A console event to a Windows child: the proxy
//!
//! A Windows process can send a console event (Ctrl+C, Ctrl+Break) only to the console it is attached
//! to. `stop::send_console_event_via_proxy` (Windows) reaches a child on another console, or from a
//! caller with none (a service), through a helper process, the proxy, that has no console of its own.
//! The proxy is a role of the host program: call [`init_helpers`] first thing in `main`, and the default
//! (`CtrlProxy::HostExecutable`) starts the file at the host's path, when the stop runs, as the proxy.
//! A host that cannot call `init_helpers`, or that replaces its own image while it runs, installs the
//! `cosca-ctrl-proxy` executable (`cargo install cosca --bin cosca-ctrl-proxy`) and names it with
//! `Command::ctrl_proxy`. A Windows stop that would start the host as the proxy panics in a host that did
//! neither; see [`stop::default`](stop::default#panics).
//!
//! # 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.
compile_error!;
// The README as the doc of an item only rustdoc's test pass sees, so the doctest step compiles its Rust
// examples.
pub use Job;
pub use ;
pub use ;
pub use ;
pub use Signal;
pub use ;
pub use CtrlEvent;
pub use CtrlProxy;
pub use StopTarget;
pub use ctrl_proxy_main;
pub use init_helpers_with_test_hooks;
pub use ;
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);
// Not cfg-gated: keeping the policy platform-independent is what makes it testable from any host.
pub use Command;
// Declared here, not in `test_groups.rs`: integration roots include that file by `#[path]`.
// Also mounted into `tests/common`.
// Its Windows twin, under the same name.
// The integration tests' seccomp filters, included rather than copied.
pub use Recursive;
pub use Process;
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.
/// The stand-in for the console-event proxy, which the caller side's tests name and read.