]> git.lizzy.rs Git - rust.git/blob - src/test/ui/fds-are-cloexec.rs
Auto merge of #75936 - sdroege:chunks-exact-construction-bounds-check, r=nagisa
[rust.git] / src / test / ui / fds-are-cloexec.rs
1 // run-pass
2 // ignore-windows
3 // ignore-android
4 // ignore-cloudabi no processes
5 // ignore-emscripten no processes
6 // ignore-haiku
7 // ignore-sgx no processes
8
9 #![feature(rustc_private)]
10
11 extern crate libc;
12
13 use std::env;
14 use std::fs::File;
15 use std::io;
16 use std::net::{TcpListener, TcpStream, UdpSocket};
17 use std::os::unix::prelude::*;
18 use std::process::{Command, Stdio};
19 use std::thread;
20
21 fn main() {
22     let args = env::args().collect::<Vec<_>>();
23     if args.len() == 1 {
24         parent()
25     } else {
26         child(&args)
27     }
28 }
29
30 fn parent() {
31     let file = File::open(env::current_exe().unwrap()).unwrap();
32     let tcp1 = TcpListener::bind("127.0.0.1:0").unwrap();
33     let tcp2 = tcp1.try_clone().unwrap();
34     let addr = tcp1.local_addr().unwrap();
35     let t = thread::spawn(move || TcpStream::connect(addr).unwrap());
36     let tcp3 = tcp1.accept().unwrap().0;
37     let tcp4 = t.join().unwrap();
38     let tcp5 = tcp3.try_clone().unwrap();
39     let tcp6 = tcp4.try_clone().unwrap();
40     let udp1 = UdpSocket::bind("127.0.0.1:0").unwrap();
41     let udp2 = udp1.try_clone().unwrap();
42
43     let mut child = Command::new(env::args().next().unwrap())
44                             .arg("100")
45                             .stdout(Stdio::piped())
46                             .stdin(Stdio::piped())
47                             .stderr(Stdio::piped())
48                             .spawn().unwrap();
49     let pipe1 = child.stdin.take().unwrap();
50     let pipe2 = child.stdout.take().unwrap();
51     let pipe3 = child.stderr.take().unwrap();
52
53
54     let status = Command::new(env::args().next().unwrap())
55                         .arg(file.as_raw_fd().to_string())
56                         .arg(tcp1.as_raw_fd().to_string())
57                         .arg(tcp2.as_raw_fd().to_string())
58                         .arg(tcp3.as_raw_fd().to_string())
59                         .arg(tcp4.as_raw_fd().to_string())
60                         .arg(tcp5.as_raw_fd().to_string())
61                         .arg(tcp6.as_raw_fd().to_string())
62                         .arg(udp1.as_raw_fd().to_string())
63                         .arg(udp2.as_raw_fd().to_string())
64                         .arg(pipe1.as_raw_fd().to_string())
65                         .arg(pipe2.as_raw_fd().to_string())
66                         .arg(pipe3.as_raw_fd().to_string())
67                         .status()
68                         .unwrap();
69     assert!(status.success());
70     child.wait().unwrap();
71 }
72
73 fn child(args: &[String]) {
74     let mut b = [0u8; 2];
75     for arg in &args[1..] {
76         let fd: libc::c_int = arg.parse().unwrap();
77         unsafe {
78             assert_eq!(libc::read(fd, b.as_mut_ptr() as *mut _, 2), -1);
79             assert_eq!(io::Error::last_os_error().raw_os_error(),
80                        Some(libc::EBADF));
81         }
82     }
83 }