Skip to content

Add everything from poll.h. #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub mod mount;
#[cfg(any(target_os = "linux"))]
pub mod mqueue;

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub mod poll;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod sched;

Expand Down
80 changes: 80 additions & 0 deletions src/poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use libc::c_int;
use {Error, Result};
use errno::Errno;

pub use self::ffi::PollFd;
pub use self::ffi::consts::*;

mod ffi {
use libc::c_int;
pub use self::consts::*;

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct PollFd {
pub fd: c_int,
pub events: EventFlags,
pub revents: EventFlags
}

#[cfg(target_os = "linux")]
pub mod consts {
use libc::{c_short, c_ulong};

bitflags! {
flags EventFlags: c_short {
const POLLIN = 0x001,
const POLLPRI = 0x002,
const POLLOUT = 0x004,
const POLLRDNORM = 0x040,
const POLLWRNORM = 0x100,
const POLLRDBAND = 0x080,
const POLLWRBAND = 0x200,
const POLLERR = 0x008,
const POLLHUP = 0x010,
const POLLNVAL = 0x020,
}
}

pub type nfds_t = c_ulong;
}

#[cfg(target_os = "macos")]
pub mod consts {
use libc::{c_short, c_uint};

bitflags! {
flags EventFlags: c_short {
const POLLIN = 0x0001,
const POLLPRI = 0x0002,
const POLLOUT = 0x0004,
const POLLRDNORM = 0x0040,
const POLLWRNORM = 0x0004,
const POLLRDBAND = 0x0080,
const POLLWRBAND = 0x0100,
const POLLERR = 0x0008,
const POLLHUP = 0x0010,
const POLLNVAL = 0x0020,
}
}

pub type nfds_t = c_uint;
}

#[allow(improper_ctypes)]
extern {
pub fn poll(fds: *mut PollFd, nfds: nfds_t, timeout: c_int) -> c_int;
}
}

pub fn poll(fds: &mut [PollFd], timeout: c_int) -> Result<c_int> {
let res = unsafe {
ffi::poll(fds.as_mut_ptr(), fds.len() as ffi::nfds_t, timeout)
};

if res < 0 {
return Err(Error::Sys(Errno::last()));
}

Ok(res)
}
3 changes: 3 additions & 0 deletions test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ mod test_unistd;
#[cfg(any(target_os = "linux"))]
mod test_mq;

#[cfg(any(target_os = "linux", target_os = "macos"))]
mod test_poll;

mod ports {
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
use std::sync::atomic::Ordering::SeqCst;
Expand Down
22 changes: 22 additions & 0 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use nix::poll::*;
use nix::unistd::{write, pipe};

#[test]
fn test_poll() {
let (r, w) = pipe().unwrap();
let mut fds = [PollFd {
fd: r,
events: POLLIN,
revents: EventFlags::empty()
}];

let nfds = poll(&mut fds, 100).unwrap();
assert_eq!(nfds, 0);
assert!(!fds[0].revents.contains(POLLIN));

write(w, b".").unwrap();

let nfds = poll(&mut fds, 100).unwrap();
assert_eq!(nfds, 1);
assert!(fds[0].revents.contains(POLLIN));
}