Skip to content

Commit

Permalink
Improve Process root information retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 14, 2023
1 parent 39f31ef commit 1df3437
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
74 changes: 42 additions & 32 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,6 @@ impl ProcessInner {
written_bytes: 0,
};
get_process_params(&mut p, refresh_kind);
// Should always be called after we refreshed `cwd`.
update_root(&mut p, refresh_kind);
Some(p)
}
}
Expand Down Expand Up @@ -444,8 +442,6 @@ impl ProcessInner {
};

get_process_params(&mut p, refresh_kind);
// Should always be called after we refreshed `cwd`.
update_root(&mut p, refresh_kind);
p
}
} else {
Expand All @@ -454,7 +450,7 @@ impl ProcessInner {
} else {
PathBuf::new()
};
let mut p = Self {
Self {
handle: None,
name,
pid,
Expand All @@ -477,10 +473,7 @@ impl ProcessInner {
old_written_bytes: 0,
read_bytes: 0,
written_bytes: 0,
};
// Should always be called after we refreshed `cwd`.
update_root(&mut p, refresh_kind);
p
}
}
}

Expand All @@ -504,15 +497,12 @@ impl ProcessInner {
}
if refresh_kind.exe() {
unsafe {
let exe = match self.handle.as_ref() {
self.exe = match self.handle.as_ref() {
Some(handle) => get_exe(handle),
None => get_executable_path(self.pid),
};
self.exe = exe;
}
}
// Should always be called after we refreshed `cwd`.
update_root(self, refresh_kind);
self.run_time = now.saturating_sub(self.start_time());
self.updated = true;
}
Expand Down Expand Up @@ -663,18 +653,19 @@ unsafe fn get_process_times(handle: HANDLE) -> u64 {
}

// On Windows, the root folder is always the current drive. So we get it from its `cwd`.
fn update_root(process: &mut ProcessInner, refresh_kind: ProcessRefreshKind) {
if refresh_kind.root() && process.cwd.parent().is_some() {
if !process.cwd.has_root() {
process.root = PathBuf::new();
return;
}
let mut ancestors = process.cwd.ancestors().peekable();
while let Some(path) = ancestors.next() {
if ancestors.peek().is_none() {
process.root = path.into();
break;
}
fn update_root(refresh_kind: ProcessRefreshKind, cwd: &Path, root: &mut PathBuf) {
if !refresh_kind.root() {
return;
}
if cwd.as_os_str().is_empty() || !cwd.has_root() {
*root = PathBuf::new();
return;
}
let mut ancestors = cwd.ancestors().peekable();
while let Some(path) = ancestors.next() {
if ancestors.peek().is_none() {
*root = path.into();
break;
}
}
}
Expand Down Expand Up @@ -851,7 +842,8 @@ unsafe fn get_process_params(process: &mut ProcessInner, refresh_kind: ProcessRe
sysinfo_debug!("Non 64 bit targets are not supported");
return;
}
if !refresh_kind.cmd() && !refresh_kind.environ() && !refresh_kind.cwd() {
if !(refresh_kind.cmd() || refresh_kind.environ() || refresh_kind.cwd() || refresh_kind.root())
{
return;
}
let handle = match process.handle.as_ref().map(|handle| handle.0) {
Expand Down Expand Up @@ -926,7 +918,13 @@ unsafe fn get_process_params(process: &mut ProcessInner, refresh_kind: ProcessRe
let proc_params = proc_params.assume_init();
get_cmd_line(&proc_params, handle, refresh_kind, &mut process.cmd);
get_proc_env(&proc_params, handle, refresh_kind, &mut process.environ);
get_cwd(&proc_params, handle, refresh_kind, &mut process.cwd);
get_cwd_and_root(
&proc_params,
handle,
refresh_kind,
&mut process.cwd,
&mut process.root,
);
}
// target is a 32 bit process in wow64 mode

Expand Down Expand Up @@ -961,24 +959,36 @@ unsafe fn get_process_params(process: &mut ProcessInner, refresh_kind: ProcessRe
let proc_params = proc_params.assume_init();
get_cmd_line(&proc_params, handle, refresh_kind, &mut process.cmd);
get_proc_env(&proc_params, handle, refresh_kind, &mut process.environ);
get_cwd(&proc_params, handle, refresh_kind, &mut process.cwd);
get_cwd_and_root(
&proc_params,
handle,
refresh_kind,
&mut process.cwd,
&mut process.root,
);
}

fn get_cwd<T: RtlUserProcessParameters>(
fn get_cwd_and_root<T: RtlUserProcessParameters>(
params: &T,
handle: HANDLE,
refresh_kind: ProcessRefreshKind,
cwd: &mut PathBuf,
root: &mut PathBuf,
) {
if !refresh_kind.cwd() {
if !refresh_kind.cwd() && !refresh_kind.root() {
return;
}
match params.get_cwd(handle) {
Ok(buffer) => unsafe {
*cwd = PathBuf::from(null_terminated_wchar_to_string(buffer.as_slice()));
let tmp_cwd = PathBuf::from(null_terminated_wchar_to_string(buffer.as_slice()));
// Should always be called after we refreshed `cwd`.
update_root(refresh_kind, &tmp_cwd, root);
if refresh_kind.cwd() {
*cwd = tmp_cwd;
}
},
Err(_e) => {
sysinfo_debug!("get_cwd failed to get data: {}", _e);
sysinfo_debug!("get_cwd_and_root failed to get data: {:?}", _e);
*cwd = PathBuf::new();
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,13 @@ fn test_process_specific_refresh() {
update_specific_and_check!(memory);
update_specific_and_check!(environ, with_environ, .len(), 0);
update_specific_and_check!(cmd, with_cmd, .len(), 0);
update_specific_and_check!(exe, with_exe, , Path::new(""));
update_specific_and_check!(cwd, with_cwd, , Path::new(""));
if !cfg!(any(
target_os = "macos",
target_os = "ios",
feature = "apple-sandbox",
)) {
update_specific_and_check!(root, with_root, , Path::new(""));
}
update_specific_and_check!(exe, with_exe, , Path::new(""));
update_specific_and_check!(cwd, with_cwd, , Path::new(""));
}

0 comments on commit 1df3437

Please sign in to comment.