@@ -15,38 +15,10 @@ const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
1515/// Trait for sending interrupts/ctrl-c to child processes
1616pub trait Interruptable : InterruptablePid {
1717 /// Send a ctrl-c interrupt to the child process
18- #[ cfg( all( not( windows) , not( unix) ) ) ]
19- fn send_ctrl_c ( & self ) -> io:: Result < ( ) > {
20- unimplemented ! ( "Not implemented for this platform" ) ;
21- }
22-
23- /// Send a ctrl-c interrupt to the child process
24- #[ cfg( unix) ]
25- fn send_ctrl_c ( & self ) -> io:: Result < ( ) > {
26- if let Some ( pid) = self . pid ( ) {
27- if unsafe { libc:: kill ( pid as i32 , libc:: SIGINT ) } == 0 {
28- Ok ( ( ) )
29- } else {
30- Err ( io:: Error :: last_os_error ( ) )
31- }
32- } else {
33- Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Process has no pid" ) )
34- }
35- }
36-
37- /// Send a ctrl-c interrupt to the child process
38- #[ cfg( windows) ]
3918 fn send_ctrl_c ( & self ) -> io:: Result < ( ) > {
40- use windows_sys:: Win32 :: System :: Console :: { CTRL_C_EVENT , GenerateConsoleCtrlEvent } ;
41- if let Some ( pid) = self . pid ( ) {
42- // NOTE: This only works if the process is in a new process group
43- if unsafe { GenerateConsoleCtrlEvent ( CTRL_C_EVENT , pid) } != 0 {
44- Ok ( ( ) )
45- } else {
46- Err ( io:: Error :: last_os_error ( ) )
47- }
48- } else {
49- Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Process has no pid" ) )
19+ match self . pid ( ) {
20+ Some ( pid) => inner:: send_ctrl_c ( pid) ,
21+ None => Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Process has no pid" ) ) ,
5022 }
5123 }
5224}
@@ -78,7 +50,33 @@ pub fn new_command<S: AsRef<OsStr>>(program: S) -> Command {
7850}
7951
8052mod inner {
81- use std:: { ffi:: OsStr , process:: Command } ;
53+ use std:: { ffi:: OsStr , io, process:: Command } ;
54+
55+ #[ cfg( all( not( windows) , not( unix) ) ) ]
56+ pub fn send_ctrl_c ( _pid : u32 ) -> io:: Result < ( ) > {
57+ unimplemented ! ( "Not implemented for this platform" ) ;
58+ }
59+
60+ #[ cfg( unix) ]
61+ pub fn send_ctrl_c ( pid : u32 ) -> io:: Result < ( ) > {
62+ if unsafe { libc:: kill ( pid as i32 , libc:: SIGINT ) } == 0 {
63+ Ok ( ( ) )
64+ } else {
65+ Err ( io:: Error :: last_os_error ( ) )
66+ }
67+ }
68+
69+ #[ cfg( windows) ]
70+ pub fn send_ctrl_c ( pid : u32 ) -> io:: Result < ( ) > {
71+ use windows_sys:: Win32 :: System :: Console :: { CTRL_C_EVENT , GenerateConsoleCtrlEvent } ;
72+
73+ // NOTE: This only works if the process is in a new process group
74+ if unsafe { GenerateConsoleCtrlEvent ( CTRL_C_EVENT , pid) } != 0 {
75+ Ok ( ( ) )
76+ } else {
77+ Err ( io:: Error :: last_os_error ( ) )
78+ }
79+ }
8280
8381 #[ cfg( windows) ]
8482 pub fn new_command < S : AsRef < OsStr > > ( program : S ) -> Command {
@@ -98,7 +96,6 @@ mod inner {
9896}
9997
10098/// Create a new interruptable tokio command
101- #[ cfg_attr( docsrs, doc( cfg( feature = "tokio" ) ) ) ]
10299#[ cfg( feature = "tokio" ) ]
103100pub fn new_tokio_command < S : AsRef < OsStr > > ( program : S ) -> tokio:: process:: Command {
104101 inner_tokio:: new_tokio_command ( program)
0 commit comments