running-process-core 3.1.0

Native process runtime for running-process
Documentation
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
//! Process containment via OS-level mechanisms.
//!
//! `ContainedProcessGroup` ensures all child processes die when the group is
//! dropped — even on a crash.
//!
//! - **Windows**: Uses a Job Object with `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE`.
//!   Dropping the group closes the handle, and Windows automatically terminates
//!   every process still assigned to the job.
//! - **Linux**: Uses `setpgid(0, 0)` to place children in a new process group
//!   and `PR_SET_PDEATHSIG(SIGKILL)` via `prctl()` so the kernel kills the
//!   child when the parent thread exits.
//!   **Caveat**: `PR_SET_PDEATHSIG` is reset on `execve` of a set-uid/set-gid
//!   binary and is tied to the *thread* that called `fork`, not the process
//!   leader. If the spawning thread exits before the parent process, children
//!   receive the signal prematurely.
//! - **macOS**: Uses `setpgid(0, 0)` for process grouping. `PR_SET_PDEATHSIG`
//!   is not available; parent-death notification is best-effort via polling
//!   `getppid()` in the child (not implemented here — the Drop-based SIGKILL
//!   to the process group is the primary mechanism).
//!
//! `Containment::Detached` spawns a process that intentionally survives the
//! group's lifetime (daemon pattern).
//!
//! # `RUNNING_PROCESS_ORIGINATOR` environment variable
//!
//! When an `originator` is set on a `ContainedProcessGroup`, all spawned child
//! processes inherit the environment variable `RUNNING_PROCESS_ORIGINATOR` with
//! the format `TOOL:PID`, where:
//!
//! - **TOOL** is the originator name (e.g., `"CLUD"`, `"JUPYTER"`)
//! - **PID** is the process ID of the parent that spawned the group
//!
//! Example value: `RUNNING_PROCESS_ORIGINATOR=CLUD:12345`
//!
//! ## Purpose
//!
//! This env var enables **cross-process session discovery** after crashes.
//!
//! ## Example
//!
//! ```no_run
//! use running_process_core::ContainedProcessGroup;
//!
//! let group = ContainedProcessGroup::with_originator("CLUD").unwrap();
//! let mut cmd = std::process::Command::new("sleep");
//! cmd.arg("60");
//! let child = group.spawn(&mut cmd).unwrap();
//! ```

use std::process::{Child, Command};

/// The environment variable name injected into child processes for
/// cross-process session discovery.
pub const ORIGINATOR_ENV_VAR: &str = "RUNNING_PROCESS_ORIGINATOR";

/// Containment policy for a spawned process.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Containment {
    /// The process is contained: it will be killed when the group is dropped,
    /// and (on Linux) when the parent thread dies.
    #[default]
    Contained,
    /// The process is detached: it will survive the group being dropped.
    /// Useful for daemon processes.
    Detached,
}

/// A group of processes that are killed together when the group is dropped.
///
/// On Windows this wraps a Job Object; on Unix it tracks a process-group ID
/// and sends `SIGKILL` to the group on drop.
pub struct ContainedProcessGroup {
    originator: Option<String>,

    #[cfg(windows)]
    job: super::WindowsJobHandle,

    #[cfg(unix)]
    pgid: std::sync::Mutex<Option<i32>>,

    #[cfg(unix)]
    child_pids: std::sync::Mutex<Vec<u32>>,
}

/// A handle to a process spawned inside a `ContainedProcessGroup`.
pub struct ContainedChild {
    pub child: Child,
    pub containment: Containment,
}

/// Format the originator env var value: `TOOL:PID`.
fn format_originator_value(tool: &str) -> String {
    format!("{}:{}", tool, std::process::id())
}

impl ContainedProcessGroup {
    /// Create a new process group without an originator.
    pub fn new() -> Result<Self, std::io::Error> {
        Self::build(None)
    }

    /// Create a new process group with an originator name.
    pub fn with_originator(originator: &str) -> Result<Self, std::io::Error> {
        Self::build(Some(originator.to_string()))
    }

    fn build(originator: Option<String>) -> Result<Self, std::io::Error> {
        #[cfg(windows)]
        {
            Self::new_windows(originator)
        }
        #[cfg(unix)]
        {
            Ok(Self {
                originator,
                pgid: std::sync::Mutex::new(None),
                child_pids: std::sync::Mutex::new(Vec::new()),
            })
        }
    }

    /// Returns the originator name, if set.
    pub fn originator(&self) -> Option<&str> {
        self.originator.as_deref()
    }

    /// Returns the full originator env var value (`TOOL:PID`), if set.
    pub fn originator_value(&self) -> Option<String> {
        self.originator.as_ref().map(|o| format_originator_value(o))
    }

    fn inject_originator_env(&self, command: &mut Command) {
        if let Some(ref originator) = self.originator {
            command.env(ORIGINATOR_ENV_VAR, format_originator_value(originator));
        }
    }

    /// Spawn a contained child process. The child will be killed when this
    /// group is dropped.
    pub fn spawn(&self, command: &mut Command) -> Result<ContainedChild, std::io::Error> {
        self.spawn_with_containment(command, Containment::Contained)
    }

    /// Spawn a detached child process. The child will survive this group
    /// being dropped.
    pub fn spawn_detached(&self, command: &mut Command) -> Result<ContainedChild, std::io::Error> {
        self.spawn_with_containment(command, Containment::Detached)
    }

    /// Spawn a child process with the given containment policy.
    pub fn spawn_with_containment(
        &self,
        command: &mut Command,
        containment: Containment,
    ) -> Result<ContainedChild, std::io::Error> {
        self.inject_originator_env(command);

        #[cfg(windows)]
        {
            self.spawn_windows(command, containment)
        }
        #[cfg(unix)]
        {
            self.spawn_unix(command, containment)
        }
    }
}

// ── Windows implementation ──────────────────────────────────────────────────

#[cfg(windows)]
impl ContainedProcessGroup {
    fn new_windows(originator: Option<String>) -> Result<Self, std::io::Error> {
        use std::mem::zeroed;
        use winapi::shared::minwindef::FALSE;
        use winapi::um::handleapi::INVALID_HANDLE_VALUE;
        use winapi::um::jobapi2::{CreateJobObjectW, SetInformationJobObject};
        use winapi::um::winnt::{
            JobObjectExtendedLimitInformation, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
            JOB_OBJECT_LIMIT_BREAKAWAY_OK, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
        };

        let job = unsafe { CreateJobObjectW(std::ptr::null_mut(), std::ptr::null()) };
        if job.is_null() || job == INVALID_HANDLE_VALUE {
            return Err(std::io::Error::last_os_error());
        }

        let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { zeroed() };
        info.BasicLimitInformation.LimitFlags =
            JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_BREAKAWAY_OK;
        let ok = unsafe {
            SetInformationJobObject(
                job,
                JobObjectExtendedLimitInformation,
                (&mut info as *mut JOBOBJECT_EXTENDED_LIMIT_INFORMATION).cast(),
                std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
            )
        };
        if ok == FALSE {
            let err = std::io::Error::last_os_error();
            unsafe { winapi::um::handleapi::CloseHandle(job) };
            return Err(err);
        }

        Ok(Self {
            originator,
            job: super::WindowsJobHandle(job as usize),
        })
    }

    fn spawn_windows(
        &self,
        command: &mut Command,
        containment: Containment,
    ) -> Result<ContainedChild, std::io::Error> {
        use winapi::shared::minwindef::FALSE;
        use winapi::um::jobapi2::AssignProcessToJobObject;

        match containment {
            Containment::Contained => {
                // Spawn the child, then assign it to our Job Object.
                let child = command.spawn()?;
                let handle = {
                    use std::os::windows::io::AsRawHandle;
                    child.as_raw_handle()
                };
                let ok = unsafe {
                    AssignProcessToJobObject(
                        self.job.0 as winapi::shared::ntdef::HANDLE,
                        handle.cast(),
                    )
                };
                if ok == FALSE {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(ContainedChild { child, containment })
            }
            Containment::Detached => {
                // Detached: simply do NOT assign the child to the Job
                // Object. The child will survive when the job handle is
                // closed (and contained siblings are killed).
                //
                // NOTE: `CREATE_BREAKAWAY_FROM_JOB` is only useful when
                // the *spawning* process is already inside a job and wants
                // to launch a child outside it. Here, our spawning process
                // is not in the job, so we just skip assignment.
                let child = command.spawn()?;
                Ok(ContainedChild { child, containment })
            }
        }
    }
}

// ── Unix implementation ─────────────────────────────────────────────────────

#[cfg(unix)]
impl ContainedProcessGroup {
    fn spawn_unix(
        &self,
        command: &mut Command,
        containment: Containment,
    ) -> Result<ContainedChild, std::io::Error> {
        use std::os::unix::process::CommandExt;

        match containment {
            Containment::Contained => {
                let pgid_lock = self.pgid.lock().expect("pgid mutex poisoned");
                let target_pgid = *pgid_lock;
                drop(pgid_lock);

                unsafe {
                    command.pre_exec(move || {
                        // Place child into the group's process group, or create
                        // a new one if this is the first child.
                        let pgid = target_pgid.unwrap_or(0);
                        if libc::setpgid(0, pgid) == -1 {
                            return Err(std::io::Error::last_os_error());
                        }

                        // Linux-only: ask the kernel to send SIGKILL to this
                        // child when the parent thread exits.
                        // NOTE: PR_SET_PDEATHSIG is tied to the calling
                        // *thread*, not the process. If the thread that spawned
                        // this child exits, the child receives the signal even
                        // if the parent process is still alive.
                        #[cfg(target_os = "linux")]
                        {
                            if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) == -1 {
                                return Err(std::io::Error::last_os_error());
                            }
                            // Re-check that the parent hasn't already died
                            // between fork() and prctl().
                            if libc::getppid() == 1 {
                                // Parent already exited; init adopted us.
                                libc::_exit(1);
                            }
                        }

                        Ok(())
                    });
                }

                let child = command.spawn()?;
                let pid = child.id();

                // Record the process group ID.
                let mut pgid_lock = self.pgid.lock().expect("pgid mutex poisoned");
                let group_pgid = if let Some(existing) = *pgid_lock {
                    existing
                } else {
                    // First child becomes the process group leader.
                    *pgid_lock = Some(pid as i32);
                    pid as i32
                };
                drop(pgid_lock);

                // Parent-side setpgid: the standard double-setpgid pattern.
                // Both parent and child call setpgid so the group assignment
                // is guaranteed regardless of scheduling order.  EACCES is
                // expected (child already exec'd) and harmless.
                unsafe {
                    libc::setpgid(pid as i32, group_pgid);
                }

                self.child_pids
                    .lock()
                    .expect("child_pids mutex poisoned")
                    .push(pid);

                Ok(ContainedChild { child, containment })
            }
            Containment::Detached => {
                unsafe {
                    command.pre_exec(|| {
                        // Create a new session so the child is fully detached.
                        if libc::setsid() == -1 {
                            return Err(std::io::Error::last_os_error());
                        }
                        Ok(())
                    });
                }
                let child = command.spawn()?;
                Ok(ContainedChild { child, containment })
            }
        }
    }
}

#[cfg(unix)]
impl Drop for ContainedProcessGroup {
    fn drop(&mut self) {
        let pgid = self.pgid.lock().expect("pgid mutex poisoned");
        if let Some(pgid) = *pgid {
            // Send SIGKILL to the entire process group. Negative PID targets
            // the group. Errors are ignored (processes may have already exited).
            unsafe {
                libc::killpg(pgid, libc::SIGKILL);
            }
        }
        drop(pgid);

        // Fallback: kill each tracked PID individually, in case any child
        // failed to join the process group (e.g. race between fork and exec).
        let pids = self.child_pids.lock().expect("child_pids mutex poisoned");
        for &pid in pids.iter() {
            unsafe {
                libc::kill(pid as i32, libc::SIGKILL);
            }
        }

        // Reap zombie children.  After SIGKILL, child processes remain as
        // zombies in the process table until waitpid() is called.  Without
        // reaping, kill(pid, 0) still reports them as alive and they consume
        // a slot in the process table.  SIGKILL is unblockable so blocking
        // waitpid returns essentially immediately.  If the PID is not our
        // child (or was already reaped), waitpid returns -1/ECHILD which we
        // safely ignore.
        for &pid in pids.iter() {
            unsafe {
                libc::waitpid(pid as i32, std::ptr::null_mut(), 0);
            }
        }
    }
}

// Windows: Job Object handle is closed by WindowsJobHandle::drop, which
// triggers JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE automatically.

// ── Default trait ───────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn containment_default_is_contained() {
        assert_eq!(Containment::default(), Containment::Contained);
    }

    #[test]
    fn containment_clone_and_copy() {
        let c = Containment::Contained;
        let c2 = c;
        assert_eq!(c, c2);
    }

    #[test]
    fn containment_debug_format() {
        assert_eq!(format!("{:?}", Containment::Contained), "Contained");
        assert_eq!(format!("{:?}", Containment::Detached), "Detached");
    }

    #[test]
    fn contained_process_group_creates_successfully() {
        let group = ContainedProcessGroup::new();
        assert!(group.is_ok());
    }

    #[test]
    fn with_originator_creates_successfully() {
        let group = ContainedProcessGroup::with_originator("CLUD");
        assert!(group.is_ok());
        let group = group.unwrap();
        assert_eq!(group.originator(), Some("CLUD"));
    }

    #[test]
    fn originator_value_format() {
        let group = ContainedProcessGroup::with_originator("CLUD").unwrap();
        let value = group.originator_value().unwrap();
        let expected = format!("CLUD:{}", std::process::id());
        assert_eq!(value, expected);
    }

    #[test]
    fn no_originator_returns_none() {
        let group = ContainedProcessGroup::new().unwrap();
        assert!(group.originator().is_none());
        assert!(group.originator_value().is_none());
    }

    #[test]
    fn format_originator_value_correct() {
        let value = format_originator_value("JUPYTER");
        let parts: Vec<&str> = value.splitn(2, ':').collect();
        assert_eq!(parts.len(), 2);
        assert_eq!(parts[0], "JUPYTER");
        assert_eq!(parts[1], std::process::id().to_string());
    }
}