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

1108 lines
21 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2009,2010 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.
// Darwin 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_bsd.go or syscall_unix.go.
package unix
import (
"fmt"
"syscall"
"unsafe"
)
//sys closedir(dir uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
func fdopendir(fd int) (dir uintptr, err error) {
2024-02-18 10:42:21 +00:00
r0, _, e1 := syscall_syscallPtr(libc_fdopendir_trampoline_addr, uintptr(fd), 0, 0)
2024-02-18 10:42:21 +00:00
dir = uintptr(r0)
2024-02-18 10:42:21 +00:00
if e1 != 0 {
2024-02-18 10:42:21 +00:00
err = errnoErr(e1)
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 libc_fdopendir_trampoline_addr uintptr
//go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"
func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
2024-02-18 10:42:21 +00:00
// Simulate Getdirentries using fdopendir/readdir_r/closedir.
2024-02-18 10:42:21 +00:00
// We store the number of entries to skip in the seek
2024-02-18 10:42:21 +00:00
// offset of fd. See issue #31368.
2024-02-18 10:42:21 +00:00
// It's not the full required semantics, but should handle the case
2024-02-18 10:42:21 +00:00
// of calling Getdirentries or ReadDirent repeatedly.
2024-02-18 10:42:21 +00:00
// It won't handle assigning the results of lseek to *basep, or handle
2024-02-18 10:42:21 +00:00
// the directory being edited underfoot.
2024-02-18 10:42:21 +00:00
skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
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
}
// We need to duplicate the incoming file descriptor
2024-02-18 10:42:21 +00:00
// because the caller expects to retain control of it, but
2024-02-18 10:42:21 +00:00
// fdopendir expects to take control of its argument.
2024-02-18 10:42:21 +00:00
// Just Dup'ing the file descriptor is not enough, as the
2024-02-18 10:42:21 +00:00
// result shares underlying state. Use Openat to make a really
2024-02-18 10:42:21 +00:00
// new file descriptor referring to the same directory.
2024-02-18 10:42:21 +00:00
fd2, err := Openat(fd, ".", O_RDONLY, 0)
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
d, err := fdopendir(fd2)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
Close(fd2)
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
defer closedir(d)
var cnt int64
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
var entry Dirent
2024-02-18 10:42:21 +00:00
var entryp *Dirent
2024-02-18 10:42:21 +00:00
e := readdir_r(d, &entry, &entryp)
2024-02-18 10:42:21 +00:00
if e != 0 {
2024-02-18 10:42:21 +00:00
return n, errnoErr(e)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if entryp == nil {
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
if skip > 0 {
2024-02-18 10:42:21 +00:00
skip--
2024-02-18 10:42:21 +00:00
cnt++
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
reclen := int(entry.Reclen)
2024-02-18 10:42:21 +00:00
if reclen > len(buf) {
2024-02-18 10:42:21 +00:00
// Not enough room. Return for now.
2024-02-18 10:42:21 +00:00
// The counter will let us know where we should start up again.
2024-02-18 10:42:21 +00:00
// Note: this strategy for suspending in the middle and
2024-02-18 10:42:21 +00:00
// restarting is O(n^2) in the length of the directory. Oh well.
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// Copy entry into return buffer.
2024-02-18 10:42:21 +00:00
s := unsafe.Slice((*byte)(unsafe.Pointer(&entry)), reclen)
2024-02-18 10:42:21 +00:00
copy(buf, s)
buf = buf[reclen:]
2024-02-18 10:42:21 +00:00
n += reclen
2024-02-18 10:42:21 +00:00
cnt++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Set the seek offset of the input fd to record
2024-02-18 10:42:21 +00:00
// how many files we've already returned.
2024-02-18 10:42:21 +00:00
_, err = Seek(fd, cnt, 0 /* SEEK_SET */)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
return n, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrDatalink struct {
Len uint8
2024-02-18 10:42:21 +00:00
Family uint8
Index uint16
Type uint8
Nlen uint8
Alen uint8
Slen uint8
Data [12]int8
raw RawSockaddrDatalink
2024-02-18 10:42:21 +00:00
}
// SockaddrCtl implements the Sockaddr interface for AF_SYSTEM type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrCtl struct {
ID uint32
2024-02-18 10:42:21 +00:00
Unit uint32
raw RawSockaddrCtl
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrCtl) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Sc_len = SizeofSockaddrCtl
2024-02-18 10:42:21 +00:00
sa.raw.Sc_family = AF_SYSTEM
2024-02-18 10:42:21 +00:00
sa.raw.Ss_sysaddr = AF_SYS_CONTROL
2024-02-18 10:42:21 +00:00
sa.raw.Sc_id = sa.ID
2024-02-18 10:42:21 +00:00
sa.raw.Sc_unit = sa.Unit
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrCtl, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrVM implements the Sockaddr interface for AF_VSOCK type sockets.
2024-02-18 10:42:21 +00:00
// SockaddrVM provides access to Darwin VM sockets: a mechanism that enables
2024-02-18 10:42:21 +00:00
// bidirectional communication between a hypervisor and its guest virtual
2024-02-18 10:42:21 +00:00
// machines.
2024-02-18 10:42:21 +00:00
type SockaddrVM struct {
2024-02-18 10:42:21 +00:00
// CID and Port specify a context ID and port address for a VM socket.
2024-02-18 10:42:21 +00:00
// Guests have a unique CID, and hosts may have a well-known CID of:
2024-02-18 10:42:21 +00:00
// - VMADDR_CID_HYPERVISOR: refers to the hypervisor process.
2024-02-18 10:42:21 +00:00
// - VMADDR_CID_LOCAL: refers to local communication (loopback).
2024-02-18 10:42:21 +00:00
// - VMADDR_CID_HOST: refers to other processes on the host.
CID uint32
2024-02-18 10:42:21 +00:00
Port uint32
raw RawSockaddrVM
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrVM) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Len = SizeofSockaddrVM
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_VSOCK
2024-02-18 10:42:21 +00:00
sa.raw.Port = sa.Port
2024-02-18 10:42:21 +00:00
sa.raw.Cid = sa.CID
return unsafe.Pointer(&sa.raw), SizeofSockaddrVM, nil
2024-02-18 10:42:21 +00:00
}
func anyToSockaddrGOOS(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_SYSTEM:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrCtl)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
if pp.Ss_sysaddr == AF_SYS_CONTROL {
2024-02-18 10:42:21 +00:00
sa := new(SockaddrCtl)
2024-02-18 10:42:21 +00:00
sa.ID = pp.Sc_id
2024-02-18 10:42:21 +00:00
sa.Unit = pp.Sc_unit
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
case AF_VSOCK:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrVM)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := &SockaddrVM{
CID: pp.Cid,
2024-02-18 10:42:21 +00:00
Port: pp.Port,
}
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
}
// Some external packages rely on SYS___SYSCTL being defined to implement their
2024-02-18 10:42:21 +00:00
// own sysctl wrappers. Provide it here, even though direct syscalls are no
2024-02-18 10:42:21 +00:00
// longer supported on darwin.
2024-02-18 10:42:21 +00:00
const SYS___SYSCTL = SYS_SYSCTL
// Translate "kern.hostname" to []_C_int{0,1,2,3}.
2024-02-18 10:42:21 +00:00
func nametomib(name string) (mib []_C_int, err error) {
2024-02-18 10:42:21 +00:00
const siz = unsafe.Sizeof(mib[0])
// NOTE(rsc): It seems strange to set the buffer to have
2024-02-18 10:42:21 +00:00
// size CTL_MAXNAME+2 but use only CTL_MAXNAME
2024-02-18 10:42:21 +00:00
// as the size. I don't know why the +2 is here, but the
2024-02-18 10:42:21 +00:00
// kernel uses +2 for its own implementation of this function.
2024-02-18 10:42:21 +00:00
// I am scared that if we don't include the +2 here, the kernel
2024-02-18 10:42:21 +00:00
// will silently write 2 words farther than we specify
2024-02-18 10:42:21 +00:00
// and we'll get memory corruption.
2024-02-18 10:42:21 +00:00
var buf [CTL_MAXNAME + 2]_C_int
2024-02-18 10:42:21 +00:00
n := uintptr(CTL_MAXNAME) * siz
p := (*byte)(unsafe.Pointer(&buf[0]))
2024-02-18 10:42:21 +00:00
bytes, 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
}
// Magic sysctl: "setting" 0.3 to a string name
2024-02-18 10:42:21 +00:00
// lets you read back the array of integers form.
2024-02-18 10:42:21 +00:00
if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); 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 buf[0 : n/siz], nil
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
return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
2024-02-18 10:42:21 +00:00
}
func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
2024-02-18 10:42:21 +00:00
func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
func PtraceDenyAttach() (err error) { return ptrace(PT_DENY_ATTACH, 0, 0, 0) }
2024-02-18 10:42:21 +00:00
//sysnb pipe(p *[2]int32) (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 x [2]int32
2024-02-18 10:42:21 +00:00
err = pipe(&x)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
p[0] = int(x[0])
2024-02-18 10:42:21 +00:00
p[1] = int(x[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
}
func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
2024-02-18 10:42:21 +00:00
var _p0 unsafe.Pointer
2024-02-18 10:42:21 +00:00
var bufsize uintptr
2024-02-18 10:42:21 +00:00
if len(buf) > 0 {
2024-02-18 10:42:21 +00:00
_p0 = unsafe.Pointer(&buf[0])
2024-02-18 10:42:21 +00:00
bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return getfsstat(_p0, bufsize, flags)
2024-02-18 10:42:21 +00:00
}
func xattrPointer(dest []byte) *byte {
2024-02-18 10:42:21 +00:00
// It's only when dest is set to NULL that the OS X implementations of
2024-02-18 10:42:21 +00:00
// getxattr() and listxattr() return the current sizes of the named attributes.
2024-02-18 10:42:21 +00:00
// An empty byte array is not sufficient. To maintain the same behaviour as the
2024-02-18 10:42:21 +00:00
// linux implementation, we wrap around the system calls and pass in NULL when
2024-02-18 10:42:21 +00:00
// dest is empty.
2024-02-18 10:42:21 +00:00
var destp *byte
2024-02-18 10:42:21 +00:00
if len(dest) > 0 {
2024-02-18 10:42:21 +00:00
destp = &dest[0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return destp
2024-02-18 10:42:21 +00:00
}
//sys getxattr(path string, attr string, dest *byte, size int, position uint32, options int) (sz int, err error)
func Getxattr(path string, attr string, dest []byte) (sz int, err error) {
2024-02-18 10:42:21 +00:00
return getxattr(path, attr, xattrPointer(dest), len(dest), 0, 0)
2024-02-18 10:42:21 +00:00
}
func Lgetxattr(link string, attr string, dest []byte) (sz int, err error) {
2024-02-18 10:42:21 +00:00
return getxattr(link, attr, xattrPointer(dest), len(dest), 0, XATTR_NOFOLLOW)
2024-02-18 10:42:21 +00:00
}
//sys fgetxattr(fd int, attr string, dest *byte, size int, position uint32, options int) (sz int, err error)
func Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) {
2024-02-18 10:42:21 +00:00
return fgetxattr(fd, attr, xattrPointer(dest), len(dest), 0, 0)
2024-02-18 10:42:21 +00:00
}
//sys setxattr(path string, attr string, data *byte, size int, position uint32, options int) (err error)
func Setxattr(path string, attr string, data []byte, flags int) (err error) {
2024-02-18 10:42:21 +00:00
// The parameters for the OS X implementation vary slightly compared to the
2024-02-18 10:42:21 +00:00
// linux system call, specifically the position parameter:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// linux:
2024-02-18 10:42:21 +00:00
// int setxattr(
2024-02-18 10:42:21 +00:00
// const char *path,
2024-02-18 10:42:21 +00:00
// const char *name,
2024-02-18 10:42:21 +00:00
// const void *value,
2024-02-18 10:42:21 +00:00
// size_t size,
2024-02-18 10:42:21 +00:00
// int flags
2024-02-18 10:42:21 +00:00
// );
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// darwin:
2024-02-18 10:42:21 +00:00
// int setxattr(
2024-02-18 10:42:21 +00:00
// const char *path,
2024-02-18 10:42:21 +00:00
// const char *name,
2024-02-18 10:42:21 +00:00
// void *value,
2024-02-18 10:42:21 +00:00
// size_t size,
2024-02-18 10:42:21 +00:00
// u_int32_t position,
2024-02-18 10:42:21 +00:00
// int options
2024-02-18 10:42:21 +00:00
// );
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// position specifies the offset within the extended attribute. In the
2024-02-18 10:42:21 +00:00
// current implementation, only the resource fork extended attribute makes
2024-02-18 10:42:21 +00:00
// use of this argument. For all others, position is reserved. We simply
2024-02-18 10:42:21 +00:00
// default to setting it to zero.
2024-02-18 10:42:21 +00:00
return setxattr(path, attr, xattrPointer(data), len(data), 0, flags)
2024-02-18 10:42:21 +00:00
}
func Lsetxattr(link string, attr string, data []byte, flags int) (err error) {
2024-02-18 10:42:21 +00:00
return setxattr(link, attr, xattrPointer(data), len(data), 0, flags|XATTR_NOFOLLOW)
2024-02-18 10:42:21 +00:00
}
//sys fsetxattr(fd int, attr string, data *byte, size int, position uint32, options int) (err error)
func Fsetxattr(fd int, attr string, data []byte, flags int) (err error) {
2024-02-18 10:42:21 +00:00
return fsetxattr(fd, attr, xattrPointer(data), len(data), 0, 0)
2024-02-18 10:42:21 +00:00
}
//sys removexattr(path string, attr string, options int) (err error)
func Removexattr(path string, attr string) (err error) {
2024-02-18 10:42:21 +00:00
// We wrap around and explicitly zero out the options provided to the OS X
2024-02-18 10:42:21 +00:00
// implementation of removexattr, we do so for interoperability with the
2024-02-18 10:42:21 +00:00
// linux variant.
2024-02-18 10:42:21 +00:00
return removexattr(path, attr, 0)
2024-02-18 10:42:21 +00:00
}
func Lremovexattr(link string, attr string) (err error) {
2024-02-18 10:42:21 +00:00
return removexattr(link, attr, XATTR_NOFOLLOW)
2024-02-18 10:42:21 +00:00
}
//sys fremovexattr(fd int, attr string, options int) (err error)
func Fremovexattr(fd int, attr string) (err error) {
2024-02-18 10:42:21 +00:00
return fremovexattr(fd, attr, 0)
2024-02-18 10:42:21 +00:00
}
//sys listxattr(path string, dest *byte, size int, options int) (sz int, err error)
func Listxattr(path string, dest []byte) (sz int, err error) {
2024-02-18 10:42:21 +00:00
return listxattr(path, xattrPointer(dest), len(dest), 0)
2024-02-18 10:42:21 +00:00
}
func Llistxattr(link string, dest []byte) (sz int, err error) {
2024-02-18 10:42:21 +00:00
return listxattr(link, xattrPointer(dest), len(dest), XATTR_NOFOLLOW)
2024-02-18 10:42:21 +00:00
}
//sys flistxattr(fd int, dest *byte, size int, options int) (sz int, err error)
func Flistxattr(fd int, dest []byte) (sz int, err error) {
2024-02-18 10:42:21 +00:00
return flistxattr(fd, xattrPointer(dest), len(dest), 0)
2024-02-18 10:42:21 +00:00
}
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
/*
2024-02-18 10:42:21 +00:00
* Wrapped
2024-02-18 10:42:21 +00:00
*/
//sys fcntl(fd int, cmd int, arg int) (val int, err error)
//sys kill(pid int, signum int, posix int) (err error)
func Kill(pid int, signum syscall.Signal) (err error) { return kill(pid, int(signum), 1) }
//sys ioctl(fd int, req uint, arg uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL
func IoctlCtlInfo(fd int, ctlInfo *CtlInfo) error {
2024-02-18 10:42:21 +00:00
return ioctlPtr(fd, CTLIOCGINFO, unsafe.Pointer(ctlInfo))
2024-02-18 10:42:21 +00:00
}
// IfreqMTU is struct ifreq used to get or set a network device's MTU.
2024-02-18 10:42:21 +00:00
type IfreqMTU struct {
Name [IFNAMSIZ]byte
MTU int32
2024-02-18 10:42:21 +00:00
}
// IoctlGetIfreqMTU performs the SIOCGIFMTU ioctl operation on fd to get the MTU
2024-02-18 10:42:21 +00:00
// of the network device specified by ifname.
2024-02-18 10:42:21 +00:00
func IoctlGetIfreqMTU(fd int, ifname string) (*IfreqMTU, error) {
2024-02-18 10:42:21 +00:00
var ifreq IfreqMTU
2024-02-18 10:42:21 +00:00
copy(ifreq.Name[:], ifname)
2024-02-18 10:42:21 +00:00
err := ioctlPtr(fd, SIOCGIFMTU, unsafe.Pointer(&ifreq))
2024-02-18 10:42:21 +00:00
return &ifreq, err
2024-02-18 10:42:21 +00:00
}
// IoctlSetIfreqMTU performs the SIOCSIFMTU ioctl operation on fd to set the MTU
2024-02-18 10:42:21 +00:00
// of the network device specified by ifreq.Name.
2024-02-18 10:42:21 +00:00
func IoctlSetIfreqMTU(fd int, ifreq *IfreqMTU) error {
2024-02-18 10:42:21 +00:00
return ioctlPtr(fd, SIOCSIFMTU, unsafe.Pointer(ifreq))
2024-02-18 10:42:21 +00:00
}
//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS_SYSCTL
func Uname(uname *Utsname) error {
2024-02-18 10:42:21 +00:00
mib := []_C_int{CTL_KERN, KERN_OSTYPE}
2024-02-18 10:42:21 +00:00
n := unsafe.Sizeof(uname.Sysname)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
2024-02-18 10:42:21 +00:00
n = unsafe.Sizeof(uname.Nodename)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
2024-02-18 10:42:21 +00:00
n = unsafe.Sizeof(uname.Release)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
mib = []_C_int{CTL_KERN, KERN_VERSION}
2024-02-18 10:42:21 +00:00
n = unsafe.Sizeof(uname.Version)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// The version might have newlines or tabs in it, convert them to
2024-02-18 10:42:21 +00:00
// spaces.
2024-02-18 10:42:21 +00:00
for i, b := range uname.Version {
2024-02-18 10:42:21 +00:00
if b == '\n' || b == '\t' {
2024-02-18 10:42:21 +00:00
if i == len(uname.Version)-1 {
2024-02-18 10:42:21 +00:00
uname.Version[i] = 0
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
uname.Version[i] = ' '
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
mib = []_C_int{CTL_HW, HW_MACHINE}
2024-02-18 10:42:21 +00:00
n = unsafe.Sizeof(uname.Machine)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
return nil
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
var length = int64(count)
2024-02-18 10:42:21 +00:00
err = sendfile(infd, outfd, *offset, &length, nil, 0)
2024-02-18 10:42:21 +00:00
written = int(length)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
2024-02-18 10:42:21 +00:00
var value IPMreqn
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofIPMreqn)
2024-02-18 10:42:21 +00:00
errno := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
2024-02-18 10:42:21 +00:00
return &value, errno
2024-02-18 10:42:21 +00:00
}
func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq))
2024-02-18 10:42:21 +00:00
}
// GetsockoptXucred is a getsockopt wrapper that returns an Xucred struct.
2024-02-18 10:42:21 +00:00
// The usual level and opt are SOL_LOCAL and LOCAL_PEERCRED, respectively.
2024-02-18 10:42:21 +00:00
func GetsockoptXucred(fd, level, opt int) (*Xucred, error) {
2024-02-18 10:42:21 +00:00
x := new(Xucred)
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofXucred)
2024-02-18 10:42:21 +00:00
err := getsockopt(fd, level, opt, unsafe.Pointer(x), &vallen)
2024-02-18 10:42:21 +00:00
return x, err
2024-02-18 10:42:21 +00:00
}
func GetsockoptTCPConnectionInfo(fd, level, opt int) (*TCPConnectionInfo, error) {
2024-02-18 10:42:21 +00:00
var value TCPConnectionInfo
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofTCPConnectionInfo)
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 SysctlKinfoProc(name string, args ...int) (*KinfoProc, error) {
2024-02-18 10:42:21 +00:00
mib, err := sysctlmib(name, args...)
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
}
var kinfo KinfoProc
2024-02-18 10:42:21 +00:00
n := uintptr(SizeofKinfoProc)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, (*byte)(unsafe.Pointer(&kinfo)), &n, nil, 0); 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
if n != SizeofKinfoProc {
2024-02-18 10:42:21 +00:00
return nil, EIO
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &kinfo, nil
2024-02-18 10:42:21 +00:00
}
func SysctlKinfoProcSlice(name string, args ...int) ([]KinfoProc, error) {
2024-02-18 10:42:21 +00:00
mib, err := sysctlmib(name, args...)
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
}
for {
2024-02-18 10:42:21 +00:00
// Find size.
2024-02-18 10:42:21 +00:00
n := uintptr(0)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, nil, &n, nil, 0); 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
if n == 0 {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n%SizeofKinfoProc != 0 {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("sysctl() returned a size of %d, which is not a multiple of %d", n, SizeofKinfoProc)
2024-02-18 10:42:21 +00:00
}
// Read into buffer of that size.
2024-02-18 10:42:21 +00:00
buf := make([]KinfoProc, n/SizeofKinfoProc)
2024-02-18 10:42:21 +00:00
if err := sysctl(mib, (*byte)(unsafe.Pointer(&buf[0])), &n, nil, 0); err != nil {
2024-02-18 10:42:21 +00:00
if err == ENOMEM {
2024-02-18 10:42:21 +00:00
// Process table grew. Try again.
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
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
if n%SizeofKinfoProc != 0 {
2024-02-18 10:42:21 +00:00
return nil, fmt.Errorf("sysctl() returned a size of %d, which is not a multiple of %d", n, SizeofKinfoProc)
2024-02-18 10:42:21 +00:00
}
// The actual call may return less than the original reported required
2024-02-18 10:42:21 +00:00
// size so ensure we deal with that.
2024-02-18 10:42:21 +00:00
return buf[:n/SizeofKinfoProc], nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
//sys sendfile(infd int, outfd int, offset int64, len *int64, hdtr unsafe.Pointer, flags int) (err error)
//sys shmat(id int, addr uintptr, flag int) (ret uintptr, err error)
2024-02-18 10:42:21 +00:00
//sys shmctl(id int, cmd int, buf *SysvShmDesc) (result int, err error)
2024-02-18 10:42:21 +00:00
//sys shmdt(addr uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys shmget(key int, size int, flag int) (id int, err error)
/*
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 Chflags(path string, flags int) (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 Clonefile(src string, dst string, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Clonefileat(srcDirfd int, src string, dstDirfd int, dst string, flags 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(from int, to int) (err error)
2024-02-18 10:42:21 +00:00
//sys Exchangedata(path1 string, path2 string, options 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 Fchflags(fd int, flags 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 Fclonefileat(srcDirfd int, dstDirfd int, dst string, flags 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 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 Getcwd(buf []byte) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys Getdtablesize() (size int)
2024-02-18 10:42:21 +00:00
//sysnb Getegid() (egid int)
2024-02-18 10:42:21 +00:00
//sysnb Geteuid() (uid int)
2024-02-18 10:42:21 +00:00
//sysnb Getgid() (gid 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() (pgrp int)
2024-02-18 10:42:21 +00:00
//sysnb Getpid() (pid int)
2024-02-18 10:42:21 +00:00
//sysnb Getppid() (ppid int)
2024-02-18 10:42:21 +00:00
//sys Getpriority(which int, who int) (prio 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(tp *Timeval) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Getuid() (uid int)
2024-02-18 10:42:21 +00:00
//sysnb Issetugid() (tainted bool)
2024-02-18 10:42:21 +00:00
//sys Kqueue() (fd int, 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 Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Listen(s int, backlog 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 Mknod(path string, mode uint32, dev int) (err error)
2024-02-18 10:42:21 +00:00
//sys Mount(fsType string, dir string, flags int, data unsafe.Pointer) (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, mode int, perm 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 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 Readlinkat(dirfd int, 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(fromfd int, from string, tofd int, to string) (err error)
2024-02-18 10:42:21 +00:00
//sys Revoke(path 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) = SYS_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
//sys Setattrlist(path string, attrlist *Attrlist, attrBuf []byte, options int) (err error)
2024-02-18 10:42:21 +00:00
//sys 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 Setlogin(name string) (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
//sys Setprivexec(flag 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 Settimeofday(tp *Timeval) (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 Symlink(path string, link string) (err error)
2024-02-18 10:42:21 +00:00
//sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
2024-02-18 10:42:21 +00:00
//sys Sync() (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 Umask(newmask int) (oldmask int)
2024-02-18 10:42:21 +00:00
//sys Undelete(path string) (err error)
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 Unmount(path string, flags int) (err error)
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 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)