Fix pipe2 and accept4 on static linked executables on linux (like musl).
This commit is contained in:
parent
e7b4bc35e9
commit
cc53f4e9f4
4 changed files with 81 additions and 27 deletions
|
@ -250,6 +250,7 @@
|
||||||
#![feature(cfg_target_vendor)]
|
#![feature(cfg_target_vendor)]
|
||||||
#![feature(char_error_internals)]
|
#![feature(char_error_internals)]
|
||||||
#![feature(compiler_builtins_lib)]
|
#![feature(compiler_builtins_lib)]
|
||||||
|
#![feature(concat_idents)]
|
||||||
#![feature(const_int_ops)]
|
#![feature(const_int_ops)]
|
||||||
#![feature(const_ip)]
|
#![feature(const_ip)]
|
||||||
#![feature(const_raw_ptr_deref)]
|
#![feature(const_raw_ptr_deref)]
|
||||||
|
|
|
@ -203,12 +203,16 @@ impl Socket {
|
||||||
// Linux. This was added in 2.6.28, however, and because we support
|
// Linux. This was added in 2.6.28, however, and because we support
|
||||||
// 2.6.18 we must detect this support dynamically.
|
// 2.6.18 we must detect this support dynamically.
|
||||||
if cfg!(target_os = "linux") {
|
if cfg!(target_os = "linux") {
|
||||||
weak! {
|
syscall! {
|
||||||
fn accept4(c_int, *mut sockaddr, *mut socklen_t, c_int) -> c_int
|
fn accept4(
|
||||||
|
fd: c_int,
|
||||||
|
addr: *mut sockaddr,
|
||||||
|
addr_len: *mut socklen_t,
|
||||||
|
flags: c_int
|
||||||
|
) -> c_int
|
||||||
}
|
}
|
||||||
if let Some(accept) = accept4.get() {
|
|
||||||
let res = cvt_r(|| unsafe {
|
let res = cvt_r(|| unsafe {
|
||||||
accept(self.0.raw(), storage, len, SOCK_CLOEXEC)
|
accept4(self.0.raw(), storage, len, SOCK_CLOEXEC)
|
||||||
});
|
});
|
||||||
match res {
|
match res {
|
||||||
Ok(fd) => return Ok(Socket(FileDesc::new(fd))),
|
Ok(fd) => return Ok(Socket(FileDesc::new(fd))),
|
||||||
|
@ -216,7 +220,6 @@ impl Socket {
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let fd = cvt_r(|| unsafe {
|
let fd = cvt_r(|| unsafe {
|
||||||
libc::accept(self.0.raw(), storage, len)
|
libc::accept(self.0.raw(), storage, len)
|
||||||
|
|
|
@ -22,7 +22,7 @@ use sys::{cvt, cvt_r};
|
||||||
pub struct AnonPipe(FileDesc);
|
pub struct AnonPipe(FileDesc);
|
||||||
|
|
||||||
pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
||||||
weak! { fn pipe2(*mut c_int, c_int) -> c_int }
|
syscall! { fn pipe2(fds: *mut c_int, flags: c_int) -> c_int }
|
||||||
static INVALID: AtomicBool = ATOMIC_BOOL_INIT;
|
static INVALID: AtomicBool = ATOMIC_BOOL_INIT;
|
||||||
|
|
||||||
let mut fds = [0; 2];
|
let mut fds = [0; 2];
|
||||||
|
@ -39,13 +39,12 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
||||||
!INVALID.load(Ordering::SeqCst)
|
!INVALID.load(Ordering::SeqCst)
|
||||||
{
|
{
|
||||||
|
|
||||||
if let Some(pipe) = pipe2.get() {
|
|
||||||
// Note that despite calling a glibc function here we may still
|
// Note that despite calling a glibc function here we may still
|
||||||
// get ENOSYS. Glibc has `pipe2` since 2.9 and doesn't try to
|
// get ENOSYS. Glibc has `pipe2` since 2.9 and doesn't try to
|
||||||
// emulate on older kernels, so if you happen to be running on
|
// emulate on older kernels, so if you happen to be running on
|
||||||
// an older kernel you may see `pipe2` as a symbol but still not
|
// an older kernel you may see `pipe2` as a symbol but still not
|
||||||
// see the syscall.
|
// see the syscall.
|
||||||
match cvt(unsafe { pipe(fds.as_mut_ptr(), libc::O_CLOEXEC) }) {
|
match cvt(unsafe { pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) }) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
return Ok((AnonPipe(FileDesc::new(fds[0])),
|
return Ok((AnonPipe(FileDesc::new(fds[0])),
|
||||||
AnonPipe(FileDesc::new(fds[1]))));
|
AnonPipe(FileDesc::new(fds[1]))));
|
||||||
|
@ -56,7 +55,6 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
|
cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
|
||||||
|
|
||||||
let fd0 = FileDesc::new(fds[0]);
|
let fd0 = FileDesc::new(fds[0]);
|
||||||
|
|
|
@ -77,3 +77,55 @@ unsafe fn fetch(name: &str) -> usize {
|
||||||
};
|
};
|
||||||
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as 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<T> SyscallParam for *mut T {
|
||||||
|
fn to_param(self) -> libc::c_long {
|
||||||
|
unsafe { mem::transmute(self) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue