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
//! Unix process-group and session containment.
//!
//! Two mechanisms are available:
//! - **ProcessGroup** (`process_group(0)` / `setpgid`): the child becomes a
//! process-group leader (pgid == pid). Teardown sends `killpg`. cgroup v2
//! preempts this on Linux when available. macOS uses this for
//! `ContainMode::Strongest`.
//! - **Session** (`setsid`): the child becomes a session leader *and*
//! process-group leader in a new session, detached from any controlling
//! terminal. Teardown is identical (`killpg` on the session's initial pgroup,
//! which equals the leader's pid). Useful for daemon-like children.
//!
//! **Mutual exclusivity:** `setsid` makes the child a session *and*
//! process-group leader simultaneously. Calling `setpgid`/`process_group(0)`
//! on a session leader fails with `EPERM`. Therefore Session mode applies
//! `setsid` *instead of* `process_group(0)` — never both.
//!
//! **Self-`setsid` escape:** a child that calls `setsid` itself exits the
//! parent's session/group; containment is then best-effort. This applies to
//! both mechanisms and is documented as a known limitation (not a sandbox).
//!
//! Parent-side signals use `nix` (not hand-rolled `libc`).
//!
//! # PGID-reuse caveat
//! `kill_tree` must run *before* the leader is reaped (`wait`): once reaped, the
//! kernel may recycle the leader's PID/PGID, so `killpg` could signal an
//! unrelated process group. The crate's `Drop` kills before it reaps, so the
//! common path is safe; an explicit `wait()` then `kill_tree()` is the unsafe
//! ordering. cgroup v2 and the identity-reverifying TreeWalk mechanism do not
//! have this hole — prefer them when the guarantee matters.
use io;
use ;
use Pid;
/// Apply pre-spawn group setup to `std_cmd` (root spawns only).
/// Must not be combined with `set_session` on the same command.
pub
/// Apply pre-spawn session setup to `std_cmd` via a `pre_exec` `setsid` call
/// (root spawns only, `ContainMode::Session`).
///
/// `setsid` makes the child a session leader *and* process-group leader (pgid
/// == sid == pid), detached from any controlling terminal. Because the child is
/// already a process-group leader after `setsid`, calling `setpgid` or
/// `process_group(0)` on it would return `EPERM` — do not call
/// `set_process_group` on the same command.
///
/// The `pre_exec` closure is async-signal-safe: it calls only raw `libc::setsid`
/// (no allocation, no unwinding). Failure of `setsid` aborts the spawn.
pub
/// Hard-kill the whole process group. ESRCH (gone) is success; EPERM falls
/// back to the leader directly (pgid == pid for a process-group root).
pub
/// SIGTERM the group; ESRCH (gone) is success; EPERM falls back to the leader
/// directly (valid because pgid == pid — the sudo-wrapper case).
pub