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