Skip to main content

is_descendant

Function is_descendant 

Source
pub fn is_descendant(tree: &impl TreeStore, pid: u32, target_cmd: &str) -> bool
Expand description

Check if pid is a descendant of any process whose cmd == target_cmd.

use proc_tree::{DefaultTree, is_descendant, TreeStore, PidNode};

let tree = DefaultTree::new(100, 0);
tree.insert_node(1, PidNode { ppid: 0, cmd: "init".into() });
tree.insert_node(100, PidNode { ppid: 1, cmd: "sshd".into() });
tree.insert_node(200, PidNode { ppid: 100, cmd: "bash".into() });

assert!(is_descendant(&tree, 200, "sshd"));
assert!(is_descendant(&tree, 200, "init"));
assert!(!is_descendant(&tree, 200, "nginx"));
assert!(!is_descendant(&tree, 1, "sshd")); // init is not a descendant of sshd