about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-05-27 12:08:27 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-05-27 17:49:31 -0700
commit034eb4e724bf138bd2d90268cd0e759f99f0d396 (patch)
tree5941c72b81bc4a09adcf0173d5d6a9c1b9895b5b /src/test
parent746d086f9322d24fa7b389dd911e204ca35012ae (diff)
downloadrust-034eb4e724bf138bd2d90268cd0e759f99f0d396.tar.gz
rust-034eb4e724bf138bd2d90268cd0e759f99f0d396.zip
native: Ignore stdio fds with /dev/null
When spawning a process, stdio file descriptors can be configured to be ignored,
which basically means that they'll be closed. Currently this is done by
literally closing the file descriptors in the child, but this can have adverse
side effects if the child process then opens a new file descriptor, assigning it
to a stdio number.

To work around the problems of the child, this commit alters the process
spawning code to map stdio fds to /dev/null on unix (and a similar equivalent on
windows) when they are specified as being ignored. This should allow spawned
programs to have more expected behavior when opening new files.

Closes #14456
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass/issue-14456.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/test/run-pass/issue-14456.rs b/src/test/run-pass/issue-14456.rs
new file mode 100644
index 00000000000..79203503373
--- /dev/null
+++ b/src/test/run-pass/issue-14456.rs
@@ -0,0 +1,55 @@
+// Copyright 2014 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.
+
+#![feature(phase)]
+
+#[phase(syntax, link)]
+extern crate green;
+extern crate native;
+
+use std::io::process;
+use std::io::Command;
+use std::io;
+use std::os;
+
+green_start!(main)
+
+fn main() {
+    let args = os::args();
+    if args.len() > 1 && args.get(1).as_slice() == "child" {
+        return child()
+    }
+
+    test();
+
+    let (tx, rx) = channel();
+    native::task::spawn(proc() {
+        tx.send(test());
+    });
+    rx.recv();
+
+}
+
+fn child() {
+    io::stdout().write_line("foo").unwrap();
+    io::stderr().write_line("bar").unwrap();
+    assert_eq!(io::stdin().read_line().err().unwrap().kind, io::EndOfFile);
+}
+
+fn test() {
+    let args = os::args();
+    let mut p = Command::new(args.get(0).as_slice()).arg("child")
+                                     .stdin(process::Ignored)
+                                     .stdout(process::Ignored)
+                                     .stderr(process::Ignored)
+                                     .spawn().unwrap();
+    assert!(p.wait().unwrap().success());
+}
+