Skip to content

Commit

Permalink
Auto merge of #1432 - josephlr:errno, r=gnzlbg
Browse files Browse the repository at this point in the history
Add __errno_location on Redox, DragonFlyBSD, Haiku

Currently [`libstd`](https://github.com/rust-lang/rust/blob/909f5a049415a815b23502a5498de9a99bbf276b/src/libstd/sys/unix/os.rs#L29-L49) and the [`getrandom` crate](https://github.com/rust-random/getrandom/pull/54/files#diff-027a56068e2bf3d31dc4273ee6e07034R12) have to use external declarations in order to implement `errno_location` across all UNIX platforms. This change adds bindings for the remaining UNIX platforms, allowing everyone to just use normal `libc` bindings.

This also removes the need for a seperate [`errno-dragonfly`](https://crates.io/crates/errno-dragonfly) crate.

Links to the relevant header files:

- Redox: [`relibc`'s `bits/errno.h`](https://github.com/redox-os/relibc/blob/master/include/bits/errno.h) uses `__errno_location`
- Haiku: [`posix/errno.h`](https://github.com/luciang/haiku/blob/master/headers/posix/errno.h) uses `_errnop`
- DragonFlyBSD: [`sys/errno.h`](https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/sys/sys/errno.h) uses `__error` just like FreeBSD (which makes sense)
  - Note that the `__error` function is inlined. I don't think this causes a problem.
  • Loading branch information
bors committed Jul 15, 2019
2 parents 54b03c5 + df34d17 commit 47535e6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
14 changes: 9 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ use std::str;
fn main() {
let rustc_minor_ver =
rustc_minor_version().expect("Failed to get rustc version");
let rustc_dep_of_std =
std::env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
let align_cargo_feature = std::env::var("CARGO_FEATURE_ALIGN").is_ok();
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();

if std::env::var("CARGO_FEATURE_USE_STD").is_ok() {
if env::var("CARGO_FEATURE_USE_STD").is_ok() {
println!(
"cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
please consider using the `std` cargo feature instead\""
);
}

if std::env::var("LIBC_CI").is_ok() {
if env::var("LIBC_CI").is_ok() {
if let Some(12) = which_freebsd() {
println!("cargo:rustc-cfg=freebsd12");
}
Expand Down Expand Up @@ -53,6 +52,11 @@ fn main() {
if rustc_minor_ver >= 33 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_packedN");
}

// #[thread_local] is currently unstable
if rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_thread_local");
}
}

fn rustc_minor_version() -> Option<u32> {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
feature = "rustc-dep-of-std",
feature(cfg_target_vendor, link_cfg, no_core)
)]
#![cfg_attr(libc_thread_local, feature(thread_local))]
// Enable extra lints:
#![cfg_attr(feature = "extra_traits", deny(missing_debug_implementations))]
#![deny(missing_copy_implementations, safe_packed_borrows)]
Expand Down
12 changes: 12 additions & 0 deletions src/unix/bsd/freebsdlike/dragonfly/errno.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// DragonFlyBSD's __error function is declared with "static inline", so it must
// be implemented in the libc crate, as a pointer to a static thread_local.
f! {
pub fn __error() -> *mut ::c_int {
&mut errno
}
}

extern {
#[thread_local]
pub static mut errno: ::c_int;
}
7 changes: 7 additions & 0 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,10 @@ extern {
pub fn fstatfs(fd: ::c_int, buf: *mut statfs) -> ::c_int;
pub fn uname(buf: *mut ::utsname) -> ::c_int;
}

cfg_if! {
if #[cfg(libc_thread_local)] {
mod errno;
pub use self::errno::*;
}
}
1 change: 1 addition & 0 deletions src/unix/haiku/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ extern {
pub fn setrlimit(resource: ::c_int, rlim: *const ::rlimit) -> ::c_int;
pub fn strerror_r(errnum: ::c_int, buf: *mut c_char,
buflen: ::size_t) -> ::c_int;
pub fn _errnop() -> *mut ::c_int;

pub fn abs(i: ::c_int) -> ::c_int;
pub fn atof(s: *const ::c_char) -> ::c_double;
Expand Down
1 change: 1 addition & 0 deletions src/unix/redox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ extern {

pub fn strerror_r(errnum: ::c_int, buf: *mut c_char,
buflen: ::size_t) -> ::c_int;
pub fn __errno_location() -> *mut ::c_int;

// malloc.h
pub fn memalign(align: ::size_t, size: ::size_t) -> *mut ::c_void;
Expand Down

0 comments on commit 47535e6

Please sign in to comment.