Skip to content

Commit 1b97d96

Browse files
polyfractalcarllerche
authored andcommitted
NetBSD tweaks for kqueue support
1 parent 04d315d commit 1b97d96

File tree

4 files changed

+142
-8
lines changed

4 files changed

+142
-8
lines changed

src/sys/event.rs

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33

44
use {Error, Result};
55
use errno::Errno;
6+
#[cfg(not(target_os = "netbsd"))]
67
use libc::{timespec, time_t, c_int, c_long, uintptr_t};
8+
#[cfg(target_os = "netbsd")]
9+
use libc::{timespec, time_t, c_long, uintptr_t, size_t};
710
use std::os::unix::io::RawFd;
811
use std::ptr;
912

1013
pub use self::ffi::kevent as KEvent;
1114

1215
mod ffi {
13-
pub use libc::{c_int, c_void, uintptr_t, intptr_t, timespec};
16+
pub use libc::{c_int, c_void, uintptr_t, intptr_t, timespec, size_t, int64_t};
1417
use super::{EventFilter, EventFlag, FilterFlag};
1518

19+
#[cfg(not(target_os = "netbsd"))]
1620
#[derive(Clone, Copy)]
1721
#[repr(C)]
1822
pub struct kevent {
@@ -24,22 +28,44 @@ mod ffi {
2428
pub udata: usize // 8
2529
}
2630

31+
#[cfg(target_os = "netbsd")]
32+
#[derive(Clone, Copy)]
33+
#[repr(C)]
34+
pub struct kevent {
35+
pub ident: uintptr_t,
36+
pub filter: EventFilter,
37+
pub flags: EventFlag,
38+
pub fflags: FilterFlag,
39+
pub data: int64_t,
40+
pub udata: intptr_t
41+
}
42+
2743
// Bug in rustc, cannot determine that kevent is #[repr(C)]
2844
#[allow(improper_ctypes)]
2945
extern {
3046
pub fn kqueue() -> c_int;
3147

48+
#[cfg(not(target_os = "netbsd"))]
3249
pub fn kevent(
3350
kq: c_int,
3451
changelist: *const kevent,
3552
nchanges: c_int,
3653
eventlist: *mut kevent,
3754
nevents: c_int,
3855
timeout: *const timespec) -> c_int;
56+
57+
#[cfg(target_os = "netbsd")]
58+
pub fn kevent(
59+
kq: c_int,
60+
changelist: *const kevent,
61+
nchanges: size_t,
62+
eventlist: *mut kevent,
63+
nevents: size_t,
64+
timeout: *const timespec) -> c_int;
3965
}
4066
}
4167

42-
#[cfg(not(target_os = "dragonfly"))]
68+
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
4369
#[repr(i16)]
4470
#[derive(Clone, Copy, Debug, PartialEq)]
4571
pub enum EventFilter {
@@ -73,7 +99,21 @@ pub enum EventFilter {
7399
EVFILT_USER = -9,
74100
}
75101

76-
#[cfg(not(target_os = "dragonfly"))]
102+
#[cfg(target_os = "netbsd")]
103+
#[repr(u32)]
104+
#[derive(Clone, Copy, Debug, PartialEq)]
105+
pub enum EventFilter {
106+
EVFILT_READ = 0,
107+
EVFILT_WRITE = 1,
108+
EVFILT_AIO = 2,
109+
EVFILT_VNODE = 3,
110+
EVFILT_PROC = 4,
111+
EVFILT_SIGNAL = 5,
112+
EVFILT_TIMER = 6,
113+
EVFILT_SYSCOUNT = 7
114+
}
115+
116+
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
77117
bitflags!(
78118
flags EventFlag: u16 {
79119
const EV_ADD = 0x0001,
@@ -110,7 +150,24 @@ bitflags!(
110150
}
111151
);
112152

113-
#[cfg(not(target_os = "dragonfly"))]
153+
#[cfg(target_os = "netbsd")]
154+
bitflags!(
155+
flags EventFlag: u32 {
156+
const EV_ADD = 0x0001,
157+
const EV_DELETE = 0x0002,
158+
const EV_ENABLE = 0x0004,
159+
const EV_DISABLE = 0x0008,
160+
const EV_ONESHOT = 0x0010,
161+
const EV_CLEAR = 0x0020,
162+
const EV_SYSFLAGS = 0xF000,
163+
const EV_NODATA = 0x1000,
164+
const EV_FLAG1 = 0x2000,
165+
const EV_EOF = 0x8000,
166+
const EV_ERROR = 0x4000
167+
}
168+
);
169+
170+
#[cfg(not(any(target_os = "dragonfly", target_os="netbsd")))]
114171
bitflags!(
115172
flags FilterFlag: u32 {
116173
const NOTE_TRIGGER = 0x01000000,
@@ -188,10 +245,33 @@ bitflags!(
188245
}
189246
);
190247

191-
#[cfg(not(target_os = "dragonfly"))]
248+
#[cfg(target_os = "netbsd")]
249+
bitflags!(
250+
flags FilterFlag: u32 {
251+
const NOTE_LOWAT = 0x00000001,
252+
const NOTE_DELETE = 0x00000001,
253+
const NOTE_WRITE = 0x00000002,
254+
const NOTE_EXTEND = 0x00000004,
255+
const NOTE_ATTRIB = 0x00000008,
256+
const NOTE_LINK = 0x00000010,
257+
const NOTE_RENAME = 0x00000020,
258+
const NOTE_REVOKE = 0x00000040,
259+
const NOTE_EXIT = 0x80000000,
260+
const NOTE_FORK = 0x40000000,
261+
const NOTE_EXEC = 0x20000000,
262+
const NOTE_SIGNAL = 0x08000000,
263+
const NOTE_PDATAMASK = 0x000fffff,
264+
const NOTE_PCTRLMASK = 0xf0000000, // NOTE: FreeBSD uses 0xfff00000,
265+
const NOTE_TRACK = 0x00000001,
266+
const NOTE_TRACKERR = 0x00000002,
267+
const NOTE_CHILD = 0x00000004
268+
}
269+
);
270+
271+
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
192272
pub const EV_POLL: EventFlag = EV_FLAG0;
193273

194-
#[cfg(not(target_os = "dragonfly"))]
274+
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
195275
pub const EV_OOBAND: EventFlag = EV_FLAG1;
196276

197277
pub fn kqueue() -> Result<RawFd> {
@@ -218,10 +298,12 @@ pub fn kevent(kq: RawFd,
218298
kevent_ts(kq, changelist, eventlist, Some(timeout))
219299
}
220300

301+
#[cfg(not(target_os = "netbsd"))]
221302
pub fn kevent_ts(kq: RawFd,
222303
changelist: &[KEvent],
223304
eventlist: &mut [KEvent],
224305
timeout_opt: Option<timespec>) -> Result<usize> {
306+
225307
let res = unsafe {
226308
ffi::kevent(
227309
kq,
@@ -239,6 +321,30 @@ pub fn kevent_ts(kq: RawFd,
239321
return Ok(res as usize)
240322
}
241323

324+
#[cfg(target_os = "netbsd")]
325+
pub fn kevent_ts(kq: RawFd,
326+
changelist: &[KEvent],
327+
eventlist: &mut [KEvent],
328+
timeout_opt: Option<timespec>) -> Result<usize> {
329+
330+
let res = unsafe {
331+
ffi::kevent(
332+
kq,
333+
changelist.as_ptr(),
334+
changelist.len() as size_t,
335+
eventlist.as_mut_ptr(),
336+
eventlist.len() as size_t,
337+
if let Some(ref timeout) = timeout_opt {timeout as *const timespec} else {ptr::null()})
338+
};
339+
340+
if res < 0 {
341+
return Err(Error::Sys(Errno::last()));
342+
}
343+
344+
return Ok(res as usize)
345+
}
346+
347+
#[cfg(not(target_os = "netbsd"))]
242348
#[inline]
243349
pub fn ev_set(ev: &mut KEvent,
244350
ident: usize,
@@ -254,3 +360,20 @@ pub fn ev_set(ev: &mut KEvent,
254360
ev.data = 0;
255361
ev.udata = udata;
256362
}
363+
364+
#[cfg(target_os = "netbsd")]
365+
#[inline]
366+
pub fn ev_set(ev: &mut KEvent,
367+
ident: usize,
368+
filter: EventFilter,
369+
flags: EventFlag,
370+
fflags: FilterFlag,
371+
udata: i64) {
372+
373+
ev.ident = ident as uintptr_t;
374+
ev.filter = filter;
375+
ev.flags = flags;
376+
ev.fflags = fflags;
377+
ev.data = 0;
378+
ev.udata = udata;
379+
}

src/sys/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#[cfg(any(target_os = "linux", target_os = "android"))]
33
pub mod epoll;
44

5-
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
5+
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd",
6+
target_os = "dragonfly", target_os = "openbsd", target_os = "netbsd"))]
67
pub mod event;
78

89
// TODO: switch from feature flags to conditional builds

src/sys/socket/consts.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,13 @@ mod os {
208208
pub const IPV6_ADD_MEMBERSHIP: c_int = libc::IPV6_ADD_MEMBERSHIP;
209209
#[cfg(not(target_os = "netbsd"))]
210210
pub const IPV6_DROP_MEMBERSHIP: c_int = libc::IPV6_DROP_MEMBERSHIP;
211-
211+
212+
#[cfg(target_os = "netbsd")]
213+
pub const IPV6_JOIN_GROUP: c_int = 12;
214+
215+
#[cfg(target_os = "netbsd")]
216+
pub const IPV6_LEAVE_GROUP: c_int = 13;
217+
212218
pub type InAddrT = u32;
213219

214220
// Declarations of special addresses

src/sys/socket/sockopt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ sockopt_impl!(SetOnly, IpDropMembership, consts::IPPROTO_IP, consts::IP_DROP_MEM
125125
sockopt_impl!(SetOnly, Ipv6AddMembership, consts::IPPROTO_IPV6, consts::IPV6_ADD_MEMBERSHIP, super::ipv6_mreq);
126126
#[cfg(not(target_os = "netbsd"))]
127127
sockopt_impl!(SetOnly, Ipv6DropMembership, consts::IPPROTO_IPV6, consts::IPV6_DROP_MEMBERSHIP, super::ipv6_mreq);
128+
#[cfg(target_os = "netbsd")]
129+
sockopt_impl!(SetOnly, Ipv6AddMembership, consts::IPPROTO_IPV6, consts::IPV6_JOIN_GROUP, super::ipv6_mreq);
130+
#[cfg(target_os = "netbsd")]
131+
sockopt_impl!(SetOnly, Ipv6DropMembership, consts::IPPROTO_IPV6, consts::IPV6_LEAVE_GROUP, super::ipv6_mreq);
128132
sockopt_impl!(Both, IpMulticastTtl, consts::IPPROTO_IP, consts::IP_MULTICAST_TTL, u8);
129133
sockopt_impl!(Both, IpMulticastLoop, consts::IPPROTO_IP, consts::IP_MULTICAST_LOOP, bool);
130134
sockopt_impl!(Both, ReceiveTimeout, consts::SOL_SOCKET, consts::SO_RCVTIMEO, TimeVal);

0 commit comments

Comments
 (0)