Skip to main content

find_by_user

Function find_by_user 

Source
pub fn find_by_user(
    tree: &impl TreeStore,
    cache: &impl CacheStore,
    target_user: &str,
) -> Vec<u32>
Expand description

Find all PIDs whose user matches the given string.

use proc_tree::{DefaultTree, DefaultCache, find_by_user, TreeStore, CacheStore, PidNode, ProcInfo};

let tree = DefaultTree::new(100, 0);
let cache = DefaultCache::new(100, 0);

tree.insert_node(1, PidNode { ppid: 0, cmd: "init".into() });
cache.insert_info(1, ProcInfo { cmd: "init".into(), user: "root".into(), ppid: 0, tgid: 1, start_time_ns: 0 });
tree.insert_node(100, PidNode { ppid: 1, cmd: "bash".into() });
cache.insert_info(100, ProcInfo { cmd: "bash".into(), user: "alice".into(), ppid: 1, tgid: 100, start_time_ns: 0 });

assert_eq!(find_by_user(&tree, &cache, "root"), vec![1]);
assert_eq!(find_by_user(&tree, &cache, "alice"), vec![100]);
assert_eq!(find_by_user(&tree, &cache, "nobody"), Vec::<u32>::new());