Skip to content

Commit

Permalink
Add FreeBSD target support and static compilation via Linux MUSL (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio authored Dec 14, 2024
1 parent b228fc9 commit 65e4371
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ jobs:
strategy:
matrix:
include:
- target: aarch64-unknown-linux-musl
os: ubuntu-latest
archive: linux-static-arm64
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
archive: linux-static-x86_64
- target: aarch64-unknown-linux-gnu
os: ubuntu-latest
archive: linux-arm64
Expand All @@ -28,6 +34,12 @@ jobs:
- target: x86_64-pc-windows-msvc
os: windows-latest
archive: windows-x86_64
- target: x86_64-unknown-freebsd
os: ubuntu-latest
archive: freebsd-x86_64
- target: aarch64-unknown-freebsd
os: ubuntu-latest
archive: freebsd-arm64
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ which = "7.0.0"
opt-level = "z" # Optimize for size.
lto = true # Enable link time optimization.
codegen-units = 1 # Reduce parallel code generation units.
strip = "symbols" # Strip debug symbols.
42 changes: 33 additions & 9 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, ErrorKind};
use std::io::Error;
use std::os::unix::process::CommandExt;

unsafe {
Expand All @@ -123,19 +123,43 @@ fn main() -> Result<()> {
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(())
});
}
}

// 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());
}

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 65e4371

Please sign in to comment.