33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6- use clap:: { Arg , ArgAction , Command } ;
6+ use clap:: { Arg , ArgAction , Command , value_parser } ;
77use libc:: mkfifo;
88use std:: ffi:: CString ;
99use std:: fs;
@@ -17,7 +17,7 @@ static ABOUT: &str = help_about!("mkfifo.md");
1717
1818mod options {
1919 pub static MODE : & str = "mode" ;
20- pub static SE_LINUX_SECURITY_CONTEXT : & str = "Z" ;
20+ pub static SELINUX : & str = "Z" ;
2121 pub static CONTEXT : & str = "context" ;
2222 pub static FIFO : & str = "fifo" ;
2323}
@@ -26,13 +26,6 @@ mod options {
2626pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
2727 let matches = uu_app ( ) . try_get_matches_from ( args) ?;
2828
29- if matches. contains_id ( options:: CONTEXT ) {
30- return Err ( USimpleError :: new ( 1 , "--context is not implemented" ) ) ;
31- }
32- if matches. get_flag ( options:: SE_LINUX_SECURITY_CONTEXT ) {
33- return Err ( USimpleError :: new ( 1 , "-Z is not implemented" ) ) ;
34- }
35-
3629 let mode = match matches. get_one :: < String > ( options:: MODE ) {
3730 // if mode is passed, ignore umask
3831 Some ( m) => match usize:: from_str_radix ( m, 8 ) {
@@ -67,6 +60,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6760 format ! ( "cannot set permissions on {}: {e}" , f. quote( ) ) ,
6861 ) ) ;
6962 }
63+
64+ // Apply SELinux context if requested
65+ #[ cfg( feature = "selinux" ) ]
66+ {
67+ // Extract the SELinux related flags and options
68+ let set_selinux_context = matches. get_flag ( options:: SELINUX ) ;
69+ let context = matches. get_one :: < String > ( options:: CONTEXT ) ;
70+
71+ if set_selinux_context || context. is_some ( ) {
72+ use std:: path:: Path ;
73+ if let Err ( e) =
74+ uucore:: selinux:: set_selinux_security_context ( Path :: new ( & f) , context)
75+ {
76+ let _ = fs:: remove_file ( f) ;
77+ return Err ( USimpleError :: new (
78+ 1 ,
79+ format ! ( "failed to set SELinux security context: {e}" ) ,
80+ ) ) ;
81+ }
82+ }
83+ }
7084 }
7185
7286 Ok ( ( ) )
@@ -86,7 +100,7 @@ pub fn uu_app() -> Command {
86100 . value_name ( "MODE" ) ,
87101 )
88102 . arg (
89- Arg :: new ( options:: SE_LINUX_SECURITY_CONTEXT )
103+ Arg :: new ( options:: SELINUX )
90104 . short ( 'Z' )
91105 . help ( "set the SELinux security context to default type" )
92106 . action ( ArgAction :: SetTrue ) ,
@@ -95,6 +109,9 @@ pub fn uu_app() -> Command {
95109 Arg :: new ( options:: CONTEXT )
96110 . long ( options:: CONTEXT )
97111 . value_name ( "CTX" )
112+ . value_parser ( value_parser ! ( String ) )
113+ . num_args ( 0 ..=1 )
114+ . require_equals ( true )
98115 . help (
99116 "like -Z, or if CTX is specified then set the SELinux \
100117 or SMACK security context to CTX",
0 commit comments