#[cfg(unix)]
mod imp {
use std::sync::Once;
static INSTALL: Once = Once::new();
extern "C" fn handle_sigbus(
_sig: libc::c_int,
info: *mut libc::siginfo_t,
_ctx: *mut libc::c_void,
) {
const MSG: &[u8] =
b"\n[goosefs-sc] FATAL: SIGBUS on a short-circuit block mmap. The backing block \
file was truncated/replaced while locked (INV-D1 violated). Aborting to avoid returning \
torn/stale data. Consider io.mode=pread for untrusted filesystems. fault_addr=0x";
unsafe {
libc::write(2, MSG.as_ptr() as *const libc::c_void, MSG.len());
if !info.is_null() {
let addr = (*info).si_addr() as usize;
let mut buf = [0u8; 16];
let mut n = addr;
let mut i = buf.len();
if n == 0 {
i -= 1;
buf[i] = b'0';
} else {
while n != 0 && i > 0 {
i -= 1;
let d = (n & 0xf) as u8;
buf[i] = if d < 10 { b'0' + d } else { b'a' + (d - 10) };
n >>= 4;
}
}
libc::write(2, buf[i..].as_ptr() as *const libc::c_void, buf.len() - i);
}
libc::write(2, b"\n".as_ptr() as *const libc::c_void, 1);
libc::abort();
}
}
pub fn install() {
INSTALL.call_once(|| {
unsafe {
let mut action: libc::sigaction = std::mem::zeroed();
action.sa_sigaction = handle_sigbus as usize;
action.sa_flags = libc::SA_SIGINFO;
libc::sigemptyset(&mut action.sa_mask);
let rc = libc::sigaction(libc::SIGBUS, &action, std::ptr::null_mut());
if rc != 0 {
const WARN: &[u8] =
b"[goosefs-sc] WARN: sigaction(SIGBUS) registration failed; \
short-circuit mmap will rely on the kernel's default SIGBUS disposition \
(terminate). Data correctness is unaffected; only the targeted INV-D1 \
diagnostic message is lost.\n";
libc::write(2, WARN.as_ptr() as *const libc::c_void, WARN.len());
}
}
});
}
}
#[cfg(not(unix))]
mod imp {
pub fn install() {}
}
pub fn install_if_enabled(enabled: bool) {
if enabled {
imp::install();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn install_disabled_is_noop() {
install_if_enabled(false);
install_if_enabled(false);
}
}