usb-ks

USB Killswitch
git clone git://git.laack.co/usb-ks.git
Log | Files | Refs | README | LICENSE

syscall_linux.go (72891B)


      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, p, 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 iov Iovec
   1507 	if len(p) > 0 {
   1508 		iov.Base = &p[0]
   1509 		iov.SetLen(len(p))
   1510 	}
   1511 	var dummy byte
   1512 	if len(oob) > 0 {
   1513 		if len(p) == 0 {
   1514 			var sockType int
   1515 			sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
   1516 			if err != nil {
   1517 				return
   1518 			}
   1519 			// receive at least one normal byte
   1520 			if sockType != SOCK_DGRAM {
   1521 				iov.Base = &dummy
   1522 				iov.SetLen(1)
   1523 			}
   1524 		}
   1525 		msg.Control = &oob[0]
   1526 		msg.SetControllen(len(oob))
   1527 	}
   1528 	msg.Iov = &iov
   1529 	msg.Iovlen = 1
   1530 	if n, err = recvmsg(fd, &msg, flags); err != nil {
   1531 		return
   1532 	}
   1533 	oobn = int(msg.Controllen)
   1534 	recvflags = int(msg.Flags)
   1535 	return
   1536 }
   1537 
   1538 func sendmsgN(fd int, p, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) {
   1539 	var msg Msghdr
   1540 	msg.Name = (*byte)(ptr)
   1541 	msg.Namelen = uint32(salen)
   1542 	var iov Iovec
   1543 	if len(p) > 0 {
   1544 		iov.Base = &p[0]
   1545 		iov.SetLen(len(p))
   1546 	}
   1547 	var dummy byte
   1548 	if len(oob) > 0 {
   1549 		if len(p) == 0 {
   1550 			var sockType int
   1551 			sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
   1552 			if err != nil {
   1553 				return 0, err
   1554 			}
   1555 			// send at least one normal byte
   1556 			if sockType != SOCK_DGRAM {
   1557 				iov.Base = &dummy
   1558 				iov.SetLen(1)
   1559 			}
   1560 		}
   1561 		msg.Control = &oob[0]
   1562 		msg.SetControllen(len(oob))
   1563 	}
   1564 	msg.Iov = &iov
   1565 	msg.Iovlen = 1
   1566 	if n, err = sendmsg(fd, &msg, flags); err != nil {
   1567 		return 0, err
   1568 	}
   1569 	if len(oob) > 0 && len(p) == 0 {
   1570 		n = 0
   1571 	}
   1572 	return n, nil
   1573 }
   1574 
   1575 // BindToDevice binds the socket associated with fd to device.
   1576 func BindToDevice(fd int, device string) (err error) {
   1577 	return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
   1578 }
   1579 
   1580 //sys	ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
   1581 
   1582 func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) {
   1583 	// The peek requests are machine-size oriented, so we wrap it
   1584 	// to retrieve arbitrary-length data.
   1585 
   1586 	// The ptrace syscall differs from glibc's ptrace.
   1587 	// Peeks returns the word in *data, not as the return value.
   1588 
   1589 	var buf [SizeofPtr]byte
   1590 
   1591 	// Leading edge. PEEKTEXT/PEEKDATA don't require aligned
   1592 	// access (PEEKUSER warns that it might), but if we don't
   1593 	// align our reads, we might straddle an unmapped page
   1594 	// boundary and not get the bytes leading up to the page
   1595 	// boundary.
   1596 	n := 0
   1597 	if addr%SizeofPtr != 0 {
   1598 		err = ptrace(req, pid, addr-addr%SizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
   1599 		if err != nil {
   1600 			return 0, err
   1601 		}
   1602 		n += copy(out, buf[addr%SizeofPtr:])
   1603 		out = out[n:]
   1604 	}
   1605 
   1606 	// Remainder.
   1607 	for len(out) > 0 {
   1608 		// We use an internal buffer to guarantee alignment.
   1609 		// It's not documented if this is necessary, but we're paranoid.
   1610 		err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
   1611 		if err != nil {
   1612 			return n, err
   1613 		}
   1614 		copied := copy(out, buf[0:])
   1615 		n += copied
   1616 		out = out[copied:]
   1617 	}
   1618 
   1619 	return n, nil
   1620 }
   1621 
   1622 func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) {
   1623 	return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out)
   1624 }
   1625 
   1626 func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
   1627 	return ptracePeek(PTRACE_PEEKDATA, pid, addr, out)
   1628 }
   1629 
   1630 func PtracePeekUser(pid int, addr uintptr, out []byte) (count int, err error) {
   1631 	return ptracePeek(PTRACE_PEEKUSR, pid, addr, out)
   1632 }
   1633 
   1634 func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) {
   1635 	// As for ptracePeek, we need to align our accesses to deal
   1636 	// with the possibility of straddling an invalid page.
   1637 
   1638 	// Leading edge.
   1639 	n := 0
   1640 	if addr%SizeofPtr != 0 {
   1641 		var buf [SizeofPtr]byte
   1642 		err = ptrace(peekReq, pid, addr-addr%SizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
   1643 		if err != nil {
   1644 			return 0, err
   1645 		}
   1646 		n += copy(buf[addr%SizeofPtr:], data)
   1647 		word := *((*uintptr)(unsafe.Pointer(&buf[0])))
   1648 		err = ptrace(pokeReq, pid, addr-addr%SizeofPtr, word)
   1649 		if err != nil {
   1650 			return 0, err
   1651 		}
   1652 		data = data[n:]
   1653 	}
   1654 
   1655 	// Interior.
   1656 	for len(data) > SizeofPtr {
   1657 		word := *((*uintptr)(unsafe.Pointer(&data[0])))
   1658 		err = ptrace(pokeReq, pid, addr+uintptr(n), word)
   1659 		if err != nil {
   1660 			return n, err
   1661 		}
   1662 		n += SizeofPtr
   1663 		data = data[SizeofPtr:]
   1664 	}
   1665 
   1666 	// Trailing edge.
   1667 	if len(data) > 0 {
   1668 		var buf [SizeofPtr]byte
   1669 		err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
   1670 		if err != nil {
   1671 			return n, err
   1672 		}
   1673 		copy(buf[0:], data)
   1674 		word := *((*uintptr)(unsafe.Pointer(&buf[0])))
   1675 		err = ptrace(pokeReq, pid, addr+uintptr(n), word)
   1676 		if err != nil {
   1677 			return n, err
   1678 		}
   1679 		n += len(data)
   1680 	}
   1681 
   1682 	return n, nil
   1683 }
   1684 
   1685 func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
   1686 	return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data)
   1687 }
   1688 
   1689 func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) {
   1690 	return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data)
   1691 }
   1692 
   1693 func PtracePokeUser(pid int, addr uintptr, data []byte) (count int, err error) {
   1694 	return ptracePoke(PTRACE_POKEUSR, PTRACE_PEEKUSR, pid, addr, data)
   1695 }
   1696 
   1697 func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) {
   1698 	return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout)))
   1699 }
   1700 
   1701 func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) {
   1702 	return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs)))
   1703 }
   1704 
   1705 func PtraceSetOptions(pid int, options int) (err error) {
   1706 	return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options))
   1707 }
   1708 
   1709 func PtraceGetEventMsg(pid int) (msg uint, err error) {
   1710 	var data _C_long
   1711 	err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data)))
   1712 	msg = uint(data)
   1713 	return
   1714 }
   1715 
   1716 func PtraceCont(pid int, signal int) (err error) {
   1717 	return ptrace(PTRACE_CONT, pid, 0, uintptr(signal))
   1718 }
   1719 
   1720 func PtraceSyscall(pid int, signal int) (err error) {
   1721 	return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal))
   1722 }
   1723 
   1724 func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) }
   1725 
   1726 func PtraceInterrupt(pid int) (err error) { return ptrace(PTRACE_INTERRUPT, pid, 0, 0) }
   1727 
   1728 func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) }
   1729 
   1730 func PtraceSeize(pid int) (err error) { return ptrace(PTRACE_SEIZE, pid, 0, 0) }
   1731 
   1732 func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) }
   1733 
   1734 //sys	reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error)
   1735 
   1736 func Reboot(cmd int) (err error) {
   1737 	return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "")
   1738 }
   1739 
   1740 func direntIno(buf []byte) (uint64, bool) {
   1741 	return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
   1742 }
   1743 
   1744 func direntReclen(buf []byte) (uint64, bool) {
   1745 	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
   1746 }
   1747 
   1748 func direntNamlen(buf []byte) (uint64, bool) {
   1749 	reclen, ok := direntReclen(buf)
   1750 	if !ok {
   1751 		return 0, false
   1752 	}
   1753 	return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
   1754 }
   1755 
   1756 //sys	mount(source string, target string, fstype string, flags uintptr, data *byte) (err error)
   1757 
   1758 func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) {
   1759 	// Certain file systems get rather angry and EINVAL if you give
   1760 	// them an empty string of data, rather than NULL.
   1761 	if data == "" {
   1762 		return mount(source, target, fstype, flags, nil)
   1763 	}
   1764 	datap, err := BytePtrFromString(data)
   1765 	if err != nil {
   1766 		return err
   1767 	}
   1768 	return mount(source, target, fstype, flags, datap)
   1769 }
   1770 
   1771 //sys	mountSetattr(dirfd int, pathname string, flags uint, attr *MountAttr, size uintptr) (err error) = SYS_MOUNT_SETATTR
   1772 
   1773 // MountSetattr is a wrapper for mount_setattr(2).
   1774 // https://man7.org/linux/man-pages/man2/mount_setattr.2.html
   1775 //
   1776 // Requires kernel >= 5.12.
   1777 func MountSetattr(dirfd int, pathname string, flags uint, attr *MountAttr) error {
   1778 	return mountSetattr(dirfd, pathname, flags, attr, unsafe.Sizeof(*attr))
   1779 }
   1780 
   1781 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   1782 	if raceenabled {
   1783 		raceReleaseMerge(unsafe.Pointer(&ioSync))
   1784 	}
   1785 	return sendfile(outfd, infd, offset, count)
   1786 }
   1787 
   1788 // Sendto
   1789 // Recvfrom
   1790 // Socketpair
   1791 
   1792 /*
   1793  * Direct access
   1794  */
   1795 //sys	Acct(path string) (err error)
   1796 //sys	AddKey(keyType string, description string, payload []byte, ringid int) (id int, err error)
   1797 //sys	Adjtimex(buf *Timex) (state int, err error)
   1798 //sysnb	Capget(hdr *CapUserHeader, data *CapUserData) (err error)
   1799 //sysnb	Capset(hdr *CapUserHeader, data *CapUserData) (err error)
   1800 //sys	Chdir(path string) (err error)
   1801 //sys	Chroot(path string) (err error)
   1802 //sys	ClockGetres(clockid int32, res *Timespec) (err error)
   1803 //sys	ClockGettime(clockid int32, time *Timespec) (err error)
   1804 //sys	ClockNanosleep(clockid int32, flags int, request *Timespec, remain *Timespec) (err error)
   1805 //sys	Close(fd int) (err error)
   1806 //sys	CloseRange(first uint, last uint, flags uint) (err error)
   1807 //sys	CopyFileRange(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
   1808 //sys	DeleteModule(name string, flags int) (err error)
   1809 //sys	Dup(oldfd int) (fd int, err error)
   1810 
   1811 func Dup2(oldfd, newfd int) error {
   1812 	return Dup3(oldfd, newfd, 0)
   1813 }
   1814 
   1815 //sys	Dup3(oldfd int, newfd int, flags int) (err error)
   1816 //sysnb	EpollCreate1(flag int) (fd int, err error)
   1817 //sysnb	EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
   1818 //sys	Eventfd(initval uint, flags int) (fd int, err error) = SYS_EVENTFD2
   1819 //sys	Exit(code int) = SYS_EXIT_GROUP
   1820 //sys	Fallocate(fd int, mode uint32, off int64, len int64) (err error)
   1821 //sys	Fchdir(fd int) (err error)
   1822 //sys	Fchmod(fd int, mode uint32) (err error)
   1823 //sys	Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
   1824 //sys	Fdatasync(fd int) (err error)
   1825 //sys	Fgetxattr(fd int, attr string, dest []byte) (sz int, err error)
   1826 //sys	FinitModule(fd int, params string, flags int) (err error)
   1827 //sys	Flistxattr(fd int, dest []byte) (sz int, err error)
   1828 //sys	Flock(fd int, how int) (err error)
   1829 //sys	Fremovexattr(fd int, attr string) (err error)
   1830 //sys	Fsetxattr(fd int, attr string, dest []byte, flags int) (err error)
   1831 //sys	Fsync(fd int) (err error)
   1832 //sys	Fsmount(fd int, flags int, mountAttrs int) (fsfd int, err error)
   1833 //sys	Fsopen(fsName string, flags int) (fd int, err error)
   1834 //sys	Fspick(dirfd int, pathName string, flags int) (fd int, err error)
   1835 //sys	Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64
   1836 //sysnb	Getpgid(pid int) (pgid int, err error)
   1837 
   1838 func Getpgrp() (pid int) {
   1839 	pid, _ = Getpgid(0)
   1840 	return
   1841 }
   1842 
   1843 //sysnb	Getpid() (pid int)
   1844 //sysnb	Getppid() (ppid int)
   1845 //sys	Getpriority(which int, who int) (prio int, err error)
   1846 //sys	Getrandom(buf []byte, flags int) (n int, err error)
   1847 //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   1848 //sysnb	Getsid(pid int) (sid int, err error)
   1849 //sysnb	Gettid() (tid int)
   1850 //sys	Getxattr(path string, attr string, dest []byte) (sz int, err error)
   1851 //sys	InitModule(moduleImage []byte, params string) (err error)
   1852 //sys	InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error)
   1853 //sysnb	InotifyInit1(flags int) (fd int, err error)
   1854 //sysnb	InotifyRmWatch(fd int, watchdesc uint32) (success int, err error)
   1855 //sysnb	Kill(pid int, sig syscall.Signal) (err error)
   1856 //sys	Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG
   1857 //sys	Lgetxattr(path string, attr string, dest []byte) (sz int, err error)
   1858 //sys	Listxattr(path string, dest []byte) (sz int, err error)
   1859 //sys	Llistxattr(path string, dest []byte) (sz int, err error)
   1860 //sys	Lremovexattr(path string, attr string) (err error)
   1861 //sys	Lsetxattr(path string, attr string, data []byte, flags int) (err error)
   1862 //sys	MemfdCreate(name string, flags int) (fd int, err error)
   1863 //sys	Mkdirat(dirfd int, path string, mode uint32) (err error)
   1864 //sys	Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
   1865 //sys	MoveMount(fromDirfd int, fromPathName string, toDirfd int, toPathName string, flags int) (err error)
   1866 //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   1867 //sys	OpenTree(dfd int, fileName string, flags uint) (r int, err error)
   1868 //sys	PerfEventOpen(attr *PerfEventAttr, pid int, cpu int, groupFd int, flags int) (fd int, err error)
   1869 //sys	PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT
   1870 //sysnb	Prlimit(pid int, resource int, newlimit *Rlimit, old *Rlimit) (err error) = SYS_PRLIMIT64
   1871 //sys	Prctl(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (err error)
   1872 //sys	Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error) = SYS_PSELECT6
   1873 //sys	read(fd int, p []byte) (n int, err error)
   1874 //sys	Removexattr(path string, attr string) (err error)
   1875 //sys	Renameat2(olddirfd int, oldpath string, newdirfd int, newpath string, flags uint) (err error)
   1876 //sys	RequestKey(keyType string, description string, callback string, destRingid int) (id int, err error)
   1877 //sys	Setdomainname(p []byte) (err error)
   1878 //sys	Sethostname(p []byte) (err error)
   1879 //sysnb	Setpgid(pid int, pgid int) (err error)
   1880 //sysnb	Setsid() (pid int, err error)
   1881 //sysnb	Settimeofday(tv *Timeval) (err error)
   1882 //sys	Setns(fd int, nstype int) (err error)
   1883 
   1884 // PrctlRetInt performs a prctl operation specified by option and further
   1885 // optional arguments arg2 through arg5 depending on option. It returns a
   1886 // non-negative integer that is returned by the prctl syscall.
   1887 func PrctlRetInt(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (int, error) {
   1888 	ret, _, err := Syscall6(SYS_PRCTL, uintptr(option), uintptr(arg2), uintptr(arg3), uintptr(arg4), uintptr(arg5), 0)
   1889 	if err != 0 {
   1890 		return 0, err
   1891 	}
   1892 	return int(ret), nil
   1893 }
   1894 
   1895 // issue 1435.
   1896 // On linux Setuid and Setgid only affects the current thread, not the process.
   1897 // This does not match what most callers expect so we must return an error
   1898 // here rather than letting the caller think that the call succeeded.
   1899 
   1900 func Setuid(uid int) (err error) {
   1901 	return EOPNOTSUPP
   1902 }
   1903 
   1904 func Setgid(uid int) (err error) {
   1905 	return EOPNOTSUPP
   1906 }
   1907 
   1908 // SetfsgidRetGid sets fsgid for current thread and returns previous fsgid set.
   1909 // setfsgid(2) will return a non-nil error only if its caller lacks CAP_SETUID capability.
   1910 // If the call fails due to other reasons, current fsgid will be returned.
   1911 func SetfsgidRetGid(gid int) (int, error) {
   1912 	return setfsgid(gid)
   1913 }
   1914 
   1915 // SetfsuidRetUid sets fsuid for current thread and returns previous fsuid set.
   1916 // setfsgid(2) will return a non-nil error only if its caller lacks CAP_SETUID capability
   1917 // If the call fails due to other reasons, current fsuid will be returned.
   1918 func SetfsuidRetUid(uid int) (int, error) {
   1919 	return setfsuid(uid)
   1920 }
   1921 
   1922 func Setfsgid(gid int) error {
   1923 	_, err := setfsgid(gid)
   1924 	return err
   1925 }
   1926 
   1927 func Setfsuid(uid int) error {
   1928 	_, err := setfsuid(uid)
   1929 	return err
   1930 }
   1931 
   1932 func Signalfd(fd int, sigmask *Sigset_t, flags int) (newfd int, err error) {
   1933 	return signalfd(fd, sigmask, _C__NSIG/8, flags)
   1934 }
   1935 
   1936 //sys	Setpriority(which int, who int, prio int) (err error)
   1937 //sys	Setxattr(path string, attr string, data []byte, flags int) (err error)
   1938 //sys	signalfd(fd int, sigmask *Sigset_t, maskSize uintptr, flags int) (newfd int, err error) = SYS_SIGNALFD4
   1939 //sys	Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error)
   1940 //sys	Sync()
   1941 //sys	Syncfs(fd int) (err error)
   1942 //sysnb	Sysinfo(info *Sysinfo_t) (err error)
   1943 //sys	Tee(rfd int, wfd int, len int, flags int) (n int64, err error)
   1944 //sysnb	TimerfdCreate(clockid int, flags int) (fd int, err error)
   1945 //sysnb	TimerfdGettime(fd int, currValue *ItimerSpec) (err error)
   1946 //sysnb	TimerfdSettime(fd int, flags int, newValue *ItimerSpec, oldValue *ItimerSpec) (err error)
   1947 //sysnb	Tgkill(tgid int, tid int, sig syscall.Signal) (err error)
   1948 //sysnb	Times(tms *Tms) (ticks uintptr, err error)
   1949 //sysnb	Umask(mask int) (oldmask int)
   1950 //sysnb	Uname(buf *Utsname) (err error)
   1951 //sys	Unmount(target string, flags int) (err error) = SYS_UMOUNT2
   1952 //sys	Unshare(flags int) (err error)
   1953 //sys	write(fd int, p []byte) (n int, err error)
   1954 //sys	exitThread(code int) (err error) = SYS_EXIT
   1955 //sys	readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ
   1956 //sys	writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE
   1957 //sys	readv(fd int, iovs []Iovec) (n int, err error) = SYS_READV
   1958 //sys	writev(fd int, iovs []Iovec) (n int, err error) = SYS_WRITEV
   1959 //sys	preadv(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr) (n int, err error) = SYS_PREADV
   1960 //sys	pwritev(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr) (n int, err error) = SYS_PWRITEV
   1961 //sys	preadv2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PREADV2
   1962 //sys	pwritev2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PWRITEV2
   1963 
   1964 func bytes2iovec(bs [][]byte) []Iovec {
   1965 	iovecs := make([]Iovec, len(bs))
   1966 	for i, b := range bs {
   1967 		iovecs[i].SetLen(len(b))
   1968 		if len(b) > 0 {
   1969 			iovecs[i].Base = &b[0]
   1970 		} else {
   1971 			iovecs[i].Base = (*byte)(unsafe.Pointer(&_zero))
   1972 		}
   1973 	}
   1974 	return iovecs
   1975 }
   1976 
   1977 // offs2lohi splits offs into its lower and upper unsigned long. On 64-bit
   1978 // systems, hi will always be 0. On 32-bit systems, offs will be split in half.
   1979 // preadv/pwritev chose this calling convention so they don't need to add a
   1980 // padding-register for alignment on ARM.
   1981 func offs2lohi(offs int64) (lo, hi uintptr) {
   1982 	return uintptr(offs), uintptr(uint64(offs) >> SizeofLong)
   1983 }
   1984 
   1985 func Readv(fd int, iovs [][]byte) (n int, err error) {
   1986 	iovecs := bytes2iovec(iovs)
   1987 	n, err = readv(fd, iovecs)
   1988 	readvRacedetect(iovecs, n, err)
   1989 	return n, err
   1990 }
   1991 
   1992 func Preadv(fd int, iovs [][]byte, offset int64) (n int, err error) {
   1993 	iovecs := bytes2iovec(iovs)
   1994 	lo, hi := offs2lohi(offset)
   1995 	n, err = preadv(fd, iovecs, lo, hi)
   1996 	readvRacedetect(iovecs, n, err)
   1997 	return n, err
   1998 }
   1999 
   2000 func Preadv2(fd int, iovs [][]byte, offset int64, flags int) (n int, err error) {
   2001 	iovecs := bytes2iovec(iovs)
   2002 	lo, hi := offs2lohi(offset)
   2003 	n, err = preadv2(fd, iovecs, lo, hi, flags)
   2004 	readvRacedetect(iovecs, n, err)
   2005 	return n, err
   2006 }
   2007 
   2008 func readvRacedetect(iovecs []Iovec, n int, err error) {
   2009 	if !raceenabled {
   2010 		return
   2011 	}
   2012 	for i := 0; n > 0 && i < len(iovecs); i++ {
   2013 		m := int(iovecs[i].Len)
   2014 		if m > n {
   2015 			m = n
   2016 		}
   2017 		n -= m
   2018 		if m > 0 {
   2019 			raceWriteRange(unsafe.Pointer(iovecs[i].Base), m)
   2020 		}
   2021 	}
   2022 	if err == nil {
   2023 		raceAcquire(unsafe.Pointer(&ioSync))
   2024 	}
   2025 }
   2026 
   2027 func Writev(fd int, iovs [][]byte) (n int, err error) {
   2028 	iovecs := bytes2iovec(iovs)
   2029 	if raceenabled {
   2030 		raceReleaseMerge(unsafe.Pointer(&ioSync))
   2031 	}
   2032 	n, err = writev(fd, iovecs)
   2033 	writevRacedetect(iovecs, n)
   2034 	return n, err
   2035 }
   2036 
   2037 func Pwritev(fd int, iovs [][]byte, offset int64) (n int, err error) {
   2038 	iovecs := bytes2iovec(iovs)
   2039 	if raceenabled {
   2040 		raceReleaseMerge(unsafe.Pointer(&ioSync))
   2041 	}
   2042 	lo, hi := offs2lohi(offset)
   2043 	n, err = pwritev(fd, iovecs, lo, hi)
   2044 	writevRacedetect(iovecs, n)
   2045 	return n, err
   2046 }
   2047 
   2048 func Pwritev2(fd int, iovs [][]byte, offset int64, flags int) (n int, err error) {
   2049 	iovecs := bytes2iovec(iovs)
   2050 	if raceenabled {
   2051 		raceReleaseMerge(unsafe.Pointer(&ioSync))
   2052 	}
   2053 	lo, hi := offs2lohi(offset)
   2054 	n, err = pwritev2(fd, iovecs, lo, hi, flags)
   2055 	writevRacedetect(iovecs, n)
   2056 	return n, err
   2057 }
   2058 
   2059 func writevRacedetect(iovecs []Iovec, n int) {
   2060 	if !raceenabled {
   2061 		return
   2062 	}
   2063 	for i := 0; n > 0 && i < len(iovecs); i++ {
   2064 		m := int(iovecs[i].Len)
   2065 		if m > n {
   2066 			m = n
   2067 		}
   2068 		n -= m
   2069 		if m > 0 {
   2070 			raceReadRange(unsafe.Pointer(iovecs[i].Base), m)
   2071 		}
   2072 	}
   2073 }
   2074 
   2075 // mmap varies by architecture; see syscall_linux_*.go.
   2076 //sys	munmap(addr uintptr, length uintptr) (err error)
   2077 
   2078 var mapper = &mmapper{
   2079 	active: make(map[*byte][]byte),
   2080 	mmap:   mmap,
   2081 	munmap: munmap,
   2082 }
   2083 
   2084 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
   2085 	return mapper.Mmap(fd, offset, length, prot, flags)
   2086 }
   2087 
   2088 func Munmap(b []byte) (err error) {
   2089 	return mapper.Munmap(b)
   2090 }
   2091 
   2092 //sys	Madvise(b []byte, advice int) (err error)
   2093 //sys	Mprotect(b []byte, prot int) (err error)
   2094 //sys	Mlock(b []byte) (err error)
   2095 //sys	Mlockall(flags int) (err error)
   2096 //sys	Msync(b []byte, flags int) (err error)
   2097 //sys	Munlock(b []byte) (err error)
   2098 //sys	Munlockall() (err error)
   2099 
   2100 // Vmsplice splices user pages from a slice of Iovecs into a pipe specified by fd,
   2101 // using the specified flags.
   2102 func Vmsplice(fd int, iovs []Iovec, flags int) (int, error) {
   2103 	var p unsafe.Pointer
   2104 	if len(iovs) > 0 {
   2105 		p = unsafe.Pointer(&iovs[0])
   2106 	}
   2107 
   2108 	n, _, errno := Syscall6(SYS_VMSPLICE, uintptr(fd), uintptr(p), uintptr(len(iovs)), uintptr(flags), 0, 0)
   2109 	if errno != 0 {
   2110 		return 0, syscall.Errno(errno)
   2111 	}
   2112 
   2113 	return int(n), nil
   2114 }
   2115 
   2116 func isGroupMember(gid int) bool {
   2117 	groups, err := Getgroups()
   2118 	if err != nil {
   2119 		return false
   2120 	}
   2121 
   2122 	for _, g := range groups {
   2123 		if g == gid {
   2124 			return true
   2125 		}
   2126 	}
   2127 	return false
   2128 }
   2129 
   2130 //sys	faccessat(dirfd int, path string, mode uint32) (err error)
   2131 //sys	Faccessat2(dirfd int, path string, mode uint32, flags int) (err error)
   2132 
   2133 func Faccessat(dirfd int, path string, mode uint32, flags int) (err error) {
   2134 	if flags == 0 {
   2135 		return faccessat(dirfd, path, mode)
   2136 	}
   2137 
   2138 	if err := Faccessat2(dirfd, path, mode, flags); err != ENOSYS && err != EPERM {
   2139 		return err
   2140 	}
   2141 
   2142 	// The Linux kernel faccessat system call does not take any flags.
   2143 	// The glibc faccessat implements the flags itself; see
   2144 	// https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/faccessat.c;hb=HEAD
   2145 	// Because people naturally expect syscall.Faccessat to act
   2146 	// like C faccessat, we do the same.
   2147 
   2148 	if flags & ^(AT_SYMLINK_NOFOLLOW|AT_EACCESS) != 0 {
   2149 		return EINVAL
   2150 	}
   2151 
   2152 	var st Stat_t
   2153 	if err := Fstatat(dirfd, path, &st, flags&AT_SYMLINK_NOFOLLOW); err != nil {
   2154 		return err
   2155 	}
   2156 
   2157 	mode &= 7
   2158 	if mode == 0 {
   2159 		return nil
   2160 	}
   2161 
   2162 	var uid int
   2163 	if flags&AT_EACCESS != 0 {
   2164 		uid = Geteuid()
   2165 	} else {
   2166 		uid = Getuid()
   2167 	}
   2168 
   2169 	if uid == 0 {
   2170 		if mode&1 == 0 {
   2171 			// Root can read and write any file.
   2172 			return nil
   2173 		}
   2174 		if st.Mode&0111 != 0 {
   2175 			// Root can execute any file that anybody can execute.
   2176 			return nil
   2177 		}
   2178 		return EACCES
   2179 	}
   2180 
   2181 	var fmode uint32
   2182 	if uint32(uid) == st.Uid {
   2183 		fmode = (st.Mode >> 6) & 7
   2184 	} else {
   2185 		var gid int
   2186 		if flags&AT_EACCESS != 0 {
   2187 			gid = Getegid()
   2188 		} else {
   2189 			gid = Getgid()
   2190 		}
   2191 
   2192 		if uint32(gid) == st.Gid || isGroupMember(int(st.Gid)) {
   2193 			fmode = (st.Mode >> 3) & 7
   2194 		} else {
   2195 			fmode = st.Mode & 7
   2196 		}
   2197 	}
   2198 
   2199 	if fmode&mode == mode {
   2200 		return nil
   2201 	}
   2202 
   2203 	return EACCES
   2204 }
   2205 
   2206 //sys	nameToHandleAt(dirFD int, pathname string, fh *fileHandle, mountID *_C_int, flags int) (err error) = SYS_NAME_TO_HANDLE_AT
   2207 //sys	openByHandleAt(mountFD int, fh *fileHandle, flags int) (fd int, err error) = SYS_OPEN_BY_HANDLE_AT
   2208 
   2209 // fileHandle is the argument to nameToHandleAt and openByHandleAt. We
   2210 // originally tried to generate it via unix/linux/types.go with "type
   2211 // fileHandle C.struct_file_handle" but that generated empty structs
   2212 // for mips64 and mips64le. Instead, hard code it for now (it's the
   2213 // same everywhere else) until the mips64 generator issue is fixed.
   2214 type fileHandle struct {
   2215 	Bytes uint32
   2216 	Type  int32
   2217 }
   2218 
   2219 // FileHandle represents the C struct file_handle used by
   2220 // name_to_handle_at (see NameToHandleAt) and open_by_handle_at (see
   2221 // OpenByHandleAt).
   2222 type FileHandle struct {
   2223 	*fileHandle
   2224 }
   2225 
   2226 // NewFileHandle constructs a FileHandle.
   2227 func NewFileHandle(handleType int32, handle []byte) FileHandle {
   2228 	const hdrSize = unsafe.Sizeof(fileHandle{})
   2229 	buf := make([]byte, hdrSize+uintptr(len(handle)))
   2230 	copy(buf[hdrSize:], handle)
   2231 	fh := (*fileHandle)(unsafe.Pointer(&buf[0]))
   2232 	fh.Type = handleType
   2233 	fh.Bytes = uint32(len(handle))
   2234 	return FileHandle{fh}
   2235 }
   2236 
   2237 func (fh *FileHandle) Size() int   { return int(fh.fileHandle.Bytes) }
   2238 func (fh *FileHandle) Type() int32 { return fh.fileHandle.Type }
   2239 func (fh *FileHandle) Bytes() []byte {
   2240 	n := fh.Size()
   2241 	if n == 0 {
   2242 		return nil
   2243 	}
   2244 	return (*[1 << 30]byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&fh.fileHandle.Type)) + 4))[:n:n]
   2245 }
   2246 
   2247 // NameToHandleAt wraps the name_to_handle_at system call; it obtains
   2248 // a handle for a path name.
   2249 func NameToHandleAt(dirfd int, path string, flags int) (handle FileHandle, mountID int, err error) {
   2250 	var mid _C_int
   2251 	// Try first with a small buffer, assuming the handle will
   2252 	// only be 32 bytes.
   2253 	size := uint32(32 + unsafe.Sizeof(fileHandle{}))
   2254 	didResize := false
   2255 	for {
   2256 		buf := make([]byte, size)
   2257 		fh := (*fileHandle)(unsafe.Pointer(&buf[0]))
   2258 		fh.Bytes = size - uint32(unsafe.Sizeof(fileHandle{}))
   2259 		err = nameToHandleAt(dirfd, path, fh, &mid, flags)
   2260 		if err == EOVERFLOW {
   2261 			if didResize {
   2262 				// We shouldn't need to resize more than once
   2263 				return
   2264 			}
   2265 			didResize = true
   2266 			size = fh.Bytes + uint32(unsafe.Sizeof(fileHandle{}))
   2267 			continue
   2268 		}
   2269 		if err != nil {
   2270 			return
   2271 		}
   2272 		return FileHandle{fh}, int(mid), nil
   2273 	}
   2274 }
   2275 
   2276 // OpenByHandleAt wraps the open_by_handle_at system call; it opens a
   2277 // file via a handle as previously returned by NameToHandleAt.
   2278 func OpenByHandleAt(mountFD int, handle FileHandle, flags int) (fd int, err error) {
   2279 	return openByHandleAt(mountFD, handle.fileHandle, flags)
   2280 }
   2281 
   2282 // Klogset wraps the sys_syslog system call; it sets console_loglevel to
   2283 // the value specified by arg and passes a dummy pointer to bufp.
   2284 func Klogset(typ int, arg int) (err error) {
   2285 	var p unsafe.Pointer
   2286 	_, _, errno := Syscall(SYS_SYSLOG, uintptr(typ), uintptr(p), uintptr(arg))
   2287 	if errno != 0 {
   2288 		return errnoErr(errno)
   2289 	}
   2290 	return nil
   2291 }
   2292 
   2293 // RemoteIovec is Iovec with the pointer replaced with an integer.
   2294 // It is used for ProcessVMReadv and ProcessVMWritev, where the pointer
   2295 // refers to a location in a different process' address space, which
   2296 // would confuse the Go garbage collector.
   2297 type RemoteIovec struct {
   2298 	Base uintptr
   2299 	Len  int
   2300 }
   2301 
   2302 //sys	ProcessVMReadv(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_READV
   2303 //sys	ProcessVMWritev(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_WRITEV
   2304 
   2305 //sys	PidfdOpen(pid int, flags int) (fd int, err error) = SYS_PIDFD_OPEN
   2306 //sys	PidfdGetfd(pidfd int, targetfd int, flags int) (fd int, err error) = SYS_PIDFD_GETFD
   2307 //sys	PidfdSendSignal(pidfd int, sig Signal, info *Siginfo, flags int) (err error) = SYS_PIDFD_SEND_SIGNAL
   2308 
   2309 //sys	shmat(id int, addr uintptr, flag int) (ret uintptr, err error)
   2310 //sys	shmctl(id int, cmd int, buf *SysvShmDesc) (result int, err error)
   2311 //sys	shmdt(addr uintptr) (err error)
   2312 //sys	shmget(key int, size int, flag int) (id int, err error)
   2313 
   2314 //sys	getitimer(which int, currValue *Itimerval) (err error)
   2315 //sys	setitimer(which int, newValue *Itimerval, oldValue *Itimerval) (err error)
   2316 
   2317 // MakeItimerval creates an Itimerval from interval and value durations.
   2318 func MakeItimerval(interval, value time.Duration) Itimerval {
   2319 	return Itimerval{
   2320 		Interval: NsecToTimeval(interval.Nanoseconds()),
   2321 		Value:    NsecToTimeval(value.Nanoseconds()),
   2322 	}
   2323 }
   2324 
   2325 // A value which may be passed to the which parameter for Getitimer and
   2326 // Setitimer.
   2327 type ItimerWhich int
   2328 
   2329 // Possible which values for Getitimer and Setitimer.
   2330 const (
   2331 	ItimerReal    ItimerWhich = ITIMER_REAL
   2332 	ItimerVirtual ItimerWhich = ITIMER_VIRTUAL
   2333 	ItimerProf    ItimerWhich = ITIMER_PROF
   2334 )
   2335 
   2336 // Getitimer wraps getitimer(2) to return the current value of the timer
   2337 // specified by which.
   2338 func Getitimer(which ItimerWhich) (Itimerval, error) {
   2339 	var it Itimerval
   2340 	if err := getitimer(int(which), &it); err != nil {
   2341 		return Itimerval{}, err
   2342 	}
   2343 
   2344 	return it, nil
   2345 }
   2346 
   2347 // Setitimer wraps setitimer(2) to arm or disarm the timer specified by which.
   2348 // It returns the previous value of the timer.
   2349 //
   2350 // If the Itimerval argument is the zero value, the timer will be disarmed.
   2351 func Setitimer(which ItimerWhich, it Itimerval) (Itimerval, error) {
   2352 	var prev Itimerval
   2353 	if err := setitimer(int(which), &it, &prev); err != nil {
   2354 		return Itimerval{}, err
   2355 	}
   2356 
   2357 	return prev, nil
   2358 }
   2359 
   2360 /*
   2361  * Unimplemented
   2362  */
   2363 // AfsSyscall
   2364 // ArchPrctl
   2365 // Brk
   2366 // ClockNanosleep
   2367 // ClockSettime
   2368 // Clone
   2369 // EpollCtlOld
   2370 // EpollPwait
   2371 // EpollWaitOld
   2372 // Execve
   2373 // Fork
   2374 // Futex
   2375 // GetKernelSyms
   2376 // GetMempolicy
   2377 // GetRobustList
   2378 // GetThreadArea
   2379 // Getpmsg
   2380 // IoCancel
   2381 // IoDestroy
   2382 // IoGetevents
   2383 // IoSetup
   2384 // IoSubmit
   2385 // IoprioGet
   2386 // IoprioSet
   2387 // KexecLoad
   2388 // LookupDcookie
   2389 // Mbind
   2390 // MigratePages
   2391 // Mincore
   2392 // ModifyLdt
   2393 // Mount
   2394 // MovePages
   2395 // MqGetsetattr
   2396 // MqNotify
   2397 // MqOpen
   2398 // MqTimedreceive
   2399 // MqTimedsend
   2400 // MqUnlink
   2401 // Mremap
   2402 // Msgctl
   2403 // Msgget
   2404 // Msgrcv
   2405 // Msgsnd
   2406 // Nfsservctl
   2407 // Personality
   2408 // Pselect6
   2409 // Ptrace
   2410 // Putpmsg
   2411 // Quotactl
   2412 // Readahead
   2413 // Readv
   2414 // RemapFilePages
   2415 // RestartSyscall
   2416 // RtSigaction
   2417 // RtSigpending
   2418 // RtSigprocmask
   2419 // RtSigqueueinfo
   2420 // RtSigreturn
   2421 // RtSigsuspend
   2422 // RtSigtimedwait
   2423 // SchedGetPriorityMax
   2424 // SchedGetPriorityMin
   2425 // SchedGetparam
   2426 // SchedGetscheduler
   2427 // SchedRrGetInterval
   2428 // SchedSetparam
   2429 // SchedYield
   2430 // Security
   2431 // Semctl
   2432 // Semget
   2433 // Semop
   2434 // Semtimedop
   2435 // SetMempolicy
   2436 // SetRobustList
   2437 // SetThreadArea
   2438 // SetTidAddress
   2439 // Sigaltstack
   2440 // Swapoff
   2441 // Swapon
   2442 // Sysfs
   2443 // TimerCreate
   2444 // TimerDelete
   2445 // TimerGetoverrun
   2446 // TimerGettime
   2447 // TimerSettime
   2448 // Tkill (obsolete)
   2449 // Tuxcall
   2450 // Umount2
   2451 // Uselib
   2452 // Utimensat
   2453 // Vfork
   2454 // Vhangup
   2455 // Vserver
   2456 // _Sysctl