summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-03-08 02:37:38 -0500
committerJesse Luehrs <doy@tozt.net>2022-03-08 02:37:38 -0500
commit1abb97cca69e0b513f7499cc7db03ba622b38711 (patch)
tree6e59e0e7e4bfe9b00c735feb40c2286b3e0955c7
parentd66f4773bccaee61c1c998640dcdb048223f8900 (diff)
downloadnbsh-1abb97cca69e0b513f7499cc7db03ba622b38711.tar.gz
nbsh-1abb97cca69e0b513f7499cc7db03ba622b38711.zip
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
-rw-r--r--src/info.rs11
-rw-r--r--src/parse/ast.rs4
-rw-r--r--src/parse/test_ast.rs2
-rw-r--r--src/shell/history/entry.rs2
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<std::path::PathBuf> {
+ Ok("/proc/self/exe".into())
+}
+
+#[cfg(not(target_os = "linux"))]
+pub fn current_exe() -> Result<std::path::PathBuf> {
+ 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) =