Skip to content

Commit

Permalink
kqueue: add more things on more BSDs
Browse files Browse the repository at this point in the history
  • Loading branch information
valpackett committed Mar 13, 2023
1 parent 1372e56 commit 752d5a1
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/io/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impl Event {
let (ident, filter, fflags) = match filter {
EventFilter::Read(fd) => (fd.as_raw_fd() as uintptr_t, c::EVFILT_READ, 0),
EventFilter::Write(fd) => (fd.as_raw_fd() as _, c::EVFILT_WRITE, 0),
#[cfg(target_os = "freebsd")]
EventFilter::Empty(fd) => (fd.as_raw_fd() as _, c::EVFILT_EMPTY, 0),
EventFilter::Vnode { vnode, flags } => {
(vnode.as_raw_fd() as _, c::EVFILT_VNODE, flags.bits())
}
Expand All @@ -37,8 +39,8 @@ impl Event {
c::EVFILT_PROC,
flags.bits(),
),
#[cfg(any(apple, target_os = "freebsd"))]
EventFilter::Timer(timer) => {
#[cfg(any(apple, target_os = "freebsd", target_os = "netbsd"))]
let (data, fflags) = match timer {
Some(timer) => {
if timer.subsec_millis() == 0 {
Expand All @@ -51,10 +53,15 @@ impl Event {
}
None => (uintptr_t::MAX, c::NOTE_SECONDS),
};
#[cfg(any(target_os = "dragonfly", target_os = "openbsd"))]
let (data, fflags) = match timer {
Some(timer) => (timer.as_millis() as _, 0),
None => (uintptr_t::MAX, 0),
};

(data, c::EVFILT_TIMER, fflags)
}
#[cfg(any(apple, freebsdlike))]
#[cfg(any(apple, freebsdlike, target_os = "netbsd"))]
EventFilter::User {
ident,
flags,
Expand Down Expand Up @@ -98,6 +105,8 @@ impl Event {
match self.inner.filter as _ {
c::EVFILT_READ => EventFilter::Read(self.inner.ident as _),
c::EVFILT_WRITE => EventFilter::Write(self.inner.ident as _),
#[cfg(target_os = "freebsd")]
c::EVFILT_EMPTY => EventFilter::Empty(self.inner.ident as _),
c::EVFILT_VNODE => EventFilter::Vnode {
vnode: self.inner.ident as _,
flags: VnodeEvents::from_bits_truncate(self.inner.fflags),
Expand All @@ -107,9 +116,9 @@ impl Event {
pid: unsafe { crate::process::Pid::from_raw(self.inner.ident as _) }.unwrap(),
flags: ProcessEvents::from_bits_truncate(self.inner.fflags),
},
#[cfg(any(apple, target_os = "freebsd"))]
c::EVFILT_TIMER => EventFilter::Timer({
let (data, fflags) = (self.inner.data, self.inner.fflags);
#[cfg(any(apple, target_os = "freebsd", target_os = "netbsd"))]
match fflags as _ {
c::NOTE_SECONDS => Some(Duration::from_secs(data as _)),
c::NOTE_USECONDS => Some(Duration::from_micros(data as _)),
Expand All @@ -119,8 +128,10 @@ impl Event {
None
}
}
#[cfg(any(target_os = "dragonfly", target_os = "openbsd"))]
Some(Duration::from_millis(data as _))
}),
#[cfg(any(apple, freebsdlike))]
#[cfg(any(apple, freebsdlike, target_os = "netbsd"))]
c::EVFILT_USER => EventFilter::User {
ident: self.inner.ident as _,
flags: UserFlags::from_bits_truncate(self.inner.fflags),
Expand All @@ -132,7 +143,7 @@ impl Event {
}

/// Bottom 24 bits of a u32.
#[cfg(any(apple, freebsdlike))]
#[cfg(any(apple, freebsdlike, target_os = "netbsd"))]
const EVFILT_USER_FLAGS: u32 = 0x00ff_ffff;

/// The possible filters for a `kqueue`.
Expand All @@ -145,6 +156,10 @@ pub enum EventFilter {
/// A write filter.
Write(RawFd),

/// An empty filter.
#[cfg(target_os = "freebsd")]
Empty(RawFd),

/// A VNode filter.
Vnode {
/// The file descriptor we looked for events in.
Expand All @@ -165,11 +180,10 @@ pub enum EventFilter {
},

/// A timer filter.
#[cfg(any(apple, target_os = "freebsd"))]
Timer(Option<Duration>),

/// A user filter.
#[cfg(any(apple, freebsdlike))]
#[cfg(any(apple, freebsdlike, target_os = "netbsd"))]
User {
/// The identifier for this event.
ident: intptr_t,
Expand Down Expand Up @@ -241,6 +255,9 @@ bitflags::bitflags! {

/// Access to the file was revoked.
const REVOKE = c::NOTE_REVOKE;

/// The link count of the file has changed.
const LINK = c::NOTE_LINK;
}
}

Expand All @@ -257,8 +274,11 @@ bitflags::bitflags! {
/// The process executed a new process.
const EXEC = c::NOTE_EXEC;

/// TODO
/// Follow the process through fork() calls (write only).
const TRACK = c::NOTE_TRACK;

/// An error has occurred with following the process.
const TRACKERR = c::NOTE_TRACKERR;
}
}

Expand Down Expand Up @@ -293,11 +313,11 @@ bitflags::bitflags! {
///
/// Only the lower 24 bits are used in this struct.
#[repr(transparent)]
#[cfg(any(apple, freebsdlike))]
#[cfg(any(apple, freebsdlike, target_os = "netbsd"))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct UserDefinedFlags(u32);

#[cfg(any(apple, freebsdlike))]
#[cfg(any(apple, freebsdlike, target_os = "netbsd"))]
impl UserDefinedFlags {
/// Create a new `UserDefinedFlags` from a `u32`.
pub fn new(flags: u32) -> Self {
Expand Down

0 comments on commit 752d5a1

Please sign in to comment.