File tree 5 files changed +56
-0
lines changed
5 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -527,6 +527,22 @@ impl Child {
527
527
}
528
528
}
529
529
530
+ /// Terminates the current process with the specified exit code.
531
+ ///
532
+ /// This function will never return and will immediately terminate the current
533
+ /// process. The exit code is passed through to the underlying OS and will be
534
+ /// available for consumption by another process.
535
+ ///
536
+ /// Note that because this function never returns, and that it terminates the
537
+ /// process, no destructors on the current stack or any other thread's stack
538
+ /// will be run. If a clean shutdown is needed it is recommended to only call
539
+ /// this function at a known point where there are no more destructors left
540
+ /// to run.
541
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
542
+ pub fn exit ( code : i32 ) -> ! {
543
+ :: sys:: os:: exit ( code)
544
+ }
545
+
530
546
#[ cfg( test) ]
531
547
mod tests {
532
548
use io:: ErrorKind ;
Original file line number Diff line number Diff line change @@ -505,3 +505,7 @@ pub fn home_dir() -> Option<PathBuf> {
505
505
}
506
506
}
507
507
}
508
+
509
+ pub fn exit ( code : i32 ) -> ! {
510
+ unsafe { libc:: exit ( code as c_int ) }
511
+ }
Original file line number Diff line number Diff line change @@ -433,6 +433,7 @@ extern "system" {
433
433
TokenHandle : * mut libc:: HANDLE ) -> libc:: BOOL ;
434
434
pub fn GetCurrentProcess ( ) -> libc:: HANDLE ;
435
435
pub fn GetStdHandle ( which : libc:: DWORD ) -> libc:: HANDLE ;
436
+ pub fn ExitProcess ( uExitCode : libc:: UINT ) -> !;
436
437
}
437
438
438
439
#[ link( name = "userenv" ) ]
Original file line number Diff line number Diff line change @@ -379,3 +379,7 @@ pub fn home_dir() -> Option<PathBuf> {
379
379
} , super :: os2path) . ok ( )
380
380
} )
381
381
}
382
+
383
+ pub fn exit ( code : i32 ) -> ! {
384
+ unsafe { libc:: ExitProcess ( code as libc:: UINT ) }
385
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2
+ // file at the top-level directory of this distribution and at
3
+ // http://rust-lang.org/COPYRIGHT.
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6
+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7
+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8
+ // option. This file may not be copied, modified, or distributed
9
+ // except according to those terms.
10
+
11
+ use std:: env;
12
+ use std:: process:: { self , Command , Stdio } ;
13
+
14
+ fn main ( ) {
15
+ let args: Vec < String > = env:: args ( ) . collect ( ) ;
16
+ if args. len ( ) > 1 && args[ 1 ] == "child" {
17
+ child ( ) ;
18
+ } else {
19
+ parent ( ) ;
20
+ }
21
+ }
22
+
23
+ fn parent ( ) {
24
+ let args: Vec < String > = env:: args ( ) . collect ( ) ;
25
+ let status = Command :: new ( & args[ 0 ] ) . arg ( "child" ) . status ( ) . unwrap ( ) ;
26
+ assert_eq ! ( status. code( ) , Some ( 2 ) ) ;
27
+ }
28
+
29
+ fn child ( ) -> i32 {
30
+ process:: exit ( 2 ) ;
31
+ }
You can’t perform that action at this time.
0 commit comments