openssh/native_mux_impl/
child.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
use super::Error;

use std::io;
use std::os::unix::process::ExitStatusExt;
use std::process::ExitStatus;

use openssh_mux_client::{EstablishedSession, SessionStatus};

#[derive(Debug)]
pub(crate) struct RemoteChild {
    established_session: EstablishedSession,
}

impl RemoteChild {
    pub(crate) fn new(established_session: EstablishedSession) -> Self {
        Self {
            established_session,
        }
    }

    pub(crate) async fn disconnect(self) -> io::Result<()> {
        // ssh multiplex protocol does not specify any message type
        // that can be used to kill the remote process or properly shutdown
        // the connection.
        //
        // So here we just let the drop handler does its job to release
        // underlying resources such as unix stream socket and heap memory allocated,
        // the remote process is not killed.
        Ok(())
    }

    pub(crate) async fn wait(self) -> Result<ExitStatus, Error> {
        let session_status = self
            .established_session
            .wait()
            .await
            .map_err(|(err, _established_session)| err)?;

        match session_status {
            SessionStatus::TtyAllocFail(_established_session) => {
                unreachable!("native_mux_impl never allocates a tty")
            }
            SessionStatus::Exited { exit_value } => {
                if let Some(val) = exit_value {
                    if val == 127 {
                        Err(Error::Remote(io::Error::new(
                            io::ErrorKind::NotFound,
                            "remote command not found",
                        )))
                    } else {
                        Ok(ExitStatusExt::from_raw((val as i32) << 8))
                    }
                } else {
                    Err(Error::RemoteProcessTerminated)
                }
            }
        }
    }
}