bors
d228cd3964
Auto merge of #30490 - ipetkov:unix-spawn, r=alexcrichton
* If the requested descriptors to inherit are stdio descriptors there
are situations where they will not be set correctly
* Example: parent's stdout --> child's stderr
parent's stderr --> child's stdout
* Solution: if the requested descriptors for the child are stdio
descriptors, `dup` them before overwriting the child's stdio
Example of a program which exhibits the bug:
```rust
// stdio.rs
use std::io::Write;
use std::io::{stdout, stderr};
use std::process::{Command, Stdio};
use std::os::unix::io::FromRawFd;
fn main() {
stdout().write_all("parent stdout\n".as_bytes()).unwrap();
stderr().write_all("parent stderr\n".as_bytes()).unwrap();
Command::new("sh")
.arg("-c")
.arg("echo 'child stdout'; echo 'child stderr' 1>&2")
.stdin(Stdio::inherit())
.stdout(unsafe { FromRawFd::from_raw_fd(2) })
.stderr(unsafe { FromRawFd::from_raw_fd(1) })
.status()
.unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
}
```
Before:
```
$ rustc --version
rustc 1.7.0-nightly (8ad12c3e2 2015-12-19)
$ rustc stdio.rs && ./stdio >out 2>err
$ cat out
parent stdout
$ cat err
parent stderr
child stdout
child stderr
```
After (expected):
```
$ rustc --version
rustc 1.7.0-dev (712eccee2 2015-12-19)
$ rustc stdio.rs && ./stdio >out 2>err
$ cat out
parent stdout
child stderr
$ cat err
parent stderr
child stdout
```