From f50a3be13a202c4436b26e883c20ab43a43cc134 Mon Sep 17 00:00:00 2001 From: Jonah Petri Date: Tue, 7 Dec 2021 16:57:52 -0500 Subject: [PATCH] uclibc support --- src/sys/mod.rs | 2 +- src/sys/personality.rs | 4 ++-- src/sys/ptrace/linux.rs | 7 ++++--- src/sys/resource.rs | 10 +++++----- src/sys/signal.rs | 16 ++++++++++++---- src/sys/socket/addr.rs | 4 ++-- src/sys/statfs.rs | 29 +++++++++++++++++++++++++---- src/sys/termios.rs | 2 +- src/sys/uio.rs | 8 ++++++-- 9 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 156b0d9d1c..a450ac5f65 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -2,7 +2,7 @@ #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "linux", + not(all(target_os = "linux", target_env = "uclibc")), target_os = "macos", target_os = "netbsd"))] pub mod aio; diff --git a/src/sys/personality.rs b/src/sys/personality.rs index b15956c469..e64c906d1a 100644 --- a/src/sys/personality.rs +++ b/src/sys/personality.rs @@ -11,13 +11,13 @@ libc_bitflags! { ADDR_NO_RANDOMIZE; ADDR_LIMIT_32BIT; ADDR_LIMIT_3GB; - #[cfg(not(target_env = "musl"))] + #[cfg(not(any(target_env = "musl", target_env = "uclibc")))] FDPIC_FUNCPTRS; MMAP_PAGE_ZERO; READ_IMPLIES_EXEC; SHORT_INODE; STICKY_TIMEOUTS; - #[cfg(not(target_env = "musl"))] + #[cfg(not(any(target_env = "musl", target_env = "uclibc")))] UNAME26; WHOLE_SECONDS; } diff --git a/src/sys/ptrace/linux.rs b/src/sys/ptrace/linux.rs index 37236790b4..6cc4fa87c5 100644 --- a/src/sys/ptrace/linux.rs +++ b/src/sys/ptrace/linux.rs @@ -20,7 +20,8 @@ use libc::user_regs_struct; cfg_if! { if #[cfg(any(all(target_os = "linux", target_arch = "s390x"), - all(target_os = "linux", target_env = "gnu")))] { + all(target_os = "linux", target_env = "gnu"), + target_env = "uclibc"))] { #[doc(hidden)] pub type RequestType = ::libc::c_uint; } else { @@ -30,8 +31,8 @@ cfg_if! { } libc_enum!{ - #[cfg_attr(not(any(target_env = "musl", target_os = "android")), repr(u32))] - #[cfg_attr(any(target_env = "musl", target_os = "android"), repr(i32))] + #[cfg_attr(not(any(target_env = "musl", target_env = "uclibc", target_os = "android")), repr(u32))] + #[cfg_attr(any(target_env = "musl", target_env = "uclibc", target_os = "android"), repr(i32))] /// Ptrace Request enum defining the action to be taken. #[non_exhaustive] pub enum Request { diff --git a/src/sys/resource.rs b/src/sys/resource.rs index f3bfb67194..b725d5a8a5 100644 --- a/src/sys/resource.rs +++ b/src/sys/resource.rs @@ -7,9 +7,9 @@ pub use libc::rlim_t; use std::mem; cfg_if! { - if #[cfg(all(target_os = "linux", target_env = "gnu"))]{ + if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]{ use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY}; - }else if #[cfg(any( + } else if #[cfg(any( target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", @@ -168,9 +168,9 @@ pub fn getrlimit(resource: Resource) -> Result<(Option, Option)> let mut old_rlim = mem::MaybeUninit::::uninit(); cfg_if! { - if #[cfg(all(target_os = "linux", target_env = "gnu"))]{ + if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]{ let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, old_rlim.as_mut_ptr()) }; - }else{ + } else { let res = unsafe { libc::getrlimit(resource as c_int, old_rlim.as_mut_ptr()) }; } } @@ -222,7 +222,7 @@ pub fn setrlimit( rlim_max: hard_limit.unwrap_or(RLIM_INFINITY), }; cfg_if! { - if #[cfg(all(target_os = "linux", target_env = "gnu"))]{ + if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]{ let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &new_rlim as *const rlimit) }; }else{ let res = unsafe { libc::setrlimit(resource as c_int, &new_rlim as *const rlimit) }; diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 61bdc74aef..14a9ba74f3 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -12,6 +12,7 @@ use std::str::FromStr; #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] use std::os::unix::io::RawFd; use std::ptr; +use cfg_if::cfg_if; #[cfg(not(any(target_os = "openbsd", target_os = "redox")))] pub use self::sigevent::*; @@ -403,10 +404,15 @@ pub const SIGPOLL : Signal = SIGIO; /// Alias for [`SIGSYS`] pub const SIGUNUSED : Signal = SIGSYS; -#[cfg(not(target_os = "redox"))] -type SaFlags_t = libc::c_int; -#[cfg(target_os = "redox")] -type SaFlags_t = libc::c_ulong; +cfg_if! { + if #[cfg(target_os = "redox")] { + type SaFlags_t = libc::c_ulong; + } else if #[cfg(target_env = "uclibc")] { + type SaFlags_t = libc::c_ulong; + } else { + type SaFlags_t = libc::c_int; + } +} libc_bitflags!{ /// Controls the behavior of a [`SigAction`] @@ -1005,6 +1011,8 @@ mod sigevent { SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID, #[cfg(all(target_os = "linux", target_env = "gnu", not(target_arch = "mips")))] SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID, + #[cfg(all(target_os = "linux", target_env = "uclibc"))] + SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID, #[cfg(any(all(target_os = "linux", target_env = "musl"), target_arch = "mips"))] SigevNotify::SigevThreadId{..} => 4 // No SIGEV_THREAD_ID defined }; diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index b119642b3f..82619ec9d9 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -97,9 +97,9 @@ pub enum AddressFamily { Wanpipe = libc::AF_WANPIPE, #[cfg(any(target_os = "android", target_os = "linux"))] Llc = libc::AF_LLC, - #[cfg(target_os = "linux")] + #[cfg(all(target_os = "linux",not(target_env = "uclibc")))] Ib = libc::AF_IB, - #[cfg(target_os = "linux")] + #[cfg(all(target_os = "linux",not(target_env = "uclibc")))] Mpls = libc::AF_MPLS, #[cfg(any(target_os = "android", target_os = "linux"))] Can = libc::AF_CAN, diff --git a/src/sys/statfs.rs b/src/sys/statfs.rs index 829be57f63..89eef3a043 100644 --- a/src/sys/statfs.rs +++ b/src/sys/statfs.rs @@ -29,7 +29,9 @@ type fs_type_t = libc::c_ulong; type fs_type_t = libc::c_uint; #[cfg(all(target_os = "linux", target_env = "musl"))] type fs_type_t = libc::c_ulong; -#[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))] +#[cfg(all(target_os = "linux", target_env = "uclibc"))] +type fs_type_t = libc::c_int; +#[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl", target_env = "uclibc"))))] type fs_type_t = libc::__fsword_t; /// Describes the file system type as known by the operating system. @@ -181,11 +183,17 @@ impl Statfs { } /// Optimal transfer block size - #[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))] + #[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl", target_env = "uclibc"))))] pub fn optimal_transfer_size(&self) -> libc::__fsword_t { self.0.f_bsize } + /// Optimal transfer block size + #[cfg(all(target_os = "linux", target_env = "uclibc"))] + pub fn optimal_transfer_size(&self) -> libc::c_int { + self.0.f_bsize + } + /// Optimal transfer block size #[cfg(target_os = "dragonfly")] pub fn optimal_transfer_size(&self) -> libc::c_long { @@ -220,7 +228,14 @@ impl Statfs { /// Size of a block // f_bsize on linux: https://github.com/torvalds/linux/blob/master/fs/nfs/super.c#L471 - #[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))] + #[cfg(all(target_os = "linux", target_env = "uclibc"))] + pub fn block_size(&self) -> libc::c_int { + self.0.f_bsize + } + + /// Size of a block + // f_bsize on linux: https://github.com/torvalds/linux/blob/master/fs/nfs/super.c#L471 + #[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl", target_env = "uclibc"))))] pub fn block_size(&self) -> libc::__fsword_t { self.0.f_bsize } @@ -262,7 +277,13 @@ impl Statfs { } /// Maximum length of filenames - #[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))] + #[cfg(all(target_os = "linux", target_env = "uclibc"))] + pub fn maximum_name_length(&self) -> libc::c_int { + self.0.f_namelen + } + + /// Maximum length of filenames + #[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl", target_env = "uclibc"))))] pub fn maximum_name_length(&self) -> libc::__fsword_t { self.0.f_namelen } diff --git a/src/sys/termios.rs b/src/sys/termios.rs index 01d4608039..e14b18dd13 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -776,7 +776,7 @@ libc_bitflags! { ALTWERASE; IEXTEN; #[cfg(not(target_os = "redox"))] - EXTPROC; + EXTPROC as tcflag_t; // uclibc gets the type wrong TOSTOP; #[cfg(not(target_os = "redox"))] FLUSHO; diff --git a/src/sys/uio.rs b/src/sys/uio.rs index 3abcde24fe..0bd07aa5ec 100644 --- a/src/sys/uio.rs +++ b/src/sys/uio.rs @@ -33,6 +33,8 @@ pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result { #[cfg(not(target_os = "redox"))] pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>], offset: off_t) -> Result { + #[cfg(target_env = "uclibc")] + let offset = offset as libc::off64_t; // uclibc doesn't use off_t let res = unsafe { libc::pwritev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset) }; @@ -50,6 +52,8 @@ pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>], #[cfg(not(target_os = "redox"))] pub fn preadv(fd: RawFd, iov: &[IoVec<&mut [u8]>], offset: off_t) -> Result { + #[cfg(target_env = "uclibc")] + let offset = offset as libc::off64_t; // uclibc doesn't use off_t let res = unsafe { libc::preadv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset) }; @@ -121,7 +125,7 @@ pub struct RemoteIoVec { /// [ptrace]: ../ptrace/index.html /// [`IoVec`]: struct.IoVec.html /// [`RemoteIoVec`]: struct.RemoteIoVec.html -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", not(target_env = "uclibc")))] pub fn process_vm_writev( pid: crate::unistd::Pid, local_iov: &[IoVec<&[u8]>], @@ -156,7 +160,7 @@ pub fn process_vm_writev( /// [`ptrace`]: ../ptrace/index.html /// [`IoVec`]: struct.IoVec.html /// [`RemoteIoVec`]: struct.RemoteIoVec.html -#[cfg(any(target_os = "linux"))] +#[cfg(all(target_os = "linux", not(target_env = "uclibc")))] pub fn process_vm_readv( pid: crate::unistd::Pid, local_iov: &[IoVec<&mut [u8]>],