Skip to content

Commit

Permalink
freebsd: Add support for FreeBSD 12.x ABI
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dumbbell committed Aug 10, 2017
1 parent 916b82d commit fa60c72
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 99 deletions.
41 changes: 41 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::process::Command;

#[cfg(not(target_os = "freebsd"))]
fn detect_freebsd_abi_changes() {}

#[cfg(target_os = "freebsd")]
fn detect_freebsd_abi_changes() {
// We use freebsd-version(1). We could use `uname -k` to get the
// version of the kernel, but it wouldn't reflect the version of the
// userland (in case we are in a jail).
//
// freebsd-version(1) appeared in FreeBSD 10.0, so it's good enough.

let _ = Command::new("freebsd-version").arg("-u")
.output()
.and_then(
|output| {
assert!(output.status.success());

let full_version_string = String::from_utf8_lossy(&output.stdout);
let split_version: Vec<&str> = full_version_string
.trim()
.splitn(2, '.').collect();

assert!(split_version.len() >= 1);

let version: u32 = split_version[0]
.parse().unwrap();

// FreeBSD kernel versions are published in the following document:
// https://www.freebsd.org/doc/en/books/porters-handbook/versions.html

if version >= 12 { println!("cargo:rustc-cfg=freebsd12_abi"); }

Ok(())
});
}

fn main() {
detect_freebsd_abi_changes();
}
10 changes: 10 additions & 0 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub type clock_t = u64;
pub type dev_t = u32;
pub type ino_t = u64;
pub type nlink_t = u32;
pub type blksize_t = i64;
Expand Down Expand Up @@ -168,6 +169,15 @@ s! {
pub ifm_index: ::c_ushort,
pub ifm_data: if_data,
}

pub struct kevent {
pub ident: ::uintptr_t,
pub filter: ::c_short,
pub flags: ::c_ushort,
pub fflags: ::c_uint,
pub data: ::intptr_t,
pub udata: *mut ::c_void,
}
}

pub const RAND_MAX: ::c_int = 0x7fff_ffff;
Expand Down
26 changes: 0 additions & 26 deletions src/unix/bsd/freebsdlike/freebsd/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,3 @@ pub type c_long = i64;
pub type c_ulong = u64;
pub type time_t = i64;
pub type suseconds_t = i64;

s! {
pub struct stat {
pub st_dev: ::dev_t,
pub st_ino: ::ino_t,
pub st_mode: ::mode_t,
pub st_nlink: ::nlink_t,
pub st_uid: ::uid_t,
pub st_gid: ::gid_t,
pub st_rdev: ::dev_t,
pub st_atime: ::time_t,
pub st_atime_nsec: ::c_long,
pub st_mtime: ::time_t,
pub st_mtime_nsec: ::c_long,
pub st_ctime: ::time_t,
pub st_ctime_nsec: ::c_long,
pub st_size: ::off_t,
pub st_blocks: ::blkcnt_t,
pub st_blksize: ::blksize_t,
pub st_flags: ::fflags_t,
pub st_gen: ::uint32_t,
pub st_lspare: ::int32_t,
pub st_birthtime: ::time_t,
pub st_birthtime_nsec: ::c_long,
}
}
52 changes: 52 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/freebsd11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pub type dev_t = u32;
pub type ino_t = u32;
pub type nlink_t = u16;

s! {
pub struct stat {
pub st_dev: ::dev_t,
pub st_ino: ::ino_t,
pub st_mode: ::mode_t,
pub st_nlink: ::nlink_t,
pub st_uid: ::uid_t,
pub st_gid: ::gid_t,
pub st_rdev: ::dev_t,
pub st_atime: ::time_t,
pub st_atime_nsec: ::c_long,
pub st_mtime: ::time_t,
pub st_mtime_nsec: ::c_long,
pub st_ctime: ::time_t,
pub st_ctime_nsec: ::c_long,
pub st_size: ::off_t,
pub st_blocks: ::blkcnt_t,
pub st_blksize: ::blksize_t,
pub st_flags: ::fflags_t,
pub st_gen: ::uint32_t,
pub st_lspare: ::int32_t,
pub st_birthtime: ::time_t,
pub st_birthtime_nsec: ::c_long,

#[cfg(target_arch = "x86")]
__unused: [u8; 8],
}

pub struct dirent {
pub d_fileno: u32,
pub d_reclen: u16,
pub d_type: u8,
pub d_namlen: u8,
pub d_name: [::c_char; 256],
}

pub struct kevent {
pub ident: ::uintptr_t,
pub filter: ::c_short,
pub flags: ::c_ushort,
pub fflags: ::c_uint,
pub data: ::intptr_t,
pub udata: *mut ::c_void,
pub ext: [::uint64_t; 0],
}
}

pub const KEVENT_EXT_ZEROED: [::uint64_t; 0] = [0; 0];
70 changes: 70 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/freebsd12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
pub type dev_t = u64;
pub type ino_t = u64;
pub type nlink_t = u64;

s! {
pub struct stat {
pub st_dev: ::dev_t,
pub st_ino: ::ino_t,
pub st_nlink: ::nlink_t,
pub st_mode: ::mode_t,
pub st_pad0: ::uint16_t,
pub st_uid: ::uid_t,
pub st_gid: ::gid_t,
pub st_pad1: ::uint32_t,
pub st_rdev: ::dev_t,

#[cfg(target_arch = "x86")]
pub st_atime_ext: ::int32_t,

pub st_atime: ::time_t,
pub st_atime_nsec: ::c_long,

#[cfg(target_arch = "x86")]
pub st_mtime_ext: i32,

pub st_mtime: ::time_t,
pub st_mtime_nsec: ::c_long,

#[cfg(target_arch = "x86")]
pub st_ctime_ext: ::int32_t,

pub st_ctime: ::time_t,
pub st_ctime_nsec: ::c_long,

#[cfg(target_arch = "x86")]
pub st_birthtime_ext: ::int32_t,

pub st_birthtime: ::time_t,
pub st_birthtime_nsec: ::c_long,
pub st_size: ::off_t,
pub st_blocks: ::blkcnt_t,
pub st_blksize: ::blksize_t,
pub st_flags: ::fflags_t,
pub st_gen: ::uint64_t,
pub st_spare: [::uint64_t; 10],
}

pub struct dirent {
pub d_fileno: u64,
pub d_off: u64,
pub d_reclen: u16,
pub d_type: u8,
pub d_pad0: u8,
pub d_namlen: u16,
pub d_pad1: u16,
pub d_name: [::c_char; 256],
}

pub struct kevent {
pub ident: ::uintptr_t,
pub filter: ::c_short,
pub flags: ::c_ushort,
pub fflags: ::c_uint,
pub data: ::int64_t,
pub udata: *mut ::c_void,
pub ext: [::uint64_t; 4],
}
}

pub const KEVENT_EXT_ZEROED: [::uint64_t; 4] = [0; 4];
23 changes: 13 additions & 10 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub type fflags_t = u32;
pub type clock_t = i32;
pub type ino_t = u32;
pub type lwpid_t = i32;
pub type nlink_t = u16;
pub type blksize_t = u32;
pub type clockid_t = ::c_int;
pub type sem_t = _sem;
Expand Down Expand Up @@ -43,14 +41,6 @@ s! {
pub aio_sigevent: sigevent
}

pub struct dirent {
pub d_fileno: u32,
pub d_reclen: u16,
pub d_type: u8,
pub d_namlen: u8,
pub d_name: [::c_char; 256],
}

pub struct jail {
pub version: u32,
pub path: *mut ::c_char,
Expand Down Expand Up @@ -595,3 +585,16 @@ cfg_if! {
// Unknown target_arch
}
}

cfg_if! {
if #[cfg(freebsd12_abi)] {
// Starting with FreeBSD 12.0-RELEASE, the kernel uses 64-bit
// ino_t. This affects `struct stat` and `struct dirent_t` as
// well as a few types above.
mod freebsd12;
pub use self::freebsd12::*;
} else {
mod freebsd11;
pub use self::freebsd11::*;
}
}
27 changes: 0 additions & 27 deletions src/unix/bsd/freebsdlike/freebsd/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,3 @@ pub type c_long = i32;
pub type c_ulong = u32;
pub type time_t = i32;
pub type suseconds_t = i32;

s! {
pub struct stat {
pub st_dev: ::dev_t,
pub st_ino: ::ino_t,
pub st_mode: ::mode_t,
pub st_nlink: ::nlink_t,
pub st_uid: ::uid_t,
pub st_gid: ::gid_t,
pub st_rdev: ::dev_t,
pub st_atime: ::time_t,
pub st_atime_nsec: ::c_long,
pub st_mtime: ::time_t,
pub st_mtime_nsec: ::c_long,
pub st_ctime: ::time_t,
pub st_ctime_nsec: ::c_long,
pub st_size: ::off_t,
pub st_blocks: ::blkcnt_t,
pub st_blksize: ::blksize_t,
pub st_flags: ::fflags_t,
pub st_gen: ::uint32_t,
pub st_lspare: ::int32_t,
pub st_birthtime: ::time_t,
pub st_birthtime_nsec: ::c_long,
__unused: [u8; 8],
}
}
26 changes: 0 additions & 26 deletions src/unix/bsd/freebsdlike/freebsd/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,3 @@ pub type c_long = i64;
pub type c_ulong = u64;
pub type time_t = i64;
pub type suseconds_t = i64;

s! {
pub struct stat {
pub st_dev: ::dev_t,
pub st_ino: ::ino_t,
pub st_mode: ::mode_t,
pub st_nlink: ::nlink_t,
pub st_uid: ::uid_t,
pub st_gid: ::gid_t,
pub st_rdev: ::dev_t,
pub st_atime: ::time_t,
pub st_atime_nsec: ::c_long,
pub st_mtime: ::time_t,
pub st_mtime_nsec: ::c_long,
pub st_ctime: ::time_t,
pub st_ctime_nsec: ::c_long,
pub st_size: ::off_t,
pub st_blocks: ::blkcnt_t,
pub st_blksize: ::blksize_t,
pub st_flags: ::fflags_t,
pub st_gen: ::uint32_t,
pub st_lspare: ::int32_t,
pub st_birthtime: ::time_t,
pub st_birthtime_nsec: ::c_long,
}
}
10 changes: 0 additions & 10 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub type dev_t = u32;
pub type mode_t = u16;
pub type pthread_attr_t = *mut ::c_void;
pub type rlim_t = i64;
Expand Down Expand Up @@ -31,15 +30,6 @@ s! {
__unused8: *mut ::c_void,
}

pub struct kevent {
pub ident: ::uintptr_t,
pub filter: ::c_short,
pub flags: ::c_ushort,
pub fflags: ::c_uint,
pub data: ::intptr_t,
pub udata: *mut ::c_void,
}

pub struct sockaddr_storage {
pub ss_len: u8,
pub ss_family: ::sa_family_t,
Expand Down

0 comments on commit fa60c72

Please sign in to comment.