From 7f5b788acbc6720795984126a9805c199b9a2cfb Mon Sep 17 00:00:00 2001 From: Patrick D'appollonio <930925+patrickdappollonio@users.noreply.github.com> Date: Sat, 14 Dec 2024 03:15:45 -0500 Subject: [PATCH] Add support for static binary for Linux. (#7) --- src/main.rs | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/src/main.rs b/src/main.rs index a846f55..db4a85d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,7 +108,7 @@ fn main() -> Result<()> { // On Linux, set the Pdeathsig so the child receives SIGTERM if the parent dies #[cfg(target_os = "linux")] { - use std::io::Error; + use std::io::{Error, ErrorKind}; use std::os::unix::process::CommandExt; unsafe { @@ -123,43 +123,19 @@ fn main() -> Result<()> { return Err(Error::last_os_error()); } - Ok(()) - }); - } - } - - // On FreeBSD, set the Pdeathsig so the child receives SIGTERM if the parent dies - #[cfg(target_os = "freebsd")] - { - use std::io::Error; - use std::os::unix::process::CommandExt; - - unsafe { - cmd.pre_exec(|| { - if libc::procctl(libc::P_PID, 0, libc::PROC_PDEATHSIG, libc::SIGTERM) != 0 { - return Err(Error::last_os_error()); - } - - if libc::procctl(libc::P_PID, 0, libc::PROC_PDEATHSIG, libc::SIGKILL) != 0 { - return Err(Error::last_os_error()); + // Double-check parent PID + let ppid = libc::getppid(); + if ppid == 1 { + // The parent is init, meaning we won't get PDEATHSIG if the original parent is gone + return Err(Error::new( + ErrorKind::Other, + "Unable to operate on a program whose parent is init", + )); } Ok(()) }); } - }; - - // Check if the parent is init, which means we won't get PDEATHSIG - #[cfg(any(target_os = "linux", target_os = "freebsd"))] - { - unsafe { - // Double-check parent PID - let ppid = libc::getppid(); - if ppid == 1 { - // The parent is init, meaning we won't get PDEATHSIG if the original parent is gone - anyhow::bail!("Unable to operate on a program whose parent is init"); - } - } } // Grab the exit code from the executed program