@@ -62,9 +62,9 @@ macro_rules! cstr2cow {
6262}
6363
6464fn get_context_help_text ( ) -> String {
65- #[ cfg( not( feature = "selinux" ) ) ]
65+ #[ cfg( not( any ( feature = "selinux" , feature = "smack" ) ) ) ]
6666 return translate ! ( "id-context-help-disabled" ) ;
67- #[ cfg( feature = "selinux" ) ]
67+ #[ cfg( any ( feature = "selinux" , feature = "smack" ) ) ]
6868 return translate ! ( "id-context-help-enabled" ) ;
6969}
7070
@@ -98,7 +98,10 @@ struct State {
9898 rflag : bool , // --real
9999 zflag : bool , // --zero
100100 cflag : bool , // --context
101+ #[ cfg( feature = "selinux" ) ]
101102 selinux_supported : bool ,
103+ #[ cfg( feature = "smack" ) ]
104+ smack_supported : bool ,
102105 ids : Option < Ids > ,
103106 // The behavior for calling GNU's `id` and calling GNU's `id $USER` is similar but different.
104107 // * The SELinux context is only displayed without a specified user.
@@ -136,16 +139,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
136139 zflag : matches. get_flag ( options:: OPT_ZERO ) ,
137140 cflag : matches. get_flag ( options:: OPT_CONTEXT ) ,
138141
139- selinux_supported : {
140- #[ cfg( feature = "selinux" ) ]
141- {
142- uucore:: selinux:: is_selinux_enabled ( )
143- }
144- #[ cfg( not( feature = "selinux" ) ) ]
145- {
146- false
147- }
148- } ,
142+ #[ cfg( feature = "selinux" ) ]
143+ selinux_supported : uucore:: selinux:: is_selinux_enabled ( ) ,
144+ #[ cfg( feature = "smack" ) ]
145+ smack_supported : uucore:: smack:: is_smack_enabled ( ) ,
149146 user_specified : !users. is_empty ( ) ,
150147 ids : None ,
151148 } ;
@@ -179,26 +176,42 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
179176 let line_ending = LineEnding :: from_zero_flag ( state. zflag ) ;
180177
181178 if state. cflag {
182- return if state . selinux_supported {
183- // print SElinux context and exit
184- # [ cfg ( all ( any ( target_os = "linux" , target_os = "android" ) , feature = "selinux" ) ) ]
179+ // SELinux context
180+ # [ cfg ( feature = "selinux" ) ]
181+ if state . selinux_supported {
185182 if let Ok ( context) = selinux:: SecurityContext :: current ( false ) {
186183 let bytes = context. as_bytes ( ) ;
187184 print ! ( "{}{line_ending}" , String :: from_utf8_lossy( bytes) ) ;
188- } else {
189- // print error because `cflag` was explicitly requested
190- return Err ( USimpleError :: new (
191- 1 ,
192- translate ! ( "id-error-cannot-get-context" ) ,
193- ) ) ;
185+ return Ok ( ( ) ) ;
194186 }
195- Ok ( ( ) )
196- } else {
197- Err ( USimpleError :: new (
187+ return Err ( USimpleError :: new (
198188 1 ,
199- translate ! ( "id-error-context-selinux-only" ) ,
200- ) )
201- } ;
189+ translate ! ( "id-error-cannot-get-context" ) ,
190+ ) ) ;
191+ }
192+
193+ // SMACK label
194+ #[ cfg( feature = "smack" ) ]
195+ if state. smack_supported {
196+ match uucore:: smack:: get_smack_label_for_self ( ) {
197+ Ok ( label) => {
198+ print ! ( "{label}{line_ending}" ) ;
199+ return Ok ( ( ) ) ;
200+ }
201+ Err ( _) => {
202+ return Err ( USimpleError :: new (
203+ 1 ,
204+ translate ! ( "id-error-cannot-get-context" ) ,
205+ ) ) ;
206+ }
207+ }
208+ }
209+
210+ // Neither SELinux nor SMACK supported
211+ return Err ( USimpleError :: new (
212+ 1 ,
213+ translate ! ( "id-error-context-selinux-only" ) ,
214+ ) ) ;
202215 }
203216
204217 for i in 0 ..=users. len ( ) {
@@ -666,7 +679,7 @@ fn id_print(state: &State, groups: &[u32]) {
666679 . join( "," )
667680 ) ;
668681
669- #[ cfg( all ( any ( target_os = "linux" , target_os = "android" ) , feature = "selinux" ) ) ]
682+ #[ cfg( feature = "selinux" ) ]
670683 if state. selinux_supported
671684 && !state. user_specified
672685 && std:: env:: var_os ( "POSIXLY_CORRECT" ) . is_none ( )
@@ -677,6 +690,17 @@ fn id_print(state: &State, groups: &[u32]) {
677690 print ! ( " context={}" , String :: from_utf8_lossy( bytes) ) ;
678691 }
679692 }
693+
694+ #[ cfg( feature = "smack" ) ]
695+ if state. smack_supported
696+ && !state. user_specified
697+ && std:: env:: var_os ( "POSIXLY_CORRECT" ) . is_none ( )
698+ {
699+ // print SMACK label (does not depend on "-Z")
700+ if let Ok ( label) = uucore:: smack:: get_smack_label_for_self ( ) {
701+ print ! ( " context={label}" ) ;
702+ }
703+ }
680704}
681705
682706#[ cfg( not( any( target_os = "linux" , target_os = "android" , target_os = "openbsd" ) ) ) ]
0 commit comments