Skip to content

Commit d48d2e3

Browse files
committed
select: add pselect syscall
This is a straight port of @abbradar's work in #276, with two (somewhat weak) tests and a bit of documentation.
1 parent c2fb79e commit d48d2e3

File tree

1 file changed

+120
-2
lines changed

1 file changed

+120
-2
lines changed

src/sys/select.rs

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::mem;
22
use std::os::unix::io::RawFd;
3-
use std::ptr::null_mut;
3+
use std::ptr::{null, null_mut};
44
use libc::{self, c_int};
55
use Result;
66
use errno::Errno;
7-
use sys::time::TimeVal;
7+
use sys::signal::SigSet;
8+
use sys::time::{TimeSpec, TimeVal};
89

910
pub use libc::FD_SETSIZE;
1011

@@ -131,6 +132,78 @@ where
131132
Errno::result(res)
132133
}
133134

135+
/// Monitors file descriptors for readiness with an altered signal mask.
136+
///
137+
/// Returns the total number of ready file descriptors in all sets. The sets are changed so that all
138+
/// file descriptors that are ready for the given operation are set.
139+
///
140+
/// When this function returns, the original signal mask is restored.
141+
///
142+
/// Unlike [`select`](#fn.select), `pselect` does not mutate the `timeout` value.
143+
///
144+
/// # Parameters
145+
///
146+
/// * `nfds`: The highest file descriptor set in any of the passed `FdSet`s, plus 1. If `None`, this
147+
/// is calculated automatically by calling [`FdSet::highest`] on all descriptor sets and adding 1
148+
/// to the maximum of that.
149+
/// * `readfds`: File descriptors to check for being ready to read.
150+
/// * `writefds`: File descriptors to check for being ready to write.
151+
/// * `errorfds`: File descriptors to check for pending error conditions.
152+
/// * `timeout`: Maximum time to wait for descriptors to become ready (`None` to block
153+
/// indefinitely).
154+
/// * `sigmask`: Signal mask to activate while waiting for file descriptors to turn
155+
/// ready (`None` to set no alternative signal mask).
156+
///
157+
/// # References
158+
///
159+
/// [pselect(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pselect.html)
160+
///
161+
/// [The new pselect() system call](https://lwn.net/Articles/176911/)
162+
///
163+
/// [`FdSet::highest`]: struct.FdSet.html#method.highest
164+
pub fn pselect<'a, N, R, W, E, T, S>(nfds: N,
165+
readfds: R,
166+
writefds: W,
167+
errorfds: E,
168+
timeout: T,
169+
sigmask: S) -> Result<c_int>
170+
where
171+
N: Into<Option<c_int>>,
172+
R: Into<Option<&'a mut FdSet>>,
173+
W: Into<Option<&'a mut FdSet>>,
174+
E: Into<Option<&'a mut FdSet>>,
175+
T: Into<Option<&'a TimeSpec>>,
176+
S: Into<Option<&'a SigSet>>,
177+
{
178+
let mut readfds = readfds.into();
179+
let mut writefds = writefds.into();
180+
let mut errorfds = errorfds.into();
181+
let sigmask = sigmask.into();
182+
let timeout = timeout.into();
183+
184+
let nfds = nfds.into().unwrap_or_else(|| {
185+
readfds.iter_mut()
186+
.chain(writefds.iter_mut())
187+
.chain(errorfds.iter_mut())
188+
.map(|set| set.highest().unwrap_or(-1))
189+
.max()
190+
.unwrap_or(-1) + 1
191+
});
192+
193+
let readfds = readfds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut());
194+
let writefds = writefds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut());
195+
let errorfds = errorfds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut());
196+
let timeout = timeout.map(|ts| ts.as_ref() as *const libc::timespec).unwrap_or(null());
197+
let sigmask = sigmask.map(|sm| sm.as_ref() as *const libc::sigset_t).unwrap_or(null());
198+
199+
let res = unsafe {
200+
libc::pselect(nfds, readfds, writefds, errorfds, timeout, sigmask)
201+
};
202+
203+
Errno::result(res)
204+
}
205+
206+
134207
#[cfg(test)]
135208
mod tests {
136209
use super::*;
@@ -227,6 +300,29 @@ mod tests {
227300
assert!(!fd_set.contains(r2));
228301
}
229302

303+
#[test]
304+
#[cfg_attr(any(target_arch = "powerpc", target_arch = "mips"), ignore)]
305+
fn test_pselect() {
306+
let (r1, w1) = pipe().unwrap();
307+
write(w1, b"hi!").unwrap();
308+
let (r2, _w2) = pipe().unwrap();
309+
310+
let mut fd_set = FdSet::new();
311+
fd_set.insert(r1);
312+
fd_set.insert(r2);
313+
314+
let timeout = TimeSpec::seconds(10);
315+
let sigmask = SigSet::empty();
316+
assert_eq!(1, pselect(None,
317+
&mut fd_set,
318+
None,
319+
None,
320+
&timeout,
321+
&sigmask).unwrap());
322+
assert!(fd_set.contains(r1));
323+
assert!(!fd_set.contains(r2));
324+
}
325+
230326
#[test]
231327
#[cfg_attr(any(target_arch = "powerpc", target_arch = "mips"), ignore)]
232328
fn test_select_nfds() {
@@ -268,4 +364,26 @@ mod tests {
268364
assert!(fd_set.contains(r1));
269365
assert!(!fd_set.contains(r2));
270366
}
367+
368+
#[test]
369+
#[cfg_attr(any(target_arch = "powerpc", target_arch = "mips"), ignore)]
370+
fn test_pselect_nfds2() {
371+
let (r1, w1) = pipe().unwrap();
372+
write(w1, b"hi!").unwrap();
373+
let (r2, _w2) = pipe().unwrap();
374+
375+
let mut fd_set = FdSet::new();
376+
fd_set.insert(r1);
377+
fd_set.insert(r2);
378+
379+
let timeout = TimeSpec::seconds(10);
380+
assert_eq!(1, pselect(::std::cmp::max(r1, r2) + 1,
381+
&mut fd_set,
382+
None,
383+
None,
384+
&timeout,
385+
None).unwrap());
386+
assert!(fd_set.contains(r1));
387+
assert!(!fd_set.contains(r2));
388+
}
271389
}

0 commit comments

Comments
 (0)