2017-10-07 00:16:47 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2015-08-28 15:33:40 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
package unix
|
2015-08-28 15:33:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
2017-04-10 11:32:00 +00:00
|
|
|
"syscall"
|
2015-08-28 15:33:40 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var randomUnsupported int32 // atomic
|
|
|
|
|
|
|
|
// GetRandomFlag is a flag supported by the getrandom system call.
|
|
|
|
type GetRandomFlag uintptr
|
|
|
|
|
|
|
|
const (
|
|
|
|
// GRND_NONBLOCK means return EAGAIN rather than blocking.
|
|
|
|
GRND_NONBLOCK GetRandomFlag = 0x0001
|
|
|
|
|
|
|
|
// GRND_RANDOM means use the /dev/random pool instead of /dev/urandom.
|
|
|
|
GRND_RANDOM GetRandomFlag = 0x0002
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetRandom calls the Linux getrandom system call.
|
|
|
|
// See https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c6e9d6f38894798696f23c8084ca7edbf16ee895
|
|
|
|
func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
|
|
|
|
if randomTrap == 0 {
|
2017-04-10 11:32:00 +00:00
|
|
|
return 0, syscall.ENOSYS
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
if len(p) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if atomic.LoadInt32(&randomUnsupported) != 0 {
|
2017-04-10 11:32:00 +00:00
|
|
|
return 0, syscall.ENOSYS
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
r1, _, errno := syscall.Syscall(randomTrap,
|
2015-08-28 15:33:40 +00:00
|
|
|
uintptr(unsafe.Pointer(&p[0])),
|
|
|
|
uintptr(len(p)),
|
|
|
|
uintptr(flags))
|
|
|
|
if errno != 0 {
|
2017-04-10 11:32:00 +00:00
|
|
|
if errno == syscall.ENOSYS {
|
2015-08-28 15:33:40 +00:00
|
|
|
atomic.StoreInt32(&randomUnsupported, 1)
|
|
|
|
}
|
|
|
|
return 0, errno
|
|
|
|
}
|
|
|
|
return int(r1), nil
|
|
|
|
}
|