Skip to content
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
10 changes: 6 additions & 4 deletions src/uu/pgrep/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,17 @@ impl ProcessInformation {
})
}

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

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

ProcessInformation::try_new(PathBuf::from_str(&format!("/proc/{pid}")).unwrap())
Self::from_pid(pid as usize)
}

pub fn proc_status(&self) -> &str {
Expand Down Expand Up @@ -967,7 +969,7 @@ unknown /dev/tty 4 1-63 console"#;
#[test]
#[cfg(target_os = "linux")]
fn test_cgroups() {
let mut pid_entry = ProcessInformation::try_new("/proc/1".into()).unwrap();
let mut pid_entry = ProcessInformation::from_pid(1).unwrap();
if pid_entry.name().unwrap() == "systemd" {
let cgroups = pid_entry.cgroups().unwrap();
if let Some(membership) = cgroups.iter().find(|cg| cg.hierarchy_id == 0) {
Expand Down
9 changes: 1 addition & 8 deletions src/uu/pidwait/src/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,9 @@ pub(crate) fn wait(procs: &[ProcessInformation]) {
}
#[cfg(target_os = "linux")]
fn is_running(pid: usize) -> bool {
use std::{path::PathBuf, str::FromStr};
use uu_pgrep::process::RunState;

let proc = PathBuf::from_str(&format!("/proc/{pid}")).unwrap();

if !proc.exists() {
return false;
}

match ProcessInformation::try_new(proc) {
match ProcessInformation::from_pid(pid) {
Ok(mut proc) => proc
.run_state()
.map(|it| it != RunState::Stopped)
Expand Down
4 changes: 1 addition & 3 deletions src/uu/ps/src/process_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ impl ProcessSelectionSettings {
if let Some(ref quick_pids) = self.quick_pids {
let mut selected = Vec::new();
for &pid in quick_pids {
if let Ok(process) =
ProcessInformation::try_new(std::path::PathBuf::from(format!("/proc/{}", pid)))
{
if let Ok(process) = ProcessInformation::from_pid(pid) {
selected.push(process);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/uu/snice/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ impl SelectedTarget {

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

process_snapshot()
.processes()
.iter()
.filter(|(pid, _)| {
let pid = pid.as_u32();
let path = PathBuf::from_str(&format!("/proc/{pid}/")).unwrap();

ProcessInformation::try_new(path).unwrap().tty() == *tty
ProcessInformation::from_pid(pid as usize).unwrap().tty() == *tty
})
.map(|(pid, _)| pid.as_u32())
.collect()
Expand Down
8 changes: 3 additions & 5 deletions src/uu/snice/src/snice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use clap::{crate_version, Arg, Command};
use prettytable::{format::consts::FORMAT_CLEAN, row, Table};
pub use process_matcher::clap_args;
use process_matcher::*;
use std::collections::HashSet;
use std::io::Write;
use std::{collections::HashSet, path::PathBuf, str::FromStr};
use sysinfo::Pid;
use uu_pgrep::process::ProcessInformation;
#[cfg(target_family = "unix")]
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
pub fn ask_user(pid: u32) -> bool {
let process = process_snapshot().process(Pid::from_u32(pid)).unwrap();

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

Expand Down Expand Up @@ -187,9 +187,7 @@ pub fn construct_verbose_result(

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

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

let user = process
.user_id()
Expand Down
Loading