ioctl_linux.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. // IoctlGetEthtoolTsInfo fetches ethtool timestamping and PHC
  50. // association for the network device specified by ifname.
  51. func IoctlGetEthtoolTsInfo(fd int, ifname string) (*EthtoolTsInfo, error) {
  52. ifr, err := NewIfreq(ifname)
  53. if err != nil {
  54. return nil, err
  55. }
  56. value := EthtoolTsInfo{Cmd: ETHTOOL_GET_TS_INFO}
  57. ifrd := ifr.withData(unsafe.Pointer(&value))
  58. err = ioctlIfreqData(fd, SIOCETHTOOL, &ifrd)
  59. return &value, err
  60. }
  61. // IoctlGetHwTstamp retrieves the hardware timestamping configuration
  62. // for the network device specified by ifname.
  63. func IoctlGetHwTstamp(fd int, ifname string) (*HwTstampConfig, error) {
  64. ifr, err := NewIfreq(ifname)
  65. if err != nil {
  66. return nil, err
  67. }
  68. value := HwTstampConfig{}
  69. ifrd := ifr.withData(unsafe.Pointer(&value))
  70. err = ioctlIfreqData(fd, SIOCGHWTSTAMP, &ifrd)
  71. return &value, err
  72. }
  73. // IoctlSetHwTstamp updates the hardware timestamping configuration for
  74. // the network device specified by ifname.
  75. func IoctlSetHwTstamp(fd int, ifname string, cfg *HwTstampConfig) error {
  76. ifr, err := NewIfreq(ifname)
  77. if err != nil {
  78. return err
  79. }
  80. ifrd := ifr.withData(unsafe.Pointer(cfg))
  81. return ioctlIfreqData(fd, SIOCSHWTSTAMP, &ifrd)
  82. }
  83. // FdToClockID derives the clock ID from the file descriptor number
  84. // - see clock_gettime(3), FD_TO_CLOCKID macros. The resulting ID is
  85. // suitable for system calls like ClockGettime.
  86. func FdToClockID(fd int) int32 { return int32((int(^fd) << 3) | 3) }
  87. // IoctlPtpClockGetcaps returns the description of a given PTP device.
  88. func IoctlPtpClockGetcaps(fd int) (*PtpClockCaps, error) {
  89. var value PtpClockCaps
  90. err := ioctlPtr(fd, PTP_CLOCK_GETCAPS2, unsafe.Pointer(&value))
  91. return &value, err
  92. }
  93. // IoctlPtpSysOffsetPrecise returns a description of the clock
  94. // offset compared to the system clock.
  95. func IoctlPtpSysOffsetPrecise(fd int) (*PtpSysOffsetPrecise, error) {
  96. var value PtpSysOffsetPrecise
  97. err := ioctlPtr(fd, PTP_SYS_OFFSET_PRECISE2, unsafe.Pointer(&value))
  98. return &value, err
  99. }
  100. // IoctlPtpSysOffsetExtended returns an extended description of the
  101. // clock offset compared to the system clock. The samples parameter
  102. // specifies the desired number of measurements.
  103. func IoctlPtpSysOffsetExtended(fd int, samples uint) (*PtpSysOffsetExtended, error) {
  104. value := PtpSysOffsetExtended{Samples: uint32(samples)}
  105. err := ioctlPtr(fd, PTP_SYS_OFFSET_EXTENDED2, unsafe.Pointer(&value))
  106. return &value, err
  107. }
  108. // IoctlPtpPinGetfunc returns the configuration of the specified
  109. // I/O pin on given PTP device.
  110. func IoctlPtpPinGetfunc(fd int, index uint) (*PtpPinDesc, error) {
  111. value := PtpPinDesc{Index: uint32(index)}
  112. err := ioctlPtr(fd, PTP_PIN_GETFUNC2, unsafe.Pointer(&value))
  113. return &value, err
  114. }
  115. // IoctlPtpPinSetfunc updates configuration of the specified PTP
  116. // I/O pin.
  117. func IoctlPtpPinSetfunc(fd int, pd *PtpPinDesc) error {
  118. return ioctlPtr(fd, PTP_PIN_SETFUNC2, unsafe.Pointer(pd))
  119. }
  120. // IoctlPtpPeroutRequest configures the periodic output mode of the
  121. // PTP I/O pins.
  122. func IoctlPtpPeroutRequest(fd int, r *PtpPeroutRequest) error {
  123. return ioctlPtr(fd, PTP_PEROUT_REQUEST2, unsafe.Pointer(r))
  124. }
  125. // IoctlPtpExttsRequest configures the external timestamping mode
  126. // of the PTP I/O pins.
  127. func IoctlPtpExttsRequest(fd int, r *PtpExttsRequest) error {
  128. return ioctlPtr(fd, PTP_EXTTS_REQUEST2, unsafe.Pointer(r))
  129. }
  130. // IoctlGetWatchdogInfo fetches information about a watchdog device from the
  131. // Linux watchdog API. For more information, see:
  132. // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
  133. func IoctlGetWatchdogInfo(fd int) (*WatchdogInfo, error) {
  134. var value WatchdogInfo
  135. err := ioctlPtr(fd, WDIOC_GETSUPPORT, unsafe.Pointer(&value))
  136. return &value, err
  137. }
  138. // IoctlWatchdogKeepalive issues a keepalive ioctl to a watchdog device. For
  139. // more information, see:
  140. // https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
  141. func IoctlWatchdogKeepalive(fd int) error {
  142. // arg is ignored and not a pointer, so ioctl is fine instead of ioctlPtr.
  143. return ioctl(fd, WDIOC_KEEPALIVE, 0)
  144. }
  145. // IoctlFileCloneRange performs an FICLONERANGE ioctl operation to clone the
  146. // range of data conveyed in value to the file associated with the file
  147. // descriptor destFd. See the ioctl_ficlonerange(2) man page for details.
  148. func IoctlFileCloneRange(destFd int, value *FileCloneRange) error {
  149. return ioctlPtr(destFd, FICLONERANGE, unsafe.Pointer(value))
  150. }
  151. // IoctlFileClone performs an FICLONE ioctl operation to clone the entire file
  152. // associated with the file description srcFd to the file associated with the
  153. // file descriptor destFd. See the ioctl_ficlone(2) man page for details.
  154. func IoctlFileClone(destFd, srcFd int) error {
  155. return ioctl(destFd, FICLONE, uintptr(srcFd))
  156. }
  157. type FileDedupeRange struct {
  158. Src_offset uint64
  159. Src_length uint64
  160. Reserved1 uint16
  161. Reserved2 uint32
  162. Info []FileDedupeRangeInfo
  163. }
  164. type FileDedupeRangeInfo struct {
  165. Dest_fd int64
  166. Dest_offset uint64
  167. Bytes_deduped uint64
  168. Status int32
  169. Reserved uint32
  170. }
  171. // IoctlFileDedupeRange performs an FIDEDUPERANGE ioctl operation to share the
  172. // range of data conveyed in value from the file associated with the file
  173. // descriptor srcFd to the value.Info destinations. See the
  174. // ioctl_fideduperange(2) man page for details.
  175. func IoctlFileDedupeRange(srcFd int, value *FileDedupeRange) error {
  176. buf := make([]byte, SizeofRawFileDedupeRange+
  177. len(value.Info)*SizeofRawFileDedupeRangeInfo)
  178. rawrange := (*RawFileDedupeRange)(unsafe.Pointer(&buf[0]))
  179. rawrange.Src_offset = value.Src_offset
  180. rawrange.Src_length = value.Src_length
  181. rawrange.Dest_count = uint16(len(value.Info))
  182. rawrange.Reserved1 = value.Reserved1
  183. rawrange.Reserved2 = value.Reserved2
  184. for i := range value.Info {
  185. rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
  186. uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
  187. uintptr(i*SizeofRawFileDedupeRangeInfo)))
  188. rawinfo.Dest_fd = value.Info[i].Dest_fd
  189. rawinfo.Dest_offset = value.Info[i].Dest_offset
  190. rawinfo.Bytes_deduped = value.Info[i].Bytes_deduped
  191. rawinfo.Status = value.Info[i].Status
  192. rawinfo.Reserved = value.Info[i].Reserved
  193. }
  194. err := ioctlPtr(srcFd, FIDEDUPERANGE, unsafe.Pointer(&buf[0]))
  195. // Output
  196. for i := range value.Info {
  197. rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
  198. uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
  199. uintptr(i*SizeofRawFileDedupeRangeInfo)))
  200. value.Info[i].Dest_fd = rawinfo.Dest_fd
  201. value.Info[i].Dest_offset = rawinfo.Dest_offset
  202. value.Info[i].Bytes_deduped = rawinfo.Bytes_deduped
  203. value.Info[i].Status = rawinfo.Status
  204. value.Info[i].Reserved = rawinfo.Reserved
  205. }
  206. return err
  207. }
  208. func IoctlHIDGetDesc(fd int, value *HIDRawReportDescriptor) error {
  209. return ioctlPtr(fd, HIDIOCGRDESC, unsafe.Pointer(value))
  210. }
  211. func IoctlHIDGetRawInfo(fd int) (*HIDRawDevInfo, error) {
  212. var value HIDRawDevInfo
  213. err := ioctlPtr(fd, HIDIOCGRAWINFO, unsafe.Pointer(&value))
  214. return &value, err
  215. }
  216. func IoctlHIDGetRawName(fd int) (string, error) {
  217. var value [_HIDIOCGRAWNAME_LEN]byte
  218. err := ioctlPtr(fd, _HIDIOCGRAWNAME, unsafe.Pointer(&value[0]))
  219. return ByteSliceToString(value[:]), err
  220. }
  221. func IoctlHIDGetRawPhys(fd int) (string, error) {
  222. var value [_HIDIOCGRAWPHYS_LEN]byte
  223. err := ioctlPtr(fd, _HIDIOCGRAWPHYS, unsafe.Pointer(&value[0]))
  224. return ByteSliceToString(value[:]), err
  225. }
  226. func IoctlHIDGetRawUniq(fd int) (string, error) {
  227. var value [_HIDIOCGRAWUNIQ_LEN]byte
  228. err := ioctlPtr(fd, _HIDIOCGRAWUNIQ, unsafe.Pointer(&value[0]))
  229. return ByteSliceToString(value[:]), err
  230. }
  231. // IoctlIfreq performs an ioctl using an Ifreq structure for input and/or
  232. // output. See the netdevice(7) man page for details.
  233. func IoctlIfreq(fd int, req uint, value *Ifreq) error {
  234. // It is possible we will add more fields to *Ifreq itself later to prevent
  235. // misuse, so pass the raw *ifreq directly.
  236. return ioctlPtr(fd, req, unsafe.Pointer(&value.raw))
  237. }
  238. // TODO(mdlayher): export if and when IfreqData is exported.
  239. // ioctlIfreqData performs an ioctl using an ifreqData structure for input
  240. // and/or output. See the netdevice(7) man page for details.
  241. func ioctlIfreqData(fd int, req uint, value *ifreqData) error {
  242. // The memory layout of IfreqData (type-safe) and ifreq (not type-safe) are
  243. // identical so pass *IfreqData directly.
  244. return ioctlPtr(fd, req, unsafe.Pointer(value))
  245. }
  246. // IoctlKCMClone attaches a new file descriptor to a multiplexor by cloning an
  247. // existing KCM socket, returning a structure containing the file descriptor of
  248. // the new socket.
  249. func IoctlKCMClone(fd int) (*KCMClone, error) {
  250. var info KCMClone
  251. if err := ioctlPtr(fd, SIOCKCMCLONE, unsafe.Pointer(&info)); err != nil {
  252. return nil, err
  253. }
  254. return &info, nil
  255. }
  256. // IoctlKCMAttach attaches a TCP socket and associated BPF program file
  257. // descriptor to a multiplexor.
  258. func IoctlKCMAttach(fd int, info KCMAttach) error {
  259. return ioctlPtr(fd, SIOCKCMATTACH, unsafe.Pointer(&info))
  260. }
  261. // IoctlKCMUnattach unattaches a TCP socket file descriptor from a multiplexor.
  262. func IoctlKCMUnattach(fd int, info KCMUnattach) error {
  263. return ioctlPtr(fd, SIOCKCMUNATTACH, unsafe.Pointer(&info))
  264. }
  265. // IoctlLoopGetStatus64 gets the status of the loop device associated with the
  266. // file descriptor fd using the LOOP_GET_STATUS64 operation.
  267. func IoctlLoopGetStatus64(fd int) (*LoopInfo64, error) {
  268. var value LoopInfo64
  269. if err := ioctlPtr(fd, LOOP_GET_STATUS64, unsafe.Pointer(&value)); err != nil {
  270. return nil, err
  271. }
  272. return &value, nil
  273. }
  274. // IoctlLoopSetStatus64 sets the status of the loop device associated with the
  275. // file descriptor fd using the LOOP_SET_STATUS64 operation.
  276. func IoctlLoopSetStatus64(fd int, value *LoopInfo64) error {
  277. return ioctlPtr(fd, LOOP_SET_STATUS64, unsafe.Pointer(value))
  278. }
  279. // IoctlLoopConfigure configures all loop device parameters in a single step
  280. func IoctlLoopConfigure(fd int, value *LoopConfig) error {
  281. return ioctlPtr(fd, LOOP_CONFIGURE, unsafe.Pointer(value))
  282. }