From cc53f4e9f48747d44718bb1b51e549437edae916 Mon Sep 17 00:00:00 2001 From: Adrian Budau Date: Wed, 19 Dec 2018 16:13:43 +0200 Subject: Fix pipe2 and accept4 on static linked executables on linux (like musl). --- src/libstd/sys/unix/weak.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/libstd/sys/unix/weak.rs') diff --git a/src/libstd/sys/unix/weak.rs b/src/libstd/sys/unix/weak.rs index 18944be58ee..3b73e3b7c50 100644 --- a/src/libstd/sys/unix/weak.rs +++ b/src/libstd/sys/unix/weak.rs @@ -77,3 +77,55 @@ unsafe fn fetch(name: &str) -> usize { }; libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize } + +#[cfg(not(target_os = "linux"))] +macro_rules! syscall { + (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( + unsafe fn $name($($arg_name: $t),*) -> $ret { + use libc; + + weak! { fn $name($($t),*) -> $ret } + + if let Some(fun) = $name.get() { + fun($($arg_name),*) + } else { + libc::ENOSYS + } + } + ) +} + +#[cfg(target_os = "linux")] +macro_rules! syscall { + (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( + unsafe fn $name($($arg_name:$t),*) -> $ret { + // This like a hack, but concat_idents only accepts idents + // (not paths). + use libc::*; + + syscall( + concat_idents!(SYS_, $name), + $(::sys::weak::SyscallParam::to_param($arg_name)),* + ) 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) } + } +} -- cgit 1.4.1-3-g733a5