Mercurial > yakumo_izuru > aya
comparison vendor/golang.org/x/sys/unix/syscall_linux.go @ 66:787b5ee0289d draft
Use vendored modules
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>
author | yakumo.izuru |
---|---|
date | Sun, 23 Jul 2023 13:18:53 +0000 |
parents | |
children | 4b79810863f6 |
comparison
equal
deleted
inserted
replaced
65:6d985efa0f7a | 66:787b5ee0289d |
---|---|
1 // Copyright 2009 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 // Linux system calls. | |
6 // This file is compiled as ordinary Go code, | |
7 // but it is also input to mksyscall, | |
8 // which parses the //sys lines and generates system call stubs. | |
9 // Note that sometimes we use a lowercase //sys name and | |
10 // wrap it in our own nicer implementation. | |
11 | |
12 package unix | |
13 | |
14 import ( | |
15 "encoding/binary" | |
16 "syscall" | |
17 "time" | |
18 "unsafe" | |
19 ) | |
20 | |
21 /* | |
22 * Wrapped | |
23 */ | |
24 | |
25 func Access(path string, mode uint32) (err error) { | |
26 return Faccessat(AT_FDCWD, path, mode, 0) | |
27 } | |
28 | |
29 func Chmod(path string, mode uint32) (err error) { | |
30 return Fchmodat(AT_FDCWD, path, mode, 0) | |
31 } | |
32 | |
33 func Chown(path string, uid int, gid int) (err error) { | |
34 return Fchownat(AT_FDCWD, path, uid, gid, 0) | |
35 } | |
36 | |
37 func Creat(path string, mode uint32) (fd int, err error) { | |
38 return Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode) | |
39 } | |
40 | |
41 func EpollCreate(size int) (fd int, err error) { | |
42 if size <= 0 { | |
43 return -1, EINVAL | |
44 } | |
45 return EpollCreate1(0) | |
46 } | |
47 | |
48 //sys FanotifyInit(flags uint, event_f_flags uint) (fd int, err error) | |
49 //sys fanotifyMark(fd int, flags uint, mask uint64, dirFd int, pathname *byte) (err error) | |
50 | |
51 func FanotifyMark(fd int, flags uint, mask uint64, dirFd int, pathname string) (err error) { | |
52 if pathname == "" { | |
53 return fanotifyMark(fd, flags, mask, dirFd, nil) | |
54 } | |
55 p, err := BytePtrFromString(pathname) | |
56 if err != nil { | |
57 return err | |
58 } | |
59 return fanotifyMark(fd, flags, mask, dirFd, p) | |
60 } | |
61 | |
62 //sys fchmodat(dirfd int, path string, mode uint32) (err error) | |
63 | |
64 func Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) { | |
65 // Linux fchmodat doesn't support the flags parameter. Mimick glibc's behavior | |
66 // and check the flags. Otherwise the mode would be applied to the symlink | |
67 // destination which is not what the user expects. | |
68 if flags&^AT_SYMLINK_NOFOLLOW != 0 { | |
69 return EINVAL | |
70 } else if flags&AT_SYMLINK_NOFOLLOW != 0 { | |
71 return EOPNOTSUPP | |
72 } | |
73 return fchmodat(dirfd, path, mode) | |
74 } | |
75 | |
76 func InotifyInit() (fd int, err error) { | |
77 return InotifyInit1(0) | |
78 } | |
79 | |
80 //sys ioctl(fd int, req uint, arg uintptr) (err error) = SYS_IOCTL | |
81 //sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL | |
82 | |
83 // ioctl itself should not be exposed directly, but additional get/set functions | |
84 // for specific types are permissible. These are defined in ioctl.go and | |
85 // ioctl_linux.go. | |
86 // | |
87 // The third argument to ioctl is often a pointer but sometimes an integer. | |
88 // Callers should use ioctlPtr when the third argument is a pointer and ioctl | |
89 // when the third argument is an integer. | |
90 // | |
91 // TODO: some existing code incorrectly uses ioctl when it should use ioctlPtr. | |
92 | |
93 //sys Linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error) | |
94 | |
95 func Link(oldpath string, newpath string) (err error) { | |
96 return Linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0) | |
97 } | |
98 | |
99 func Mkdir(path string, mode uint32) (err error) { | |
100 return Mkdirat(AT_FDCWD, path, mode) | |
101 } | |
102 | |
103 func Mknod(path string, mode uint32, dev int) (err error) { | |
104 return Mknodat(AT_FDCWD, path, mode, dev) | |
105 } | |
106 | |
107 func Open(path string, mode int, perm uint32) (fd int, err error) { | |
108 return openat(AT_FDCWD, path, mode|O_LARGEFILE, perm) | |
109 } | |
110 | |
111 //sys openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) | |
112 | |
113 func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { | |
114 return openat(dirfd, path, flags|O_LARGEFILE, mode) | |
115 } | |
116 | |
117 //sys openat2(dirfd int, path string, open_how *OpenHow, size int) (fd int, err error) | |
118 | |
119 func Openat2(dirfd int, path string, how *OpenHow) (fd int, err error) { | |
120 return openat2(dirfd, path, how, SizeofOpenHow) | |
121 } | |
122 | |
123 func Pipe(p []int) error { | |
124 return Pipe2(p, 0) | |
125 } | |
126 | |
127 //sysnb pipe2(p *[2]_C_int, flags int) (err error) | |
128 | |
129 func Pipe2(p []int, flags int) error { | |
130 if len(p) != 2 { | |
131 return EINVAL | |
132 } | |
133 var pp [2]_C_int | |
134 err := pipe2(&pp, flags) | |
135 if err == nil { | |
136 p[0] = int(pp[0]) | |
137 p[1] = int(pp[1]) | |
138 } | |
139 return err | |
140 } | |
141 | |
142 //sys ppoll(fds *PollFd, nfds int, timeout *Timespec, sigmask *Sigset_t) (n int, err error) | |
143 | |
144 func Ppoll(fds []PollFd, timeout *Timespec, sigmask *Sigset_t) (n int, err error) { | |
145 if len(fds) == 0 { | |
146 return ppoll(nil, 0, timeout, sigmask) | |
147 } | |
148 return ppoll(&fds[0], len(fds), timeout, sigmask) | |
149 } | |
150 | |
151 func Poll(fds []PollFd, timeout int) (n int, err error) { | |
152 var ts *Timespec | |
153 if timeout >= 0 { | |
154 ts = new(Timespec) | |
155 *ts = NsecToTimespec(int64(timeout) * 1e6) | |
156 } | |
157 return Ppoll(fds, ts, nil) | |
158 } | |
159 | |
160 //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error) | |
161 | |
162 func Readlink(path string, buf []byte) (n int, err error) { | |
163 return Readlinkat(AT_FDCWD, path, buf) | |
164 } | |
165 | |
166 func Rename(oldpath string, newpath string) (err error) { | |
167 return Renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath) | |
168 } | |
169 | |
170 func Rmdir(path string) error { | |
171 return Unlinkat(AT_FDCWD, path, AT_REMOVEDIR) | |
172 } | |
173 | |
174 //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error) | |
175 | |
176 func Symlink(oldpath string, newpath string) (err error) { | |
177 return Symlinkat(oldpath, AT_FDCWD, newpath) | |
178 } | |
179 | |
180 func Unlink(path string) error { | |
181 return Unlinkat(AT_FDCWD, path, 0) | |
182 } | |
183 | |
184 //sys Unlinkat(dirfd int, path string, flags int) (err error) | |
185 | |
186 func Utimes(path string, tv []Timeval) error { | |
187 if tv == nil { | |
188 err := utimensat(AT_FDCWD, path, nil, 0) | |
189 if err != ENOSYS { | |
190 return err | |
191 } | |
192 return utimes(path, nil) | |
193 } | |
194 if len(tv) != 2 { | |
195 return EINVAL | |
196 } | |
197 var ts [2]Timespec | |
198 ts[0] = NsecToTimespec(TimevalToNsec(tv[0])) | |
199 ts[1] = NsecToTimespec(TimevalToNsec(tv[1])) | |
200 err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0) | |
201 if err != ENOSYS { | |
202 return err | |
203 } | |
204 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) | |
205 } | |
206 | |
207 //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) | |
208 | |
209 func UtimesNano(path string, ts []Timespec) error { | |
210 return UtimesNanoAt(AT_FDCWD, path, ts, 0) | |
211 } | |
212 | |
213 func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error { | |
214 if ts == nil { | |
215 return utimensat(dirfd, path, nil, flags) | |
216 } | |
217 if len(ts) != 2 { | |
218 return EINVAL | |
219 } | |
220 return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags) | |
221 } | |
222 | |
223 func Futimesat(dirfd int, path string, tv []Timeval) error { | |
224 if tv == nil { | |
225 return futimesat(dirfd, path, nil) | |
226 } | |
227 if len(tv) != 2 { | |
228 return EINVAL | |
229 } | |
230 return futimesat(dirfd, path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) | |
231 } | |
232 | |
233 func Futimes(fd int, tv []Timeval) (err error) { | |
234 // Believe it or not, this is the best we can do on Linux | |
235 // (and is what glibc does). | |
236 return Utimes("/proc/self/fd/"+itoa(fd), tv) | |
237 } | |
238 | |
239 const ImplementsGetwd = true | |
240 | |
241 //sys Getcwd(buf []byte) (n int, err error) | |
242 | |
243 func Getwd() (wd string, err error) { | |
244 var buf [PathMax]byte | |
245 n, err := Getcwd(buf[0:]) | |
246 if err != nil { | |
247 return "", err | |
248 } | |
249 // Getcwd returns the number of bytes written to buf, including the NUL. | |
250 if n < 1 || n > len(buf) || buf[n-1] != 0 { | |
251 return "", EINVAL | |
252 } | |
253 // In some cases, Linux can return a path that starts with the | |
254 // "(unreachable)" prefix, which can potentially be a valid relative | |
255 // path. To work around that, return ENOENT if path is not absolute. | |
256 if buf[0] != '/' { | |
257 return "", ENOENT | |
258 } | |
259 | |
260 return string(buf[0 : n-1]), nil | |
261 } | |
262 | |
263 func Getgroups() (gids []int, err error) { | |
264 n, err := getgroups(0, nil) | |
265 if err != nil { | |
266 return nil, err | |
267 } | |
268 if n == 0 { | |
269 return nil, nil | |
270 } | |
271 | |
272 // Sanity check group count. Max is 1<<16 on Linux. | |
273 if n < 0 || n > 1<<20 { | |
274 return nil, EINVAL | |
275 } | |
276 | |
277 a := make([]_Gid_t, n) | |
278 n, err = getgroups(n, &a[0]) | |
279 if err != nil { | |
280 return nil, err | |
281 } | |
282 gids = make([]int, n) | |
283 for i, v := range a[0:n] { | |
284 gids[i] = int(v) | |
285 } | |
286 return | |
287 } | |
288 | |
289 func Setgroups(gids []int) (err error) { | |
290 if len(gids) == 0 { | |
291 return setgroups(0, nil) | |
292 } | |
293 | |
294 a := make([]_Gid_t, len(gids)) | |
295 for i, v := range gids { | |
296 a[i] = _Gid_t(v) | |
297 } | |
298 return setgroups(len(a), &a[0]) | |
299 } | |
300 | |
301 type WaitStatus uint32 | |
302 | |
303 // Wait status is 7 bits at bottom, either 0 (exited), | |
304 // 0x7F (stopped), or a signal number that caused an exit. | |
305 // The 0x80 bit is whether there was a core dump. | |
306 // An extra number (exit code, signal causing a stop) | |
307 // is in the high bits. At least that's the idea. | |
308 // There are various irregularities. For example, the | |
309 // "continued" status is 0xFFFF, distinguishing itself | |
310 // from stopped via the core dump bit. | |
311 | |
312 const ( | |
313 mask = 0x7F | |
314 core = 0x80 | |
315 exited = 0x00 | |
316 stopped = 0x7F | |
317 shift = 8 | |
318 ) | |
319 | |
320 func (w WaitStatus) Exited() bool { return w&mask == exited } | |
321 | |
322 func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited } | |
323 | |
324 func (w WaitStatus) Stopped() bool { return w&0xFF == stopped } | |
325 | |
326 func (w WaitStatus) Continued() bool { return w == 0xFFFF } | |
327 | |
328 func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 } | |
329 | |
330 func (w WaitStatus) ExitStatus() int { | |
331 if !w.Exited() { | |
332 return -1 | |
333 } | |
334 return int(w>>shift) & 0xFF | |
335 } | |
336 | |
337 func (w WaitStatus) Signal() syscall.Signal { | |
338 if !w.Signaled() { | |
339 return -1 | |
340 } | |
341 return syscall.Signal(w & mask) | |
342 } | |
343 | |
344 func (w WaitStatus) StopSignal() syscall.Signal { | |
345 if !w.Stopped() { | |
346 return -1 | |
347 } | |
348 return syscall.Signal(w>>shift) & 0xFF | |
349 } | |
350 | |
351 func (w WaitStatus) TrapCause() int { | |
352 if w.StopSignal() != SIGTRAP { | |
353 return -1 | |
354 } | |
355 return int(w>>shift) >> 8 | |
356 } | |
357 | |
358 //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error) | |
359 | |
360 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) { | |
361 var status _C_int | |
362 wpid, err = wait4(pid, &status, options, rusage) | |
363 if wstatus != nil { | |
364 *wstatus = WaitStatus(status) | |
365 } | |
366 return | |
367 } | |
368 | |
369 //sys Waitid(idType int, id int, info *Siginfo, options int, rusage *Rusage) (err error) | |
370 | |
371 func Mkfifo(path string, mode uint32) error { | |
372 return Mknod(path, mode|S_IFIFO, 0) | |
373 } | |
374 | |
375 func Mkfifoat(dirfd int, path string, mode uint32) error { | |
376 return Mknodat(dirfd, path, mode|S_IFIFO, 0) | |
377 } | |
378 | |
379 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
380 if sa.Port < 0 || sa.Port > 0xFFFF { | |
381 return nil, 0, EINVAL | |
382 } | |
383 sa.raw.Family = AF_INET | |
384 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) | |
385 p[0] = byte(sa.Port >> 8) | |
386 p[1] = byte(sa.Port) | |
387 sa.raw.Addr = sa.Addr | |
388 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil | |
389 } | |
390 | |
391 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
392 if sa.Port < 0 || sa.Port > 0xFFFF { | |
393 return nil, 0, EINVAL | |
394 } | |
395 sa.raw.Family = AF_INET6 | |
396 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) | |
397 p[0] = byte(sa.Port >> 8) | |
398 p[1] = byte(sa.Port) | |
399 sa.raw.Scope_id = sa.ZoneId | |
400 sa.raw.Addr = sa.Addr | |
401 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil | |
402 } | |
403 | |
404 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
405 name := sa.Name | |
406 n := len(name) | |
407 if n >= len(sa.raw.Path) { | |
408 return nil, 0, EINVAL | |
409 } | |
410 sa.raw.Family = AF_UNIX | |
411 for i := 0; i < n; i++ { | |
412 sa.raw.Path[i] = int8(name[i]) | |
413 } | |
414 // length is family (uint16), name, NUL. | |
415 sl := _Socklen(2) | |
416 if n > 0 { | |
417 sl += _Socklen(n) + 1 | |
418 } | |
419 if sa.raw.Path[0] == '@' { | |
420 sa.raw.Path[0] = 0 | |
421 // Don't count trailing NUL for abstract address. | |
422 sl-- | |
423 } | |
424 | |
425 return unsafe.Pointer(&sa.raw), sl, nil | |
426 } | |
427 | |
428 // SockaddrLinklayer implements the Sockaddr interface for AF_PACKET type sockets. | |
429 type SockaddrLinklayer struct { | |
430 Protocol uint16 | |
431 Ifindex int | |
432 Hatype uint16 | |
433 Pkttype uint8 | |
434 Halen uint8 | |
435 Addr [8]byte | |
436 raw RawSockaddrLinklayer | |
437 } | |
438 | |
439 func (sa *SockaddrLinklayer) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
440 if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff { | |
441 return nil, 0, EINVAL | |
442 } | |
443 sa.raw.Family = AF_PACKET | |
444 sa.raw.Protocol = sa.Protocol | |
445 sa.raw.Ifindex = int32(sa.Ifindex) | |
446 sa.raw.Hatype = sa.Hatype | |
447 sa.raw.Pkttype = sa.Pkttype | |
448 sa.raw.Halen = sa.Halen | |
449 sa.raw.Addr = sa.Addr | |
450 return unsafe.Pointer(&sa.raw), SizeofSockaddrLinklayer, nil | |
451 } | |
452 | |
453 // SockaddrNetlink implements the Sockaddr interface for AF_NETLINK type sockets. | |
454 type SockaddrNetlink struct { | |
455 Family uint16 | |
456 Pad uint16 | |
457 Pid uint32 | |
458 Groups uint32 | |
459 raw RawSockaddrNetlink | |
460 } | |
461 | |
462 func (sa *SockaddrNetlink) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
463 sa.raw.Family = AF_NETLINK | |
464 sa.raw.Pad = sa.Pad | |
465 sa.raw.Pid = sa.Pid | |
466 sa.raw.Groups = sa.Groups | |
467 return unsafe.Pointer(&sa.raw), SizeofSockaddrNetlink, nil | |
468 } | |
469 | |
470 // SockaddrHCI implements the Sockaddr interface for AF_BLUETOOTH type sockets | |
471 // using the HCI protocol. | |
472 type SockaddrHCI struct { | |
473 Dev uint16 | |
474 Channel uint16 | |
475 raw RawSockaddrHCI | |
476 } | |
477 | |
478 func (sa *SockaddrHCI) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
479 sa.raw.Family = AF_BLUETOOTH | |
480 sa.raw.Dev = sa.Dev | |
481 sa.raw.Channel = sa.Channel | |
482 return unsafe.Pointer(&sa.raw), SizeofSockaddrHCI, nil | |
483 } | |
484 | |
485 // SockaddrL2 implements the Sockaddr interface for AF_BLUETOOTH type sockets | |
486 // using the L2CAP protocol. | |
487 type SockaddrL2 struct { | |
488 PSM uint16 | |
489 CID uint16 | |
490 Addr [6]uint8 | |
491 AddrType uint8 | |
492 raw RawSockaddrL2 | |
493 } | |
494 | |
495 func (sa *SockaddrL2) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
496 sa.raw.Family = AF_BLUETOOTH | |
497 psm := (*[2]byte)(unsafe.Pointer(&sa.raw.Psm)) | |
498 psm[0] = byte(sa.PSM) | |
499 psm[1] = byte(sa.PSM >> 8) | |
500 for i := 0; i < len(sa.Addr); i++ { | |
501 sa.raw.Bdaddr[i] = sa.Addr[len(sa.Addr)-1-i] | |
502 } | |
503 cid := (*[2]byte)(unsafe.Pointer(&sa.raw.Cid)) | |
504 cid[0] = byte(sa.CID) | |
505 cid[1] = byte(sa.CID >> 8) | |
506 sa.raw.Bdaddr_type = sa.AddrType | |
507 return unsafe.Pointer(&sa.raw), SizeofSockaddrL2, nil | |
508 } | |
509 | |
510 // SockaddrRFCOMM implements the Sockaddr interface for AF_BLUETOOTH type sockets | |
511 // using the RFCOMM protocol. | |
512 // | |
513 // Server example: | |
514 // | |
515 // fd, _ := Socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM) | |
516 // _ = unix.Bind(fd, &unix.SockaddrRFCOMM{ | |
517 // Channel: 1, | |
518 // Addr: [6]uint8{0, 0, 0, 0, 0, 0}, // BDADDR_ANY or 00:00:00:00:00:00 | |
519 // }) | |
520 // _ = Listen(fd, 1) | |
521 // nfd, sa, _ := Accept(fd) | |
522 // fmt.Printf("conn addr=%v fd=%d", sa.(*unix.SockaddrRFCOMM).Addr, nfd) | |
523 // Read(nfd, buf) | |
524 // | |
525 // Client example: | |
526 // | |
527 // fd, _ := Socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM) | |
528 // _ = Connect(fd, &SockaddrRFCOMM{ | |
529 // Channel: 1, | |
530 // Addr: [6]byte{0x11, 0x22, 0x33, 0xaa, 0xbb, 0xcc}, // CC:BB:AA:33:22:11 | |
531 // }) | |
532 // Write(fd, []byte(`hello`)) | |
533 type SockaddrRFCOMM struct { | |
534 // Addr represents a bluetooth address, byte ordering is little-endian. | |
535 Addr [6]uint8 | |
536 | |
537 // Channel is a designated bluetooth channel, only 1-30 are available for use. | |
538 // Since Linux 2.6.7 and further zero value is the first available channel. | |
539 Channel uint8 | |
540 | |
541 raw RawSockaddrRFCOMM | |
542 } | |
543 | |
544 func (sa *SockaddrRFCOMM) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
545 sa.raw.Family = AF_BLUETOOTH | |
546 sa.raw.Channel = sa.Channel | |
547 sa.raw.Bdaddr = sa.Addr | |
548 return unsafe.Pointer(&sa.raw), SizeofSockaddrRFCOMM, nil | |
549 } | |
550 | |
551 // SockaddrCAN implements the Sockaddr interface for AF_CAN type sockets. | |
552 // The RxID and TxID fields are used for transport protocol addressing in | |
553 // (CAN_TP16, CAN_TP20, CAN_MCNET, and CAN_ISOTP), they can be left with | |
554 // zero values for CAN_RAW and CAN_BCM sockets as they have no meaning. | |
555 // | |
556 // The SockaddrCAN struct must be bound to the socket file descriptor | |
557 // using Bind before the CAN socket can be used. | |
558 // | |
559 // // Read one raw CAN frame | |
560 // fd, _ := Socket(AF_CAN, SOCK_RAW, CAN_RAW) | |
561 // addr := &SockaddrCAN{Ifindex: index} | |
562 // Bind(fd, addr) | |
563 // frame := make([]byte, 16) | |
564 // Read(fd, frame) | |
565 // | |
566 // The full SocketCAN documentation can be found in the linux kernel | |
567 // archives at: https://www.kernel.org/doc/Documentation/networking/can.txt | |
568 type SockaddrCAN struct { | |
569 Ifindex int | |
570 RxID uint32 | |
571 TxID uint32 | |
572 raw RawSockaddrCAN | |
573 } | |
574 | |
575 func (sa *SockaddrCAN) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
576 if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff { | |
577 return nil, 0, EINVAL | |
578 } | |
579 sa.raw.Family = AF_CAN | |
580 sa.raw.Ifindex = int32(sa.Ifindex) | |
581 rx := (*[4]byte)(unsafe.Pointer(&sa.RxID)) | |
582 for i := 0; i < 4; i++ { | |
583 sa.raw.Addr[i] = rx[i] | |
584 } | |
585 tx := (*[4]byte)(unsafe.Pointer(&sa.TxID)) | |
586 for i := 0; i < 4; i++ { | |
587 sa.raw.Addr[i+4] = tx[i] | |
588 } | |
589 return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil | |
590 } | |
591 | |
592 // SockaddrCANJ1939 implements the Sockaddr interface for AF_CAN using J1939 | |
593 // protocol (https://en.wikipedia.org/wiki/SAE_J1939). For more information | |
594 // on the purposes of the fields, check the official linux kernel documentation | |
595 // available here: https://www.kernel.org/doc/Documentation/networking/j1939.rst | |
596 type SockaddrCANJ1939 struct { | |
597 Ifindex int | |
598 Name uint64 | |
599 PGN uint32 | |
600 Addr uint8 | |
601 raw RawSockaddrCAN | |
602 } | |
603 | |
604 func (sa *SockaddrCANJ1939) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
605 if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff { | |
606 return nil, 0, EINVAL | |
607 } | |
608 sa.raw.Family = AF_CAN | |
609 sa.raw.Ifindex = int32(sa.Ifindex) | |
610 n := (*[8]byte)(unsafe.Pointer(&sa.Name)) | |
611 for i := 0; i < 8; i++ { | |
612 sa.raw.Addr[i] = n[i] | |
613 } | |
614 p := (*[4]byte)(unsafe.Pointer(&sa.PGN)) | |
615 for i := 0; i < 4; i++ { | |
616 sa.raw.Addr[i+8] = p[i] | |
617 } | |
618 sa.raw.Addr[12] = sa.Addr | |
619 return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil | |
620 } | |
621 | |
622 // SockaddrALG implements the Sockaddr interface for AF_ALG type sockets. | |
623 // SockaddrALG enables userspace access to the Linux kernel's cryptography | |
624 // subsystem. The Type and Name fields specify which type of hash or cipher | |
625 // should be used with a given socket. | |
626 // | |
627 // To create a file descriptor that provides access to a hash or cipher, both | |
628 // Bind and Accept must be used. Once the setup process is complete, input | |
629 // data can be written to the socket, processed by the kernel, and then read | |
630 // back as hash output or ciphertext. | |
631 // | |
632 // Here is an example of using an AF_ALG socket with SHA1 hashing. | |
633 // The initial socket setup process is as follows: | |
634 // | |
635 // // Open a socket to perform SHA1 hashing. | |
636 // fd, _ := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0) | |
637 // addr := &unix.SockaddrALG{Type: "hash", Name: "sha1"} | |
638 // unix.Bind(fd, addr) | |
639 // // Note: unix.Accept does not work at this time; must invoke accept() | |
640 // // manually using unix.Syscall. | |
641 // hashfd, _, _ := unix.Syscall(unix.SYS_ACCEPT, uintptr(fd), 0, 0) | |
642 // | |
643 // Once a file descriptor has been returned from Accept, it may be used to | |
644 // perform SHA1 hashing. The descriptor is not safe for concurrent use, but | |
645 // may be re-used repeatedly with subsequent Write and Read operations. | |
646 // | |
647 // When hashing a small byte slice or string, a single Write and Read may | |
648 // be used: | |
649 // | |
650 // // Assume hashfd is already configured using the setup process. | |
651 // hash := os.NewFile(hashfd, "sha1") | |
652 // // Hash an input string and read the results. Each Write discards | |
653 // // previous hash state. Read always reads the current state. | |
654 // b := make([]byte, 20) | |
655 // for i := 0; i < 2; i++ { | |
656 // io.WriteString(hash, "Hello, world.") | |
657 // hash.Read(b) | |
658 // fmt.Println(hex.EncodeToString(b)) | |
659 // } | |
660 // // Output: | |
661 // // 2ae01472317d1935a84797ec1983ae243fc6aa28 | |
662 // // 2ae01472317d1935a84797ec1983ae243fc6aa28 | |
663 // | |
664 // For hashing larger byte slices, or byte streams such as those read from | |
665 // a file or socket, use Sendto with MSG_MORE to instruct the kernel to update | |
666 // the hash digest instead of creating a new one for a given chunk and finalizing it. | |
667 // | |
668 // // Assume hashfd and addr are already configured using the setup process. | |
669 // hash := os.NewFile(hashfd, "sha1") | |
670 // // Hash the contents of a file. | |
671 // f, _ := os.Open("/tmp/linux-4.10-rc7.tar.xz") | |
672 // b := make([]byte, 4096) | |
673 // for { | |
674 // n, err := f.Read(b) | |
675 // if err == io.EOF { | |
676 // break | |
677 // } | |
678 // unix.Sendto(hashfd, b[:n], unix.MSG_MORE, addr) | |
679 // } | |
680 // hash.Read(b) | |
681 // fmt.Println(hex.EncodeToString(b)) | |
682 // // Output: 85cdcad0c06eef66f805ecce353bec9accbeecc5 | |
683 // | |
684 // For more information, see: http://www.chronox.de/crypto-API/crypto/userspace-if.html. | |
685 type SockaddrALG struct { | |
686 Type string | |
687 Name string | |
688 Feature uint32 | |
689 Mask uint32 | |
690 raw RawSockaddrALG | |
691 } | |
692 | |
693 func (sa *SockaddrALG) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
694 // Leave room for NUL byte terminator. | |
695 if len(sa.Type) > 13 { | |
696 return nil, 0, EINVAL | |
697 } | |
698 if len(sa.Name) > 63 { | |
699 return nil, 0, EINVAL | |
700 } | |
701 | |
702 sa.raw.Family = AF_ALG | |
703 sa.raw.Feat = sa.Feature | |
704 sa.raw.Mask = sa.Mask | |
705 | |
706 typ, err := ByteSliceFromString(sa.Type) | |
707 if err != nil { | |
708 return nil, 0, err | |
709 } | |
710 name, err := ByteSliceFromString(sa.Name) | |
711 if err != nil { | |
712 return nil, 0, err | |
713 } | |
714 | |
715 copy(sa.raw.Type[:], typ) | |
716 copy(sa.raw.Name[:], name) | |
717 | |
718 return unsafe.Pointer(&sa.raw), SizeofSockaddrALG, nil | |
719 } | |
720 | |
721 // SockaddrVM implements the Sockaddr interface for AF_VSOCK type sockets. | |
722 // SockaddrVM provides access to Linux VM sockets: a mechanism that enables | |
723 // bidirectional communication between a hypervisor and its guest virtual | |
724 // machines. | |
725 type SockaddrVM struct { | |
726 // CID and Port specify a context ID and port address for a VM socket. | |
727 // Guests have a unique CID, and hosts may have a well-known CID of: | |
728 // - VMADDR_CID_HYPERVISOR: refers to the hypervisor process. | |
729 // - VMADDR_CID_LOCAL: refers to local communication (loopback). | |
730 // - VMADDR_CID_HOST: refers to other processes on the host. | |
731 CID uint32 | |
732 Port uint32 | |
733 Flags uint8 | |
734 raw RawSockaddrVM | |
735 } | |
736 | |
737 func (sa *SockaddrVM) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
738 sa.raw.Family = AF_VSOCK | |
739 sa.raw.Port = sa.Port | |
740 sa.raw.Cid = sa.CID | |
741 sa.raw.Flags = sa.Flags | |
742 | |
743 return unsafe.Pointer(&sa.raw), SizeofSockaddrVM, nil | |
744 } | |
745 | |
746 type SockaddrXDP struct { | |
747 Flags uint16 | |
748 Ifindex uint32 | |
749 QueueID uint32 | |
750 SharedUmemFD uint32 | |
751 raw RawSockaddrXDP | |
752 } | |
753 | |
754 func (sa *SockaddrXDP) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
755 sa.raw.Family = AF_XDP | |
756 sa.raw.Flags = sa.Flags | |
757 sa.raw.Ifindex = sa.Ifindex | |
758 sa.raw.Queue_id = sa.QueueID | |
759 sa.raw.Shared_umem_fd = sa.SharedUmemFD | |
760 | |
761 return unsafe.Pointer(&sa.raw), SizeofSockaddrXDP, nil | |
762 } | |
763 | |
764 // This constant mirrors the #define of PX_PROTO_OE in | |
765 // linux/if_pppox.h. We're defining this by hand here instead of | |
766 // autogenerating through mkerrors.sh because including | |
767 // linux/if_pppox.h causes some declaration conflicts with other | |
768 // includes (linux/if_pppox.h includes linux/in.h, which conflicts | |
769 // with netinet/in.h). Given that we only need a single zero constant | |
770 // out of that file, it's cleaner to just define it by hand here. | |
771 const px_proto_oe = 0 | |
772 | |
773 type SockaddrPPPoE struct { | |
774 SID uint16 | |
775 Remote []byte | |
776 Dev string | |
777 raw RawSockaddrPPPoX | |
778 } | |
779 | |
780 func (sa *SockaddrPPPoE) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
781 if len(sa.Remote) != 6 { | |
782 return nil, 0, EINVAL | |
783 } | |
784 if len(sa.Dev) > IFNAMSIZ-1 { | |
785 return nil, 0, EINVAL | |
786 } | |
787 | |
788 *(*uint16)(unsafe.Pointer(&sa.raw[0])) = AF_PPPOX | |
789 // This next field is in host-endian byte order. We can't use the | |
790 // same unsafe pointer cast as above, because this value is not | |
791 // 32-bit aligned and some architectures don't allow unaligned | |
792 // access. | |
793 // | |
794 // However, the value of px_proto_oe is 0, so we can use | |
795 // encoding/binary helpers to write the bytes without worrying | |
796 // about the ordering. | |
797 binary.BigEndian.PutUint32(sa.raw[2:6], px_proto_oe) | |
798 // This field is deliberately big-endian, unlike the previous | |
799 // one. The kernel expects SID to be in network byte order. | |
800 binary.BigEndian.PutUint16(sa.raw[6:8], sa.SID) | |
801 copy(sa.raw[8:14], sa.Remote) | |
802 for i := 14; i < 14+IFNAMSIZ; i++ { | |
803 sa.raw[i] = 0 | |
804 } | |
805 copy(sa.raw[14:], sa.Dev) | |
806 return unsafe.Pointer(&sa.raw), SizeofSockaddrPPPoX, nil | |
807 } | |
808 | |
809 // SockaddrTIPC implements the Sockaddr interface for AF_TIPC type sockets. | |
810 // For more information on TIPC, see: http://tipc.sourceforge.net/. | |
811 type SockaddrTIPC struct { | |
812 // Scope is the publication scopes when binding service/service range. | |
813 // Should be set to TIPC_CLUSTER_SCOPE or TIPC_NODE_SCOPE. | |
814 Scope int | |
815 | |
816 // Addr is the type of address used to manipulate a socket. Addr must be | |
817 // one of: | |
818 // - *TIPCSocketAddr: "id" variant in the C addr union | |
819 // - *TIPCServiceRange: "nameseq" variant in the C addr union | |
820 // - *TIPCServiceName: "name" variant in the C addr union | |
821 // | |
822 // If nil, EINVAL will be returned when the structure is used. | |
823 Addr TIPCAddr | |
824 | |
825 raw RawSockaddrTIPC | |
826 } | |
827 | |
828 // TIPCAddr is implemented by types that can be used as an address for | |
829 // SockaddrTIPC. It is only implemented by *TIPCSocketAddr, *TIPCServiceRange, | |
830 // and *TIPCServiceName. | |
831 type TIPCAddr interface { | |
832 tipcAddrtype() uint8 | |
833 tipcAddr() [12]byte | |
834 } | |
835 | |
836 func (sa *TIPCSocketAddr) tipcAddr() [12]byte { | |
837 var out [12]byte | |
838 copy(out[:], (*(*[unsafe.Sizeof(TIPCSocketAddr{})]byte)(unsafe.Pointer(sa)))[:]) | |
839 return out | |
840 } | |
841 | |
842 func (sa *TIPCSocketAddr) tipcAddrtype() uint8 { return TIPC_SOCKET_ADDR } | |
843 | |
844 func (sa *TIPCServiceRange) tipcAddr() [12]byte { | |
845 var out [12]byte | |
846 copy(out[:], (*(*[unsafe.Sizeof(TIPCServiceRange{})]byte)(unsafe.Pointer(sa)))[:]) | |
847 return out | |
848 } | |
849 | |
850 func (sa *TIPCServiceRange) tipcAddrtype() uint8 { return TIPC_SERVICE_RANGE } | |
851 | |
852 func (sa *TIPCServiceName) tipcAddr() [12]byte { | |
853 var out [12]byte | |
854 copy(out[:], (*(*[unsafe.Sizeof(TIPCServiceName{})]byte)(unsafe.Pointer(sa)))[:]) | |
855 return out | |
856 } | |
857 | |
858 func (sa *TIPCServiceName) tipcAddrtype() uint8 { return TIPC_SERVICE_ADDR } | |
859 | |
860 func (sa *SockaddrTIPC) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
861 if sa.Addr == nil { | |
862 return nil, 0, EINVAL | |
863 } | |
864 sa.raw.Family = AF_TIPC | |
865 sa.raw.Scope = int8(sa.Scope) | |
866 sa.raw.Addrtype = sa.Addr.tipcAddrtype() | |
867 sa.raw.Addr = sa.Addr.tipcAddr() | |
868 return unsafe.Pointer(&sa.raw), SizeofSockaddrTIPC, nil | |
869 } | |
870 | |
871 // SockaddrL2TPIP implements the Sockaddr interface for IPPROTO_L2TP/AF_INET sockets. | |
872 type SockaddrL2TPIP struct { | |
873 Addr [4]byte | |
874 ConnId uint32 | |
875 raw RawSockaddrL2TPIP | |
876 } | |
877 | |
878 func (sa *SockaddrL2TPIP) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
879 sa.raw.Family = AF_INET | |
880 sa.raw.Conn_id = sa.ConnId | |
881 sa.raw.Addr = sa.Addr | |
882 return unsafe.Pointer(&sa.raw), SizeofSockaddrL2TPIP, nil | |
883 } | |
884 | |
885 // SockaddrL2TPIP6 implements the Sockaddr interface for IPPROTO_L2TP/AF_INET6 sockets. | |
886 type SockaddrL2TPIP6 struct { | |
887 Addr [16]byte | |
888 ZoneId uint32 | |
889 ConnId uint32 | |
890 raw RawSockaddrL2TPIP6 | |
891 } | |
892 | |
893 func (sa *SockaddrL2TPIP6) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
894 sa.raw.Family = AF_INET6 | |
895 sa.raw.Conn_id = sa.ConnId | |
896 sa.raw.Scope_id = sa.ZoneId | |
897 sa.raw.Addr = sa.Addr | |
898 return unsafe.Pointer(&sa.raw), SizeofSockaddrL2TPIP6, nil | |
899 } | |
900 | |
901 // SockaddrIUCV implements the Sockaddr interface for AF_IUCV sockets. | |
902 type SockaddrIUCV struct { | |
903 UserID string | |
904 Name string | |
905 raw RawSockaddrIUCV | |
906 } | |
907 | |
908 func (sa *SockaddrIUCV) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
909 sa.raw.Family = AF_IUCV | |
910 // These are EBCDIC encoded by the kernel, but we still need to pad them | |
911 // with blanks. Initializing with blanks allows the caller to feed in either | |
912 // a padded or an unpadded string. | |
913 for i := 0; i < 8; i++ { | |
914 sa.raw.Nodeid[i] = ' ' | |
915 sa.raw.User_id[i] = ' ' | |
916 sa.raw.Name[i] = ' ' | |
917 } | |
918 if len(sa.UserID) > 8 || len(sa.Name) > 8 { | |
919 return nil, 0, EINVAL | |
920 } | |
921 for i, b := range []byte(sa.UserID[:]) { | |
922 sa.raw.User_id[i] = int8(b) | |
923 } | |
924 for i, b := range []byte(sa.Name[:]) { | |
925 sa.raw.Name[i] = int8(b) | |
926 } | |
927 return unsafe.Pointer(&sa.raw), SizeofSockaddrIUCV, nil | |
928 } | |
929 | |
930 type SockaddrNFC struct { | |
931 DeviceIdx uint32 | |
932 TargetIdx uint32 | |
933 NFCProtocol uint32 | |
934 raw RawSockaddrNFC | |
935 } | |
936 | |
937 func (sa *SockaddrNFC) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
938 sa.raw.Sa_family = AF_NFC | |
939 sa.raw.Dev_idx = sa.DeviceIdx | |
940 sa.raw.Target_idx = sa.TargetIdx | |
941 sa.raw.Nfc_protocol = sa.NFCProtocol | |
942 return unsafe.Pointer(&sa.raw), SizeofSockaddrNFC, nil | |
943 } | |
944 | |
945 type SockaddrNFCLLCP struct { | |
946 DeviceIdx uint32 | |
947 TargetIdx uint32 | |
948 NFCProtocol uint32 | |
949 DestinationSAP uint8 | |
950 SourceSAP uint8 | |
951 ServiceName string | |
952 raw RawSockaddrNFCLLCP | |
953 } | |
954 | |
955 func (sa *SockaddrNFCLLCP) sockaddr() (unsafe.Pointer, _Socklen, error) { | |
956 sa.raw.Sa_family = AF_NFC | |
957 sa.raw.Dev_idx = sa.DeviceIdx | |
958 sa.raw.Target_idx = sa.TargetIdx | |
959 sa.raw.Nfc_protocol = sa.NFCProtocol | |
960 sa.raw.Dsap = sa.DestinationSAP | |
961 sa.raw.Ssap = sa.SourceSAP | |
962 if len(sa.ServiceName) > len(sa.raw.Service_name) { | |
963 return nil, 0, EINVAL | |
964 } | |
965 copy(sa.raw.Service_name[:], sa.ServiceName) | |
966 sa.raw.SetServiceNameLen(len(sa.ServiceName)) | |
967 return unsafe.Pointer(&sa.raw), SizeofSockaddrNFCLLCP, nil | |
968 } | |
969 | |
970 var socketProtocol = func(fd int) (int, error) { | |
971 return GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL) | |
972 } | |
973 | |
974 func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { | |
975 switch rsa.Addr.Family { | |
976 case AF_NETLINK: | |
977 pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa)) | |
978 sa := new(SockaddrNetlink) | |
979 sa.Family = pp.Family | |
980 sa.Pad = pp.Pad | |
981 sa.Pid = pp.Pid | |
982 sa.Groups = pp.Groups | |
983 return sa, nil | |
984 | |
985 case AF_PACKET: | |
986 pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa)) | |
987 sa := new(SockaddrLinklayer) | |
988 sa.Protocol = pp.Protocol | |
989 sa.Ifindex = int(pp.Ifindex) | |
990 sa.Hatype = pp.Hatype | |
991 sa.Pkttype = pp.Pkttype | |
992 sa.Halen = pp.Halen | |
993 sa.Addr = pp.Addr | |
994 return sa, nil | |
995 | |
996 case AF_UNIX: | |
997 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) | |
998 sa := new(SockaddrUnix) | |
999 if pp.Path[0] == 0 { | |
1000 // "Abstract" Unix domain socket. | |
1001 // Rewrite leading NUL as @ for textual display. | |
1002 // (This is the standard convention.) | |
1003 // Not friendly to overwrite in place, | |
1004 // but the callers below don't care. | |
1005 pp.Path[0] = '@' | |
1006 } | |
1007 | |
1008 // Assume path ends at NUL. | |
1009 // This is not technically the Linux semantics for | |
1010 // abstract Unix domain sockets--they are supposed | |
1011 // to be uninterpreted fixed-size binary blobs--but | |
1012 // everyone uses this convention. | |
1013 n := 0 | |
1014 for n < len(pp.Path) && pp.Path[n] != 0 { | |
1015 n++ | |
1016 } | |
1017 bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n] | |
1018 sa.Name = string(bytes) | |
1019 return sa, nil | |
1020 | |
1021 case AF_INET: | |
1022 proto, err := socketProtocol(fd) | |
1023 if err != nil { | |
1024 return nil, err | |
1025 } | |
1026 | |
1027 switch proto { | |
1028 case IPPROTO_L2TP: | |
1029 pp := (*RawSockaddrL2TPIP)(unsafe.Pointer(rsa)) | |
1030 sa := new(SockaddrL2TPIP) | |
1031 sa.ConnId = pp.Conn_id | |
1032 sa.Addr = pp.Addr | |
1033 return sa, nil | |
1034 default: | |
1035 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa)) | |
1036 sa := new(SockaddrInet4) | |
1037 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) | |
1038 sa.Port = int(p[0])<<8 + int(p[1]) | |
1039 sa.Addr = pp.Addr | |
1040 return sa, nil | |
1041 } | |
1042 | |
1043 case AF_INET6: | |
1044 proto, err := socketProtocol(fd) | |
1045 if err != nil { | |
1046 return nil, err | |
1047 } | |
1048 | |
1049 switch proto { | |
1050 case IPPROTO_L2TP: | |
1051 pp := (*RawSockaddrL2TPIP6)(unsafe.Pointer(rsa)) | |
1052 sa := new(SockaddrL2TPIP6) | |
1053 sa.ConnId = pp.Conn_id | |
1054 sa.ZoneId = pp.Scope_id | |
1055 sa.Addr = pp.Addr | |
1056 return sa, nil | |
1057 default: | |
1058 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa)) | |
1059 sa := new(SockaddrInet6) | |
1060 p := (*[2]byte)(unsafe.Pointer(&pp.Port)) | |
1061 sa.Port = int(p[0])<<8 + int(p[1]) | |
1062 sa.ZoneId = pp.Scope_id | |
1063 sa.Addr = pp.Addr | |
1064 return sa, nil | |
1065 } | |
1066 | |
1067 case AF_VSOCK: | |
1068 pp := (*RawSockaddrVM)(unsafe.Pointer(rsa)) | |
1069 sa := &SockaddrVM{ | |
1070 CID: pp.Cid, | |
1071 Port: pp.Port, | |
1072 Flags: pp.Flags, | |
1073 } | |
1074 return sa, nil | |
1075 case AF_BLUETOOTH: | |
1076 proto, err := socketProtocol(fd) | |
1077 if err != nil { | |
1078 return nil, err | |
1079 } | |
1080 // only BTPROTO_L2CAP and BTPROTO_RFCOMM can accept connections | |
1081 switch proto { | |
1082 case BTPROTO_L2CAP: | |
1083 pp := (*RawSockaddrL2)(unsafe.Pointer(rsa)) | |
1084 sa := &SockaddrL2{ | |
1085 PSM: pp.Psm, | |
1086 CID: pp.Cid, | |
1087 Addr: pp.Bdaddr, | |
1088 AddrType: pp.Bdaddr_type, | |
1089 } | |
1090 return sa, nil | |
1091 case BTPROTO_RFCOMM: | |
1092 pp := (*RawSockaddrRFCOMM)(unsafe.Pointer(rsa)) | |
1093 sa := &SockaddrRFCOMM{ | |
1094 Channel: pp.Channel, | |
1095 Addr: pp.Bdaddr, | |
1096 } | |
1097 return sa, nil | |
1098 } | |
1099 case AF_XDP: | |
1100 pp := (*RawSockaddrXDP)(unsafe.Pointer(rsa)) | |
1101 sa := &SockaddrXDP{ | |
1102 Flags: pp.Flags, | |
1103 Ifindex: pp.Ifindex, | |
1104 QueueID: pp.Queue_id, | |
1105 SharedUmemFD: pp.Shared_umem_fd, | |
1106 } | |
1107 return sa, nil | |
1108 case AF_PPPOX: | |
1109 pp := (*RawSockaddrPPPoX)(unsafe.Pointer(rsa)) | |
1110 if binary.BigEndian.Uint32(pp[2:6]) != px_proto_oe { | |
1111 return nil, EINVAL | |
1112 } | |
1113 sa := &SockaddrPPPoE{ | |
1114 SID: binary.BigEndian.Uint16(pp[6:8]), | |
1115 Remote: pp[8:14], | |
1116 } | |
1117 for i := 14; i < 14+IFNAMSIZ; i++ { | |
1118 if pp[i] == 0 { | |
1119 sa.Dev = string(pp[14:i]) | |
1120 break | |
1121 } | |
1122 } | |
1123 return sa, nil | |
1124 case AF_TIPC: | |
1125 pp := (*RawSockaddrTIPC)(unsafe.Pointer(rsa)) | |
1126 | |
1127 sa := &SockaddrTIPC{ | |
1128 Scope: int(pp.Scope), | |
1129 } | |
1130 | |
1131 // Determine which union variant is present in pp.Addr by checking | |
1132 // pp.Addrtype. | |
1133 switch pp.Addrtype { | |
1134 case TIPC_SERVICE_RANGE: | |
1135 sa.Addr = (*TIPCServiceRange)(unsafe.Pointer(&pp.Addr)) | |
1136 case TIPC_SERVICE_ADDR: | |
1137 sa.Addr = (*TIPCServiceName)(unsafe.Pointer(&pp.Addr)) | |
1138 case TIPC_SOCKET_ADDR: | |
1139 sa.Addr = (*TIPCSocketAddr)(unsafe.Pointer(&pp.Addr)) | |
1140 default: | |
1141 return nil, EINVAL | |
1142 } | |
1143 | |
1144 return sa, nil | |
1145 case AF_IUCV: | |
1146 pp := (*RawSockaddrIUCV)(unsafe.Pointer(rsa)) | |
1147 | |
1148 var user [8]byte | |
1149 var name [8]byte | |
1150 | |
1151 for i := 0; i < 8; i++ { | |
1152 user[i] = byte(pp.User_id[i]) | |
1153 name[i] = byte(pp.Name[i]) | |
1154 } | |
1155 | |
1156 sa := &SockaddrIUCV{ | |
1157 UserID: string(user[:]), | |
1158 Name: string(name[:]), | |
1159 } | |
1160 return sa, nil | |
1161 | |
1162 case AF_CAN: | |
1163 proto, err := socketProtocol(fd) | |
1164 if err != nil { | |
1165 return nil, err | |
1166 } | |
1167 | |
1168 pp := (*RawSockaddrCAN)(unsafe.Pointer(rsa)) | |
1169 | |
1170 switch proto { | |
1171 case CAN_J1939: | |
1172 sa := &SockaddrCANJ1939{ | |
1173 Ifindex: int(pp.Ifindex), | |
1174 } | |
1175 name := (*[8]byte)(unsafe.Pointer(&sa.Name)) | |
1176 for i := 0; i < 8; i++ { | |
1177 name[i] = pp.Addr[i] | |
1178 } | |
1179 pgn := (*[4]byte)(unsafe.Pointer(&sa.PGN)) | |
1180 for i := 0; i < 4; i++ { | |
1181 pgn[i] = pp.Addr[i+8] | |
1182 } | |
1183 addr := (*[1]byte)(unsafe.Pointer(&sa.Addr)) | |
1184 addr[0] = pp.Addr[12] | |
1185 return sa, nil | |
1186 default: | |
1187 sa := &SockaddrCAN{ | |
1188 Ifindex: int(pp.Ifindex), | |
1189 } | |
1190 rx := (*[4]byte)(unsafe.Pointer(&sa.RxID)) | |
1191 for i := 0; i < 4; i++ { | |
1192 rx[i] = pp.Addr[i] | |
1193 } | |
1194 tx := (*[4]byte)(unsafe.Pointer(&sa.TxID)) | |
1195 for i := 0; i < 4; i++ { | |
1196 tx[i] = pp.Addr[i+4] | |
1197 } | |
1198 return sa, nil | |
1199 } | |
1200 case AF_NFC: | |
1201 proto, err := socketProtocol(fd) | |
1202 if err != nil { | |
1203 return nil, err | |
1204 } | |
1205 switch proto { | |
1206 case NFC_SOCKPROTO_RAW: | |
1207 pp := (*RawSockaddrNFC)(unsafe.Pointer(rsa)) | |
1208 sa := &SockaddrNFC{ | |
1209 DeviceIdx: pp.Dev_idx, | |
1210 TargetIdx: pp.Target_idx, | |
1211 NFCProtocol: pp.Nfc_protocol, | |
1212 } | |
1213 return sa, nil | |
1214 case NFC_SOCKPROTO_LLCP: | |
1215 pp := (*RawSockaddrNFCLLCP)(unsafe.Pointer(rsa)) | |
1216 if uint64(pp.Service_name_len) > uint64(len(pp.Service_name)) { | |
1217 return nil, EINVAL | |
1218 } | |
1219 sa := &SockaddrNFCLLCP{ | |
1220 DeviceIdx: pp.Dev_idx, | |
1221 TargetIdx: pp.Target_idx, | |
1222 NFCProtocol: pp.Nfc_protocol, | |
1223 DestinationSAP: pp.Dsap, | |
1224 SourceSAP: pp.Ssap, | |
1225 ServiceName: string(pp.Service_name[:pp.Service_name_len]), | |
1226 } | |
1227 return sa, nil | |
1228 default: | |
1229 return nil, EINVAL | |
1230 } | |
1231 } | |
1232 return nil, EAFNOSUPPORT | |
1233 } | |
1234 | |
1235 func Accept(fd int) (nfd int, sa Sockaddr, err error) { | |
1236 var rsa RawSockaddrAny | |
1237 var len _Socklen = SizeofSockaddrAny | |
1238 nfd, err = accept4(fd, &rsa, &len, 0) | |
1239 if err != nil { | |
1240 return | |
1241 } | |
1242 sa, err = anyToSockaddr(fd, &rsa) | |
1243 if err != nil { | |
1244 Close(nfd) | |
1245 nfd = 0 | |
1246 } | |
1247 return | |
1248 } | |
1249 | |
1250 func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) { | |
1251 var rsa RawSockaddrAny | |
1252 var len _Socklen = SizeofSockaddrAny | |
1253 nfd, err = accept4(fd, &rsa, &len, flags) | |
1254 if err != nil { | |
1255 return | |
1256 } | |
1257 if len > SizeofSockaddrAny { | |
1258 panic("RawSockaddrAny too small") | |
1259 } | |
1260 sa, err = anyToSockaddr(fd, &rsa) | |
1261 if err != nil { | |
1262 Close(nfd) | |
1263 nfd = 0 | |
1264 } | |
1265 return | |
1266 } | |
1267 | |
1268 func Getsockname(fd int) (sa Sockaddr, err error) { | |
1269 var rsa RawSockaddrAny | |
1270 var len _Socklen = SizeofSockaddrAny | |
1271 if err = getsockname(fd, &rsa, &len); err != nil { | |
1272 return | |
1273 } | |
1274 return anyToSockaddr(fd, &rsa) | |
1275 } | |
1276 | |
1277 func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) { | |
1278 var value IPMreqn | |
1279 vallen := _Socklen(SizeofIPMreqn) | |
1280 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) | |
1281 return &value, err | |
1282 } | |
1283 | |
1284 func GetsockoptUcred(fd, level, opt int) (*Ucred, error) { | |
1285 var value Ucred | |
1286 vallen := _Socklen(SizeofUcred) | |
1287 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) | |
1288 return &value, err | |
1289 } | |
1290 | |
1291 func GetsockoptTCPInfo(fd, level, opt int) (*TCPInfo, error) { | |
1292 var value TCPInfo | |
1293 vallen := _Socklen(SizeofTCPInfo) | |
1294 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) | |
1295 return &value, err | |
1296 } | |
1297 | |
1298 // GetsockoptString returns the string value of the socket option opt for the | |
1299 // socket associated with fd at the given socket level. | |
1300 func GetsockoptString(fd, level, opt int) (string, error) { | |
1301 buf := make([]byte, 256) | |
1302 vallen := _Socklen(len(buf)) | |
1303 err := getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen) | |
1304 if err != nil { | |
1305 if err == ERANGE { | |
1306 buf = make([]byte, vallen) | |
1307 err = getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen) | |
1308 } | |
1309 if err != nil { | |
1310 return "", err | |
1311 } | |
1312 } | |
1313 return string(buf[:vallen-1]), nil | |
1314 } | |
1315 | |
1316 func GetsockoptTpacketStats(fd, level, opt int) (*TpacketStats, error) { | |
1317 var value TpacketStats | |
1318 vallen := _Socklen(SizeofTpacketStats) | |
1319 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) | |
1320 return &value, err | |
1321 } | |
1322 | |
1323 func GetsockoptTpacketStatsV3(fd, level, opt int) (*TpacketStatsV3, error) { | |
1324 var value TpacketStatsV3 | |
1325 vallen := _Socklen(SizeofTpacketStatsV3) | |
1326 err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) | |
1327 return &value, err | |
1328 } | |
1329 | |
1330 func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) { | |
1331 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq)) | |
1332 } | |
1333 | |
1334 func SetsockoptPacketMreq(fd, level, opt int, mreq *PacketMreq) error { | |
1335 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq)) | |
1336 } | |
1337 | |
1338 // SetsockoptSockFprog attaches a classic BPF or an extended BPF program to a | |
1339 // socket to filter incoming packets. See 'man 7 socket' for usage information. | |
1340 func SetsockoptSockFprog(fd, level, opt int, fprog *SockFprog) error { | |
1341 return setsockopt(fd, level, opt, unsafe.Pointer(fprog), unsafe.Sizeof(*fprog)) | |
1342 } | |
1343 | |
1344 func SetsockoptCanRawFilter(fd, level, opt int, filter []CanFilter) error { | |
1345 var p unsafe.Pointer | |
1346 if len(filter) > 0 { | |
1347 p = unsafe.Pointer(&filter[0]) | |
1348 } | |
1349 return setsockopt(fd, level, opt, p, uintptr(len(filter)*SizeofCanFilter)) | |
1350 } | |
1351 | |
1352 func SetsockoptTpacketReq(fd, level, opt int, tp *TpacketReq) error { | |
1353 return setsockopt(fd, level, opt, unsafe.Pointer(tp), unsafe.Sizeof(*tp)) | |
1354 } | |
1355 | |
1356 func SetsockoptTpacketReq3(fd, level, opt int, tp *TpacketReq3) error { | |
1357 return setsockopt(fd, level, opt, unsafe.Pointer(tp), unsafe.Sizeof(*tp)) | |
1358 } | |
1359 | |
1360 func SetsockoptTCPRepairOpt(fd, level, opt int, o []TCPRepairOpt) (err error) { | |
1361 if len(o) == 0 { | |
1362 return EINVAL | |
1363 } | |
1364 return setsockopt(fd, level, opt, unsafe.Pointer(&o[0]), uintptr(SizeofTCPRepairOpt*len(o))) | |
1365 } | |
1366 | |
1367 // Keyctl Commands (http://man7.org/linux/man-pages/man2/keyctl.2.html) | |
1368 | |
1369 // KeyctlInt calls keyctl commands in which each argument is an int. | |
1370 // These commands are KEYCTL_REVOKE, KEYCTL_CHOWN, KEYCTL_CLEAR, KEYCTL_LINK, | |
1371 // KEYCTL_UNLINK, KEYCTL_NEGATE, KEYCTL_SET_REQKEY_KEYRING, KEYCTL_SET_TIMEOUT, | |
1372 // KEYCTL_ASSUME_AUTHORITY, KEYCTL_SESSION_TO_PARENT, KEYCTL_REJECT, | |
1373 // KEYCTL_INVALIDATE, and KEYCTL_GET_PERSISTENT. | |
1374 //sys KeyctlInt(cmd int, arg2 int, arg3 int, arg4 int, arg5 int) (ret int, err error) = SYS_KEYCTL | |
1375 | |
1376 // KeyctlBuffer calls keyctl commands in which the third and fourth | |
1377 // arguments are a buffer and its length, respectively. | |
1378 // These commands are KEYCTL_UPDATE, KEYCTL_READ, and KEYCTL_INSTANTIATE. | |
1379 //sys KeyctlBuffer(cmd int, arg2 int, buf []byte, arg5 int) (ret int, err error) = SYS_KEYCTL | |
1380 | |
1381 // KeyctlString calls keyctl commands which return a string. | |
1382 // These commands are KEYCTL_DESCRIBE and KEYCTL_GET_SECURITY. | |
1383 func KeyctlString(cmd int, id int) (string, error) { | |
1384 // We must loop as the string data may change in between the syscalls. | |
1385 // We could allocate a large buffer here to reduce the chance that the | |
1386 // syscall needs to be called twice; however, this is unnecessary as | |
1387 // the performance loss is negligible. | |
1388 var buffer []byte | |
1389 for { | |
1390 // Try to fill the buffer with data | |
1391 length, err := KeyctlBuffer(cmd, id, buffer, 0) | |
1392 if err != nil { | |
1393 return "", err | |
1394 } | |
1395 | |
1396 // Check if the data was written | |
1397 if length <= len(buffer) { | |
1398 // Exclude the null terminator | |
1399 return string(buffer[:length-1]), nil | |
1400 } | |
1401 | |
1402 // Make a bigger buffer if needed | |
1403 buffer = make([]byte, length) | |
1404 } | |
1405 } | |
1406 | |
1407 // Keyctl commands with special signatures. | |
1408 | |
1409 // KeyctlGetKeyringID implements the KEYCTL_GET_KEYRING_ID command. | |
1410 // See the full documentation at: | |
1411 // http://man7.org/linux/man-pages/man3/keyctl_get_keyring_ID.3.html | |
1412 func KeyctlGetKeyringID(id int, create bool) (ringid int, err error) { | |
1413 createInt := 0 | |
1414 if create { | |
1415 createInt = 1 | |
1416 } | |
1417 return KeyctlInt(KEYCTL_GET_KEYRING_ID, id, createInt, 0, 0) | |
1418 } | |
1419 | |
1420 // KeyctlSetperm implements the KEYCTL_SETPERM command. The perm value is the | |
1421 // key handle permission mask as described in the "keyctl setperm" section of | |
1422 // http://man7.org/linux/man-pages/man1/keyctl.1.html. | |
1423 // See the full documentation at: | |
1424 // http://man7.org/linux/man-pages/man3/keyctl_setperm.3.html | |
1425 func KeyctlSetperm(id int, perm uint32) error { | |
1426 _, err := KeyctlInt(KEYCTL_SETPERM, id, int(perm), 0, 0) | |
1427 return err | |
1428 } | |
1429 | |
1430 //sys keyctlJoin(cmd int, arg2 string) (ret int, err error) = SYS_KEYCTL | |
1431 | |
1432 // KeyctlJoinSessionKeyring implements the KEYCTL_JOIN_SESSION_KEYRING command. | |
1433 // See the full documentation at: | |
1434 // http://man7.org/linux/man-pages/man3/keyctl_join_session_keyring.3.html | |
1435 func KeyctlJoinSessionKeyring(name string) (ringid int, err error) { | |
1436 return keyctlJoin(KEYCTL_JOIN_SESSION_KEYRING, name) | |
1437 } | |
1438 | |
1439 //sys keyctlSearch(cmd int, arg2 int, arg3 string, arg4 string, arg5 int) (ret int, err error) = SYS_KEYCTL | |
1440 | |
1441 // KeyctlSearch implements the KEYCTL_SEARCH command. | |
1442 // See the full documentation at: | |
1443 // http://man7.org/linux/man-pages/man3/keyctl_search.3.html | |
1444 func KeyctlSearch(ringid int, keyType, description string, destRingid int) (id int, err error) { | |
1445 return keyctlSearch(KEYCTL_SEARCH, ringid, keyType, description, destRingid) | |
1446 } | |
1447 | |
1448 //sys keyctlIOV(cmd int, arg2 int, payload []Iovec, arg5 int) (err error) = SYS_KEYCTL | |
1449 | |
1450 // KeyctlInstantiateIOV implements the KEYCTL_INSTANTIATE_IOV command. This | |
1451 // command is similar to KEYCTL_INSTANTIATE, except that the payload is a slice | |
1452 // of Iovec (each of which represents a buffer) instead of a single buffer. | |
1453 // See the full documentation at: | |
1454 // http://man7.org/linux/man-pages/man3/keyctl_instantiate_iov.3.html | |
1455 func KeyctlInstantiateIOV(id int, payload []Iovec, ringid int) error { | |
1456 return keyctlIOV(KEYCTL_INSTANTIATE_IOV, id, payload, ringid) | |
1457 } | |
1458 | |
1459 //sys keyctlDH(cmd int, arg2 *KeyctlDHParams, buf []byte) (ret int, err error) = SYS_KEYCTL | |
1460 | |
1461 // KeyctlDHCompute implements the KEYCTL_DH_COMPUTE command. This command | |
1462 // computes a Diffie-Hellman shared secret based on the provide params. The | |
1463 // secret is written to the provided buffer and the returned size is the number | |
1464 // of bytes written (returning an error if there is insufficient space in the | |
1465 // buffer). If a nil buffer is passed in, this function returns the minimum | |
1466 // buffer length needed to store the appropriate data. Note that this differs | |
1467 // from KEYCTL_READ's behavior which always returns the requested payload size. | |
1468 // See the full documentation at: | |
1469 // http://man7.org/linux/man-pages/man3/keyctl_dh_compute.3.html | |
1470 func KeyctlDHCompute(params *KeyctlDHParams, buffer []byte) (size int, err error) { | |
1471 return keyctlDH(KEYCTL_DH_COMPUTE, params, buffer) | |
1472 } | |
1473 | |
1474 // KeyctlRestrictKeyring implements the KEYCTL_RESTRICT_KEYRING command. This | |
1475 // command limits the set of keys that can be linked to the keyring, regardless | |
1476 // of keyring permissions. The command requires the "setattr" permission. | |
1477 // | |
1478 // When called with an empty keyType the command locks the keyring, preventing | |
1479 // any further keys from being linked to the keyring. | |
1480 // | |
1481 // The "asymmetric" keyType defines restrictions requiring key payloads to be | |
1482 // DER encoded X.509 certificates signed by keys in another keyring. Restrictions | |
1483 // for "asymmetric" include "builtin_trusted", "builtin_and_secondary_trusted", | |
1484 // "key_or_keyring:<key>", and "key_or_keyring:<key>:chain". | |
1485 // | |
1486 // As of Linux 4.12, only the "asymmetric" keyType defines type-specific | |
1487 // restrictions. | |
1488 // | |
1489 // See the full documentation at: | |
1490 // http://man7.org/linux/man-pages/man3/keyctl_restrict_keyring.3.html | |
1491 // http://man7.org/linux/man-pages/man2/keyctl.2.html | |
1492 func KeyctlRestrictKeyring(ringid int, keyType string, restriction string) error { | |
1493 if keyType == "" { | |
1494 return keyctlRestrictKeyring(KEYCTL_RESTRICT_KEYRING, ringid) | |
1495 } | |
1496 return keyctlRestrictKeyringByType(KEYCTL_RESTRICT_KEYRING, ringid, keyType, restriction) | |
1497 } | |
1498 | |
1499 //sys keyctlRestrictKeyringByType(cmd int, arg2 int, keyType string, restriction string) (err error) = SYS_KEYCTL | |
1500 //sys keyctlRestrictKeyring(cmd int, arg2 int) (err error) = SYS_KEYCTL | |
1501 | |
1502 func recvmsgRaw(fd int, iov []Iovec, oob []byte, flags int, rsa *RawSockaddrAny) (n, oobn int, recvflags int, err error) { | |
1503 var msg Msghdr | |
1504 msg.Name = (*byte)(unsafe.Pointer(rsa)) | |
1505 msg.Namelen = uint32(SizeofSockaddrAny) | |
1506 var dummy byte | |
1507 if len(oob) > 0 { | |
1508 if emptyIovecs(iov) { | |
1509 var sockType int | |
1510 sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE) | |
1511 if err != nil { | |
1512 return | |
1513 } | |
1514 // receive at least one normal byte | |
1515 if sockType != SOCK_DGRAM { | |
1516 var iova [1]Iovec | |
1517 iova[0].Base = &dummy | |
1518 iova[0].SetLen(1) | |
1519 iov = iova[:] | |
1520 } | |
1521 } | |
1522 msg.Control = &oob[0] | |
1523 msg.SetControllen(len(oob)) | |
1524 } | |
1525 if len(iov) > 0 { | |
1526 msg.Iov = &iov[0] | |
1527 msg.SetIovlen(len(iov)) | |
1528 } | |
1529 if n, err = recvmsg(fd, &msg, flags); err != nil { | |
1530 return | |
1531 } | |
1532 oobn = int(msg.Controllen) | |
1533 recvflags = int(msg.Flags) | |
1534 return | |
1535 } | |
1536 | |
1537 func sendmsgN(fd int, iov []Iovec, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) { | |
1538 var msg Msghdr | |
1539 msg.Name = (*byte)(ptr) | |
1540 msg.Namelen = uint32(salen) | |
1541 var dummy byte | |
1542 var empty bool | |
1543 if len(oob) > 0 { | |
1544 empty := emptyIovecs(iov) | |
1545 if empty { | |
1546 var sockType int | |
1547 sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE) | |
1548 if err != nil { | |
1549 return 0, err | |
1550 } | |
1551 // send at least one normal byte | |
1552 if sockType != SOCK_DGRAM { | |
1553 var iova [1]Iovec | |
1554 iova[0].Base = &dummy | |
1555 iova[0].SetLen(1) | |
1556 } | |
1557 } | |
1558 msg.Control = &oob[0] | |
1559 msg.SetControllen(len(oob)) | |
1560 } | |
1561 if len(iov) > 0 { | |
1562 msg.Iov = &iov[0] | |
1563 msg.SetIovlen(len(iov)) | |
1564 } | |
1565 if n, err = sendmsg(fd, &msg, flags); err != nil { | |
1566 return 0, err | |
1567 } | |
1568 if len(oob) > 0 && empty { | |
1569 n = 0 | |
1570 } | |
1571 return n, nil | |
1572 } | |
1573 | |
1574 // BindToDevice binds the socket associated with fd to device. | |
1575 func BindToDevice(fd int, device string) (err error) { | |
1576 return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device) | |
1577 } | |
1578 | |
1579 //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error) | |
1580 | |
1581 func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) { | |
1582 // The peek requests are machine-size oriented, so we wrap it | |
1583 // to retrieve arbitrary-length data. | |
1584 | |
1585 // The ptrace syscall differs from glibc's ptrace. | |
1586 // Peeks returns the word in *data, not as the return value. | |
1587 | |
1588 var buf [SizeofPtr]byte | |
1589 | |
1590 // Leading edge. PEEKTEXT/PEEKDATA don't require aligned | |
1591 // access (PEEKUSER warns that it might), but if we don't | |
1592 // align our reads, we might straddle an unmapped page | |
1593 // boundary and not get the bytes leading up to the page | |
1594 // boundary. | |
1595 n := 0 | |
1596 if addr%SizeofPtr != 0 { | |
1597 err = ptrace(req, pid, addr-addr%SizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) | |
1598 if err != nil { | |
1599 return 0, err | |
1600 } | |
1601 n += copy(out, buf[addr%SizeofPtr:]) | |
1602 out = out[n:] | |
1603 } | |
1604 | |
1605 // Remainder. | |
1606 for len(out) > 0 { | |
1607 // We use an internal buffer to guarantee alignment. | |
1608 // It's not documented if this is necessary, but we're paranoid. | |
1609 err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) | |
1610 if err != nil { | |
1611 return n, err | |
1612 } | |
1613 copied := copy(out, buf[0:]) | |
1614 n += copied | |
1615 out = out[copied:] | |
1616 } | |
1617 | |
1618 return n, nil | |
1619 } | |
1620 | |
1621 func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) { | |
1622 return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out) | |
1623 } | |
1624 | |
1625 func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) { | |
1626 return ptracePeek(PTRACE_PEEKDATA, pid, addr, out) | |
1627 } | |
1628 | |
1629 func PtracePeekUser(pid int, addr uintptr, out []byte) (count int, err error) { | |
1630 return ptracePeek(PTRACE_PEEKUSR, pid, addr, out) | |
1631 } | |
1632 | |
1633 func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) { | |
1634 // As for ptracePeek, we need to align our accesses to deal | |
1635 // with the possibility of straddling an invalid page. | |
1636 | |
1637 // Leading edge. | |
1638 n := 0 | |
1639 if addr%SizeofPtr != 0 { | |
1640 var buf [SizeofPtr]byte | |
1641 err = ptrace(peekReq, pid, addr-addr%SizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) | |
1642 if err != nil { | |
1643 return 0, err | |
1644 } | |
1645 n += copy(buf[addr%SizeofPtr:], data) | |
1646 word := *((*uintptr)(unsafe.Pointer(&buf[0]))) | |
1647 err = ptrace(pokeReq, pid, addr-addr%SizeofPtr, word) | |
1648 if err != nil { | |
1649 return 0, err | |
1650 } | |
1651 data = data[n:] | |
1652 } | |
1653 | |
1654 // Interior. | |
1655 for len(data) > SizeofPtr { | |
1656 word := *((*uintptr)(unsafe.Pointer(&data[0]))) | |
1657 err = ptrace(pokeReq, pid, addr+uintptr(n), word) | |
1658 if err != nil { | |
1659 return n, err | |
1660 } | |
1661 n += SizeofPtr | |
1662 data = data[SizeofPtr:] | |
1663 } | |
1664 | |
1665 // Trailing edge. | |
1666 if len(data) > 0 { | |
1667 var buf [SizeofPtr]byte | |
1668 err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) | |
1669 if err != nil { | |
1670 return n, err | |
1671 } | |
1672 copy(buf[0:], data) | |
1673 word := *((*uintptr)(unsafe.Pointer(&buf[0]))) | |
1674 err = ptrace(pokeReq, pid, addr+uintptr(n), word) | |
1675 if err != nil { | |
1676 return n, err | |
1677 } | |
1678 n += len(data) | |
1679 } | |
1680 | |
1681 return n, nil | |
1682 } | |
1683 | |
1684 func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) { | |
1685 return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data) | |
1686 } | |
1687 | |
1688 func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) { | |
1689 return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data) | |
1690 } | |
1691 | |
1692 func PtracePokeUser(pid int, addr uintptr, data []byte) (count int, err error) { | |
1693 return ptracePoke(PTRACE_POKEUSR, PTRACE_PEEKUSR, pid, addr, data) | |
1694 } | |
1695 | |
1696 func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) { | |
1697 return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout))) | |
1698 } | |
1699 | |
1700 func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) { | |
1701 return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs))) | |
1702 } | |
1703 | |
1704 func PtraceSetOptions(pid int, options int) (err error) { | |
1705 return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options)) | |
1706 } | |
1707 | |
1708 func PtraceGetEventMsg(pid int) (msg uint, err error) { | |
1709 var data _C_long | |
1710 err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data))) | |
1711 msg = uint(data) | |
1712 return | |
1713 } | |
1714 | |
1715 func PtraceCont(pid int, signal int) (err error) { | |
1716 return ptrace(PTRACE_CONT, pid, 0, uintptr(signal)) | |
1717 } | |
1718 | |
1719 func PtraceSyscall(pid int, signal int) (err error) { | |
1720 return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal)) | |
1721 } | |
1722 | |
1723 func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) } | |
1724 | |
1725 func PtraceInterrupt(pid int) (err error) { return ptrace(PTRACE_INTERRUPT, pid, 0, 0) } | |
1726 | |
1727 func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) } | |
1728 | |
1729 func PtraceSeize(pid int) (err error) { return ptrace(PTRACE_SEIZE, pid, 0, 0) } | |
1730 | |
1731 func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) } | |
1732 | |
1733 //sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error) | |
1734 | |
1735 func Reboot(cmd int) (err error) { | |
1736 return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "") | |
1737 } | |
1738 | |
1739 func direntIno(buf []byte) (uint64, bool) { | |
1740 return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino)) | |
1741 } | |
1742 | |
1743 func direntReclen(buf []byte) (uint64, bool) { | |
1744 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen)) | |
1745 } | |
1746 | |
1747 func direntNamlen(buf []byte) (uint64, bool) { | |
1748 reclen, ok := direntReclen(buf) | |
1749 if !ok { | |
1750 return 0, false | |
1751 } | |
1752 return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true | |
1753 } | |
1754 | |
1755 //sys mount(source string, target string, fstype string, flags uintptr, data *byte) (err error) | |
1756 | |
1757 func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) { | |
1758 // Certain file systems get rather angry and EINVAL if you give | |
1759 // them an empty string of data, rather than NULL. | |
1760 if data == "" { | |
1761 return mount(source, target, fstype, flags, nil) | |
1762 } | |
1763 datap, err := BytePtrFromString(data) | |
1764 if err != nil { | |
1765 return err | |
1766 } | |
1767 return mount(source, target, fstype, flags, datap) | |
1768 } | |
1769 | |
1770 //sys mountSetattr(dirfd int, pathname string, flags uint, attr *MountAttr, size uintptr) (err error) = SYS_MOUNT_SETATTR | |
1771 | |
1772 // MountSetattr is a wrapper for mount_setattr(2). | |
1773 // https://man7.org/linux/man-pages/man2/mount_setattr.2.html | |
1774 // | |
1775 // Requires kernel >= 5.12. | |
1776 func MountSetattr(dirfd int, pathname string, flags uint, attr *MountAttr) error { | |
1777 return mountSetattr(dirfd, pathname, flags, attr, unsafe.Sizeof(*attr)) | |
1778 } | |
1779 | |
1780 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { | |
1781 if raceenabled { | |
1782 raceReleaseMerge(unsafe.Pointer(&ioSync)) | |
1783 } | |
1784 return sendfile(outfd, infd, offset, count) | |
1785 } | |
1786 | |
1787 // Sendto | |
1788 // Recvfrom | |
1789 // Socketpair | |
1790 | |
1791 /* | |
1792 * Direct access | |
1793 */ | |
1794 //sys Acct(path string) (err error) | |
1795 //sys AddKey(keyType string, description string, payload []byte, ringid int) (id int, err error) | |
1796 //sys Adjtimex(buf *Timex) (state int, err error) | |
1797 //sysnb Capget(hdr *CapUserHeader, data *CapUserData) (err error) | |
1798 //sysnb Capset(hdr *CapUserHeader, data *CapUserData) (err error) | |
1799 //sys Chdir(path string) (err error) | |
1800 //sys Chroot(path string) (err error) | |
1801 //sys ClockGetres(clockid int32, res *Timespec) (err error) | |
1802 //sys ClockGettime(clockid int32, time *Timespec) (err error) | |
1803 //sys ClockNanosleep(clockid int32, flags int, request *Timespec, remain *Timespec) (err error) | |
1804 //sys Close(fd int) (err error) | |
1805 //sys CloseRange(first uint, last uint, flags uint) (err error) | |
1806 //sys CopyFileRange(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error) | |
1807 //sys DeleteModule(name string, flags int) (err error) | |
1808 //sys Dup(oldfd int) (fd int, err error) | |
1809 | |
1810 func Dup2(oldfd, newfd int) error { | |
1811 return Dup3(oldfd, newfd, 0) | |
1812 } | |
1813 | |
1814 //sys Dup3(oldfd int, newfd int, flags int) (err error) | |
1815 //sysnb EpollCreate1(flag int) (fd int, err error) | |
1816 //sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error) | |
1817 //sys Eventfd(initval uint, flags int) (fd int, err error) = SYS_EVENTFD2 | |
1818 //sys Exit(code int) = SYS_EXIT_GROUP | |
1819 //sys Fallocate(fd int, mode uint32, off int64, len int64) (err error) | |
1820 //sys Fchdir(fd int) (err error) | |
1821 //sys Fchmod(fd int, mode uint32) (err error) | |
1822 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) | |
1823 //sys Fdatasync(fd int) (err error) | |
1824 //sys Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) | |
1825 //sys FinitModule(fd int, params string, flags int) (err error) | |
1826 //sys Flistxattr(fd int, dest []byte) (sz int, err error) | |
1827 //sys Flock(fd int, how int) (err error) | |
1828 //sys Fremovexattr(fd int, attr string) (err error) | |
1829 //sys Fsetxattr(fd int, attr string, dest []byte, flags int) (err error) | |
1830 //sys Fsync(fd int) (err error) | |
1831 //sys Fsmount(fd int, flags int, mountAttrs int) (fsfd int, err error) | |
1832 //sys Fsopen(fsName string, flags int) (fd int, err error) | |
1833 //sys Fspick(dirfd int, pathName string, flags int) (fd int, err error) | |
1834 //sys Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64 | |
1835 //sysnb Getpgid(pid int) (pgid int, err error) | |
1836 | |
1837 func Getpgrp() (pid int) { | |
1838 pid, _ = Getpgid(0) | |
1839 return | |
1840 } | |
1841 | |
1842 //sysnb Getpid() (pid int) | |
1843 //sysnb Getppid() (ppid int) | |
1844 //sys Getpriority(which int, who int) (prio int, err error) | |
1845 //sys Getrandom(buf []byte, flags int) (n int, err error) | |
1846 //sysnb Getrusage(who int, rusage *Rusage) (err error) | |
1847 //sysnb Getsid(pid int) (sid int, err error) | |
1848 //sysnb Gettid() (tid int) | |
1849 //sys Getxattr(path string, attr string, dest []byte) (sz int, err error) | |
1850 //sys InitModule(moduleImage []byte, params string) (err error) | |
1851 //sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error) | |
1852 //sysnb InotifyInit1(flags int) (fd int, err error) | |
1853 //sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error) | |
1854 //sysnb Kill(pid int, sig syscall.Signal) (err error) | |
1855 //sys Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG | |
1856 //sys Lgetxattr(path string, attr string, dest []byte) (sz int, err error) | |
1857 //sys Listxattr(path string, dest []byte) (sz int, err error) | |
1858 //sys Llistxattr(path string, dest []byte) (sz int, err error) | |
1859 //sys Lremovexattr(path string, attr string) (err error) | |
1860 //sys Lsetxattr(path string, attr string, data []byte, flags int) (err error) | |
1861 //sys MemfdCreate(name string, flags int) (fd int, err error) | |
1862 //sys Mkdirat(dirfd int, path string, mode uint32) (err error) | |
1863 //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error) | |
1864 //sys MoveMount(fromDirfd int, fromPathName string, toDirfd int, toPathName string, flags int) (err error) | |
1865 //sys Nanosleep(time *Timespec, leftover *Timespec) (err error) | |
1866 //sys OpenTree(dfd int, fileName string, flags uint) (r int, err error) | |
1867 //sys PerfEventOpen(attr *PerfEventAttr, pid int, cpu int, groupFd int, flags int) (fd int, err error) | |
1868 //sys PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT | |
1869 //sysnb Prlimit(pid int, resource int, newlimit *Rlimit, old *Rlimit) (err error) = SYS_PRLIMIT64 | |
1870 //sys Prctl(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (err error) | |
1871 //sys Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error) = SYS_PSELECT6 | |
1872 //sys read(fd int, p []byte) (n int, err error) | |
1873 //sys Removexattr(path string, attr string) (err error) | |
1874 //sys Renameat2(olddirfd int, oldpath string, newdirfd int, newpath string, flags uint) (err error) | |
1875 //sys RequestKey(keyType string, description string, callback string, destRingid int) (id int, err error) | |
1876 //sys Setdomainname(p []byte) (err error) | |
1877 //sys Sethostname(p []byte) (err error) | |
1878 //sysnb Setpgid(pid int, pgid int) (err error) | |
1879 //sysnb Setsid() (pid int, err error) | |
1880 //sysnb Settimeofday(tv *Timeval) (err error) | |
1881 //sys Setns(fd int, nstype int) (err error) | |
1882 | |
1883 // PrctlRetInt performs a prctl operation specified by option and further | |
1884 // optional arguments arg2 through arg5 depending on option. It returns a | |
1885 // non-negative integer that is returned by the prctl syscall. | |
1886 func PrctlRetInt(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (int, error) { | |
1887 ret, _, err := Syscall6(SYS_PRCTL, uintptr(option), uintptr(arg2), uintptr(arg3), uintptr(arg4), uintptr(arg5), 0) | |
1888 if err != 0 { | |
1889 return 0, err | |
1890 } | |
1891 return int(ret), nil | |
1892 } | |
1893 | |
1894 // issue 1435. | |
1895 // On linux Setuid and Setgid only affects the current thread, not the process. | |
1896 // This does not match what most callers expect so we must return an error | |
1897 // here rather than letting the caller think that the call succeeded. | |
1898 | |
1899 func Setuid(uid int) (err error) { | |
1900 return EOPNOTSUPP | |
1901 } | |
1902 | |
1903 func Setgid(uid int) (err error) { | |
1904 return EOPNOTSUPP | |
1905 } | |
1906 | |
1907 // SetfsgidRetGid sets fsgid for current thread and returns previous fsgid set. | |
1908 // setfsgid(2) will return a non-nil error only if its caller lacks CAP_SETUID capability. | |
1909 // If the call fails due to other reasons, current fsgid will be returned. | |
1910 func SetfsgidRetGid(gid int) (int, error) { | |
1911 return setfsgid(gid) | |
1912 } | |
1913 | |
1914 // SetfsuidRetUid sets fsuid for current thread and returns previous fsuid set. | |
1915 // setfsgid(2) will return a non-nil error only if its caller lacks CAP_SETUID capability | |
1916 // If the call fails due to other reasons, current fsuid will be returned. | |
1917 func SetfsuidRetUid(uid int) (int, error) { | |
1918 return setfsuid(uid) | |
1919 } | |
1920 | |
1921 func Setfsgid(gid int) error { | |
1922 _, err := setfsgid(gid) | |
1923 return err | |
1924 } | |
1925 | |
1926 func Setfsuid(uid int) error { | |
1927 _, err := setfsuid(uid) | |
1928 return err | |
1929 } | |
1930 | |
1931 func Signalfd(fd int, sigmask *Sigset_t, flags int) (newfd int, err error) { | |
1932 return signalfd(fd, sigmask, _C__NSIG/8, flags) | |
1933 } | |
1934 | |
1935 //sys Setpriority(which int, who int, prio int) (err error) | |
1936 //sys Setxattr(path string, attr string, data []byte, flags int) (err error) | |
1937 //sys signalfd(fd int, sigmask *Sigset_t, maskSize uintptr, flags int) (newfd int, err error) = SYS_SIGNALFD4 | |
1938 //sys Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error) | |
1939 //sys Sync() | |
1940 //sys Syncfs(fd int) (err error) | |
1941 //sysnb Sysinfo(info *Sysinfo_t) (err error) | |
1942 //sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error) | |
1943 //sysnb TimerfdCreate(clockid int, flags int) (fd int, err error) | |
1944 //sysnb TimerfdGettime(fd int, currValue *ItimerSpec) (err error) | |
1945 //sysnb TimerfdSettime(fd int, flags int, newValue *ItimerSpec, oldValue *ItimerSpec) (err error) | |
1946 //sysnb Tgkill(tgid int, tid int, sig syscall.Signal) (err error) | |
1947 //sysnb Times(tms *Tms) (ticks uintptr, err error) | |
1948 //sysnb Umask(mask int) (oldmask int) | |
1949 //sysnb Uname(buf *Utsname) (err error) | |
1950 //sys Unmount(target string, flags int) (err error) = SYS_UMOUNT2 | |
1951 //sys Unshare(flags int) (err error) | |
1952 //sys write(fd int, p []byte) (n int, err error) | |
1953 //sys exitThread(code int) (err error) = SYS_EXIT | |
1954 //sys readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ | |
1955 //sys writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE | |
1956 //sys readv(fd int, iovs []Iovec) (n int, err error) = SYS_READV | |
1957 //sys writev(fd int, iovs []Iovec) (n int, err error) = SYS_WRITEV | |
1958 //sys preadv(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr) (n int, err error) = SYS_PREADV | |
1959 //sys pwritev(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr) (n int, err error) = SYS_PWRITEV | |
1960 //sys preadv2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PREADV2 | |
1961 //sys pwritev2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PWRITEV2 | |
1962 | |
1963 func bytes2iovec(bs [][]byte) []Iovec { | |
1964 iovecs := make([]Iovec, len(bs)) | |
1965 for i, b := range bs { | |
1966 iovecs[i].SetLen(len(b)) | |
1967 if len(b) > 0 { | |
1968 iovecs[i].Base = &b[0] | |
1969 } else { | |
1970 iovecs[i].Base = (*byte)(unsafe.Pointer(&_zero)) | |
1971 } | |
1972 } | |
1973 return iovecs | |
1974 } | |
1975 | |
1976 // offs2lohi splits offs into its lower and upper unsigned long. On 64-bit | |
1977 // systems, hi will always be 0. On 32-bit systems, offs will be split in half. | |
1978 // preadv/pwritev chose this calling convention so they don't need to add a | |
1979 // padding-register for alignment on ARM. | |
1980 func offs2lohi(offs int64) (lo, hi uintptr) { | |
1981 return uintptr(offs), uintptr(uint64(offs) >> SizeofLong) | |
1982 } | |
1983 | |
1984 func Readv(fd int, iovs [][]byte) (n int, err error) { | |
1985 iovecs := bytes2iovec(iovs) | |
1986 n, err = readv(fd, iovecs) | |
1987 readvRacedetect(iovecs, n, err) | |
1988 return n, err | |
1989 } | |
1990 | |
1991 func Preadv(fd int, iovs [][]byte, offset int64) (n int, err error) { | |
1992 iovecs := bytes2iovec(iovs) | |
1993 lo, hi := offs2lohi(offset) | |
1994 n, err = preadv(fd, iovecs, lo, hi) | |
1995 readvRacedetect(iovecs, n, err) | |
1996 return n, err | |
1997 } | |
1998 | |
1999 func Preadv2(fd int, iovs [][]byte, offset int64, flags int) (n int, err error) { | |
2000 iovecs := bytes2iovec(iovs) | |
2001 lo, hi := offs2lohi(offset) | |
2002 n, err = preadv2(fd, iovecs, lo, hi, flags) | |
2003 readvRacedetect(iovecs, n, err) | |
2004 return n, err | |
2005 } | |
2006 | |
2007 func readvRacedetect(iovecs []Iovec, n int, err error) { | |
2008 if !raceenabled { | |
2009 return | |
2010 } | |
2011 for i := 0; n > 0 && i < len(iovecs); i++ { | |
2012 m := int(iovecs[i].Len) | |
2013 if m > n { | |
2014 m = n | |
2015 } | |
2016 n -= m | |
2017 if m > 0 { | |
2018 raceWriteRange(unsafe.Pointer(iovecs[i].Base), m) | |
2019 } | |
2020 } | |
2021 if err == nil { | |
2022 raceAcquire(unsafe.Pointer(&ioSync)) | |
2023 } | |
2024 } | |
2025 | |
2026 func Writev(fd int, iovs [][]byte) (n int, err error) { | |
2027 iovecs := bytes2iovec(iovs) | |
2028 if raceenabled { | |
2029 raceReleaseMerge(unsafe.Pointer(&ioSync)) | |
2030 } | |
2031 n, err = writev(fd, iovecs) | |
2032 writevRacedetect(iovecs, n) | |
2033 return n, err | |
2034 } | |
2035 | |
2036 func Pwritev(fd int, iovs [][]byte, offset int64) (n int, err error) { | |
2037 iovecs := bytes2iovec(iovs) | |
2038 if raceenabled { | |
2039 raceReleaseMerge(unsafe.Pointer(&ioSync)) | |
2040 } | |
2041 lo, hi := offs2lohi(offset) | |
2042 n, err = pwritev(fd, iovecs, lo, hi) | |
2043 writevRacedetect(iovecs, n) | |
2044 return n, err | |
2045 } | |
2046 | |
2047 func Pwritev2(fd int, iovs [][]byte, offset int64, flags int) (n int, err error) { | |
2048 iovecs := bytes2iovec(iovs) | |
2049 if raceenabled { | |
2050 raceReleaseMerge(unsafe.Pointer(&ioSync)) | |
2051 } | |
2052 lo, hi := offs2lohi(offset) | |
2053 n, err = pwritev2(fd, iovecs, lo, hi, flags) | |
2054 writevRacedetect(iovecs, n) | |
2055 return n, err | |
2056 } | |
2057 | |
2058 func writevRacedetect(iovecs []Iovec, n int) { | |
2059 if !raceenabled { | |
2060 return | |
2061 } | |
2062 for i := 0; n > 0 && i < len(iovecs); i++ { | |
2063 m := int(iovecs[i].Len) | |
2064 if m > n { | |
2065 m = n | |
2066 } | |
2067 n -= m | |
2068 if m > 0 { | |
2069 raceReadRange(unsafe.Pointer(iovecs[i].Base), m) | |
2070 } | |
2071 } | |
2072 } | |
2073 | |
2074 // mmap varies by architecture; see syscall_linux_*.go. | |
2075 //sys munmap(addr uintptr, length uintptr) (err error) | |
2076 | |
2077 var mapper = &mmapper{ | |
2078 active: make(map[*byte][]byte), | |
2079 mmap: mmap, | |
2080 munmap: munmap, | |
2081 } | |
2082 | |
2083 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) { | |
2084 return mapper.Mmap(fd, offset, length, prot, flags) | |
2085 } | |
2086 | |
2087 func Munmap(b []byte) (err error) { | |
2088 return mapper.Munmap(b) | |
2089 } | |
2090 | |
2091 //sys Madvise(b []byte, advice int) (err error) | |
2092 //sys Mprotect(b []byte, prot int) (err error) | |
2093 //sys Mlock(b []byte) (err error) | |
2094 //sys Mlockall(flags int) (err error) | |
2095 //sys Msync(b []byte, flags int) (err error) | |
2096 //sys Munlock(b []byte) (err error) | |
2097 //sys Munlockall() (err error) | |
2098 | |
2099 // Vmsplice splices user pages from a slice of Iovecs into a pipe specified by fd, | |
2100 // using the specified flags. | |
2101 func Vmsplice(fd int, iovs []Iovec, flags int) (int, error) { | |
2102 var p unsafe.Pointer | |
2103 if len(iovs) > 0 { | |
2104 p = unsafe.Pointer(&iovs[0]) | |
2105 } | |
2106 | |
2107 n, _, errno := Syscall6(SYS_VMSPLICE, uintptr(fd), uintptr(p), uintptr(len(iovs)), uintptr(flags), 0, 0) | |
2108 if errno != 0 { | |
2109 return 0, syscall.Errno(errno) | |
2110 } | |
2111 | |
2112 return int(n), nil | |
2113 } | |
2114 | |
2115 func isGroupMember(gid int) bool { | |
2116 groups, err := Getgroups() | |
2117 if err != nil { | |
2118 return false | |
2119 } | |
2120 | |
2121 for _, g := range groups { | |
2122 if g == gid { | |
2123 return true | |
2124 } | |
2125 } | |
2126 return false | |
2127 } | |
2128 | |
2129 //sys faccessat(dirfd int, path string, mode uint32) (err error) | |
2130 //sys Faccessat2(dirfd int, path string, mode uint32, flags int) (err error) | |
2131 | |
2132 func Faccessat(dirfd int, path string, mode uint32, flags int) (err error) { | |
2133 if flags == 0 { | |
2134 return faccessat(dirfd, path, mode) | |
2135 } | |
2136 | |
2137 if err := Faccessat2(dirfd, path, mode, flags); err != ENOSYS && err != EPERM { | |
2138 return err | |
2139 } | |
2140 | |
2141 // The Linux kernel faccessat system call does not take any flags. | |
2142 // The glibc faccessat implements the flags itself; see | |
2143 // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/faccessat.c;hb=HEAD | |
2144 // Because people naturally expect syscall.Faccessat to act | |
2145 // like C faccessat, we do the same. | |
2146 | |
2147 if flags & ^(AT_SYMLINK_NOFOLLOW|AT_EACCESS) != 0 { | |
2148 return EINVAL | |
2149 } | |
2150 | |
2151 var st Stat_t | |
2152 if err := Fstatat(dirfd, path, &st, flags&AT_SYMLINK_NOFOLLOW); err != nil { | |
2153 return err | |
2154 } | |
2155 | |
2156 mode &= 7 | |
2157 if mode == 0 { | |
2158 return nil | |
2159 } | |
2160 | |
2161 var uid int | |
2162 if flags&AT_EACCESS != 0 { | |
2163 uid = Geteuid() | |
2164 } else { | |
2165 uid = Getuid() | |
2166 } | |
2167 | |
2168 if uid == 0 { | |
2169 if mode&1 == 0 { | |
2170 // Root can read and write any file. | |
2171 return nil | |
2172 } | |
2173 if st.Mode&0111 != 0 { | |
2174 // Root can execute any file that anybody can execute. | |
2175 return nil | |
2176 } | |
2177 return EACCES | |
2178 } | |
2179 | |
2180 var fmode uint32 | |
2181 if uint32(uid) == st.Uid { | |
2182 fmode = (st.Mode >> 6) & 7 | |
2183 } else { | |
2184 var gid int | |
2185 if flags&AT_EACCESS != 0 { | |
2186 gid = Getegid() | |
2187 } else { | |
2188 gid = Getgid() | |
2189 } | |
2190 | |
2191 if uint32(gid) == st.Gid || isGroupMember(int(st.Gid)) { | |
2192 fmode = (st.Mode >> 3) & 7 | |
2193 } else { | |
2194 fmode = st.Mode & 7 | |
2195 } | |
2196 } | |
2197 | |
2198 if fmode&mode == mode { | |
2199 return nil | |
2200 } | |
2201 | |
2202 return EACCES | |
2203 } | |
2204 | |
2205 //sys nameToHandleAt(dirFD int, pathname string, fh *fileHandle, mountID *_C_int, flags int) (err error) = SYS_NAME_TO_HANDLE_AT | |
2206 //sys openByHandleAt(mountFD int, fh *fileHandle, flags int) (fd int, err error) = SYS_OPEN_BY_HANDLE_AT | |
2207 | |
2208 // fileHandle is the argument to nameToHandleAt and openByHandleAt. We | |
2209 // originally tried to generate it via unix/linux/types.go with "type | |
2210 // fileHandle C.struct_file_handle" but that generated empty structs | |
2211 // for mips64 and mips64le. Instead, hard code it for now (it's the | |
2212 // same everywhere else) until the mips64 generator issue is fixed. | |
2213 type fileHandle struct { | |
2214 Bytes uint32 | |
2215 Type int32 | |
2216 } | |
2217 | |
2218 // FileHandle represents the C struct file_handle used by | |
2219 // name_to_handle_at (see NameToHandleAt) and open_by_handle_at (see | |
2220 // OpenByHandleAt). | |
2221 type FileHandle struct { | |
2222 *fileHandle | |
2223 } | |
2224 | |
2225 // NewFileHandle constructs a FileHandle. | |
2226 func NewFileHandle(handleType int32, handle []byte) FileHandle { | |
2227 const hdrSize = unsafe.Sizeof(fileHandle{}) | |
2228 buf := make([]byte, hdrSize+uintptr(len(handle))) | |
2229 copy(buf[hdrSize:], handle) | |
2230 fh := (*fileHandle)(unsafe.Pointer(&buf[0])) | |
2231 fh.Type = handleType | |
2232 fh.Bytes = uint32(len(handle)) | |
2233 return FileHandle{fh} | |
2234 } | |
2235 | |
2236 func (fh *FileHandle) Size() int { return int(fh.fileHandle.Bytes) } | |
2237 func (fh *FileHandle) Type() int32 { return fh.fileHandle.Type } | |
2238 func (fh *FileHandle) Bytes() []byte { | |
2239 n := fh.Size() | |
2240 if n == 0 { | |
2241 return nil | |
2242 } | |
2243 return (*[1 << 30]byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&fh.fileHandle.Type)) + 4))[:n:n] | |
2244 } | |
2245 | |
2246 // NameToHandleAt wraps the name_to_handle_at system call; it obtains | |
2247 // a handle for a path name. | |
2248 func NameToHandleAt(dirfd int, path string, flags int) (handle FileHandle, mountID int, err error) { | |
2249 var mid _C_int | |
2250 // Try first with a small buffer, assuming the handle will | |
2251 // only be 32 bytes. | |
2252 size := uint32(32 + unsafe.Sizeof(fileHandle{})) | |
2253 didResize := false | |
2254 for { | |
2255 buf := make([]byte, size) | |
2256 fh := (*fileHandle)(unsafe.Pointer(&buf[0])) | |
2257 fh.Bytes = size - uint32(unsafe.Sizeof(fileHandle{})) | |
2258 err = nameToHandleAt(dirfd, path, fh, &mid, flags) | |
2259 if err == EOVERFLOW { | |
2260 if didResize { | |
2261 // We shouldn't need to resize more than once | |
2262 return | |
2263 } | |
2264 didResize = true | |
2265 size = fh.Bytes + uint32(unsafe.Sizeof(fileHandle{})) | |
2266 continue | |
2267 } | |
2268 if err != nil { | |
2269 return | |
2270 } | |
2271 return FileHandle{fh}, int(mid), nil | |
2272 } | |
2273 } | |
2274 | |
2275 // OpenByHandleAt wraps the open_by_handle_at system call; it opens a | |
2276 // file via a handle as previously returned by NameToHandleAt. | |
2277 func OpenByHandleAt(mountFD int, handle FileHandle, flags int) (fd int, err error) { | |
2278 return openByHandleAt(mountFD, handle.fileHandle, flags) | |
2279 } | |
2280 | |
2281 // Klogset wraps the sys_syslog system call; it sets console_loglevel to | |
2282 // the value specified by arg and passes a dummy pointer to bufp. | |
2283 func Klogset(typ int, arg int) (err error) { | |
2284 var p unsafe.Pointer | |
2285 _, _, errno := Syscall(SYS_SYSLOG, uintptr(typ), uintptr(p), uintptr(arg)) | |
2286 if errno != 0 { | |
2287 return errnoErr(errno) | |
2288 } | |
2289 return nil | |
2290 } | |
2291 | |
2292 // RemoteIovec is Iovec with the pointer replaced with an integer. | |
2293 // It is used for ProcessVMReadv and ProcessVMWritev, where the pointer | |
2294 // refers to a location in a different process' address space, which | |
2295 // would confuse the Go garbage collector. | |
2296 type RemoteIovec struct { | |
2297 Base uintptr | |
2298 Len int | |
2299 } | |
2300 | |
2301 //sys ProcessVMReadv(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_READV | |
2302 //sys ProcessVMWritev(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_WRITEV | |
2303 | |
2304 //sys PidfdOpen(pid int, flags int) (fd int, err error) = SYS_PIDFD_OPEN | |
2305 //sys PidfdGetfd(pidfd int, targetfd int, flags int) (fd int, err error) = SYS_PIDFD_GETFD | |
2306 //sys PidfdSendSignal(pidfd int, sig Signal, info *Siginfo, flags int) (err error) = SYS_PIDFD_SEND_SIGNAL | |
2307 | |
2308 //sys shmat(id int, addr uintptr, flag int) (ret uintptr, err error) | |
2309 //sys shmctl(id int, cmd int, buf *SysvShmDesc) (result int, err error) | |
2310 //sys shmdt(addr uintptr) (err error) | |
2311 //sys shmget(key int, size int, flag int) (id int, err error) | |
2312 | |
2313 //sys getitimer(which int, currValue *Itimerval) (err error) | |
2314 //sys setitimer(which int, newValue *Itimerval, oldValue *Itimerval) (err error) | |
2315 | |
2316 // MakeItimerval creates an Itimerval from interval and value durations. | |
2317 func MakeItimerval(interval, value time.Duration) Itimerval { | |
2318 return Itimerval{ | |
2319 Interval: NsecToTimeval(interval.Nanoseconds()), | |
2320 Value: NsecToTimeval(value.Nanoseconds()), | |
2321 } | |
2322 } | |
2323 | |
2324 // A value which may be passed to the which parameter for Getitimer and | |
2325 // Setitimer. | |
2326 type ItimerWhich int | |
2327 | |
2328 // Possible which values for Getitimer and Setitimer. | |
2329 const ( | |
2330 ItimerReal ItimerWhich = ITIMER_REAL | |
2331 ItimerVirtual ItimerWhich = ITIMER_VIRTUAL | |
2332 ItimerProf ItimerWhich = ITIMER_PROF | |
2333 ) | |
2334 | |
2335 // Getitimer wraps getitimer(2) to return the current value of the timer | |
2336 // specified by which. | |
2337 func Getitimer(which ItimerWhich) (Itimerval, error) { | |
2338 var it Itimerval | |
2339 if err := getitimer(int(which), &it); err != nil { | |
2340 return Itimerval{}, err | |
2341 } | |
2342 | |
2343 return it, nil | |
2344 } | |
2345 | |
2346 // Setitimer wraps setitimer(2) to arm or disarm the timer specified by which. | |
2347 // It returns the previous value of the timer. | |
2348 // | |
2349 // If the Itimerval argument is the zero value, the timer will be disarmed. | |
2350 func Setitimer(which ItimerWhich, it Itimerval) (Itimerval, error) { | |
2351 var prev Itimerval | |
2352 if err := setitimer(int(which), &it, &prev); err != nil { | |
2353 return Itimerval{}, err | |
2354 } | |
2355 | |
2356 return prev, nil | |
2357 } | |
2358 | |
2359 /* | |
2360 * Unimplemented | |
2361 */ | |
2362 // AfsSyscall | |
2363 // ArchPrctl | |
2364 // Brk | |
2365 // ClockNanosleep | |
2366 // ClockSettime | |
2367 // Clone | |
2368 // EpollCtlOld | |
2369 // EpollPwait | |
2370 // EpollWaitOld | |
2371 // Execve | |
2372 // Fork | |
2373 // Futex | |
2374 // GetKernelSyms | |
2375 // GetMempolicy | |
2376 // GetRobustList | |
2377 // GetThreadArea | |
2378 // Getpmsg | |
2379 // IoCancel | |
2380 // IoDestroy | |
2381 // IoGetevents | |
2382 // IoSetup | |
2383 // IoSubmit | |
2384 // IoprioGet | |
2385 // IoprioSet | |
2386 // KexecLoad | |
2387 // LookupDcookie | |
2388 // Mbind | |
2389 // MigratePages | |
2390 // Mincore | |
2391 // ModifyLdt | |
2392 // Mount | |
2393 // MovePages | |
2394 // MqGetsetattr | |
2395 // MqNotify | |
2396 // MqOpen | |
2397 // MqTimedreceive | |
2398 // MqTimedsend | |
2399 // MqUnlink | |
2400 // Mremap | |
2401 // Msgctl | |
2402 // Msgget | |
2403 // Msgrcv | |
2404 // Msgsnd | |
2405 // Nfsservctl | |
2406 // Personality | |
2407 // Pselect6 | |
2408 // Ptrace | |
2409 // Putpmsg | |
2410 // Quotactl | |
2411 // Readahead | |
2412 // Readv | |
2413 // RemapFilePages | |
2414 // RestartSyscall | |
2415 // RtSigaction | |
2416 // RtSigpending | |
2417 // RtSigprocmask | |
2418 // RtSigqueueinfo | |
2419 // RtSigreturn | |
2420 // RtSigsuspend | |
2421 // RtSigtimedwait | |
2422 // SchedGetPriorityMax | |
2423 // SchedGetPriorityMin | |
2424 // SchedGetparam | |
2425 // SchedGetscheduler | |
2426 // SchedRrGetInterval | |
2427 // SchedSetparam | |
2428 // SchedYield | |
2429 // Security | |
2430 // Semctl | |
2431 // Semget | |
2432 // Semop | |
2433 // Semtimedop | |
2434 // SetMempolicy | |
2435 // SetRobustList | |
2436 // SetThreadArea | |
2437 // SetTidAddress | |
2438 // Sigaltstack | |
2439 // Swapoff | |
2440 // Swapon | |
2441 // Sysfs | |
2442 // TimerCreate | |
2443 // TimerDelete | |
2444 // TimerGetoverrun | |
2445 // TimerGettime | |
2446 // TimerSettime | |
2447 // Tkill (obsolete) | |
2448 // Tuxcall | |
2449 // Umount2 | |
2450 // Uselib | |
2451 // Utimensat | |
2452 // Vfork | |
2453 // Vhangup | |
2454 // Vserver | |
2455 // _Sysctl |