Skip to content

Commit

Permalink
Fix default shell when /bin/bash is not present (#58)
Browse files Browse the repository at this point in the history
Resolves #56.
  • Loading branch information
ekzhang authored Nov 23, 2023
1 parent 0782485 commit c8f51a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/sshx/examples/stdin_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tracing::{error, info, trace};
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

let shell = get_default_shell();
let shell = get_default_shell().await;
info!(%shell, "using default shell");

let mut terminal = Terminal::new(&shell).await?;
Expand Down
5 changes: 4 additions & 1 deletion crates/sshx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ fn print_greeting(shell: &str, controller: &Controller) {

#[tokio::main]
async fn start(args: Args) -> Result<()> {
let shell = args.shell.unwrap_or_else(get_default_shell);
let shell = match args.shell {
Some(shell) => shell,
None => get_default_shell().await,
};

let runner = Runner::Shell(shell.clone());
let mut controller = Controller::new(&args.server, runner).await?;
Expand Down
16 changes: 13 additions & 3 deletions crates/sshx/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ use nix::sys::signal::{kill, Signal::SIGKILL};
use nix::sys::wait::waitpid;
use nix::unistd::{execvp, fork, ForkResult, Pid};
use pin_project::{pin_project, pinned_drop};
use tokio::fs::File;
use tokio::fs::{self, File};
use tokio::io::{self, AsyncRead, AsyncWrite};
use tracing::{instrument, trace};

/// Returns the default shell on this system.
pub fn get_default_shell() -> String {
env::var("SHELL").unwrap_or_else(|_| String::from("/bin/bash"))
pub async fn get_default_shell() -> String {
if let Ok(shell) = env::var("SHELL") {
if !shell.is_empty() {
return shell;
}
}
for shell in ["/bin/bash", "/bin/sh"] {
if fs::metadata(shell).await.is_ok() {
return shell.to_string();
}
}
String::from("sh")
}

/// An object that stores the state for a terminal session.
Expand Down

0 comments on commit c8f51a5

Please sign in to comment.