1
2
3
4
5 package unix
6
7 import "unsafe"
8
9
10
11
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
20 func IoctlGetUint32(fd int, req uint) (uint32, error) {
21 var value uint32
22 err := ioctlPtr(fd, req, unsafe.Pointer(&value))
23 return value, err
24 }
25
26 func IoctlGetRTCTime(fd int) (*RTCTime, error) {
27 var value RTCTime
28 err := ioctlPtr(fd, RTC_RD_TIME, unsafe.Pointer(&value))
29 return &value, err
30 }
31
32 func IoctlSetRTCTime(fd int, value *RTCTime) error {
33 return ioctlPtr(fd, RTC_SET_TIME, unsafe.Pointer(value))
34 }
35
36 func IoctlGetRTCWkAlrm(fd int) (*RTCWkAlrm, error) {
37 var value RTCWkAlrm
38 err := ioctlPtr(fd, RTC_WKALM_RD, unsafe.Pointer(&value))
39 return &value, err
40 }
41
42 func IoctlSetRTCWkAlrm(fd int, value *RTCWkAlrm) error {
43 return ioctlPtr(fd, RTC_WKALM_SET, unsafe.Pointer(value))
44 }
45
46
47
48 func IoctlGetEthtoolDrvinfo(fd int, ifname string) (*EthtoolDrvinfo, error) {
49 ifr, err := NewIfreq(ifname)
50 if err != nil {
51 return nil, err
52 }
53
54 value := EthtoolDrvinfo{Cmd: ETHTOOL_GDRVINFO}
55 ifrd := ifr.withData(unsafe.Pointer(&value))
56
57 err = ioctlIfreqData(fd, SIOCETHTOOL, &ifrd)
58 return &value, err
59 }
60
61
62
63 func IoctlGetEthtoolTsInfo(fd int, ifname string) (*EthtoolTsInfo, error) {
64 ifr, err := NewIfreq(ifname)
65 if err != nil {
66 return nil, err
67 }
68
69 value := EthtoolTsInfo{Cmd: ETHTOOL_GET_TS_INFO}
70 ifrd := ifr.withData(unsafe.Pointer(&value))
71
72 err = ioctlIfreqData(fd, SIOCETHTOOL, &ifrd)
73 return &value, err
74 }
75
76
77
78 func IoctlGetHwTstamp(fd int, ifname string) (*HwTstampConfig, error) {
79 ifr, err := NewIfreq(ifname)
80 if err != nil {
81 return nil, err
82 }
83
84 value := HwTstampConfig{}
85 ifrd := ifr.withData(unsafe.Pointer(&value))
86
87 err = ioctlIfreqData(fd, SIOCGHWTSTAMP, &ifrd)
88 return &value, err
89 }
90
91
92
93 func IoctlSetHwTstamp(fd int, ifname string, cfg *HwTstampConfig) error {
94 ifr, err := NewIfreq(ifname)
95 if err != nil {
96 return err
97 }
98 ifrd := ifr.withData(unsafe.Pointer(cfg))
99 return ioctlIfreqData(fd, SIOCSHWTSTAMP, &ifrd)
100 }
101
102
103
104
105 func FdToClockID(fd int) int32 { return int32((int(^fd) << 3) | 3) }
106
107
108 func IoctlPtpClockGetcaps(fd int) (*PtpClockCaps, error) {
109 var value PtpClockCaps
110 err := ioctlPtr(fd, PTP_CLOCK_GETCAPS2, unsafe.Pointer(&value))
111 return &value, err
112 }
113
114
115
116 func IoctlPtpSysOffsetPrecise(fd int) (*PtpSysOffsetPrecise, error) {
117 var value PtpSysOffsetPrecise
118 err := ioctlPtr(fd, PTP_SYS_OFFSET_PRECISE2, unsafe.Pointer(&value))
119 return &value, err
120 }
121
122
123
124
125 func IoctlPtpSysOffsetExtended(fd int, samples uint) (*PtpSysOffsetExtended, error) {
126 value := PtpSysOffsetExtended{Samples: uint32(samples)}
127 err := ioctlPtr(fd, PTP_SYS_OFFSET_EXTENDED2, unsafe.Pointer(&value))
128 return &value, err
129 }
130
131
132
133 func IoctlPtpPinGetfunc(fd int, index uint) (*PtpPinDesc, error) {
134 value := PtpPinDesc{Index: uint32(index)}
135 err := ioctlPtr(fd, PTP_PIN_GETFUNC2, unsafe.Pointer(&value))
136 return &value, err
137 }
138
139
140
141 func IoctlPtpPinSetfunc(fd int, pd *PtpPinDesc) error {
142 return ioctlPtr(fd, PTP_PIN_SETFUNC2, unsafe.Pointer(pd))
143 }
144
145
146
147 func IoctlPtpPeroutRequest(fd int, r *PtpPeroutRequest) error {
148 return ioctlPtr(fd, PTP_PEROUT_REQUEST2, unsafe.Pointer(r))
149 }
150
151
152
153 func IoctlPtpExttsRequest(fd int, r *PtpExttsRequest) error {
154 return ioctlPtr(fd, PTP_EXTTS_REQUEST2, unsafe.Pointer(r))
155 }
156
157
158
159
160 func IoctlGetWatchdogInfo(fd int) (*WatchdogInfo, error) {
161 var value WatchdogInfo
162 err := ioctlPtr(fd, WDIOC_GETSUPPORT, unsafe.Pointer(&value))
163 return &value, err
164 }
165
166
167
168
169 func IoctlWatchdogKeepalive(fd int) error {
170
171 return ioctl(fd, WDIOC_KEEPALIVE, 0)
172 }
173
174
175
176
177 func IoctlFileCloneRange(destFd int, value *FileCloneRange) error {
178 return ioctlPtr(destFd, FICLONERANGE, unsafe.Pointer(value))
179 }
180
181
182
183
184 func IoctlFileClone(destFd, srcFd int) error {
185 return ioctl(destFd, FICLONE, uintptr(srcFd))
186 }
187
188 type FileDedupeRange struct {
189 Src_offset uint64
190 Src_length uint64
191 Reserved1 uint16
192 Reserved2 uint32
193 Info []FileDedupeRangeInfo
194 }
195
196 type FileDedupeRangeInfo struct {
197 Dest_fd int64
198 Dest_offset uint64
199 Bytes_deduped uint64
200 Status int32
201 Reserved uint32
202 }
203
204
205
206
207
208 func IoctlFileDedupeRange(srcFd int, value *FileDedupeRange) error {
209 buf := make([]byte, SizeofRawFileDedupeRange+
210 len(value.Info)*SizeofRawFileDedupeRangeInfo)
211 rawrange := (*RawFileDedupeRange)(unsafe.Pointer(&buf[0]))
212 rawrange.Src_offset = value.Src_offset
213 rawrange.Src_length = value.Src_length
214 rawrange.Dest_count = uint16(len(value.Info))
215 rawrange.Reserved1 = value.Reserved1
216 rawrange.Reserved2 = value.Reserved2
217
218 for i := range value.Info {
219 rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
220 uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
221 uintptr(i*SizeofRawFileDedupeRangeInfo)))
222 rawinfo.Dest_fd = value.Info[i].Dest_fd
223 rawinfo.Dest_offset = value.Info[i].Dest_offset
224 rawinfo.Bytes_deduped = value.Info[i].Bytes_deduped
225 rawinfo.Status = value.Info[i].Status
226 rawinfo.Reserved = value.Info[i].Reserved
227 }
228
229 err := ioctlPtr(srcFd, FIDEDUPERANGE, unsafe.Pointer(&buf[0]))
230
231
232 for i := range value.Info {
233 rawinfo := (*RawFileDedupeRangeInfo)(unsafe.Pointer(
234 uintptr(unsafe.Pointer(&buf[0])) + uintptr(SizeofRawFileDedupeRange) +
235 uintptr(i*SizeofRawFileDedupeRangeInfo)))
236 value.Info[i].Dest_fd = rawinfo.Dest_fd
237 value.Info[i].Dest_offset = rawinfo.Dest_offset
238 value.Info[i].Bytes_deduped = rawinfo.Bytes_deduped
239 value.Info[i].Status = rawinfo.Status
240 value.Info[i].Reserved = rawinfo.Reserved
241 }
242
243 return err
244 }
245
246 func IoctlHIDGetDesc(fd int, value *HIDRawReportDescriptor) error {
247 return ioctlPtr(fd, HIDIOCGRDESC, unsafe.Pointer(value))
248 }
249
250 func IoctlHIDGetRawInfo(fd int) (*HIDRawDevInfo, error) {
251 var value HIDRawDevInfo
252 err := ioctlPtr(fd, HIDIOCGRAWINFO, unsafe.Pointer(&value))
253 return &value, err
254 }
255
256 func IoctlHIDGetRawName(fd int) (string, error) {
257 var value [_HIDIOCGRAWNAME_LEN]byte
258 err := ioctlPtr(fd, _HIDIOCGRAWNAME, unsafe.Pointer(&value[0]))
259 return ByteSliceToString(value[:]), err
260 }
261
262 func IoctlHIDGetRawPhys(fd int) (string, error) {
263 var value [_HIDIOCGRAWPHYS_LEN]byte
264 err := ioctlPtr(fd, _HIDIOCGRAWPHYS, unsafe.Pointer(&value[0]))
265 return ByteSliceToString(value[:]), err
266 }
267
268 func IoctlHIDGetRawUniq(fd int) (string, error) {
269 var value [_HIDIOCGRAWUNIQ_LEN]byte
270 err := ioctlPtr(fd, _HIDIOCGRAWUNIQ, unsafe.Pointer(&value[0]))
271 return ByteSliceToString(value[:]), err
272 }
273
274
275
276 func IoctlIfreq(fd int, req uint, value *Ifreq) error {
277
278
279 return ioctlPtr(fd, req, unsafe.Pointer(&value.raw))
280 }
281
282
283
284
285
286 func ioctlIfreqData(fd int, req uint, value *ifreqData) error {
287
288
289 return ioctlPtr(fd, req, unsafe.Pointer(value))
290 }
291
292
293
294
295 func IoctlKCMClone(fd int) (*KCMClone, error) {
296 var info KCMClone
297 if err := ioctlPtr(fd, SIOCKCMCLONE, unsafe.Pointer(&info)); err != nil {
298 return nil, err
299 }
300
301 return &info, nil
302 }
303
304
305
306 func IoctlKCMAttach(fd int, info KCMAttach) error {
307 return ioctlPtr(fd, SIOCKCMATTACH, unsafe.Pointer(&info))
308 }
309
310
311 func IoctlKCMUnattach(fd int, info KCMUnattach) error {
312 return ioctlPtr(fd, SIOCKCMUNATTACH, unsafe.Pointer(&info))
313 }
314
315
316
317 func IoctlLoopGetStatus64(fd int) (*LoopInfo64, error) {
318 var value LoopInfo64
319 if err := ioctlPtr(fd, LOOP_GET_STATUS64, unsafe.Pointer(&value)); err != nil {
320 return nil, err
321 }
322 return &value, nil
323 }
324
325
326
327 func IoctlLoopSetStatus64(fd int, value *LoopInfo64) error {
328 return ioctlPtr(fd, LOOP_SET_STATUS64, unsafe.Pointer(value))
329 }
330
331
332 func IoctlLoopConfigure(fd int, value *LoopConfig) error {
333 return ioctlPtr(fd, LOOP_CONFIGURE, unsafe.Pointer(value))
334 }
335
View as plain text