gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

syscall_unix.go (16642B)


      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 //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
      6 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
      7 
      8 package unix
      9 
     10 import (
     11 	"bytes"
     12 	"sort"
     13 	"sync"
     14 	"syscall"
     15 	"unsafe"
     16 )
     17 
     18 var (
     19 	Stdin  = 0
     20 	Stdout = 1
     21 	Stderr = 2
     22 )
     23 
     24 // Do the interface allocations only once for common
     25 // Errno values.
     26 var (
     27 	errEAGAIN error = syscall.EAGAIN
     28 	errEINVAL error = syscall.EINVAL
     29 	errENOENT error = syscall.ENOENT
     30 )
     31 
     32 var (
     33 	signalNameMapOnce sync.Once
     34 	signalNameMap     map[string]syscall.Signal
     35 )
     36 
     37 // errnoErr returns common boxed Errno values, to prevent
     38 // allocations at runtime.
     39 func errnoErr(e syscall.Errno) error {
     40 	switch e {
     41 	case 0:
     42 		return nil
     43 	case EAGAIN:
     44 		return errEAGAIN
     45 	case EINVAL:
     46 		return errEINVAL
     47 	case ENOENT:
     48 		return errENOENT
     49 	}
     50 	return e
     51 }
     52 
     53 // ErrnoName returns the error name for error number e.
     54 func ErrnoName(e syscall.Errno) string {
     55 	i := sort.Search(len(errorList), func(i int) bool {
     56 		return errorList[i].num >= e
     57 	})
     58 	if i < len(errorList) && errorList[i].num == e {
     59 		return errorList[i].name
     60 	}
     61 	return ""
     62 }
     63 
     64 // SignalName returns the signal name for signal number s.
     65 func SignalName(s syscall.Signal) string {
     66 	i := sort.Search(len(signalList), func(i int) bool {
     67 		return signalList[i].num >= s
     68 	})
     69 	if i < len(signalList) && signalList[i].num == s {
     70 		return signalList[i].name
     71 	}
     72 	return ""
     73 }
     74 
     75 // SignalNum returns the syscall.Signal for signal named s,
     76 // or 0 if a signal with such name is not found.
     77 // The signal name should start with "SIG".
     78 func SignalNum(s string) syscall.Signal {
     79 	signalNameMapOnce.Do(func() {
     80 		signalNameMap = make(map[string]syscall.Signal, len(signalList))
     81 		for _, signal := range signalList {
     82 			signalNameMap[signal.name] = signal.num
     83 		}
     84 	})
     85 	return signalNameMap[s]
     86 }
     87 
     88 // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
     89 func clen(n []byte) int {
     90 	i := bytes.IndexByte(n, 0)
     91 	if i == -1 {
     92 		i = len(n)
     93 	}
     94 	return i
     95 }
     96 
     97 // Mmap manager, for use by operating system-specific implementations.
     98 
     99 type mmapper struct {
    100 	sync.Mutex
    101 	active map[*byte][]byte // active mappings; key is last byte in mapping
    102 	mmap   func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
    103 	munmap func(addr uintptr, length uintptr) error
    104 }
    105 
    106 func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
    107 	if length <= 0 {
    108 		return nil, EINVAL
    109 	}
    110 
    111 	// Map the requested memory.
    112 	addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
    113 	if errno != nil {
    114 		return nil, errno
    115 	}
    116 
    117 	// Use unsafe to convert addr into a []byte.
    118 	b := unsafe.Slice((*byte)(unsafe.Pointer(addr)), length)
    119 
    120 	// Register mapping in m and return it.
    121 	p := &b[cap(b)-1]
    122 	m.Lock()
    123 	defer m.Unlock()
    124 	m.active[p] = b
    125 	return b, nil
    126 }
    127 
    128 func (m *mmapper) Munmap(data []byte) (err error) {
    129 	if len(data) == 0 || len(data) != cap(data) {
    130 		return EINVAL
    131 	}
    132 
    133 	// Find the base of the mapping.
    134 	p := &data[cap(data)-1]
    135 	m.Lock()
    136 	defer m.Unlock()
    137 	b := m.active[p]
    138 	if b == nil || &b[0] != &data[0] {
    139 		return EINVAL
    140 	}
    141 
    142 	// Unmap the memory and update m.
    143 	if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
    144 		return errno
    145 	}
    146 	delete(m.active, p)
    147 	return nil
    148 }
    149 
    150 func Read(fd int, p []byte) (n int, err error) {
    151 	n, err = read(fd, p)
    152 	if raceenabled {
    153 		if n > 0 {
    154 			raceWriteRange(unsafe.Pointer(&p[0]), n)
    155 		}
    156 		if err == nil {
    157 			raceAcquire(unsafe.Pointer(&ioSync))
    158 		}
    159 	}
    160 	return
    161 }
    162 
    163 func Write(fd int, p []byte) (n int, err error) {
    164 	if raceenabled {
    165 		raceReleaseMerge(unsafe.Pointer(&ioSync))
    166 	}
    167 	n, err = write(fd, p)
    168 	if raceenabled && n > 0 {
    169 		raceReadRange(unsafe.Pointer(&p[0]), n)
    170 	}
    171 	return
    172 }
    173 
    174 func Pread(fd int, p []byte, offset int64) (n int, err error) {
    175 	n, err = pread(fd, p, offset)
    176 	if raceenabled {
    177 		if n > 0 {
    178 			raceWriteRange(unsafe.Pointer(&p[0]), n)
    179 		}
    180 		if err == nil {
    181 			raceAcquire(unsafe.Pointer(&ioSync))
    182 		}
    183 	}
    184 	return
    185 }
    186 
    187 func Pwrite(fd int, p []byte, offset int64) (n int, err error) {
    188 	if raceenabled {
    189 		raceReleaseMerge(unsafe.Pointer(&ioSync))
    190 	}
    191 	n, err = pwrite(fd, p, offset)
    192 	if raceenabled && n > 0 {
    193 		raceReadRange(unsafe.Pointer(&p[0]), n)
    194 	}
    195 	return
    196 }
    197 
    198 // For testing: clients can set this flag to force
    199 // creation of IPv6 sockets to return EAFNOSUPPORT.
    200 var SocketDisableIPv6 bool
    201 
    202 // Sockaddr represents a socket address.
    203 type Sockaddr interface {
    204 	sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
    205 }
    206 
    207 // SockaddrInet4 implements the Sockaddr interface for AF_INET type sockets.
    208 type SockaddrInet4 struct {
    209 	Port int
    210 	Addr [4]byte
    211 	raw  RawSockaddrInet4
    212 }
    213 
    214 // SockaddrInet6 implements the Sockaddr interface for AF_INET6 type sockets.
    215 type SockaddrInet6 struct {
    216 	Port   int
    217 	ZoneId uint32
    218 	Addr   [16]byte
    219 	raw    RawSockaddrInet6
    220 }
    221 
    222 // SockaddrUnix implements the Sockaddr interface for AF_UNIX type sockets.
    223 type SockaddrUnix struct {
    224 	Name string
    225 	raw  RawSockaddrUnix
    226 }
    227 
    228 func Bind(fd int, sa Sockaddr) (err error) {
    229 	ptr, n, err := sa.sockaddr()
    230 	if err != nil {
    231 		return err
    232 	}
    233 	return bind(fd, ptr, n)
    234 }
    235 
    236 func Connect(fd int, sa Sockaddr) (err error) {
    237 	ptr, n, err := sa.sockaddr()
    238 	if err != nil {
    239 		return err
    240 	}
    241 	return connect(fd, ptr, n)
    242 }
    243 
    244 func Getpeername(fd int) (sa Sockaddr, err error) {
    245 	var rsa RawSockaddrAny
    246 	var len _Socklen = SizeofSockaddrAny
    247 	if err = getpeername(fd, &rsa, &len); err != nil {
    248 		return
    249 	}
    250 	return anyToSockaddr(fd, &rsa)
    251 }
    252 
    253 func GetsockoptByte(fd, level, opt int) (value byte, err error) {
    254 	var n byte
    255 	vallen := _Socklen(1)
    256 	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
    257 	return n, err
    258 }
    259 
    260 func GetsockoptInt(fd, level, opt int) (value int, err error) {
    261 	var n int32
    262 	vallen := _Socklen(4)
    263 	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
    264 	return int(n), err
    265 }
    266 
    267 func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
    268 	vallen := _Socklen(4)
    269 	err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
    270 	return value, err
    271 }
    272 
    273 func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
    274 	var value IPMreq
    275 	vallen := _Socklen(SizeofIPMreq)
    276 	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
    277 	return &value, err
    278 }
    279 
    280 func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
    281 	var value IPv6Mreq
    282 	vallen := _Socklen(SizeofIPv6Mreq)
    283 	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
    284 	return &value, err
    285 }
    286 
    287 func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
    288 	var value IPv6MTUInfo
    289 	vallen := _Socklen(SizeofIPv6MTUInfo)
    290 	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
    291 	return &value, err
    292 }
    293 
    294 func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
    295 	var value ICMPv6Filter
    296 	vallen := _Socklen(SizeofICMPv6Filter)
    297 	err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
    298 	return &value, err
    299 }
    300 
    301 func GetsockoptLinger(fd, level, opt int) (*Linger, error) {
    302 	var linger Linger
    303 	vallen := _Socklen(SizeofLinger)
    304 	err := getsockopt(fd, level, opt, unsafe.Pointer(&linger), &vallen)
    305 	return &linger, err
    306 }
    307 
    308 func GetsockoptTimeval(fd, level, opt int) (*Timeval, error) {
    309 	var tv Timeval
    310 	vallen := _Socklen(unsafe.Sizeof(tv))
    311 	err := getsockopt(fd, level, opt, unsafe.Pointer(&tv), &vallen)
    312 	return &tv, err
    313 }
    314 
    315 func GetsockoptUint64(fd, level, opt int) (value uint64, err error) {
    316 	var n uint64
    317 	vallen := _Socklen(8)
    318 	err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
    319 	return n, err
    320 }
    321 
    322 func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
    323 	var rsa RawSockaddrAny
    324 	var len _Socklen = SizeofSockaddrAny
    325 	if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
    326 		return
    327 	}
    328 	if rsa.Addr.Family != AF_UNSPEC {
    329 		from, err = anyToSockaddr(fd, &rsa)
    330 	}
    331 	return
    332 }
    333 
    334 // Recvmsg receives a message from a socket using the recvmsg system call. The
    335 // received non-control data will be written to p, and any "out of band"
    336 // control data will be written to oob. The flags are passed to recvmsg.
    337 //
    338 // The results are:
    339 //   - n is the number of non-control data bytes read into p
    340 //   - oobn is the number of control data bytes read into oob; this may be interpreted using [ParseSocketControlMessage]
    341 //   - recvflags is flags returned by recvmsg
    342 //   - from is the address of the sender
    343 //
    344 // If the underlying socket type is not SOCK_DGRAM, a received message
    345 // containing oob data and a single '\0' of non-control data is treated as if
    346 // the message contained only control data, i.e. n will be zero on return.
    347 func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
    348 	var iov [1]Iovec
    349 	if len(p) > 0 {
    350 		iov[0].Base = &p[0]
    351 		iov[0].SetLen(len(p))
    352 	}
    353 	var rsa RawSockaddrAny
    354 	n, oobn, recvflags, err = recvmsgRaw(fd, iov[:], oob, flags, &rsa)
    355 	// source address is only specified if the socket is unconnected
    356 	if rsa.Addr.Family != AF_UNSPEC {
    357 		from, err = anyToSockaddr(fd, &rsa)
    358 	}
    359 	return
    360 }
    361 
    362 // RecvmsgBuffers receives a message from a socket using the recvmsg system
    363 // call. This function is equivalent to Recvmsg, but non-control data read is
    364 // scattered into the buffers slices.
    365 func RecvmsgBuffers(fd int, buffers [][]byte, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
    366 	iov := make([]Iovec, len(buffers))
    367 	for i := range buffers {
    368 		if len(buffers[i]) > 0 {
    369 			iov[i].Base = &buffers[i][0]
    370 			iov[i].SetLen(len(buffers[i]))
    371 		} else {
    372 			iov[i].Base = (*byte)(unsafe.Pointer(&_zero))
    373 		}
    374 	}
    375 	var rsa RawSockaddrAny
    376 	n, oobn, recvflags, err = recvmsgRaw(fd, iov, oob, flags, &rsa)
    377 	if err == nil && rsa.Addr.Family != AF_UNSPEC {
    378 		from, err = anyToSockaddr(fd, &rsa)
    379 	}
    380 	return
    381 }
    382 
    383 // Sendmsg sends a message on a socket to an address using the sendmsg system
    384 // call. This function is equivalent to SendmsgN, but does not return the
    385 // number of bytes actually sent.
    386 func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
    387 	_, err = SendmsgN(fd, p, oob, to, flags)
    388 	return
    389 }
    390 
    391 // SendmsgN sends a message on a socket to an address using the sendmsg system
    392 // call. p contains the non-control data to send, and oob contains the "out of
    393 // band" control data. The flags are passed to sendmsg. The number of
    394 // non-control bytes actually written to the socket is returned.
    395 //
    396 // Some socket types do not support sending control data without accompanying
    397 // non-control data. If p is empty, and oob contains control data, and the
    398 // underlying socket type is not SOCK_DGRAM, p will be treated as containing a
    399 // single '\0' and the return value will indicate zero bytes sent.
    400 //
    401 // The Go function Recvmsg, if called with an empty p and a non-empty oob,
    402 // will read and ignore this additional '\0'.  If the message is received by
    403 // code that does not use Recvmsg, or that does not use Go at all, that code
    404 // will need to be written to expect and ignore the additional '\0'.
    405 //
    406 // If you need to send non-empty oob with p actually empty, and if the
    407 // underlying socket type supports it, you can do so via a raw system call as
    408 // follows:
    409 //
    410 //	msg := &unix.Msghdr{
    411 //	    Control: &oob[0],
    412 //	}
    413 //	msg.SetControllen(len(oob))
    414 //	n, _, errno := unix.Syscall(unix.SYS_SENDMSG, uintptr(fd), uintptr(unsafe.Pointer(msg)), flags)
    415 func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
    416 	var iov [1]Iovec
    417 	if len(p) > 0 {
    418 		iov[0].Base = &p[0]
    419 		iov[0].SetLen(len(p))
    420 	}
    421 	var ptr unsafe.Pointer
    422 	var salen _Socklen
    423 	if to != nil {
    424 		ptr, salen, err = to.sockaddr()
    425 		if err != nil {
    426 			return 0, err
    427 		}
    428 	}
    429 	return sendmsgN(fd, iov[:], oob, ptr, salen, flags)
    430 }
    431 
    432 // SendmsgBuffers sends a message on a socket to an address using the sendmsg
    433 // system call. This function is equivalent to SendmsgN, but the non-control
    434 // data is gathered from buffers.
    435 func SendmsgBuffers(fd int, buffers [][]byte, oob []byte, to Sockaddr, flags int) (n int, err error) {
    436 	iov := make([]Iovec, len(buffers))
    437 	for i := range buffers {
    438 		if len(buffers[i]) > 0 {
    439 			iov[i].Base = &buffers[i][0]
    440 			iov[i].SetLen(len(buffers[i]))
    441 		} else {
    442 			iov[i].Base = (*byte)(unsafe.Pointer(&_zero))
    443 		}
    444 	}
    445 	var ptr unsafe.Pointer
    446 	var salen _Socklen
    447 	if to != nil {
    448 		ptr, salen, err = to.sockaddr()
    449 		if err != nil {
    450 			return 0, err
    451 		}
    452 	}
    453 	return sendmsgN(fd, iov, oob, ptr, salen, flags)
    454 }
    455 
    456 func Send(s int, buf []byte, flags int) (err error) {
    457 	return sendto(s, buf, flags, nil, 0)
    458 }
    459 
    460 func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
    461 	var ptr unsafe.Pointer
    462 	var salen _Socklen
    463 	if to != nil {
    464 		ptr, salen, err = to.sockaddr()
    465 		if err != nil {
    466 			return err
    467 		}
    468 	}
    469 	return sendto(fd, p, flags, ptr, salen)
    470 }
    471 
    472 func SetsockoptByte(fd, level, opt int, value byte) (err error) {
    473 	return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
    474 }
    475 
    476 func SetsockoptInt(fd, level, opt int, value int) (err error) {
    477 	var n = int32(value)
    478 	return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
    479 }
    480 
    481 func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
    482 	return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
    483 }
    484 
    485 func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
    486 	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
    487 }
    488 
    489 func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
    490 	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
    491 }
    492 
    493 func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
    494 	return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
    495 }
    496 
    497 func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
    498 	return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
    499 }
    500 
    501 func SetsockoptString(fd, level, opt int, s string) (err error) {
    502 	var p unsafe.Pointer
    503 	if len(s) > 0 {
    504 		p = unsafe.Pointer(&[]byte(s)[0])
    505 	}
    506 	return setsockopt(fd, level, opt, p, uintptr(len(s)))
    507 }
    508 
    509 func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
    510 	return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
    511 }
    512 
    513 func SetsockoptUint64(fd, level, opt int, value uint64) (err error) {
    514 	return setsockopt(fd, level, opt, unsafe.Pointer(&value), 8)
    515 }
    516 
    517 func Socket(domain, typ, proto int) (fd int, err error) {
    518 	if domain == AF_INET6 && SocketDisableIPv6 {
    519 		return -1, EAFNOSUPPORT
    520 	}
    521 	fd, err = socket(domain, typ, proto)
    522 	return
    523 }
    524 
    525 func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
    526 	var fdx [2]int32
    527 	err = socketpair(domain, typ, proto, &fdx)
    528 	if err == nil {
    529 		fd[0] = int(fdx[0])
    530 		fd[1] = int(fdx[1])
    531 	}
    532 	return
    533 }
    534 
    535 var ioSync int64
    536 
    537 func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
    538 
    539 func SetNonblock(fd int, nonblocking bool) (err error) {
    540 	flag, err := fcntl(fd, F_GETFL, 0)
    541 	if err != nil {
    542 		return err
    543 	}
    544 	if nonblocking {
    545 		flag |= O_NONBLOCK
    546 	} else {
    547 		flag &= ^O_NONBLOCK
    548 	}
    549 	_, err = fcntl(fd, F_SETFL, flag)
    550 	return err
    551 }
    552 
    553 // Exec calls execve(2), which replaces the calling executable in the process
    554 // tree. argv0 should be the full path to an executable ("/bin/ls") and the
    555 // executable name should also be the first argument in argv (["ls", "-l"]).
    556 // envv are the environment variables that should be passed to the new
    557 // process (["USER=go", "PWD=/tmp"]).
    558 func Exec(argv0 string, argv []string, envv []string) error {
    559 	return syscall.Exec(argv0, argv, envv)
    560 }
    561 
    562 // Lutimes sets the access and modification times tv on path. If path refers to
    563 // a symlink, it is not dereferenced and the timestamps are set on the symlink.
    564 // If tv is nil, the access and modification times are set to the current time.
    565 // Otherwise tv must contain exactly 2 elements, with access time as the first
    566 // element and modification time as the second element.
    567 func Lutimes(path string, tv []Timeval) error {
    568 	if tv == nil {
    569 		return UtimesNanoAt(AT_FDCWD, path, nil, AT_SYMLINK_NOFOLLOW)
    570 	}
    571 	if len(tv) != 2 {
    572 		return EINVAL
    573 	}
    574 	ts := []Timespec{
    575 		NsecToTimespec(TimevalToNsec(tv[0])),
    576 		NsecToTimespec(TimevalToNsec(tv[1])),
    577 	}
    578 	return UtimesNanoAt(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW)
    579 }
    580 
    581 // emptyIovecs reports whether there are no bytes in the slice of Iovec.
    582 func emptyIovecs(iov []Iovec) bool {
    583 	for i := range iov {
    584 		if iov[i].Len > 0 {
    585 			return false
    586 		}
    587 	}
    588 	return true
    589 }
    590 
    591 // Setrlimit sets a resource limit.
    592 func Setrlimit(resource int, rlim *Rlimit) error {
    593 	// Just call the syscall version, because as of Go 1.21
    594 	// it will affect starting a new process.
    595 	return syscall.Setrlimit(resource, (*syscall.Rlimit)(rlim))
    596 }