Skip to content

Commit 47535e6

Browse files
committed
Auto merge of #1432 - josephlr:errno, r=gnzlbg
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.
2 parents 54b03c5 + df34d17 commit 47535e6

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

build.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ use std::str;
55
fn main() {
66
let rustc_minor_ver =
77
rustc_minor_version().expect("Failed to get rustc version");
8-
let rustc_dep_of_std =
9-
std::env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
10-
let align_cargo_feature = std::env::var("CARGO_FEATURE_ALIGN").is_ok();
8+
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
9+
let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();
1110

12-
if std::env::var("CARGO_FEATURE_USE_STD").is_ok() {
11+
if env::var("CARGO_FEATURE_USE_STD").is_ok() {
1312
println!(
1413
"cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
1514
please consider using the `std` cargo feature instead\""
1615
);
1716
}
1817

19-
if std::env::var("LIBC_CI").is_ok() {
18+
if env::var("LIBC_CI").is_ok() {
2019
if let Some(12) = which_freebsd() {
2120
println!("cargo:rustc-cfg=freebsd12");
2221
}
@@ -53,6 +52,11 @@ fn main() {
5352
if rustc_minor_ver >= 33 || rustc_dep_of_std {
5453
println!("cargo:rustc-cfg=libc_packedN");
5554
}
55+
56+
// #[thread_local] is currently unstable
57+
if rustc_dep_of_std {
58+
println!("cargo:rustc-cfg=libc_thread_local");
59+
}
5660
}
5761

5862
fn rustc_minor_version() -> Option<u32> {

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
feature = "rustc-dep-of-std",
2222
feature(cfg_target_vendor, link_cfg, no_core)
2323
)]
24+
#![cfg_attr(libc_thread_local, feature(thread_local))]
2425
// Enable extra lints:
2526
#![cfg_attr(feature = "extra_traits", deny(missing_debug_implementations))]
2627
#![deny(missing_copy_implementations, safe_packed_borrows)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// DragonFlyBSD's __error function is declared with "static inline", so it must
2+
// be implemented in the libc crate, as a pointer to a static thread_local.
3+
f! {
4+
pub fn __error() -> *mut ::c_int {
5+
&mut errno
6+
}
7+
}
8+
9+
extern {
10+
#[thread_local]
11+
pub static mut errno: ::c_int;
12+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,10 @@ extern {
10601060
pub fn fstatfs(fd: ::c_int, buf: *mut statfs) -> ::c_int;
10611061
pub fn uname(buf: *mut ::utsname) -> ::c_int;
10621062
}
1063+
1064+
cfg_if! {
1065+
if #[cfg(libc_thread_local)] {
1066+
mod errno;
1067+
pub use self::errno::*;
1068+
}
1069+
}

src/unix/haiku/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ extern {
12671267
pub fn setrlimit(resource: ::c_int, rlim: *const ::rlimit) -> ::c_int;
12681268
pub fn strerror_r(errnum: ::c_int, buf: *mut c_char,
12691269
buflen: ::size_t) -> ::c_int;
1270+
pub fn _errnop() -> *mut ::c_int;
12701271

12711272
pub fn abs(i: ::c_int) -> ::c_int;
12721273
pub fn atof(s: *const ::c_char) -> ::c_double;

src/unix/redox/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ extern {
539539

540540
pub fn strerror_r(errnum: ::c_int, buf: *mut c_char,
541541
buflen: ::size_t) -> ::c_int;
542+
pub fn __errno_location() -> *mut ::c_int;
542543

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

0 commit comments

Comments
 (0)