From aa3578fa8551a1b38985b166aee93bb03003b58b Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Tue, 9 Jul 2019 00:19:23 -0700 Subject: [PATCH 1/6] Add __errno_location on Redox, DragonFlyBSD, Haiku --- src/unix/bsd/freebsdlike/freebsd/mod.rs | 2 -- src/unix/bsd/freebsdlike/mod.rs | 2 ++ src/unix/haiku/mod.rs | 1 + src/unix/redox/mod.rs | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/unix/bsd/freebsdlike/freebsd/mod.rs b/src/unix/bsd/freebsdlike/freebsd/mod.rs index 9f11c202bb4ea..2fe755d9d13d4 100644 --- a/src/unix/bsd/freebsdlike/freebsd/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/mod.rs @@ -1132,8 +1132,6 @@ f! { } extern { - pub fn __error() -> *mut ::c_int; - pub fn clock_getres(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int; pub fn clock_gettime(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int; pub fn clock_settime(clk_id: ::clockid_t, tp: *const ::timespec) -> ::c_int; diff --git a/src/unix/bsd/freebsdlike/mod.rs b/src/unix/bsd/freebsdlike/mod.rs index ce5452062ce3d..38dd8d05feef4 100644 --- a/src/unix/bsd/freebsdlike/mod.rs +++ b/src/unix/bsd/freebsdlike/mod.rs @@ -1098,6 +1098,8 @@ f! { } extern { + pub fn __error() -> *mut ::c_int; + pub fn sem_destroy(sem: *mut sem_t) -> ::c_int; pub fn sem_init(sem: *mut sem_t, pshared: ::c_int, diff --git a/src/unix/haiku/mod.rs b/src/unix/haiku/mod.rs index 7bb3285a2e163..d3e83f109e532 100644 --- a/src/unix/haiku/mod.rs +++ b/src/unix/haiku/mod.rs @@ -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 libc::c_int; pub fn abs(i: ::c_int) -> ::c_int; pub fn atof(s: *const ::c_char) -> ::c_double; diff --git a/src/unix/redox/mod.rs b/src/unix/redox/mod.rs index 388918eb9b5dc..a25f79540dfcb 100644 --- a/src/unix/redox/mod.rs +++ b/src/unix/redox/mod.rs @@ -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 libc::c_int; // malloc.h pub fn memalign(align: ::size_t, size: ::size_t) -> *mut ::c_void; From b7a27b5c7c9a0962d114b3330f68d06eb2fc7fec Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Tue, 9 Jul 2019 02:48:53 -0700 Subject: [PATCH 2/6] libc::c_int -> ::c_int --- src/unix/haiku/mod.rs | 2 +- src/unix/redox/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/unix/haiku/mod.rs b/src/unix/haiku/mod.rs index d3e83f109e532..c8cf8ad25d9a6 100644 --- a/src/unix/haiku/mod.rs +++ b/src/unix/haiku/mod.rs @@ -1267,7 +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 libc::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; diff --git a/src/unix/redox/mod.rs b/src/unix/redox/mod.rs index a25f79540dfcb..121bb63a6601f 100644 --- a/src/unix/redox/mod.rs +++ b/src/unix/redox/mod.rs @@ -539,7 +539,7 @@ extern { pub fn strerror_r(errnum: ::c_int, buf: *mut c_char, buflen: ::size_t) -> ::c_int; - pub fn __errno_location() -> *mut libc::c_int; + pub fn __errno_location() -> *mut ::c_int; // malloc.h pub fn memalign(align: ::size_t, size: ::size_t) -> *mut ::c_void; From 956ba427538edb63338aaaffe8372379459ac431 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Wed, 10 Jul 2019 18:01:41 -0700 Subject: [PATCH 3/6] dragonfly: Add errno behind libc_thread_local --- src/unix/bsd/freebsdlike/dragonfly/mod.rs | 9 +++++++++ src/unix/bsd/freebsdlike/freebsd/mod.rs | 2 ++ src/unix/bsd/freebsdlike/mod.rs | 2 -- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/unix/bsd/freebsdlike/dragonfly/mod.rs b/src/unix/bsd/freebsdlike/dragonfly/mod.rs index 3088b2dccd6f7..d1e0a473ab670 100644 --- a/src/unix/bsd/freebsdlike/dragonfly/mod.rs +++ b/src/unix/bsd/freebsdlike/dragonfly/mod.rs @@ -1036,9 +1036,18 @@ f! { (_CMSG_ALIGN(::mem::size_of::<::cmsghdr>()) + _CMSG_ALIGN(length as usize)) as ::c_uint } + + #[cfg(libc_thread_local)] + pub fn __error() -> *mut ::c_int { + &mut errno + } } extern { + #[cfg(libc_thread_local)] + #[thread_local] + static mut errno: ::c_int; + pub fn setgrent(); pub fn mprotect(addr: *mut ::c_void, len: ::size_t, prot: ::c_int) -> ::c_int; diff --git a/src/unix/bsd/freebsdlike/freebsd/mod.rs b/src/unix/bsd/freebsdlike/freebsd/mod.rs index 2fe755d9d13d4..9f11c202bb4ea 100644 --- a/src/unix/bsd/freebsdlike/freebsd/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/mod.rs @@ -1132,6 +1132,8 @@ f! { } extern { + pub fn __error() -> *mut ::c_int; + pub fn clock_getres(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int; pub fn clock_gettime(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int; pub fn clock_settime(clk_id: ::clockid_t, tp: *const ::timespec) -> ::c_int; diff --git a/src/unix/bsd/freebsdlike/mod.rs b/src/unix/bsd/freebsdlike/mod.rs index 38dd8d05feef4..ce5452062ce3d 100644 --- a/src/unix/bsd/freebsdlike/mod.rs +++ b/src/unix/bsd/freebsdlike/mod.rs @@ -1098,8 +1098,6 @@ f! { } extern { - pub fn __error() -> *mut ::c_int; - pub fn sem_destroy(sem: *mut sem_t) -> ::c_int; pub fn sem_init(sem: *mut sem_t, pshared: ::c_int, From 3fa021d7ab36b59e877082d3150d626afabc8454 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Wed, 10 Jul 2019 18:03:24 -0700 Subject: [PATCH 4/6] Add libc_thread_local cfg and unstable feature --- Cargo.toml | 3 ++- README.md | 3 +++ build.rs | 15 ++++++++++----- src/lib.rs | 1 + src/unix/bsd/freebsdlike/dragonfly/errno.rs | 12 ++++++++++++ src/unix/bsd/freebsdlike/dragonfly/mod.rs | 16 +++++++--------- 6 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 src/unix/bsd/freebsdlike/dragonfly/errno.rs diff --git a/Cargo.toml b/Cargo.toml index 120fbe19c662d..cf54d45a6e5a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,8 +26,9 @@ rustc-std-workspace-core = { version = "1.0.0", optional = true } default = ["std"] std = [] align = [] -rustc-dep-of-std = ['align', 'rustc-std-workspace-core'] +rustc-dep-of-std = ['align', 'rustc-std-workspace-core', 'unstable'] extra_traits = [] +unstable = [] # use_std is deprecated, use `std` instead use_std = [ 'std' ] diff --git a/README.md b/README.md index dc5ff04fccff7..1af0b1c2070ad 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,9 @@ libc = "0.2" * `extra_traits`: all `struct`s implemented in `libc` are `Copy` and `Clone`. This feature derives `Debug`, `Eq`, `Hash`, and `PartialEq`. +* `unstable`: enable currently unstable bindings. Right now, this just allows + bindings to `#[thread_local]` statics on certain platforms. Requires nightly. + * **deprecated**: `use_std` is deprecated, and is equivalent to `std`. ## Rust version support diff --git a/build.rs b/build.rs index 845294007409d..c997e6acfd3e7 100644 --- a/build.rs +++ b/build.rs @@ -5,18 +5,18 @@ 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(); + let unstable_cargo_feature = env::var("CARGO_FEATURE_UNSTABLE").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"); } @@ -53,6 +53,11 @@ fn main() { if rustc_minor_ver >= 33 || rustc_dep_of_std { println!("cargo:rustc-cfg=libc_packedN"); } + + // #[thread_local] is currently unstable + if unstable_cargo_feature || rustc_dep_of_std { + println!("cargo:rustc-cfg=libc_thread_local"); + } } fn rustc_minor_version() -> Option { diff --git a/src/lib.rs b/src/lib.rs index 2dc42702fcb7d..0f800cea0ae16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] diff --git a/src/unix/bsd/freebsdlike/dragonfly/errno.rs b/src/unix/bsd/freebsdlike/dragonfly/errno.rs new file mode 100644 index 0000000000000..e18036adf5c52 --- /dev/null +++ b/src/unix/bsd/freebsdlike/dragonfly/errno.rs @@ -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; +} diff --git a/src/unix/bsd/freebsdlike/dragonfly/mod.rs b/src/unix/bsd/freebsdlike/dragonfly/mod.rs index d1e0a473ab670..fc94fd3c7136e 100644 --- a/src/unix/bsd/freebsdlike/dragonfly/mod.rs +++ b/src/unix/bsd/freebsdlike/dragonfly/mod.rs @@ -1036,18 +1036,9 @@ f! { (_CMSG_ALIGN(::mem::size_of::<::cmsghdr>()) + _CMSG_ALIGN(length as usize)) as ::c_uint } - - #[cfg(libc_thread_local)] - pub fn __error() -> *mut ::c_int { - &mut errno - } } extern { - #[cfg(libc_thread_local)] - #[thread_local] - static mut errno: ::c_int; - pub fn setgrent(); pub fn mprotect(addr: *mut ::c_void, len: ::size_t, prot: ::c_int) -> ::c_int; @@ -1069,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::*; + } +} From 1b4747e77bc7e8219c7dac9119e0a309870a425b Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 12 Jul 2019 10:24:39 -0700 Subject: [PATCH 5/6] Update documentation to point to Rust issue --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1af0b1c2070ad..e39e442e7af7f 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,10 @@ libc = "0.2" * `extra_traits`: all `struct`s implemented in `libc` are `Copy` and `Clone`. This feature derives `Debug`, `Eq`, `Hash`, and `PartialEq`. -* `unstable`: enable currently unstable bindings. Right now, this just allows - bindings to `#[thread_local]` statics on certain platforms. Requires nightly. +* `unstable`: This feature enables `libc` bindings that are only possible with + unstable Rust features. Right now, this just for + [`extern` `#[thread_local]` statics](https://github.com/rust-lang/rust/issues/29594) + on certain platforms. Requires nightly. * **deprecated**: `use_std` is deprecated, and is equivalent to `std`. From df34d17bd9c3427731a000d6356e7d04d0255c8d Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Sun, 14 Jul 2019 22:10:05 -0700 Subject: [PATCH 6/6] Remove use of `unstable` feature --- Cargo.toml | 3 +-- README.md | 5 ----- build.rs | 3 +-- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf54d45a6e5a0..120fbe19c662d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,9 +26,8 @@ rustc-std-workspace-core = { version = "1.0.0", optional = true } default = ["std"] std = [] align = [] -rustc-dep-of-std = ['align', 'rustc-std-workspace-core', 'unstable'] +rustc-dep-of-std = ['align', 'rustc-std-workspace-core'] extra_traits = [] -unstable = [] # use_std is deprecated, use `std` instead use_std = [ 'std' ] diff --git a/README.md b/README.md index e39e442e7af7f..dc5ff04fccff7 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,6 @@ libc = "0.2" * `extra_traits`: all `struct`s implemented in `libc` are `Copy` and `Clone`. This feature derives `Debug`, `Eq`, `Hash`, and `PartialEq`. -* `unstable`: This feature enables `libc` bindings that are only possible with - unstable Rust features. Right now, this just for - [`extern` `#[thread_local]` statics](https://github.com/rust-lang/rust/issues/29594) - on certain platforms. Requires nightly. - * **deprecated**: `use_std` is deprecated, and is equivalent to `std`. ## Rust version support diff --git a/build.rs b/build.rs index c997e6acfd3e7..76ca0961e4782 100644 --- a/build.rs +++ b/build.rs @@ -7,7 +7,6 @@ fn main() { rustc_minor_version().expect("Failed to get rustc version"); 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(); - let unstable_cargo_feature = env::var("CARGO_FEATURE_UNSTABLE").is_ok(); if env::var("CARGO_FEATURE_USE_STD").is_ok() { println!( @@ -55,7 +54,7 @@ fn main() { } // #[thread_local] is currently unstable - if unstable_cargo_feature || rustc_dep_of_std { + if rustc_dep_of_std { println!("cargo:rustc-cfg=libc_thread_local"); } }