Skip to content
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

Use libc in poll.rs #399

Merged
merged 2 commits into from
Aug 31, 2016
Merged
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#407](https://github.com/nix-rust/nix/pull/407))
- Added `CpuSet::unset` in `::nix::sched`.
([#402](https://github.com/nix-rust/nix/pull/402))
- Added constructor method `new()` to `PollFd` in `::nix::poll`, in order to
allow creation of objects, after removing public access to members.
([#399](https://github.com/nix-rust/nix/pull/399))
- Added method `revents()` to `PollFd` in `::nix::poll`, in order to provide
read access to formerly public member `revents`.
([#399](https://github.com/nix-rust/nix/pull/399))

### Changed
- Replaced the reexported integer constants for signals by the enumeration
Expand All @@ -40,6 +46,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#362](https://github.com/nix-rust/nix/pull/362))
- Type alias `CpuMask` from `::nix::shed`.
([#402](https://github.com/nix-rust/nix/pull/402))
- Removed public fields from `PollFd` in `::nix::poll`. (See also added method
`revents()`.
([#399](https://github.com/nix-rust/nix/pull/399))

### Fixed
- Fixed the build problem for NetBSD (Note, that we currently do not support
Expand Down
92 changes: 33 additions & 59 deletions src/poll.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,48 @@
use libc::c_int;
use libc;
use {Errno, Result};

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};
#[repr(C)]
#[derive(Clone, Copy)]
pub struct PollFd {
pollfd: libc::pollfd,
}

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,
}
impl PollFd {
pub fn new(fd: libc::c_int, events: EventFlags, revents: EventFlags) -> PollFd {
PollFd {
pollfd: libc::pollfd {
fd: fd,
events: events.bits(),
revents: revents.bits(),
},
}

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;
pub fn revents(&self) -> Option<EventFlags> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll also need at least read/write access to events and possibly write access to revents. This looks a little less ergonomic than direct access to the fields we have right now :( Maybe we can keep the struct definition as is? Are there any reasons to switch other than libc having a better test suite to verify the structure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that, but I could only come up with read access to revents as necessary after creation. So, thank you for the feedback.
You text sounds like you have used poll somewhere. Can you point me code that uses it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these bindings to nix in #228 as I planned to use it in a project but I never got around to it since then. A quick code search on Github shows atleast 2 people are using it: https://github.com/tailhook/stator and https://github.com/hjr3/carp-rs; they might have some useful feedback :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both example only need read access to revents after creation.

EventFlags::from_bits(self.pollfd.revents)
}
}

#[allow(improper_ctypes)]
extern {
pub fn poll(fds: *mut PollFd, nfds: nfds_t, timeout: c_int) -> c_int;
libc_bitflags! {
flags EventFlags: libc::c_short {
POLLIN,
POLLPRI,
POLLOUT,
POLLRDNORM,
POLLWRNORM,
POLLRDBAND,
POLLWRBAND,
POLLERR,
POLLHUP,
POLLNVAL,
}
}

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

Errno::result(res)
Expand Down
10 changes: 3 additions & 7 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ 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 mut fds = [PollFd::new(r, POLLIN, EventFlags::empty())];

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

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

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