Skip to content

Commit 18d7d9c

Browse files
authored
Merge pull request #581 from dezgeg/process_info_refact
process: Refactor and create ProcessInformation::from_pid()
2 parents 804275b + 98e484e commit 18d7d9c

File tree

5 files changed

+12
-23
lines changed

5 files changed

+12
-23
lines changed

src/uu/pgrep/src/process.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,17 @@ impl ProcessInformation {
485485
})
486486
}
487487

488-
pub fn current_process_info() -> Result<ProcessInformation, io::Error> {
489-
use std::str::FromStr;
488+
pub fn from_pid(pid: usize) -> Result<Self, io::Error> {
489+
Self::try_new(PathBuf::from(format!("/proc/{}", pid)))
490+
}
490491

492+
pub fn current_process_info() -> Result<ProcessInformation, io::Error> {
491493
#[cfg(target_os = "linux")]
492494
let pid = uucore::process::getpid();
493495
#[cfg(not(target_os = "linux"))]
494496
let pid = 0; // dummy
495497

496-
ProcessInformation::try_new(PathBuf::from_str(&format!("/proc/{pid}")).unwrap())
498+
Self::from_pid(pid as usize)
497499
}
498500

499501
pub fn proc_status(&self) -> &str {
@@ -967,7 +969,7 @@ unknown /dev/tty 4 1-63 console"#;
967969
#[test]
968970
#[cfg(target_os = "linux")]
969971
fn test_cgroups() {
970-
let mut pid_entry = ProcessInformation::try_new("/proc/1".into()).unwrap();
972+
let mut pid_entry = ProcessInformation::from_pid(1).unwrap();
971973
if pid_entry.name().unwrap() == "systemd" {
972974
let cgroups = pid_entry.cgroups().unwrap();
973975
if let Some(membership) = cgroups.iter().find(|cg| cg.hierarchy_id == 0) {

src/uu/pidwait/src/wait.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,9 @@ pub(crate) fn wait(procs: &[ProcessInformation]) {
3030
}
3131
#[cfg(target_os = "linux")]
3232
fn is_running(pid: usize) -> bool {
33-
use std::{path::PathBuf, str::FromStr};
3433
use uu_pgrep::process::RunState;
3534

36-
let proc = PathBuf::from_str(&format!("/proc/{pid}")).unwrap();
37-
38-
if !proc.exists() {
39-
return false;
40-
}
41-
42-
match ProcessInformation::try_new(proc) {
35+
match ProcessInformation::from_pid(pid) {
4336
Ok(mut proc) => proc
4437
.run_state()
4538
.map(|it| it != RunState::Stopped)

src/uu/ps/src/process_selection.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ impl ProcessSelectionSettings {
116116
if let Some(ref quick_pids) = self.quick_pids {
117117
let mut selected = Vec::new();
118118
for &pid in quick_pids {
119-
if let Ok(process) =
120-
ProcessInformation::try_new(std::path::PathBuf::from(format!("/proc/{}", pid)))
121-
{
119+
if let Ok(process) = ProcessInformation::from_pid(pid) {
122120
selected.push(process);
123121
}
124122
}

src/uu/snice/src/action.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ impl SelectedTarget {
5252

5353
#[cfg(target_os = "linux")]
5454
fn from_tty(tty: &Teletype) -> Vec<u32> {
55-
use std::{path::PathBuf, str::FromStr};
5655
use uu_pgrep::process::ProcessInformation;
5756

5857
process_snapshot()
5958
.processes()
6059
.iter()
6160
.filter(|(pid, _)| {
6261
let pid = pid.as_u32();
63-
let path = PathBuf::from_str(&format!("/proc/{pid}/")).unwrap();
6462

65-
ProcessInformation::try_new(path).unwrap().tty() == *tty
63+
ProcessInformation::from_pid(pid as usize).unwrap().tty() == *tty
6664
})
6765
.map(|(pid, _)| pid.as_u32())
6866
.collect()

src/uu/snice/src/snice.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use clap::{crate_version, Arg, Command};
1010
use prettytable::{format::consts::FORMAT_CLEAN, row, Table};
1111
pub use process_matcher::clap_args;
1212
use process_matcher::*;
13+
use std::collections::HashSet;
1314
use std::io::Write;
14-
use std::{collections::HashSet, path::PathBuf, str::FromStr};
1515
use sysinfo::Pid;
1616
use uu_pgrep::process::ProcessInformation;
1717
#[cfg(target_family = "unix")]
@@ -128,7 +128,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
128128
pub fn ask_user(pid: u32) -> bool {
129129
let process = process_snapshot().process(Pid::from_u32(pid)).unwrap();
130130

131-
let tty = ProcessInformation::try_new(PathBuf::from_str(&format!("/proc/{pid}")).unwrap())
131+
let tty = ProcessInformation::from_pid(pid as usize)
132132
.map(|mut v| v.tty().to_string())
133133
.unwrap_or(String::from("?"));
134134

@@ -187,9 +187,7 @@ pub fn construct_verbose_result(
187187

188188
let process = process_snapshot().process(Pid::from_u32(pid)).unwrap();
189189

190-
let tty =
191-
ProcessInformation::try_new(PathBuf::from_str(&format!("/proc/{pid}")).unwrap())
192-
.map(|mut v| v.tty().to_string());
190+
let tty = ProcessInformation::from_pid(pid as usize).map(|mut v| v.tty().to_string());
193191

194192
let user = process
195193
.user_id()

0 commit comments

Comments
 (0)