mirror of
https://github.com/uutils/procps.git
synced 2026-05-06 14:17:04 -04:00
process: Refactor and create ProcessInformation::from_pid()
This commit is contained in:
@@ -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 {
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,6 @@ 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()
|
||||
@@ -60,9 +59,8 @@ impl SelectedTarget {
|
||||
.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()
|
||||
|
||||
@@ -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")]
|
||||
@@ -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("?"));
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user