Skip to content

Commit

Permalink
update recv to use correct values on FreeBSD
Browse files Browse the repository at this point in the history
Use correct value for POLLRDHUP and cast fd.len as c_uint.

Signed-off-by: 180watt <180watt@duck.com>
  • Loading branch information
180watt committed Jul 29, 2024
1 parent f47d019 commit 0a84f22
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,18 +1162,36 @@ impl UnixCmsg {
}
},
BlockingMode::Timeout(duration) => {
#[cfg(target_os = "linux")]
let events = libc::POLLIN | libc::POLLPRI | libc::POLLRDHUP;

// It's rather unfortunate that Rust's libc doesn't know that
// FreeBSD has POLLRDHUP, but in the mean time we can add
// the value ourselves.
// https://cgit.freebsd.org/src/tree/sys/sys/poll.h#n72
#[cfg(target_os = "freebsd")]
let events = libc::POLLIN | libc::POLLPRI | 0x4000;

let mut fd = [libc::pollfd {
fd,
events,
revents: 0,
}];
#[cfg(target_os = "linux")]
let result = libc::poll(
fd.as_mut_ptr(),
fd.len() as libc::c_ulong,
duration.as_millis().try_into().unwrap_or(-1),
);

// FreeBSD's nfds_t is an unsigned integer.
#[cfg(target_os = "freebsd")]
let result = libc::poll(
fd.as_mut_ptr(),
fd.len() as libc::c_uint,
duration.as_millis().try_into().unwrap_or(-1),
);

match result.cmp(&0) {
cmp::Ordering::Equal => return Err(UnixError::Errno(EAGAIN)),
cmp::Ordering::Less => return Err(UnixError::last()),
Expand Down

0 comments on commit 0a84f22

Please sign in to comment.