2021-08-12 21:03:24 +02:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package unix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IoctlRetInt performs an ioctl operation specified by req on a device
|
|
|
|
// associated with opened file descriptor fd, and returns a non-negative
|
|
|
|
// integer that is returned by the ioctl syscall.
|
|
|
|
func IoctlRetInt(fd int, req uint) (int, error) {
|
|
|
|
ret, _, err := Syscall(SYS_IOCTL, uintptr(fd), uintptr(req), 0)
|
|
|
|
if err != 0 {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(ret), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlGetUint32(fd int, req uint) (uint32, error) {
|
|
|
|
var value uint32
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
|
2021-08-12 21:03:24 +02:00
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlGetRTCTime(fd int) (*RTCTime, error) {
|
|
|
|
var value RTCTime
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, RTC_RD_TIME, unsafe.Pointer(&value))
|
2021-08-12 21:03:24 +02:00
|
|
|
return &value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlSetRTCTime(fd int, value *RTCTime) error {
|
2021-08-29 16:41:41 +02:00
|
|
|
return ioctlPtr(fd, RTC_SET_TIME, unsafe.Pointer(value))
|
2021-08-12 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlGetRTCWkAlrm(fd int) (*RTCWkAlrm, error) {
|
|
|
|
var value RTCWkAlrm
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, RTC_WKALM_RD, unsafe.Pointer(&value))
|
2021-08-12 21:03:24 +02:00
|
|
|
return &value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlSetRTCWkAlrm(fd int, value *RTCWkAlrm) error {
|
2021-08-29 16:41:41 +02:00
|
|
|
return ioctlPtr(fd, RTC_WKALM_SET, unsafe.Pointer(value))
|
2021-08-12 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IoctlGetEthtoolDrvinfo fetches ethtool driver information for the network
|
|
|
|
// device specified by ifname.
|
|
|
|
func IoctlGetEthtoolDrvinfo(fd int, ifname string) (*EthtoolDrvinfo, error) {
|
2021-08-29 16:41:41 +02:00
|
|
|
ifr, err := NewIfreq(ifname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-12 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 16:41:41 +02:00
|
|
|
value := EthtoolDrvinfo{Cmd: ETHTOOL_GDRVINFO}
|
|
|
|
ifrd := ifr.withData(unsafe.Pointer(&value))
|
|
|
|
|
|
|
|
err = ioctlIfreqData(fd, SIOCETHTOOL, &ifrd)
|
2021-08-12 21:03:24 +02:00
|
|
|
return &value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IoctlGetWatchdogInfo fetches information about a watchdog device from the
|
|
|
|
// Linux watchdog API. For more information, see:
|
|
|
|
// https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
|
|
|
|
func IoctlGetWatchdogInfo(fd int) (*WatchdogInfo, error) {
|
|
|
|
var value WatchdogInfo
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, WDIOC_GETSUPPORT, unsafe.Pointer(&value))
|
2021-08-12 21:03:24 +02:00
|
|
|
return &value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IoctlWatchdogKeepalive issues a keepalive ioctl to a watchdog device. For
|
|
|
|
// more information, see:
|
|
|
|
// https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.
|
|
|
|
func IoctlWatchdogKeepalive(fd int) error {
|
2021-08-29 16:41:41 +02:00
|
|
|
// arg is ignored and not a pointer, so ioctl is fine instead of ioctlPtr.
|
2021-08-12 21:03:24 +02:00
|
|
|
return ioctl(fd, WDIOC_KEEPALIVE, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IoctlFileCloneRange performs an FICLONERANGE ioctl operation to clone the
|
|
|
|
// range of data conveyed in value to the file associated with the file
|
|
|
|
// descriptor destFd. See the ioctl_ficlonerange(2) man page for details.
|
|
|
|
func IoctlFileCloneRange(destFd int, value *FileCloneRange) error {
|
2021-08-29 16:41:41 +02:00
|
|
|
return ioctlPtr(destFd, FICLONERANGE, unsafe.Pointer(value))
|
2021-08-12 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IoctlFileClone performs an FICLONE ioctl operation to clone the entire file
|
|
|
|
// associated with the file description srcFd to the file associated with the
|
|
|
|
// file descriptor destFd. See the ioctl_ficlone(2) man page for details.
|
|
|
|
func IoctlFileClone(destFd, srcFd int) error {
|
|
|
|
return ioctl(destFd, FICLONE, uintptr(srcFd))
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileDedupeRange struct {
|
|
|
|
Src_offset uint64
|
|
|
|
Src_length uint64
|
|
|
|
Reserved1 uint16
|
|
|
|
Reserved2 uint32
|
|
|
|
Info []FileDedupeRangeInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileDedupeRangeInfo struct {
|
|
|
|
Dest_fd int64
|
|
|
|
Dest_offset uint64
|
|
|
|
Bytes_deduped uint64
|
|
|
|
Status int32
|
|
|
|
Reserved uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// IoctlFileDedupeRange performs an FIDEDUPERANGE ioctl operation to share the
|
|
|
|
// range of data conveyed in value from the file associated with the file
|
|
|
|
// descriptor srcFd to the value.Info destinations. See the
|
|
|
|
// ioctl_fideduperange(2) man page for details.
|
|
|
|
func IoctlFileDedupeRange(srcFd int, value *FileDedupeRange) error {
|
|
|
|
buf := make([]byte, SizeofRawFileDedupeRange+
|
|
|
|
len(value.Info)*SizeofRawFileDedupeRangeInfo)
|
|
|
|
rawrange := (*RawFileDedupeRange)(unsafe.Pointer(&buf[0]))
|
|
|
|
rawrange.Src_offset = value.Src_offset
|
|
|
|
rawrange.Src_length = value.Src_length
|
|
|
|
rawrange.Dest_count = uint16(len(value.Info))
|
|
|
|
rawrange.Reserved1 = value.Reserved1
|
|
|
|
rawrange.Reserved2 = value.Reserved2
|
|
|
|
|
|
|
|
for i := range value.Info {
|
|
|
|
rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
|
|
|
|
uintptr(i*SizeofRawFileDedupeRangeInfo)))
|
|
|
|
rawinfo.Dest_fd = value.Info[i].Dest_fd
|
|
|
|
rawinfo.Dest_offset = value.Info[i].Dest_offset
|
|
|
|
rawinfo.Bytes_deduped = value.Info[i].Bytes_deduped
|
|
|
|
rawinfo.Status = value.Info[i].Status
|
|
|
|
rawinfo.Reserved = value.Info[i].Reserved
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(srcFd, FIDEDUPERANGE, unsafe.Pointer(&buf[0]))
|
2021-08-12 21:03:24 +02:00
|
|
|
|
|
|
|
// Output
|
|
|
|
for i := range value.Info {
|
|
|
|
rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
|
|
|
|
uintptr(i*SizeofRawFileDedupeRangeInfo)))
|
|
|
|
value.Info[i].Dest_fd = rawinfo.Dest_fd
|
|
|
|
value.Info[i].Dest_offset = rawinfo.Dest_offset
|
|
|
|
value.Info[i].Bytes_deduped = rawinfo.Bytes_deduped
|
|
|
|
value.Info[i].Status = rawinfo.Status
|
|
|
|
value.Info[i].Reserved = rawinfo.Reserved
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlHIDGetDesc(fd int, value *HIDRawReportDescriptor) error {
|
2021-08-29 16:41:41 +02:00
|
|
|
return ioctlPtr(fd, HIDIOCGRDESC, unsafe.Pointer(value))
|
2021-08-12 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlHIDGetRawInfo(fd int) (*HIDRawDevInfo, error) {
|
|
|
|
var value HIDRawDevInfo
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, HIDIOCGRAWINFO, unsafe.Pointer(&value))
|
2021-08-12 21:03:24 +02:00
|
|
|
return &value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlHIDGetRawName(fd int) (string, error) {
|
|
|
|
var value [_HIDIOCGRAWNAME_LEN]byte
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, _HIDIOCGRAWNAME, unsafe.Pointer(&value[0]))
|
2021-08-12 21:03:24 +02:00
|
|
|
return ByteSliceToString(value[:]), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlHIDGetRawPhys(fd int) (string, error) {
|
|
|
|
var value [_HIDIOCGRAWPHYS_LEN]byte
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, _HIDIOCGRAWPHYS, unsafe.Pointer(&value[0]))
|
2021-08-12 21:03:24 +02:00
|
|
|
return ByteSliceToString(value[:]), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IoctlHIDGetRawUniq(fd int) (string, error) {
|
|
|
|
var value [_HIDIOCGRAWUNIQ_LEN]byte
|
2021-08-29 16:41:41 +02:00
|
|
|
err := ioctlPtr(fd, _HIDIOCGRAWUNIQ, unsafe.Pointer(&value[0]))
|
2021-08-12 21:03:24 +02:00
|
|
|
return ByteSliceToString(value[:]), err
|
|
|
|
}
|
2021-08-29 16:41:41 +02:00
|
|
|
|
|
|
|
// IoctlIfreq performs an ioctl using an Ifreq structure for input and/or
|
|
|
|
// output. See the netdevice(7) man page for details.
|
|
|
|
func IoctlIfreq(fd int, req uint, value *Ifreq) error {
|
|
|
|
// It is possible we will add more fields to *Ifreq itself later to prevent
|
|
|
|
// misuse, so pass the raw *ifreq directly.
|
|
|
|
return ioctlPtr(fd, req, unsafe.Pointer(&value.raw))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mdlayher): export if and when IfreqData is exported.
|
|
|
|
|
|
|
|
// ioctlIfreqData performs an ioctl using an ifreqData structure for input
|
|
|
|
// and/or output. See the netdevice(7) man page for details.
|
|
|
|
func ioctlIfreqData(fd int, req uint, value *ifreqData) error {
|
|
|
|
// The memory layout of IfreqData (type-safe) and ifreq (not type-safe) are
|
|
|
|
// identical so pass *IfreqData directly.
|
|
|
|
return ioctlPtr(fd, req, unsafe.Pointer(value))
|
|
|
|
}
|