From 894772fd48cf0ede4a1ce4a90aa7694674806200 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Mon, 3 Oct 2016 20:06:31 -0700 Subject: basic file descriptor passing --- src/main.rs | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index d87f037..d4221dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,28 +19,45 @@ extern crate getopts; extern crate log; extern crate env_logger; +extern crate nix; use std::env; use std::u64; use std::str::FromStr; -use std::{time, thread}; use std::process::exit; use std::process::Command; +use std::process::Child; +use std::net::SocketAddr; +use std::net::TcpListener; +use std::net::ToSocketAddrs; use getopts::Options; -fn run(prog: Vec, number: u64) { +use std::os::unix::io::IntoRawFd; +fn run(binds: Vec, mut prog: Command, number: u64) { + prog.env("EINHORN_FD_COUNT", binds.len().to_string()); + // This iterator destroys the TcpListeners + for (i, b) in binds.into_iter().enumerate() { + let orig_fd = b.into_raw_fd(); + // Duplicate, which also clears the CLOEXEC flag + //let fd = nix::fcntl::fcntl(nix::fcntl::FcntlArg::F_DUPFD(orig_fd)).unwrap(); + let fd = nix::unistd::dup(orig_fd).unwrap(); + println!("fd={} FD_CLOEXEC={}", fd, nix::fcntl::fcntl(fd, nix::fcntl::FcntlArg::F_GETFD).unwrap()); + prog.env(format!("EINHORN_FD_{}", i), fd.to_string()); + // NB: is fd getting destroyed here? + } + + let mut children: Vec = vec![]; for _ in 0..number { println!("Running!"); - Command::new(&prog[0]) - .args(&prog[1..]) - .spawn() - .expect("command failed"); + children.push(prog.spawn().expect("error spawning")); } - println!("Sleeping for 1sec"); - thread::sleep(time::Duration::from_secs(1)); + println!("Waiting for all children to die"); + for mut c in children { + c.wait().unwrap(); + } } fn print_usage(opts: Options) { @@ -54,6 +71,7 @@ fn main() { let args: Vec = env::args().collect(); let mut opts = Options::new(); + opts.parsing_style(getopts::ParsingStyle::StopAtFirstFree); opts.optflag("h", "help", "print this help menu"); opts.optflag("v", "verbose", "more debugging messages"); opts.optopt("n", "number", "how many program copies to spawn", "COUNT"); @@ -70,10 +88,16 @@ fn main() { } let number: u64 = match matches.opt_str("number") { - Some(n) => u64::from_str(&n).unwrap(), + Some(n) => u64::from_str(&n).expect("number arg should be an integer"), None => 1 }; + let sock_addrs: Vec = matches.opt_strs("bind").iter().map(|b| { + //let sa: SocketAddr = b.to_socket_addrs().unwrap().next().unwrap(); + //sa + b.to_socket_addrs().unwrap().next().unwrap() + }).collect(); + let program_and_args = if !matches.free.is_empty() { matches.free } else { @@ -88,5 +112,13 @@ fn main() { } builder.init().unwrap(); - run(program_and_args, number); + let binds: Vec = sock_addrs.iter().map(|sa| { + TcpListener::bind(sa).unwrap() + }).collect(); + + let mut prog = Command::new(&program_and_args[0]); + prog.args(&program_and_args[1..]); + + run(binds, prog, number); + exit(0); } -- cgit v1.2.3