ioctl_linux.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 (
  6. "runtime"
  7. "unsafe"
  8. )
  9. // IoctlRetInt performs an ioctl operation specified by req on a device
  10. // associated with opened file descriptor fd, and returns a non-negative
  11. // integer that is returned by the ioctl syscall.
  12. func IoctlRetInt(fd int, req uint) (int, error) {
  13. ret, _, err := Syscall(SYS_IOCTL, uintptr(fd), uintptr(req), 0)
  14. if err != 0 {
  15. return 0, err
  16. }
  17. return int(ret), nil
  18. }
  19. func IoctlGetUint32(fd int, req uint) (uint32, error) {
  20. var value uint32
  21. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  22. return value, err
  23. }
  24. func IoctlGetRTCTime(fd int) (*RTCTime, error) {
  25. var value RTCTime
  26. err := ioctl(fd, RTC_RD_TIME, uintptr(unsafe.Pointer(&value)))
  27. return &value, err
  28. }
  29. func IoctlSetRTCTime(fd int, value *RTCTime) error {
  30. err := ioctl(fd, RTC_SET_TIME, uintptr(unsafe.Pointer(value)))
  31. runtime.KeepAlive(value)
  32. return err
  33. }
  34. func IoctlGetRTCWkAlrm(fd int) (*RTCWkAlrm, error) {
  35. var value RTCWkAlrm
  36. err := ioctl(fd, RTC_WKALM_RD, uintptr(unsafe.Pointer(&value)))
  37. return &value, err
  38. }
  39. func IoctlSetRTCWkAlrm(fd int, value *RTCWkAlrm) error {
  40. err := ioctl(fd, RTC_WKALM_SET, uintptr(unsafe.Pointer(value)))
  41. runtime.KeepAlive(value)
  42. return err
  43. }
  44. type ifreqEthtool struct {
  45. name [IFNAMSIZ]byte
  46. data unsafe.Pointer
  47. }
  48. // IoctlGetEthtoolDrvinfo fetches ethtool driver information for the network
  49. // device specified by ifname.
  50. func IoctlGetEthtoolDrvinfo(fd int, ifname string) (*EthtoolDrvinfo, error) {
  51. // Leave room for terminating NULL byte.
  52. if len(ifname) >= IFNAMSIZ {
  53. return nil, EINVAL
  54. }
  55. value := EthtoolDrvinfo{
  56. Cmd: ETHTOOL_GDRVINFO,
  57. }
  58. ifreq := ifreqEthtool{
  59. data: unsafe.Pointer(&value),
  60. }
  61. copy(ifreq.name[:], ifname)
  62. err := ioctl(fd, SIOCETHTOOL, uintptr(unsafe.Pointer(&ifreq)))
  63. runtime.KeepAlive(ifreq)
  64. return &value, err
  65. }
  66. // IoctlGetWatchdogInfo fetches information about a watchdog device from the
  67. // Linux watchdog API. For more information, see:
  68. // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
  69. func IoctlGetWatchdogInfo(fd int) (*WatchdogInfo, error) {
  70. var value WatchdogInfo
  71. err := ioctl(fd, WDIOC_GETSUPPORT, uintptr(unsafe.Pointer(&value)))
  72. return &value, err
  73. }
  74. // IoctlWatchdogKeepalive issues a keepalive ioctl to a watchdog device. For
  75. // more information, see:
  76. // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
  77. func IoctlWatchdogKeepalive(fd int) error {
  78. return ioctl(fd, WDIOC_KEEPALIVE, 0)
  79. }
  80. // IoctlFileCloneRange performs an FICLONERANGE ioctl operation to clone the
  81. // range of data conveyed in value to the file associated with the file
  82. // descriptor destFd. See the ioctl_ficlonerange(2) man page for details.
  83. func IoctlFileCloneRange(destFd int, value *FileCloneRange) error {
  84. err := ioctl(destFd, FICLONERANGE, uintptr(unsafe.Pointer(value)))
  85. runtime.KeepAlive(value)
  86. return err
  87. }
  88. // IoctlFileClone performs an FICLONE ioctl operation to clone the entire file
  89. // associated with the file description srcFd to the file associated with the
  90. // file descriptor destFd. See the ioctl_ficlone(2) man page for details.
  91. func IoctlFileClone(destFd, srcFd int) error {
  92. return ioctl(destFd, FICLONE, uintptr(srcFd))
  93. }
  94. type FileDedupeRange struct {
  95. Src_offset uint64
  96. Src_length uint64
  97. Reserved1 uint16
  98. Reserved2 uint32
  99. Info []FileDedupeRangeInfo
  100. }
  101. type FileDedupeRangeInfo struct {
  102. Dest_fd int64
  103. Dest_offset uint64
  104. Bytes_deduped uint64
  105. Status int32
  106. Reserved uint32
  107. }
  108. // IoctlFileDedupeRange performs an FIDEDUPERANGE ioctl operation to share the
  109. // range of data conveyed in value from the file associated with the file
  110. // descriptor srcFd to the value.Info destinations. See the
  111. // ioctl_fideduperange(2) man page for details.
  112. func IoctlFileDedupeRange(srcFd int, value *FileDedupeRange) error {
  113. buf := make([]byte, SizeofRawFileDedupeRange+
  114. len(value.Info)*SizeofRawFileDedupeRangeInfo)
  115. rawrange := (*RawFileDedupeRange)(unsafe.Pointer(&buf[0]))
  116. rawrange.Src_offset = value.Src_offset
  117. rawrange.Src_length = value.Src_length
  118. rawrange.Dest_count = uint16(len(value.Info))
  119. rawrange.Reserved1 = value.Reserved1
  120. rawrange.Reserved2 = value.Reserved2
  121. for i := range value.Info {
  122. rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
  123. uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
  124. uintptr(i*SizeofRawFileDedupeRangeInfo)))
  125. rawinfo.Dest_fd = value.Info[i].Dest_fd
  126. rawinfo.Dest_offset = value.Info[i].Dest_offset
  127. rawinfo.Bytes_deduped = value.Info[i].Bytes_deduped
  128. rawinfo.Status = value.Info[i].Status
  129. rawinfo.Reserved = value.Info[i].Reserved
  130. }
  131. err := ioctl(srcFd, FIDEDUPERANGE, uintptr(unsafe.Pointer(&buf[0])))
  132. // Output
  133. for i := range value.Info {
  134. rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
  135. uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
  136. uintptr(i*SizeofRawFileDedupeRangeInfo)))
  137. value.Info[i].Dest_fd = rawinfo.Dest_fd
  138. value.Info[i].Dest_offset = rawinfo.Dest_offset
  139. value.Info[i].Bytes_deduped = rawinfo.Bytes_deduped
  140. value.Info[i].Status = rawinfo.Status
  141. value.Info[i].Reserved = rawinfo.Reserved
  142. }
  143. return err
  144. }
  145. func IoctlHIDGetDesc(fd int, value *HIDRawReportDescriptor) error {
  146. err := ioctl(fd, HIDIOCGRDESC, uintptr(unsafe.Pointer(value)))
  147. runtime.KeepAlive(value)
  148. return err
  149. }
  150. func IoctlHIDGetRawInfo(fd int) (*HIDRawDevInfo, error) {
  151. var value HIDRawDevInfo
  152. err := ioctl(fd, HIDIOCGRAWINFO, uintptr(unsafe.Pointer(&value)))
  153. return &value, err
  154. }
  155. func IoctlHIDGetRawName(fd int) (string, error) {
  156. var value [_HIDIOCGRAWNAME_LEN]byte
  157. err := ioctl(fd, _HIDIOCGRAWNAME, uintptr(unsafe.Pointer(&value[0])))
  158. return ByteSliceToString(value[:]), err
  159. }
  160. func IoctlHIDGetRawPhys(fd int) (string, error) {
  161. var value [_HIDIOCGRAWPHYS_LEN]byte
  162. err := ioctl(fd, _HIDIOCGRAWPHYS, uintptr(unsafe.Pointer(&value[0])))
  163. return ByteSliceToString(value[:]), err
  164. }
  165. func IoctlHIDGetRawUniq(fd int) (string, error) {
  166. var value [_HIDIOCGRAWUNIQ_LEN]byte
  167. err := ioctl(fd, _HIDIOCGRAWUNIQ, uintptr(unsafe.Pointer(&value[0])))
  168. return ByteSliceToString(value[:]), err
  169. }