1
2
3
4
5
6
7 package unix
8
9 import (
10 "sync/atomic"
11 "syscall"
12 "unsafe"
13 )
14
15 var getrandomUnsupported int32
16
17
18 type GetRandomFlag uintptr
19
20
21 func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
22 if len(p) == 0 {
23 return 0, nil
24 }
25 if atomic.LoadInt32(&getrandomUnsupported) != 0 {
26 return 0, syscall.ENOSYS
27 }
28 r1, _, errno := syscall.Syscall(getrandomTrap,
29 uintptr(unsafe.Pointer(&p[0])),
30 uintptr(len(p)),
31 uintptr(flags))
32 if errno != 0 {
33 if errno == syscall.ENOSYS {
34 atomic.StoreInt32(&getrandomUnsupported, 1)
35 }
36 return 0, errno
37 }
38 return int(r1), nil
39 }
40
View as plain text