From 0a84f225f3a55c673450342a297c98f3cc569dcf Mon Sep 17 00:00:00 2001 From: 180watt <160363743+180watt@users.noreply.github.com> Date: Sun, 28 Jul 2024 19:10:06 -0400 Subject: [PATCH] update recv to use correct values on FreeBSD Use correct value for POLLRDHUP and cast fd.len as c_uint. Signed-off-by: 180watt <180watt@duck.com> --- src/platform/unix/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/platform/unix/mod.rs b/src/platform/unix/mod.rs index c5b71670..194da613 100644 --- a/src/platform/unix/mod.rs +++ b/src/platform/unix/mod.rs @@ -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()),