| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package unix |
| 6 |
|
| 7 |
import ( |
| 8 |
"syscall" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 12 |
const ( |
| 13 |
_CTL_KERN = 1 |
| 14 |
|
| 15 |
_KERN_ARND = 81 |
| 16 |
) |
| 17 |
|
| 18 |
func Arandom(p []byte) error { |
| 19 |
mib := [2]uint32{_CTL_KERN, _KERN_ARND} |
| 20 |
n := uintptr(len(p)) |
| 21 |
_, _, errno := syscall.Syscall6( |
| 22 |
syscall.SYS___SYSCTL, |
| 23 |
uintptr(unsafe.Pointer(&mib[0])), |
| 24 |
uintptr(len(mib)), |
| 25 |
uintptr(unsafe.Pointer(&p[0])), // olddata |
| 26 |
uintptr(unsafe.Pointer(&n)), // &oldlen |
| 27 |
uintptr(unsafe.Pointer(nil)), // newdata |
| 28 |
0) // newlen |
| 29 |
if errno != 0 { |
| 30 |
return syscall.Errno(errno) |
| 31 |
} |
| 32 |
if n != uintptr(len(p)) { |
| 33 |
return syscall.EINVAL |
| 34 |
} |
| 35 |
return nil |
| 36 |
} |