Skip to content

Commit 99f9f07

Browse files
xtqqczzenaoNao89
andcommitted
fix(id): refine feature checks for SELinux and SMACK support
Co-authored-by: naoNao89 <90588855+naoNao89@users.noreply.github.com>
1 parent 3f6a73d commit 99f9f07

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

src/uu/id/src/id.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,17 @@ macro_rules! cstr2cow {
6363
}
6464

6565
fn get_context_help_text() -> String {
66-
#[cfg(not(any(feature = "selinux", feature = "smack")))]
67-
return translate!("id-context-help-disabled");
68-
#[cfg(any(feature = "selinux", feature = "smack"))]
66+
#[cfg(any(
67+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
68+
all(feature = "smack", target_os = "linux"),
69+
))]
6970
return translate!("id-context-help-enabled");
71+
72+
#[cfg(not(any(
73+
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
74+
all(feature = "smack", target_os = "linux"),
75+
)))]
76+
return translate!("id-context-help-disabled");
7077
}
7178

7279
mod options {
@@ -99,9 +106,9 @@ struct State {
99106
rflag: bool, // --real
100107
zflag: bool, // --zero
101108
cflag: bool, // --context
102-
#[cfg(feature = "selinux")]
109+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
103110
selinux_supported: bool,
104-
#[cfg(feature = "smack")]
111+
#[cfg(all(feature = "smack", target_os = "linux"))]
105112
smack_supported: bool,
106113
ids: Option<Ids>,
107114
// The behavior for calling GNU's `id` and calling GNU's `id $USER` is similar but different.
@@ -140,10 +147,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
140147
rflag: matches.get_flag(options::OPT_REAL_ID),
141148
zflag: matches.get_flag(options::OPT_ZERO),
142149
cflag: matches.get_flag(options::OPT_CONTEXT),
143-
144-
#[cfg(feature = "selinux")]
150+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
145151
selinux_supported: uucore::selinux::is_selinux_enabled(),
146-
#[cfg(feature = "smack")]
152+
#[cfg(all(feature = "smack", target_os = "linux"))]
147153
smack_supported: uucore::smack::is_smack_enabled(),
148154
user_specified: !users.is_empty(),
149155
ids: None,
@@ -179,7 +185,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
179185

180186
if state.cflag {
181187
// SELinux context
182-
#[cfg(feature = "selinux")]
188+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
183189
if state.selinux_supported {
184190
if let Ok(context) = selinux::SecurityContext::current(false) {
185191
let bytes = context.as_bytes();
@@ -193,7 +199,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
193199
}
194200

195201
// SMACK label
196-
#[cfg(feature = "smack")]
202+
#[cfg(all(feature = "smack", target_os = "linux"))]
197203
if state.smack_supported {
198204
match uucore::smack::get_smack_label_for_self() {
199205
Ok(label) => {
@@ -705,7 +711,7 @@ fn id_print(state: &State, groups: &[u32]) -> io::Result<()> {
705711
.join(",")
706712
)?;
707713

708-
#[cfg(feature = "selinux")]
714+
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
709715
if state.selinux_supported
710716
&& !state.user_specified
711717
&& std::env::var_os("POSIXLY_CORRECT").is_none()
@@ -717,7 +723,7 @@ fn id_print(state: &State, groups: &[u32]) -> io::Result<()> {
717723
}
718724
}
719725

720-
#[cfg(feature = "smack")]
726+
#[cfg(all(feature = "smack", target_os = "linux"))]
721727
if state.smack_supported
722728
&& !state.user_specified
723729
&& std::env::var_os("POSIXLY_CORRECT").is_none()

0 commit comments

Comments
 (0)