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