github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/crypto/rand/rand_linux.go (about) 1 // Copyright 2014 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 rand 6 7 import ( 8 "internal/syscall/unix" 9 ) 10 11 func init() { 12 altGetRandom = getRandomLinux 13 } 14 15 // If the kernel is too old (before 3.17) to support the getrandom syscall(), 16 // unix.GetRandom will immediately return ENOSYS and we will then fall back to 17 // reading from /dev/urandom in rand_unix.go. unix.GetRandom caches the ENOSYS 18 // result so we only suffer the syscall overhead once in this case. 19 // If the kernel supports the getrandom() syscall, unix.GetRandom will block 20 // until the kernel has sufficient randomness (as we don't use GRND_NONBLOCK). 21 // In this case, unix.GetRandom will not return an error. 22 func getRandomLinux(p []byte) (ok bool) { 23 n, err := unix.GetRandom(p, 0) 24 return n == len(p) && err == nil 25 }