44// file that was distributed with this source code.
55
66// Remove this if the tool is ported to Non-UNIX platforms.
7+ #![ cfg_attr( not( target_os = "linux" ) , allow( dead_code) ) ]
78
9+ mod errors;
10+
11+ use crate :: errors:: KillError :: { self , NoSuchProcess , OperationNotPermitted } ;
812use clap:: { Arg , ArgAction , Command , crate_version, value_parser} ;
913use uucore:: libc;
1014use uucore:: { error:: UResult , format_usage, help_about, help_usage} ;
1115
1216const ABOUT : & str = help_about ! ( "kill.md" ) ;
1317const USAGE : & str = help_usage ! ( "kill.md" ) ;
1418
19+ #[ cfg( not( target_os = "linux" ) ) ]
20+ fn kill ( pid : i32 , signal : i32 ) -> Result < ( ) , KillError > {
21+ Err ( UnsupportedPlatform )
22+ }
23+
24+ #[ cfg( target_os = "linux" ) ]
25+ fn kill ( pid : i32 , signal : i32 ) -> Result < ( ) , KillError > {
26+ unsafe { libc:: kill ( pid, signal) } ;
27+
28+ let err = std:: io:: Error :: last_os_error ( ) . raw_os_error ( ) ;
29+ if let Some ( err_no) = err {
30+ match err_no {
31+ libc:: EPERM => return Err ( OperationNotPermitted ( pid) ) ,
32+ libc:: ESRCH => return Err ( NoSuchProcess ( pid) ) ,
33+ _ => { }
34+ }
35+ }
36+
37+ Ok ( ( ) )
38+ }
39+
1540#[ uucore:: main]
1641pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
1742 let matches = uu_app ( ) . try_get_matches_from ( args) ?;
1843
1944 if let Some ( pids) = matches. get_many :: < i32 > ( "pid" ) {
2045 for pid in pids {
21- unsafe { libc:: kill ( * pid, libc:: SIGTERM ) } ;
22-
23- let err = std:: io:: Error :: last_os_error ( ) . raw_os_error ( ) ;
24- if let Some ( err_no) = err {
25- match err_no {
26- libc:: EPERM => {
27- eprintln ! ( "bash: kill: ({pid}) - Operation not permitted" ) ;
28- }
29- libc:: ESRCH => {
30- eprintln ! ( "bash: kill: ({pid}) - No such process" ) ;
31- }
32- _ => { }
33- }
34- }
46+ kill ( * pid, libc:: SIGTERM ) ?;
3547 }
3648 }
3749
@@ -47,7 +59,7 @@ pub fn uu_app() -> Command {
4759 . arg (
4860 Arg :: new ( "pid" )
4961 . help ( "PID of the process to kill" )
50- . required ( false )
62+ . required ( true )
5163 . action ( ArgAction :: Append )
5264 . value_name ( "PID" )
5365 . value_parser ( value_parser ! ( i32 ) ) ,
0 commit comments