Skip to content

Commit 6091d0b

Browse files
committed
selinux: use the uucore::selinux::is_selinux_enabled() function
1 parent 45b0c39 commit 6091d0b

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

src/uu/id/src/id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
138138
selinux_supported: {
139139
#[cfg(feature = "selinux")]
140140
{
141-
selinux::kernel_support() != selinux::KernelSupport::Unsupported
141+
uucore::selinux::is_selinux_enabled()
142142
}
143143
#[cfg(not(feature = "selinux"))]
144144
{

src/uu/ls/src/ls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ impl Config {
11571157
selinux_supported: {
11581158
#[cfg(feature = "selinux")]
11591159
{
1160-
selinux::kernel_support() != selinux::KernelSupport::Unsupported
1160+
uucore::selinux::is_selinux_enabled()
11611161
}
11621162
#[cfg(not(feature = "selinux"))]
11631163
{

src/uu/runcon/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ path = "src/runcon.rs"
1919

2020
[dependencies]
2121
clap = { workspace = true }
22-
uucore = { workspace = true, features = ["entries", "fs", "perms"] }
22+
uucore = { workspace = true, features = ["entries", "fs", "perms", "selinux"] }
2323
selinux = { workspace = true }
2424
thiserror = { workspace = true }
2525
libc = { workspace = true }

src/uu/runcon/src/runcon.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn set_next_exec_context(context: &OpaqueSecurityContext) -> Result<()> {
271271
}
272272

273273
fn get_plain_context(context: &OsStr) -> Result<OpaqueSecurityContext> {
274-
if selinux::kernel_support() == selinux::KernelSupport::Unsupported {
274+
if !uucore::selinux::is_selinux_enabled() {
275275
return Err(Error::SELinuxNotEnabled);
276276
}
277277

@@ -342,7 +342,7 @@ fn get_custom_context(
342342
use OpaqueSecurityContext as OSC;
343343
type SetNewValueProc = fn(&OSC, &CStr) -> selinux::errors::Result<()>;
344344

345-
if selinux::kernel_support() == selinux::KernelSupport::Unsupported {
345+
if !uucore::selinux::is_selinux_enabled() {
346346
return Err(Error::SELinuxNotEnabled);
347347
}
348348

src/uucore/src/lib/features/selinux.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ mod tests {
284284
fn test_invalid_context_string_error() {
285285
let tmpfile = NamedTempFile::new().expect("Failed to create tempfile");
286286
let path = tmpfile.path();
287-
287+
if !is_selinux_enabled() {
288+
println!("test skipped: Kernel has no support for SElinux context");
289+
return;
290+
}
288291
// Pass a context string containing a null byte to trigger CString::new error
289292
let invalid_context = String::from("invalid\0context");
290293
let result = set_selinux_security_context(path, Some(&invalid_context));
@@ -322,7 +325,10 @@ mod tests {
322325
fn test_get_selinux_security_context() {
323326
let tmpfile = NamedTempFile::new().expect("Failed to create tempfile");
324327
let path = tmpfile.path();
325-
328+
if !is_selinux_enabled() {
329+
println!("test skipped: Kernel has no support for SElinux context");
330+
return;
331+
}
326332
std::fs::write(path, b"test content").expect("Failed to write to tempfile");
327333

328334
let result = get_selinux_security_context(path);
@@ -387,7 +393,10 @@ mod tests {
387393
#[test]
388394
fn test_get_selinux_context_nonexistent_file() {
389395
let path = Path::new("/nonexistent/file/that/does/not/exist");
390-
396+
if !is_selinux_enabled() {
397+
println!("test skipped: Kernel has no support for SElinux context");
398+
return;
399+
}
391400
let result = get_selinux_security_context(path);
392401

393402
assert!(result.is_err());

tests/by-util/test_id.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ fn test_id_zero() {
376376
#[test]
377377
#[cfg(feature = "feat_selinux")]
378378
fn test_id_context() {
379-
use selinux::{self, KernelSupport};
380-
if selinux::kernel_support() == KernelSupport::Unsupported {
379+
if !uucore::selinux::is_selinux_enabled() {
381380
println!("test skipped: Kernel has no support for SElinux context");
382381
return;
383382
}
@@ -450,12 +449,11 @@ fn test_id_no_specified_user_posixly() {
450449
feature = "feat_selinux"
451450
))]
452451
{
453-
use selinux::{self, KernelSupport};
454-
if selinux::kernel_support() == KernelSupport::Unsupported {
455-
println!("test skipped: Kernel has no support for SElinux context");
456-
} else {
452+
if uucore::selinux::is_selinux_enabled() {
457453
let result = ts.ucmd().succeeds();
458454
assert!(result.stdout_str().contains("context="));
455+
} else {
456+
println!("test skipped: Kernel has no support for SElinux context");
459457
}
460458
}
461459
}

0 commit comments

Comments
 (0)