]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/no-stdio.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / test / run-pass / no-stdio.rs
CommitLineData
7453a54e
SL
1// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
5bcae85e
SL
11// ignore-emscripten
12
7453a54e
SL
13#![feature(libc)]
14
15extern crate libc;
16
17use std::process::{Command, Stdio};
18use std::env;
19use std::io::{self, Read, Write};
20
21#[cfg(unix)]
22unsafe fn without_stdio<R, F: FnOnce() -> R>(f: F) -> R {
23 let doit = |a| {
24 let r = libc::dup(a);
25 assert!(r >= 0);
26 return r
27 };
28 let a = doit(0);
29 let b = doit(1);
30 let c = doit(2);
31
32 assert!(libc::close(0) >= 0);
33 assert!(libc::close(1) >= 0);
34 assert!(libc::close(2) >= 0);
35
36 let r = f();
37
38 assert!(libc::dup2(a, 0) >= 0);
39 assert!(libc::dup2(b, 1) >= 0);
40 assert!(libc::dup2(c, 2) >= 0);
41
42 return r
43}
44
45#[cfg(windows)]
46unsafe fn without_stdio<R, F: FnOnce() -> R>(f: F) -> R {
47 type DWORD = u32;
48 type HANDLE = *mut u8;
49 type BOOL = i32;
50
51 const STD_INPUT_HANDLE: DWORD = -10i32 as DWORD;
52 const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
53 const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
54 const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
55
56 extern "system" {
57 fn GetStdHandle(which: DWORD) -> HANDLE;
58 fn SetStdHandle(which: DWORD, handle: HANDLE) -> BOOL;
59 }
60
61 let doit = |id| {
62 let handle = GetStdHandle(id);
63 assert!(handle != INVALID_HANDLE_VALUE);
64 assert!(SetStdHandle(id, INVALID_HANDLE_VALUE) != 0);
65 return handle
66 };
67
68 let a = doit(STD_INPUT_HANDLE);
69 let b = doit(STD_OUTPUT_HANDLE);
70 let c = doit(STD_ERROR_HANDLE);
71
72 let r = f();
73
74 let doit = |id, handle| {
75 assert!(SetStdHandle(id, handle) != 0);
76 };
77 doit(STD_INPUT_HANDLE, a);
78 doit(STD_OUTPUT_HANDLE, b);
79 doit(STD_ERROR_HANDLE, c);
80
81 return r
82}
83
84fn main() {
85 if env::args().len() > 1 {
86 println!("test");
87 assert!(io::stdout().write(b"test\n").is_ok());
88 assert!(io::stderr().write(b"test\n").is_ok());
89 assert_eq!(io::stdin().read(&mut [0; 10]).unwrap(), 0);
90 return
91 }
92
93 // First, make sure reads/writes without stdio work if stdio itself is
94 // missing.
95 let (a, b, c) = unsafe {
96 without_stdio(|| {
97 let a = io::stdout().write(b"test\n");
98 let b = io::stderr().write(b"test\n");
99 let c = io::stdin().read(&mut [0; 10]);
100
101 (a, b, c)
102 })
103 };
104
105 assert_eq!(a.unwrap(), 5);
106 assert_eq!(b.unwrap(), 5);
107 assert_eq!(c.unwrap(), 0);
108
109 // Second, spawn a child and do some work with "null" descriptors to make
110 // sure it's ok
111 let me = env::current_exe().unwrap();
112 let status = Command::new(&me)
113 .arg("next")
114 .stdin(Stdio::null())
115 .stdout(Stdio::null())
116 .stderr(Stdio::null())
117 .status().unwrap();
118 assert!(status.success(), "{:?} isn't a success", status);
119
120 // Finally, close everything then spawn a child to make sure everything is
121 // *still* ok.
122 let status = unsafe {
123 without_stdio(|| Command::new(&me).arg("next").status())
124 }.unwrap();
125 assert!(status.success(), "{:?} isn't a success", status);
126}