Skip to content

Commit

Permalink
Add support for static binary for Linux. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio authored Dec 14, 2024
1 parent 65e4371 commit 7f5b788
Showing 1 changed file with 9 additions and 33 deletions.
42 changes: 9 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 7f5b788

Please sign in to comment.