From bf172c532af44628e3c3323a5f8a8c1726ffbd30 Mon Sep 17 00:00:00 2001 From: Adrian Budau Date: Fri, 21 Dec 2018 15:53:37 +0200 Subject: [PATCH] Properly report ENOSYS by modifying errno --- src/libstd/sys/unix/os.rs | 15 ++++++++++++++- src/libstd/sys/unix/weak.rs | 25 ++++--------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 03e81a720dc..6e8ee445994 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -67,7 +67,8 @@ pub fn errno() -> i32 { } /// Sets the platform-specific value of errno -#[cfg(any(target_os = "solaris", target_os = "fuchsia"))] // only needed for readdir so far +#[cfg(all(not(target_os = "linux"), + not(target_os = "dragonfly")))] // needed for readdir and syscall! pub fn set_errno(e: i32) { unsafe { *errno_location() = e as c_int @@ -84,6 +85,18 @@ pub fn errno() -> i32 { unsafe { errno as i32 } } +#[cfg(target_os = "dragonfly")] +pub fn set_errno(e: i32) { + extern { + #[thread_local] + static mut errno: c_int; + } + + unsafe { + errno = e; + } +} + /// Gets a detailed string description for the given error number. pub fn error_string(errno: i32) -> String { extern { diff --git a/src/libstd/sys/unix/weak.rs b/src/libstd/sys/unix/weak.rs index ab75a39eecc..7d293f1c47a 100644 --- a/src/libstd/sys/unix/weak.rs +++ b/src/libstd/sys/unix/weak.rs @@ -83,13 +83,15 @@ macro_rules! syscall { (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( unsafe fn $name($($arg_name: $t),*) -> $ret { use libc; + use super::os; weak! { fn $name($($t),*) -> $ret } if let Some(fun) = $name.get() { fun($($arg_name),*) } else { - libc::ENOSYS + os::set_errno(libc::ENOSYS); + -1 } } ) @@ -105,27 +107,8 @@ macro_rules! syscall { syscall( concat_idents!(SYS_, $name), - $(::sys::weak::SyscallParam::to_param($arg_name)),* + $($arg_name as c_long),* ) as $ret } ) } - -#[cfg(target_os = "linux")] -pub trait SyscallParam { - fn to_param(self) -> libc::c_long; -} - -#[cfg(target_os = "linux")] -impl SyscallParam for libc::c_int { - fn to_param(self) -> libc::c_long { - self as libc::c_long - } -} - -#[cfg(target_os = "linux")] -impl SyscallParam for *mut T { - fn to_param(self) -> libc::c_long { - unsafe { mem::transmute(self) } - } -}