syscall_illumos.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // illumos system calls not present on Solaris.
  5. //go:build amd64 && illumos
  6. // +build amd64,illumos
  7. package unix
  8. import (
  9. "fmt"
  10. "runtime"
  11. "unsafe"
  12. )
  13. func bytes2iovec(bs [][]byte) []Iovec {
  14. iovecs := make([]Iovec, len(bs))
  15. for i, b := range bs {
  16. iovecs[i].SetLen(len(b))
  17. if len(b) > 0 {
  18. // somehow Iovec.Base on illumos is (*int8), not (*byte)
  19. iovecs[i].Base = (*int8)(unsafe.Pointer(&b[0]))
  20. } else {
  21. iovecs[i].Base = (*int8)(unsafe.Pointer(&_zero))
  22. }
  23. }
  24. return iovecs
  25. }
  26. //sys readv(fd int, iovs []Iovec) (n int, err error)
  27. func Readv(fd int, iovs [][]byte) (n int, err error) {
  28. iovecs := bytes2iovec(iovs)
  29. n, err = readv(fd, iovecs)
  30. return n, err
  31. }
  32. //sys preadv(fd int, iovs []Iovec, off int64) (n int, err error)
  33. func Preadv(fd int, iovs [][]byte, off int64) (n int, err error) {
  34. iovecs := bytes2iovec(iovs)
  35. n, err = preadv(fd, iovecs, off)
  36. return n, err
  37. }
  38. //sys writev(fd int, iovs []Iovec) (n int, err error)
  39. func Writev(fd int, iovs [][]byte) (n int, err error) {
  40. iovecs := bytes2iovec(iovs)
  41. n, err = writev(fd, iovecs)
  42. return n, err
  43. }
  44. //sys pwritev(fd int, iovs []Iovec, off int64) (n int, err error)
  45. func Pwritev(fd int, iovs [][]byte, off int64) (n int, err error) {
  46. iovecs := bytes2iovec(iovs)
  47. n, err = pwritev(fd, iovecs, off)
  48. return n, err
  49. }
  50. //sys accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) = libsocket.accept4
  51. func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) {
  52. var rsa RawSockaddrAny
  53. var len _Socklen = SizeofSockaddrAny
  54. nfd, err = accept4(fd, &rsa, &len, flags)
  55. if err != nil {
  56. return
  57. }
  58. if len > SizeofSockaddrAny {
  59. panic("RawSockaddrAny too small")
  60. }
  61. sa, err = anyToSockaddr(fd, &rsa)
  62. if err != nil {
  63. Close(nfd)
  64. nfd = 0
  65. }
  66. return
  67. }
  68. //sys putmsg(fd int, clptr *strbuf, dataptr *strbuf, flags int) (err error)
  69. func Putmsg(fd int, cl []byte, data []byte, flags int) (err error) {
  70. var clp, datap *strbuf
  71. if len(cl) > 0 {
  72. clp = &strbuf{
  73. Len: int32(len(cl)),
  74. Buf: (*int8)(unsafe.Pointer(&cl[0])),
  75. }
  76. }
  77. if len(data) > 0 {
  78. datap = &strbuf{
  79. Len: int32(len(data)),
  80. Buf: (*int8)(unsafe.Pointer(&data[0])),
  81. }
  82. }
  83. return putmsg(fd, clp, datap, flags)
  84. }
  85. //sys getmsg(fd int, clptr *strbuf, dataptr *strbuf, flags *int) (err error)
  86. func Getmsg(fd int, cl []byte, data []byte) (retCl []byte, retData []byte, flags int, err error) {
  87. var clp, datap *strbuf
  88. if len(cl) > 0 {
  89. clp = &strbuf{
  90. Maxlen: int32(len(cl)),
  91. Buf: (*int8)(unsafe.Pointer(&cl[0])),
  92. }
  93. }
  94. if len(data) > 0 {
  95. datap = &strbuf{
  96. Maxlen: int32(len(data)),
  97. Buf: (*int8)(unsafe.Pointer(&data[0])),
  98. }
  99. }
  100. if err = getmsg(fd, clp, datap, &flags); err != nil {
  101. return nil, nil, 0, err
  102. }
  103. if len(cl) > 0 {
  104. retCl = cl[:clp.Len]
  105. }
  106. if len(data) > 0 {
  107. retData = data[:datap.Len]
  108. }
  109. return retCl, retData, flags, nil
  110. }
  111. func IoctlSetIntRetInt(fd int, req uint, arg int) (int, error) {
  112. return ioctlRet(fd, req, uintptr(arg))
  113. }
  114. func IoctlSetString(fd int, req uint, val string) error {
  115. bs := make([]byte, len(val)+1)
  116. copy(bs[:len(bs)-1], val)
  117. err := ioctl(fd, req, uintptr(unsafe.Pointer(&bs[0])))
  118. runtime.KeepAlive(&bs[0])
  119. return err
  120. }
  121. // Lifreq Helpers
  122. func (l *Lifreq) SetName(name string) error {
  123. if len(name) >= len(l.Name) {
  124. return fmt.Errorf("name cannot be more than %d characters", len(l.Name)-1)
  125. }
  126. for i := range name {
  127. l.Name[i] = int8(name[i])
  128. }
  129. return nil
  130. }
  131. func (l *Lifreq) SetLifruInt(d int) {
  132. *(*int)(unsafe.Pointer(&l.Lifru[0])) = d
  133. }
  134. func (l *Lifreq) GetLifruInt() int {
  135. return *(*int)(unsafe.Pointer(&l.Lifru[0]))
  136. }
  137. func (l *Lifreq) SetLifruUint(d uint) {
  138. *(*uint)(unsafe.Pointer(&l.Lifru[0])) = d
  139. }
  140. func (l *Lifreq) GetLifruUint() uint {
  141. return *(*uint)(unsafe.Pointer(&l.Lifru[0]))
  142. }
  143. func IoctlLifreq(fd int, req uint, l *Lifreq) error {
  144. return ioctl(fd, req, uintptr(unsafe.Pointer(l)))
  145. }
  146. // Strioctl Helpers
  147. func (s *Strioctl) SetInt(i int) {
  148. s.Len = int32(unsafe.Sizeof(i))
  149. s.Dp = (*int8)(unsafe.Pointer(&i))
  150. }
  151. func IoctlSetStrioctlRetInt(fd int, req uint, s *Strioctl) (int, error) {
  152. return ioctlRet(fd, req, uintptr(unsafe.Pointer(s)))
  153. }