use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use crate::process_identity::{process_start_time, ProcessIdentity, ProcessLiveness};
struct Orphans {
groups: BTreeSet<i32>,
file: Option<(PathBuf, ProcessIdentity)>,
}
static ORPHANS: Mutex<Orphans> = Mutex::new(Orphans {
groups: BTreeSet::new(),
file: None,
});
#[derive(Serialize, Deserialize)]
struct Record {
owner_pid: u32,
owner_started_at: i64,
groups: Vec<i32>,
}
pub(crate) fn started(group: i32) {
update(|groups| {
groups.retain(|g| crate::process::group_alive(*g));
groups.insert(group)
});
}
pub(crate) fn stopped(group: i32) {
update(|groups| groups.remove(&group));
}
#[derive(Debug)]
pub struct Listed(i32);
pub fn list(group: i32) -> Listed {
started(group);
Listed(group)
}
impl Drop for Listed {
fn drop(&mut self) {
stopped(self.0);
}
}
pub fn listed() -> Vec<i32> {
ORPHANS
.lock()
.unwrap_or_else(|e| e.into_inner())
.groups
.iter()
.copied()
.collect()
}
fn update(change: impl FnOnce(&mut BTreeSet<i32>) -> bool) {
let mut orphans = ORPHANS.lock().unwrap_or_else(|e| e.into_inner());
let changed = change(&mut orphans.groups);
if let (true, Some((path, owner))) = (changed, &orphans.file) {
write(path, owner, &orphans.groups);
}
}
fn write(path: &Path, owner: &ProcessIdentity, groups: &BTreeSet<i32>) {
let record = Record {
owner_pid: owner.pid,
owner_started_at: owner.started_at,
groups: groups.iter().copied().collect(),
};
let Ok(bytes) = serde_json::to_vec(&record) else {
return;
};
let _ = base::fs::write_atomic(path, &bytes, base::fs::WriteOpts::FAST);
}
pub fn persist_to(path: PathBuf) {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let owner = ProcessIdentity::current();
let mut orphans = ORPHANS.lock().unwrap_or_else(|e| e.into_inner());
write(&path, &owner, &orphans.groups);
orphans.file = Some((path, owner));
}
pub fn stop_persisting(path: &Path) {
let mut orphans = ORPHANS.lock().unwrap_or_else(|e| e.into_inner());
if orphans.file.as_ref().is_some_and(|(kept, _)| kept == path) {
orphans.file = None;
let _ = std::fs::remove_file(path);
}
}
pub fn live_owner(path: &Path) -> Option<u32> {
let record = serde_json::from_slice::<Record>(&std::fs::read(path).ok()?).ok()?;
if record.owner_pid == std::process::id() {
return None;
}
let owner = ProcessIdentity {
pid: record.owner_pid,
started_at: record.owner_started_at,
};
match ProcessIdentity::check(&owner) {
ProcessLiveness::Alive | ProcessLiveness::Unknown => Some(owner.pid),
ProcessLiveness::Dead | ProcessLiveness::Reused => None,
}
}
pub fn kill_all() -> Vec<i32> {
let Ok(orphans) = ORPHANS.try_lock() else {
return Vec::new();
};
let groups: Vec<i32> = orphans.groups.iter().copied().collect();
for g in &groups {
#[cfg(unix)]
crate::process::signal_group(*g, libc::SIGKILL);
}
groups
}
pub fn reap_previous(path: &Path, grace: Duration) -> Vec<i32> {
let Ok(bytes) = std::fs::read(path) else {
return Vec::new();
};
let Ok(record) = serde_json::from_slice::<Record>(&bytes) else {
let _ = std::fs::remove_file(path);
return Vec::new();
};
let owner = ProcessIdentity {
pid: record.owner_pid,
started_at: record.owner_started_at,
};
if owner.pid == std::process::id() {
return Vec::new();
}
match ProcessIdentity::check(&owner) {
ProcessLiveness::Alive | ProcessLiveness::Unknown => return Vec::new(),
ProcessLiveness::Dead | ProcessLiveness::Reused => {}
}
let written = std::fs::metadata(path)
.and_then(|m| m.modified())
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|d| d.as_secs() as i64)
.unwrap_or_else(|| {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
});
let targets: Vec<i32> = record
.groups
.into_iter()
.filter(|g| crate::process::group_alive(*g) && !leader_is_newer(*g, written))
.collect();
#[cfg(unix)]
{
for g in &targets {
crate::process::signal_group(*g, libc::SIGTERM);
}
let deadline = std::time::Instant::now() + grace;
while std::time::Instant::now() < deadline
&& targets.iter().any(|g| crate::process::group_alive(*g))
{
std::thread::sleep(Duration::from_millis(20));
}
for g in &targets {
if crate::process::group_alive(*g) {
crate::process::signal_group(*g, libc::SIGKILL);
}
}
}
#[cfg(not(unix))]
let _ = grace;
let _ = std::fs::remove_file(path);
targets
}
fn leader_is_newer(group: i32, written: i64) -> bool {
process_start_time(group as u32).is_some_and(|started| started > written + 2)
}
#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::*;
fn spawn_group(cmd: &str) -> std::process::Child {
use std::os::unix::process::CommandExt;
std::process::Command::new("sh")
.args(["-c", cmd])
.process_group(0)
.spawn()
.unwrap()
}
#[test]
fn a_dead_owners_groups_are_stopped_and_the_record_removed() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("groups.json");
let mut child = spawn_group("sleep 30");
let group = child.id() as i32;
let record = Record {
owner_pid: i32::MAX as u32,
owner_started_at: 1,
groups: vec![group],
};
std::fs::write(&path, serde_json::to_vec(&record).unwrap()).unwrap();
assert_eq!(
reap_previous(&path, Duration::from_millis(500)),
vec![group]
);
let _ = child.wait();
assert!(!crate::process::group_alive(group));
assert!(!path.exists());
}
#[test]
fn a_live_owners_record_is_left_alone() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("groups.json");
let mut child = spawn_group("sleep 30");
let group = child.id() as i32;
let owner = std::os::unix::process::parent_id();
let record = Record {
owner_pid: owner,
owner_started_at: process_start_time(owner).unwrap_or(0),
groups: vec![group],
};
std::fs::write(&path, serde_json::to_vec(&record).unwrap()).unwrap();
assert!(reap_previous(&path, Duration::from_millis(100)).is_empty());
assert!(path.exists());
let _ = child.kill();
let _ = child.wait();
}
}