Skip to content

Commit cec5c61

Browse files
utkarshkukretikamalmarhubi
authored andcommitted
Add everything from poll.h.
1 parent 5700e39 commit cec5c61

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub mod mount;
3131
#[cfg(any(target_os = "linux"))]
3232
pub mod mqueue;
3333

34+
#[cfg(any(target_os = "linux", target_os = "macos"))]
35+
pub mod poll;
36+
3437
#[cfg(any(target_os = "linux", target_os = "android"))]
3538
pub mod sched;
3639

src/poll.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use libc::c_int;
2+
use {Error, Result};
3+
use errno::Errno;
4+
5+
pub use self::ffi::PollFd;
6+
pub use self::ffi::consts::*;
7+
8+
mod ffi {
9+
use libc::c_int;
10+
pub use self::consts::*;
11+
12+
#[derive(Clone, Copy, Debug)]
13+
#[repr(C)]
14+
pub struct PollFd {
15+
pub fd: c_int,
16+
pub events: EventFlags,
17+
pub revents: EventFlags
18+
}
19+
20+
#[cfg(target_os = "linux")]
21+
pub mod consts {
22+
use libc::{c_short, c_ulong};
23+
24+
bitflags! {
25+
flags EventFlags: c_short {
26+
const POLLIN = 0x001,
27+
const POLLPRI = 0x002,
28+
const POLLOUT = 0x004,
29+
const POLLRDNORM = 0x040,
30+
const POLLWRNORM = 0x100,
31+
const POLLRDBAND = 0x080,
32+
const POLLWRBAND = 0x200,
33+
const POLLERR = 0x008,
34+
const POLLHUP = 0x010,
35+
const POLLNVAL = 0x020,
36+
}
37+
}
38+
39+
pub type nfds_t = c_ulong;
40+
}
41+
42+
#[cfg(target_os = "macos")]
43+
pub mod consts {
44+
use libc::{c_short, c_uint};
45+
46+
bitflags! {
47+
flags EventFlags: c_short {
48+
const POLLIN = 0x0001,
49+
const POLLPRI = 0x0002,
50+
const POLLOUT = 0x0004,
51+
const POLLRDNORM = 0x0040,
52+
const POLLWRNORM = 0x0004,
53+
const POLLRDBAND = 0x0080,
54+
const POLLWRBAND = 0x0100,
55+
const POLLERR = 0x0008,
56+
const POLLHUP = 0x0010,
57+
const POLLNVAL = 0x0020,
58+
}
59+
}
60+
61+
pub type nfds_t = c_uint;
62+
}
63+
64+
#[allow(improper_ctypes)]
65+
extern {
66+
pub fn poll(fds: *mut PollFd, nfds: nfds_t, timeout: c_int) -> c_int;
67+
}
68+
}
69+
70+
pub fn poll(fds: &mut [PollFd], timeout: c_int) -> Result<c_int> {
71+
let res = unsafe {
72+
ffi::poll(fds.as_mut_ptr(), fds.len() as ffi::nfds_t, timeout)
73+
};
74+
75+
if res < 0 {
76+
return Err(Error::Sys(Errno::last()));
77+
}
78+
79+
Ok(res)
80+
}

test/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ mod test_unistd;
1414
#[cfg(any(target_os = "linux"))]
1515
mod test_mq;
1616

17+
#[cfg(any(target_os = "linux", target_os = "macos"))]
18+
mod test_poll;
19+
1720
mod ports {
1821
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
1922
use std::sync::atomic::Ordering::SeqCst;

test/test_poll.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use nix::poll::*;
2+
use nix::unistd::{write, pipe};
3+
4+
#[test]
5+
fn test_poll() {
6+
let (r, w) = pipe().unwrap();
7+
let mut fds = [PollFd {
8+
fd: r,
9+
events: POLLIN,
10+
revents: EventFlags::empty()
11+
}];
12+
13+
let nfds = poll(&mut fds, 100).unwrap();
14+
assert_eq!(nfds, 0);
15+
assert!(!fds[0].revents.contains(POLLIN));
16+
17+
write(w, b".").unwrap();
18+
19+
let nfds = poll(&mut fds, 100).unwrap();
20+
assert_eq!(nfds, 1);
21+
assert!(fds[0].revents.contains(POLLIN));
22+
}

0 commit comments

Comments
 (0)