#![allow(unsafe_code)]
pub(crate) fn reset_signal_dispositions() {
let signals = signal_range();
for sig in signals {
if sig == libc::SIGKILL || sig == libc::SIGSTOP {
continue;
}
let mut sa: libc::sigaction = unsafe { std::mem::zeroed() };
sa.sa_sigaction = libc::SIG_DFL;
unsafe { libc::sigemptyset(&mut sa.sa_mask) };
sa.sa_flags = 0;
let ret = unsafe { libc::sigaction(sig, &sa, std::ptr::null_mut()) };
if ret != 0 {
let err = std::io::Error::last_os_error();
if err.raw_os_error() != Some(libc::EINVAL) {
panic!("sigaction({sig}) failed: {err}");
}
}
}
}
fn signal_range() -> Vec<i32> {
#[cfg(target_os = "linux")]
{
let rtmin = libc::SIGRTMIN();
let rtmax = libc::SIGRTMAX();
(1..32).chain(rtmin..=rtmax).collect()
}
#[cfg(not(target_os = "linux"))]
{
(1..=64).collect()
}
}
pub(crate) fn raw_close(fd: i32) {
unsafe { libc::close(fd) };
}
pub(crate) fn raw_initgroups(
user: &std::ffi::CStr,
group: libc::gid_t,
) -> Result<(), nix::errno::Errno> {
let ret = unsafe { libc::initgroups(user.as_ptr(), group as _) };
if ret < 0 {
Err(nix::errno::Errno::last())
} else {
Ok(())
}
}
pub(crate) fn raw_set_env_var(
key: impl AsRef<std::ffi::OsStr>,
value: impl AsRef<std::ffi::OsStr>,
) {
unsafe { std::env::set_var(key, value) };
}
#[cfg(test)]
pub(crate) fn raw_remove_env_var(key: &str) {
unsafe { std::env::remove_var(key) };
}
pub(crate) fn raw_exit(code: i32) -> ! {
unsafe { libc::_exit(code) }
}
pub(crate) fn at_fdcwd() -> std::os::fd::BorrowedFd<'static> {
unsafe { std::os::fd::BorrowedFd::borrow_raw(libc::AT_FDCWD) }
}