Skip to content

Commit

Permalink
Update Process::parent at every refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 6, 2024
1 parent 772c189 commit 21d6513
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/unix/apple/macos/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ unsafe fn get_bsd_info(pid: Pid) -> Option<libc::proc_bsdinfo> {
}
}

fn get_parent(info: &libc::proc_bsdinfo) -> Option<Pid> {
match info.pbi_ppid as i32 {
0 => None,
p => Some(Pid(p)),
}
}

unsafe fn create_new_process(
pid: Pid,
now: u64,
Expand All @@ -353,10 +360,8 @@ unsafe fn create_new_process(
return Err(());
}
};
let parent = match info.pbi_ppid as i32 {
0 => None,
p => Some(Pid(p)),
};

let parent = get_parent(&info);

let start_time = info.pbi_start_tvsec;
let run_time = now.saturating_sub(start_time);
Expand Down Expand Up @@ -642,6 +647,11 @@ pub(crate) fn update_process(
// The owner of this PID changed.
return create_new_process(pid, now, refresh_kind, Some(info));
}
let parent = get_parent(&info);
// Update the parent if it changed.
if entry.parent != parent {
entry.parent = parent;
}
}

if !get_process_infos(p, refresh_kind) {
Expand Down
5 changes: 5 additions & 0 deletions src/unix/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,11 @@ pub(crate) fn _get_process_data(
let parts = parse_stat_file(&data).ok_or(())?;
let start_time_without_boot_time = compute_start_time_without_boot_time(&parts, info);

// Update the parent if it changed.
if entry.parent != parent_pid {
entry.parent = parent_pid;
}

// It's possible that a new process took this same PID when the "original one" terminated.
// If the start time differs, then it means it's not the same process anymore and that we
// need to get all its information, hence why we check it here.
Expand Down
2 changes: 1 addition & 1 deletion src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub(crate) struct ProcessInner {
root: Option<PathBuf>,
pub(crate) memory: u64,
pub(crate) virtual_memory: u64,
parent: Option<Pid>,
pub(crate) parent: Option<Pid>,
status: ProcessStatus,
handle: Option<Arc<HandleWrapper>>,
cpu_calc_values: CPUsageCalculationValues,
Expand Down
13 changes: 8 additions & 5 deletions src/windows/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ impl SystemInner {
// as above, read_unaligned is necessary
let pi = ptr::read_unaligned(pi.0);
let pid = Pid(pi.UniqueProcessId as _);
let parent = if pi.InheritedFromUniqueProcessId as usize != 0 {
Some(Pid(pi.InheritedFromUniqueProcessId as _))
} else {
None
};
if let Some(proc_) = (*process_list.0.get()).get_mut(&pid) {
let proc_ = &mut proc_.inner;
if proc_
Expand All @@ -321,6 +326,8 @@ impl SystemInner {
proc_.virtual_memory = pi.VirtualSize as _;
}
proc_.update(refresh_kind, nb_cpus, now);
// Update the parent in case it changed.
proc_.parent = parent;
return None;
}
// If the PID owner changed, we need to recompute the whole process.
Expand All @@ -334,11 +341,7 @@ impl SystemInner {
};
let mut p = ProcessInner::new_full(
pid,
if pi.InheritedFromUniqueProcessId as usize != 0 {
Some(Pid(pi.InheritedFromUniqueProcessId as _))
} else {
None
},
parent,
memory,
virtual_memory,
name,
Expand Down

0 comments on commit 21d6513

Please sign in to comment.