forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/unix/syscall_unix.go

1048 lines
17 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
package unix
import (
"bytes"
"sort"
"sync"
"syscall"
"unsafe"
)
var (
Stdin = 0
2024-02-18 10:42:21 +00:00
Stdout = 1
2024-02-18 10:42:21 +00:00
Stderr = 2
)
// Do the interface allocations only once for common
2024-02-18 10:42:21 +00:00
// Errno values.
2024-02-18 10:42:21 +00:00
var (
errEAGAIN error = syscall.EAGAIN
2024-02-18 10:42:21 +00:00
errEINVAL error = syscall.EINVAL
2024-02-18 10:42:21 +00:00
errENOENT error = syscall.ENOENT
)
var (
signalNameMapOnce sync.Once
signalNameMap map[string]syscall.Signal
2024-02-18 10:42:21 +00:00
)
// errnoErr returns common boxed Errno values, to prevent
2024-02-18 10:42:21 +00:00
// allocations at runtime.
2024-02-18 10:42:21 +00:00
func errnoErr(e syscall.Errno) error {
2024-02-18 10:42:21 +00:00
switch e {
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
case EAGAIN:
2024-02-18 10:42:21 +00:00
return errEAGAIN
2024-02-18 10:42:21 +00:00
case EINVAL:
2024-02-18 10:42:21 +00:00
return errEINVAL
2024-02-18 10:42:21 +00:00
case ENOENT:
2024-02-18 10:42:21 +00:00
return errENOENT
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
// ErrnoName returns the error name for error number e.
2024-02-18 10:42:21 +00:00
func ErrnoName(e syscall.Errno) string {
2024-02-18 10:42:21 +00:00
i := sort.Search(len(errorList), func(i int) bool {
2024-02-18 10:42:21 +00:00
return errorList[i].num >= e
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if i < len(errorList) && errorList[i].num == e {
2024-02-18 10:42:21 +00:00
return errorList[i].name
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
// SignalName returns the signal name for signal number s.
2024-02-18 10:42:21 +00:00
func SignalName(s syscall.Signal) string {
2024-02-18 10:42:21 +00:00
i := sort.Search(len(signalList), func(i int) bool {
2024-02-18 10:42:21 +00:00
return signalList[i].num >= s
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if i < len(signalList) && signalList[i].num == s {
2024-02-18 10:42:21 +00:00
return signalList[i].name
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
// SignalNum returns the syscall.Signal for signal named s,
2024-02-18 10:42:21 +00:00
// or 0 if a signal with such name is not found.
2024-02-18 10:42:21 +00:00
// The signal name should start with "SIG".
2024-02-18 10:42:21 +00:00
func SignalNum(s string) syscall.Signal {
2024-02-18 10:42:21 +00:00
signalNameMapOnce.Do(func() {
2024-02-18 10:42:21 +00:00
signalNameMap = make(map[string]syscall.Signal, len(signalList))
2024-02-18 10:42:21 +00:00
for _, signal := range signalList {
2024-02-18 10:42:21 +00:00
signalNameMap[signal.name] = signal.num
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
return signalNameMap[s]
2024-02-18 10:42:21 +00:00
}
// clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
2024-02-18 10:42:21 +00:00
func clen(n []byte) int {
2024-02-18 10:42:21 +00:00
i := bytes.IndexByte(n, 0)
2024-02-18 10:42:21 +00:00
if i == -1 {
2024-02-18 10:42:21 +00:00
i = len(n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return i
2024-02-18 10:42:21 +00:00
}
// Mmap manager, for use by operating system-specific implementations.
type mmapper struct {
sync.Mutex
2024-02-18 10:42:21 +00:00
active map[*byte][]byte // active mappings; key is last byte in mapping
mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
2024-02-18 10:42:21 +00:00
munmap func(addr uintptr, length uintptr) error
}
func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
2024-02-18 10:42:21 +00:00
if length <= 0 {
2024-02-18 10:42:21 +00:00
return nil, EINVAL
2024-02-18 10:42:21 +00:00
}
// Map the requested memory.
2024-02-18 10:42:21 +00:00
addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
2024-02-18 10:42:21 +00:00
if errno != nil {
2024-02-18 10:42:21 +00:00
return nil, errno
2024-02-18 10:42:21 +00:00
}
// Use unsafe to convert addr into a []byte.
2024-02-18 10:42:21 +00:00
b := unsafe.Slice((*byte)(unsafe.Pointer(addr)), length)
// Register mapping in m and return it.
2024-02-18 10:42:21 +00:00
p := &b[cap(b)-1]
2024-02-18 10:42:21 +00:00
m.Lock()
2024-02-18 10:42:21 +00:00
defer m.Unlock()
2024-02-18 10:42:21 +00:00
m.active[p] = b
2024-02-18 10:42:21 +00:00
return b, nil
2024-02-18 10:42:21 +00:00
}
func (m *mmapper) Munmap(data []byte) (err error) {
2024-02-18 10:42:21 +00:00
if len(data) == 0 || len(data) != cap(data) {
2024-02-18 10:42:21 +00:00
return EINVAL
2024-02-18 10:42:21 +00:00
}
// Find the base of the mapping.
2024-02-18 10:42:21 +00:00
p := &data[cap(data)-1]
2024-02-18 10:42:21 +00:00
m.Lock()
2024-02-18 10:42:21 +00:00
defer m.Unlock()
2024-02-18 10:42:21 +00:00
b := m.active[p]
2024-02-18 10:42:21 +00:00
if b == nil || &b[0] != &data[0] {
2024-02-18 10:42:21 +00:00
return EINVAL
2024-02-18 10:42:21 +00:00
}
// Unmap the memory and update m.
2024-02-18 10:42:21 +00:00
if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
2024-02-18 10:42:21 +00:00
return errno
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
delete(m.active, p)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
2024-02-18 10:42:21 +00:00
return mapper.Mmap(fd, offset, length, prot, flags)
2024-02-18 10:42:21 +00:00
}
func Munmap(b []byte) (err error) {
2024-02-18 10:42:21 +00:00
return mapper.Munmap(b)
2024-02-18 10:42:21 +00:00
}
func Read(fd int, p []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
n, err = read(fd, p)
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
if n > 0 {
2024-02-18 10:42:21 +00:00
raceWriteRange(unsafe.Pointer(&p[0]), n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
raceAcquire(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func Write(fd int, p []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
raceReleaseMerge(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n, err = write(fd, p)
2024-02-18 10:42:21 +00:00
if raceenabled && n > 0 {
2024-02-18 10:42:21 +00:00
raceReadRange(unsafe.Pointer(&p[0]), n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func Pread(fd int, p []byte, offset int64) (n int, err error) {
2024-02-18 10:42:21 +00:00
n, err = pread(fd, p, offset)
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
if n > 0 {
2024-02-18 10:42:21 +00:00
raceWriteRange(unsafe.Pointer(&p[0]), n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
raceAcquire(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func Pwrite(fd int, p []byte, offset int64) (n int, err error) {
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
raceReleaseMerge(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n, err = pwrite(fd, p, offset)
2024-02-18 10:42:21 +00:00
if raceenabled && n > 0 {
2024-02-18 10:42:21 +00:00
raceReadRange(unsafe.Pointer(&p[0]), n)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// For testing: clients can set this flag to force
2024-02-18 10:42:21 +00:00
// creation of IPv6 sockets to return EAFNOSUPPORT.
2024-02-18 10:42:21 +00:00
var SocketDisableIPv6 bool
// Sockaddr represents a socket address.
2024-02-18 10:42:21 +00:00
type Sockaddr interface {
sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
2024-02-18 10:42:21 +00:00
}
// SockaddrInet4 implements the Sockaddr interface for AF_INET type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrInet4 struct {
Port int
2024-02-18 10:42:21 +00:00
Addr [4]byte
raw RawSockaddrInet4
2024-02-18 10:42:21 +00:00
}
// SockaddrInet6 implements the Sockaddr interface for AF_INET6 type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrInet6 struct {
Port int
2024-02-18 10:42:21 +00:00
ZoneId uint32
Addr [16]byte
raw RawSockaddrInet6
2024-02-18 10:42:21 +00:00
}
// SockaddrUnix implements the Sockaddr interface for AF_UNIX type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrUnix struct {
Name string
raw RawSockaddrUnix
2024-02-18 10:42:21 +00:00
}
func Bind(fd int, sa Sockaddr) (err error) {
2024-02-18 10:42:21 +00:00
ptr, n, err := sa.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return bind(fd, ptr, n)
2024-02-18 10:42:21 +00:00
}
func Connect(fd int, sa Sockaddr) (err error) {
2024-02-18 10:42:21 +00:00
ptr, n, err := sa.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return connect(fd, ptr, n)
2024-02-18 10:42:21 +00:00
}
func Getpeername(fd int) (sa Sockaddr, err error) {
2024-02-18 10:42:21 +00:00
var rsa RawSockaddrAny
2024-02-18 10:42:21 +00:00
var len _Socklen = SizeofSockaddrAny
2024-02-18 10:42:21 +00:00
if err = getpeername(fd, &rsa, &len); err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return anyToSockaddr(fd, &rsa)
2024-02-18 10:42:21 +00:00
}
func GetsockoptByte(fd, level, opt int) (value byte, err error) {
2024-02-18 10:42:21 +00:00
var n byte
2024-02-18 10:42:21 +00:00
vallen := _Socklen(1)
2024-02-18 10:42:21 +00:00
err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptInt(fd, level, opt int) (value int, err error) {
2024-02-18 10:42:21 +00:00
var n int32
2024-02-18 10:42:21 +00:00
vallen := _Socklen(4)
2024-02-18 10:42:21 +00:00
err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
2024-02-18 10:42:21 +00:00
return int(n), err
2024-02-18 10:42:21 +00:00
}
func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
2024-02-18 10:42:21 +00:00
vallen := _Socklen(4)
2024-02-18 10:42:21 +00:00
err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
2024-02-18 10:42:21 +00:00
return value, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
2024-02-18 10:42:21 +00:00
var value IPMreq
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofIPMreq)
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
2024-02-18 10:42:21 +00:00
return &value, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
2024-02-18 10:42:21 +00:00
var value IPv6Mreq
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofIPv6Mreq)
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
2024-02-18 10:42:21 +00:00
return &value, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
2024-02-18 10:42:21 +00:00
var value IPv6MTUInfo
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofIPv6MTUInfo)
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
2024-02-18 10:42:21 +00:00
return &value, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
2024-02-18 10:42:21 +00:00
var value ICMPv6Filter
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofICMPv6Filter)
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
2024-02-18 10:42:21 +00:00
return &value, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptLinger(fd, level, opt int) (*Linger, error) {
2024-02-18 10:42:21 +00:00
var linger Linger
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofLinger)
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(&linger), &vallen)
2024-02-18 10:42:21 +00:00
return &linger, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptTimeval(fd, level, opt int) (*Timeval, error) {
2024-02-18 10:42:21 +00:00
var tv Timeval
2024-02-18 10:42:21 +00:00
vallen := _Socklen(unsafe.Sizeof(tv))
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(&tv), &vallen)
2024-02-18 10:42:21 +00:00
return &tv, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptUint64(fd, level, opt int) (value uint64, err error) {
2024-02-18 10:42:21 +00:00
var n uint64
2024-02-18 10:42:21 +00:00
vallen := _Socklen(8)
2024-02-18 10:42:21 +00:00
err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
2024-02-18 10:42:21 +00:00
var rsa RawSockaddrAny
2024-02-18 10:42:21 +00:00
var len _Socklen = SizeofSockaddrAny
2024-02-18 10:42:21 +00:00
if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if rsa.Addr.Family != AF_UNSPEC {
2024-02-18 10:42:21 +00:00
from, err = anyToSockaddr(fd, &rsa)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// Recvmsg receives a message from a socket using the recvmsg system call. The
2024-02-18 10:42:21 +00:00
// received non-control data will be written to p, and any "out of band"
2024-02-18 10:42:21 +00:00
// control data will be written to oob. The flags are passed to recvmsg.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The results are:
2024-02-18 10:42:21 +00:00
// - n is the number of non-control data bytes read into p
2024-02-18 10:42:21 +00:00
// - oobn is the number of control data bytes read into oob; this may be interpreted using [ParseSocketControlMessage]
2024-02-18 10:42:21 +00:00
// - recvflags is flags returned by recvmsg
2024-02-18 10:42:21 +00:00
// - from is the address of the sender
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If the underlying socket type is not SOCK_DGRAM, a received message
2024-02-18 10:42:21 +00:00
// containing oob data and a single '\0' of non-control data is treated as if
2024-02-18 10:42:21 +00:00
// the message contained only control data, i.e. n will be zero on return.
2024-02-18 10:42:21 +00:00
func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
2024-02-18 10:42:21 +00:00
var iov [1]Iovec
2024-02-18 10:42:21 +00:00
if len(p) > 0 {
2024-02-18 10:42:21 +00:00
iov[0].Base = &p[0]
2024-02-18 10:42:21 +00:00
iov[0].SetLen(len(p))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var rsa RawSockaddrAny
2024-02-18 10:42:21 +00:00
n, oobn, recvflags, err = recvmsgRaw(fd, iov[:], oob, flags, &rsa)
2024-02-18 10:42:21 +00:00
// source address is only specified if the socket is unconnected
2024-02-18 10:42:21 +00:00
if rsa.Addr.Family != AF_UNSPEC {
2024-02-18 10:42:21 +00:00
from, err = anyToSockaddr(fd, &rsa)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// RecvmsgBuffers receives a message from a socket using the recvmsg system
2024-02-18 10:42:21 +00:00
// call. This function is equivalent to Recvmsg, but non-control data read is
2024-02-18 10:42:21 +00:00
// scattered into the buffers slices.
2024-02-18 10:42:21 +00:00
func RecvmsgBuffers(fd int, buffers [][]byte, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
2024-02-18 10:42:21 +00:00
iov := make([]Iovec, len(buffers))
2024-02-18 10:42:21 +00:00
for i := range buffers {
2024-02-18 10:42:21 +00:00
if len(buffers[i]) > 0 {
2024-02-18 10:42:21 +00:00
iov[i].Base = &buffers[i][0]
2024-02-18 10:42:21 +00:00
iov[i].SetLen(len(buffers[i]))
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
iov[i].Base = (*byte)(unsafe.Pointer(&_zero))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var rsa RawSockaddrAny
2024-02-18 10:42:21 +00:00
n, oobn, recvflags, err = recvmsgRaw(fd, iov, oob, flags, &rsa)
2024-02-18 10:42:21 +00:00
if err == nil && rsa.Addr.Family != AF_UNSPEC {
2024-02-18 10:42:21 +00:00
from, err = anyToSockaddr(fd, &rsa)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// Sendmsg sends a message on a socket to an address using the sendmsg system
2024-02-18 10:42:21 +00:00
// call. This function is equivalent to SendmsgN, but does not return the
2024-02-18 10:42:21 +00:00
// number of bytes actually sent.
2024-02-18 10:42:21 +00:00
func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
2024-02-18 10:42:21 +00:00
_, err = SendmsgN(fd, p, oob, to, flags)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// SendmsgN sends a message on a socket to an address using the sendmsg system
2024-02-18 10:42:21 +00:00
// call. p contains the non-control data to send, and oob contains the "out of
2024-02-18 10:42:21 +00:00
// band" control data. The flags are passed to sendmsg. The number of
2024-02-18 10:42:21 +00:00
// non-control bytes actually written to the socket is returned.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Some socket types do not support sending control data without accompanying
2024-02-18 10:42:21 +00:00
// non-control data. If p is empty, and oob contains control data, and the
2024-02-18 10:42:21 +00:00
// underlying socket type is not SOCK_DGRAM, p will be treated as containing a
2024-02-18 10:42:21 +00:00
// single '\0' and the return value will indicate zero bytes sent.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The Go function Recvmsg, if called with an empty p and a non-empty oob,
2024-02-18 10:42:21 +00:00
// will read and ignore this additional '\0'. If the message is received by
2024-02-18 10:42:21 +00:00
// code that does not use Recvmsg, or that does not use Go at all, that code
2024-02-18 10:42:21 +00:00
// will need to be written to expect and ignore the additional '\0'.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If you need to send non-empty oob with p actually empty, and if the
2024-02-18 10:42:21 +00:00
// underlying socket type supports it, you can do so via a raw system call as
2024-02-18 10:42:21 +00:00
// follows:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// msg := &unix.Msghdr{
2024-02-18 10:42:21 +00:00
// Control: &oob[0],
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
// msg.SetControllen(len(oob))
2024-02-18 10:42:21 +00:00
// n, _, errno := unix.Syscall(unix.SYS_SENDMSG, uintptr(fd), uintptr(unsafe.Pointer(msg)), flags)
2024-02-18 10:42:21 +00:00
func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
2024-02-18 10:42:21 +00:00
var iov [1]Iovec
2024-02-18 10:42:21 +00:00
if len(p) > 0 {
2024-02-18 10:42:21 +00:00
iov[0].Base = &p[0]
2024-02-18 10:42:21 +00:00
iov[0].SetLen(len(p))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var ptr unsafe.Pointer
2024-02-18 10:42:21 +00:00
var salen _Socklen
2024-02-18 10:42:21 +00:00
if to != nil {
2024-02-18 10:42:21 +00:00
ptr, salen, err = to.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sendmsgN(fd, iov[:], oob, ptr, salen, flags)
2024-02-18 10:42:21 +00:00
}
// SendmsgBuffers sends a message on a socket to an address using the sendmsg
2024-02-18 10:42:21 +00:00
// system call. This function is equivalent to SendmsgN, but the non-control
2024-02-18 10:42:21 +00:00
// data is gathered from buffers.
2024-02-18 10:42:21 +00:00
func SendmsgBuffers(fd int, buffers [][]byte, oob []byte, to Sockaddr, flags int) (n int, err error) {
2024-02-18 10:42:21 +00:00
iov := make([]Iovec, len(buffers))
2024-02-18 10:42:21 +00:00
for i := range buffers {
2024-02-18 10:42:21 +00:00
if len(buffers[i]) > 0 {
2024-02-18 10:42:21 +00:00
iov[i].Base = &buffers[i][0]
2024-02-18 10:42:21 +00:00
iov[i].SetLen(len(buffers[i]))
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
iov[i].Base = (*byte)(unsafe.Pointer(&_zero))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var ptr unsafe.Pointer
2024-02-18 10:42:21 +00:00
var salen _Socklen
2024-02-18 10:42:21 +00:00
if to != nil {
2024-02-18 10:42:21 +00:00
ptr, salen, err = to.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sendmsgN(fd, iov, oob, ptr, salen, flags)
2024-02-18 10:42:21 +00:00
}
func Send(s int, buf []byte, flags int) (err error) {
2024-02-18 10:42:21 +00:00
return sendto(s, buf, flags, nil, 0)
2024-02-18 10:42:21 +00:00
}
func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
2024-02-18 10:42:21 +00:00
var ptr unsafe.Pointer
2024-02-18 10:42:21 +00:00
var salen _Socklen
2024-02-18 10:42:21 +00:00
if to != nil {
2024-02-18 10:42:21 +00:00
ptr, salen, err = to.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sendto(fd, p, flags, ptr, salen)
2024-02-18 10:42:21 +00:00
}
func SetsockoptByte(fd, level, opt int, value byte) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
2024-02-18 10:42:21 +00:00
}
func SetsockoptInt(fd, level, opt int, value int) (err error) {
2024-02-18 10:42:21 +00:00
var n = int32(value)
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
2024-02-18 10:42:21 +00:00
}
func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
2024-02-18 10:42:21 +00:00
}
func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
2024-02-18 10:42:21 +00:00
}
func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
2024-02-18 10:42:21 +00:00
}
func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
2024-02-18 10:42:21 +00:00
}
func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
2024-02-18 10:42:21 +00:00
}
func SetsockoptString(fd, level, opt int, s string) (err error) {
2024-02-18 10:42:21 +00:00
var p unsafe.Pointer
2024-02-18 10:42:21 +00:00
if len(s) > 0 {
2024-02-18 10:42:21 +00:00
p = unsafe.Pointer(&[]byte(s)[0])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, p, uintptr(len(s)))
2024-02-18 10:42:21 +00:00
}
func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
2024-02-18 10:42:21 +00:00
}
func SetsockoptUint64(fd, level, opt int, value uint64) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(&value), 8)
2024-02-18 10:42:21 +00:00
}
func Socket(domain, typ, proto int) (fd int, err error) {
2024-02-18 10:42:21 +00:00
if domain == AF_INET6 && SocketDisableIPv6 {
2024-02-18 10:42:21 +00:00
return -1, EAFNOSUPPORT
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fd, err = socket(domain, typ, proto)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
2024-02-18 10:42:21 +00:00
var fdx [2]int32
2024-02-18 10:42:21 +00:00
err = socketpair(domain, typ, proto, &fdx)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
fd[0] = int(fdx[0])
2024-02-18 10:42:21 +00:00
fd[1] = int(fdx[1])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
var ioSync int64
func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
func SetNonblock(fd int, nonblocking bool) (err error) {
2024-02-18 10:42:21 +00:00
flag, err := fcntl(fd, F_GETFL, 0)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if (flag&O_NONBLOCK != 0) == nonblocking {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if nonblocking {
2024-02-18 10:42:21 +00:00
flag |= O_NONBLOCK
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
flag &= ^O_NONBLOCK
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_, err = fcntl(fd, F_SETFL, flag)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Exec calls execve(2), which replaces the calling executable in the process
2024-02-18 10:42:21 +00:00
// tree. argv0 should be the full path to an executable ("/bin/ls") and the
2024-02-18 10:42:21 +00:00
// executable name should also be the first argument in argv (["ls", "-l"]).
2024-02-18 10:42:21 +00:00
// envv are the environment variables that should be passed to the new
2024-02-18 10:42:21 +00:00
// process (["USER=go", "PWD=/tmp"]).
2024-02-18 10:42:21 +00:00
func Exec(argv0 string, argv []string, envv []string) error {
2024-02-18 10:42:21 +00:00
return syscall.Exec(argv0, argv, envv)
2024-02-18 10:42:21 +00:00
}
// Lutimes sets the access and modification times tv on path. If path refers to
2024-02-18 10:42:21 +00:00
// a symlink, it is not dereferenced and the timestamps are set on the symlink.
2024-02-18 10:42:21 +00:00
// If tv is nil, the access and modification times are set to the current time.
2024-02-18 10:42:21 +00:00
// Otherwise tv must contain exactly 2 elements, with access time as the first
2024-02-18 10:42:21 +00:00
// element and modification time as the second element.
2024-02-18 10:42:21 +00:00
func Lutimes(path string, tv []Timeval) error {
2024-02-18 10:42:21 +00:00
if tv == nil {
2024-02-18 10:42:21 +00:00
return UtimesNanoAt(AT_FDCWD, path, nil, AT_SYMLINK_NOFOLLOW)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(tv) != 2 {
2024-02-18 10:42:21 +00:00
return EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
ts := []Timespec{
2024-02-18 10:42:21 +00:00
NsecToTimespec(TimevalToNsec(tv[0])),
2024-02-18 10:42:21 +00:00
NsecToTimespec(TimevalToNsec(tv[1])),
}
2024-02-18 10:42:21 +00:00
return UtimesNanoAt(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW)
2024-02-18 10:42:21 +00:00
}
// emptyIovecs reports whether there are no bytes in the slice of Iovec.
2024-02-18 10:42:21 +00:00
func emptyIovecs(iov []Iovec) bool {
2024-02-18 10:42:21 +00:00
for i := range iov {
2024-02-18 10:42:21 +00:00
if iov[i].Len > 0 {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Setrlimit sets a resource limit.
2024-02-18 10:42:21 +00:00
func Setrlimit(resource int, rlim *Rlimit) error {
2024-02-18 10:42:21 +00:00
// Just call the syscall version, because as of Go 1.21
2024-02-18 10:42:21 +00:00
// it will affect starting a new process.
2024-02-18 10:42:21 +00:00
return syscall.Setrlimit(resource, (*syscall.Rlimit)(rlim))
2024-02-18 10:42:21 +00:00
}