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