Skip to content

Commit

Permalink
Add SecContext column #260
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Mar 3, 2023
1 parent d520133 commit 5a99a2c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
Expand Down
11 changes: 11 additions & 0 deletions src/columns/os_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -183,6 +185,7 @@ pub enum ConfigColumnKind {
Processor,
ReadBytes,
RtPriority,
SecContext,
Separator,
Session,
ShdPnd,
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -397,6 +401,10 @@ pub static KIND_LIST: Lazy<HashMap<ConfigColumnKind, (&'static str, &'static str
ConfigColumnKind::RtPriority,
("RtPriority", "Real-time priority"),
),
(
ConfigColumnKind::SecContext,
("SecContext", "Security context"),
),
(
ConfigColumnKind::Separator,
("Separator", "Show | for column separation"),
Expand Down Expand Up @@ -779,6 +787,9 @@ style = "Cyan"
kind = "RtPriority"
style = "White"
[[columns]]
kind = "SecContext"
style = "White"
[[columns]]
kind = "Separator"
style = "White"
[[columns]]
Expand Down
51 changes: 51 additions & 0 deletions src/columns/sec_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::process::ProcessInfo;
use crate::{column_default, Column};
use std::cmp;
use std::collections::HashMap;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::io::Read;

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

impl SecContext {
pub fn new(header: Option<String>) -> 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);
}

0 comments on commit 5a99a2c

Please sign in to comment.