|
| 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 | + |
| 13 | +#![feature(libc)] |
| 14 | + |
| 15 | +extern crate libc; |
| 16 | + |
| 17 | +use std::env; |
| 18 | +use std::fs::{self, File}; |
| 19 | +use std::io; |
| 20 | +use std::net::{TcpListener, TcpStream, UdpSocket}; |
| 21 | +use std::os::unix::prelude::*; |
| 22 | +use std::process::Command; |
| 23 | +use std::thread; |
| 24 | + |
| 25 | +fn main() { |
| 26 | + let args = env::args().collect::<Vec<_>>(); |
| 27 | + if args.len() == 1 { |
| 28 | + parent() |
| 29 | + } else { |
| 30 | + child(&args) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn parent() { |
| 35 | + let file = File::open("Makefile").unwrap(); |
| 36 | + let _dir = fs::read_dir("/").unwrap(); |
| 37 | + let tcp1 = TcpListener::bind("127.0.0.1:0").unwrap(); |
| 38 | + assert_eq!(tcp1.as_raw_fd(), file.as_raw_fd() + 2); |
| 39 | + let tcp2 = tcp1.try_clone().unwrap(); |
| 40 | + let addr = tcp1.local_addr().unwrap(); |
| 41 | + let t = thread::scoped(|| TcpStream::connect(addr).unwrap()); |
| 42 | + let tcp3 = tcp1.accept().unwrap().0; |
| 43 | + let tcp4 = t.join(); |
| 44 | + let tcp5 = tcp3.try_clone().unwrap(); |
| 45 | + let tcp6 = tcp4.try_clone().unwrap(); |
| 46 | + let udp1 = UdpSocket::bind("127.0.0.1:0").unwrap(); |
| 47 | + let udp2 = udp1.try_clone().unwrap(); |
| 48 | + |
| 49 | + let status = Command::new(env::args().next().unwrap()) |
| 50 | + .arg(file.as_raw_fd().to_string()) |
| 51 | + .arg((file.as_raw_fd() + 1).to_string()) |
| 52 | + .arg(tcp1.as_raw_fd().to_string()) |
| 53 | + .arg(tcp2.as_raw_fd().to_string()) |
| 54 | + .arg(tcp3.as_raw_fd().to_string()) |
| 55 | + .arg(tcp4.as_raw_fd().to_string()) |
| 56 | + .arg(tcp5.as_raw_fd().to_string()) |
| 57 | + .arg(tcp6.as_raw_fd().to_string()) |
| 58 | + .arg(udp1.as_raw_fd().to_string()) |
| 59 | + .arg(udp2.as_raw_fd().to_string()) |
| 60 | + .status() |
| 61 | + .unwrap(); |
| 62 | + assert!(status.success()); |
| 63 | +} |
| 64 | + |
| 65 | +fn child(args: &[String]) { |
| 66 | + let mut b = [0u8; 2]; |
| 67 | + for arg in &args[1..] { |
| 68 | + let fd: libc::c_int = arg.parse().unwrap(); |
| 69 | + unsafe { |
| 70 | + assert_eq!(libc::read(fd, b.as_mut_ptr() as *mut _, 2), -1); |
| 71 | + assert_eq!(io::Error::last_os_error().raw_os_error(), |
| 72 | + Some(libc::EBADF)); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments