blob: 481804f8b2a1e4ab51bc83a7988cc402b699c07f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
use crate::error::*;
use std::os::unix::io::{AsRawFd as _, FromRawFd as _};
use std::os::unix::process::CommandExt as _;
pub struct Command {
pty: crate::pty::Pty,
command: std::process::Command,
slave_fh: Option<std::fs::File>,
}
impl Command {
pub fn new<S: AsRef<std::ffi::OsStr>>(program: S) -> Result<Self> {
let pty = crate::pty::Pty::new()?;
let master_fd = pty.master().as_raw_fd();
let slave_fh = pty.slave()?;
let slave_fd = slave_fh.as_raw_fd();
let mut command = std::process::Command::new(program);
command
.stdin(unsafe { std::process::Stdio::from_raw_fd(slave_fd) })
.stdout(unsafe { std::process::Stdio::from_raw_fd(slave_fd) })
.stderr(unsafe { std::process::Stdio::from_raw_fd(slave_fd) });
unsafe {
command.pre_exec(move || {
// XXX unwrap
nix::unistd::close(master_fd)
.map_err(|e| e.as_errno().unwrap())?;
nix::unistd::close(slave_fd)
.map_err(|e| e.as_errno().unwrap())?;
Ok(())
});
}
Ok(Self {
pty,
command,
slave_fh: Some(slave_fh),
})
}
pub fn pty(&self) -> &std::fs::File {
self.pty.master()
}
pub fn args<I, S>(&mut self, args: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
self.command.args(args);
self
}
pub fn spawn(&mut self) -> Result<std::process::Child> {
let child = self.command.spawn()?;
self.slave_fh = None;
Ok(child)
}
}
|