From 1abb97cca69e0b513f7499cc7db03ba622b38711 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 8 Mar 2022 02:37:38 -0500 Subject: use /proc/self/exe directly when re-execing ourself when possible this will use the same binary that is currently running, even if it has been deleted or replaced --- src/info.rs | 11 +++++++++++ src/parse/ast.rs | 4 ++-- src/parse/test_ast.rs | 2 +- src/shell/history/entry.rs | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/info.rs b/src/info.rs index dc62125..6a5ad4f 100644 --- a/src/info.rs +++ b/src/info.rs @@ -35,6 +35,17 @@ pub fn pid() -> String { nix::unistd::getpid().to_string() } +#[cfg(target_os = "linux")] +#[allow(clippy::unnecessary_wraps)] +pub fn current_exe() -> Result { + Ok("/proc/self/exe".into()) +} + +#[cfg(not(target_os = "linux"))] +pub fn current_exe() -> Result { + Ok(std::env::current_exe()?) +} + // the time crate is currently unable to get the local offset on unix due to // soundness concerns, so we have to do it manually/: // diff --git a/src/parse/ast.rs b/src/parse/ast.rs index 92c04ab..5bceed5 100644 --- a/src/parse/ast.rs +++ b/src/parse/ast.rs @@ -171,7 +171,7 @@ impl Exe { return Self { exe: Word { parts: vec![WordPart::SingleQuoted( - std::env::current_exe() + crate::info::current_exe() .unwrap() .to_str() .unwrap() @@ -370,7 +370,7 @@ impl WordPart { Self::Alternation(_) => unreachable!(), Self::Substitution(commands) => { let mut cmd = tokio::process::Command::new( - std::env::current_exe().unwrap(), + crate::info::current_exe().unwrap(), ); cmd.args(&["-c", &commands]); cmd.stdin(std::process::Stdio::inherit()); diff --git a/src/parse/test_ast.rs b/src/parse/test_ast.rs index fb3a4e3..a1f83dd 100644 --- a/src/parse/test_ast.rs +++ b/src/parse/test_ast.rs @@ -216,7 +216,7 @@ fn test_basic() { ); // XXX this parse may change in the future - let exe = std::env::current_exe() + let exe = crate::info::current_exe() .unwrap() .into_os_string() .into_string() diff --git a/src/shell/history/entry.rs b/src/shell/history/entry.rs index 56f378a..145e0cf 100644 --- a/src/shell/history/entry.rs +++ b/src/shell/history/entry.rs @@ -296,7 +296,7 @@ impl Entry { env: &Env, pts: &pty_process::Pts, ) -> Result<(tokio::process::Child, std::fs::File)> { - let mut cmd = pty_process::Command::new(std::env::current_exe()?); + let mut cmd = pty_process::Command::new(crate::info::current_exe()?); cmd.args(&["-c", cmdline, "--status-fd", "3"]); env.apply(&mut cmd); let (from_r, from_w) = -- cgit v1.2.3-54-g00ecf