1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use libc::c_int;
use std::os::unix::prelude::*;
use std::time::Duration;
#[derive(Clone, Copy)]
pub enum Timeout {
Duration(Duration),
Forever,
}
impl Timeout {
pub const ZERO: Timeout = Timeout::Duration(Duration::ZERO);
/// Convert from usecs to poll-friendly msecs.
#[allow(unused)]
fn as_poll_msecs(&self) -> c_int {
match self {
// Negative values mean wait forever in poll-speak.
Timeout::Forever => -1 as c_int,
Timeout::Duration(duration) => {
assert!(
duration.as_millis() < c_int::MAX as _,
"Timeout too long but not forever!"
);
duration.as_millis() as c_int
}
}
}
}
/// Returns `true` if the fd is or becomes readable within the given timeout.
/// This returns `false` if the waiting is interrupted by a signal.
pub fn is_fd_readable(fd: i32, timeout: Timeout) -> bool {
FdReadableSet::is_fd_readable(fd, timeout)
}
/// Returns whether an fd is readable.
pub fn poll_fd_readable(fd: i32) -> bool {
FdReadableSet::poll_fd_readable(fd)
}
/// A modest wrapper around select() or poll().
/// This allows accumulating a set of fds and then seeing if they are readable.
/// This only handles readability.
/// Apple's `man poll`: "The poll() system call currently does not support devices."
#[cfg(apple)]
pub struct FdReadableSet {
// The underlying fdset and nfds value to pass to select().
fdset_: libc::fd_set,
nfds_: c_int,
}
#[cfg(apple)]
impl FdReadableSet {
/// Construct an empty set.
pub fn new() -> FdReadableSet {
let mut fdset_ = std::mem::MaybeUninit::uninit();
let fdset_ = unsafe {
libc::FD_ZERO(fdset_.as_mut_ptr());
fdset_.assume_init()
};
FdReadableSet { fdset_, nfds_: 0 }
}
/// Reset back to an empty set.
pub fn clear(&mut self) {
self.nfds_ = 0;
unsafe {
libc::FD_ZERO(&mut self.fdset_);
}
}
/// Add an fd to the set. The fd is ignored if negative (for convenience).
pub fn add(&mut self, fd: RawFd) {
if fd >= (libc::FD_SETSIZE as RawFd) {
//flogf!(error, "fd %d too large for select()", fd);
return;
}
if fd >= 0 {
unsafe { libc::FD_SET(fd, &mut self.fdset_) };
self.nfds_ = std::cmp::max(self.nfds_, fd + 1);
}
}
/// Returns `true` if the given `fd` is marked as set, in our set. Returns `false` if `fd` is
/// negative.
pub fn test(&self, fd: RawFd) -> bool {
fd >= 0 && unsafe { libc::FD_ISSET(fd, &self.fdset_) }
}
/// Call `select()`. Note this destructively modifies the set. Returns the result of
/// `select()`.
pub fn check_readable(&mut self, timeout: Timeout) -> c_int {
let null = std::ptr::null_mut();
let mut tvs;
let timeout = match timeout {
Timeout::Forever => std::ptr::null_mut(),
Timeout::Duration(duration) => {
tvs = libc::timeval {
tv_sec: duration.as_secs() as libc::time_t,
tv_usec: duration.subsec_micros() as libc::suseconds_t,
};
&mut tvs
}
};
unsafe { libc::select(self.nfds_, &mut self.fdset_, null, null, timeout) }
}
/// Check if a single fd is readable, with a given timeout.
/// Returns `true` if readable, `false` otherwise.
pub fn is_fd_readable(fd: RawFd, timeout: Timeout) -> bool {
if fd < 0 {
return false;
}
let mut s = Self::new();
s.add(fd);
let res = s.check_readable(timeout);
res > 0 && s.test(fd)
}
/// Check if a single fd is readable, without blocking.
/// Returns `true` if readable, `false` if not.
pub fn poll_fd_readable(fd: RawFd) -> bool {
Self::is_fd_readable(fd, Timeout::ZERO)
}
}
#[cfg(not(apple))]
pub struct FdReadableSet {
pollfds_: Vec<libc::pollfd>,
}
#[cfg(not(apple))]
impl FdReadableSet {
/// Construct an empty set.
pub fn new() -> FdReadableSet {
FdReadableSet {
pollfds_: Vec::new(),
}
}
/// Reset back to an empty set.
pub fn clear(&mut self) {
self.pollfds_.clear();
}
#[inline]
fn pollfd_get_fd(pollfd: &libc::pollfd) -> RawFd {
pollfd.fd
}
/// Add an fd to the set. The fd is ignored if negative (for convenience). The fd is also
/// ignored if it's already in the set.
pub fn add(&mut self, fd: RawFd) {
if fd < 0 {
return;
}
let pos = match self.pollfds_.binary_search_by_key(&fd, Self::pollfd_get_fd) {
Ok(_) => return,
Err(pos) => pos,
};
self.pollfds_.insert(
pos,
libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
},
);
}
/// Returns `true` if the given `fd` has input available to read or has been HUP'd.
/// Returns `false` if `fd` is negative or was not found in the set.
pub fn test(&self, fd: RawFd) -> bool {
// If a pipe is widowed with no data, Linux sets POLLHUP but not POLLIN, so test for both.
if let Ok(pos) = self.pollfds_.binary_search_by_key(&fd, Self::pollfd_get_fd) {
let pollfd = &self.pollfds_[pos];
debug_assert_eq!(pollfd.fd, fd);
return pollfd.revents & (libc::POLLIN | libc::POLLHUP) != 0;
}
false
}
fn do_poll(fds: &mut [libc::pollfd], timeout: Timeout) -> c_int {
let count = fds.len();
assert!(count <= libc::nfds_t::MAX as usize, "count too big");
unsafe {
libc::poll(
fds.as_mut_ptr(),
count as libc::nfds_t,
timeout.as_poll_msecs(),
)
}
}
/// Call poll(). Note this destructively modifies the set. Return the result of poll().
pub fn check_readable(&mut self, timeout: Timeout) -> c_int {
if self.pollfds_.is_empty() {
return 0;
}
Self::do_poll(&mut self.pollfds_, timeout)
}
/// Check if a single fd is readable, with a given timeout.
/// Return true if `fd` is our set and is readable, `false` otherwise.
pub fn is_fd_readable(fd: RawFd, timeout: Timeout) -> bool {
if fd < 0 {
return false;
}
let mut pfd = libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
};
let ret = Self::do_poll(std::slice::from_mut(&mut pfd), timeout);
ret > 0 && (pfd.revents & libc::POLLIN) != 0
}
/// Check if a single fd is readable, without blocking.
/// Return true if readable, false if not.
pub fn poll_fd_readable(fd: RawFd) -> bool {
Self::is_fd_readable(fd, Timeout::ZERO)
}
}