Skip to content

Commit 02312bf

Browse files
authored
fix: Gate SELinux to Linux and add cross-platform CI tests (#8795)
Gate SELinux functionality to Linux-only and provide stub implementations for chcon/runcon on non-Linux platforms to maintain cross-platform builds. Changes: - Gate all SELinux code with target_os = "linux" checks - Add stub main() for chcon/runcon on non-Linux with user-friendly errors - Add CI job to verify stubs build correctly on macOS and Windows - Update ls to check both selinux feature AND target_os Benefits: - Fixes build failures on macOS/Windows (#8581, #7996, #7695, #6491) - Maintains workspace buildability across all platforms - Provides clear error messages instead of silent failures - Prevents accidental SELinux usage on unsupported platforms CI Testing: - New 'Build/SELinux-Stubs (Non-Linux)' job tests macOS and Windows - Verifies stub binaries are created and compilation succeeds - Validates full workspace builds with stubs present Addresses maintainer feedback in PR #8795
1 parent e16ce60 commit 02312bf

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

.github/workflows/CICD.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,40 @@ jobs:
12801280
- name: Lint with SELinux
12811281
run: lima bash -c "cd work && cargo clippy --all-targets --features 'feat_selinux' -- -D warnings"
12821282

1283+
test_selinux_stubs:
1284+
name: Build/SELinux-Stubs (Non-Linux)
1285+
needs: [ min_version, deps ]
1286+
runs-on: ${{ matrix.job.os }}
1287+
strategy:
1288+
fail-fast: false
1289+
matrix:
1290+
job:
1291+
- { os: macos-latest , features: feat_os_macos }
1292+
- { os: windows-latest , features: feat_os_windows }
1293+
steps:
1294+
- uses: actions/checkout@v5
1295+
with:
1296+
persist-credentials: false
1297+
- uses: dtolnay/rust-toolchain@stable
1298+
- uses: Swatinem/rust-cache@v2
1299+
1300+
- name: Build SELinux utilities as stubs
1301+
run: cargo build -p uu_chcon -p uu_runcon
1302+
1303+
- name: Verify stub binaries exist
1304+
shell: bash
1305+
run: |
1306+
if [ "${{ runner.os }}" = "Windows" ]; then
1307+
test -f target/debug/chcon.exe || exit 1
1308+
test -f target/debug/runcon.exe || exit 1
1309+
else
1310+
test -f target/debug/chcon || exit 1
1311+
test -f target/debug/runcon || exit 1
1312+
fi
1313+
1314+
- name: Verify workspace builds with stubs
1315+
run: cargo build --features ${{ matrix.job.features }}
1316+
12831317
benchmarks:
12841318
name: Run benchmarks (CodSpeed)
12851319
runs-on: ubuntu-latest

src/uu/chcon/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
#![cfg(target_os = "linux")]
1+
// On non-Linux targets, provide a stub main to keep the binary target present
2+
// and the workspace buildable. Using item-level cfg avoids excluding the crate
3+
// entirely (via #![cfg(...)]), which can break tooling and cross builds that
4+
// expect this binary to exist even when it's a no-op off Linux.
5+
#[cfg(target_os = "linux")]
26
uucore::bin!(uu_chcon);
7+
8+
#[cfg(not(target_os = "linux"))]
9+
fn main() {
10+
eprintln!("chcon: SELinux is not supported on this platform");
11+
std::process::exit(1);
12+
}

src/uu/ls/src/ls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,11 +1085,11 @@ impl Config {
10851085
time_format_older,
10861086
context,
10871087
selinux_supported: {
1088-
#[cfg(feature = "selinux")]
1088+
#[cfg(all(feature = "selinux", target_os = "linux"))]
10891089
{
10901090
uucore::selinux::is_selinux_enabled()
10911091
}
1092-
#[cfg(not(feature = "selinux"))]
1092+
#[cfg(not(all(feature = "selinux", target_os = "linux")))]
10931093
{
10941094
false
10951095
}
@@ -3309,7 +3309,7 @@ fn get_security_context<'a>(
33093309
}
33103310

33113311
if config.selinux_supported {
3312-
#[cfg(feature = "selinux")]
3312+
#[cfg(all(feature = "selinux", target_os = "linux"))]
33133313
{
33143314
match selinux::SecurityContext::of_path(path, must_dereference, false) {
33153315
Err(_r) => {

src/uu/runcon/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
#![cfg(target_os = "linux")]
1+
// On non-Linux targets, provide a stub main to keep the binary target present
2+
// and the workspace buildable. Using item-level cfg avoids excluding the crate
3+
// entirely (via #![cfg(...)]), which can break tooling and cross builds that
4+
// expect this binary to exist even when it's a no-op off Linux.
5+
#[cfg(target_os = "linux")]
26
uucore::bin!(uu_runcon);
7+
8+
#[cfg(not(target_os = "linux"))]
9+
fn main() {
10+
eprintln!("runcon: SELinux is not supported on this platform");
11+
std::process::exit(1);
12+
}

0 commit comments

Comments
 (0)