diff --git a/src/libstd/process.rs b/src/libstd/process.rs index b4bd513e8f0..5f29275df1f 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -527,6 +527,22 @@ impl Child { } } +/// Terminates the current process with the specified exit code. +/// +/// This function will never return and will immediately terminate the current +/// process. The exit code is passed through to the underlying OS and will be +/// available for consumption by another process. +/// +/// Note that because this function never returns, and that it terminates the +/// process, no destructors on the current stack or any other thread's stack +/// will be run. If a clean shutdown is needed it is recommended to only call +/// this function at a known point where there are no more destructors left +/// to run. +#[stable(feature = "rust1", since = "1.0.0")] +pub fn exit(code: i32) -> ! { + ::sys::os::exit(code) +} + #[cfg(test)] mod tests { use io::ErrorKind; diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index fab443feebd..2ff7eba5732 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -505,3 +505,7 @@ pub fn home_dir() -> Option { } } } + +pub fn exit(code: i32) -> ! { + unsafe { libc::exit(code as c_int) } +} diff --git a/src/libstd/sys/windows/c.rs b/src/libstd/sys/windows/c.rs index b9be4eb6bf5..b930e35c064 100644 --- a/src/libstd/sys/windows/c.rs +++ b/src/libstd/sys/windows/c.rs @@ -433,6 +433,7 @@ extern "system" { TokenHandle: *mut libc::HANDLE) -> libc::BOOL; pub fn GetCurrentProcess() -> libc::HANDLE; pub fn GetStdHandle(which: libc::DWORD) -> libc::HANDLE; + pub fn ExitProcess(uExitCode: libc::UINT) -> !; } #[link(name = "userenv")] diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index 167db1e8ac2..cbbce7f8f0a 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -379,3 +379,7 @@ pub fn home_dir() -> Option { }, super::os2path).ok() }) } + +pub fn exit(code: i32) -> ! { + unsafe { libc::ExitProcess(code as libc::UINT) } +} diff --git a/src/test/run-pass/process-exit.rs b/src/test/run-pass/process-exit.rs new file mode 100644 index 00000000000..9ef66ff2d71 --- /dev/null +++ b/src/test/run-pass/process-exit.rs @@ -0,0 +1,31 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::env; +use std::process::{self, Command, Stdio}; + +fn main() { + let args: Vec = env::args().collect(); + if args.len() > 1 && args[1] == "child" { + child(); + } else { + parent(); + } +} + +fn parent() { + let args: Vec = env::args().collect(); + let status = Command::new(&args[0]).arg("child").status().unwrap(); + assert_eq!(status.code(), Some(2)); +} + +fn child() -> i32 { + process::exit(2); +}