|
| 1 | +/* Copyright (c) [2023] [Syswonder Community] |
| 2 | + * [Rukos] is licensed under Mulan PSL v2. |
| 3 | + * You can use this software according to the terms and conditions of the Mulan PSL v2. |
| 4 | + * You may obtain a copy of Mulan PSL v2 at: |
| 5 | + * http://license.coscl.org.cn/MulanPSL2 |
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| 7 | + * See the Mulan PSL v2 for more details. |
| 8 | + */ |
| 9 | +//! random number generator from ruxrand for `/dev/random` and `/dev/urandom` |
| 10 | +
|
| 11 | +use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodePerm, VfsNodeType, VfsResult}; |
| 12 | + |
| 13 | +use axerrno::AxError; |
| 14 | + |
| 15 | +use ruxrand::request_entropy; |
| 16 | + |
| 17 | +/// A random device behaves like `/dev/random`. |
| 18 | +/// |
| 19 | +/// It always returns a chunk of random bytes when read, and all writes are discarded. |
| 20 | +pub struct RandomDev; |
| 21 | + |
| 22 | +impl VfsNodeOps for RandomDev { |
| 23 | + fn get_attr(&self) -> VfsResult<VfsNodeAttr> { |
| 24 | + Ok(VfsNodeAttr::new( |
| 25 | + 6, |
| 26 | + VfsNodePerm::default_file(), |
| 27 | + VfsNodeType::CharDevice, |
| 28 | + 0, |
| 29 | + 0, |
| 30 | + )) |
| 31 | + } |
| 32 | + |
| 33 | + fn set_mode(&self, _mode: VfsNodePerm) -> VfsResult { |
| 34 | + Ok(()) |
| 35 | + } |
| 36 | + |
| 37 | + fn read_at(&self, _offset: u64, buf: &mut [u8]) -> VfsResult<usize> { |
| 38 | + request_entropy(buf).map_err(|_e| AxError::Io)?; |
| 39 | + Ok(buf.len()) |
| 40 | + } |
| 41 | + |
| 42 | + fn write_at(&self, _offset: u64, buf: &[u8]) -> VfsResult<usize> { |
| 43 | + Ok(buf.len()) |
| 44 | + } |
| 45 | + |
| 46 | + fn truncate(&self, _size: u64) -> VfsResult { |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | + |
| 50 | + axfs_vfs::impl_vfs_non_dir_default! {} |
| 51 | +} |
0 commit comments