File tree Expand file tree Collapse file tree 6 files changed +78
-0
lines changed Expand file tree Collapse file tree 6 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -1860,6 +1860,38 @@ impl Child {
1860
1860
self . handle . kill ( )
1861
1861
}
1862
1862
1863
+ /// Signals the child process to exit. If the child has already exited, an [`InvalidInput`]
1864
+ /// error is returned.
1865
+ ///
1866
+ /// Unlike [`kill`] this allows the process to catch the signal and gracefully exit.
1867
+ ///
1868
+ /// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function.
1869
+ ///
1870
+ /// This is equivalent to sending a SIGINT on Unix platforms.
1871
+ ///
1872
+ /// # Examples
1873
+ ///
1874
+ /// Basic usage:
1875
+ ///
1876
+ /// ```no_run
1877
+ /// use std::process::Command;
1878
+ ///
1879
+ /// let mut command = Command::new("yes");
1880
+ /// if let Ok(mut child) = command.spawn() {
1881
+ /// child.interrupt().expect("command wasn't running");
1882
+ /// } else {
1883
+ /// println!("yes command didn't start");
1884
+ /// }
1885
+ /// ```
1886
+ ///
1887
+ /// [`ErrorKind`]: io::ErrorKind
1888
+ /// [`InvalidInput`]: io::ErrorKind::InvalidInput
1889
+ #[ stable( feature = "process" , since = "1.0.0" ) ]
1890
+ pub fn interrupt ( & mut self ) -> io:: Result < ( ) > {
1891
+ self . handle . interrupt ( )
1892
+ }
1893
+
1894
+
1863
1895
/// Returns the OS-assigned process identifier associated with this child.
1864
1896
///
1865
1897
/// # Examples
Original file line number Diff line number Diff line change @@ -160,6 +160,16 @@ impl Process {
160
160
Ok ( ( ) )
161
161
}
162
162
163
+ pub fn interrupt ( & mut self ) -> io:: Result < ( ) > {
164
+ use crate :: sys:: process:: zircon:: * ;
165
+
166
+ unsafe {
167
+ zx_cvt ( zx_task_kill ( self . handle . raw ( ) ) ) ?;
168
+ }
169
+
170
+ Ok ( ( ) )
171
+ }
172
+
163
173
pub fn wait ( & mut self ) -> io:: Result < ExitStatus > {
164
174
use crate :: default:: Default ;
165
175
use crate :: sys:: process:: zircon:: * ;
Original file line number Diff line number Diff line change @@ -604,6 +604,20 @@ impl Process {
604
604
cvt ( unsafe { libc:: kill ( self . pid , libc:: SIGKILL ) } ) . map ( drop)
605
605
}
606
606
}
607
+
608
+ pub fn interrupt ( & mut self ) -> io:: Result < ( ) > {
609
+ // If we've already waited on this process then the pid can be recycled
610
+ // and used for another process, and we probably shouldn't be interrupting
611
+ // random processes, so just return an error.
612
+ if self . status . is_some ( ) {
613
+ Err ( io:: const_io_error!(
614
+ ErrorKind :: InvalidInput ,
615
+ "invalid argument: can't interrupt an exited process" ,
616
+ ) )
617
+ } else {
618
+ cvt ( unsafe { libc:: kill ( self . pid , libc:: SIGINT ) } ) . map ( drop)
619
+ }
620
+ }
607
621
608
622
pub fn wait ( & mut self ) -> io:: Result < ExitStatus > {
609
623
use crate :: sys:: cvt_r;
Original file line number Diff line number Diff line change @@ -42,6 +42,10 @@ impl Process {
42
42
unsupported ( )
43
43
}
44
44
45
+ pub fn interrupt ( & mut self ) -> io:: Result < ( ) > {
46
+ unsupported ( )
47
+ }
48
+
45
49
pub fn wait ( & mut self ) -> io:: Result < ExitStatus > {
46
50
unsupported ( )
47
51
}
Original file line number Diff line number Diff line change @@ -150,6 +150,20 @@ impl Process {
150
150
}
151
151
}
152
152
153
+ pub fn interrupt ( & mut self ) -> io:: Result < ( ) > {
154
+ // If we've already waited on this process then the pid can be recycled
155
+ // and used for another process, and we probably shouldn't be interrupting
156
+ // random processes, so just return an error.
157
+ if self . status . is_some ( ) {
158
+ Err ( io:: const_io_error!(
159
+ ErrorKind :: InvalidInput ,
160
+ "invalid argument: can't kill an exited process" ,
161
+ ) )
162
+ } else {
163
+ cvt ( unsafe { libc:: kill ( self . pid , libc:: SIGINT ) } ) . map ( drop)
164
+ }
165
+ }
166
+
153
167
pub fn wait ( & mut self ) -> io:: Result < ExitStatus > {
154
168
use crate :: sys:: cvt_r;
155
169
if let Some ( status) = self . status {
Original file line number Diff line number Diff line change @@ -182,6 +182,10 @@ impl Process {
182
182
self . 0
183
183
}
184
184
185
+ pub fn interrupt ( & mut self ) -> io:: Result < ( ) > {
186
+ self . 0
187
+ }
188
+
185
189
pub fn wait ( & mut self ) -> io:: Result < ExitStatus > {
186
190
self . 0
187
191
}
You can’t perform that action at this time.
0 commit comments