Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ feat_common_core = [
"lsipc",
"lslocks",
"lsmem",
"lsns",
"mcookie",
"mesg",
"mountpoint",
Expand Down Expand Up @@ -106,6 +107,7 @@ lscpu = { optional = true, version = "0.0.1", package = "uu_lscpu", path = "src/
lsipc = { optional = true, version = "0.0.1", package = "uu_lsipc", path = "src/uu/lsipc" }
lslocks = { optional = true, version = "0.0.1", package = "uu_lslocks", path = "src/uu/lslocks" }
lsmem = { optional = true, version = "0.0.1", package = "uu_lsmem", path = "src/uu/lsmem" }
lsns = { optional = true, version = "0.0.1", package = "uu_lsns", path = "src/uu/lsns" }
mcookie = { optional = true, version = "0.0.1", package = "uu_mcookie", path = "src/uu/mcookie" }
mesg = { optional = true, version = "0.0.1", package = "uu_mesg", path = "src/uu/mesg" }
mountpoint = { optional = true, version = "0.0.1", package = "uu_mountpoint", path = "src/uu/mountpoint" }
Expand Down
16 changes: 16 additions & 0 deletions src/uu/lsns/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "uu_lsns"
version = "0.0.1"
edition = "2024"

[lib]
path = "src/lsns.rs"

[[bin]]
name = "lsns"
path = "src/main.rs"

[dependencies]
uucore = { workspace = true, features = ["entries"] }
clap = { workspace = true }
smartcols-sys = { workspace = true }
7 changes: 7 additions & 0 deletions src/uu/lsns/lsns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# lsns

```
lsns [OPTION]...
```

List the namespaces in the system.
105 changes: 105 additions & 0 deletions src/uu/lsns/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use std::ffi::c_int;
use std::fmt;

use uucore::error::UError;

#[derive(Debug)]
pub enum LsnsError {
/// Generic I/O error with context message
IOError(String, std::io::Error),
/// CString conversion error (null byte in string)
NulError(String, std::ffi::NulError),
/// Invalid namespace type index
InvalidNamespaceType(usize),
/// Unsupported platform
#[cfg(not(target_os = "linux"))]
UnsupportedPlatform,
/// Invalid namespace inode format
InvalidNamespaceInodeFormat(String),
/// Invalid process stat format
InvalidProcessStatFormat(String),
/// Failed to get UID from directory entry
FailedToGetUid(String),
/// Failed to get PID from directory entry
FailedToGetPid(String),
/// Failed to read process information
FailedToReadProcess(String),
}

impl LsnsError {
/// Create an I/O error with a context message
pub(crate) fn io0(message: impl Into<String>, error: impl Into<std::io::Error>) -> Self {
Self::IOError(message.into(), error.into())
}

/// Helper to convert negative errno to Result
pub(crate) fn io_from_neg_errno(
message: impl Into<String>,
result: c_int,
) -> Result<usize, LsnsError> {
if let Ok(result) = usize::try_from(result) {
Ok(result)
} else {
let err = std::io::Error::from_raw_os_error(-result);
Err(Self::IOError(message.into(), err))
}
}
}

impl fmt::Display for LsnsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IOError(message, err) => write!(f, "{message}: {err}"),
Self::NulError(message, err) => write!(f, "{message}: {err}"),
Self::InvalidNamespaceType(idx) => write!(f, "Invalid namespace type index: {}", idx),
#[cfg(not(target_os = "linux"))]
Self::UnsupportedPlatform => write!(f, "lsns is only supported on Linux"),
Self::InvalidNamespaceInodeFormat(s) => {
write!(f, "Invalid namespace inode format: {}", s)
}
Self::InvalidProcessStatFormat(s) => {
write!(f, "Invalid process stat format: {}", s)
}
Self::FailedToGetUid(s) => {
write!(f, "Failed to get UID from directory entry: {}", s)
}
Self::FailedToGetPid(s) => {
write!(f, "Failed to get PID from directory entry: {}", s)
}
Self::FailedToReadProcess(s) => {
write!(f, "Failed to read process information: {}", s)
}
}
}
}

impl UError for LsnsError {
fn code(&self) -> i32 {
1
}

fn usage(&self) -> bool {
false
}
}

impl std::error::Error for LsnsError {}

// Implement From trait for automatic conversion from std::io::Error
impl From<std::io::Error> for LsnsError {
fn from(err: std::io::Error) -> Self {
Self::IOError(String::new(), err)
}
}

// Implement From trait for automatic conversion from std::ffi::NulError
impl From<std::ffi::NulError> for LsnsError {
fn from(err: std::ffi::NulError) -> Self {
Self::NulError(String::new(), err)
}
}
Loading
Loading