Skip to content

Commit 6cd56ed

Browse files
committed
Support nullable timeout in ppoll
1 parent a777389 commit 6cd56ed

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3636
- Changed `fallocate` return type from `c_int` to `()` (#[1201](https://github.com/nix-rust/nix/pull/1201))
3737
- Enabled `sys::ptrace::setregs` and `sys::ptrace::getregs` on x86_64-unknown-linux-musl target
3838
(#[1198](https://github.com/nix-rust/nix/pull/1198))
39-
- On Linux, `ptrace::write` is now an `unsafe` function. Caveat programmer.
39+
- On Linux, `ptrace::write` is now an `unsafe` function. Caveat programmer.
4040
(#[1245](https://github.com/nix-rust/nix/pull/1245))
4141
- `execv`, `execve`, `execvp` and `execveat` in `::nix::unistd` and `reboot` in
4242
`::nix::sys::reboot` now return `Result<Infallible>` instead of `Result<Void>` (#[1239](https://github.com/nix-rust/nix/pull/1239))
43+
- `nix::poll::ppoll`: `timeout` parameter is now optional, None is equivalent for infinite timeout.
4344

4445
### Fixed
4546

src/poll.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,20 @@ pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> {
130130
/// ([`poll(2)`](http://man7.org/linux/man-pages/man2/poll.2.html))
131131
///
132132
/// `ppoll` behaves like `poll`, but let you specify what signals may interrupt it
133-
/// with the `sigmask` argument.
133+
/// with the `sigmask` argument. If you want `ppoll` to block indefinitely,
134+
/// specify `None` as `timeout` (it is like `timeout = -1` for `poll`).
134135
///
135136
#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
136-
pub fn ppoll(fds: &mut [PollFd], timeout: TimeSpec, sigmask: SigSet) -> Result<libc::c_int> {
137-
137+
pub fn ppoll(fds: &mut [PollFd], timeout: Option<TimeSpec>, sigmask: SigSet) -> Result<libc::c_int> {
138+
let timeout = match timeout {
139+
Some(t) => t.as_ref(),
140+
None => core::ptr::null()
141+
};
138142

139143
let res = unsafe {
140144
libc::ppoll(fds.as_mut_ptr() as *mut libc::pollfd,
141145
fds.len() as libc::nfds_t,
142-
timeout.as_ref(),
146+
timeout,
143147
sigmask.as_ref())
144148
};
145149
Errno::result(res)

test/test_poll.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ fn test_ppoll() {
3737
let mut fds = [PollFd::new(r, PollFlags::POLLIN)];
3838

3939
// Poll an idle pipe. Should timeout
40-
let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap();
40+
let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap();
4141
assert_eq!(nfds, 0);
4242
assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));
4343

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

4646
// Poll a readable pipe. Should return an event.
47-
let nfds = ppoll(&mut fds, timeout, SigSet::empty()).unwrap();
47+
let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap();
4848
assert_eq!(nfds, 1);
4949
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
5050
}

0 commit comments

Comments
 (0)