Skip to content

Commit 661738c

Browse files
Merge #1366
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: наб <[email protected]>
2 parents de43d76 + 7cdae09 commit 661738c

File tree

10 files changed

+119
-82
lines changed

10 files changed

+119
-82
lines changed

.cirrus.yml

+4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ task:
157157
- name: Linux s390x
158158
env:
159159
TARGET: s390x-unknown-linux-gnu
160+
- name: Linux x32
161+
env:
162+
TARGET: x86_64-unknown-linux-gnux32
163+
CHECK_TESTS: true
160164
- name: NetBSD x86_64
161165
env:
162166
TARGET: x86_64-unknown-netbsd

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1414
- Implemented `IntoIterator` for `Dir`
1515
(#[1333](https://github.com/nix-rust/nix/pull/1333)).
1616
### Changed
17+
1718
### Fixed
1819
- Define `*_MAGIC` filesystem constants on Linux s390x
1920
(#[1372](https://github.com/nix-rust/nix/pull/1372))
21+
- mqueue, sysinfo, timespec, statfs, test_ptrace_syscall() on x32
22+
(#[1366](https://github.com/nix-rust/nix/pull/1366))
2023

2124
### Changed
2225

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ tempfile = "3.0.5"
4646
semver = "0.9.0"
4747

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

5151
[target.'cfg(target_os = "freebsd")'.dev-dependencies]
5252
sysctl = "0.1"

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Tier 2:
8383
Tier 3:
8484
* x86_64-fuchsia
8585
* x86_64-unknown-redox
86+
* x86_64-unknown-linux-gnux32
8687

8788
## Usage
8889

src/mqueue.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::Result;
66
use crate::errno::Errno;
77

8-
use libc::{self, c_char, c_long, mqd_t, size_t};
8+
use libc::{self, c_char, mqd_t, size_t};
99
use std::ffi::CString;
1010
use crate::sys::stat::Mode;
1111
use std::mem;
@@ -34,11 +34,18 @@ pub struct MqAttr {
3434
mq_attr: libc::mq_attr,
3535
}
3636

37+
// x32 compatibility
38+
// See https://sourceware.org/bugzilla/show_bug.cgi?id=21279
39+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
40+
pub type mq_attr_member_t = i64;
41+
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
42+
pub type mq_attr_member_t = libc::c_long;
43+
3744
impl MqAttr {
38-
pub fn new(mq_flags: c_long,
39-
mq_maxmsg: c_long,
40-
mq_msgsize: c_long,
41-
mq_curmsgs: c_long)
45+
pub fn new(mq_flags: mq_attr_member_t,
46+
mq_maxmsg: mq_attr_member_t,
47+
mq_msgsize: mq_attr_member_t,
48+
mq_curmsgs: mq_attr_member_t)
4249
-> MqAttr
4350
{
4451
let mut attr = mem::MaybeUninit::<libc::mq_attr>::uninit();
@@ -52,7 +59,7 @@ impl MqAttr {
5259
}
5360
}
5461

55-
pub fn flags(&self) -> c_long {
62+
pub fn flags(&self) -> mq_attr_member_t {
5663
self.mq_attr.mq_flags
5764
}
5865
}
@@ -150,7 +157,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
150157
/// Returns the old attributes
151158
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<MqAttr> {
152159
let oldattr = mq_getattr(mqd)?;
153-
let newattr = MqAttr::new(c_long::from(MQ_OFlag::O_NONBLOCK.bits()),
160+
let newattr = MqAttr::new(mq_attr_member_t::from(MQ_OFlag::O_NONBLOCK.bits()),
154161
oldattr.mq_attr.mq_maxmsg,
155162
oldattr.mq_attr.mq_msgsize,
156163
oldattr.mq_attr.mq_curmsgs);

src/sys/statfs.rs

+63-52
Original file line numberDiff line numberDiff line change
@@ -16,79 +16,85 @@ pub type fsid_t = libc::fsid_t;
1616
pub struct Statfs(libc::statfs);
1717

1818
#[cfg(target_os = "freebsd")]
19-
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
20-
pub struct FsType(pub u32);
19+
type fs_type_t = u32;
2120
#[cfg(target_os = "android")]
22-
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
23-
pub struct FsType(pub libc::c_ulong);
21+
type fs_type_t = libc::c_ulong;
2422
#[cfg(all(target_os = "linux", target_arch = "s390x"))]
25-
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
26-
pub struct FsType(pub u32);
23+
type fs_type_t = libc::c_uint;
2724
#[cfg(all(target_os = "linux", target_env = "musl"))]
28-
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
29-
pub struct FsType(pub libc::c_ulong);
25+
type fs_type_t = libc::c_ulong;
3026
#[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))]
27+
type fs_type_t = libc::__fsword_t;
28+
29+
#[cfg(any(
30+
target_os = "freebsd",
31+
target_os = "android",
32+
all(target_os = "linux", target_arch = "s390x"),
33+
all(target_os = "linux", target_env = "musl"),
34+
all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))),
35+
))]
3136
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
32-
pub struct FsType(pub libc::c_long);
37+
pub struct FsType(pub fs_type_t);
3338

3439
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
35-
pub const ADFS_SUPER_MAGIC: FsType = FsType(libc::ADFS_SUPER_MAGIC);
40+
pub const ADFS_SUPER_MAGIC: FsType = FsType(libc::ADFS_SUPER_MAGIC as fs_type_t);
3641
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
37-
pub const AFFS_SUPER_MAGIC: FsType = FsType(libc::AFFS_SUPER_MAGIC);
42+
pub const AFFS_SUPER_MAGIC: FsType = FsType(libc::AFFS_SUPER_MAGIC as fs_type_t);
3843
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
39-
pub const CODA_SUPER_MAGIC: FsType = FsType(libc::CODA_SUPER_MAGIC);
44+
pub const CODA_SUPER_MAGIC: FsType = FsType(libc::CODA_SUPER_MAGIC as fs_type_t);
4045
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
41-
pub const CRAMFS_MAGIC: FsType = FsType(libc::CRAMFS_MAGIC);
46+
pub const CRAMFS_MAGIC: FsType = FsType(libc::CRAMFS_MAGIC as fs_type_t);
4247
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
43-
pub const EFS_SUPER_MAGIC: FsType = FsType(libc::EFS_SUPER_MAGIC);
48+
pub const EFS_SUPER_MAGIC: FsType = FsType(libc::EFS_SUPER_MAGIC as fs_type_t);
4449
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
45-
pub const EXT2_SUPER_MAGIC: FsType = FsType(libc::EXT2_SUPER_MAGIC);
50+
pub const EXT2_SUPER_MAGIC: FsType = FsType(libc::EXT2_SUPER_MAGIC as fs_type_t);
4651
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
47-
pub const EXT3_SUPER_MAGIC: FsType = FsType(libc::EXT3_SUPER_MAGIC);
52+
pub const EXT3_SUPER_MAGIC: FsType = FsType(libc::EXT3_SUPER_MAGIC as fs_type_t);
4853
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
49-
pub const EXT4_SUPER_MAGIC: FsType = FsType(libc::EXT4_SUPER_MAGIC);
54+
pub const EXT4_SUPER_MAGIC: FsType = FsType(libc::EXT4_SUPER_MAGIC as fs_type_t);
5055
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
51-
pub const HPFS_SUPER_MAGIC: FsType = FsType(libc::HPFS_SUPER_MAGIC);
56+
pub const HPFS_SUPER_MAGIC: FsType = FsType(libc::HPFS_SUPER_MAGIC as fs_type_t);
5257
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
53-
pub const HUGETLBFS_MAGIC: FsType = FsType(libc::HUGETLBFS_MAGIC);
58+
pub const HUGETLBFS_MAGIC: FsType = FsType(libc::HUGETLBFS_MAGIC as fs_type_t);
5459
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
55-
pub const ISOFS_SUPER_MAGIC: FsType = FsType(libc::ISOFS_SUPER_MAGIC);
60+
pub const ISOFS_SUPER_MAGIC: FsType = FsType(libc::ISOFS_SUPER_MAGIC as fs_type_t);
5661
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
57-
pub const JFFS2_SUPER_MAGIC: FsType = FsType(libc::JFFS2_SUPER_MAGIC);
62+
pub const JFFS2_SUPER_MAGIC: FsType = FsType(libc::JFFS2_SUPER_MAGIC as fs_type_t);
5863
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
59-
pub const MINIX_SUPER_MAGIC: FsType = FsType(libc::MINIX_SUPER_MAGIC);
64+
pub const MINIX_SUPER_MAGIC: FsType = FsType(libc::MINIX_SUPER_MAGIC as fs_type_t);
6065
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
61-
pub const MINIX_SUPER_MAGIC2: FsType = FsType(libc::MINIX_SUPER_MAGIC2);
66+
pub const MINIX_SUPER_MAGIC2: FsType = FsType(libc::MINIX_SUPER_MAGIC2 as fs_type_t);
6267
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
63-
pub const MINIX2_SUPER_MAGIC: FsType = FsType(libc::MINIX2_SUPER_MAGIC);
68+
pub const MINIX2_SUPER_MAGIC: FsType = FsType(libc::MINIX2_SUPER_MAGIC as fs_type_t);
6469
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
65-
pub const MINIX2_SUPER_MAGIC2: FsType = FsType(libc::MINIX2_SUPER_MAGIC2);
70+
pub const MINIX2_SUPER_MAGIC2: FsType = FsType(libc::MINIX2_SUPER_MAGIC2 as fs_type_t);
6671
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
67-
pub const MSDOS_SUPER_MAGIC: FsType = FsType(libc::MSDOS_SUPER_MAGIC);
72+
pub const MSDOS_SUPER_MAGIC: FsType = FsType(libc::MSDOS_SUPER_MAGIC as fs_type_t);
6873
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
69-
pub const NCP_SUPER_MAGIC: FsType = FsType(libc::NCP_SUPER_MAGIC);
74+
pub const NCP_SUPER_MAGIC: FsType = FsType(libc::NCP_SUPER_MAGIC as fs_type_t);
7075
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
71-
pub const NFS_SUPER_MAGIC: FsType = FsType(libc::NFS_SUPER_MAGIC);
76+
pub const NFS_SUPER_MAGIC: FsType = FsType(libc::NFS_SUPER_MAGIC as fs_type_t);
7277
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
73-
pub const OPENPROM_SUPER_MAGIC: FsType = FsType(libc::OPENPROM_SUPER_MAGIC);
78+
pub const OPENPROM_SUPER_MAGIC: FsType = FsType(libc::OPENPROM_SUPER_MAGIC as fs_type_t);
7479
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
75-
pub const OVERLAYFS_SUPER_MAGIC: FsType = FsType(libc::OVERLAYFS_SUPER_MAGIC);
80+
pub const OVERLAYFS_SUPER_MAGIC: FsType = FsType(libc::OVERLAYFS_SUPER_MAGIC as fs_type_t);
7681
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
77-
pub const PROC_SUPER_MAGIC: FsType = FsType(libc::PROC_SUPER_MAGIC);
82+
pub const PROC_SUPER_MAGIC: FsType = FsType(libc::PROC_SUPER_MAGIC as fs_type_t);
7883
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
79-
pub const QNX4_SUPER_MAGIC: FsType = FsType(libc::QNX4_SUPER_MAGIC);
84+
pub const QNX4_SUPER_MAGIC: FsType = FsType(libc::QNX4_SUPER_MAGIC as fs_type_t);
8085
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
81-
pub const REISERFS_SUPER_MAGIC: FsType = FsType(libc::REISERFS_SUPER_MAGIC);
86+
pub const REISERFS_SUPER_MAGIC: FsType = FsType(libc::REISERFS_SUPER_MAGIC as fs_type_t);
8287
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
83-
pub const SMB_SUPER_MAGIC: FsType = FsType(libc::SMB_SUPER_MAGIC);
88+
pub const SMB_SUPER_MAGIC: FsType = FsType(libc::SMB_SUPER_MAGIC as fs_type_t);
8489
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
85-
pub const TMPFS_MAGIC: FsType = FsType(libc::TMPFS_MAGIC);
90+
pub const TMPFS_MAGIC: FsType = FsType(libc::TMPFS_MAGIC as fs_type_t);
8691
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
87-
pub const USBDEVICE_SUPER_MAGIC: FsType = FsType(libc::USBDEVICE_SUPER_MAGIC);
92+
pub const USBDEVICE_SUPER_MAGIC: FsType = FsType(libc::USBDEVICE_SUPER_MAGIC as fs_type_t);
8893
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
89-
pub const CGROUP_SUPER_MAGIC: FsType = FsType(libc::CGROUP_SUPER_MAGIC);
94+
pub const CGROUP_SUPER_MAGIC: FsType = FsType(libc::CGROUP_SUPER_MAGIC as fs_type_t);
9095
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
91-
pub const CGROUP2_SUPER_MAGIC: FsType = FsType(libc::CGROUP2_SUPER_MAGIC);
96+
pub const CGROUP2_SUPER_MAGIC: FsType = FsType(libc::CGROUP2_SUPER_MAGIC as fs_type_t);
97+
9298

9399
impl Statfs {
94100
/// Magic code defining system type
@@ -138,7 +144,7 @@ impl Statfs {
138144

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

@@ -177,7 +183,7 @@ impl Statfs {
177183
/// Size of a block
178184
// f_bsize on linux: https://github.com/torvalds/linux/blob/master/fs/nfs/super.c#L471
179185
#[cfg(all(target_os = "linux", not(any(target_arch = "s390x", target_env = "musl"))))]
180-
pub fn block_size(&self) -> libc::c_long {
186+
pub fn block_size(&self) -> libc::__fsword_t {
181187
self.0.f_bsize
182188
}
183189

@@ -219,7 +225,7 @@ impl Statfs {
219225

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

@@ -248,7 +254,7 @@ impl Statfs {
248254
}
249255

250256
/// Total data blocks in filesystem
251-
#[cfg(all(target_os = "linux", target_env = "musl"))]
257+
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
252258
pub fn blocks(&self) -> u64 {
253259
self.0.f_blocks
254260
}
@@ -261,7 +267,7 @@ impl Statfs {
261267
target_os = "freebsd",
262268
target_os = "openbsd",
263269
target_os = "dragonfly",
264-
all(target_os = "linux", target_env = "musl")
270+
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
265271
)))]
266272
pub fn blocks(&self) -> libc::c_ulong {
267273
self.0.f_blocks
@@ -286,7 +292,7 @@ impl Statfs {
286292
}
287293

288294
/// Free blocks in filesystem
289-
#[cfg(all(target_os = "linux", target_env = "musl"))]
295+
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
290296
pub fn blocks_free(&self) -> u64 {
291297
self.0.f_bfree
292298
}
@@ -299,7 +305,7 @@ impl Statfs {
299305
target_os = "freebsd",
300306
target_os = "openbsd",
301307
target_os = "dragonfly",
302-
all(target_os = "linux", target_env = "musl")
308+
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
303309
)))]
304310
pub fn blocks_free(&self) -> libc::c_ulong {
305311
self.0.f_bfree
@@ -324,7 +330,7 @@ impl Statfs {
324330
}
325331

326332
/// Free blocks available to unprivileged user
327-
#[cfg(all(target_os = "linux", target_env = "musl"))]
333+
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
328334
pub fn blocks_available(&self) -> u64 {
329335
self.0.f_bavail
330336
}
@@ -337,7 +343,7 @@ impl Statfs {
337343
target_os = "freebsd",
338344
target_os = "openbsd",
339345
target_os = "dragonfly",
340-
all(target_os = "linux", target_env = "musl")
346+
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
341347
)))]
342348
pub fn blocks_available(&self) -> libc::c_ulong {
343349
self.0.f_bavail
@@ -362,8 +368,8 @@ impl Statfs {
362368
}
363369

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

@@ -375,7 +381,7 @@ impl Statfs {
375381
target_os = "freebsd",
376382
target_os = "openbsd",
377383
target_os = "dragonfly",
378-
all(target_os = "linux", target_env = "musl")
384+
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
379385
)))]
380386
pub fn files(&self) -> libc::c_ulong {
381387
self.0.f_files
@@ -385,7 +391,6 @@ impl Statfs {
385391
#[cfg(any(
386392
target_os = "android",
387393
target_os = "ios",
388-
all(target_os = "linux", target_env = "musl"),
389394
target_os = "macos",
390395
target_os = "openbsd"
391396
))]
@@ -405,6 +410,12 @@ impl Statfs {
405410
self.0.f_ffree
406411
}
407412

413+
/// Free file nodes in filesystem
414+
#[cfg(all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32"))))]
415+
pub fn files_free(&self) -> libc::fsfilcnt_t {
416+
self.0.f_ffree
417+
}
418+
408419
/// Free file nodes in filesystem
409420
#[cfg(not(any(
410421
target_os = "ios",
@@ -413,7 +424,7 @@ impl Statfs {
413424
target_os = "freebsd",
414425
target_os = "openbsd",
415426
target_os = "dragonfly",
416-
all(target_os = "linux", target_env = "musl")
427+
all(target_os = "linux", any(target_env = "musl", all(target_arch = "x86_64", target_pointer_width = "32")))
417428
)))]
418429
pub fn files_free(&self) -> libc::c_ulong {
419430
self.0.f_ffree

src/sys/sysinfo.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ use crate::errno::Errno;
1010
#[repr(transparent)]
1111
pub struct SysInfo(libc::sysinfo);
1212

13+
// The fields are c_ulong on 32-bit linux, u64 on 64-bit linux; x32's ulong is u32
14+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
15+
type mem_blocks_t = u64;
16+
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
17+
type mem_blocks_t = libc::c_ulong;
18+
1319
impl SysInfo {
1420
/// Returns the load average tuple.
1521
///
@@ -58,7 +64,7 @@ impl SysInfo {
5864
self.scale_mem(self.0.freeram)
5965
}
6066

61-
fn scale_mem(&self, units: libc::c_ulong) -> u64 {
67+
fn scale_mem(&self, units: mem_blocks_t) -> u64 {
6268
units as u64 * self.0.mem_unit as u64
6369
}
6470
}

0 commit comments

Comments
 (0)