-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathzombies.rs
42 lines (37 loc) · 1.05 KB
/
zombies.rs
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
use anyhow::Result as AnyResult;
use std::{process::Command, thread, time};
pub fn spawn_reaper() {
thread::spawn(|| {
let delay = time::Duration::from_secs(1);
let my_pid = std::process::id();
loop {
reap_children(my_pid).unwrap_or_else(|err| eprintln!("{:?}", err));
thread::sleep(delay);
}
});
}
pub fn reap_children(parent_pid: u32) -> AnyResult<()> {
for pid in get_children(parent_pid)? {
let _ = nix::sys::wait::waitpid(
nix::unistd::Pid::from_raw(pid as i32),
Some(nix::sys::wait::WaitPidFlag::WNOHANG),
)?;
}
Ok(())
}
pub fn get_children(pid: u32) -> AnyResult<Vec<u32>> {
let stdout = Command::new("pgrep")
.arg("-P")
.arg(pid.to_string())
.output()?
.stdout;
Ok(String::from_utf8_lossy(&stdout)
.trim()
.to_string()
.split("\n")
.into_iter()
.map(|s| s.parse::<u32>())
.filter(|r| r.is_ok())
.map(|r| r.unwrap())
.collect::<Vec<u32>>())
}