Debian Codegraph @

libgo/go/internal/syscall/unix/getrandom.go

raw
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 //go:build dragonfly || freebsd || linux
6
7 package unix
8
9 import (
10 "sync/atomic"
11 "syscall"
12 "unsafe"
13 )
14
15 var getrandomUnsupported int32 // atomic
16
17 // GetRandomFlag is a flag supported by the getrandom system call.
18 type GetRandomFlag uintptr
19
20 // GetRandom calls the getrandom system call.
21 func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
22 if len(p) == 0 {
23 return 0, nil
24 }
25 if atomic.LoadInt32(&getrandomUnsupported) != 0 {
26 return 0, syscall.ENOSYS
27 }
28 r1, _, errno := syscall.Syscall(getrandomTrap,
29 uintptr(unsafe.Pointer(&p[0])),
30 uintptr(len(p)),
31 uintptr(flags))
32 if errno != 0 {
33 if errno == syscall.ENOSYS {
34 atomic.StoreInt32(&getrandomUnsupported, 1)
35 }
36 return 0, errno
37 }
38 return int(r1), nil
39 }