Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Process::exe, Process::cwd and Process::root return an Option<&Path> #1162

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ pub extern "C" fn sysinfo_process_executable_path(process: CProcess) -> RString
assert!(!process.is_null());
let process = process as *const Process;
unsafe {
if let Some(p) = (*process).exe().to_str() {
if let Some(p) = (*process).exe().and_then(|exe| exe.to_str()) {
if let Ok(c) = CString::new(p) {
return c.into_raw() as _;
}
Expand All @@ -477,7 +477,7 @@ pub extern "C" fn sysinfo_process_root_directory(process: CProcess) -> RString {
assert!(!process.is_null());
let process = process as *const Process;
unsafe {
if let Some(p) = (*process).root().to_str() {
if let Some(p) = (*process).root().and_then(|root| root.to_str()) {
if let Ok(c) = CString::new(p) {
return c.into_raw() as _;
}
Expand All @@ -492,7 +492,7 @@ pub extern "C" fn sysinfo_process_current_directory(process: CProcess) -> RStrin
assert!(!process.is_null());
let process = process as *const Process;
unsafe {
if let Some(p) = (*process).cwd().to_str() {
if let Some(p) = (*process).cwd().and_then(|cwd| cwd.to_str()) {
if let Ok(c) = CString::new(p) {
return c.into_raw() as _;
}
Expand Down
12 changes: 6 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ impl Process {
///
/// let s = System::new_all();
/// if let Some(process) = s.process(Pid::from(1337)) {
/// println!("{}", process.exe().display());
/// println!("{:?}", process.exe());
/// }
/// ```
///
Expand All @@ -870,7 +870,7 @@ impl Process {
/// replacement for this.
/// A process [may change its `cmd[0]` value](https://man7.org/linux/man-pages/man5/proc.5.html)
/// freely, making this an untrustworthy source of information.
pub fn exe(&self) -> &Path {
pub fn exe(&self) -> Option<&Path> {
self.inner.exe()
}

Expand Down Expand Up @@ -909,10 +909,10 @@ impl Process {
///
/// let s = System::new_all();
/// if let Some(process) = s.process(Pid::from(1337)) {
/// println!("{}", process.cwd().display());
/// println!("{:?}", process.cwd());
/// }
/// ```
pub fn cwd(&self) -> &Path {
pub fn cwd(&self) -> Option<&Path> {
self.inner.cwd()
}

Expand All @@ -923,10 +923,10 @@ impl Process {
///
/// let s = System::new_all();
/// if let Some(process) = s.process(Pid::from(1337)) {
/// println!("{}", process.root().display());
/// println!("{:?}", process.root());
/// }
/// ```
pub fn root(&self) -> &Path {
pub fn root(&self) -> Option<&Path> {
self.inner.root()
}

Expand Down
12 changes: 6 additions & 6 deletions src/unix/apple/app_store/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl ProcessInner {
&[]
}

pub(crate) fn exe(&self) -> &Path {
Path::new("/")
pub(crate) fn exe(&self) -> Option<&Path> {
None
}

pub(crate) fn pid(&self) -> Pid {
Expand All @@ -31,12 +31,12 @@ impl ProcessInner {
&[]
}

pub(crate) fn cwd(&self) -> &Path {
Path::new("/")
pub(crate) fn cwd(&self) -> Option<&Path> {
None
}

pub(crate) fn root(&self) -> &Path {
Path::new("/")
pub(crate) fn root(&self) -> Option<&Path> {
None
}

pub(crate) fn memory(&self) -> u64 {
Expand Down
59 changes: 23 additions & 36 deletions src/unix/apple/macos/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use crate::unix::utils::cstr_to_rust_with_size;
pub(crate) struct ProcessInner {
pub(crate) name: String,
pub(crate) cmd: Vec<String>,
pub(crate) exe: PathBuf,
pub(crate) exe: Option<PathBuf>,
pid: Pid,
parent: Option<Pid>,
pub(crate) environ: Vec<String>,
cwd: PathBuf,
pub(crate) root: PathBuf,
cwd: Option<PathBuf>,
pub(crate) root: Option<PathBuf>,
pub(crate) memory: u64,
pub(crate) virtual_memory: u64,
old_utime: u64,
Expand Down Expand Up @@ -52,9 +52,9 @@ impl ProcessInner {
parent: None,
cmd: Vec::new(),
environ: Vec::new(),
exe: PathBuf::new(),
cwd: PathBuf::new(),
root: PathBuf::new(),
exe: None,
cwd: None,
root: None,
memory: 0,
virtual_memory: 0,
cpu_usage: 0.,
Expand Down Expand Up @@ -83,9 +83,9 @@ impl ProcessInner {
parent,
cmd: Vec::new(),
environ: Vec::new(),
exe: PathBuf::new(),
cwd: PathBuf::new(),
root: PathBuf::new(),
exe: None,
cwd: None,
root: None,
memory: 0,
virtual_memory: 0,
cpu_usage: 0.,
Expand Down Expand Up @@ -120,8 +120,8 @@ impl ProcessInner {
&self.cmd
}

pub(crate) fn exe(&self) -> &Path {
self.exe.as_path()
pub(crate) fn exe(&self) -> Option<&Path> {
self.exe.as_deref()
}

pub(crate) fn pid(&self) -> Pid {
Expand All @@ -132,12 +132,12 @@ impl ProcessInner {
&self.environ
}

pub(crate) fn cwd(&self) -> &Path {
self.cwd.as_path()
pub(crate) fn cwd(&self) -> Option<&Path> {
self.cwd.as_deref()
}

pub(crate) fn root(&self) -> &Path {
self.root.as_path()
pub(crate) fn root(&self) -> Option<&Path> {
self.root.as_deref()
}

pub(crate) fn memory(&self) -> u64 {
Expand Down Expand Up @@ -388,9 +388,7 @@ unsafe fn get_exe_and_name_backup(
process: &mut ProcessInner,
refresh_kind: ProcessRefreshKind,
) -> bool {
let exe_needs_update = refresh_kind
.exe()
.needs_update(|| process.exe.as_os_str().is_empty());
let exe_needs_update = refresh_kind.exe().needs_update(|| process.exe.is_none());
if !process.name.is_empty() && !exe_needs_update {
return false;
}
Expand All @@ -412,7 +410,7 @@ unsafe fn get_exe_and_name_backup(
.to_owned();
}
if exe_needs_update {
process.exe = exe;
process.exe = Some(exe);
}
true
}
Expand All @@ -432,12 +430,8 @@ unsafe fn convert_node_path_info(node: &libc::vnode_info_path) -> Option<PathBuf
}

unsafe fn get_cwd_root(process: &mut ProcessInner, refresh_kind: ProcessRefreshKind) {
let cwd_needs_update = refresh_kind
.cwd()
.needs_update(|| process.cwd.as_os_str().is_empty());
let root_needs_update = refresh_kind
.root()
.needs_update(|| process.root.as_os_str().is_empty());
let cwd_needs_update = refresh_kind.cwd().needs_update(|| process.cwd.is_none());
let root_needs_update = refresh_kind.root().needs_update(|| process.root.is_none());
if !cwd_needs_update && !root_needs_update {
return;
}
Expand All @@ -454,14 +448,10 @@ unsafe fn get_cwd_root(process: &mut ProcessInner, refresh_kind: ProcessRefreshK
return;
}
if cwd_needs_update {
if let Some(cwd) = convert_node_path_info(&vnodepathinfo.pvi_cdir) {
process.cwd = cwd;
}
process.cwd = convert_node_path_info(&vnodepathinfo.pvi_cdir);
}
if root_needs_update {
if let Some(root) = convert_node_path_info(&vnodepathinfo.pvi_rdir) {
process.root = root;
}
process.root = convert_node_path_info(&vnodepathinfo.pvi_rdir);
}
}

Expand Down Expand Up @@ -557,11 +547,8 @@ unsafe fn get_process_infos(process: &mut ProcessInner, refresh_kind: ProcessRef
.to_owned();
}

if refresh_kind
.exe()
.needs_update(|| process.exe.as_os_str().is_empty())
{
process.exe = exe;
if refresh_kind.exe().needs_update(|| process.exe.is_none()) {
process.exe = Some(exe);
}

let environ_needs_update = refresh_kind
Expand Down
41 changes: 19 additions & 22 deletions src/unix/freebsd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ impl fmt::Display for ProcessStatus {
pub(crate) struct ProcessInner {
pub(crate) name: String,
pub(crate) cmd: Vec<String>,
pub(crate) exe: PathBuf,
pub(crate) exe: Option<PathBuf>,
pub(crate) pid: Pid,
parent: Option<Pid>,
pub(crate) environ: Vec<String>,
pub(crate) cwd: PathBuf,
pub(crate) root: PathBuf,
pub(crate) cwd: Option<PathBuf>,
pub(crate) root: Option<PathBuf>,
pub(crate) memory: u64,
pub(crate) virtual_memory: u64,
pub(crate) updated: bool,
Expand Down Expand Up @@ -80,8 +80,8 @@ impl ProcessInner {
&self.cmd
}

pub(crate) fn exe(&self) -> &Path {
self.exe.as_path()
pub(crate) fn exe(&self) -> Option<&Path> {
self.exe.as_deref()
}

pub(crate) fn pid(&self) -> Pid {
Expand All @@ -92,12 +92,12 @@ impl ProcessInner {
&self.environ
}

pub(crate) fn cwd(&self) -> &Path {
self.cwd.as_path()
pub(crate) fn cwd(&self) -> Option<&Path> {
self.cwd.as_deref()
}

pub(crate) fn root(&self) -> &Path {
self.root.as_path()
pub(crate) fn root(&self) -> Option<&Path> {
self.root.as_deref()
}

pub(crate) fn memory(&self) -> u64 {
Expand Down Expand Up @@ -277,14 +277,14 @@ pub(crate) unsafe fn get_process_data(
virtual_memory,
memory,
// procstat_getfiles
cwd: PathBuf::new(),
exe: PathBuf::new(),
cwd: None,
exe: None,
// kvm_getargv isn't thread-safe so we get it in the main thread.
name: String::new(),
// kvm_getargv isn't thread-safe so we get it in the main thread.
cmd: Vec::new(),
// kvm_getargv isn't thread-safe so we get it in the main thread.
root: PathBuf::new(),
root: None,
// kvm_getenvv isn't thread-safe so we get it in the main thread.
environ: Vec::new(),
status,
Expand All @@ -297,11 +297,12 @@ pub(crate) unsafe fn get_process_data(
}))
}

pub(crate) unsafe fn get_exe(exe: &mut PathBuf, pid: crate::Pid, refresh_kind: ProcessRefreshKind) {
if refresh_kind
.exe()
.needs_update(|| exe.as_os_str().is_empty())
{
pub(crate) unsafe fn get_exe(
exe: &mut Option<PathBuf>,
pid: crate::Pid,
refresh_kind: ProcessRefreshKind,
) {
if refresh_kind.exe().needs_update(|| exe.is_none()) {
let mut buffer = [0; libc::PATH_MAX as usize + 1];

*exe = get_sys_value_str(
Expand All @@ -313,10 +314,6 @@ pub(crate) unsafe fn get_exe(exe: &mut PathBuf, pid: crate::Pid, refresh_kind: P
],
&mut buffer,
)
.map(PathBuf::from)
.unwrap_or_else(|| {
sysinfo_debug!("Failed to get `exe` for {}", pid.0);
PathBuf::new()
});
.map(PathBuf::from);
}
}
12 changes: 4 additions & 8 deletions src/unix/freebsd/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,8 @@ impl SystemInfo {
refresh_kind: ProcessRefreshKind,
) {
let mut done = 0;
let cwd_needs_update = refresh_kind
.cwd()
.needs_update(|| proc_.cwd().as_os_str().is_empty());
let root_needs_update = refresh_kind
.root()
.needs_update(|| proc_.root().as_os_str().is_empty());
let cwd_needs_update = refresh_kind.cwd().needs_update(|| proc_.cwd().is_none());
let root_needs_update = refresh_kind.root().needs_update(|| proc_.root().is_none());
if cwd_needs_update {
done += 1;
}
Expand Down Expand Up @@ -632,14 +628,14 @@ impl SystemInfo {
if tmp.fs_uflags & libc::PS_FST_UFLAG_CDIR != 0 {
if cwd_needs_update && !tmp.fs_path.is_null() {
if let Ok(p) = CStr::from_ptr(tmp.fs_path).to_str() {
proc_.cwd = PathBuf::from(p);
proc_.cwd = Some(PathBuf::from(p));
done -= 1;
}
}
} else if tmp.fs_uflags & libc::PS_FST_UFLAG_RDIR != 0 {
if root_needs_update && !tmp.fs_path.is_null() {
if let Ok(p) = CStr::from_ptr(tmp.fs_path).to_str() {
proc_.root = PathBuf::from(p);
proc_.root = Some(PathBuf::from(p));
done -= 1;
}
}
Expand Down
Loading
Loading