github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/osutil/sharedmem_file.go (about) 1 // Copyright 2021 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 //go:build freebsd || netbsd || openbsd || darwin 5 6 package osutil 7 8 import ( 9 "fmt" 10 "io/ioutil" 11 "os" 12 ) 13 14 func CreateSharedMemFile(size int) (f *os.File, err error) { 15 f, err = ioutil.TempFile("./", "syzkaller-shm") 16 if err != nil { 17 err = fmt.Errorf("failed to create temp file: %v", err) 18 return 19 } 20 f.Close() 21 fname := f.Name() 22 f, err = os.OpenFile(f.Name(), os.O_RDWR, DefaultFilePerm) 23 if err != nil { 24 err = fmt.Errorf("failed to open shm file: %v", err) 25 os.Remove(fname) 26 } 27 return 28 } 29 30 func CloseSharedMemFile(f *os.File) error { 31 err1 := f.Close() 32 err2 := os.Remove(f.Name()) 33 switch { 34 case err1 != nil: 35 return err1 36 case err2 != nil: 37 return err2 38 default: 39 return nil 40 } 41 }