Skip to content

Commit 517f034

Browse files
authored
Expose a Session's control socket path (#41)
In some situations, it's handy to have access to a SSH session's control socket path. Therefore, expose it via a getter function. While there, adjust the variable names of native_mux_impl's and process_impl's Session variables of the control socket and its directory.
1 parent d6b6d2e commit 517f034

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ compile_error!("This crate can only be used on unix");
151151

152152
use std::borrow::Cow;
153153
use std::ffi::OsStr;
154+
use std::path::Path;
154155

155156
mod stdio;
156157
pub use stdio::{ChildStderr, ChildStdin, ChildStdout, Stdio};
@@ -291,6 +292,11 @@ impl Session {
291292
delegate!(&self.0, imp, { imp.check().await })
292293
}
293294

295+
/// Get the SSH connection's control socket path.
296+
pub fn control_socket(&self) -> &Path {
297+
delegate!(&self.0, imp, { imp.ctl() })
298+
}
299+
294300
/// Constructs a new [`Command`] for launching the program at path `program` on the remote
295301
/// host.
296302
///

src/native_mux_impl/session.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl Session {
3333
Ok(())
3434
}
3535

36+
pub(crate) fn ctl(&self) -> &Path {
37+
&self.ctl
38+
}
39+
3640
pub(crate) fn raw_command<S: AsRef<OsStr>>(&self, program: S) -> Command {
3741
Command::new(self.ctl.clone(), program.as_ref().as_bytes().into())
3842
}

src/process_impl/session.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ use tempfile::TempDir;
1212

1313
#[derive(Debug)]
1414
pub(crate) struct Session {
15-
ctl: Option<TempDir>,
16-
ctl_path: Box<Path>,
15+
tempdir: Option<TempDir>,
16+
ctl: Box<Path>,
1717
addr: Box<str>,
1818
master_log: Box<Path>,
1919
}
2020

2121
impl Session {
22-
pub(crate) fn new(ctl: TempDir, addr: &str) -> Self {
23-
let log = ctl.path().join("log").into_boxed_path();
24-
let ctl_path = ctl.path().join("master").into_boxed_path();
22+
pub(crate) fn new(tempdir: TempDir, addr: &str) -> Self {
23+
let log = tempdir.path().join("log").into_boxed_path();
24+
let ctl = tempdir.path().join("master").into_boxed_path();
2525

2626
Self {
27-
ctl: Some(ctl),
28-
ctl_path,
27+
tempdir: Some(tempdir),
28+
ctl,
2929
addr: addr.into(),
3030
master_log: log,
3131
}
@@ -35,7 +35,7 @@ impl Session {
3535
let mut cmd = std::process::Command::new("ssh");
3636
cmd.stdin(Stdio::null())
3737
.arg("-S")
38-
.arg(&*self.ctl_path)
38+
.arg(&*self.ctl)
3939
.arg("-o")
4040
.arg("BatchMode=yes")
4141
.args(args)
@@ -65,6 +65,10 @@ impl Session {
6565
}
6666
}
6767

68+
pub(crate) fn ctl(&self) -> &Path {
69+
&self.ctl
70+
}
71+
6872
pub(crate) fn raw_command<S: AsRef<OsStr>>(&self, program: S) -> Command {
6973
// XXX: Should we do a self.check() here first?
7074

@@ -117,8 +121,8 @@ impl Session {
117121
pub(crate) async fn close(mut self) -> Result<(), Error> {
118122
let mut exit_cmd = self.new_cmd(&["-O", "exit"]);
119123

120-
// Take self.ctl so that drop would do nothing
121-
let ctl = self.ctl.take().unwrap();
124+
// Take self.tempdir so that drop would do nothing
125+
let tempdir = self.tempdir.take().unwrap();
122126

123127
let exit = exit_cmd.output().await.map_err(Error::Ssh)?;
124128

@@ -148,7 +152,7 @@ impl Session {
148152
)));
149153
}
150154

151-
ctl.close().map_err(Error::Cleanup)?;
155+
tempdir.close().map_err(Error::Cleanup)?;
152156

153157
Ok(())
154158
}
@@ -184,8 +188,8 @@ impl Session {
184188
impl Drop for Session {
185189
fn drop(&mut self) {
186190
// Keep tempdir alive until the connection is established
187-
let _ctl = match self.ctl.take() {
188-
Some(ctl) => ctl,
191+
let _tempdir = match self.tempdir.take() {
192+
Some(tempdir) => tempdir,
189193
// return since close must have already been called.
190194
None => return,
191195
};

0 commit comments

Comments
 (0)