Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default shell when /bin/bash is not present #58

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading