Skip to content

Commit

Permalink
Merge pull request #699 from dalance/procfs_option
Browse files Browse the repository at this point in the history
Add procfs path option
  • Loading branch information
dalance authored Oct 21, 2024
2 parents ad5effd + e779cca commit 57efa0d
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/columns/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ use crate::process::ProcessInfo;
use crate::{column_default, Column};
use std::cmp;
use std::collections::HashMap;
use std::path::PathBuf;

pub struct Env {
header: String,
unit: String,
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, String>,
width: usize,
procfs: Option<PathBuf>,
}

impl Env {
pub fn new(header: Option<String>) -> Self {
pub fn new(header: Option<String>, procfs: Option<PathBuf>) -> Self {
let header = header.unwrap_or_else(|| String::from("Env"));
let unit = String::new();
Self {
Expand All @@ -21,6 +23,7 @@ impl Env {
width: 0,
header,
unit,
procfs,
}
}
}
Expand All @@ -29,7 +32,7 @@ impl Env {
impl Column for Env {
fn add(&mut self, proc: &ProcessInfo) {
let mut fmt_content = String::new();
if let Ok(proc) = procfs::process::Process::new(proc.pid) {
if let Ok(proc) = crate::util::process_new(proc.pid, &self.procfs) {
if let Ok(envs) = proc.environ() {
for (k, v) in envs {
fmt_content.push_str(&format!(
Expand Down
4 changes: 3 additions & 1 deletion src/columns/os_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ use crate::column::Column;
use once_cell::sync::Lazy;
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;

// ---------------------------------------------------------------------------------------------------------------------
// ConfigColumnKind
Expand Down Expand Up @@ -180,14 +181,15 @@ pub fn gen_column(
separator: &str,
abbr_sid: bool,
tree_symbols: &[String; 5],
procfs: Option<PathBuf>,
) -> Box<dyn Column> {
match kind {
ConfigColumnKind::Command => Box::new(Command::new(header)),
ConfigColumnKind::ContextSw => Box::new(ContextSw::new(header)),
ConfigColumnKind::CpuTime => Box::new(CpuTime::new(header)),
ConfigColumnKind::ElapsedTime => Box::new(ElapsedTime::new(header)),
ConfigColumnKind::Empty => Box::new(Empty::new()),
ConfigColumnKind::Env => Box::new(Env::new(header)),
ConfigColumnKind::Env => Box::new(Env::new(header, procfs)),
ConfigColumnKind::FileName => Box::new(FileName::new(header)),
ConfigColumnKind::Gid => Box::new(Gid::new(header, abbr_sid)),
ConfigColumnKind::GidReal => Box::new(GidReal::new(header)),
Expand Down
8 changes: 5 additions & 3 deletions src/columns/os_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ use crate::column::Column;
use once_cell::sync::Lazy;
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;

// ---------------------------------------------------------------------------------------------------------------------
// ConfigColumnKind
Expand Down Expand Up @@ -258,6 +259,7 @@ pub fn gen_column(
separator: &str,
abbr_sid: bool,
tree_symbols: &[String; 5],
procfs: Option<PathBuf>,
) -> Box<dyn Column> {
match kind {
ConfigColumnKind::Ccgroup => Box::new(Ccgroup::new(header)),
Expand All @@ -272,7 +274,7 @@ pub fn gen_column(
ConfigColumnKind::Eip => Box::new(Eip::new(header)),
ConfigColumnKind::ElapsedTime => Box::new(ElapsedTime::new(header)),
ConfigColumnKind::Empty => Box::new(Empty::new()),
ConfigColumnKind::Env => Box::new(Env::new(header)),
ConfigColumnKind::Env => Box::new(Env::new(header, procfs)),
ConfigColumnKind::Esp => Box::new(Esp::new(header)),
ConfigColumnKind::FileName => Box::new(FileName::new(header)),
ConfigColumnKind::Gid => Box::new(Gid::new(header, abbr_sid)),
Expand All @@ -295,7 +297,7 @@ pub fn gen_column(
ConfigColumnKind::Processor => Box::new(Processor::new(header)),
ConfigColumnKind::ReadBytes => Box::new(ReadBytes::new(header)),
ConfigColumnKind::RtPriority => Box::new(RtPriority::new(header)),
ConfigColumnKind::SecContext => Box::new(SecContext::new(header)),
ConfigColumnKind::SecContext => Box::new(SecContext::new(header, procfs)),
ConfigColumnKind::Separator => Box::new(Separator::new(separator)),
ConfigColumnKind::Session => Box::new(Session::new(header)),
ConfigColumnKind::ShdPnd => Box::new(ShdPnd::new(header)),
Expand Down Expand Up @@ -339,7 +341,7 @@ pub fn gen_column(
ConfigColumnKind::VmStack => Box::new(VmStack::new(header)),
ConfigColumnKind::VmSwap => Box::new(VmSwap::new(header)),
ConfigColumnKind::Wchan => Box::new(Wchan::new(header)),
ConfigColumnKind::WorkDir => Box::new(WorkDir::new(header)),
ConfigColumnKind::WorkDir => Box::new(WorkDir::new(header, procfs)),
ConfigColumnKind::WriteBytes => Box::new(WriteBytes::new(header)),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/columns/os_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ use crate::column::Column;
use once_cell::sync::Lazy;
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;

// ---------------------------------------------------------------------------------------------------------------------
// ConfigColumnKind
Expand Down Expand Up @@ -159,6 +160,7 @@ pub fn gen_column(
separator: &str,
abbr_sid: bool,
tree_symbols: &[String; 5],
_procfs: Option<PathBuf>,
) -> Box<dyn Column> {
match kind {
ConfigColumnKind::Command => Box::new(Command::new(header)),
Expand Down
2 changes: 2 additions & 0 deletions src/columns/os_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use crate::column::Column;
use once_cell::sync::Lazy;
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;

// ---------------------------------------------------------------------------------------------------------------------
// ConfigColumnKind
Expand Down Expand Up @@ -114,6 +115,7 @@ pub fn gen_column(
separator: &str,
abbr_sid: bool,
tree_symbols: &[String; 5],
_procfs: Option<PathBuf>,
) -> Box<dyn Column> {
match kind {
ConfigColumnKind::Command => Box::new(Command::new(header)),
Expand Down
7 changes: 5 additions & 2 deletions src/columns/sec_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ use std::cmp;
use std::collections::HashMap;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::io::Read;
use std::path::PathBuf;

pub struct SecContext {
header: String,
unit: String,
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, String>,
width: usize,
procfs: Option<PathBuf>,
}

impl SecContext {
pub fn new(header: Option<String>) -> Self {
pub fn new(header: Option<String>, procfs: Option<PathBuf>) -> Self {
let header = header.unwrap_or_else(|| String::from("Context"));
let unit = String::new();
Self {
Expand All @@ -23,14 +25,15 @@ impl SecContext {
width: 0,
header,
unit,
procfs,
}
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
impl Column for SecContext {
fn add(&mut self, proc: &ProcessInfo) {
let fmt_content = if let Ok(proc) = procfs::process::Process::new(proc.pid) {
let fmt_content = if let Ok(proc) = crate::util::process_new(proc.pid, &self.procfs) {
if let Ok(mut file) = proc.open_relative("attr/current") {
let mut ret = String::new();
let _ = file.read_to_string(&mut ret);
Expand Down
7 changes: 5 additions & 2 deletions src/columns/work_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ use crate::process::ProcessInfo;
use crate::{column_default, Column};
use std::cmp;
use std::collections::HashMap;
use std::path::PathBuf;

pub struct WorkDir {
header: String,
unit: String,
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, String>,
width: usize,
procfs: Option<PathBuf>,
}

impl WorkDir {
pub fn new(header: Option<String>) -> Self {
pub fn new(header: Option<String>, procfs: Option<PathBuf>) -> Self {
let header = header.unwrap_or_else(|| String::from("WorkDir"));
let unit = String::new();
Self {
Expand All @@ -21,14 +23,15 @@ impl WorkDir {
width: 0,
header,
unit,
procfs,
}
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
impl Column for WorkDir {
fn add(&mut self, proc: &ProcessInfo) {
let fmt_content = if let Ok(proc) = procfs::process::Process::new(proc.pid) {
let fmt_content = if let Ok(proc) = crate::util::process_new(proc.pid, &self.procfs) {
if let Ok(path) = proc.cwd() {
path.to_string_lossy().to_string()
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ pub struct Opt {
#[clap(long = "no-header")]
pub no_header: bool,

/// Path to procfs
#[clap(long = "procfs")]
pub procfs: Option<PathBuf>,

/// Show debug message
#[clap(long = "debug", hide = true)]
pub debug: bool,
Expand Down
1 change: 1 addition & 0 deletions src/process/freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn collect_proc(
interval: Duration,
_with_thread: bool,
_show_kthreads: bool,
_procfs_path: &Option<PathBuf>,
) -> Vec<ProcessInfo> {
let mut base_procs = HashMap::new();
let mut ret = Vec::new();
Expand Down
12 changes: 10 additions & 2 deletions src/process/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use procfs::process::{FDInfo, Io, Process, Stat, Status, TasksIter};
use procfs::ProcError;
use procfs::ProcessCGroup;
use std::collections::HashMap;
use std::path::PathBuf;
use std::thread;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -83,12 +84,19 @@ pub fn collect_proc(
interval: Duration,
with_thread: bool,
show_kthreads: bool,
procfs_path: &Option<PathBuf>,
) -> Vec<ProcessInfo> {
let mut base_procs = Vec::new();
let mut base_tasks = HashMap::new();
let mut ret = Vec::new();

if let Ok(all_proc) = procfs::process::all_processes() {
let all_proc = if let Some(ref x) = procfs_path {
procfs::process::all_processes_with_root(x)
} else {
procfs::process::all_processes()
};

if let Ok(all_proc) = all_proc {
for proc in all_proc.flatten() {
if let Ok(stat) = proc.stat() {
let io = proc.io().ok();
Expand All @@ -106,7 +114,7 @@ pub fn collect_proc(
thread::sleep(interval);

for (pid, prev_stat, prev_io, prev_time) in base_procs {
let curr_proc = if let Ok(proc) = Process::new(pid) {
let curr_proc = if let Ok(proc) = crate::util::process_new(pid, procfs_path) {
proc
} else {
continue;
Expand Down
1 change: 1 addition & 0 deletions src/process/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn collect_proc(
interval: Duration,
_with_thread: bool,
_show_kthreads: bool,
_procfs_path: &Option<PathBuf>,
) -> Vec<ProcessInfo> {
let mut base_procs = Vec::new();
let mut ret = Vec::new();
Expand Down
2 changes: 2 additions & 0 deletions src/process/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;
use std::mem::{size_of, zeroed, MaybeUninit};
use std::path::PathBuf;
use std::ptr;
use std::thread;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -74,6 +75,7 @@ pub fn collect_proc(
interval: Duration,
_with_thread: bool,
_show_kthreads: bool,
_procfs_path: &Option<PathBuf>,
) -> Vec<ProcessInfo> {
let mut base_procs = Vec::new();
let mut ret = Vec::new();
Expand Down
13 changes: 13 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,16 @@ pub fn ptr_to_cstr(
let x = unsafe { std::slice::from_raw_parts::<u8>(ptr, len) };
std::ffi::CStr::from_bytes_until_nul(x)
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn process_new(
pid: i32,
procfs: &Option<std::path::PathBuf>,
) -> procfs::ProcResult<procfs::process::Process> {
if let Some(ref x) = procfs {
let path = x.join(pid.to_string());
procfs::process::Process::new_with_root(path)
} else {
procfs::process::Process::new(pid)
}
}
2 changes: 2 additions & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl View {
&config.display.separator,
config.display.abbr_sid,
&config.display.tree_symbols,
opt.procfs.clone(),
);
if column.available() {
columns.push(ColumnInfo {
Expand Down Expand Up @@ -172,6 +173,7 @@ impl View {
Duration::from_millis(opt.interval),
show_thread,
config.display.show_kthreads,
&opt.procfs,
);
for c in columns.iter_mut() {
for p in &proc {
Expand Down

0 comments on commit 57efa0d

Please sign in to comment.