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

1938 lines
32 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.
// Solaris system calls.
2024-02-18 10:42:21 +00:00
// This file is compiled as ordinary Go code,
2024-02-18 10:42:21 +00:00
// but it is also input to mksyscall,
2024-02-18 10:42:21 +00:00
// which parses the //sys lines and generates system call stubs.
2024-02-18 10:42:21 +00:00
// Note that sometimes we use a lowercase //sys name and wrap
2024-02-18 10:42:21 +00:00
// it in our own nicer implementation, either here or in
2024-02-18 10:42:21 +00:00
// syscall_solaris.go or syscall_unix.go.
package unix
import (
"fmt"
"os"
"runtime"
"sync"
"syscall"
"unsafe"
)
// Implemented in runtime/syscall_solaris.go.
2024-02-18 10:42:21 +00:00
type syscallFunc uintptr
func rawSysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
2024-02-18 10:42:21 +00:00
func sysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
// SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrDatalink struct {
Family uint16
Index uint16
Type uint8
Nlen uint8
Alen uint8
Slen uint8
Data [244]int8
raw RawSockaddrDatalink
2024-02-18 10:42:21 +00:00
}
func direntIno(buf []byte) (uint64, bool) {
2024-02-18 10:42:21 +00:00
return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
2024-02-18 10:42:21 +00:00
}
func direntReclen(buf []byte) (uint64, bool) {
2024-02-18 10:42:21 +00:00
return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
2024-02-18 10:42:21 +00:00
}
func direntNamlen(buf []byte) (uint64, bool) {
2024-02-18 10:42:21 +00:00
reclen, ok := direntReclen(buf)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return 0, false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
2024-02-18 10:42:21 +00:00
}
//sysnb pipe(p *[2]_C_int) (n int, err error)
func Pipe(p []int) (err error) {
2024-02-18 10:42:21 +00:00
if len(p) != 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
var pp [2]_C_int
2024-02-18 10:42:21 +00:00
n, err := pipe(&pp)
2024-02-18 10:42:21 +00:00
if n != 0 {
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 err == nil {
2024-02-18 10:42:21 +00:00
p[0] = int(pp[0])
2024-02-18 10:42:21 +00:00
p[1] = int(pp[1])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
//sysnb pipe2(p *[2]_C_int, flags int) (err error)
func Pipe2(p []int, flags int) error {
2024-02-18 10:42:21 +00:00
if len(p) != 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
var pp [2]_C_int
2024-02-18 10:42:21 +00:00
err := pipe2(&pp, flags)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
p[0] = int(pp[0])
2024-02-18 10:42:21 +00:00
p[1] = int(pp[1])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
if sa.Port < 0 || sa.Port > 0xFFFF {
2024-02-18 10:42:21 +00:00
return nil, 0, EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_INET
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
2024-02-18 10:42:21 +00:00
p[0] = byte(sa.Port >> 8)
2024-02-18 10:42:21 +00:00
p[1] = byte(sa.Port)
2024-02-18 10:42:21 +00:00
sa.raw.Addr = sa.Addr
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
if sa.Port < 0 || sa.Port > 0xFFFF {
2024-02-18 10:42:21 +00:00
return nil, 0, EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_INET6
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
2024-02-18 10:42:21 +00:00
p[0] = byte(sa.Port >> 8)
2024-02-18 10:42:21 +00:00
p[1] = byte(sa.Port)
2024-02-18 10:42:21 +00:00
sa.raw.Scope_id = sa.ZoneId
2024-02-18 10:42:21 +00:00
sa.raw.Addr = sa.Addr
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
name := sa.Name
2024-02-18 10:42:21 +00:00
n := len(name)
2024-02-18 10:42:21 +00:00
if n >= len(sa.raw.Path) {
2024-02-18 10:42:21 +00:00
return nil, 0, EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_UNIX
2024-02-18 10:42:21 +00:00
for i := 0; i < n; i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Path[i] = int8(name[i])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// length is family (uint16), name, NUL.
2024-02-18 10:42:21 +00:00
sl := _Socklen(2)
2024-02-18 10:42:21 +00:00
if n > 0 {
2024-02-18 10:42:21 +00:00
sl += _Socklen(n) + 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) {
2024-02-18 10:42:21 +00:00
// Check sl > 3 so we don't change unnamed socket behavior.
2024-02-18 10:42:21 +00:00
sa.raw.Path[0] = 0
2024-02-18 10:42:21 +00:00
// Don't count trailing NUL for abstract address.
2024-02-18 10:42:21 +00:00
sl--
2024-02-18 10:42:21 +00:00
}
return unsafe.Pointer(&sa.raw), sl, nil
2024-02-18 10:42:21 +00:00
}
//sys getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getsockname
func Getsockname(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 = getsockname(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
}
// GetsockoptString returns the string value of the socket option opt for the
2024-02-18 10:42:21 +00:00
// socket associated with fd at the given socket level.
2024-02-18 10:42:21 +00:00
func GetsockoptString(fd, level, opt int) (string, error) {
2024-02-18 10:42:21 +00:00
buf := make([]byte, 256)
2024-02-18 10:42:21 +00:00
vallen := _Socklen(len(buf))
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen)
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 ByteSliceToString(buf[:vallen]), nil
2024-02-18 10:42:21 +00:00
}
const ImplementsGetwd = true
//sys Getcwd(buf []byte) (n int, err error)
func Getwd() (wd string, err error) {
2024-02-18 10:42:21 +00:00
var buf [PathMax]byte
2024-02-18 10:42:21 +00:00
// Getcwd will return an error if it failed for any reason.
2024-02-18 10:42:21 +00:00
_, err = Getcwd(buf[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
n := clen(buf[:])
2024-02-18 10:42:21 +00:00
if n < 1 {
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
return string(buf[:n]), nil
2024-02-18 10:42:21 +00:00
}
/*
2024-02-18 10:42:21 +00:00
* Wrapped
2024-02-18 10:42:21 +00:00
*/
//sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error)
2024-02-18 10:42:21 +00:00
//sysnb setgroups(ngid int, gid *_Gid_t) (err error)
func Getgroups() (gids []int, err error) {
2024-02-18 10:42:21 +00:00
n, err := getgroups(0, nil)
2024-02-18 10:42:21 +00:00
// Check for error and sanity check group count. Newer versions of
2024-02-18 10:42:21 +00:00
// Solaris allow up to 1024 (NGROUPS_MAX).
2024-02-18 10:42:21 +00:00
if n < 0 || n > 1024 {
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, EINVAL
2024-02-18 10:42:21 +00:00
} else if n == 0 {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
a := make([]_Gid_t, n)
2024-02-18 10:42:21 +00:00
n, err = getgroups(n, &a[0])
2024-02-18 10:42:21 +00:00
if n == -1 {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
gids = make([]int, n)
2024-02-18 10:42:21 +00:00
for i, v := range a[0:n] {
2024-02-18 10:42:21 +00:00
gids[i] = int(v)
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 Setgroups(gids []int) (err error) {
2024-02-18 10:42:21 +00:00
if len(gids) == 0 {
2024-02-18 10:42:21 +00:00
return setgroups(0, nil)
2024-02-18 10:42:21 +00:00
}
a := make([]_Gid_t, len(gids))
2024-02-18 10:42:21 +00:00
for i, v := range gids {
2024-02-18 10:42:21 +00:00
a[i] = _Gid_t(v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return setgroups(len(a), &a[0])
2024-02-18 10:42:21 +00:00
}
// ReadDirent reads directory entries from fd and writes them into buf.
2024-02-18 10:42:21 +00:00
func ReadDirent(fd int, buf []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
// Final argument is (basep *uintptr) and the syscall doesn't take nil.
2024-02-18 10:42:21 +00:00
// TODO(rsc): Can we use a single global basep for all calls?
2024-02-18 10:42:21 +00:00
return Getdents(fd, buf, new(uintptr))
2024-02-18 10:42:21 +00:00
}
// Wait status is 7 bits at bottom, either 0 (exited),
2024-02-18 10:42:21 +00:00
// 0x7F (stopped), or a signal number that caused an exit.
2024-02-18 10:42:21 +00:00
// The 0x80 bit is whether there was a core dump.
2024-02-18 10:42:21 +00:00
// An extra number (exit code, signal causing a stop)
2024-02-18 10:42:21 +00:00
// is in the high bits.
type WaitStatus uint32
const (
mask = 0x7F
core = 0x80
2024-02-18 10:42:21 +00:00
shift = 8
exited = 0
2024-02-18 10:42:21 +00:00
stopped = 0x7F
)
func (w WaitStatus) Exited() bool { return w&mask == exited }
func (w WaitStatus) ExitStatus() int {
2024-02-18 10:42:21 +00:00
if w&mask != exited {
2024-02-18 10:42:21 +00:00
return -1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return int(w >> shift)
2024-02-18 10:42:21 +00:00
}
func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
func (w WaitStatus) Signal() syscall.Signal {
2024-02-18 10:42:21 +00:00
sig := syscall.Signal(w & mask)
2024-02-18 10:42:21 +00:00
if sig == stopped || sig == 0 {
2024-02-18 10:42:21 +00:00
return -1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sig
2024-02-18 10:42:21 +00:00
}
func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
func (w WaitStatus) Stopped() bool { return w&mask == stopped && syscall.Signal(w>>shift) != SIGSTOP }
func (w WaitStatus) Continued() bool { return w&mask == stopped && syscall.Signal(w>>shift) == SIGSTOP }
func (w WaitStatus) StopSignal() syscall.Signal {
2024-02-18 10:42:21 +00:00
if !w.Stopped() {
2024-02-18 10:42:21 +00:00
return -1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return syscall.Signal(w>>shift) & 0xFF
2024-02-18 10:42:21 +00:00
}
func (w WaitStatus) TrapCause() int { return -1 }
//sys wait4(pid int32, statusp *_C_int, options int, rusage *Rusage) (wpid int32, err error)
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (int, error) {
2024-02-18 10:42:21 +00:00
var status _C_int
2024-02-18 10:42:21 +00:00
rpid, err := wait4(int32(pid), &status, options, rusage)
2024-02-18 10:42:21 +00:00
wpid := int(rpid)
2024-02-18 10:42:21 +00:00
if wpid == -1 {
2024-02-18 10:42:21 +00:00
return wpid, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if wstatus != nil {
2024-02-18 10:42:21 +00:00
*wstatus = WaitStatus(status)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return wpid, nil
2024-02-18 10:42:21 +00:00
}
//sys gethostname(buf []byte) (n int, err error)
func Gethostname() (name string, err error) {
2024-02-18 10:42:21 +00:00
var buf [MaxHostNameLen]byte
2024-02-18 10:42:21 +00:00
n, err := gethostname(buf[:])
2024-02-18 10:42:21 +00:00
if n != 0 {
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
n = clen(buf[:])
2024-02-18 10:42:21 +00:00
if n < 1 {
2024-02-18 10:42:21 +00:00
return "", EFAULT
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(buf[:n]), nil
2024-02-18 10:42:21 +00:00
}
//sys utimes(path string, times *[2]Timeval) (err error)
func Utimes(path string, tv []Timeval) (err error) {
2024-02-18 10:42:21 +00:00
if tv == nil {
2024-02-18 10:42:21 +00:00
return utimes(path, nil)
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
return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
2024-02-18 10:42:21 +00:00
}
//sys utimensat(fd int, path string, times *[2]Timespec, flag int) (err error)
func UtimesNano(path string, ts []Timespec) error {
2024-02-18 10:42:21 +00:00
if ts == nil {
2024-02-18 10:42:21 +00:00
return utimensat(AT_FDCWD, path, nil, 0)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(ts) != 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
return utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
2024-02-18 10:42:21 +00:00
}
func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
2024-02-18 10:42:21 +00:00
if ts == nil {
2024-02-18 10:42:21 +00:00
return utimensat(dirfd, path, nil, flags)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(ts) != 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
return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
2024-02-18 10:42:21 +00:00
}
//sys fcntl(fd int, cmd int, arg int) (val int, err error)
// FcntlInt performs a fcntl syscall on fd with the provided command and argument.
2024-02-18 10:42:21 +00:00
func FcntlInt(fd uintptr, cmd, arg int) (int, error) {
2024-02-18 10:42:21 +00:00
valptr, _, errno := sysvicall6(uintptr(unsafe.Pointer(&procfcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0)
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
if errno != 0 {
2024-02-18 10:42:21 +00:00
err = errno
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return int(valptr), err
2024-02-18 10:42:21 +00:00
}
// FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
2024-02-18 10:42:21 +00:00
func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
2024-02-18 10:42:21 +00:00
_, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procfcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(unsafe.Pointer(lk)), 0, 0, 0)
2024-02-18 10:42:21 +00:00
if e1 != 0 {
2024-02-18 10:42:21 +00:00
return e1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
//sys futimesat(fildes int, path *byte, times *[2]Timeval) (err error)
func Futimesat(dirfd int, path string, tv []Timeval) error {
2024-02-18 10:42:21 +00:00
pathp, err := BytePtrFromString(path)
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 tv == nil {
2024-02-18 10:42:21 +00:00
return futimesat(dirfd, pathp, nil)
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
return futimesat(dirfd, pathp, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
2024-02-18 10:42:21 +00:00
}
// Solaris doesn't have an futimes function because it allows NULL to be
2024-02-18 10:42:21 +00:00
// specified as the path for futimesat. However, Go doesn't like
2024-02-18 10:42:21 +00:00
// NULL-style string interfaces, so this simple wrapper is provided.
2024-02-18 10:42:21 +00:00
func Futimes(fd int, tv []Timeval) error {
2024-02-18 10:42:21 +00:00
if tv == nil {
2024-02-18 10:42:21 +00:00
return futimesat(fd, nil, nil)
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
return futimesat(fd, nil, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
2024-02-18 10:42:21 +00:00
}
func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
2024-02-18 10:42:21 +00:00
switch rsa.Addr.Family {
2024-02-18 10:42:21 +00:00
case AF_UNIX:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrUnix)
2024-02-18 10:42:21 +00:00
// Assume path ends at NUL.
2024-02-18 10:42:21 +00:00
// This is not technically the Solaris semantics for
2024-02-18 10:42:21 +00:00
// abstract Unix domain sockets -- they are supposed
2024-02-18 10:42:21 +00:00
// to be uninterpreted fixed-size binary blobs -- but
2024-02-18 10:42:21 +00:00
// everyone uses this convention.
2024-02-18 10:42:21 +00:00
n := 0
2024-02-18 10:42:21 +00:00
for n < len(pp.Path) && pp.Path[n] != 0 {
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.Name = string(unsafe.Slice((*byte)(unsafe.Pointer(&pp.Path[0])), n))
2024-02-18 10:42:21 +00:00
return sa, nil
case AF_INET:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrInet4)
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&pp.Port))
2024-02-18 10:42:21 +00:00
sa.Port = int(p[0])<<8 + int(p[1])
2024-02-18 10:42:21 +00:00
sa.Addr = pp.Addr
2024-02-18 10:42:21 +00:00
return sa, nil
case AF_INET6:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrInet6)
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&pp.Port))
2024-02-18 10:42:21 +00:00
sa.Port = int(p[0])<<8 + int(p[1])
2024-02-18 10:42:21 +00:00
sa.ZoneId = pp.Scope_id
2024-02-18 10:42:21 +00:00
sa.Addr = pp.Addr
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, EAFNOSUPPORT
2024-02-18 10:42:21 +00:00
}
//sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) = libsocket.accept
func Accept(fd int) (nfd 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
nfd, err = accept(fd, &rsa, &len)
2024-02-18 10:42:21 +00:00
if nfd == -1 {
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
sa, err = anyToSockaddr(fd, &rsa)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
Close(nfd)
2024-02-18 10:42:21 +00:00
nfd = 0
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
}
//sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_recvmsg
func recvmsgRaw(fd int, iov []Iovec, oob []byte, flags int, rsa *RawSockaddrAny) (n, oobn int, recvflags int, err error) {
2024-02-18 10:42:21 +00:00
var msg Msghdr
2024-02-18 10:42:21 +00:00
msg.Name = (*byte)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
msg.Namelen = uint32(SizeofSockaddrAny)
2024-02-18 10:42:21 +00:00
var dummy byte
2024-02-18 10:42:21 +00:00
if len(oob) > 0 {
2024-02-18 10:42:21 +00:00
// receive at least one normal byte
2024-02-18 10:42:21 +00:00
if emptyIovecs(iov) {
2024-02-18 10:42:21 +00:00
var iova [1]Iovec
2024-02-18 10:42:21 +00:00
iova[0].Base = &dummy
2024-02-18 10:42:21 +00:00
iova[0].SetLen(1)
2024-02-18 10:42:21 +00:00
iov = iova[:]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
msg.Accrightslen = int32(len(oob))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(iov) > 0 {
2024-02-18 10:42:21 +00:00
msg.Iov = &iov[0]
2024-02-18 10:42:21 +00:00
msg.SetIovlen(len(iov))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n, err = recvmsg(fd, &msg, flags); n == -1 {
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
oobn = int(msg.Accrightslen)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
//sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_sendmsg
func sendmsgN(fd int, iov []Iovec, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) {
2024-02-18 10:42:21 +00:00
var msg Msghdr
2024-02-18 10:42:21 +00:00
msg.Name = (*byte)(unsafe.Pointer(ptr))
2024-02-18 10:42:21 +00:00
msg.Namelen = uint32(salen)
2024-02-18 10:42:21 +00:00
var dummy byte
2024-02-18 10:42:21 +00:00
var empty bool
2024-02-18 10:42:21 +00:00
if len(oob) > 0 {
2024-02-18 10:42:21 +00:00
// send at least one normal byte
2024-02-18 10:42:21 +00:00
empty = emptyIovecs(iov)
2024-02-18 10:42:21 +00:00
if empty {
2024-02-18 10:42:21 +00:00
var iova [1]Iovec
2024-02-18 10:42:21 +00:00
iova[0].Base = &dummy
2024-02-18 10:42:21 +00:00
iova[0].SetLen(1)
2024-02-18 10:42:21 +00:00
iov = iova[:]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
msg.Accrightslen = int32(len(oob))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(iov) > 0 {
2024-02-18 10:42:21 +00:00
msg.Iov = &iov[0]
2024-02-18 10:42:21 +00:00
msg.SetIovlen(len(iov))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n, err = sendmsg(fd, &msg, flags); 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
if len(oob) > 0 && empty {
2024-02-18 10:42:21 +00:00
n = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, nil
2024-02-18 10:42:21 +00:00
}
//sys acct(path *byte) (err error)
func Acct(path string) (err error) {
2024-02-18 10:42:21 +00:00
if len(path) == 0 {
2024-02-18 10:42:21 +00:00
// Assume caller wants to disable accounting.
2024-02-18 10:42:21 +00:00
return acct(nil)
2024-02-18 10:42:21 +00:00
}
pathp, err := BytePtrFromString(path)
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 acct(pathp)
2024-02-18 10:42:21 +00:00
}
//sys __makedev(version int, major uint, minor uint) (val uint64)
func Mkdev(major, minor uint32) uint64 {
2024-02-18 10:42:21 +00:00
return __makedev(NEWDEV, uint(major), uint(minor))
2024-02-18 10:42:21 +00:00
}
//sys __major(version int, dev uint64) (val uint)
func Major(dev uint64) uint32 {
2024-02-18 10:42:21 +00:00
return uint32(__major(NEWDEV, dev))
2024-02-18 10:42:21 +00:00
}
//sys __minor(version int, dev uint64) (val uint)
func Minor(dev uint64) uint32 {
2024-02-18 10:42:21 +00:00
return uint32(__minor(NEWDEV, dev))
2024-02-18 10:42:21 +00:00
}
/*
2024-02-18 10:42:21 +00:00
* Expose the ioctl function
2024-02-18 10:42:21 +00:00
*/
//sys ioctlRet(fd int, req int, arg uintptr) (ret int, err error) = libc.ioctl
2024-02-18 10:42:21 +00:00
//sys ioctlPtrRet(fd int, req int, arg unsafe.Pointer) (ret int, err error) = libc.ioctl
func ioctl(fd int, req int, arg uintptr) (err error) {
2024-02-18 10:42:21 +00:00
_, err = ioctlRet(fd, req, arg)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func ioctlPtr(fd int, req int, arg unsafe.Pointer) (err error) {
2024-02-18 10:42:21 +00:00
_, err = ioctlPtrRet(fd, req, arg)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func IoctlSetTermio(fd int, req int, value *Termio) error {
2024-02-18 10:42:21 +00:00
return ioctlPtr(fd, req, unsafe.Pointer(value))
2024-02-18 10:42:21 +00:00
}
func IoctlGetTermio(fd int, req int) (*Termio, error) {
2024-02-18 10:42:21 +00:00
var value Termio
2024-02-18 10:42:21 +00:00
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
2024-02-18 10:42:21 +00:00
return &value, err
2024-02-18 10:42:21 +00:00
}
//sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
func Poll(fds []PollFd, timeout int) (n int, err error) {
2024-02-18 10:42:21 +00:00
if len(fds) == 0 {
2024-02-18 10:42:21 +00:00
return poll(nil, 0, timeout)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return poll(&fds[0], len(fds), timeout)
2024-02-18 10:42:21 +00:00
}
func Sendfile(outfd int, infd int, offset *int64, count int) (written 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
return sendfile(outfd, infd, offset, count)
2024-02-18 10:42:21 +00:00
}
/*
2024-02-18 10:42:21 +00:00
* Exposed directly
2024-02-18 10:42:21 +00:00
*/
2024-02-18 10:42:21 +00:00
//sys Access(path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
2024-02-18 10:42:21 +00:00
//sys Chdir(path string) (err error)
2024-02-18 10:42:21 +00:00
//sys Chmod(path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Chown(path string, uid int, gid int) (err error)
2024-02-18 10:42:21 +00:00
//sys Chroot(path string) (err error)
2024-02-18 10:42:21 +00:00
//sys ClockGettime(clockid int32, time *Timespec) (err error)
2024-02-18 10:42:21 +00:00
//sys Close(fd int) (err error)
2024-02-18 10:42:21 +00:00
//sys Creat(path string, mode uint32) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sys Dup(fd int) (nfd int, err error)
2024-02-18 10:42:21 +00:00
//sys Dup2(oldfd int, newfd int) (err error)
2024-02-18 10:42:21 +00:00
//sys Exit(code int)
2024-02-18 10:42:21 +00:00
//sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Fchdir(fd int) (err error)
2024-02-18 10:42:21 +00:00
//sys Fchmod(fd int, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Fchown(fd int, uid int, gid int) (err error)
2024-02-18 10:42:21 +00:00
//sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Fdatasync(fd int) (err error)
2024-02-18 10:42:21 +00:00
//sys Flock(fd int, how int) (err error)
2024-02-18 10:42:21 +00:00
//sys Fpathconf(fd int, name int) (val int, err error)
2024-02-18 10:42:21 +00:00
//sys Fstat(fd int, stat *Stat_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Fstatvfs(fd int, vfsstat *Statvfs_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Getdents(fd int, buf []byte, basep *uintptr) (n int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Getgid() (gid int)
2024-02-18 10:42:21 +00:00
//sysnb Getpid() (pid int)
2024-02-18 10:42:21 +00:00
//sysnb Getpgid(pid int) (pgid int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Getpgrp() (pgid int, err error)
2024-02-18 10:42:21 +00:00
//sys Geteuid() (euid int)
2024-02-18 10:42:21 +00:00
//sys Getegid() (egid int)
2024-02-18 10:42:21 +00:00
//sys Getppid() (ppid int)
2024-02-18 10:42:21 +00:00
//sys Getpriority(which int, who int) (n int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Getrlimit(which int, lim *Rlimit) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Getrusage(who int, rusage *Rusage) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Getsid(pid int) (sid int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Gettimeofday(tv *Timeval) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Getuid() (uid int)
2024-02-18 10:42:21 +00:00
//sys Kill(pid int, signum syscall.Signal) (err error)
2024-02-18 10:42:21 +00:00
//sys Lchown(path string, uid int, gid int) (err error)
2024-02-18 10:42:21 +00:00
//sys Link(path string, link string) (err error)
2024-02-18 10:42:21 +00:00
//sys Listen(s int, backlog int) (err error) = libsocket.__xnet_llisten
2024-02-18 10:42:21 +00:00
//sys Lstat(path string, stat *Stat_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Madvise(b []byte, advice int) (err error)
2024-02-18 10:42:21 +00:00
//sys Mkdir(path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Mkdirat(dirfd int, path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Mkfifo(path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Mkfifoat(dirfd int, path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Mknod(path string, mode uint32, dev int) (err error)
2024-02-18 10:42:21 +00:00
//sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
2024-02-18 10:42:21 +00:00
//sys Mlock(b []byte) (err error)
2024-02-18 10:42:21 +00:00
//sys Mlockall(flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Mprotect(b []byte, prot int) (err error)
2024-02-18 10:42:21 +00:00
//sys Msync(b []byte, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Munlock(b []byte) (err error)
2024-02-18 10:42:21 +00:00
//sys Munlockall() (err error)
2024-02-18 10:42:21 +00:00
//sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
2024-02-18 10:42:21 +00:00
//sys Open(path string, mode int, perm uint32) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sys Pathconf(path string, name int) (val int, err error)
2024-02-18 10:42:21 +00:00
//sys Pause() (err error)
2024-02-18 10:42:21 +00:00
//sys pread(fd int, p []byte, offset int64) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys pwrite(fd int, p []byte, offset int64) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys read(fd int, p []byte) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys Readlink(path string, buf []byte) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys Rename(from string, to string) (err error)
2024-02-18 10:42:21 +00:00
//sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
2024-02-18 10:42:21 +00:00
//sys Rmdir(path string) (err error)
2024-02-18 10:42:21 +00:00
//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = lseek
2024-02-18 10:42:21 +00:00
//sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Setegid(egid int) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Seteuid(euid int) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Setgid(gid int) (err error)
2024-02-18 10:42:21 +00:00
//sys Sethostname(p []byte) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Setpgid(pid int, pgid int) (err error)
2024-02-18 10:42:21 +00:00
//sys Setpriority(which int, who int, prio int) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Setregid(rgid int, egid int) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Setreuid(ruid int, euid int) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Setsid() (pid int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Setuid(uid int) (err error)
2024-02-18 10:42:21 +00:00
//sys Shutdown(s int, how int) (err error) = libsocket.shutdown
2024-02-18 10:42:21 +00:00
//sys Stat(path string, stat *Stat_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Statvfs(path string, vfsstat *Statvfs_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Symlink(path string, link string) (err error)
2024-02-18 10:42:21 +00:00
//sys Sync() (err error)
2024-02-18 10:42:21 +00:00
//sys Sysconf(which int) (n int64, err error)
2024-02-18 10:42:21 +00:00
//sysnb Times(tms *Tms) (ticks uintptr, err error)
2024-02-18 10:42:21 +00:00
//sys Truncate(path string, length int64) (err error)
2024-02-18 10:42:21 +00:00
//sys Fsync(fd int) (err error)
2024-02-18 10:42:21 +00:00
//sys Ftruncate(fd int, length int64) (err error)
2024-02-18 10:42:21 +00:00
//sys Umask(mask int) (oldmask int)
2024-02-18 10:42:21 +00:00
//sysnb Uname(buf *Utsname) (err error)
2024-02-18 10:42:21 +00:00
//sys Unmount(target string, flags int) (err error) = libc.umount
2024-02-18 10:42:21 +00:00
//sys Unlink(path string) (err error)
2024-02-18 10:42:21 +00:00
//sys Unlinkat(dirfd int, path string, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Ustat(dev int, ubuf *Ustat_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Utime(path string, buf *Utimbuf) (err error)
2024-02-18 10:42:21 +00:00
//sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_bind
2024-02-18 10:42:21 +00:00
//sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_connect
2024-02-18 10:42:21 +00:00
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
2024-02-18 10:42:21 +00:00
//sys munmap(addr uintptr, length uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = libsendfile.sendfile
2024-02-18 10:42:21 +00:00
//sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_sendto
2024-02-18 10:42:21 +00:00
//sys socket(domain int, typ int, proto int) (fd int, err error) = libsocket.__xnet_socket
2024-02-18 10:42:21 +00:00
//sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) = libsocket.__xnet_socketpair
2024-02-18 10:42:21 +00:00
//sys write(fd int, p []byte) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) = libsocket.__xnet_getsockopt
2024-02-18 10:42:21 +00:00
//sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getpeername
2024-02-18 10:42:21 +00:00
//sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) = libsocket.setsockopt
2024-02-18 10:42:21 +00:00
//sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) = libsocket.recvfrom
// Event Ports
type fileObjCookie struct {
fobj *fileObj
2024-02-18 10:42:21 +00:00
cookie interface{}
}
// EventPort provides a safe abstraction on top of Solaris/illumos Event Ports.
2024-02-18 10:42:21 +00:00
type EventPort struct {
port int
mu sync.Mutex
fds map[uintptr]*fileObjCookie
2024-02-18 10:42:21 +00:00
paths map[string]*fileObjCookie
2024-02-18 10:42:21 +00:00
// The user cookie presents an interesting challenge from a memory management perspective.
2024-02-18 10:42:21 +00:00
// There are two paths by which we can discover that it is no longer in use:
2024-02-18 10:42:21 +00:00
// 1. The user calls port_dissociate before any events fire
2024-02-18 10:42:21 +00:00
// 2. An event fires and we return it to the user
2024-02-18 10:42:21 +00:00
// The tricky situation is if the event has fired in the kernel but
2024-02-18 10:42:21 +00:00
// the user hasn't requested/received it yet.
2024-02-18 10:42:21 +00:00
// If the user wants to port_dissociate before the event has been processed,
2024-02-18 10:42:21 +00:00
// we should handle things gracefully. To do so, we need to keep an extra
2024-02-18 10:42:21 +00:00
// reference to the cookie around until the event is processed
2024-02-18 10:42:21 +00:00
// thus the otherwise seemingly extraneous "cookies" map
2024-02-18 10:42:21 +00:00
// The key of this map is a pointer to the corresponding fCookie
2024-02-18 10:42:21 +00:00
cookies map[*fileObjCookie]struct{}
}
// PortEvent is an abstraction of the port_event C struct.
2024-02-18 10:42:21 +00:00
// Compare Source against PORT_SOURCE_FILE or PORT_SOURCE_FD
2024-02-18 10:42:21 +00:00
// to see if Path or Fd was the event source. The other will be
2024-02-18 10:42:21 +00:00
// uninitialized.
2024-02-18 10:42:21 +00:00
type PortEvent struct {
Cookie interface{}
2024-02-18 10:42:21 +00:00
Events int32
Fd uintptr
Path string
2024-02-18 10:42:21 +00:00
Source uint16
fobj *fileObj
2024-02-18 10:42:21 +00:00
}
// NewEventPort creates a new EventPort including the
2024-02-18 10:42:21 +00:00
// underlying call to port_create(3c).
2024-02-18 10:42:21 +00:00
func NewEventPort() (*EventPort, error) {
2024-02-18 10:42:21 +00:00
port, err := port_create()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e := &EventPort{
port: port,
fds: make(map[uintptr]*fileObjCookie),
paths: make(map[string]*fileObjCookie),
2024-02-18 10:42:21 +00:00
cookies: make(map[*fileObjCookie]struct{}),
}
2024-02-18 10:42:21 +00:00
return e, nil
2024-02-18 10:42:21 +00:00
}
//sys port_create() (n int, err error)
2024-02-18 10:42:21 +00:00
//sys port_associate(port int, source int, object uintptr, events int, user *byte) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys port_dissociate(port int, source int, object uintptr) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys port_get(port int, pe *portEvent, timeout *Timespec) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys port_getn(port int, pe *portEvent, max uint32, nget *uint32, timeout *Timespec) (n int, err error)
// Close closes the event port.
2024-02-18 10:42:21 +00:00
func (e *EventPort) Close() error {
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
err := Close(e.port)
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
e.fds = nil
2024-02-18 10:42:21 +00:00
e.paths = nil
2024-02-18 10:42:21 +00:00
e.cookies = nil
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// PathIsWatched checks to see if path is associated with this EventPort.
2024-02-18 10:42:21 +00:00
func (e *EventPort) PathIsWatched(path string) bool {
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
_, found := e.paths[path]
2024-02-18 10:42:21 +00:00
return found
2024-02-18 10:42:21 +00:00
}
// FdIsWatched checks to see if fd is associated with this EventPort.
2024-02-18 10:42:21 +00:00
func (e *EventPort) FdIsWatched(fd uintptr) bool {
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
_, found := e.fds[fd]
2024-02-18 10:42:21 +00:00
return found
2024-02-18 10:42:21 +00:00
}
// AssociatePath wraps port_associate(3c) for a filesystem path including
2024-02-18 10:42:21 +00:00
// creating the necessary file_obj from the provided stat information.
2024-02-18 10:42:21 +00:00
func (e *EventPort) AssociatePath(path string, stat os.FileInfo, events int, cookie interface{}) error {
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
if _, found := e.paths[path]; found {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("%v is already associated with this Event Port", path)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fCookie, err := createFileObjCookie(path, stat, cookie)
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
_, err = port_associate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(fCookie.fobj)), events, (*byte)(unsafe.Pointer(fCookie)))
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
e.paths[path] = fCookie
2024-02-18 10:42:21 +00:00
e.cookies[fCookie] = struct{}{}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// DissociatePath wraps port_dissociate(3c) for a filesystem path.
2024-02-18 10:42:21 +00:00
func (e *EventPort) DissociatePath(path string) error {
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
f, ok := e.paths[path]
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("%v is not associated with this Event Port", path)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_, err := port_dissociate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(f.fobj)))
2024-02-18 10:42:21 +00:00
// If the path is no longer associated with this event port (ENOENT)
2024-02-18 10:42:21 +00:00
// we should delete it from our map. We can still return ENOENT to the caller.
2024-02-18 10:42:21 +00:00
// But we need to save the cookie
2024-02-18 10:42:21 +00:00
if err != nil && err != ENOENT {
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 err == nil {
2024-02-18 10:42:21 +00:00
// dissociate was successful, safe to delete the cookie
2024-02-18 10:42:21 +00:00
fCookie := e.paths[path]
2024-02-18 10:42:21 +00:00
delete(e.cookies, fCookie)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
delete(e.paths, path)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// AssociateFd wraps calls to port_associate(3c) on file descriptors.
2024-02-18 10:42:21 +00:00
func (e *EventPort) AssociateFd(fd uintptr, events int, cookie interface{}) error {
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
if _, found := e.fds[fd]; found {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("%v is already associated with this Event Port", fd)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fCookie, err := createFileObjCookie("", nil, cookie)
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
_, err = port_associate(e.port, PORT_SOURCE_FD, fd, events, (*byte)(unsafe.Pointer(fCookie)))
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
e.fds[fd] = fCookie
2024-02-18 10:42:21 +00:00
e.cookies[fCookie] = struct{}{}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// DissociateFd wraps calls to port_dissociate(3c) on file descriptors.
2024-02-18 10:42:21 +00:00
func (e *EventPort) DissociateFd(fd uintptr) error {
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
_, ok := e.fds[fd]
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("%v is not associated with this Event Port", fd)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_, err := port_dissociate(e.port, PORT_SOURCE_FD, fd)
2024-02-18 10:42:21 +00:00
if err != nil && err != ENOENT {
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 err == nil {
2024-02-18 10:42:21 +00:00
// dissociate was successful, safe to delete the cookie
2024-02-18 10:42:21 +00:00
fCookie := e.fds[fd]
2024-02-18 10:42:21 +00:00
delete(e.cookies, fCookie)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
delete(e.fds, fd)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func createFileObjCookie(name string, stat os.FileInfo, cookie interface{}) (*fileObjCookie, error) {
2024-02-18 10:42:21 +00:00
fCookie := new(fileObjCookie)
2024-02-18 10:42:21 +00:00
fCookie.cookie = cookie
2024-02-18 10:42:21 +00:00
if name != "" && stat != nil {
2024-02-18 10:42:21 +00:00
fCookie.fobj = new(fileObj)
2024-02-18 10:42:21 +00:00
bs, err := ByteSliceFromString(name)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
fCookie.fobj.Name = (*int8)(unsafe.Pointer(&bs[0]))
2024-02-18 10:42:21 +00:00
s := stat.Sys().(*syscall.Stat_t)
2024-02-18 10:42:21 +00:00
fCookie.fobj.Atim.Sec = s.Atim.Sec
2024-02-18 10:42:21 +00:00
fCookie.fobj.Atim.Nsec = s.Atim.Nsec
2024-02-18 10:42:21 +00:00
fCookie.fobj.Mtim.Sec = s.Mtim.Sec
2024-02-18 10:42:21 +00:00
fCookie.fobj.Mtim.Nsec = s.Mtim.Nsec
2024-02-18 10:42:21 +00:00
fCookie.fobj.Ctim.Sec = s.Ctim.Sec
2024-02-18 10:42:21 +00:00
fCookie.fobj.Ctim.Nsec = s.Ctim.Nsec
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fCookie, nil
2024-02-18 10:42:21 +00:00
}
// GetOne wraps port_get(3c) and returns a single PortEvent.
2024-02-18 10:42:21 +00:00
func (e *EventPort) GetOne(t *Timespec) (*PortEvent, error) {
2024-02-18 10:42:21 +00:00
pe := new(portEvent)
2024-02-18 10:42:21 +00:00
_, err := port_get(e.port, pe, t)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := new(PortEvent)
2024-02-18 10:42:21 +00:00
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
err = e.peIntToExt(pe, p)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p, nil
2024-02-18 10:42:21 +00:00
}
// peIntToExt converts a cgo portEvent struct into the friendlier PortEvent
2024-02-18 10:42:21 +00:00
// NOTE: Always call this function while holding the e.mu mutex
2024-02-18 10:42:21 +00:00
func (e *EventPort) peIntToExt(peInt *portEvent, peExt *PortEvent) error {
2024-02-18 10:42:21 +00:00
if e.cookies == nil {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("this EventPort is already closed")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
peExt.Events = peInt.Events
2024-02-18 10:42:21 +00:00
peExt.Source = peInt.Source
2024-02-18 10:42:21 +00:00
fCookie := (*fileObjCookie)(unsafe.Pointer(peInt.User))
2024-02-18 10:42:21 +00:00
_, found := e.cookies[fCookie]
if !found {
2024-02-18 10:42:21 +00:00
panic("unexpected event port address; may be due to kernel bug; see https://go.dev/issue/54254")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
peExt.Cookie = fCookie.cookie
2024-02-18 10:42:21 +00:00
delete(e.cookies, fCookie)
switch peInt.Source {
2024-02-18 10:42:21 +00:00
case PORT_SOURCE_FD:
2024-02-18 10:42:21 +00:00
peExt.Fd = uintptr(peInt.Object)
2024-02-18 10:42:21 +00:00
// Only remove the fds entry if it exists and this cookie matches
2024-02-18 10:42:21 +00:00
if fobj, ok := e.fds[peExt.Fd]; ok {
2024-02-18 10:42:21 +00:00
if fobj == fCookie {
2024-02-18 10:42:21 +00:00
delete(e.fds, peExt.Fd)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case PORT_SOURCE_FILE:
2024-02-18 10:42:21 +00:00
peExt.fobj = fCookie.fobj
2024-02-18 10:42:21 +00:00
peExt.Path = BytePtrToString((*byte)(unsafe.Pointer(peExt.fobj.Name)))
2024-02-18 10:42:21 +00:00
// Only remove the paths entry if it exists and this cookie matches
2024-02-18 10:42:21 +00:00
if fobj, ok := e.paths[peExt.Path]; ok {
2024-02-18 10:42:21 +00:00
if fobj == fCookie {
2024-02-18 10:42:21 +00:00
delete(e.paths, peExt.Path)
2024-02-18 10:42:21 +00:00
}
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 nil
2024-02-18 10:42:21 +00:00
}
// Pending wraps port_getn(3c) and returns how many events are pending.
2024-02-18 10:42:21 +00:00
func (e *EventPort) Pending() (int, error) {
2024-02-18 10:42:21 +00:00
var n uint32 = 0
2024-02-18 10:42:21 +00:00
_, err := port_getn(e.port, nil, 0, &n, nil)
2024-02-18 10:42:21 +00:00
return int(n), err
2024-02-18 10:42:21 +00:00
}
// Get wraps port_getn(3c) and fills a slice of PortEvent.
2024-02-18 10:42:21 +00:00
// It will block until either min events have been received
2024-02-18 10:42:21 +00:00
// or the timeout has been exceeded. It will return how many
2024-02-18 10:42:21 +00:00
// events were actually received along with any error information.
2024-02-18 10:42:21 +00:00
func (e *EventPort) Get(s []PortEvent, min int, timeout *Timespec) (int, error) {
2024-02-18 10:42:21 +00:00
if min == 0 {
2024-02-18 10:42:21 +00:00
return 0, fmt.Errorf("need to request at least one event or use Pending() instead")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(s) < min {
2024-02-18 10:42:21 +00:00
return 0, fmt.Errorf("len(s) (%d) is less than min events requested (%d)", len(s), min)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
got := uint32(min)
2024-02-18 10:42:21 +00:00
max := uint32(len(s))
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
ps := make([]portEvent, max)
2024-02-18 10:42:21 +00:00
_, err = port_getn(e.port, &ps[0], max, &got, timeout)
2024-02-18 10:42:21 +00:00
// got will be trustworthy with ETIME, but not any other error.
2024-02-18 10:42:21 +00:00
if err != nil && err != ETIME {
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
e.mu.Lock()
2024-02-18 10:42:21 +00:00
defer e.mu.Unlock()
2024-02-18 10:42:21 +00:00
valid := 0
2024-02-18 10:42:21 +00:00
for i := 0; i < int(got); i++ {
2024-02-18 10:42:21 +00:00
err2 := e.peIntToExt(&ps[i], &s[i])
2024-02-18 10:42:21 +00:00
if err2 != nil {
2024-02-18 10:42:21 +00:00
if valid == 0 && err == nil {
2024-02-18 10:42:21 +00:00
// If err2 is the only error and there are no valid events
2024-02-18 10:42:21 +00:00
// to return, return it to the caller.
2024-02-18 10:42:21 +00:00
err = err2
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
valid = i + 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return valid, err
2024-02-18 10:42:21 +00:00
}
//sys putmsg(fd int, clptr *strbuf, dataptr *strbuf, flags int) (err error)
func Putmsg(fd int, cl []byte, data []byte, flags int) (err error) {
2024-02-18 10:42:21 +00:00
var clp, datap *strbuf
2024-02-18 10:42:21 +00:00
if len(cl) > 0 {
2024-02-18 10:42:21 +00:00
clp = &strbuf{
2024-02-18 10:42:21 +00:00
Len: int32(len(cl)),
2024-02-18 10:42:21 +00:00
Buf: (*int8)(unsafe.Pointer(&cl[0])),
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(data) > 0 {
2024-02-18 10:42:21 +00:00
datap = &strbuf{
2024-02-18 10:42:21 +00:00
Len: int32(len(data)),
2024-02-18 10:42:21 +00:00
Buf: (*int8)(unsafe.Pointer(&data[0])),
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return putmsg(fd, clp, datap, flags)
2024-02-18 10:42:21 +00:00
}
//sys getmsg(fd int, clptr *strbuf, dataptr *strbuf, flags *int) (err error)
func Getmsg(fd int, cl []byte, data []byte) (retCl []byte, retData []byte, flags int, err error) {
2024-02-18 10:42:21 +00:00
var clp, datap *strbuf
2024-02-18 10:42:21 +00:00
if len(cl) > 0 {
2024-02-18 10:42:21 +00:00
clp = &strbuf{
2024-02-18 10:42:21 +00:00
Maxlen: int32(len(cl)),
Buf: (*int8)(unsafe.Pointer(&cl[0])),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(data) > 0 {
2024-02-18 10:42:21 +00:00
datap = &strbuf{
2024-02-18 10:42:21 +00:00
Maxlen: int32(len(data)),
Buf: (*int8)(unsafe.Pointer(&data[0])),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if err = getmsg(fd, clp, datap, &flags); err != nil {
2024-02-18 10:42:21 +00:00
return nil, nil, 0, err
2024-02-18 10:42:21 +00:00
}
if len(cl) > 0 {
2024-02-18 10:42:21 +00:00
retCl = cl[:clp.Len]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(data) > 0 {
2024-02-18 10:42:21 +00:00
retData = data[:datap.Len]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return retCl, retData, flags, nil
2024-02-18 10:42:21 +00:00
}
func IoctlSetIntRetInt(fd int, req int, arg int) (int, error) {
2024-02-18 10:42:21 +00:00
return ioctlRet(fd, req, uintptr(arg))
2024-02-18 10:42:21 +00:00
}
func IoctlSetString(fd int, req int, val string) error {
2024-02-18 10:42:21 +00:00
bs := make([]byte, len(val)+1)
2024-02-18 10:42:21 +00:00
copy(bs[:len(bs)-1], val)
2024-02-18 10:42:21 +00:00
err := ioctlPtr(fd, req, unsafe.Pointer(&bs[0]))
2024-02-18 10:42:21 +00:00
runtime.KeepAlive(&bs[0])
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Lifreq Helpers
func (l *Lifreq) SetName(name string) error {
2024-02-18 10:42:21 +00:00
if len(name) >= len(l.Name) {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("name cannot be more than %d characters", len(l.Name)-1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := range name {
2024-02-18 10:42:21 +00:00
l.Name[i] = int8(name[i])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func (l *Lifreq) SetLifruInt(d int) {
2024-02-18 10:42:21 +00:00
*(*int)(unsafe.Pointer(&l.Lifru[0])) = d
2024-02-18 10:42:21 +00:00
}
func (l *Lifreq) GetLifruInt() int {
2024-02-18 10:42:21 +00:00
return *(*int)(unsafe.Pointer(&l.Lifru[0]))
2024-02-18 10:42:21 +00:00
}
func (l *Lifreq) SetLifruUint(d uint) {
2024-02-18 10:42:21 +00:00
*(*uint)(unsafe.Pointer(&l.Lifru[0])) = d
2024-02-18 10:42:21 +00:00
}
func (l *Lifreq) GetLifruUint() uint {
2024-02-18 10:42:21 +00:00
return *(*uint)(unsafe.Pointer(&l.Lifru[0]))
2024-02-18 10:42:21 +00:00
}
func IoctlLifreq(fd int, req int, l *Lifreq) error {
2024-02-18 10:42:21 +00:00
return ioctlPtr(fd, req, unsafe.Pointer(l))
2024-02-18 10:42:21 +00:00
}
// Strioctl Helpers
func (s *Strioctl) SetInt(i int) {
2024-02-18 10:42:21 +00:00
s.Len = int32(unsafe.Sizeof(i))
2024-02-18 10:42:21 +00:00
s.Dp = (*int8)(unsafe.Pointer(&i))
2024-02-18 10:42:21 +00:00
}
func IoctlSetStrioctlRetInt(fd int, req int, s *Strioctl) (int, error) {
2024-02-18 10:42:21 +00:00
return ioctlPtrRet(fd, req, unsafe.Pointer(s))
2024-02-18 10:42:21 +00:00
}