forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#58059 - RalfJung:before_exec, r=alexcrichton
deprecate before_exec in favor of unsafe pre_exec Fixes rust-lang#39575 As per the [lang team decision](rust-lang#39575 (comment)): > The language team agreed that before_exec should be unsafe, and leaves the details of a transition plan to the libs team. Cc @alexcrichton @rust-lang/libs how would you like to proceed?
- Loading branch information
Showing
6 changed files
with
171 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#![allow(stable_features)] | ||
// ignore-windows - this is a unix-specific test | ||
// ignore-cloudabi no processes | ||
// ignore-emscripten no processes | ||
#![feature(process_exec, rustc_private)] | ||
|
||
extern crate libc; | ||
|
||
use std::env; | ||
use std::io::Error; | ||
use std::os::unix::process::CommandExt; | ||
use std::process::Command; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::sync::Arc; | ||
|
||
fn main() { | ||
if let Some(arg) = env::args().nth(1) { | ||
match &arg[..] { | ||
"test1" => println!("hello2"), | ||
"test2" => assert_eq!(env::var("FOO").unwrap(), "BAR"), | ||
"test3" => assert_eq!(env::current_dir().unwrap().to_str().unwrap(), "/"), | ||
"empty" => {} | ||
_ => panic!("unknown argument: {}", arg), | ||
} | ||
return; | ||
} | ||
|
||
let me = env::current_exe().unwrap(); | ||
|
||
let output = unsafe { | ||
Command::new(&me) | ||
.arg("test1") | ||
.pre_exec(|| { | ||
println!("hello"); | ||
Ok(()) | ||
}) | ||
.output() | ||
.unwrap() | ||
}; | ||
assert!(output.status.success()); | ||
assert!(output.stderr.is_empty()); | ||
assert_eq!(output.stdout, b"hello\nhello2\n"); | ||
|
||
let output = unsafe { | ||
Command::new(&me) | ||
.arg("test2") | ||
.pre_exec(|| { | ||
env::set_var("FOO", "BAR"); | ||
Ok(()) | ||
}) | ||
.output() | ||
.unwrap() | ||
}; | ||
assert!(output.status.success()); | ||
assert!(output.stderr.is_empty()); | ||
assert!(output.stdout.is_empty()); | ||
|
||
let output = unsafe { | ||
Command::new(&me) | ||
.arg("test3") | ||
.pre_exec(|| { | ||
env::set_current_dir("/").unwrap(); | ||
Ok(()) | ||
}) | ||
.output() | ||
.unwrap() | ||
}; | ||
assert!(output.status.success()); | ||
assert!(output.stderr.is_empty()); | ||
assert!(output.stdout.is_empty()); | ||
|
||
let output = unsafe { | ||
Command::new(&me) | ||
.arg("bad") | ||
.pre_exec(|| Err(Error::from_raw_os_error(102))) | ||
.output() | ||
.unwrap_err() | ||
}; | ||
assert_eq!(output.raw_os_error(), Some(102)); | ||
|
||
let pid = unsafe { libc::getpid() }; | ||
assert!(pid >= 0); | ||
let output = unsafe { | ||
Command::new(&me) | ||
.arg("empty") | ||
.pre_exec(move || { | ||
let child = libc::getpid(); | ||
assert!(child >= 0); | ||
assert!(pid != child); | ||
Ok(()) | ||
}) | ||
.output() | ||
.unwrap() | ||
}; | ||
assert!(output.status.success()); | ||
assert!(output.stderr.is_empty()); | ||
assert!(output.stdout.is_empty()); | ||
|
||
let mem = Arc::new(AtomicUsize::new(0)); | ||
let mem2 = mem.clone(); | ||
let output = unsafe { | ||
Command::new(&me) | ||
.arg("empty") | ||
.pre_exec(move || { | ||
assert_eq!(mem2.fetch_add(1, Ordering::SeqCst), 0); | ||
Ok(()) | ||
}) | ||
.output() | ||
.unwrap() | ||
}; | ||
assert!(output.status.success()); | ||
assert!(output.stderr.is_empty()); | ||
assert!(output.stdout.is_empty()); | ||
assert_eq!(mem.load(Ordering::SeqCst), 0); | ||
} |