shm_syscall.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. //go:build darwin || freebsd
  3. package shm
  4. import (
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/fs"
  9. "os"
  10. "strings"
  11. "unsafe"
  12. "kitty/tools/utils"
  13. "golang.org/x/sys/unix"
  14. )
  15. var _ = fmt.Print
  16. // ByteSliceFromString makes a zero terminated byte slice from the string
  17. func ByteSliceFromString(s string) []byte {
  18. a := make([]byte, len(s)+1)
  19. copy(a, s)
  20. return a
  21. }
  22. func BytePtrFromString(s string) *byte {
  23. a := ByteSliceFromString(s)
  24. return &a[0]
  25. }
  26. func shm_unlink(name string) (err error) {
  27. bname := BytePtrFromString(name)
  28. for {
  29. _, _, errno := unix.Syscall(unix.SYS_SHM_UNLINK, uintptr(unsafe.Pointer(bname)), 0, 0)
  30. if errno != unix.EINTR {
  31. if errno != 0 {
  32. err = fmt.Errorf("shm_unlink() failed with error: %w", errno)
  33. }
  34. break
  35. }
  36. }
  37. return
  38. }
  39. func shm_open(name string, flags, perm int) (ans *os.File, err error) {
  40. bname := BytePtrFromString(name)
  41. var fd uintptr
  42. var errno unix.Errno
  43. for {
  44. fd, _, errno = unix.Syscall(unix.SYS_SHM_OPEN, uintptr(unsafe.Pointer(bname)), uintptr(flags), uintptr(perm))
  45. if errno != unix.EINTR {
  46. if errno != 0 {
  47. err = fmt.Errorf("shm_open() failed with error: %w", errno)
  48. }
  49. break
  50. }
  51. }
  52. if err == nil {
  53. ans = os.NewFile(fd, name)
  54. }
  55. return
  56. }
  57. type syscall_based_mmap struct {
  58. f *os.File
  59. pos int64
  60. region []byte
  61. unlinked bool
  62. }
  63. func syscall_mmap(f *os.File, size uint64, access AccessFlags, truncate bool) (MMap, error) {
  64. if truncate {
  65. err := truncate_or_unlink(f, size, shm_unlink)
  66. if err != nil {
  67. return nil, fmt.Errorf("truncate failed with error: %w", err)
  68. }
  69. }
  70. region, err := mmap(int(size), access, int(f.Fd()), 0)
  71. if err != nil {
  72. _ = f.Close()
  73. _ = shm_unlink(f.Name())
  74. return nil, fmt.Errorf("mmap failed with error: %w", err)
  75. }
  76. return &syscall_based_mmap{f: f, region: region}, nil
  77. }
  78. func (self *syscall_based_mmap) Name() string {
  79. return self.f.Name()
  80. }
  81. func (self *syscall_based_mmap) Stat() (fs.FileInfo, error) {
  82. return self.f.Stat()
  83. }
  84. func (self *syscall_based_mmap) Flush() error {
  85. return unix.Msync(self.region, unix.MS_SYNC)
  86. }
  87. func (self *syscall_based_mmap) Slice() []byte {
  88. return self.region
  89. }
  90. func (self *syscall_based_mmap) Close() (err error) {
  91. if self.region != nil {
  92. self.f.Close()
  93. munmap(self.region)
  94. self.region = nil
  95. }
  96. return
  97. }
  98. func (self *syscall_based_mmap) Unlink() (err error) {
  99. if self.unlinked {
  100. return nil
  101. }
  102. self.unlinked = true
  103. return shm_unlink(self.Name())
  104. }
  105. func (self *syscall_based_mmap) Seek(offset int64, whence int) (ret int64, err error) {
  106. switch whence {
  107. case io.SeekStart:
  108. self.pos = offset
  109. case io.SeekEnd:
  110. self.pos = int64(len(self.region)) + offset
  111. case io.SeekCurrent:
  112. self.pos += offset
  113. }
  114. return self.pos, nil
  115. }
  116. func (self *syscall_based_mmap) Read(b []byte) (n int, err error) {
  117. return Read(self, b)
  118. }
  119. func (self *syscall_based_mmap) Write(b []byte) (n int, err error) {
  120. return Write(self, b)
  121. }
  122. func (self *syscall_based_mmap) IsFileSystemBacked() bool { return false }
  123. func (self *syscall_based_mmap) FileSystemName() string { return "" }
  124. func create_temp(pattern string, size uint64) (ans MMap, err error) {
  125. var prefix, suffix string
  126. prefix, suffix, err = prefix_and_suffix(pattern)
  127. if err != nil {
  128. return
  129. }
  130. if SHM_REQUIRED_PREFIX != "" && !strings.HasPrefix(pattern, SHM_REQUIRED_PREFIX) {
  131. // FreeBSD requires name to start with /
  132. prefix = SHM_REQUIRED_PREFIX + prefix
  133. }
  134. var f *os.File
  135. try := 0
  136. for {
  137. name := prefix + utils.RandomFilename() + suffix
  138. if len(name) > SHM_NAME_MAX {
  139. return nil, ErrPatternTooLong
  140. }
  141. f, err = shm_open(name, os.O_EXCL|os.O_CREATE|os.O_RDWR, 0600)
  142. if err != nil && (errors.Is(err, fs.ErrExist) || errors.Unwrap(err) == unix.EEXIST) {
  143. try += 1
  144. if try > 10000 {
  145. return nil, &os.PathError{Op: "createtemp", Path: prefix + "*" + suffix, Err: fs.ErrExist}
  146. }
  147. continue
  148. }
  149. break
  150. }
  151. if err != nil {
  152. return nil, err
  153. }
  154. return syscall_mmap(f, size, WRITE, true)
  155. }
  156. func Open(name string, size uint64) (MMap, error) {
  157. ans, err := shm_open(name, os.O_RDONLY, 0)
  158. if err != nil {
  159. return nil, err
  160. }
  161. if size == 0 {
  162. s, err := ans.Stat()
  163. if err != nil {
  164. ans.Close()
  165. return nil, fmt.Errorf("Failed to stat SHM file with error: %w", err)
  166. }
  167. size = uint64(s.Size())
  168. }
  169. return syscall_mmap(ans, size, READ, false)
  170. }