Skip to content

Commit

Permalink
Merge pull request #1239 from GuillaumeGomez/update-process-parent
Browse files Browse the repository at this point in the history
Update `Process::parent` at every refresh
  • Loading branch information
GuillaumeGomez committed Apr 7, 2024
2 parents 772c189 + 3fd2df9 commit 9f05196
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 207 deletions.
2 changes: 1 addition & 1 deletion src/common.rs
Expand Up @@ -1627,7 +1627,7 @@ impl UpdateKind {
///
/// When all refresh are ruled out, a [`Process`] will still retrieve the following information:
/// * Process ID ([`Pid`])
/// * Parent process ID
/// * Parent process ID (on Windows it never changes though)
/// * Process name
/// * Start time
///
Expand Down
18 changes: 14 additions & 4 deletions src/unix/apple/macos/process.rs
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 p.parent != parent {
p.parent = parent;
}
}

if !get_process_infos(p, refresh_kind) {
Expand Down
2 changes: 1 addition & 1 deletion src/unix/linux/network.rs
@@ -1,9 +1,9 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::collections::{hash_map, HashMap};
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::{fs::File, u8};

use crate::common::{IpNetwork, MacAddr};
use crate::network::refresh_networks_addresses;
Expand Down
23 changes: 15 additions & 8 deletions src/unix/linux/process.rs
Expand Up @@ -397,12 +397,15 @@ fn refresh_user_group_ids(
#[allow(clippy::too_many_arguments)]
fn update_proc_info(
p: &mut ProcessInner,
parent_pid: Option<Pid>,
refresh_kind: ProcessRefreshKind,
proc_path: &mut PathHandler,
str_parts: &[&str],
uptime: u64,
info: &SystemInfo,
) {
update_parent_pid(p, parent_pid, str_parts);

get_status(p, str_parts[ProcIndex::State as usize]);
refresh_user_group_ids(p, proc_path, refresh_kind);

Expand Down Expand Up @@ -431,6 +434,16 @@ fn update_proc_info(
}
}

fn update_parent_pid(p: &mut ProcessInner, parent_pid: Option<Pid>, str_parts: &[&str]) {
p.parent = match parent_pid {
Some(parent_pid) if parent_pid.0 != 0 => Some(parent_pid),
_ => match Pid::from_str(str_parts[ProcIndex::ParentPid as usize]) {
Ok(p) if p.0 != 0 => Some(p),
_ => None,
},
};
}

fn retrieve_all_new_process_info(
pid: Pid,
parent_pid: Option<Pid>,
Expand All @@ -444,14 +457,6 @@ fn retrieve_all_new_process_info(
let mut proc_path = PathHandler::new(path);
let name = parts.short_exe;

p.parent = match parent_pid {
Some(parent_pid) if parent_pid.0 != 0 => Some(parent_pid),
_ => match Pid::from_str(parts.str_parts[ProcIndex::ParentPid as usize]) {
Ok(p) if p.0 != 0 => Some(p),
_ => None,
},
};

p.start_time_without_boot_time = compute_start_time_without_boot_time(parts, info);
p.start_time = p
.start_time_without_boot_time
Expand All @@ -469,6 +474,7 @@ fn retrieve_all_new_process_info(

update_proc_info(
&mut p,
parent_pid,
refresh_kind,
&mut proc_path,
&parts.str_parts,
Expand Down Expand Up @@ -518,6 +524,7 @@ pub(crate) fn _get_process_data(

update_proc_info(
entry,
parent_pid,
refresh_kind,
&mut proc_path,
&parts.str_parts,
Expand Down
36 changes: 6 additions & 30 deletions src/windows/disk.rs
@@ -1,5 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::sys::utils::HandleWrapper;
use crate::{Disk, DiskKind};

use std::ffi::{c_void, OsStr, OsString};
Expand All @@ -8,11 +9,10 @@ use std::os::windows::ffi::OsStringExt;
use std::path::Path;

use windows::core::{Error, HRESULT, PCWSTR};
use windows::Win32::Foundation::{CloseHandle, HANDLE, MAX_PATH};
use windows::Win32::Foundation::MAX_PATH;
use windows::Win32::Storage::FileSystem::{
CreateFileW, FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, GetDiskFreeSpaceExW,
GetDriveTypeW, GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, FILE_ACCESS_RIGHTS,
FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, GetDiskFreeSpaceExW, GetDriveTypeW,
GetVolumeInformationW, GetVolumePathNamesForVolumeNameW,
};
use windows::Win32::System::Ioctl::{
PropertyStandardQuery, StorageDeviceSeekPenaltyProperty, DEVICE_SEEK_PENALTY_DESCRIPTOR,
Expand Down Expand Up @@ -205,31 +205,6 @@ impl DisksInner {
}
}

struct HandleWrapper(HANDLE);

impl HandleWrapper {
unsafe fn new(drive_name: &[u16], open_rights: FILE_ACCESS_RIGHTS) -> Option<Self> {
let lpfilename = PCWSTR::from_raw(drive_name.as_ptr());
let handle = CreateFileW(
lpfilename,
open_rights.0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
None,
OPEN_EXISTING,
Default::default(),
HANDLE::default(),
)
.ok()?;
Some(Self(handle))
}
}

impl Drop for HandleWrapper {
fn drop(&mut self) {
let _err = unsafe { CloseHandle(self.0) };
}
}

unsafe fn get_drive_size(mount_point: &[u16]) -> Option<(u64, u64)> {
let mut total_size = 0;
let mut available_space = 0;
Expand Down Expand Up @@ -292,7 +267,8 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {
.copied()
.chain([0])
.collect::<Vec<_>>();
let Some(handle) = HandleWrapper::new(&device_path[..], Default::default()) else {
let Some(handle) = HandleWrapper::new_from_file(&device_path[..], Default::default())
else {
return Vec::new();
};
let Some((total_space, available_space)) = get_drive_size(&mount_paths[0][..]) else {
Expand Down

0 comments on commit 9f05196

Please sign in to comment.