MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / poll

Function poll

src/backend/linux_raw/event/syscalls.rs:19–105  ·  view source on GitHub ↗
(fds: &mut [PollFd<'_>], timeout: Option<&Timespec>)

Source from the content-addressed store, hash-verified

17
18#[inline]
19pub(crate) fn poll(fds: &mut [PollFd<'_>], timeout: Option<&Timespec>) -> io::Result<usize> {
20 let (fds_addr_mut, fds_len) = slice_mut(fds);
21
22 #[cfg(target_pointer_width = "32")]
23 unsafe {
24 // If we don't have Linux 5.1, and the timeout fits in a
25 // `__kernel_old_timespec`, use plain `ppoll`.
26 //
27 // We do this unconditionally, rather than trying `ppoll_time64` and
28 // falling back on `Errno::NOSYS`, because seccomp configurations will
29 // sometimes abort the process on syscalls they don't recognize.
30 #[cfg(not(feature = "linux_5_1"))]
31 {
32 use linux_raw_sys::general::__kernel_old_timespec;
33
34 // If we don't have a timeout, or if we can convert the timeout to
35 // a `__kernel_old_timespec`, the use `__NR_ppoll`.
36 fn convert(timeout: &Timespec) -> Option<__kernel_old_timespec> {
37 Some(__kernel_old_timespec {
38 tv_sec: timeout.tv_sec.try_into().ok()?,
39 tv_nsec: timeout.tv_nsec.try_into().ok()?,
40 })
41 }
42 let old_timeout = if let Some(timeout) = timeout {
43 match convert(timeout) {
44 // Could not convert timeout.
45 None => None,
46 // Could convert timeout. Ok!
47 Some(old_timeout) => Some(Some(old_timeout)),
48 }
49 } else {
50 // No timeout. Ok!
51 Some(None)
52 };
53 if let Some(mut old_timeout) = old_timeout {
54 // Call `ppoll`.
55 //
56 // Linux's `ppoll` mutates the timeout argument. Our public
57 // interface does not do this, because it's not portable to other
58 // platforms, so we create a temporary value to hide this behavior.
59 return ret_usize(syscall!(
60 __NR_ppoll,
61 fds_addr_mut,
62 fds_len,
63 opt_mut(old_timeout.as_mut()),
64 zero(),
65 size_of::<kernel_sigset_t, _>()
66 ));
67 }
68 }
69
70 // We either have Linux 5.1 or the timeout didn't fit in
71 // `__kernel_old_timespec` so `__NR_ppoll_time64` will either
72 // succeed or fail due to our having no other options.
73
74 // Call `ppoll_time64`.
75 //
76 // Linux's `ppoll_time64` mutates the timeout argument. Our public

Callers

nothing calls this directly

Calls 3

slice_mutFunction · 0.85
convertFunction · 0.70
ret_usizeFunction · 0.50

Tested by

no test coverage detected