diff --git a/CHANGELOG.md b/CHANGELOG.md index 59e6b1a71..21befef6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [Added] `--load-config` option to specify config file [#394](https://github.com/dalance/procs/issues/394) * [Added] `--use-config` option to specify built-in config [#152](https://github.com/dalance/procs/pull/152) * [Added] `show_header` and `show_footer` config [#405](https://github.com/dalance/procs/issues/405) +* [Added] SecContext column [#260](https://github.com/dalance/procs/issues/260) * [Fixed] hang on terminals which ignore DSR request [#288](https://github.com/dalance/procs/issues/288) ## [v0.13.4](https://github.com/dalance/procs/compare/v0.13.3...v0.13.4) - 2023-01-29 diff --git a/README.md b/README.md index 989781386..3af29d3b4 100644 --- a/README.md +++ b/README.md @@ -436,6 +436,7 @@ The first `[[columns]]` is shown at left side, and the last is shown at right si | Processor | psr | Currently assigned processor | o | | | | ReadBytes | -not supported- | Read bytes from storage | o | o | o | | RtPriority | rtprio | Real-time priority | o | | | +| SecContext | label | Security context | o | | | | Separator | -not supported- | Show `\|` for column separation | o | o | o | | Session | sid | Session ID | o | o | | | ShdPnd | pending | Pending signal mask for process | o | | | diff --git a/src/columns/os_linux.rs b/src/columns/os_linux.rs index 22d7b3b66..9319a9c12 100644 --- a/src/columns/os_linux.rs +++ b/src/columns/os_linux.rs @@ -27,6 +27,7 @@ pub mod priority; pub mod processor; pub mod read_bytes; pub mod rt_priority; +pub mod sec_context; pub mod separator; pub mod session; pub mod shd_pnd; @@ -100,6 +101,7 @@ pub use self::priority::Priority; pub use self::processor::Processor; pub use self::read_bytes::ReadBytes; pub use self::rt_priority::RtPriority; +pub use self::sec_context::SecContext; pub use self::separator::Separator; pub use self::session::Session; pub use self::shd_pnd::ShdPnd; @@ -183,6 +185,7 @@ pub enum ConfigColumnKind { Processor, ReadBytes, RtPriority, + SecContext, Separator, Session, ShdPnd, @@ -273,6 +276,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::Separator => Box::new(Separator::new(separator)), ConfigColumnKind::Session => Box::new(Session::new(header)), ConfigColumnKind::ShdPnd => Box::new(ShdPnd::new(header)), @@ -397,6 +401,10 @@ pub static KIND_LIST: Lazy, + raw_contents: HashMap, + width: usize, +} + +impl SecContext { + pub fn new(header: Option) -> Self { + let header = header.unwrap_or_else(|| String::from("Context")); + let unit = String::new(); + SecContext { + fmt_contents: HashMap::new(), + raw_contents: HashMap::new(), + width: 0, + header, + unit, + } + } +} + +#[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) { + if let Ok(mut file) = proc.open_relative("attr/current") { + let mut ret = String::new(); + let _ = file.read_to_string(&mut ret); + ret + } else { + String::from("") + } + } else { + String::from("") + }; + let raw_content = fmt_content.clone(); + + self.fmt_contents.insert(proc.pid, fmt_content); + self.raw_contents.insert(proc.pid, raw_content); + } + + column_default!(String); +}