#![cfg_attr(not(feature = "guard"), allow(dead_code))]
use core::ffi::{c_char, c_int, c_long, c_uint, c_ulong, c_void};
pub(crate) type Uid = u32;
pub(crate) type Mode = u32;
pub(crate) type Time = c_long;
pub(crate) type ClockId = c_int;
#[repr(C)]
pub(crate) struct Statvfs {
pub(crate) bsize: c_ulong,
pub(crate) frsize: c_ulong,
pub(crate) blocks: u64,
pub(crate) bfree: u64,
pub(crate) bavail: u64,
pub(crate) files: u64,
pub(crate) ffree: u64,
pub(crate) favail: u64,
pub(crate) fsid: c_ulong,
#[cfg(target_pointer_width = "32")]
pub(crate) unused: c_int,
pub(crate) flag: c_ulong,
pub(crate) namemax: c_ulong,
pub(crate) spare: [c_int; 6],
}
#[cfg(target_pointer_width = "64")]
layout!(Statvfs, size = 112, align = 8);
#[cfg(all(target_pointer_width = "32", target_arch = "x86"))]
layout!(Statvfs, size = 96, align = 4);
#[cfg(all(target_pointer_width = "32", target_arch = "arm"))]
layout!(Statvfs, size = 96, align = 8);
pub(crate) const ST_RDONLY: c_ulong = 1;
#[repr(C)]
pub(crate) struct Statfs {
pub(crate) ftype: c_long,
pub(crate) bsize: c_long,
pub(crate) blocks: u64,
pub(crate) bfree: u64,
pub(crate) bavail: u64,
pub(crate) files: u64,
pub(crate) ffree: u64,
pub(crate) fsid: [c_int; 2],
pub(crate) namelen: c_long,
pub(crate) frsize: c_long,
pub(crate) flags: c_long,
pub(crate) spare: [c_long; 4],
}
#[cfg(target_pointer_width = "64")]
layout!(Statfs, size = 120, align = 8);
#[cfg(all(target_pointer_width = "32", target_arch = "x86"))]
layout!(Statfs, size = 84, align = 4);
#[cfg(all(target_pointer_width = "32", target_arch = "arm"))]
layout!(Statfs, size = 88, align = 8);
pub(crate) const NFS_SUPER_MAGIC: u32 = 0x6969;
pub(crate) const SMB_SUPER_MAGIC: u32 = 0x517B;
pub(crate) const CIFS_SUPER_MAGIC: u32 = 0xFF53_4D42;
pub(crate) const SMB2_SUPER_MAGIC: u32 = 0xFE53_4D42;
pub(crate) const FUSE_SUPER_MAGIC: u32 = 0x6573_5546;
pub(crate) const V9FS_MAGIC: u32 = 0x0102_1997;
pub(crate) const CODA_SUPER_MAGIC: u32 = 0x7375_7245;
pub(crate) const AFS_SUPER_MAGIC: u32 = 0x5346_414F;
pub(crate) const CEPH_SUPER_MAGIC: u32 = 0x00C3_6400;
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct Rlimit {
pub(crate) cur: u64,
pub(crate) max: u64,
}
#[cfg(not(all(target_pointer_width = "32", target_arch = "x86")))]
layout!(Rlimit, size = 16, align = 8);
#[cfg(all(target_pointer_width = "32", target_arch = "x86"))]
layout!(Rlimit, size = 16, align = 4);
pub(crate) const RLIM_INFINITY: u64 = u64::MAX;
pub(crate) const RLIMIT_STACK: c_int = 3;
pub(crate) const RLIMIT_CORE: c_int = 4;
pub(crate) const RLIMIT_NPROC: c_int = 6;
pub(crate) const RLIMIT_NOFILE: c_int = 7;
pub(crate) const RLIMIT_MEMLOCK: c_int = 8;
pub(crate) const RLIMIT_AS: c_int = 9;
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct Timeval {
pub(crate) sec: Time,
pub(crate) usec: c_long,
}
#[cfg(target_pointer_width = "64")]
layout!(Timeval, size = 16, align = 8);
#[cfg(target_pointer_width = "32")]
layout!(Timeval, size = 8, align = 4);
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct Timespec {
pub(crate) sec: Time,
pub(crate) nsec: c_long,
}
#[cfg(target_pointer_width = "64")]
layout!(Timespec, size = 16, align = 8);
#[cfg(target_pointer_width = "32")]
layout!(Timespec, size = 8, align = 4);
#[repr(C)]
pub(crate) struct Rusage {
pub(crate) utime: Timeval,
pub(crate) stime: Timeval,
pub(crate) maxrss: c_long,
pub(crate) ixrss: c_long,
pub(crate) idrss: c_long,
pub(crate) isrss: c_long,
pub(crate) minflt: c_long,
pub(crate) majflt: c_long,
pub(crate) nswap: c_long,
pub(crate) inblock: c_long,
pub(crate) oublock: c_long,
pub(crate) msgsnd: c_long,
pub(crate) msgrcv: c_long,
pub(crate) nsignals: c_long,
pub(crate) nvcsw: c_long,
pub(crate) nivcsw: c_long,
#[cfg(target_env = "musl")]
pub(crate) reserved: [c_long; 16],
}
#[cfg(all(target_pointer_width = "64", not(target_env = "musl")))]
layout!(Rusage, size = 144, align = 8);
#[cfg(target_pointer_width = "32")]
layout!(Rusage, size = 72, align = 4);
#[cfg(target_env = "musl")]
layout!(Rusage, size = 272, align = 8);
pub(crate) const RUSAGE_SELF: c_int = 0;
#[repr(C)]
pub(crate) struct Passwd {
pub(crate) name: *mut c_char,
pub(crate) passwd: *mut c_char,
pub(crate) uid: Uid,
pub(crate) gid: u32,
pub(crate) gecos: *mut c_char,
pub(crate) dir: *mut c_char,
pub(crate) shell: *mut c_char,
}
#[cfg(target_pointer_width = "64")]
layout!(Passwd, size = 48, align = 8);
#[cfg(target_pointer_width = "32")]
layout!(Passwd, size = 28, align = 4);
#[repr(C)]
pub(crate) struct Tm {
pub(crate) sec: c_int,
pub(crate) min: c_int,
pub(crate) hour: c_int,
pub(crate) mday: c_int,
pub(crate) mon: c_int,
pub(crate) year: c_int,
pub(crate) wday: c_int,
pub(crate) yday: c_int,
pub(crate) isdst: c_int,
pub(crate) gmtoff: c_long,
pub(crate) zone: *const c_char,
}
#[cfg(target_pointer_width = "64")]
layout!(Tm, size = 56, align = 8);
#[cfg(target_pointer_width = "32")]
layout!(Tm, size = 44, align = 4);
#[repr(C)]
pub(crate) struct Utsname {
pub(crate) sysname: [c_char; 65],
pub(crate) nodename: [c_char; 65],
pub(crate) release: [c_char; 65],
pub(crate) version: [c_char; 65],
pub(crate) machine: [c_char; 65],
pub(crate) domainname: [c_char; 65],
}
layout!(Utsname, size = 390, align = 1);
#[repr(C)]
pub(crate) struct Sockaddr {
pub(crate) family: u16,
pub(crate) data: [c_char; 14],
}
layout!(Sockaddr, size = 16, align = 2);
#[cfg(not(target_os = "android"))]
#[repr(C)]
pub(crate) struct Timex {
pub(crate) modes: c_uint,
pub(crate) offset: c_long,
pub(crate) freq: c_long,
pub(crate) maxerror: c_long,
pub(crate) esterror: c_long,
pub(crate) status: c_int,
#[cfg(target_pointer_width = "64")]
pub(crate) rest: [c_long; 20],
#[cfg(target_pointer_width = "32")]
pub(crate) rest: [c_long; 26],
}
#[cfg(all(not(target_os = "android"), target_pointer_width = "64"))]
layout!(Timex, size = 208, align = 8);
#[cfg(target_pointer_width = "32")]
layout!(Timex, size = 128, align = 4);
#[cfg(not(target_os = "android"))]
pub(crate) const TIME_ERROR: c_int = 5;
#[cfg(not(target_os = "android"))]
pub(crate) const STA_UNSYNC: c_int = 0x0040;
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct SockaddrIn {
pub(crate) family: u16,
pub(crate) port: u16,
pub(crate) addr: u32,
pub(crate) zero: [u8; 8],
}
layout!(SockaddrIn, size = 16, align = 4);
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct SockaddrIn6 {
pub(crate) family: u16,
pub(crate) port: u16,
pub(crate) flowinfo: u32,
pub(crate) addr: [u8; 16],
pub(crate) scope_id: u32,
}
layout!(SockaddrIn6, size = 28, align = 4);
#[repr(C)]
pub(crate) struct Ifaddrs {
pub(crate) next: *mut Ifaddrs,
pub(crate) name: *mut c_char,
pub(crate) flags: c_uint,
pub(crate) addr: *mut Sockaddr,
pub(crate) netmask: *mut Sockaddr,
pub(crate) ifu: *mut Sockaddr,
pub(crate) data: *mut c_void,
}
#[cfg(target_pointer_width = "64")]
layout!(Ifaddrs, size = 56, align = 8);
#[cfg(target_pointer_width = "32")]
layout!(Ifaddrs, size = 28, align = 4);
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct SockaddrLl {
pub(crate) family: u16,
pub(crate) protocol: u16,
pub(crate) ifindex: c_int,
pub(crate) hatype: u16,
pub(crate) pkttype: u8,
pub(crate) halen: u8,
pub(crate) addr: [u8; 8],
}
layout!(SockaddrLl, size = 20, align = 4);
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub(crate) struct Dirent {
pub(crate) ino: u64,
pub(crate) off: i64,
pub(crate) reclen: u16,
pub(crate) dtype: u8,
pub(crate) name: [c_char; 256],
}
#[cfg(not(all(target_pointer_width = "32", target_arch = "x86")))]
layout!(Dirent, size = 280, align = 8);
#[cfg(all(target_pointer_width = "32", target_arch = "x86"))]
layout!(Dirent, size = 276, align = 4);
#[repr(C)]
pub(crate) struct Dir {
_private: [u8; 0],
}
pub(crate) const AF_INET: u16 = 2;
pub(crate) const AF_INET6: u16 = 10;
pub(crate) const AF_PACKET: u16 = 17;
pub(crate) const IFF_UP: c_uint = 0x1;
pub(crate) const IFF_LOOPBACK: c_uint = 0x8;
pub(crate) const IFF_RUNNING: c_uint = 0x40;
#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc64"
))]
pub(crate) const O_NOFOLLOW: c_int = 0o100_000;
#[cfg(not(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc64"
)))]
pub(crate) const O_NOFOLLOW: c_int = 0o400_000;
pub(crate) const ERANGE: c_int = 34;
pub(crate) const CLOCK_BOOTTIME: ClockId = 7;
#[cfg(not(target_os = "android"))]
pub(crate) const SC_CLK_TCK: c_int = 2;
#[cfg(not(target_os = "android"))]
pub(crate) const SC_PAGESIZE: c_int = 30;
#[cfg(not(target_os = "android"))]
pub(crate) const SC_NPROCESSORS_CONF: c_int = 83;
#[cfg(target_os = "android")]
pub(crate) const SC_CLK_TCK: c_int = 0x0006;
#[cfg(target_os = "android")]
pub(crate) const SC_PAGESIZE: c_int = 0x0027;
#[cfg(target_os = "android")]
pub(crate) const SC_NPROCESSORS_CONF: c_int = 0x0060;
#[cfg(target_os = "android")]
pub(crate) const PROP_VALUE_MAX: usize = 92;
unsafe extern "C" {
pub(crate) safe fn sysconf(name: c_int) -> c_long;
pub(crate) safe fn geteuid() -> Uid;
pub(crate) safe fn umask(mask: Mode) -> Mode;
#[cfg_attr(target_pointer_width = "32", link_name = "statvfs64")]
pub(crate) fn statvfs(path: *const c_char, buf: *mut Statvfs) -> c_int;
#[cfg_attr(target_pointer_width = "32", link_name = "statfs64")]
pub(crate) fn statfs(path: *const c_char, buf: *mut Statfs) -> c_int;
#[cfg_attr(target_pointer_width = "32", link_name = "getrlimit64")]
pub(crate) fn getrlimit(resource: c_int, rlim: *mut Rlimit) -> c_int;
#[cfg_attr(target_pointer_width = "32", link_name = "setrlimit64")]
pub(crate) fn setrlimit(resource: c_int, rlim: *const Rlimit) -> c_int;
pub(crate) fn getrusage(who: c_int, usage: *mut Rusage) -> c_int;
pub(crate) fn getpwuid_r(
uid: Uid,
pwd: *mut Passwd,
buf: *mut c_char,
buflen: usize,
result: *mut *mut Passwd,
) -> c_int;
pub(crate) fn uname(buf: *mut Utsname) -> c_int;
pub(crate) fn localtime_r(time: *const Time, result: *mut Tm) -> *mut Tm;
pub(crate) fn clock_gettime(clock: ClockId, tp: *mut Timespec) -> c_int;
#[cfg(not(target_os = "android"))]
pub(crate) fn adjtimex(buf: *mut Timex) -> c_int;
pub(crate) fn getifaddrs(ifap: *mut *mut Ifaddrs) -> c_int;
pub(crate) fn freeifaddrs(ifa: *mut Ifaddrs);
pub(crate) fn opendir(name: *const c_char) -> *mut Dir;
#[cfg_attr(target_pointer_width = "32", link_name = "readdir64")]
pub(crate) fn readdir(dir: *mut Dir) -> *mut Dirent;
pub(crate) fn closedir(dir: *mut Dir) -> c_int;
}
#[cfg(target_os = "android")]
unsafe extern "C" {
pub(crate) fn __system_property_get(name: *const c_char, value: *mut c_char) -> c_int;
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(target_os = "android"))]
const _: () = {
assert!(size_of::<Timex>() == size_of::<libc::timex>());
assert!(core::mem::offset_of!(Timex, modes) == core::mem::offset_of!(libc::timex, modes));
assert!(
core::mem::offset_of!(Timex, maxerror) == core::mem::offset_of!(libc::timex, maxerror)
);
assert!(
core::mem::offset_of!(Timex, esterror) == core::mem::offset_of!(libc::timex, esterror)
);
assert!(core::mem::offset_of!(Timex, status) == core::mem::offset_of!(libc::timex, status));
assert!(TIME_ERROR == libc::TIME_ERROR);
assert!(STA_UNSYNC == libc::STA_UNSYNC);
};
#[cfg(target_pointer_width = "64")]
use libc::{dirent as dirent_t, rlimit as rlimit_t, statfs as statfs_t, statvfs as statvfs_t};
#[cfg(target_pointer_width = "32")]
use libc::{
dirent64 as dirent_t, rlimit64 as rlimit_t, statfs64 as statfs_t, statvfs64 as statvfs_t,
};
same_layout!(Statvfs, statvfs_t,
bsize => f_bsize, frsize => f_frsize, blocks => f_blocks, bfree => f_bfree,
bavail => f_bavail, files => f_files, ffree => f_ffree, favail => f_favail,
fsid => f_fsid, flag => f_flag, namemax => f_namemax);
same_layout!(Statfs, statfs_t,
ftype => f_type, bsize => f_bsize, blocks => f_blocks, bfree => f_bfree,
bavail => f_bavail, files => f_files, ffree => f_ffree, fsid => f_fsid,
namelen => f_namelen, frsize => f_frsize);
same_layout!(Rlimit, rlimit_t, cur => rlim_cur, max => rlim_max);
same_layout!(Timeval, libc::timeval, sec => tv_sec, usec => tv_usec);
same_layout!(Timespec, libc::timespec, sec => tv_sec, nsec => tv_nsec);
same_layout!(Rusage, libc::rusage,
utime => ru_utime, stime => ru_stime, maxrss => ru_maxrss, minflt => ru_minflt,
majflt => ru_majflt, inblock => ru_inblock, oublock => ru_oublock,
nvcsw => ru_nvcsw, nivcsw => ru_nivcsw);
same_layout!(Passwd, libc::passwd,
name => pw_name, passwd => pw_passwd, uid => pw_uid, gid => pw_gid,
gecos => pw_gecos, dir => pw_dir, shell => pw_shell);
same_layout!(Tm, libc::tm,
sec => tm_sec, min => tm_min, hour => tm_hour, mday => tm_mday, mon => tm_mon,
year => tm_year, wday => tm_wday, yday => tm_yday, isdst => tm_isdst,
gmtoff => tm_gmtoff, zone => tm_zone);
same_layout!(Utsname, libc::utsname,
sysname => sysname, nodename => nodename, release => release,
version => version, machine => machine, domainname => domainname);
same_layout!(Sockaddr, libc::sockaddr, family => sa_family, data => sa_data);
same_layout!(SockaddrIn, libc::sockaddr_in,
family => sin_family, port => sin_port, addr => sin_addr, zero => sin_zero);
same_layout!(SockaddrIn6, libc::sockaddr_in6,
family => sin6_family, port => sin6_port, flowinfo => sin6_flowinfo,
addr => sin6_addr, scope_id => sin6_scope_id);
same_layout!(SockaddrLl, libc::sockaddr_ll,
family => sll_family, protocol => sll_protocol, ifindex => sll_ifindex,
hatype => sll_hatype, pkttype => sll_pkttype, halen => sll_halen, addr => sll_addr);
same_layout!(Dirent, dirent_t,
ino => d_ino, off => d_off, reclen => d_reclen, dtype => d_type, name => d_name);
same_layout!(Ifaddrs, libc::ifaddrs,
next => ifa_next, name => ifa_name, flags => ifa_flags, addr => ifa_addr,
netmask => ifa_netmask, ifu => ifa_ifu, data => ifa_data);
#[allow(
clippy::unnecessary_cast,
reason = "libc's constant types differ between glibc, musl, and bionic"
)]
const _: () = {
assert!(ST_RDONLY == libc::ST_RDONLY as c_ulong);
#[cfg(target_pointer_width = "64")]
assert!(RLIM_INFINITY == libc::RLIM_INFINITY as u64);
#[cfg(target_pointer_width = "32")]
assert!(RLIM_INFINITY == libc::RLIM64_INFINITY);
assert!(RLIMIT_STACK == libc::RLIMIT_STACK as c_int);
assert!(RLIMIT_CORE == libc::RLIMIT_CORE as c_int);
assert!(RLIMIT_NPROC == libc::RLIMIT_NPROC as c_int);
assert!(RLIMIT_MEMLOCK == libc::RLIMIT_MEMLOCK as c_int);
assert!(RLIMIT_NOFILE == libc::RLIMIT_NOFILE as c_int);
assert!(RLIMIT_AS == libc::RLIMIT_AS as c_int);
assert!(RUSAGE_SELF == libc::RUSAGE_SELF);
assert!(AF_INET as c_int == libc::AF_INET);
assert!(AF_INET6 as c_int == libc::AF_INET6);
assert!(AF_PACKET as c_int == libc::AF_PACKET);
assert!(IFF_UP as c_int == libc::IFF_UP);
assert!(IFF_LOOPBACK as c_int == libc::IFF_LOOPBACK);
assert!(IFF_RUNNING as c_int == libc::IFF_RUNNING);
assert!(O_NOFOLLOW == libc::O_NOFOLLOW);
assert!(ERANGE == libc::ERANGE);
assert!(CLOCK_BOOTTIME == libc::CLOCK_BOOTTIME);
assert!(SC_CLK_TCK == libc::_SC_CLK_TCK);
assert!(SC_PAGESIZE == libc::_SC_PAGESIZE);
assert!(SC_NPROCESSORS_CONF == libc::_SC_NPROCESSORS_CONF);
assert!(NFS_SUPER_MAGIC as i64 == libc::NFS_SUPER_MAGIC as i64);
assert!(SMB_SUPER_MAGIC as i64 == libc::SMB_SUPER_MAGIC as i64);
assert!(CODA_SUPER_MAGIC as i64 == libc::CODA_SUPER_MAGIC as i64);
assert!(AFS_SUPER_MAGIC as i64 == libc::AFS_SUPER_MAGIC as i64);
};
#[cfg(target_os = "android")]
const _: () = assert!(PROP_VALUE_MAX == libc::PROP_VALUE_MAX as usize);
#[test]
fn test_ffi_linux_layouts_match_libc() {
assert!(sysconf(SC_PAGESIZE) >= 4096);
assert!(sysconf(SC_CLK_TCK) > 0);
}
}