From 6cd56edadf8342b6face0a1d7b34026f20eb655e Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Sun, 5 Jul 2020 23:36:35 +0300 Subject: [PATCH] Support nullable timeout in ppoll --- CHANGELOG.md | 3 ++- src/poll.rs | 12 ++++++++---- test/test_poll.rs | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e2b5ba1a..4b5c3223f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,10 +36,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Changed `fallocate` return type from `c_int` to `()` (#[1201](https://github.com/nix-rust/nix/pull/1201)) - Enabled `sys::ptrace::setregs` and `sys::ptrace::getregs` on x86_64-unknown-linux-musl target (#[1198](https://github.com/nix-rust/nix/pull/1198)) -- On Linux, `ptrace::write` is now an `unsafe` function. Caveat programmer. +- On Linux, `ptrace::write` is now an `unsafe` function. Caveat programmer. (#[1245](https://github.com/nix-rust/nix/pull/1245)) - `execv`, `execve`, `execvp` and `execveat` in `::nix::unistd` and `reboot` in `::nix::sys::reboot` now return `Result` instead of `Result` (#[1239](https://github.com/nix-rust/nix/pull/1239)) +- `nix::poll::ppoll`: `timeout` parameter is now optional, None is equivalent for infinite timeout. ### Fixed diff --git a/src/poll.rs b/src/poll.rs index e276090eb2..40f963c833 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -130,16 +130,20 @@ pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result { /// ([`poll(2)`](http://man7.org/linux/man-pages/man2/poll.2.html)) /// /// `ppoll` behaves like `poll`, but let you specify what signals may interrupt it -/// with the `sigmask` argument. +/// with the `sigmask` argument. If you want `ppoll` to block indefinitely, +/// specify `None` as `timeout` (it is like `timeout = -1` for `poll`). /// #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))] -pub fn ppoll(fds: &mut [PollFd], timeout: TimeSpec, sigmask: SigSet) -> Result { - +pub fn ppoll(fds: &mut [PollFd], timeout: Option, sigmask: SigSet) -> Result { + let timeout = match timeout { + Some(t) => t.as_ref(), + None => core::ptr::null() + }; let res = unsafe { libc::ppoll(fds.as_mut_ptr() as *mut libc::pollfd, fds.len() as libc::nfds_t, - timeout.as_ref(), + timeout, sigmask.as_ref()) }; Errno::result(res) diff --git a/test/test_poll.rs b/test/test_poll.rs index aef40e4792..d1974acfc6 100644 --- a/test/test_poll.rs +++ b/test/test_poll.rs @@ -37,14 +37,14 @@ fn test_ppoll() { let mut fds = [PollFd::new(r, PollFlags::POLLIN)]; // Poll an idle pipe. Should timeout - let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap(); + let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap(); assert_eq!(nfds, 0); assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN)); write(w, b".").unwrap(); // Poll a readable pipe. Should return an event. - let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap(); + let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap(); assert_eq!(nfds, 1); assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN)); }