Add getrandom and sys/random.h
This commit is contained in:
@@ -50,6 +50,7 @@ pub mod _wctype;
|
||||
pub mod arch_aarch64_user;
|
||||
pub mod arch_x64_user;
|
||||
pub mod sys_procfs;
|
||||
pub mod sys_random;
|
||||
pub mod sys_uio;
|
||||
pub mod sys_un;
|
||||
pub mod sys_utsname;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
sys_includes = ["sys/types.h"]
|
||||
include_guard = "_SYS_RANDOM_H"
|
||||
language = "C"
|
||||
style = "Tag"
|
||||
no_includes = true
|
||||
cpp_compat = true
|
||||
|
||||
[enum]
|
||||
prefix_with_name = true
|
||||
@@ -0,0 +1,14 @@
|
||||
use core::slice;
|
||||
|
||||
use crate::platform::{Pal, Sys, types::*};
|
||||
|
||||
pub const GRND_NONBLOCK: c_uint = 1;
|
||||
pub const GRND_RANDOM: c_uint = 2;
|
||||
|
||||
#[no_mangle]
|
||||
unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_uint) -> ssize_t {
|
||||
Sys::getrandom(slice::from_raw_parts_mut(
|
||||
buf as *mut u8,
|
||||
buflen as usize
|
||||
), flags)
|
||||
}
|
||||
@@ -254,6 +254,10 @@ impl Pal for Sys {
|
||||
e(unsafe { syscall!(GETPPID) }) as pid_t
|
||||
}
|
||||
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> ssize_t {
|
||||
e(unsafe { syscall!(GETRANDOM, buf.as_mut_ptr(), buf.len(), flags) }) as ssize_t
|
||||
}
|
||||
|
||||
unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> c_int {
|
||||
e(syscall!(GETRLIMIT, resource, rlim)) as c_int
|
||||
}
|
||||
|
||||
@@ -91,6 +91,8 @@ pub trait Pal {
|
||||
|
||||
fn getppid() -> pid_t;
|
||||
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> ssize_t;
|
||||
|
||||
unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> c_int;
|
||||
|
||||
fn gettid() -> pid_t;
|
||||
|
||||
@@ -13,6 +13,7 @@ use crate::{
|
||||
errno::{EINVAL, EIO, EPERM, ERANGE},
|
||||
fcntl,
|
||||
sys_mman::MAP_ANON,
|
||||
sys_random,
|
||||
sys_resource::{rlimit, RLIM_INFINITY},
|
||||
sys_stat::stat,
|
||||
sys_statvfs::statvfs,
|
||||
@@ -555,6 +556,33 @@ impl Pal for Sys {
|
||||
e(syscall::getppid()) as pid_t
|
||||
}
|
||||
|
||||
fn getrandom(buf: &mut [u8], flags: c_uint) -> ssize_t {
|
||||
//TODO: make this a system call?
|
||||
|
||||
let path = if flags & sys_random::GRND_RANDOM != 0 {
|
||||
//TODO: /dev/random equivalent
|
||||
"rand:"
|
||||
} else {
|
||||
"rand:"
|
||||
};
|
||||
|
||||
let mut open_flags = syscall::O_RDONLY | syscall::O_CLOEXEC;
|
||||
if flags & sys_random::GRND_NONBLOCK != 0 {
|
||||
open_flags |= syscall::O_NONBLOCK;
|
||||
}
|
||||
|
||||
let fd = e(syscall::open(path, open_flags));
|
||||
if fd == !0 {
|
||||
return -1;
|
||||
}
|
||||
|
||||
let res = e(syscall::read(fd, buf)) as ssize_t;
|
||||
|
||||
let _ = syscall::close(fd);
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> c_int {
|
||||
//TODO
|
||||
if !rlim.is_null() {
|
||||
|
||||
@@ -63,3 +63,22 @@ fn clock_gettime() {
|
||||
assert_ne!(timespec.tv_nsec, -1);
|
||||
}
|
||||
}
|
||||
|
||||
//TDOO: everything else
|
||||
|
||||
#[test]
|
||||
fn getrandom() {
|
||||
use crate::header::sys_random;
|
||||
use crate::platform::types::ssize_t;
|
||||
|
||||
let mut arrays = [[0; 32]; 32];
|
||||
for i in 1..arrays.len() {
|
||||
assert_eq!(Sys::getrandom(&mut arrays[i], 0), arrays[i].len() as ssize_t);
|
||||
|
||||
for j in 0..arrays.len() {
|
||||
if i != j {
|
||||
assert_ne!(&arrays[i], &arrays[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user