Skip to content

std: Add a process::exit function #23907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,7 @@ pub fn home_dir() -> Option<PathBuf> {
}
}
}

pub fn exit(code: i32) -> ! {
unsafe { libc::exit(code as c_int) }
}
1 change: 1 addition & 0 deletions src/libstd/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,7 @@ pub fn home_dir() -> Option<PathBuf> {
}, super::os2path).ok()
})
}

pub fn exit(code: i32) -> ! {
unsafe { libc::ExitProcess(code as libc::UINT) }
}
31 changes: 31 additions & 0 deletions src/test/run-pass/process-exit.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<String> = env::args().collect();
if args.len() > 1 && args[1] == "child" {
child();
} else {
parent();
}
}

fn parent() {
let args: Vec<String> = 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);
}