From e92544421f1978f38f5543228789cdbce312e446 Mon Sep 17 00:00:00 2001 From: Patrick D'appollonio <930925+patrickdappollonio@users.noreply.github.com> Date: Sat, 14 Dec 2024 03:09:22 -0500 Subject: [PATCH] Add freebsd settings and support. --- .github/workflows/release.yaml | 12 ++++++++++ Cargo.toml | 1 + src/main.rs | 42 ++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 16156a4..5f4b503 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -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 @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 366d1b5..1190a97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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. diff --git a/src/main.rs b/src/main.rs index db4a85d..a846f55 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, ErrorKind}; + use std::io::Error; use std::os::unix::process::CommandExt; unsafe { @@ -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