Skip to content

Commit cbcc42d

Browse files
committed
Support nullable timeout in ppoll
1 parent 57b6bd7 commit cbcc42d

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4848
- Changed `fallocate` return type from `c_int` to `()` (#[1201](https://github.com/nix-rust/nix/pull/1201))
4949
- Enabled `sys::ptrace::setregs` and `sys::ptrace::getregs` on x86_64-unknown-linux-musl target
5050
(#[1198](https://github.com/nix-rust/nix/pull/1198))
51-
- On Linux, `ptrace::write` is now an `unsafe` function. Caveat programmer.
51+
- On Linux, `ptrace::write` is now an `unsafe` function. Caveat programmer.
5252
(#[1245](https://github.com/nix-rust/nix/pull/1245))
5353
- `execv`, `execve`, `execvp` and `execveat` in `::nix::unistd` and `reboot` in
5454
`::nix::sys::reboot` now return `Result<Infallible>` instead of `Result<Void>` (#[1239](https://github.com/nix-rust/nix/pull/1239))
@@ -61,6 +61,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
6161
(#[1244](https://github.com/nix-rust/nix/pull/1244))
6262
- Several `Inotify` methods now take `self` by value instead of by reference
6363
(#[1244](https://github.com/nix-rust/nix/pull/1244))
64+
- `nix::poll::ppoll`: `timeout` parameter is now optional, None is equivalent for infinite timeout.
6465

6566
### Fixed
6667

src/poll.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ 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-
138-
137+
pub fn ppoll(fds: &mut [PollFd], timeout: Option<TimeSpec>, sigmask: SigSet) -> Result<libc::c_int> {
138+
let timeout = timeout.as_ref().map_or(core::ptr::null(), |r| r.as_ref());
139139
let res = unsafe {
140140
libc::ppoll(fds.as_mut_ptr() as *mut libc::pollfd,
141141
fds.len() as libc::nfds_t,
142-
timeout.as_ref(),
142+
timeout,
143143
sigmask.as_ref())
144144
};
145145
Errno::result(res)

test/test_poll.rs

+2-2
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)