]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/fds-are-cloexec.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / test / run-pass / fds-are-cloexec.rs
CommitLineData
9346a6ac
AL
1// Copyright 2015 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
11// ignore-windows
12// ignore-android
abe05a73 13// ignore-emscripten no processes
9e0c209e 14// ignore-haiku
9346a6ac
AL
15
16#![feature(libc)]
17
18extern crate libc;
19
20use std::env;
7453a54e 21use std::fs::File;
9346a6ac
AL
22use std::io;
23use std::net::{TcpListener, TcpStream, UdpSocket};
24use std::os::unix::prelude::*;
7453a54e 25use std::process::{Command, Stdio};
9346a6ac
AL
26use std::thread;
27
28fn main() {
29 let args = env::args().collect::<Vec<_>>();
30 if args.len() == 1 {
31 parent()
32 } else {
33 child(&args)
34 }
35}
36
37fn parent() {
8bb4bdeb 38 let file = File::open(env::current_exe().unwrap()).unwrap();
9346a6ac
AL
39 let tcp1 = TcpListener::bind("127.0.0.1:0").unwrap();
40 let tcp2 = tcp1.try_clone().unwrap();
41 let addr = tcp1.local_addr().unwrap();
42 let t = thread::spawn(move || TcpStream::connect(addr).unwrap());
43 let tcp3 = tcp1.accept().unwrap().0;
44 let tcp4 = t.join().unwrap();
45 let tcp5 = tcp3.try_clone().unwrap();
46 let tcp6 = tcp4.try_clone().unwrap();
47 let udp1 = UdpSocket::bind("127.0.0.1:0").unwrap();
48 let udp2 = udp1.try_clone().unwrap();
49
7453a54e
SL
50 let mut child = Command::new(env::args().next().unwrap())
51 .arg("100")
52 .stdout(Stdio::piped())
53 .stdin(Stdio::piped())
54 .stderr(Stdio::piped())
55 .spawn().unwrap();
56 let pipe1 = child.stdin.take().unwrap();
57 let pipe2 = child.stdout.take().unwrap();
58 let pipe3 = child.stderr.take().unwrap();
59
60
9346a6ac
AL
61 let status = Command::new(env::args().next().unwrap())
62 .arg(file.as_raw_fd().to_string())
63 .arg(tcp1.as_raw_fd().to_string())
64 .arg(tcp2.as_raw_fd().to_string())
65 .arg(tcp3.as_raw_fd().to_string())
66 .arg(tcp4.as_raw_fd().to_string())
67 .arg(tcp5.as_raw_fd().to_string())
68 .arg(tcp6.as_raw_fd().to_string())
69 .arg(udp1.as_raw_fd().to_string())
70 .arg(udp2.as_raw_fd().to_string())
7453a54e
SL
71 .arg(pipe1.as_raw_fd().to_string())
72 .arg(pipe2.as_raw_fd().to_string())
73 .arg(pipe3.as_raw_fd().to_string())
9346a6ac
AL
74 .status()
75 .unwrap();
76 assert!(status.success());
7453a54e 77 child.wait().unwrap();
9346a6ac
AL
78}
79
80fn child(args: &[String]) {
81 let mut b = [0u8; 2];
82 for arg in &args[1..] {
83 let fd: libc::c_int = arg.parse().unwrap();
84 unsafe {
85 assert_eq!(libc::read(fd, b.as_mut_ptr() as *mut _, 2), -1);
86 assert_eq!(io::Error::last_os_error().raw_os_error(),
87 Some(libc::EBADF));
88 }
89 }
90}