Source file src/internal/syscall/unix/getrandom_solaris.go

     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.
     4  
     5  package unix
     6  
     7  import (
     8  	"sync/atomic"
     9  	"syscall"
    10  	"unsafe"
    11  )
    12  
    13  //go:cgo_import_dynamic libc_getrandom getrandom "libc.so"
    14  
    15  //go:linkname procGetrandom libc_getrandom
    16  
    17  var procGetrandom uintptr
    18  
    19  var getrandomUnsupported atomic.Bool
    20  
    21  // GetRandomFlag is a flag supported by the getrandom system call.
    22  type GetRandomFlag uintptr
    23  
    24  const (
    25  	// GRND_NONBLOCK means return EAGAIN rather than blocking.
    26  	GRND_NONBLOCK GetRandomFlag = 0x0001
    27  
    28  	// GRND_RANDOM means use the /dev/random pool instead of /dev/urandom.
    29  	GRND_RANDOM GetRandomFlag = 0x0002
    30  )
    31  
    32  // GetRandom calls the getrandom system call.
    33  func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
    34  	if len(p) == 0 {
    35  		return 0, nil
    36  	}
    37  	if getrandomUnsupported.Load() {
    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  			getrandomUnsupported.Store(true)
    49  		}
    50  		return 0, errno
    51  	}
    52  	return int(r1), nil
    53  }
    54  

View as plain text