2018-12-21 07:08:00 +00:00
|
|
|
use crate::common::file_descriptor::FileDescriptor;
|
2018-12-11 01:06:55 +00:00
|
|
|
use libc;
|
2018-12-21 05:50:24 +00:00
|
|
|
use std::io::BufReader;
|
2018-12-15 06:46:11 +00:00
|
|
|
use std::io::Read;
|
2018-12-11 01:06:55 +00:00
|
|
|
|
|
|
|
// A struct to hold the references to the base stdout and the captured one
|
|
|
|
pub struct StdioCapturer {
|
|
|
|
stdout_backup: libc::c_int,
|
|
|
|
stderr_backup: libc::c_int,
|
|
|
|
stdout_reader: libc::c_int,
|
|
|
|
stderr_reader: libc::c_int,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation inspired in
|
|
|
|
// https://github.com/rust-lang/rust/blob/7d52cbce6db83e4fc2d8706b4e4b9c7da76cbcf8/src/test/run-pass/issues/issue-30490.rs
|
|
|
|
// Currently only works in Unix systems (Mac, Linux)
|
|
|
|
impl StdioCapturer {
|
|
|
|
fn pipe() -> (libc::c_int, libc::c_int) {
|
|
|
|
let mut fds = [0; 2];
|
2018-12-21 05:50:24 +00:00
|
|
|
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
2018-12-11 01:06:55 +00:00
|
|
|
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
|
2018-12-21 05:50:24 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
2018-12-21 07:08:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
unsafe { libc::pipe(fds.as_mut_ptr(), 1000, libc::O_TEXT) },
|
|
|
|
0
|
|
|
|
);
|
2018-12-21 05:50:24 +00:00
|
|
|
|
2018-12-11 01:06:55 +00:00
|
|
|
(fds[0], fds[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let stdout_backup = unsafe { libc::dup(libc::STDOUT_FILENO) };
|
|
|
|
let stderr_backup = unsafe { libc::dup(libc::STDERR_FILENO) };
|
2018-12-11 03:19:46 +00:00
|
|
|
|
2018-12-11 01:06:55 +00:00
|
|
|
let (stdout_reader, stdout_writer) = Self::pipe();
|
|
|
|
let (stderr_reader, stderr_writer) = Self::pipe();
|
2018-12-11 03:19:46 +00:00
|
|
|
|
2018-12-11 01:06:55 +00:00
|
|
|
assert!(unsafe { libc::dup2(stdout_writer, libc::STDOUT_FILENO) } > -1);
|
|
|
|
assert!(unsafe { libc::dup2(stderr_writer, libc::STDERR_FILENO) } > -1);
|
|
|
|
|
|
|
|
// Make sure we close any duplicates of the writer end of the pipe,
|
|
|
|
// otherwise we can get stuck reading from the pipe which has open
|
|
|
|
// writers but no one supplying any input
|
|
|
|
assert_eq!(unsafe { libc::close(stdout_writer) }, 0);
|
|
|
|
assert_eq!(unsafe { libc::close(stderr_writer) }, 0);
|
|
|
|
|
|
|
|
StdioCapturer {
|
|
|
|
stdout_backup,
|
|
|
|
stderr_backup,
|
|
|
|
stdout_reader,
|
2018-12-15 06:46:11 +00:00
|
|
|
stderr_reader,
|
2018-12-11 01:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 05:50:24 +00:00
|
|
|
pub fn end(self) -> Result<(String, String), std::io::Error> {
|
2018-12-11 01:06:55 +00:00
|
|
|
// The Stdio passed into the Command took over (and closed) std{out, err}
|
|
|
|
// so we should restore them as they were.
|
|
|
|
|
|
|
|
assert!(unsafe { libc::dup2(self.stdout_backup, libc::STDOUT_FILENO) } > -1);
|
|
|
|
assert!(unsafe { libc::dup2(self.stderr_backup, libc::STDERR_FILENO) } > -1);
|
|
|
|
|
2018-12-21 05:50:24 +00:00
|
|
|
let fd = FileDescriptor::new(self.stdout_reader);
|
|
|
|
let mut reader = BufReader::new(fd);
|
|
|
|
let mut stdout_read = "".to_string();
|
|
|
|
let _ = reader.read_to_string(&mut stdout_read)?;
|
2018-12-11 01:06:55 +00:00
|
|
|
|
2018-12-21 05:50:24 +00:00
|
|
|
let fd = FileDescriptor::new(self.stderr_reader);
|
|
|
|
let mut reader = BufReader::new(fd);
|
|
|
|
let mut stderr_read = "".to_string();
|
|
|
|
let _ = reader.read_to_string(&mut stderr_read)?;
|
2018-12-11 01:06:55 +00:00
|
|
|
|
2018-12-21 05:50:24 +00:00
|
|
|
Ok((stdout_read, stderr_read))
|
2018-12-11 01:06:55 +00:00
|
|
|
}
|
|
|
|
}
|