2015-04-03 22:30:10 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
|
|
// ignore-windows
|
2015-04-03 22:44:14 +00:00
|
|
|
// ignore-android
|
2016-02-11 11:34:41 +00:00
|
|
|
// ignore-emscripten
|
2016-09-25 04:42:50 +00:00
|
|
|
// ignore-haiku
|
2015-04-03 22:30:10 +00:00
|
|
|
|
|
|
|
#![feature(libc)]
|
|
|
|
|
|
|
|
extern crate libc;
|
|
|
|
|
|
|
|
use std::env;
|
2016-02-04 23:23:26 +00:00
|
|
|
use std::fs::File;
|
2015-04-03 22:30:10 +00:00
|
|
|
use std::io;
|
|
|
|
use std::net::{TcpListener, TcpStream, UdpSocket};
|
|
|
|
use std::os::unix::prelude::*;
|
2016-02-04 23:23:26 +00:00
|
|
|
use std::process::{Command, Stdio};
|
2015-04-03 22:30:10 +00:00
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = env::args().collect::<Vec<_>>();
|
|
|
|
if args.len() == 1 {
|
|
|
|
parent()
|
|
|
|
} else {
|
|
|
|
child(&args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent() {
|
2017-01-28 21:38:06 +00:00
|
|
|
let file = File::open(env::current_exe().unwrap()).unwrap();
|
2015-04-03 22:30:10 +00:00
|
|
|
let tcp1 = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
|
|
let tcp2 = tcp1.try_clone().unwrap();
|
|
|
|
let addr = tcp1.local_addr().unwrap();
|
2015-04-13 22:15:32 +00:00
|
|
|
let t = thread::spawn(move || TcpStream::connect(addr).unwrap());
|
2015-04-03 22:30:10 +00:00
|
|
|
let tcp3 = tcp1.accept().unwrap().0;
|
2015-04-13 22:15:32 +00:00
|
|
|
let tcp4 = t.join().unwrap();
|
2015-04-03 22:30:10 +00:00
|
|
|
let tcp5 = tcp3.try_clone().unwrap();
|
|
|
|
let tcp6 = tcp4.try_clone().unwrap();
|
|
|
|
let udp1 = UdpSocket::bind("127.0.0.1:0").unwrap();
|
|
|
|
let udp2 = udp1.try_clone().unwrap();
|
|
|
|
|
2016-02-04 23:23:26 +00:00
|
|
|
let mut child = Command::new(env::args().next().unwrap())
|
|
|
|
.arg("100")
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.spawn().unwrap();
|
|
|
|
let pipe1 = child.stdin.take().unwrap();
|
|
|
|
let pipe2 = child.stdout.take().unwrap();
|
|
|
|
let pipe3 = child.stderr.take().unwrap();
|
|
|
|
|
|
|
|
|
2015-04-03 22:30:10 +00:00
|
|
|
let status = Command::new(env::args().next().unwrap())
|
|
|
|
.arg(file.as_raw_fd().to_string())
|
|
|
|
.arg(tcp1.as_raw_fd().to_string())
|
|
|
|
.arg(tcp2.as_raw_fd().to_string())
|
|
|
|
.arg(tcp3.as_raw_fd().to_string())
|
|
|
|
.arg(tcp4.as_raw_fd().to_string())
|
|
|
|
.arg(tcp5.as_raw_fd().to_string())
|
|
|
|
.arg(tcp6.as_raw_fd().to_string())
|
|
|
|
.arg(udp1.as_raw_fd().to_string())
|
|
|
|
.arg(udp2.as_raw_fd().to_string())
|
2016-02-04 23:23:26 +00:00
|
|
|
.arg(pipe1.as_raw_fd().to_string())
|
|
|
|
.arg(pipe2.as_raw_fd().to_string())
|
|
|
|
.arg(pipe3.as_raw_fd().to_string())
|
2015-04-03 22:30:10 +00:00
|
|
|
.status()
|
|
|
|
.unwrap();
|
|
|
|
assert!(status.success());
|
2016-02-04 23:23:26 +00:00
|
|
|
child.wait().unwrap();
|
2015-04-03 22:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn child(args: &[String]) {
|
|
|
|
let mut b = [0u8; 2];
|
|
|
|
for arg in &args[1..] {
|
|
|
|
let fd: libc::c_int = arg.parse().unwrap();
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(libc::read(fd, b.as_mut_ptr() as *mut _, 2), -1);
|
|
|
|
assert_eq!(io::Error::last_os_error().raw_os_error(),
|
|
|
|
Some(libc::EBADF));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|