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