Skip to content

Commit

Permalink
Couple of changes:
Browse files Browse the repository at this point in the history
1. Add support to detach processes from distant server
2. Refactor distant_bin and distant_args to distant.bin and distant.args
3. Add use_login_shell option for launch distant opts
  • Loading branch information
chipsenkbeil committed Oct 10, 2021
1 parent bd526c9 commit 043ae6c
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 65 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ opt-level = 'z'
lto = true
codegen-units = 1

[patch.crates-io]
ssh2 = { git = "https://github.com/wez/ssh2-rs.git", branch="win32ssl" }

[features]
default = ["ssh2"]
ssh2 = ["distant-ssh2"]
Expand Down
4 changes: 3 additions & 1 deletion distant-core/src/client/lsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ impl RemoteLspProcess {
channel: SessionChannel,
cmd: impl Into<String>,
args: Vec<String>,
detached: bool,
) -> Result<Self, RemoteProcessError> {
let mut inner = RemoteProcess::spawn(tenant, channel, cmd, args).await?;
let mut inner = RemoteProcess::spawn(tenant, channel, cmd, args, detached).await?;
let stdin = inner.stdin.take().map(RemoteLspStdin::new);
let stdout = inner.stdout.take().map(RemoteLspStdout::new);
let stderr = inner.stderr.take().map(RemoteLspStderr::new);
Expand Down Expand Up @@ -324,6 +325,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down
17 changes: 16 additions & 1 deletion distant-core/src/client/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl RemoteProcess {
mut channel: SessionChannel,
cmd: impl Into<String>,
args: Vec<String>,
detached: bool,
) -> Result<Self, RemoteProcessError> {
let tenant = tenant.into();
let cmd = cmd.into();
Expand All @@ -75,7 +76,11 @@ impl RemoteProcess {
let mut mailbox = channel
.mail(Request::new(
tenant.as_str(),
vec![RequestData::ProcRun { cmd, args }],
vec![RequestData::ProcRun {
cmd,
args,
detached,
}],
))
.await?;

Expand Down Expand Up @@ -365,6 +370,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -403,6 +409,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -448,6 +455,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -493,6 +501,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -549,6 +558,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -604,6 +614,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -653,6 +664,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -702,6 +714,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -744,6 +757,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down Expand Up @@ -793,6 +807,7 @@ mod tests {
session.clone_channel(),
String::from("cmd"),
vec![String::from("arg")],
false,
)
.await
});
Expand Down
12 changes: 10 additions & 2 deletions distant-core/src/client/session/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub trait SessionChannelExt {
tenant: impl Into<String>,
cmd: impl Into<String>,
args: Vec<impl Into<String>>,
detached: bool,
) -> AsyncReturn<'_, RemoteProcess, RemoteProcessError>;

/// Spawns an LSP process on the remote machine
Expand All @@ -129,6 +130,7 @@ pub trait SessionChannelExt {
tenant: impl Into<String>,
cmd: impl Into<String>,
args: Vec<impl Into<String>>,
detached: bool,
) -> AsyncReturn<'_, RemoteLspProcess, RemoteProcessError>;

/// Retrieves information about the remote system
Expand Down Expand Up @@ -367,23 +369,29 @@ impl SessionChannelExt for SessionChannel {
tenant: impl Into<String>,
cmd: impl Into<String>,
args: Vec<impl Into<String>>,
detached: bool,
) -> AsyncReturn<'_, RemoteProcess, RemoteProcessError> {
let tenant = tenant.into();
let cmd = cmd.into();
let args = args.into_iter().map(Into::into).collect();
Box::pin(async move { RemoteProcess::spawn(tenant, self.clone(), cmd, args).await })
Box::pin(
async move { RemoteProcess::spawn(tenant, self.clone(), cmd, args, detached).await },
)
}

fn spawn_lsp(
&mut self,
tenant: impl Into<String>,
cmd: impl Into<String>,
args: Vec<impl Into<String>>,
detached: bool,
) -> AsyncReturn<'_, RemoteLspProcess, RemoteProcessError> {
let tenant = tenant.into();
let cmd = cmd.into();
let args = args.into_iter().map(Into::into).collect();
Box::pin(async move { RemoteLspProcess::spawn(tenant, self.clone(), cmd, args).await })
Box::pin(
async move { RemoteLspProcess::spawn(tenant, self.clone(), cmd, args, detached).await },
)
}

fn system_info(&mut self, tenant: impl Into<String>) -> AsyncReturn<'_, SystemInfo> {
Expand Down
8 changes: 8 additions & 0 deletions distant-core/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ pub enum RequestData {

/// Arguments for the command
args: Vec<String>,

/// Whether or not the process should be detached, meaning that the process will not be
/// killed when the associated client disconnects
#[cfg_attr(feature = "structopt", structopt(long))]
detached: bool,
},

/// Kills a process running on the remote machine
Expand Down Expand Up @@ -465,6 +470,9 @@ pub struct RunningProcess {
/// Arguments for the command
pub args: Vec<String>,

/// Whether or not the process was run in detached mode
pub detached: bool,

/// Arbitrary id associated with running process
///
/// Not the same as the process' pid!
Expand Down
20 changes: 18 additions & 2 deletions distant-core/src/server/distant/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ pub(super) async fn process(
canonicalize,
resolve_file_type,
} => metadata(path, canonicalize, resolve_file_type).await,
RequestData::ProcRun { cmd, args } => proc_run(conn_id, state, reply, cmd, args).await,
RequestData::ProcRun {
cmd,
args,
detached,
} => proc_run(conn_id, state, reply, cmd, args, detached).await,
RequestData::ProcKill { id } => proc_kill(state, id).await,
RequestData::ProcStdin { id, data } => proc_stdin(state, id, data).await,
RequestData::ProcList {} => proc_list(state).await,
Expand Down Expand Up @@ -424,6 +428,7 @@ async fn proc_run<F>(
reply: F,
cmd: String,
args: Vec<String>,
detached: bool,
) -> Result<Outgoing, ServerError>
where
F: FnMut(Vec<ResponseData>) -> ReplyRet + Clone + Send + 'static,
Expand All @@ -439,7 +444,7 @@ where
state
.lock()
.await
.push_process(conn_id, Process::new(id, cmd, args));
.push_process(conn_id, Process::new(id, cmd, args, detached));

let post_hook = Box::new(move |mut state_lock: MutexGuard<'_, State>| {
// Spawn a task that sends stdout as a response
Expand Down Expand Up @@ -656,6 +661,7 @@ async fn proc_list(state: HState) -> Result<Outgoing, ServerError> {
.map(|p| RunningProcess {
cmd: p.cmd.to_string(),
args: p.args.clone(),
detached: p.detached,
id: p.id,
})
.collect(),
Expand Down Expand Up @@ -2096,6 +2102,7 @@ mod tests {
vec![RequestData::ProcRun {
cmd: DOES_NOT_EXIST_BIN.to_str().unwrap().to_string(),
args: Vec::new(),
detached: false,
}],
);

Expand All @@ -2121,6 +2128,7 @@ mod tests {
vec![RequestData::ProcRun {
cmd: SCRIPT_RUNNER.to_string(),
args: vec![ECHO_ARGS_TO_STDOUT_SH.to_str().unwrap().to_string()],
detached: false,
}],
);

Expand Down Expand Up @@ -2153,6 +2161,7 @@ mod tests {
ECHO_ARGS_TO_STDOUT_SH.to_str().unwrap().to_string(),
String::from("some stdout"),
],
detached: false,
}],
);

Expand Down Expand Up @@ -2218,6 +2227,7 @@ mod tests {
ECHO_ARGS_TO_STDERR_SH.to_str().unwrap().to_string(),
String::from("some stderr"),
],
detached: false,
}],
);

Expand Down Expand Up @@ -2280,6 +2290,7 @@ mod tests {
vec![RequestData::ProcRun {
cmd: SCRIPT_RUNNER.to_string(),
args: vec![SLEEP_SH.to_str().unwrap().to_string(), String::from("0.1")],
detached: false,
}],
);

Expand Down Expand Up @@ -2322,6 +2333,7 @@ mod tests {
vec![RequestData::ProcRun {
cmd: SCRIPT_RUNNER.to_string(),
args: vec![SLEEP_SH.to_str().unwrap().to_string(), String::from("1")],
detached: false,
}],
);

Expand Down Expand Up @@ -2396,6 +2408,7 @@ mod tests {
vec![RequestData::ProcRun {
cmd: SCRIPT_RUNNER.to_string(),
args: vec![SLEEP_SH.to_str().unwrap().to_string(), String::from("1")],
detached: false,
}],
);

Expand Down Expand Up @@ -2491,6 +2504,7 @@ mod tests {
vec![RequestData::ProcRun {
cmd: SCRIPT_RUNNER.to_string(),
args: vec![ECHO_STDIN_TO_STDOUT_SH.to_str().unwrap().to_string()],
detached: false,
}],
);

Expand Down Expand Up @@ -2563,6 +2577,7 @@ mod tests {
RequestData::ProcRun {
cmd: SCRIPT_RUNNER.to_string(),
args: vec![SLEEP_SH.to_str().unwrap().to_string(), String::from("1")],
detached: false,
},
RequestData::ProcList {},
],
Expand All @@ -2586,6 +2601,7 @@ mod tests {
entries: vec![RunningProcess {
cmd: SCRIPT_RUNNER.to_string(),
args: vec![SLEEP_SH.to_str().unwrap().to_string(), String::from("1")],
detached: false,
id,
}],
},
Expand Down
30 changes: 21 additions & 9 deletions distant-core/src/server/distant/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,22 @@ impl State {
if let Some(ids) = self.client_processes.remove(&conn_id) {
for id in ids {
if let Some(process) = self.processes.remove(&id) {
trace!(
"<Conn @ {:?}> Requesting proc {} be killed",
conn_id,
process.id
);
let pid = process.id;
if !process.kill() {
error!("Conn {} failed to send process {} kill signal", id, pid);
if !process.detached {
trace!(
"<Conn @ {:?}> Requesting proc {} be killed",
conn_id,
process.id
);
let pid = process.id;
if !process.kill() {
error!("Conn {} failed to send process {} kill signal", id, pid);
}
} else {
trace!(
"<Conn @ {:?}> Proc {} is detached and will not be killed",
conn_id,
process.id
);
}
}
}
Expand All @@ -94,6 +102,9 @@ pub struct Process {
/// Arguments associated with the process
pub args: Vec<String>,

/// Whether or not this process was run detached
pub detached: bool,

/// Transport channel to send new input to the stdin of the process,
/// one line at a time
stdin_tx: Option<mpsc::Sender<String>>,
Expand All @@ -106,11 +117,12 @@ pub struct Process {
}

impl Process {
pub fn new(id: usize, cmd: String, args: Vec<String>) -> Self {
pub fn new(id: usize, cmd: String, args: Vec<String>, detached: bool) -> Self {
Self {
id,
cmd,
args,
detached,
stdin_tx: None,
kill_tx: None,
wait_task: None,
Expand Down
Loading

0 comments on commit 043ae6c

Please sign in to comment.