Skip to content

Commit

Permalink
fix(launchpad): prevent loops from terminal/sudo relaunching
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed May 8, 2024
1 parent 300b549 commit cfe6da8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
27 changes: 24 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions node-launchpad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter", "serde"] }
tui-input = "0.8.0"
which = "6.0.1"
nix = { version = "0.28.0", features = ["user"] }
atty = "0.2.14"

[build-dependencies]
vergen = { version = "8.2.6", features = ["build", "git", "gitoxide", "cargo"] }
15 changes: 14 additions & 1 deletion node-launchpad/src/bin/tui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,31 @@ async fn tokio_main() -> Result<()> {
Ok(())
}

fn is_running_in_terminal() -> bool {
atty::is(atty::Stream::Stdout)
}

#[tokio::main]
async fn main() -> Result<()> {
// now launch the terminal
let terminal_type = terminal::detect_and_setup_terminal()?;
terminal::launch_terminal(&terminal_type)?;

if !is_running_in_terminal() || !terminal::is_running_root() {
terminal::launch_terminal(&terminal_type)?;

// early return for _this_ process.
// The spawned process will be sudo'd and run in the terminal,
// taking over stdout/stderr/stdin.
return Ok(());
}

// Construct a local task set that can run `!Send` futures.
let local = LocalSet::new();
local
.run_until(async {
if let Err(e) = tokio_main().await {
eprintln!("{} failed:", env!("CARGO_PKG_NAME"));

Err(e)
} else {
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions node-launchpad/src/bin/tui/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ pub(crate) enum TerminalType {
}

#[cfg(not(windows))]
fn is_running_root() -> bool {
pub(crate) fn is_running_root() -> bool {
use nix::unistd::geteuid;
geteuid().is_root()
}

#[cfg(windows)]
fn is_running_root() -> bool {
pub(crate) fn is_running_root() -> bool {
// Example: Attempt to read from a typically restricted system directory
std::fs::read_dir("C:\\Windows\\System32\\config").is_ok()
}
Expand Down

0 comments on commit cfe6da8

Please sign in to comment.