Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make libc dependency optional #928

Merged
merged 3 commits into from Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,10 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
### Fixes
- The `Bernoulli` distribution constructors now reports an error on NaN and on
`denominator == 0`. (#925)
- Use `std::sync::Once` to register fork handler, avoiding possible atomicity violation (#928)

### Changes
- Unix: make libc dependency optional; only use fork protection with std feature (#928)

### Additions
- Implement `std::error::Error` for `BernoulliError` (#919)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -27,7 +27,7 @@ nightly = ["simd_support"] # enables all features requiring nightly rust
serde1 = [] # does nothing, deprecated

# Optional dependencies:
std = ["rand_core/std", "rand_chacha/std", "alloc", "getrandom"]
std = ["rand_core/std", "rand_chacha/std", "alloc", "getrandom", "libc"]
alloc = ["rand_core/alloc"] # enables Vec and Box support (without std)
# re-export optional WASM dependencies to avoid breakage:
# Warning: wasm-bindgen and stdweb features will be removed in rand 0.8;
Expand Down Expand Up @@ -68,7 +68,7 @@ features = ["into_bits"]

[target.'cfg(unix)'.dependencies]
# Used for fork protection (reseeding.rs)
libc = { version = "0.2.22", default-features = false }
libc = { version = "0.2.22", optional = true, default-features = false }

# Emscripten does not support 128-bit integers, which are used by ChaCha code.
# We work around this by using a different RNG.
Expand Down
23 changes: 9 additions & 14 deletions src/rngs/adapter/reseeding.rs
Expand Up @@ -279,11 +279,10 @@ where
}


#[cfg(all(unix, not(target_os = "emscripten")))]
#[cfg(all(unix, feature = "std", not(target_os = "emscripten")))]
mod fork {
Copy link
Contributor

@bjorn3 bjorn3 Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires libc and not std, right?

Edit: missed that you switched to Once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once requires std. std implies libc in the Cargo.toml

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was a separate commit, so may be confusing.

use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[allow(deprecated)] // Required for compatibility with Rust < 1.24.
use core::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT};
use core::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Once;

// Fork protection
//
Expand All @@ -297,31 +296,27 @@ mod fork {
// don't update `fork_counter`, so a reseed is attempted as soon as
// possible.

#[allow(deprecated)]
static RESEEDING_RNG_FORK_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
static RESEEDING_RNG_FORK_COUNTER: AtomicUsize = AtomicUsize::new(0);

pub fn get_fork_counter() -> usize {
RESEEDING_RNG_FORK_COUNTER.load(Ordering::Relaxed)
}

#[allow(deprecated)]
static FORK_HANDLER_REGISTERED: AtomicBool = ATOMIC_BOOL_INIT;

extern "C" fn fork_handler() {
// Note: fetch_add is defined to wrap on overflow
// (which is what we want).
RESEEDING_RNG_FORK_COUNTER.fetch_add(1, Ordering::Relaxed);
}

pub fn register_fork_handler() {
if !FORK_HANDLER_REGISTERED.load(Ordering::Relaxed) {
unsafe { libc::pthread_atfork(None, None, Some(fork_handler)) };
FORK_HANDLER_REGISTERED.store(true, Ordering::Relaxed);
}
static REGISTER: Once = Once::new();
REGISTER.call_once(|| unsafe {
libc::pthread_atfork(None, None, Some(fork_handler));
});
}
}

#[cfg(not(all(unix, not(target_os = "emscripten"))))]
#[cfg(not(all(unix, feature = "std", not(target_os = "emscripten"))))]
mod fork {
pub fn get_fork_counter() -> usize {
0
Expand Down