ioctl_linux.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. package unix
  5. import "unsafe"
  6. // IoctlRetInt performs an ioctl operation specified by req on a device
  7. // associated with opened file descriptor fd, and returns a non-negative
  8. // integer that is returned by the ioctl syscall.
  9. func IoctlRetInt(fd int, req uint) (int, error) {
  10. ret, _, err := Syscall(SYS_IOCTL, uintptr(fd), uintptr(req), 0)
  11. if err != 0 {
  12. return 0, err
  13. }
  14. return int(ret), nil
  15. }
  16. func IoctlGetUint32(fd int, req uint) (uint32, error) {
  17. var value uint32
  18. err := ioctlPtr(fd, req, unsafe.Pointer(&value))
  19. return value, err
  20. }
  21. func IoctlGetRTCTime(fd int) (*RTCTime, error) {
  22. var value RTCTime
  23. err := ioctlPtr(fd, RTC_RD_TIME, unsafe.Pointer(&value))
  24. return &value, err
  25. }
  26. func IoctlSetRTCTime(fd int, value *RTCTime) error {
  27. return ioctlPtr(fd, RTC_SET_TIME, unsafe.Pointer(value))
  28. }
  29. func IoctlGetRTCWkAlrm(fd int) (*RTCWkAlrm, error) {
  30. var value RTCWkAlrm
  31. err := ioctlPtr(fd, RTC_WKALM_RD, unsafe.Pointer(&value))
  32. return &value, err
  33. }
  34. func IoctlSetRTCWkAlrm(fd int, value *RTCWkAlrm) error {
  35. return ioctlPtr(fd, RTC_WKALM_SET, unsafe.Pointer(value))
  36. }
  37. // IoctlGetEthtoolDrvinfo fetches ethtool driver information for the network
  38. // device specified by ifname.
  39. func IoctlGetEthtoolDrvinfo(fd int, ifname string) (*EthtoolDrvinfo, error) {
  40. ifr, err := NewIfreq(ifname)
  41. if err != nil {
  42. return nil, err
  43. }
  44. value := EthtoolDrvinfo{Cmd: ETHTOOL_GDRVINFO}
  45. ifrd := ifr.withData(unsafe.Pointer(&value))
  46. err = ioctlIfreqData(fd, SIOCETHTOOL, &ifrd)
  47. return &value, err
  48. }
  49. // IoctlGetWatchdogInfo fetches information about a watchdog device from the
  50. // Linux watchdog API. For more information, see:
  51. // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
  52. func IoctlGetWatchdogInfo(fd int) (*WatchdogInfo, error) {
  53. var value WatchdogInfo
  54. err := ioctlPtr(fd, WDIOC_GETSUPPORT, unsafe.Pointer(&value))
  55. return &value, err
  56. }
  57. // IoctlWatchdogKeepalive issues a keepalive ioctl to a watchdog device. For
  58. // more information, see:
  59. // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
  60. func IoctlWatchdogKeepalive(fd int) error {
  61. // arg is ignored and not a pointer, so ioctl is fine instead of ioctlPtr.
  62. return ioctl(fd, WDIOC_KEEPALIVE, 0)
  63. }
  64. // IoctlFileCloneRange performs an FICLONERANGE ioctl operation to clone the
  65. // range of data conveyed in value to the file associated with the file
  66. // descriptor destFd. See the ioctl_ficlonerange(2) man page for details.
  67. func IoctlFileCloneRange(destFd int, value *FileCloneRange) error {
  68. return ioctlPtr(destFd, FICLONERANGE, unsafe.Pointer(value))
  69. }
  70. // IoctlFileClone performs an FICLONE ioctl operation to clone the entire file
  71. // associated with the file description srcFd to the file associated with the
  72. // file descriptor destFd. See the ioctl_ficlone(2) man page for details.
  73. func IoctlFileClone(destFd, srcFd int) error {
  74. return ioctl(destFd, FICLONE, uintptr(srcFd))
  75. }
  76. type FileDedupeRange struct {
  77. Src_offset uint64
  78. Src_length uint64
  79. Reserved1 uint16
  80. Reserved2 uint32
  81. Info []FileDedupeRangeInfo
  82. }
  83. type FileDedupeRangeInfo struct {
  84. Dest_fd int64
  85. Dest_offset uint64
  86. Bytes_deduped uint64
  87. Status int32
  88. Reserved uint32
  89. }
  90. // IoctlFileDedupeRange performs an FIDEDUPERANGE ioctl operation to share the
  91. // range of data conveyed in value from the file associated with the file
  92. // descriptor srcFd to the value.Info destinations. See the
  93. // ioctl_fideduperange(2) man page for details.
  94. func IoctlFileDedupeRange(srcFd int, value *FileDedupeRange) error {
  95. buf := make([]byte, SizeofRawFileDedupeRange+
  96. len(value.Info)*SizeofRawFileDedupeRangeInfo)
  97. rawrange := (*RawFileDedupeRange)(unsafe.Pointer(&buf[0]))
  98. rawrange.Src_offset = value.Src_offset
  99. rawrange.Src_length = value.Src_length
  100. rawrange.Dest_count = uint16(len(value.Info))
  101. rawrange.Reserved1 = value.Reserved1
  102. rawrange.Reserved2 = value.Reserved2
  103. for i := range value.Info {
  104. rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
  105. uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
  106. uintptr(i*SizeofRawFileDedupeRangeInfo)))
  107. rawinfo.Dest_fd = value.Info[i].Dest_fd
  108. rawinfo.Dest_offset = value.Info[i].Dest_offset
  109. rawinfo.Bytes_deduped = value.Info[i].Bytes_deduped
  110. rawinfo.Status = value.Info[i].Status
  111. rawinfo.Reserved = value.Info[i].Reserved
  112. }
  113. err := ioctlPtr(srcFd, FIDEDUPERANGE, unsafe.Pointer(&buf[0]))
  114. // Output
  115. for i := range value.Info {
  116. rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
  117. uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
  118. uintptr(i*SizeofRawFileDedupeRangeInfo)))
  119. value.Info[i].Dest_fd = rawinfo.Dest_fd
  120. value.Info[i].Dest_offset = rawinfo.Dest_offset
  121. value.Info[i].Bytes_deduped = rawinfo.Bytes_deduped
  122. value.Info[i].Status = rawinfo.Status
  123. value.Info[i].Reserved = rawinfo.Reserved
  124. }
  125. return err
  126. }
  127. func IoctlHIDGetDesc(fd int, value *HIDRawReportDescriptor) error {
  128. return ioctlPtr(fd, HIDIOCGRDESC, unsafe.Pointer(value))
  129. }
  130. func IoctlHIDGetRawInfo(fd int) (*HIDRawDevInfo, error) {
  131. var value HIDRawDevInfo
  132. err := ioctlPtr(fd, HIDIOCGRAWINFO, unsafe.Pointer(&value))
  133. return &value, err
  134. }
  135. func IoctlHIDGetRawName(fd int) (string, error) {
  136. var value [_HIDIOCGRAWNAME_LEN]byte
  137. err := ioctlPtr(fd, _HIDIOCGRAWNAME, unsafe.Pointer(&value[0]))
  138. return ByteSliceToString(value[:]), err
  139. }
  140. func IoctlHIDGetRawPhys(fd int) (string, error) {
  141. var value [_HIDIOCGRAWPHYS_LEN]byte
  142. err := ioctlPtr(fd, _HIDIOCGRAWPHYS, unsafe.Pointer(&value[0]))
  143. return ByteSliceToString(value[:]), err
  144. }
  145. func IoctlHIDGetRawUniq(fd int) (string, error) {
  146. var value [_HIDIOCGRAWUNIQ_LEN]byte
  147. err := ioctlPtr(fd, _HIDIOCGRAWUNIQ, unsafe.Pointer(&value[0]))
  148. return ByteSliceToString(value[:]), err
  149. }
  150. // IoctlIfreq performs an ioctl using an Ifreq structure for input and/or
  151. // output. See the netdevice(7) man page for details.
  152. func IoctlIfreq(fd int, req uint, value *Ifreq) error {
  153. // It is possible we will add more fields to *Ifreq itself later to prevent
  154. // misuse, so pass the raw *ifreq directly.
  155. return ioctlPtr(fd, req, unsafe.Pointer(&value.raw))
  156. }
  157. // TODO(mdlayher): export if and when IfreqData is exported.
  158. // ioctlIfreqData performs an ioctl using an ifreqData structure for input
  159. // and/or output. See the netdevice(7) man page for details.
  160. func ioctlIfreqData(fd int, req uint, value *ifreqData) error {
  161. // The memory layout of IfreqData (type-safe) and ifreq (not type-safe) are
  162. // identical so pass *IfreqData directly.
  163. return ioctlPtr(fd, req, unsafe.Pointer(value))
  164. }
  165. // IoctlKCMClone attaches a new file descriptor to a multiplexor by cloning an
  166. // existing KCM socket, returning a structure containing the file descriptor of
  167. // the new socket.
  168. func IoctlKCMClone(fd int) (*KCMClone, error) {
  169. var info KCMClone
  170. if err := ioctlPtr(fd, SIOCKCMCLONE, unsafe.Pointer(&info)); err != nil {
  171. return nil, err
  172. }
  173. return &info, nil
  174. }
  175. // IoctlKCMAttach attaches a TCP socket and associated BPF program file
  176. // descriptor to a multiplexor.
  177. func IoctlKCMAttach(fd int, info KCMAttach) error {
  178. return ioctlPtr(fd, SIOCKCMATTACH, unsafe.Pointer(&info))
  179. }
  180. // IoctlKCMUnattach unattaches a TCP socket file descriptor from a multiplexor.
  181. func IoctlKCMUnattach(fd int, info KCMUnattach) error {
  182. return ioctlPtr(fd, SIOCKCMUNATTACH, unsafe.Pointer(&info))
  183. }
  184. // IoctlLoopGetStatus64 gets the status of the loop device associated with the
  185. // file descriptor fd using the LOOP_GET_STATUS64 operation.
  186. func IoctlLoopGetStatus64(fd int) (*LoopInfo64, error) {
  187. var value LoopInfo64
  188. if err := ioctlPtr(fd, LOOP_GET_STATUS64, unsafe.Pointer(&value)); err != nil {
  189. return nil, err
  190. }
  191. return &value, nil
  192. }
  193. // IoctlLoopSetStatus64 sets the status of the loop device associated with the
  194. // file descriptor fd using the LOOP_SET_STATUS64 operation.
  195. func IoctlLoopSetStatus64(fd int, value *LoopInfo64) error {
  196. return ioctlPtr(fd, LOOP_SET_STATUS64, unsafe.Pointer(value))
  197. }
  198. // IoctlLoopConfigure configures all loop device parameters in a single step
  199. func IoctlLoopConfigure(fd int, value *LoopConfig) error {
  200. return ioctlPtr(fd, LOOP_CONFIGURE, unsafe.Pointer(value))
  201. }