@@ -14,21 +14,30 @@ use std::io;
14
14
use core:: ptr;
15
15
use core:: num:: NonZeroU32 ;
16
16
17
+ fn kern_arnd ( buf : & mut [ u8 ] ) -> Result < usize , Error > {
18
+ static MIB : [ libc:: c_int ; 2 ] = [ libc:: CTL_KERN , libc:: KERN_ARND ] ;
19
+ let mut len = buf. len ( ) ;
20
+ let ret = unsafe {
21
+ libc:: sysctl (
22
+ MIB . as_ptr ( ) ,
23
+ MIB . len ( ) as libc:: c_uint ,
24
+ buf. as_mut_ptr ( ) as * mut _ ,
25
+ & mut len,
26
+ ptr:: null ( ) ,
27
+ 0 ,
28
+ )
29
+ } ;
30
+ if ret == -1 {
31
+ error ! ( "freebsd: kern.arandom syscall failed" ) ;
32
+ return Err ( io:: Error :: last_os_error ( ) . into ( ) ) ;
33
+ }
34
+ Ok ( len)
35
+ }
36
+
17
37
pub fn getrandom_inner ( dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
18
- let mib = [ libc:: CTL_KERN , libc:: KERN_ARND ] ;
19
- // kern.arandom permits a maximum buffer size of 256 bytes
20
- for chunk in dest. chunks_mut ( 256 ) {
21
- let mut len = chunk. len ( ) ;
22
- let ret = unsafe {
23
- libc:: sysctl (
24
- mib. as_ptr ( ) , mib. len ( ) as libc:: c_uint ,
25
- chunk. as_mut_ptr ( ) as * mut _ , & mut len, ptr:: null ( ) , 0 ,
26
- )
27
- } ;
28
- if ret == -1 || len != chunk. len ( ) {
29
- error ! ( "freebsd: kern.arandom syscall failed" ) ;
30
- return Err ( io:: Error :: last_os_error ( ) . into ( ) ) ;
31
- }
38
+ let mut start = 0 ;
39
+ while start < dest. len ( ) {
40
+ start += kern_arnd ( & mut dest[ start..] ) ?;
32
41
}
33
42
Ok ( ( ) )
34
43
}
0 commit comments