]> git.proxmox.com Git - rustc.git/blame - vendor/getrandom/src/bsd_arandom.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / vendor / getrandom / src / bsd_arandom.rs
CommitLineData
f20569fa
XL
1// Copyright 2018 Developers of the Rand project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Implementation for FreeBSD and NetBSD
10use crate::{util_libc::sys_fill_exact, Error};
11use core::ptr;
12
13fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t {
14 static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
15 let mut len = buf.len();
16 let ret = unsafe {
17 libc::sysctl(
18 MIB.as_ptr(),
19 MIB.len() as libc::c_uint,
20 buf.as_mut_ptr() as *mut _,
21 &mut len,
22 ptr::null(),
23 0,
24 )
25 };
26 if ret == -1 {
27 -1
28 } else {
29 len as libc::ssize_t
30 }
31}
32
33pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
34 #[cfg(target_os = "freebsd")]
35 {
36 use crate::util_libc::Weak;
37 static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
38 type GetRandomFn =
39 unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t;
40
41 if let Some(fptr) = GETRANDOM.ptr() {
42 let func: GetRandomFn = unsafe { core::mem::transmute(fptr) };
43 return sys_fill_exact(dest, |buf| unsafe { func(buf.as_mut_ptr(), buf.len(), 0) });
44 }
45 }
46 // Both FreeBSD and NetBSD will only return up to 256 bytes at a time, and
47 // older NetBSD kernels will fail on longer buffers.
48 for chunk in dest.chunks_mut(256) {
49 sys_fill_exact(chunk, kern_arnd)?
50 }
51 Ok(())
52}