aboutsummaryrefslogtreecommitdiffstats
path: root/src/command.rs
blob: e11df575e4bf0a27eb2d9dcc20fbf7536e13ed66 (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
59
60
61
62
use crate::error::*;

use std::os::unix::io::{AsRawFd as _, FromRawFd as _};
use std::os::unix::process::CommandExt as _;

pub trait Command {
    fn spawn_pty(&mut self) -> Result<Child>;
}

impl Command for std::process::Command {
    fn spawn_pty(&mut self) -> Result<Child> {
        let pty = crate::pty::Pty::new()?;
        let pts = pty.pts()?;

        let pt_fd = pty.pt().as_raw_fd();
        let pts_fd = pts.as_raw_fd();

        self.stdin(unsafe { std::process::Stdio::from_raw_fd(pts_fd) })
            .stdout(unsafe { std::process::Stdio::from_raw_fd(pts_fd) })
            .stderr(unsafe { std::process::Stdio::from_raw_fd(pts_fd) });

        unsafe {
            self.pre_exec(move || {
                // XXX unwrap
                nix::unistd::close(pt_fd)
                    .map_err(|e| e.as_errno().unwrap())?;
                nix::unistd::close(pts_fd)
                    .map_err(|e| e.as_errno().unwrap())?;
                Ok(())
            });
        }

        let child = self.spawn()?;

        Ok(Child { child, pty })
    }
}

pub struct Child {
    child: std::process::Child,
    pty: crate::pty::Pty,
}

impl Child {
    pub fn pty(&self) -> &std::fs::File {
        self.pty.pt()
    }
}

impl std::ops::Deref for Child {
    type Target = std::process::Child;

    fn deref(&self) -> &Self::Target {
        &self.child
    }
}

impl std::ops::DerefMut for Child {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.child
    }
}