| 1 |
//! These files provide additional information about the /dev/random device |
| 2 |
//! |
| 3 |
//! Note that some of these entries are only documented in random(4), while some are also documented under proc(5) |
| 4 |
|
| 5 |
use crate::{read_value, write_value, ProcError, ProcResult}; |
| 6 |
use std::path::Path; |
| 7 |
|
| 8 |
const RANDOM_ROOT: &str = "/proc/sys/kernel/random"; |
| 9 |
|
| 10 |
/// This read-only file gives the available entropy, in bits. This will be a number in the range |
| 11 |
/// 0 to 4096 |
| 12 |
pub fn entropy_avail() -> ProcResult<u16> { |
| 13 |
read_value(Path::new(RANDOM_ROOT).join("entropy_avail")) |
| 14 |
} |
| 15 |
|
| 16 |
/// This file gives the size of the entropy pool |
| 17 |
/// |
| 18 |
/// The semantics of this file are different on kernel versions older than 2.6, however, since |
| 19 |
/// Linux 2.6 it is read-only, and gives the size of the entropy pool in bits, containing the value 4096. |
| 20 |
/// |
| 21 |
/// See `man random(4)` for more information |
| 22 |
pub fn poolsize() -> ProcResult<u16> { |
| 23 |
read_value(Path::new(RANDOM_ROOT).join("poolsize")) |
| 24 |
} |
| 25 |
|
| 26 |
/// This file contains the number of bits of entropy required for waking up processes that sleep waiting |
| 27 |
/// for entropy from /dev/random |
| 28 |
/// |
| 29 |
/// The default is 64. |
| 30 |
/// |
| 31 |
/// This will first attempt to read from `/proc/sys/kernel/random/read_wakeup_threshold` but it |
| 32 |
/// will fallback to `/proc/sys/kernel/random/write_wakeup_threshold` if the former file is not found. |
| 33 |
pub fn read_wakeup_threshold() -> ProcResult<u32> { |
| 34 |
match read_value(Path::new(RANDOM_ROOT).join("read_wakeup_threshold")) { |
| 35 |
Ok(val) => Ok(val), |
| 36 |
Err(err) => match err { |
| 37 |
ProcError::NotFound(_) => read_value(Path::new(RANDOM_ROOT).join("write_wakeup_threshold")), |
| 38 |
err => Err(err), |
| 39 |
}, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/// This file contains the number of bits of entropy below which we wake up processes that do a |
| 44 |
/// select(2) or poll(2) for write access to /dev/random. These values can be changed by writing to the file. |
| 45 |
pub fn write_wakeup_threshold(new_value: u32) -> ProcResult<()> { |
| 46 |
write_value(Path::new(RANDOM_ROOT).join("write_wakeup_threshold"), new_value) |
| 47 |
} |
| 48 |
|
| 49 |
/// This read-only file randomly generates a fresh 128-bit UUID on each read |
| 50 |
pub fn uuid() -> ProcResult<String> { |
| 51 |
read_value(Path::new(RANDOM_ROOT).join("uuid")) |
| 52 |
} |
| 53 |
|
| 54 |
/// This is a read-only file containing a 128-bit UUID generated at boot |
| 55 |
pub fn boot_id() -> ProcResult<String> { |
| 56 |
read_value(Path::new(RANDOM_ROOT).join("boot_id")) |
| 57 |
} |
| 58 |
|
| 59 |
#[cfg(test)] |
| 60 |
mod tests { |
| 61 |
use super::*; |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn test_entropy_avail() { |
| 65 |
let entropy = entropy_avail().unwrap(); |
| 66 |
assert!(entropy <= 4096); |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn test_poolsize() { |
| 71 |
// The kernel support section in the root lib.rs file says that we only aim to support >= 2.6 kernels, |
| 72 |
// so only test that case |
| 73 |
let _poolsize = poolsize().unwrap(); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_read_wakeup_threshold() { |
| 78 |
let threshold = read_wakeup_threshold().unwrap(); |
| 79 |
|
| 80 |
println!("{}", threshold); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn test_write_wakeup_threshold() { |
| 85 |
let old_threshold = read_wakeup_threshold().unwrap(); |
| 86 |
|
| 87 |
match write_wakeup_threshold(1024) { |
| 88 |
Ok(_) => (), |
| 89 |
Err(err) => match err { |
| 90 |
ProcError::PermissionDenied(_) => { |
| 91 |
// This is ok, not everyone wants to run our tests as root |
| 92 |
return; |
| 93 |
} |
| 94 |
err => panic!("test_write_wakeup_threshold error: {:?}", err), |
| 95 |
}, |
| 96 |
} |
| 97 |
|
| 98 |
// If we got here, let's restore the old threshold |
| 99 |
let _ = write_wakeup_threshold(old_threshold); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_uuid_fns() { |
| 104 |
let uuid = uuid().unwrap(); |
| 105 |
let boot_id = boot_id().unwrap(); |
| 106 |
|
| 107 |
println!("UUID: {}", uuid); |
| 108 |
println!("boot UUID: {}", boot_id); |
| 109 |
} |
| 110 |
} |