| 1 |
|
| 2 |
|
| 3 |
use crate::sys::{mode_to_flags, SourceId}; |
| 4 |
use crate::{PollMode, Poller}; |
| 5 |
|
| 6 |
use std::io; |
| 7 |
use std::marker::PhantomData; |
| 8 |
use std::process::Child; |
| 9 |
use std::time::Duration; |
| 10 |
|
| 11 |
use rustix::event::kqueue; |
| 12 |
|
| 13 |
use super::__private::PollerSealed; |
| 14 |
use __private::FilterSealed; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
pub trait PollerKqueueExt<F: Filter>: PollerSealed { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
fn add_filter(&self, filter: F, key: usize, mode: PollMode) -> io::Result<()>; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
fn modify_filter(&self, filter: F, key: usize, mode: PollMode) -> io::Result<()>; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
fn delete_filter(&self, filter: F) -> io::Result<()>; |
| 96 |
} |
| 97 |
|
| 98 |
impl<F: Filter> PollerKqueueExt<F> for Poller { |
| 99 |
#[inline(always)] |
| 100 |
fn add_filter(&self, filter: F, key: usize, mode: PollMode) -> io::Result<()> { |
| 101 |
|
| 102 |
self.poller.add_source(filter.source_id())?; |
| 103 |
self.modify_filter(filter, key, mode) |
| 104 |
} |
| 105 |
|
| 106 |
fn modify_filter(&self, filter: F, key: usize, mode: PollMode) -> io::Result<()> { |
| 107 |
self.poller.has_source(filter.source_id())?; |
| 108 |
|
| 109 |
|
| 110 |
let event = filter.filter(kqueue::EventFlags::ADD | mode_to_flags(mode), key); |
| 111 |
|
| 112 |
|
| 113 |
self.poller.submit_changes([event]) |
| 114 |
} |
| 115 |
|
| 116 |
fn delete_filter(&self, filter: F) -> io::Result<()> { |
| 117 |
|
| 118 |
let event = filter.filter(kqueue::EventFlags::DELETE, 0); |
| 119 |
|
| 120 |
|
| 121 |
self.poller.submit_changes([event])?; |
| 122 |
|
| 123 |
self.poller.remove_source(filter.source_id()) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
pub trait Filter: FilterSealed {} |
| 129 |
|
| 130 |
unsafe impl<T: FilterSealed + ?Sized> FilterSealed for &T { |
| 131 |
#[inline(always)] |
| 132 |
fn filter(&self, flags: kqueue::EventFlags, key: usize) -> kqueue::Event { |
| 133 |
(**self).filter(flags, key) |
| 134 |
} |
| 135 |
|
| 136 |
#[inline(always)] |
| 137 |
fn source_id(&self) -> SourceId { |
| 138 |
(**self).source_id() |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
impl<T: Filter + ?Sized> Filter for &T {} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 149 |
pub struct Signal(pub std::os::raw::c_int); |
| 150 |
|
| 151 |
unsafe impl FilterSealed for Signal { |
| 152 |
#[inline(always)] |
| 153 |
fn filter(&self, flags: kqueue::EventFlags, key: usize) -> kqueue::Event { |
| 154 |
kqueue::Event::new( |
| 155 |
kqueue::EventFilter::Signal { |
| 156 |
signal: rustix::process::Signal::from_named_raw(self.0) |
| 157 |
.expect("invalid signal number"), |
| 158 |
times: 0, |
| 159 |
}, |
| 160 |
flags | kqueue::EventFlags::RECEIPT, |
| 161 |
key as _, |
| 162 |
) |
| 163 |
} |
| 164 |
|
| 165 |
#[inline(always)] |
| 166 |
fn source_id(&self) -> SourceId { |
| 167 |
SourceId::Signal(self.0) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
impl Filter for Signal {} |
| 172 |
|
| 173 |
|
| 174 |
#[derive(Debug)] |
| 175 |
pub struct Process<'a> { |
| 176 |
|
| 177 |
pid: rustix::process::Pid, |
| 178 |
|
| 179 |
|
| 180 |
ops: ProcessOps, |
| 181 |
|
| 182 |
|
| 183 |
_lt: PhantomData<&'a Child>, |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 188 |
#[non_exhaustive] |
| 189 |
pub enum ProcessOps { |
| 190 |
|
| 191 |
Exit, |
| 192 |
|
| 193 |
|
| 194 |
Fork, |
| 195 |
|
| 196 |
|
| 197 |
Exec, |
| 198 |
} |
| 199 |
|
| 200 |
impl<'a> Process<'a> { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
pub unsafe fn new(child: &'a Child, ops: ProcessOps) -> Self { |
| 208 |
Self { |
| 209 |
pid: rustix::process::Pid::from_child(child), |
| 210 |
ops, |
| 211 |
_lt: PhantomData, |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
pub unsafe fn from_pid(pid: std::num::NonZeroI32, ops: ProcessOps) -> Self { |
| 221 |
Self { |
| 222 |
pid: unsafe { rustix::process::Pid::from_raw_unchecked(pid.get()) }, |
| 223 |
ops, |
| 224 |
_lt: PhantomData, |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
unsafe impl FilterSealed for Process<'_> { |
| 230 |
#[inline(always)] |
| 231 |
fn filter(&self, flags: kqueue::EventFlags, key: usize) -> kqueue::Event { |
| 232 |
let events = match self.ops { |
| 233 |
ProcessOps::Exit => kqueue::ProcessEvents::EXIT, |
| 234 |
ProcessOps::Fork => kqueue::ProcessEvents::FORK, |
| 235 |
ProcessOps::Exec => kqueue::ProcessEvents::EXEC, |
| 236 |
}; |
| 237 |
|
| 238 |
kqueue::Event::new( |
| 239 |
kqueue::EventFilter::Proc { |
| 240 |
|
| 241 |
pid: self.pid, |
| 242 |
flags: events, |
| 243 |
}, |
| 244 |
flags | kqueue::EventFlags::RECEIPT, |
| 245 |
key as _, |
| 246 |
) |
| 247 |
} |
| 248 |
|
| 249 |
#[inline(always)] |
| 250 |
fn source_id(&self) -> SourceId { |
| 251 |
|
| 252 |
SourceId::Pid(self.pid) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
impl Filter for Process<'_> {} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 262 |
pub struct Timer { |
| 263 |
|
| 264 |
pub id: usize, |
| 265 |
|
| 266 |
|
| 267 |
pub timeout: Duration, |
| 268 |
} |
| 269 |
|
| 270 |
unsafe impl FilterSealed for Timer { |
| 271 |
fn filter(&self, flags: kqueue::EventFlags, key: usize) -> kqueue::Event { |
| 272 |
kqueue::Event::new( |
| 273 |
kqueue::EventFilter::Timer { |
| 274 |
ident: self.id as _, |
| 275 |
timer: Some(self.timeout), |
| 276 |
}, |
| 277 |
flags | kqueue::EventFlags::RECEIPT, |
| 278 |
key as _, |
| 279 |
) |
| 280 |
} |
| 281 |
|
| 282 |
#[inline(always)] |
| 283 |
fn source_id(&self) -> SourceId { |
| 284 |
SourceId::Timer(self.id) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
impl Filter for Timer {} |
| 289 |
|
| 290 |
mod __private { |
| 291 |
use crate::sys::SourceId; |
| 292 |
use rustix::event::kqueue; |
| 293 |
|
| 294 |
#[doc(hidden)] |
| 295 |
pub unsafe trait FilterSealed { |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
fn filter(&self, flags: kqueue::EventFlags, key: usize) -> kqueue::Event; |
| 300 |
|
| 301 |
|
| 302 |
fn source_id(&self) -> SourceId; |
| 303 |
} |
| 304 |
} |