Skip to content

libnative: process spawning should not close inherited file descriptors #16421

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
Aug 13, 2014
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
2 changes: 1 addition & 1 deletion src/libnative/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Process {
rtio::Ignored => { ret.push(None); Ok(None) }
rtio::InheritFd(fd) => {
ret.push(None);
Ok(Some(file::FileDesc::new(fd, true)))
Ok(Some(file::FileDesc::new(fd, false)))
}
rtio::CreatePipe(readable, _writable) => {
let (reader, writer) = try!(pipe());
Expand Down
25 changes: 24 additions & 1 deletion src/libstd/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ pub enum StdioContainer {
Ignored,

/// The specified file descriptor is inherited for the stream which it is
/// specified for.
/// specified for. Ownership of the file descriptor is *not* taken, so the
/// caller must clean it up.
InheritFd(libc::c_int),

/// Creates a pipe for the specified file descriptor which will be created
Expand Down Expand Up @@ -605,6 +606,7 @@ impl Drop for Process {

#[cfg(test)]
mod tests {
extern crate native;
use io::process::{Command, Process};
use prelude::*;

Expand Down Expand Up @@ -1017,4 +1019,25 @@ mod tests {
assert!(Process::kill(id, 0).is_ok());
assert!(Process::kill(id, PleaseExitSignal).is_ok());
})

iotest!(fn dont_close_fd_on_command_spawn() {
use std::rt::rtio::{Truncate, Write};
use native::io::file;

let path = if cfg!(windows) {
Path::new("NUL")
} else {
Path::new("/dev/null")
};

let mut fdes = match file::open(&path.to_c_str(), Truncate, Write) {
Ok(f) => f,
Err(_) => fail!("failed to open file descriptor"),
};

let mut cmd = pwd_cmd();
let _ = cmd.stdout(InheritFd(fdes.fd()));
assert!(cmd.status().unwrap().success());
assert!(fdes.inner_write("extra write\n".as_bytes()).is_ok());
})
}