Skip to content

Commit

Permalink
fix(utils): use revursive find for kill_child_procs()
Browse files Browse the repository at this point in the history
  • Loading branch information
bkatsevych committed Mar 29, 2024
1 parent 654c71a commit 1697bab
Showing 1 changed file with 6 additions and 32 deletions.
38 changes: 6 additions & 32 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,35 @@ pub fn kill_child_procs() {

let current_pid = process::id();
println!("Signal caught for {} ", current_pid);
let processes = system.processes();

for (&pid, proc) in processes {
if proc.parent() == Some(sysinfo::Pid::from_u32(current_pid)) {
println!("Terminating {}", pid);
proc.kill_with(Signal::Term);
println!("Waiting for {} termination", pid);
proc.wait();
println!("{} exited", pid);
println!("Killing {}", pid);
let child_pids = find_child_processes_recursive(&system, sysinfo::Pid::from_u32(current_pid));

for pid in child_pids {
if let Some(proc) = system.process(pid) {
println!("Killing child process {} ", pid);
proc.kill();
}
}

process::exit(1);
}

// pub fn children(pid: u32, ) -> Vec<u32> {
// let system: &System = System::new_all();
// let mut children: Vec<u32>= Vec::new();
// let processes = system.processes();

// for (&_p, proc) in processes {
// if proc.parent() == Some(Pid::from_u32(pid)) {
// children.push(proc);
// }
// }

// children
// }

// Function to recursively determine all child processes with `pid` as the root
pub fn find_child_processes_recursive(system: &System, pid: Pid) -> Vec<Pid> {
// let mut system = System::new_all();
let mut child_pids = Vec::new();

// Recursively find child processes
fn find_children(system: &System, pid: Pid, child_pids: &mut Vec<Pid>) {
// Iterate through all processes
for (process_id, process) in system.processes() {
// Check if the process has `pid` as its parent
if let Some(parent_pid) = process.parent() {
if parent_pid == pid {
// If so, add it to the list of child processes
let child_pid = *process_id;
child_pids.push(child_pid);
// Recursively find children of the current process
find_children(system, child_pid, child_pids);
}
}
}
}

// Start the recursive search from the root process
find_children(system, pid, &mut child_pids);

child_pids
}
}

0 comments on commit 1697bab

Please sign in to comment.