5
5
// * For the full copyright and license information, please view the LICENSE file
6
6
// * that was distributed with this source code.
7
7
8
- // spell-checker:ignore (ToDO) signalname pids
8
+ // spell-checker:ignore (ToDO) signalname pids killpg
9
9
10
10
#[ macro_use]
11
11
extern crate uucore;
12
12
13
13
use clap:: { crate_version, Arg , Command } ;
14
- use libc:: { c_int, pid_t} ;
14
+ use nix:: sys:: signal:: { self , Signal } ;
15
+ use nix:: unistd:: Pid ;
15
16
use std:: io:: Error ;
16
17
use uucore:: display:: Quotable ;
17
- use uucore:: error:: { UResult , USimpleError } ;
18
+ use uucore:: error:: { FromIo , UError , UResult , USimpleError } ;
18
19
use uucore:: signals:: { signal_by_name_or_value, ALL_SIGNALS } ;
19
20
use uucore:: { format_usage, InvalidEncodingHandling } ;
20
21
@@ -67,8 +68,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
67
68
} else {
68
69
15_usize //SIGTERM
69
70
} ;
71
+ let sig: Signal = ( sig as i32 )
72
+ . try_into ( )
73
+ . map_err ( |e| std:: io:: Error :: from_raw_os_error ( e as i32 ) ) ?;
70
74
let pids = parse_pids ( & pids_or_signals) ?;
71
- kill ( sig, & pids)
75
+ kill ( sig, & pids) ;
76
+ Ok ( ( ) )
72
77
}
73
78
Mode :: Table => {
74
79
table ( ) ;
@@ -84,6 +89,7 @@ pub fn uu_app<'a>() -> Command<'a> {
84
89
. about ( ABOUT )
85
90
. override_usage ( format_usage ( USAGE ) )
86
91
. infer_long_args ( true )
92
+ . allow_negative_numbers ( true )
87
93
. arg (
88
94
Arg :: new ( options:: LIST )
89
95
. short ( 'l' )
@@ -109,8 +115,7 @@ pub fn uu_app<'a>() -> Command<'a> {
109
115
. arg (
110
116
Arg :: new ( options:: PIDS_OR_SIGNALS )
111
117
. hide ( true )
112
- . multiple_occurrences ( true )
113
- . allow_hyphen_values ( true )
118
+ . multiple_occurrences ( true ) ,
114
119
)
115
120
}
116
121
@@ -191,21 +196,21 @@ fn parse_signal_value(signal_name: &str) -> UResult<usize> {
191
196
}
192
197
}
193
198
194
- fn parse_pids ( pids : & [ String ] ) -> UResult < Vec < usize > > {
199
+ fn parse_pids ( pids : & [ String ] ) -> UResult < Vec < i32 > > {
195
200
pids. iter ( )
196
201
. map ( |x| {
197
- x. parse :: < usize > ( ) . map_err ( |e| {
202
+ x. parse :: < i32 > ( ) . map_err ( |e| {
198
203
USimpleError :: new ( 1 , format ! ( "failed to parse argument {}: {}" , x. quote( ) , e) )
199
204
} )
200
205
} )
201
206
. collect ( )
202
207
}
203
208
204
- fn kill ( signal_value : usize , pids : & [ usize ] ) -> UResult < ( ) > {
209
+ fn kill ( sig : Signal , pids : & [ i32 ] ) {
205
210
for & pid in pids {
206
- if unsafe { libc:: kill ( pid as pid_t , signal_value as c_int ) } != 0 {
207
- show ! ( USimpleError :: new( 1 , format!( "{}" , Error :: last_os_error( ) ) ) ) ;
211
+ if let Err ( e) = signal:: kill ( Pid :: from_raw ( pid) , sig) {
212
+ show ! ( Error :: from_raw_os_error( e as i32 )
213
+ . map_err_context( || format!( "sending signal to {} failed" , pid) ) ) ;
208
214
}
209
215
}
210
- Ok ( ( ) )
211
216
}
0 commit comments