Skip to content

Commit 71982aa

Browse files
committed
std: Add a process::exit function
This commit is an implementation of [RFC #1011][rfc] which adds an `exit` function to the standard library for immediately terminating the current process with a specified exit code. [rfc]: rust-lang/rfcs#1011
1 parent 80bf31d commit 71982aa

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

src/libstd/process.rs

+16
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,22 @@ impl Child {
527527
}
528528
}
529529

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+
530546
#[cfg(test)]
531547
mod tests {
532548
use io::ErrorKind;

src/libstd/sys/unix/os.rs

+4
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,7 @@ pub fn home_dir() -> Option<PathBuf> {
505505
}
506506
}
507507
}
508+
509+
pub fn exit(code: i32) -> ! {
510+
unsafe { libc::exit(code as c_int) }
511+
}

src/libstd/sys/windows/c.rs

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ extern "system" {
433433
TokenHandle: *mut libc::HANDLE) -> libc::BOOL;
434434
pub fn GetCurrentProcess() -> libc::HANDLE;
435435
pub fn GetStdHandle(which: libc::DWORD) -> libc::HANDLE;
436+
pub fn ExitProcess(uExitCode: libc::UINT) -> !;
436437
}
437438

438439
#[link(name = "userenv")]

src/libstd/sys/windows/os.rs

+4
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,7 @@ pub fn home_dir() -> Option<PathBuf> {
379379
}, super::os2path).ok()
380380
})
381381
}
382+
383+
pub fn exit(code: i32) -> ! {
384+
unsafe { libc::ExitProcess(code as libc::UINT) }
385+
}

src/test/run-pass/process-exit.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)