openssh/native_mux_impl/
command.rs

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use super::Error;
use super::RemoteChild;
use super::{ChildStderr, ChildStdin, ChildStdout, Stdio};

use std::borrow::Cow;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;

use openssh_mux_client::{Connection, NonZeroByteSlice, Session};

#[derive(Debug)]
pub(crate) struct Command {
    cmd: Vec<u8>,
    ctl: Box<Path>,
    subsystem: bool,

    stdin_v: Stdio,
    stdout_v: Stdio,
    stderr_v: Stdio,
}

impl Command {
    pub(crate) fn new(ctl: Box<Path>, cmd: Vec<u8>, subsystem: bool) -> Self {
        Self {
            cmd,
            ctl,
            subsystem,

            stdin_v: Stdio::inherit(),
            stdout_v: Stdio::inherit(),
            stderr_v: Stdio::inherit(),
        }
    }

    pub(crate) fn raw_arg<S: AsRef<OsStr>>(&mut self, arg: S) {
        self.cmd.push(b' ');
        self.cmd.extend_from_slice(arg.as_ref().as_bytes());
    }

    pub(crate) fn stdin<T: Into<Stdio>>(&mut self, cfg: T) {
        self.stdin_v = cfg.into();
    }

    pub(crate) fn stdout<T: Into<Stdio>>(&mut self, cfg: T) {
        self.stdout_v = cfg.into();
    }

    pub(crate) fn stderr<T: Into<Stdio>>(&mut self, cfg: T) {
        self.stderr_v = cfg.into();
    }

    pub(crate) async fn spawn(
        &mut self,
    ) -> Result<
        (
            RemoteChild,
            Option<ChildStdin>,
            Option<ChildStdout>,
            Option<ChildStderr>,
        ),
        Error,
    > {
        let (stdin, child_stdin) = self.stdin_v.to_stdin()?;
        let (stdout, child_stdout) = self.stdout_v.to_stdout()?;
        let (stderr, child_stderr) = self.stderr_v.to_stderr()?;

        let stdios = [
            stdin.as_raw_fd_or_null_fd()?,
            stdout.as_raw_fd_or_null_fd()?,
            stderr.as_raw_fd_or_null_fd()?,
        ];

        let cmd = NonZeroByteSlice::new(&self.cmd).ok_or(Error::InvalidCommand)?;

        let session = Session::builder()
            .cmd(Cow::Borrowed(cmd))
            .subsystem(self.subsystem)
            .build();

        let established_session = Connection::connect(&self.ctl)
            .await?
            .open_new_session(&session, &stdios)
            .await?;

        Ok((
            RemoteChild::new(established_session),
            child_stdin,
            child_stdout,
            child_stderr,
        ))
    }
}