blob: d3d09289b6182a7168ac528f0d4bc611e74582fb [file] [log] [blame]
Joel Galenson4be0c6d2020-07-07 13:20:14 -07001// 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 CloudABI
10use crate::Error;
11use core::num::NonZeroU32;
12
13extern "C" {
14 fn cloudabi_sys_random_get(buf: *mut u8, buf_len: usize) -> u16;
15}
16
17pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
18 let errno = unsafe { cloudabi_sys_random_get(dest.as_mut_ptr(), dest.len()) };
19 if let Some(code) = NonZeroU32::new(errno as u32) {
20 error!("cloudabi_sys_random_get: failed with {}", errno);
21 Err(Error::from(code))
22 } else {
23 Ok(()) // Zero means success for CloudABI
24 }
25}