pub fn disable_core_dumps() {
#[cfg(unix)]
{
let limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
unsafe {
libc::setrlimit(libc::RLIMIT_CORE, &raw const limit);
}
}
}
pub fn strict_mode() -> bool {
effective_strict_from(
&std::env::var("MURK_STRICT").unwrap_or_default(),
&std::env::var("MURK_AGENT").unwrap_or_default(),
)
}
pub fn agent_context() -> bool {
strict_from(&std::env::var("MURK_AGENT").unwrap_or_default())
}
pub fn ci_context() -> bool {
strict_from(&std::env::var("CI").unwrap_or_default())
}
pub fn self_scope() -> bool {
strict_from(&std::env::var("MURK_SELF_SCOPE").unwrap_or_default()) || agent_context()
}
fn strict_from(val: &str) -> bool {
matches!(
val.trim().to_ascii_lowercase().as_str(),
"1" | "true" | "yes"
)
}
fn effective_strict_from(strict: &str, agent: &str) -> bool {
strict_from(strict) || strict_from(agent)
}
pub fn is_ram_backed(path: &std::path::Path) -> bool {
#[cfg(target_os = "linux")]
{
use std::os::unix::ffi::OsStrExt;
const TMPFS_MAGIC: libc::c_long = 0x0102_1994;
const RAMFS_MAGIC: libc::c_long = 0x858_458f6_u64 as libc::c_long;
let Ok(c_path) = std::ffi::CString::new(path.as_os_str().as_bytes()) else {
return false;
};
let mut buf: libc::statfs = unsafe { std::mem::zeroed() };
let rc = unsafe { libc::statfs(c_path.as_ptr(), &raw mut buf) };
rc == 0 && matches!(buf.f_type as libc::c_long, TMPFS_MAGIC | RAMFS_MAGIC)
}
#[cfg(target_os = "macos")]
{
use std::os::unix::ffi::OsStrExt;
let Ok(c_path) = std::ffi::CString::new(path.as_os_str().as_bytes()) else {
return false;
};
let mut buf: libc::statfs = unsafe { std::mem::zeroed() };
let rc = unsafe { libc::statfs(c_path.as_ptr(), &raw mut buf) };
if rc != 0 {
return false;
}
let name: Vec<u8> = buf
.f_fstypename
.iter()
.take_while(|&&c| c != 0)
.map(|&c| c.cast_unsigned())
.collect();
name == b"tmpfs"
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = path;
false
}
}
pub fn stdout_is_regular_file() -> bool {
#[cfg(unix)]
{
use std::os::unix::io::{AsRawFd, FromRawFd};
let fd = std::io::stdout().as_raw_fd();
let f = std::mem::ManuallyDrop::new(unsafe { std::fs::File::from_raw_fd(fd) });
f.metadata().is_ok_and(|m| m.is_file())
}
#[cfg(not(unix))]
{
false
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[test]
fn disables_core_dumps() {
disable_core_dumps();
let mut current = libc::rlimit {
rlim_cur: 1,
rlim_max: 1,
};
let rc = unsafe { libc::getrlimit(libc::RLIMIT_CORE, &raw mut current) };
assert_eq!(rc, 0, "getrlimit failed");
assert_eq!(current.rlim_cur, 0);
assert_eq!(current.rlim_max, 0);
}
#[test]
fn strict_truthiness() {
for on in ["1", "true", "yes", "YES", " True ", "Yes"] {
assert!(strict_from(on), "{on:?} should enable strict mode");
}
for off in ["", "0", "false", "no", "off", "enabled", "2"] {
assert!(!strict_from(off), "{off:?} should not enable strict mode");
}
}
#[test]
fn effective_strict_from_decision_table() {
let cases = [
("1", "", true),
("yes", "", true),
("", "1", true),
("true", "true", true),
("0", "1", true),
("false", "1", true),
("bogus", "1", true),
("", "", false),
("0", "", false),
("", "0", false),
("bogus", "", false),
("2", "enabled", false),
];
for (strict, agent, expected) in cases {
assert_eq!(
effective_strict_from(strict, agent),
expected,
"strict={strict:?} agent={agent:?}"
);
}
}
#[test]
fn nonexistent_path_is_not_ram_backed() {
assert!(!is_ram_backed(std::path::Path::new(
"/no/such/murk/path/exists"
)));
}
#[cfg(target_os = "linux")]
#[test]
fn dev_shm_is_ram_backed() {
let shm = std::path::Path::new("/dev/shm");
if shm.is_dir() {
assert!(is_ram_backed(shm), "/dev/shm should be tmpfs");
}
}
#[cfg(target_os = "macos")]
#[test]
fn macos_tmp_is_not_ram_backed() {
assert!(!is_ram_backed(&std::env::temp_dir()));
}
}