use sysctl::{
Ctl,
CtlValue,
Sysctl,
};
#[derive(Debug)]
pub enum RctlState {
Disabled,
Enabled,
Jailed,
NotPresent,
}
const CTL_KERN_RACCT_ENABLE: &str = "kern.racct.enable";
const CTL_SECURITY_JAIL_JAILED: &str = "security.jail.jailed";
impl RctlState {
pub fn check() -> Self {
if Self::jailed() {
return Self::Jailed;
}
let res = Ctl::new(CTL_KERN_RACCT_ENABLE);
let ctl = match res {
Ok(ctl) => ctl,
Err(_) => return Self::NotPresent,
};
if let Ok(value) = ctl.value() {
match value {
CtlValue::U8(1) | CtlValue::Uint(1) => Self::Enabled,
_ => Self::Disabled,
}
}
else {
Self::Disabled
}
}
fn jailed() -> bool {
let res = Ctl::new(CTL_SECURITY_JAIL_JAILED);
let ctl = match res {
Ok(ctl) => ctl,
Err(_) => return true,
};
if let Ok(value) = ctl.value() {
matches!(value, CtlValue::Int(1))
}
else {
true
}
}
}