use crate::bin_error;
#[cfg(target_os = "linux")]
pub fn disable_tracing() -> bin_error::Result<()> {
const PR_SET_DUMPABLE: i32 = 4;
let ret = unsafe { libc::prctl(PR_SET_DUMPABLE, 0) };
if ret == 0 {
Ok(())
} else {
let e = std::io::Error::last_os_error();
Err(bin_error::Error::msg(format!("failed to disable PTRACE_ATTACH, agent memory may be dumpable by other processes: {e}")))
}
}
#[cfg(target_os = "macos")]
pub fn disable_tracing() -> bin_error::Result<()> {
let ret = unsafe {
libc::ptrace(libc::PT_DENY_ATTACH, 0, std::ptr::null_mut(), 0)
};
if ret != 0 {
let e = std::io::Error::last_os_error();
return Err(bin_error::Error::msg(format!(
"failed to deny debugger attach, agent memory may be readable by other processes: {e}"
)));
}
let rlim = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
let ret = unsafe { libc::setrlimit(libc::RLIMIT_CORE, &raw const rlim) };
if ret != 0 {
let e = std::io::Error::last_os_error();
return Err(bin_error::Error::msg(format!(
"failed to disable core dumps, agent memory may be dumped to disk: {e}"
)));
}
Ok(())
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
pub fn disable_tracing() -> bin_error::Result<()> {
Err(bin_error::Error::msg("failed to disable PTRACE_ATTACH, agent memory may be dumpable by other processes: unimplemented on this platform"))
}