From 68d2a8490c1b49e4b23731089423c278a5ed9a0b Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Mon, 6 Jan 2020 17:02:25 -0800 Subject: [PATCH] dragonfly: Don't try to read errno value We don't have a way to do it on stable Rust. Signed-off-by: Joe Richey --- src/util_libc.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/util_libc.rs b/src/util_libc.rs index 5a051701..3fa11bcd 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -18,18 +18,27 @@ cfg_if! { use libc::__errno_location as errno_location; } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] { use libc::___errno as errno_location; - } else if #[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "dragonfly"))] { + } else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] { use libc::__error as errno_location; } else if #[cfg(target_os = "haiku")] { use libc::_errnop as errno_location; } } +cfg_if! { + if #[cfg(target_os = "vxworks")] { + use libc::errnoGet as get_errno; + } else if #[cfg(target_os = "dragonfly")] { + // Until rust-lang/rust#29594 is stable, we cannot get the errno value + // on DragonFlyBSD. So we just return an out-of-range errno. + unsafe fn get_errno() -> libc::c_int { -1 } + } else { + unsafe fn get_errno() -> libc::c_int { *errno_location() } + } +} + pub fn last_os_error() -> Error { - #[cfg(not(target_os = "vxworks"))] - let errno = unsafe { *errno_location() }; - #[cfg(target_os = "vxworks")] - let errno = unsafe { libc::errnoGet() }; + let errno = unsafe { get_errno() }; if errno > 0 { Error::from(NonZeroU32::new(errno as u32).unwrap()) } else {