Skip to content
Open
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
42 changes: 30 additions & 12 deletions src/uu/lsns/src/lsns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct Lsns {
processes: Vec<Process>,
namespaces: Vec<Namespace>,
noheadings: bool,
persistent: bool,
}

#[uucore::main]
Expand All @@ -95,6 +96,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
processes: Vec::new(),
namespaces: Vec::new(),
noheadings: matches.get_flag("noheadings"),
persistent: matches.get_flag("persistent"),
};

read_processes(PATH_PROC, &mut lsns)?;
Expand All @@ -119,6 +121,13 @@ pub fn uu_app() -> Command {
.action(ArgAction::SetTrue)
.help("don't print headings"),
)
.arg(
Arg::new("persistent")
.short('P')
.long("persistent")
.action(ArgAction::SetTrue)
.help("namespaces without processes"),
)
}

/// Read information of all the processes from /proc
Expand Down Expand Up @@ -488,22 +497,12 @@ impl NamespaceType {
}
}

/// Display namespaces in default format using smartcols
#[cfg(target_os = "linux")]
fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {
fn get_table_with_columns() -> Result<Table, LsnsError> {
use smartcols_sys::{SCOLS_FL_RIGHT, SCOLS_FL_TRUNC};

// Initialize smartcols
smartcols::initialize();

// Create table
let mut table = Table::new()?;

// Enable or disable headings based on flag
if lsns.noheadings {
table.enable_headings(false)?;
}

// NS: width_hint=10, right-aligned
table.new_column(c"NS", 10.0, SCOLS_FL_RIGHT)?;
// TYPE: width_hint=5, left-aligned
Expand All @@ -517,6 +516,22 @@ fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {
// COMMAND: width_hint=0 (auto-size), truncate if too long
table.new_column(c"COMMAND", 0.0, SCOLS_FL_TRUNC)?;

Ok(table)
}

/// Display namespaces in default format using smartcols
#[cfg(target_os = "linux")]
fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {
// Initialize smartcols
smartcols::initialize();

let mut table = get_table_with_columns()?;

// Enable or disable headings based on flag
if lsns.noheadings {
table.enable_headings(false)?;
}

// Build username cache once before displaying
let mut username_cache = HashMap::new();

Expand All @@ -525,7 +540,9 @@ fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {

// Add each namespace as a row
for ns in &lsns.namespaces {
let mut line = table.new_line(None)?;
if lsns.persistent && ns.nprocs != 0 {
continue;
}

// Get namespace type name
let ns_type = NSNAMES[ns.ns_type as usize];
Expand Down Expand Up @@ -557,6 +574,7 @@ fn display_namespaces(lsns: &Lsns) -> Result<(), LsnsError> {
let user_str = CString::new(user)?;
let command_str = CString::new(command)?;

let mut line = table.new_line(None)?;
line.set_data(0, &ns_str)?;
line.set_data(1, &type_str)?;
line.set_data(2, &nprocs_str)?;
Expand Down
Loading