Skip to content

Commit

Permalink
Properly report ENOSYS by modifying errno
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-budau committed Dec 21, 2018
1 parent da47bd3 commit bf172c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
15 changes: 14 additions & 1 deletion src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
25 changes: 4 additions & 21 deletions src/libstd/sys/unix/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
)
Expand All @@ -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<T> SyscallParam for *mut T {
fn to_param(self) -> libc::c_long {
unsafe { mem::transmute(self) }
}
}

0 comments on commit bf172c5

Please sign in to comment.