Skip to content

Commit

Permalink
Merge #1366
Browse files Browse the repository at this point in the history
1366: x32 port r=asomers a=nabijaczleweli

I played pretty loose with the statfs bit, hoping for CI to tell me if I broke something.

Co-authored-by: наб <nabijaczleweli@nabijaczleweli.xyz>
  • Loading branch information
bors[bot] and nabijaczleweli committed Feb 20, 2021
2 parents de43d76 + 7cdae09 commit 661738c
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .cirrus.yml
Expand Up @@ -157,6 +157,10 @@ task:
- name: Linux s390x
env:
TARGET: s390x-unknown-linux-gnu
- name: Linux x32
env:
TARGET: x86_64-unknown-linux-gnux32
CHECK_TESTS: true
- name: NetBSD x86_64
env:
TARGET: x86_64-unknown-netbsd
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,9 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Implemented `IntoIterator` for `Dir`
(#[1333](https://github.com/nix-rust/nix/pull/1333)).
### Changed

### Fixed
- Define `*_MAGIC` filesystem constants on Linux s390x
(#[1372](https://github.com/nix-rust/nix/pull/1372))
- mqueue, sysinfo, timespec, statfs, test_ptrace_syscall() on x32
(#[1366](https://github.com/nix-rust/nix/pull/1366))

### Changed

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -46,7 +46,7 @@ tempfile = "3.0.5"
semver = "0.9.0"

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dev-dependencies]
caps = "0.3.1"
caps = "0.5.1"

[target.'cfg(target_os = "freebsd")'.dev-dependencies]
sysctl = "0.1"
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -83,6 +83,7 @@ Tier 2:
Tier 3:
* x86_64-fuchsia
* x86_64-unknown-redox
* x86_64-unknown-linux-gnux32

## Usage

Expand Down
21 changes: 14 additions & 7 deletions src/mqueue.rs
Expand Up @@ -5,7 +5,7 @@
use crate::Result;
use crate::errno::Errno;

use libc::{self, c_char, c_long, mqd_t, size_t};
use libc::{self, c_char, mqd_t, size_t};
use std::ffi::CString;
use crate::sys::stat::Mode;
use std::mem;
Expand Down Expand Up @@ -34,11 +34,18 @@ pub struct MqAttr {
mq_attr: libc::mq_attr,
}

// x32 compatibility
// See https://sourceware.org/bugzilla/show_bug.cgi?id=21279
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
pub type mq_attr_member_t = i64;
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
pub type mq_attr_member_t = libc::c_long;

impl MqAttr {
pub fn new(mq_flags: c_long,
mq_maxmsg: c_long,
mq_msgsize: c_long,
mq_curmsgs: c_long)
pub fn new(mq_flags: mq_attr_member_t,
mq_maxmsg: mq_attr_member_t,
mq_msgsize: mq_attr_member_t,
mq_curmsgs: mq_attr_member_t)
-> MqAttr
{
let mut attr = mem::MaybeUninit::<libc::mq_attr>::uninit();
Expand All @@ -52,7 +59,7 @@ impl MqAttr {
}
}

pub fn flags(&self) -> c_long {
pub fn flags(&self) -> mq_attr_member_t {
self.mq_attr.mq_flags
}
}
Expand Down Expand Up @@ -150,7 +157,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
/// Returns the old attributes
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<MqAttr> {
let oldattr = mq_getattr(mqd)?;
let newattr = MqAttr::new(c_long::from(MQ_OFlag::O_NONBLOCK.bits()),
let newattr = MqAttr::new(mq_attr_member_t::from(MQ_OFlag::O_NONBLOCK.bits()),
oldattr.mq_attr.mq_maxmsg,
oldattr.mq_attr.mq_msgsize,
oldattr.mq_attr.mq_curmsgs);
Expand Down
115 changes: 63 additions & 52 deletions src/sys/statfs.rs
Expand Up @@ -16,79 +16,85 @@ pub type fsid_t = libc::fsid_t;
pub struct Statfs(libc::statfs);

#[cfg(target_os = "freebsd")]
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
pub struct FsType(pub u32);
type fs_type_t = u32;
#[cfg(target_os = "android")]
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
pub struct FsType(pub libc::c_ulong);
type fs_type_t = libc::c_ulong;
#[cfg(all(target_os = "linux", target_arch = "s390x"))]
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
pub struct FsType(pub u32);
type fs_type_t = libc::c_uint;
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
pub struct FsType(pub libc::c_ulong);
type fs_type_t = libc::c_ulong;
#[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))]
type fs_type_t = libc::__fsword_t;

#[cfg(any(
target_os = "freebsd",
target_os = "android",
all(target_os = "linux", target_arch = "s390x"),
all(target_os = "linux", target_env = "musl"),
all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))),
))]
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
pub struct FsType(pub libc::c_long);
pub struct FsType(pub fs_type_t);

#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const ADFS_SUPER_MAGIC: FsType = FsType(libc::ADFS_SUPER_MAGIC);
pub const ADFS_SUPER_MAGIC: FsType = FsType(libc::ADFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const AFFS_SUPER_MAGIC: FsType = FsType(libc::AFFS_SUPER_MAGIC);
pub const AFFS_SUPER_MAGIC: FsType = FsType(libc::AFFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const CODA_SUPER_MAGIC: FsType = FsType(libc::CODA_SUPER_MAGIC);
pub const CODA_SUPER_MAGIC: FsType = FsType(libc::CODA_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const CRAMFS_MAGIC: FsType = FsType(libc::CRAMFS_MAGIC);
pub const CRAMFS_MAGIC: FsType = FsType(libc::CRAMFS_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const EFS_SUPER_MAGIC: FsType = FsType(libc::EFS_SUPER_MAGIC);
pub const EFS_SUPER_MAGIC: FsType = FsType(libc::EFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const EXT2_SUPER_MAGIC: FsType = FsType(libc::EXT2_SUPER_MAGIC);
pub const EXT2_SUPER_MAGIC: FsType = FsType(libc::EXT2_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const EXT3_SUPER_MAGIC: FsType = FsType(libc::EXT3_SUPER_MAGIC);
pub const EXT3_SUPER_MAGIC: FsType = FsType(libc::EXT3_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const EXT4_SUPER_MAGIC: FsType = FsType(libc::EXT4_SUPER_MAGIC);
pub const EXT4_SUPER_MAGIC: FsType = FsType(libc::EXT4_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const HPFS_SUPER_MAGIC: FsType = FsType(libc::HPFS_SUPER_MAGIC);
pub const HPFS_SUPER_MAGIC: FsType = FsType(libc::HPFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const HUGETLBFS_MAGIC: FsType = FsType(libc::HUGETLBFS_MAGIC);
pub const HUGETLBFS_MAGIC: FsType = FsType(libc::HUGETLBFS_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const ISOFS_SUPER_MAGIC: FsType = FsType(libc::ISOFS_SUPER_MAGIC);
pub const ISOFS_SUPER_MAGIC: FsType = FsType(libc::ISOFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const JFFS2_SUPER_MAGIC: FsType = FsType(libc::JFFS2_SUPER_MAGIC);
pub const JFFS2_SUPER_MAGIC: FsType = FsType(libc::JFFS2_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const MINIX_SUPER_MAGIC: FsType = FsType(libc::MINIX_SUPER_MAGIC);
pub const MINIX_SUPER_MAGIC: FsType = FsType(libc::MINIX_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const MINIX_SUPER_MAGIC2: FsType = FsType(libc::MINIX_SUPER_MAGIC2);
pub const MINIX_SUPER_MAGIC2: FsType = FsType(libc::MINIX_SUPER_MAGIC2 as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const MINIX2_SUPER_MAGIC: FsType = FsType(libc::MINIX2_SUPER_MAGIC);
pub const MINIX2_SUPER_MAGIC: FsType = FsType(libc::MINIX2_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const MINIX2_SUPER_MAGIC2: FsType = FsType(libc::MINIX2_SUPER_MAGIC2);
pub const MINIX2_SUPER_MAGIC2: FsType = FsType(libc::MINIX2_SUPER_MAGIC2 as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const MSDOS_SUPER_MAGIC: FsType = FsType(libc::MSDOS_SUPER_MAGIC);
pub const MSDOS_SUPER_MAGIC: FsType = FsType(libc::MSDOS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const NCP_SUPER_MAGIC: FsType = FsType(libc::NCP_SUPER_MAGIC);
pub const NCP_SUPER_MAGIC: FsType = FsType(libc::NCP_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const NFS_SUPER_MAGIC: FsType = FsType(libc::NFS_SUPER_MAGIC);
pub const NFS_SUPER_MAGIC: FsType = FsType(libc::NFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const OPENPROM_SUPER_MAGIC: FsType = FsType(libc::OPENPROM_SUPER_MAGIC);
pub const OPENPROM_SUPER_MAGIC: FsType = FsType(libc::OPENPROM_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const OVERLAYFS_SUPER_MAGIC: FsType = FsType(libc::OVERLAYFS_SUPER_MAGIC);
pub const OVERLAYFS_SUPER_MAGIC: FsType = FsType(libc::OVERLAYFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const PROC_SUPER_MAGIC: FsType = FsType(libc::PROC_SUPER_MAGIC);
pub const PROC_SUPER_MAGIC: FsType = FsType(libc::PROC_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const QNX4_SUPER_MAGIC: FsType = FsType(libc::QNX4_SUPER_MAGIC);
pub const QNX4_SUPER_MAGIC: FsType = FsType(libc::QNX4_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const REISERFS_SUPER_MAGIC: FsType = FsType(libc::REISERFS_SUPER_MAGIC);
pub const REISERFS_SUPER_MAGIC: FsType = FsType(libc::REISERFS_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const SMB_SUPER_MAGIC: FsType = FsType(libc::SMB_SUPER_MAGIC);
pub const SMB_SUPER_MAGIC: FsType = FsType(libc::SMB_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const TMPFS_MAGIC: FsType = FsType(libc::TMPFS_MAGIC);
pub const TMPFS_MAGIC: FsType = FsType(libc::TMPFS_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const USBDEVICE_SUPER_MAGIC: FsType = FsType(libc::USBDEVICE_SUPER_MAGIC);
pub const USBDEVICE_SUPER_MAGIC: FsType = FsType(libc::USBDEVICE_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const CGROUP_SUPER_MAGIC: FsType = FsType(libc::CGROUP_SUPER_MAGIC);
pub const CGROUP_SUPER_MAGIC: FsType = FsType(libc::CGROUP_SUPER_MAGIC as fs_type_t);
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
pub const CGROUP2_SUPER_MAGIC: FsType = FsType(libc::CGROUP2_SUPER_MAGIC);
pub const CGROUP2_SUPER_MAGIC: FsType = FsType(libc::CGROUP2_SUPER_MAGIC as fs_type_t);


impl Statfs {
/// Magic code defining system type
Expand Down Expand Up @@ -138,7 +144,7 @@ impl Statfs {

/// Optimal transfer block size
#[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))]
pub fn optimal_transfer_size(&self) -> libc::c_long {
pub fn optimal_transfer_size(&self) -> libc::__fsword_t {
self.0.f_bsize
}

Expand Down Expand Up @@ -177,7 +183,7 @@ 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"))))]
pub fn block_size(&self) -> libc::c_long {
pub fn block_size(&self) -> libc::__fsword_t {
self.0.f_bsize
}

Expand Down Expand Up @@ -219,7 +225,7 @@ impl Statfs {

/// Maximum length of filenames
#[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))]
pub fn maximum_name_length(&self) -> libc::c_long {
pub fn maximum_name_length(&self) -> libc::__fsword_t {
self.0.f_namelen
}

Expand Down Expand Up @@ -248,7 +254,7 @@ impl Statfs {
}

/// Total data blocks in filesystem
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
pub fn blocks(&self) -> u64 {
self.0.f_blocks
}
Expand All @@ -261,7 +267,7 @@ impl Statfs {
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", target_env = "musl")
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
pub fn blocks(&self) -> libc::c_ulong {
self.0.f_blocks
Expand All @@ -286,7 +292,7 @@ impl Statfs {
}

/// Free blocks in filesystem
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
pub fn blocks_free(&self) -> u64 {
self.0.f_bfree
}
Expand All @@ -299,7 +305,7 @@ impl Statfs {
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", target_env = "musl")
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
pub fn blocks_free(&self) -> libc::c_ulong {
self.0.f_bfree
Expand All @@ -324,7 +330,7 @@ impl Statfs {
}

/// Free blocks available to unprivileged user
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
pub fn blocks_available(&self) -> u64 {
self.0.f_bavail
}
Expand All @@ -337,7 +343,7 @@ impl Statfs {
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", target_env = "musl")
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
pub fn blocks_available(&self) -> libc::c_ulong {
self.0.f_bavail
Expand All @@ -362,8 +368,8 @@ impl Statfs {
}

/// Total file nodes in filesystem
#[cfg(all(target_os = "linux", target_env = "musl"))]
pub fn files(&self) -> u64 {
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
pub fn files(&self) -> libc::fsfilcnt_t {
self.0.f_files
}

Expand All @@ -375,7 +381,7 @@ impl Statfs {
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", target_env = "musl")
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
pub fn files(&self) -> libc::c_ulong {
self.0.f_files
Expand All @@ -385,7 +391,6 @@ impl Statfs {
#[cfg(any(
target_os = "android",
target_os = "ios",
all(target_os = "linux", target_env = "musl"),
target_os = "macos",
target_os = "openbsd"
))]
Expand All @@ -405,6 +410,12 @@ impl Statfs {
self.0.f_ffree
}

/// Free file nodes in filesystem
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
pub fn files_free(&self) -> libc::fsfilcnt_t {
self.0.f_ffree
}

/// Free file nodes in filesystem
#[cfg(not(any(
target_os = "ios",
Expand All @@ -413,7 +424,7 @@ impl Statfs {
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", target_env = "musl")
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
pub fn files_free(&self) -> libc::c_ulong {
self.0.f_ffree
Expand Down
8 changes: 7 additions & 1 deletion src/sys/sysinfo.rs
Expand Up @@ -10,6 +10,12 @@ use crate::errno::Errno;
#[repr(transparent)]
pub struct SysInfo(libc::sysinfo);

// The fields are c_ulong on 32-bit linux, u64 on 64-bit linux; x32's ulong is u32
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
type mem_blocks_t = u64;
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
type mem_blocks_t = libc::c_ulong;

impl SysInfo {
/// Returns the load average tuple.
///
Expand Down Expand Up @@ -58,7 +64,7 @@ impl SysInfo {
self.scale_mem(self.0.freeram)
}

fn scale_mem(&self, units: libc::c_ulong) -> u64 {
fn scale_mem(&self, units: mem_blocks_t) -> u64 {
units as u64 * self.0.mem_unit as u64
}
}
Expand Down

0 comments on commit 661738c

Please sign in to comment.