1 // Copyright 2021 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.
13 func libc_getrandom(*byte, uintptr, uint32) uintptr
15 var getrandomUnsupported
int32 // atomic
17 // GetRandomFlag is a flag supported by the getrandom system call.
18 type GetRandomFlag
uintptr
21 // GRND_NONBLOCK means return EAGAIN rather than blocking.
22 GRND_NONBLOCK GetRandomFlag
= 0x0001
24 // GRND_RANDOM means use the /dev/random pool instead of /dev/urandom.
25 GRND_RANDOM GetRandomFlag
= 0x0002
28 // GetRandom calls the getrandom system call.
29 func GetRandom(p
[]byte, flags GetRandomFlag
) (n
int, err error
) {
33 if atomic
.LoadInt32(&getrandomUnsupported
) != 0 {
34 return 0, syscall
.ENOSYS
36 syscall
.Entersyscall()
37 r1
:= libc_getrandom(&p
[0], uintptr(len(p
)), uint32(flags
))
40 errno
:= syscall
.GetErrno()
41 if errno
== syscall
.ENOSYS
{
42 atomic
.StoreInt32(&getrandomUnsupported
, 1)