#![allow(unsafe_code)]
use crate::error::{Error, Result};
macro_rules! io_err {
() => {
Err(Error::Io(std::io::Error::last_os_error()))
};
}
use std::os::fd::{AsRawFd, OwnedFd};
use std::os::unix::io::FromRawFd;
use std::time::Duration;
const EVFILT_JAIL: i16 = -14;
const EVFILT_JAILDESC: i16 = -15;
fn kevent_ident(id: i32) -> Result<usize> {
usize::try_from(id).map_err(|_| Error::Io(std::io::Error::from_raw_os_error(libc::EINVAL)))
}
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] const fn ident_i32(ident: usize) -> i32 {
ident as i32
}
pub const NOTE_JAIL_CHILD: u32 = 0x8000_0000;
pub const NOTE_JAIL_SET: u32 = 0x4000_0000;
pub const NOTE_JAIL_ATTACH: u32 = 0x2000_0000;
pub const NOTE_JAIL_REMOVE: u32 = 0x1000_0000;
pub const NOTE_JAIL_MULTI: u32 = 0x0800_0000;
pub const NOTE_JAIL_ALL: u32 =
NOTE_JAIL_CHILD | NOTE_JAIL_SET | NOTE_JAIL_ATTACH | NOTE_JAIL_REMOVE;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum JailIdent {
Jid(i32),
Descriptor(i32),
}
impl std::fmt::Display for JailIdent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Jid(jid) => write!(f, "JID {jid}"),
Self::Descriptor(fd) => write!(f, "jail descriptor {fd}"),
}
}
}
#[derive(Debug, Clone)]
pub enum JailEvent {
Child { ident: JailIdent, coalesced: bool },
Set { ident: JailIdent },
Attach { ident: JailIdent, coalesced: bool },
Remove { ident: JailIdent },
}
impl JailEvent {
pub const fn ident(&self) -> JailIdent {
match self {
Self::Child { ident, .. }
| Self::Set { ident }
| Self::Attach { ident, .. }
| Self::Remove { ident } => *ident,
}
}
}
pub struct JailEventSource {
kq: OwnedFd,
}
impl JailEventSource {
pub fn new() -> Result<Self> {
let kq = unsafe { libc::kqueue() };
if kq < 0 {
return io_err!();
}
let fd = unsafe { OwnedFd::from_raw_fd(kq) };
Ok(Self { kq: fd })
}
pub fn register_jail(&self, jid: i32) -> Result<()> {
self.register(kevent_ident(jid)?, EVFILT_JAIL, NOTE_JAIL_ALL)
}
pub fn register_jaildesc(&self, fd: i32) -> Result<()> {
self.register(kevent_ident(fd)?, EVFILT_JAILDESC, NOTE_JAIL_ALL)
}
fn kevent_change(&self, changelist: &[libc::kevent; 1]) -> Result<()> {
let ret = unsafe {
libc::kevent(
self.kq.as_raw_fd(),
changelist.as_ptr(),
1,
std::ptr::null_mut(),
0,
std::ptr::null(),
)
};
if ret < 0 {
return io_err!();
}
Ok(())
}
fn register(&self, ident: usize, filter: i16, fflags: u32) -> Result<()> {
self.kevent_change(&[libc::kevent {
ident,
filter,
flags: libc::EV_ADD | libc::EV_CLEAR,
fflags,
data: 0,
udata: std::ptr::null_mut(),
ext: [0; 4],
}])
}
pub fn poll(&self, timeout: Option<Duration>) -> Result<Vec<JailEvent>> {
let mut events = [unsafe { std::mem::zeroed::<libc::kevent>() }; 16];
let timeout_spec = timeout.map(|d| libc::timespec {
tv_sec: libc::time_t::try_from(d.as_secs()).unwrap_or(libc::time_t::MAX),
tv_nsec: libc::c_long::from(d.subsec_nanos()),
});
let timeout_ptr = match &timeout_spec {
Some(ts) => std::ptr::from_ref::<libc::timespec>(ts),
None => std::ptr::null(),
};
let nevents = libc::c_int::try_from(events.len()).unwrap_or(libc::c_int::MAX);
let n = unsafe {
libc::kevent(
self.kq.as_raw_fd(),
std::ptr::null(),
0,
events.as_mut_ptr(),
nevents,
timeout_ptr,
)
};
let Ok(n) = usize::try_from(n) else {
return io_err!();
};
let mut result = Vec::new();
for ev in &events[..n] {
let ident = if ev.filter == EVFILT_JAILDESC {
JailIdent::Descriptor(ident_i32(ev.ident))
} else {
JailIdent::Jid(ident_i32(ev.ident))
};
let coalesced = ev.fflags & NOTE_JAIL_MULTI != 0;
if ev.fflags & NOTE_JAIL_REMOVE != 0 {
result.push(JailEvent::Remove { ident });
}
if ev.fflags & NOTE_JAIL_CHILD != 0 {
result.push(JailEvent::Child { ident, coalesced });
}
if ev.fflags & NOTE_JAIL_SET != 0 {
result.push(JailEvent::Set { ident });
}
if ev.fflags & NOTE_JAIL_ATTACH != 0 {
result.push(JailEvent::Attach { ident, coalesced });
}
}
Ok(result)
}
pub fn unregister(&self, ident: JailIdent) -> Result<()> {
let (raw, filter) = match ident {
JailIdent::Jid(jid) => (jid, EVFILT_JAIL),
JailIdent::Descriptor(fd) => (fd, EVFILT_JAILDESC),
};
let result = kevent_ident(raw).and_then(|raw| {
self.kevent_change(&[libc::kevent {
ident: raw,
filter,
flags: libc::EV_DELETE,
fflags: 0,
data: 0,
udata: std::ptr::null_mut(),
ext: [0; 4],
}])
});
match result {
Err(Error::Io(e))
if matches!(
e.raw_os_error(),
Some(libc::ENOENT | libc::EINVAL | libc::EBADF)
) =>
{
Ok(())
}
other => other,
}
}
}
#[cfg(test)]
mod tests;