Skip to content

Commit fa60c72

Browse files
committed
freebsd: Add support for FreeBSD 12.x ABI
In FreeBSD 12.x, several ABI were changed in an incompatible way: * Inodes were changed from 32-bit to 64-bit. This affects `struct stat` and `struct dirent` structures in Rust libc. https://svnweb.freebsd.org/base?view=revision&revision=318736 * Time-related members of `struct kevent` were changed to 64-bit. Again, this affects the definition of the same structure in Rust libc. https://svnweb.freebsd.org/base?view=revision&revision=320043 Currently, a Rust program compiled on FreeBSD 11.x will work on FreeBSD 12.x thanks to symbol versioning. Unfortunately, a program compiled on FreeBSD 12.x will break. Until Rust gains support for target versions, we need to detect the correct structures to use, based on the build host. This won't make it possible to build a FreeBSD 11 executable on a FreeBSD 12 host, but it is good enough when the build and target hosts are the same. This patch introduces a build script which uses freebsd-version(1) to detect the version. When it is 12 or more, it defines the `freebsd12_abi` feature. Modules in `src/unix/bsd/freebsdlike/freebsd` were reorganized. They are now split based on this ABI version instead of the target architecture. For instance, `struct stat` is defined once in `freebsd12.rs` for all architectures. `struct kevent` grew a new member, `ext` in FreeBSD 12.x. To avoid to much pain to users of this structure, both flavors (`freebsd11.rs` and `freebsd12.rs`) now have the `ext` member. In the case of `freebsd11`, this is a 0-length array. To help initialize the structure, one can use the `libc::KEVENT_EXT_ZEROED` constant.
1 parent 916b82d commit fa60c72

File tree

9 files changed

+186
-99
lines changed

9 files changed

+186
-99
lines changed

build.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::process::Command;
2+
3+
#[cfg(not(target_os = "freebsd"))]
4+
fn detect_freebsd_abi_changes() {}
5+
6+
#[cfg(target_os = "freebsd")]
7+
fn detect_freebsd_abi_changes() {
8+
// We use freebsd-version(1). We could use `uname -k` to get the
9+
// version of the kernel, but it wouldn't reflect the version of the
10+
// userland (in case we are in a jail).
11+
//
12+
// freebsd-version(1) appeared in FreeBSD 10.0, so it's good enough.
13+
14+
let _ = Command::new("freebsd-version").arg("-u")
15+
.output()
16+
.and_then(
17+
|output| {
18+
assert!(output.status.success());
19+
20+
let full_version_string = String::from_utf8_lossy(&output.stdout);
21+
let split_version: Vec<&str> = full_version_string
22+
.trim()
23+
.splitn(2, '.').collect();
24+
25+
assert!(split_version.len() >= 1);
26+
27+
let version: u32 = split_version[0]
28+
.parse().unwrap();
29+
30+
// FreeBSD kernel versions are published in the following document:
31+
// https://www.freebsd.org/doc/en/books/porters-handbook/versions.html
32+
33+
if version >= 12 { println!("cargo:rustc-cfg=freebsd12_abi"); }
34+
35+
Ok(())
36+
});
37+
}
38+
39+
fn main() {
40+
detect_freebsd_abi_changes();
41+
}

src/unix/bsd/freebsdlike/dragonfly/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub type clock_t = u64;
2+
pub type dev_t = u32;
23
pub type ino_t = u64;
34
pub type nlink_t = u32;
45
pub type blksize_t = i64;
@@ -168,6 +169,15 @@ s! {
168169
pub ifm_index: ::c_ushort,
169170
pub ifm_data: if_data,
170171
}
172+
173+
pub struct kevent {
174+
pub ident: ::uintptr_t,
175+
pub filter: ::c_short,
176+
pub flags: ::c_ushort,
177+
pub fflags: ::c_uint,
178+
pub data: ::intptr_t,
179+
pub udata: *mut ::c_void,
180+
}
171181
}
172182

173183
pub const RAND_MAX: ::c_int = 0x7fff_ffff;

src/unix/bsd/freebsdlike/freebsd/aarch64.rs

-26
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,3 @@ pub type c_long = i64;
22
pub type c_ulong = u64;
33
pub type time_t = i64;
44
pub type suseconds_t = i64;
5-
6-
s! {
7-
pub struct stat {
8-
pub st_dev: ::dev_t,
9-
pub st_ino: ::ino_t,
10-
pub st_mode: ::mode_t,
11-
pub st_nlink: ::nlink_t,
12-
pub st_uid: ::uid_t,
13-
pub st_gid: ::gid_t,
14-
pub st_rdev: ::dev_t,
15-
pub st_atime: ::time_t,
16-
pub st_atime_nsec: ::c_long,
17-
pub st_mtime: ::time_t,
18-
pub st_mtime_nsec: ::c_long,
19-
pub st_ctime: ::time_t,
20-
pub st_ctime_nsec: ::c_long,
21-
pub st_size: ::off_t,
22-
pub st_blocks: ::blkcnt_t,
23-
pub st_blksize: ::blksize_t,
24-
pub st_flags: ::fflags_t,
25-
pub st_gen: ::uint32_t,
26-
pub st_lspare: ::int32_t,
27-
pub st_birthtime: ::time_t,
28-
pub st_birthtime_nsec: ::c_long,
29-
}
30-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
pub type dev_t = u32;
2+
pub type ino_t = u32;
3+
pub type nlink_t = u16;
4+
5+
s! {
6+
pub struct stat {
7+
pub st_dev: ::dev_t,
8+
pub st_ino: ::ino_t,
9+
pub st_mode: ::mode_t,
10+
pub st_nlink: ::nlink_t,
11+
pub st_uid: ::uid_t,
12+
pub st_gid: ::gid_t,
13+
pub st_rdev: ::dev_t,
14+
pub st_atime: ::time_t,
15+
pub st_atime_nsec: ::c_long,
16+
pub st_mtime: ::time_t,
17+
pub st_mtime_nsec: ::c_long,
18+
pub st_ctime: ::time_t,
19+
pub st_ctime_nsec: ::c_long,
20+
pub st_size: ::off_t,
21+
pub st_blocks: ::blkcnt_t,
22+
pub st_blksize: ::blksize_t,
23+
pub st_flags: ::fflags_t,
24+
pub st_gen: ::uint32_t,
25+
pub st_lspare: ::int32_t,
26+
pub st_birthtime: ::time_t,
27+
pub st_birthtime_nsec: ::c_long,
28+
29+
#[cfg(target_arch = "x86")]
30+
__unused: [u8; 8],
31+
}
32+
33+
pub struct dirent {
34+
pub d_fileno: u32,
35+
pub d_reclen: u16,
36+
pub d_type: u8,
37+
pub d_namlen: u8,
38+
pub d_name: [::c_char; 256],
39+
}
40+
41+
pub struct kevent {
42+
pub ident: ::uintptr_t,
43+
pub filter: ::c_short,
44+
pub flags: ::c_ushort,
45+
pub fflags: ::c_uint,
46+
pub data: ::intptr_t,
47+
pub udata: *mut ::c_void,
48+
pub ext: [::uint64_t; 0],
49+
}
50+
}
51+
52+
pub const KEVENT_EXT_ZEROED: [::uint64_t; 0] = [0; 0];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
pub type dev_t = u64;
2+
pub type ino_t = u64;
3+
pub type nlink_t = u64;
4+
5+
s! {
6+
pub struct stat {
7+
pub st_dev: ::dev_t,
8+
pub st_ino: ::ino_t,
9+
pub st_nlink: ::nlink_t,
10+
pub st_mode: ::mode_t,
11+
pub st_pad0: ::uint16_t,
12+
pub st_uid: ::uid_t,
13+
pub st_gid: ::gid_t,
14+
pub st_pad1: ::uint32_t,
15+
pub st_rdev: ::dev_t,
16+
17+
#[cfg(target_arch = "x86")]
18+
pub st_atime_ext: ::int32_t,
19+
20+
pub st_atime: ::time_t,
21+
pub st_atime_nsec: ::c_long,
22+
23+
#[cfg(target_arch = "x86")]
24+
pub st_mtime_ext: i32,
25+
26+
pub st_mtime: ::time_t,
27+
pub st_mtime_nsec: ::c_long,
28+
29+
#[cfg(target_arch = "x86")]
30+
pub st_ctime_ext: ::int32_t,
31+
32+
pub st_ctime: ::time_t,
33+
pub st_ctime_nsec: ::c_long,
34+
35+
#[cfg(target_arch = "x86")]
36+
pub st_birthtime_ext: ::int32_t,
37+
38+
pub st_birthtime: ::time_t,
39+
pub st_birthtime_nsec: ::c_long,
40+
pub st_size: ::off_t,
41+
pub st_blocks: ::blkcnt_t,
42+
pub st_blksize: ::blksize_t,
43+
pub st_flags: ::fflags_t,
44+
pub st_gen: ::uint64_t,
45+
pub st_spare: [::uint64_t; 10],
46+
}
47+
48+
pub struct dirent {
49+
pub d_fileno: u64,
50+
pub d_off: u64,
51+
pub d_reclen: u16,
52+
pub d_type: u8,
53+
pub d_pad0: u8,
54+
pub d_namlen: u16,
55+
pub d_pad1: u16,
56+
pub d_name: [::c_char; 256],
57+
}
58+
59+
pub struct kevent {
60+
pub ident: ::uintptr_t,
61+
pub filter: ::c_short,
62+
pub flags: ::c_ushort,
63+
pub fflags: ::c_uint,
64+
pub data: ::int64_t,
65+
pub udata: *mut ::c_void,
66+
pub ext: [::uint64_t; 4],
67+
}
68+
}
69+
70+
pub const KEVENT_EXT_ZEROED: [::uint64_t; 4] = [0; 4];

src/unix/bsd/freebsdlike/freebsd/mod.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
pub type fflags_t = u32;
22
pub type clock_t = i32;
3-
pub type ino_t = u32;
43
pub type lwpid_t = i32;
5-
pub type nlink_t = u16;
64
pub type blksize_t = u32;
75
pub type clockid_t = ::c_int;
86
pub type sem_t = _sem;
@@ -43,14 +41,6 @@ s! {
4341
pub aio_sigevent: sigevent
4442
}
4543

46-
pub struct dirent {
47-
pub d_fileno: u32,
48-
pub d_reclen: u16,
49-
pub d_type: u8,
50-
pub d_namlen: u8,
51-
pub d_name: [::c_char; 256],
52-
}
53-
5444
pub struct jail {
5545
pub version: u32,
5646
pub path: *mut ::c_char,
@@ -595,3 +585,16 @@ cfg_if! {
595585
// Unknown target_arch
596586
}
597587
}
588+
589+
cfg_if! {
590+
if #[cfg(freebsd12_abi)] {
591+
// Starting with FreeBSD 12.0-RELEASE, the kernel uses 64-bit
592+
// ino_t. This affects `struct stat` and `struct dirent_t` as
593+
// well as a few types above.
594+
mod freebsd12;
595+
pub use self::freebsd12::*;
596+
} else {
597+
mod freebsd11;
598+
pub use self::freebsd11::*;
599+
}
600+
}

src/unix/bsd/freebsdlike/freebsd/x86.rs

-27
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,3 @@ pub type c_long = i32;
22
pub type c_ulong = u32;
33
pub type time_t = i32;
44
pub type suseconds_t = i32;
5-
6-
s! {
7-
pub struct stat {
8-
pub st_dev: ::dev_t,
9-
pub st_ino: ::ino_t,
10-
pub st_mode: ::mode_t,
11-
pub st_nlink: ::nlink_t,
12-
pub st_uid: ::uid_t,
13-
pub st_gid: ::gid_t,
14-
pub st_rdev: ::dev_t,
15-
pub st_atime: ::time_t,
16-
pub st_atime_nsec: ::c_long,
17-
pub st_mtime: ::time_t,
18-
pub st_mtime_nsec: ::c_long,
19-
pub st_ctime: ::time_t,
20-
pub st_ctime_nsec: ::c_long,
21-
pub st_size: ::off_t,
22-
pub st_blocks: ::blkcnt_t,
23-
pub st_blksize: ::blksize_t,
24-
pub st_flags: ::fflags_t,
25-
pub st_gen: ::uint32_t,
26-
pub st_lspare: ::int32_t,
27-
pub st_birthtime: ::time_t,
28-
pub st_birthtime_nsec: ::c_long,
29-
__unused: [u8; 8],
30-
}
31-
}

src/unix/bsd/freebsdlike/freebsd/x86_64.rs

-26
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,3 @@ pub type c_long = i64;
22
pub type c_ulong = u64;
33
pub type time_t = i64;
44
pub type suseconds_t = i64;
5-
6-
s! {
7-
pub struct stat {
8-
pub st_dev: ::dev_t,
9-
pub st_ino: ::ino_t,
10-
pub st_mode: ::mode_t,
11-
pub st_nlink: ::nlink_t,
12-
pub st_uid: ::uid_t,
13-
pub st_gid: ::gid_t,
14-
pub st_rdev: ::dev_t,
15-
pub st_atime: ::time_t,
16-
pub st_atime_nsec: ::c_long,
17-
pub st_mtime: ::time_t,
18-
pub st_mtime_nsec: ::c_long,
19-
pub st_ctime: ::time_t,
20-
pub st_ctime_nsec: ::c_long,
21-
pub st_size: ::off_t,
22-
pub st_blocks: ::blkcnt_t,
23-
pub st_blksize: ::blksize_t,
24-
pub st_flags: ::fflags_t,
25-
pub st_gen: ::uint32_t,
26-
pub st_lspare: ::int32_t,
27-
pub st_birthtime: ::time_t,
28-
pub st_birthtime_nsec: ::c_long,
29-
}
30-
}

src/unix/bsd/freebsdlike/mod.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub type dev_t = u32;
21
pub type mode_t = u16;
32
pub type pthread_attr_t = *mut ::c_void;
43
pub type rlim_t = i64;
@@ -31,15 +30,6 @@ s! {
3130
__unused8: *mut ::c_void,
3231
}
3332

34-
pub struct kevent {
35-
pub ident: ::uintptr_t,
36-
pub filter: ::c_short,
37-
pub flags: ::c_ushort,
38-
pub fflags: ::c_uint,
39-
pub data: ::intptr_t,
40-
pub udata: *mut ::c_void,
41-
}
42-
4333
pub struct sockaddr_storage {
4434
pub ss_len: u8,
4535
pub ss_family: ::sa_family_t,

0 commit comments

Comments
 (0)