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

4476 lines
79 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.
// Linux 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
2024-02-18 10:42:21 +00:00
// wrap it in our own nicer implementation.
package unix
import (
"encoding/binary"
"strconv"
"syscall"
"time"
"unsafe"
)
/*
2024-02-18 10:42:21 +00:00
* Wrapped
2024-02-18 10:42:21 +00:00
*/
func Access(path string, mode uint32) (err error) {
2024-02-18 10:42:21 +00:00
return Faccessat(AT_FDCWD, path, mode, 0)
2024-02-18 10:42:21 +00:00
}
func Chmod(path string, mode uint32) (err error) {
2024-02-18 10:42:21 +00:00
return Fchmodat(AT_FDCWD, path, mode, 0)
2024-02-18 10:42:21 +00:00
}
func Chown(path string, uid int, gid int) (err error) {
2024-02-18 10:42:21 +00:00
return Fchownat(AT_FDCWD, path, uid, gid, 0)
2024-02-18 10:42:21 +00:00
}
func Creat(path string, mode uint32) (fd int, err error) {
2024-02-18 10:42:21 +00:00
return Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode)
2024-02-18 10:42:21 +00:00
}
func EpollCreate(size int) (fd int, err error) {
2024-02-18 10:42:21 +00:00
if size <= 0 {
2024-02-18 10:42:21 +00:00
return -1, EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return EpollCreate1(0)
2024-02-18 10:42:21 +00:00
}
//sys FanotifyInit(flags uint, event_f_flags uint) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sys fanotifyMark(fd int, flags uint, mask uint64, dirFd int, pathname *byte) (err error)
func FanotifyMark(fd int, flags uint, mask uint64, dirFd int, pathname string) (err error) {
2024-02-18 10:42:21 +00:00
if pathname == "" {
2024-02-18 10:42:21 +00:00
return fanotifyMark(fd, flags, mask, dirFd, nil)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p, err := BytePtrFromString(pathname)
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 fanotifyMark(fd, flags, mask, dirFd, p)
2024-02-18 10:42:21 +00:00
}
//sys fchmodat(dirfd int, path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys fchmodat2(dirfd int, path string, mode uint32, flags int) (err error)
func Fchmodat(dirfd int, path string, mode uint32, flags int) error {
2024-02-18 10:42:21 +00:00
// Linux fchmodat doesn't support the flags parameter, but fchmodat2 does.
2024-02-18 10:42:21 +00:00
// Try fchmodat2 if flags are specified.
2024-02-18 10:42:21 +00:00
if flags != 0 {
2024-02-18 10:42:21 +00:00
err := fchmodat2(dirfd, path, mode, flags)
2024-02-18 10:42:21 +00:00
if err == ENOSYS {
2024-02-18 10:42:21 +00:00
// fchmodat2 isn't available. If the flags are known to be valid,
2024-02-18 10:42:21 +00:00
// return EOPNOTSUPP to indicate that fchmodat doesn't support them.
2024-02-18 10:42:21 +00:00
if flags&^(AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) != 0 {
2024-02-18 10:42:21 +00:00
return EINVAL
2024-02-18 10:42:21 +00:00
} else if flags&(AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) != 0 {
2024-02-18 10:42:21 +00:00
return EOPNOTSUPP
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 err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fchmodat(dirfd, path, mode)
2024-02-18 10:42:21 +00:00
}
func InotifyInit() (fd int, err error) {
2024-02-18 10:42:21 +00:00
return InotifyInit1(0)
2024-02-18 10:42:21 +00:00
}
//sys ioctl(fd int, req uint, arg uintptr) (err error) = SYS_IOCTL
2024-02-18 10:42:21 +00:00
//sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL
// ioctl itself should not be exposed directly, but additional get/set functions
2024-02-18 10:42:21 +00:00
// for specific types are permissible. These are defined in ioctl.go and
2024-02-18 10:42:21 +00:00
// ioctl_linux.go.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The third argument to ioctl is often a pointer but sometimes an integer.
2024-02-18 10:42:21 +00:00
// Callers should use ioctlPtr when the third argument is a pointer and ioctl
2024-02-18 10:42:21 +00:00
// when the third argument is an integer.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// TODO: some existing code incorrectly uses ioctl when it should use ioctlPtr.
//sys Linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error)
func Link(oldpath string, newpath string) (err error) {
2024-02-18 10:42:21 +00:00
return Linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0)
2024-02-18 10:42:21 +00:00
}
func Mkdir(path string, mode uint32) (err error) {
2024-02-18 10:42:21 +00:00
return Mkdirat(AT_FDCWD, path, mode)
2024-02-18 10:42:21 +00:00
}
func Mknod(path string, mode uint32, dev int) (err error) {
2024-02-18 10:42:21 +00:00
return Mknodat(AT_FDCWD, path, mode, dev)
2024-02-18 10:42:21 +00:00
}
func Open(path string, mode int, perm uint32) (fd int, err error) {
2024-02-18 10:42:21 +00:00
return openat(AT_FDCWD, path, mode|O_LARGEFILE, perm)
2024-02-18 10:42:21 +00:00
}
//sys openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
2024-02-18 10:42:21 +00:00
return openat(dirfd, path, flags|O_LARGEFILE, mode)
2024-02-18 10:42:21 +00:00
}
//sys openat2(dirfd int, path string, open_how *OpenHow, size int) (fd int, err error)
func Openat2(dirfd int, path string, how *OpenHow) (fd int, err error) {
2024-02-18 10:42:21 +00:00
return openat2(dirfd, path, how, SizeofOpenHow)
2024-02-18 10:42:21 +00:00
}
func Pipe(p []int) error {
2024-02-18 10:42:21 +00:00
return Pipe2(p, 0)
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
}
//sys ppoll(fds *PollFd, nfds int, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
func Ppoll(fds []PollFd, timeout *Timespec, sigmask *Sigset_t) (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 ppoll(nil, 0, timeout, sigmask)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ppoll(&fds[0], len(fds), timeout, sigmask)
2024-02-18 10:42:21 +00:00
}
func Poll(fds []PollFd, timeout int) (n int, err error) {
2024-02-18 10:42:21 +00:00
var ts *Timespec
2024-02-18 10:42:21 +00:00
if timeout >= 0 {
2024-02-18 10:42:21 +00:00
ts = new(Timespec)
2024-02-18 10:42:21 +00:00
*ts = NsecToTimespec(int64(timeout) * 1e6)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return Ppoll(fds, ts, nil)
2024-02-18 10:42:21 +00:00
}
//sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
func Readlink(path string, buf []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
return Readlinkat(AT_FDCWD, path, buf)
2024-02-18 10:42:21 +00:00
}
func Rename(oldpath string, newpath string) (err error) {
2024-02-18 10:42:21 +00:00
return Renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath)
2024-02-18 10:42:21 +00:00
}
func Rmdir(path string) error {
2024-02-18 10:42:21 +00:00
return Unlinkat(AT_FDCWD, path, AT_REMOVEDIR)
2024-02-18 10:42:21 +00:00
}
//sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
func Symlink(oldpath string, newpath string) (err error) {
2024-02-18 10:42:21 +00:00
return Symlinkat(oldpath, AT_FDCWD, newpath)
2024-02-18 10:42:21 +00:00
}
func Unlink(path string) error {
2024-02-18 10:42:21 +00:00
return Unlinkat(AT_FDCWD, path, 0)
2024-02-18 10:42:21 +00:00
}
//sys Unlinkat(dirfd int, path string, flags int) (err error)
func Utimes(path string, tv []Timeval) error {
2024-02-18 10:42:21 +00:00
if tv == nil {
2024-02-18 10:42:21 +00:00
err := utimensat(AT_FDCWD, path, nil, 0)
2024-02-18 10:42:21 +00:00
if err != ENOSYS {
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 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
var ts [2]Timespec
2024-02-18 10:42:21 +00:00
ts[0] = NsecToTimespec(TimevalToNsec(tv[0]))
2024-02-18 10:42:21 +00:00
ts[1] = NsecToTimespec(TimevalToNsec(tv[1]))
2024-02-18 10:42:21 +00:00
err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
2024-02-18 10:42:21 +00:00
if err != ENOSYS {
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 utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
2024-02-18 10:42:21 +00:00
}
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
func UtimesNano(path string, ts []Timespec) error {
2024-02-18 10:42:21 +00:00
return UtimesNanoAt(AT_FDCWD, path, ts, 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
}
func Futimesat(dirfd int, path string, tv []Timeval) error {
2024-02-18 10:42:21 +00:00
if tv == nil {
2024-02-18 10:42:21 +00:00
return futimesat(dirfd, 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 futimesat(dirfd, path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
2024-02-18 10:42:21 +00:00
}
func Futimes(fd int, tv []Timeval) (err error) {
2024-02-18 10:42:21 +00:00
// Believe it or not, this is the best we can do on Linux
2024-02-18 10:42:21 +00:00
// (and is what glibc does).
2024-02-18 10:42:21 +00:00
return Utimes("/proc/self/fd/"+strconv.Itoa(fd), tv)
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
n, 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
// Getcwd returns the number of bytes written to buf, including the NUL.
2024-02-18 10:42:21 +00:00
if n < 1 || n > len(buf) || buf[n-1] != 0 {
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
// In some cases, Linux can return a path that starts with the
2024-02-18 10:42:21 +00:00
// "(unreachable)" prefix, which can potentially be a valid relative
2024-02-18 10:42:21 +00:00
// path. To work around that, return ENOENT if path is not absolute.
2024-02-18 10:42:21 +00:00
if buf[0] != '/' {
2024-02-18 10:42:21 +00:00
return "", ENOENT
2024-02-18 10:42:21 +00:00
}
return string(buf[0 : n-1]), nil
2024-02-18 10:42:21 +00:00
}
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
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
if n == 0 {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
// Sanity check group count. Max is 1<<16 on Linux.
2024-02-18 10:42:21 +00:00
if n < 0 || n > 1<<20 {
2024-02-18 10:42:21 +00:00
return nil, EINVAL
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 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
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
}
type WaitStatus uint32
// 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. At least that's the idea.
2024-02-18 10:42:21 +00:00
// There are various irregularities. For example, the
2024-02-18 10:42:21 +00:00
// "continued" status is 0xFFFF, distinguishing itself
2024-02-18 10:42:21 +00:00
// from stopped via the core dump bit.
const (
mask = 0x7F
core = 0x80
exited = 0x00
2024-02-18 10:42:21 +00:00
stopped = 0x7F
shift = 8
2024-02-18 10:42:21 +00:00
)
func (w WaitStatus) Exited() bool { return w&mask == exited }
func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited }
func (w WaitStatus) Stopped() bool { return w&0xFF == stopped }
func (w WaitStatus) Continued() bool { return w == 0xFFFF }
func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
func (w WaitStatus) ExitStatus() int {
2024-02-18 10:42:21 +00:00
if !w.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) & 0xFF
2024-02-18 10:42:21 +00:00
}
func (w WaitStatus) Signal() syscall.Signal {
2024-02-18 10:42:21 +00:00
if !w.Signaled() {
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 & mask)
2024-02-18 10:42:21 +00:00
}
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 {
2024-02-18 10:42:21 +00:00
if w.StopSignal() != SIGTRAP {
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) >> 8
2024-02-18 10:42:21 +00:00
}
//sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
2024-02-18 10:42:21 +00:00
var status _C_int
2024-02-18 10:42:21 +00:00
wpid, err = wait4(pid, &status, options, rusage)
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
2024-02-18 10:42:21 +00:00
}
//sys Waitid(idType int, id int, info *Siginfo, options int, rusage *Rusage) (err error)
func Mkfifo(path string, mode uint32) error {
2024-02-18 10:42:21 +00:00
return Mknod(path, mode|S_IFIFO, 0)
2024-02-18 10:42:21 +00:00
}
func Mkfifoat(dirfd int, path string, mode uint32) error {
2024-02-18 10:42:21 +00:00
return Mknodat(dirfd, path, mode|S_IFIFO, 0)
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
}
// SockaddrLinklayer implements the Sockaddr interface for AF_PACKET type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrLinklayer struct {
Protocol uint16
Ifindex int
Hatype uint16
Pkttype uint8
Halen uint8
Addr [8]byte
raw RawSockaddrLinklayer
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrLinklayer) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
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_PACKET
2024-02-18 10:42:21 +00:00
sa.raw.Protocol = sa.Protocol
2024-02-18 10:42:21 +00:00
sa.raw.Ifindex = int32(sa.Ifindex)
2024-02-18 10:42:21 +00:00
sa.raw.Hatype = sa.Hatype
2024-02-18 10:42:21 +00:00
sa.raw.Pkttype = sa.Pkttype
2024-02-18 10:42:21 +00:00
sa.raw.Halen = sa.Halen
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), SizeofSockaddrLinklayer, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrNetlink implements the Sockaddr interface for AF_NETLINK type sockets.
2024-02-18 10:42:21 +00:00
type SockaddrNetlink struct {
Family uint16
Pad uint16
Pid uint32
2024-02-18 10:42:21 +00:00
Groups uint32
raw RawSockaddrNetlink
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrNetlink) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_NETLINK
2024-02-18 10:42:21 +00:00
sa.raw.Pad = sa.Pad
2024-02-18 10:42:21 +00:00
sa.raw.Pid = sa.Pid
2024-02-18 10:42:21 +00:00
sa.raw.Groups = sa.Groups
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrNetlink, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrHCI implements the Sockaddr interface for AF_BLUETOOTH type sockets
2024-02-18 10:42:21 +00:00
// using the HCI protocol.
2024-02-18 10:42:21 +00:00
type SockaddrHCI struct {
Dev uint16
2024-02-18 10:42:21 +00:00
Channel uint16
raw RawSockaddrHCI
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrHCI) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_BLUETOOTH
2024-02-18 10:42:21 +00:00
sa.raw.Dev = sa.Dev
2024-02-18 10:42:21 +00:00
sa.raw.Channel = sa.Channel
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrHCI, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrL2 implements the Sockaddr interface for AF_BLUETOOTH type sockets
2024-02-18 10:42:21 +00:00
// using the L2CAP protocol.
2024-02-18 10:42:21 +00:00
type SockaddrL2 struct {
PSM uint16
CID uint16
Addr [6]uint8
2024-02-18 10:42:21 +00:00
AddrType uint8
raw RawSockaddrL2
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrL2) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_BLUETOOTH
2024-02-18 10:42:21 +00:00
psm := (*[2]byte)(unsafe.Pointer(&sa.raw.Psm))
2024-02-18 10:42:21 +00:00
psm[0] = byte(sa.PSM)
2024-02-18 10:42:21 +00:00
psm[1] = byte(sa.PSM >> 8)
2024-02-18 10:42:21 +00:00
for i := 0; i < len(sa.Addr); i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Bdaddr[i] = sa.Addr[len(sa.Addr)-1-i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cid := (*[2]byte)(unsafe.Pointer(&sa.raw.Cid))
2024-02-18 10:42:21 +00:00
cid[0] = byte(sa.CID)
2024-02-18 10:42:21 +00:00
cid[1] = byte(sa.CID >> 8)
2024-02-18 10:42:21 +00:00
sa.raw.Bdaddr_type = sa.AddrType
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrL2, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrRFCOMM implements the Sockaddr interface for AF_BLUETOOTH type sockets
2024-02-18 10:42:21 +00:00
// using the RFCOMM protocol.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Server example:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// fd, _ := Socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)
2024-02-18 10:42:21 +00:00
// _ = unix.Bind(fd, &unix.SockaddrRFCOMM{
2024-02-18 10:42:21 +00:00
// Channel: 1,
2024-02-18 10:42:21 +00:00
// Addr: [6]uint8{0, 0, 0, 0, 0, 0}, // BDADDR_ANY or 00:00:00:00:00:00
2024-02-18 10:42:21 +00:00
// })
2024-02-18 10:42:21 +00:00
// _ = Listen(fd, 1)
2024-02-18 10:42:21 +00:00
// nfd, sa, _ := Accept(fd)
2024-02-18 10:42:21 +00:00
// fmt.Printf("conn addr=%v fd=%d", sa.(*unix.SockaddrRFCOMM).Addr, nfd)
2024-02-18 10:42:21 +00:00
// Read(nfd, buf)
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Client example:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// fd, _ := Socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)
2024-02-18 10:42:21 +00:00
// _ = Connect(fd, &SockaddrRFCOMM{
2024-02-18 10:42:21 +00:00
// Channel: 1,
2024-02-18 10:42:21 +00:00
// Addr: [6]byte{0x11, 0x22, 0x33, 0xaa, 0xbb, 0xcc}, // CC:BB:AA:33:22:11
2024-02-18 10:42:21 +00:00
// })
2024-02-18 10:42:21 +00:00
// Write(fd, []byte(`hello`))
2024-02-18 10:42:21 +00:00
type SockaddrRFCOMM struct {
2024-02-18 10:42:21 +00:00
// Addr represents a bluetooth address, byte ordering is little-endian.
2024-02-18 10:42:21 +00:00
Addr [6]uint8
// Channel is a designated bluetooth channel, only 1-30 are available for use.
2024-02-18 10:42:21 +00:00
// Since Linux 2.6.7 and further zero value is the first available channel.
2024-02-18 10:42:21 +00:00
Channel uint8
raw RawSockaddrRFCOMM
}
func (sa *SockaddrRFCOMM) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_BLUETOOTH
2024-02-18 10:42:21 +00:00
sa.raw.Channel = sa.Channel
2024-02-18 10:42:21 +00:00
sa.raw.Bdaddr = sa.Addr
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrRFCOMM, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrCAN implements the Sockaddr interface for AF_CAN type sockets.
2024-02-18 10:42:21 +00:00
// The RxID and TxID fields are used for transport protocol addressing in
2024-02-18 10:42:21 +00:00
// (CAN_TP16, CAN_TP20, CAN_MCNET, and CAN_ISOTP), they can be left with
2024-02-18 10:42:21 +00:00
// zero values for CAN_RAW and CAN_BCM sockets as they have no meaning.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The SockaddrCAN struct must be bound to the socket file descriptor
2024-02-18 10:42:21 +00:00
// using Bind before the CAN socket can be used.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// // Read one raw CAN frame
2024-02-18 10:42:21 +00:00
// fd, _ := Socket(AF_CAN, SOCK_RAW, CAN_RAW)
2024-02-18 10:42:21 +00:00
// addr := &SockaddrCAN{Ifindex: index}
2024-02-18 10:42:21 +00:00
// Bind(fd, addr)
2024-02-18 10:42:21 +00:00
// frame := make([]byte, 16)
2024-02-18 10:42:21 +00:00
// Read(fd, frame)
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The full SocketCAN documentation can be found in the linux kernel
2024-02-18 10:42:21 +00:00
// archives at: https://www.kernel.org/doc/Documentation/networking/can.txt
2024-02-18 10:42:21 +00:00
type SockaddrCAN struct {
Ifindex int
RxID uint32
TxID uint32
raw RawSockaddrCAN
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrCAN) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
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_CAN
2024-02-18 10:42:21 +00:00
sa.raw.Ifindex = int32(sa.Ifindex)
2024-02-18 10:42:21 +00:00
rx := (*[4]byte)(unsafe.Pointer(&sa.RxID))
2024-02-18 10:42:21 +00:00
for i := 0; i < 4; i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Addr[i] = rx[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
tx := (*[4]byte)(unsafe.Pointer(&sa.TxID))
2024-02-18 10:42:21 +00:00
for i := 0; i < 4; i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Addr[i+4] = tx[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrCANJ1939 implements the Sockaddr interface for AF_CAN using J1939
2024-02-18 10:42:21 +00:00
// protocol (https://en.wikipedia.org/wiki/SAE_J1939). For more information
2024-02-18 10:42:21 +00:00
// on the purposes of the fields, check the official linux kernel documentation
2024-02-18 10:42:21 +00:00
// available here: https://www.kernel.org/doc/Documentation/networking/j1939.rst
2024-02-18 10:42:21 +00:00
type SockaddrCANJ1939 struct {
Ifindex int
Name uint64
PGN uint32
Addr uint8
raw RawSockaddrCAN
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrCANJ1939) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
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_CAN
2024-02-18 10:42:21 +00:00
sa.raw.Ifindex = int32(sa.Ifindex)
2024-02-18 10:42:21 +00:00
n := (*[8]byte)(unsafe.Pointer(&sa.Name))
2024-02-18 10:42:21 +00:00
for i := 0; i < 8; i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Addr[i] = n[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := (*[4]byte)(unsafe.Pointer(&sa.PGN))
2024-02-18 10:42:21 +00:00
for i := 0; i < 4; i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Addr[i+8] = p[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.raw.Addr[12] = sa.Addr
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrALG implements the Sockaddr interface for AF_ALG type sockets.
2024-02-18 10:42:21 +00:00
// SockaddrALG enables userspace access to the Linux kernel's cryptography
2024-02-18 10:42:21 +00:00
// subsystem. The Type and Name fields specify which type of hash or cipher
2024-02-18 10:42:21 +00:00
// should be used with a given socket.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// To create a file descriptor that provides access to a hash or cipher, both
2024-02-18 10:42:21 +00:00
// Bind and Accept must be used. Once the setup process is complete, input
2024-02-18 10:42:21 +00:00
// data can be written to the socket, processed by the kernel, and then read
2024-02-18 10:42:21 +00:00
// back as hash output or ciphertext.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Here is an example of using an AF_ALG socket with SHA1 hashing.
2024-02-18 10:42:21 +00:00
// The initial socket setup process is as follows:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// // Open a socket to perform SHA1 hashing.
2024-02-18 10:42:21 +00:00
// fd, _ := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0)
2024-02-18 10:42:21 +00:00
// addr := &unix.SockaddrALG{Type: "hash", Name: "sha1"}
2024-02-18 10:42:21 +00:00
// unix.Bind(fd, addr)
2024-02-18 10:42:21 +00:00
// // Note: unix.Accept does not work at this time; must invoke accept()
2024-02-18 10:42:21 +00:00
// // manually using unix.Syscall.
2024-02-18 10:42:21 +00:00
// hashfd, _, _ := unix.Syscall(unix.SYS_ACCEPT, uintptr(fd), 0, 0)
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Once a file descriptor has been returned from Accept, it may be used to
2024-02-18 10:42:21 +00:00
// perform SHA1 hashing. The descriptor is not safe for concurrent use, but
2024-02-18 10:42:21 +00:00
// may be re-used repeatedly with subsequent Write and Read operations.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// When hashing a small byte slice or string, a single Write and Read may
2024-02-18 10:42:21 +00:00
// be used:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// // Assume hashfd is already configured using the setup process.
2024-02-18 10:42:21 +00:00
// hash := os.NewFile(hashfd, "sha1")
2024-02-18 10:42:21 +00:00
// // Hash an input string and read the results. Each Write discards
2024-02-18 10:42:21 +00:00
// // previous hash state. Read always reads the current state.
2024-02-18 10:42:21 +00:00
// b := make([]byte, 20)
2024-02-18 10:42:21 +00:00
// for i := 0; i < 2; i++ {
2024-02-18 10:42:21 +00:00
// io.WriteString(hash, "Hello, world.")
2024-02-18 10:42:21 +00:00
// hash.Read(b)
2024-02-18 10:42:21 +00:00
// fmt.Println(hex.EncodeToString(b))
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
// // Output:
2024-02-18 10:42:21 +00:00
// // 2ae01472317d1935a84797ec1983ae243fc6aa28
2024-02-18 10:42:21 +00:00
// // 2ae01472317d1935a84797ec1983ae243fc6aa28
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// For hashing larger byte slices, or byte streams such as those read from
2024-02-18 10:42:21 +00:00
// a file or socket, use Sendto with MSG_MORE to instruct the kernel to update
2024-02-18 10:42:21 +00:00
// the hash digest instead of creating a new one for a given chunk and finalizing it.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// // Assume hashfd and addr are already configured using the setup process.
2024-02-18 10:42:21 +00:00
// hash := os.NewFile(hashfd, "sha1")
2024-02-18 10:42:21 +00:00
// // Hash the contents of a file.
2024-02-18 10:42:21 +00:00
// f, _ := os.Open("/tmp/linux-4.10-rc7.tar.xz")
2024-02-18 10:42:21 +00:00
// b := make([]byte, 4096)
2024-02-18 10:42:21 +00:00
// for {
2024-02-18 10:42:21 +00:00
// n, err := f.Read(b)
2024-02-18 10:42:21 +00:00
// if err == io.EOF {
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
// unix.Sendto(hashfd, b[:n], unix.MSG_MORE, addr)
2024-02-18 10:42:21 +00:00
// }
2024-02-18 10:42:21 +00:00
// hash.Read(b)
2024-02-18 10:42:21 +00:00
// fmt.Println(hex.EncodeToString(b))
2024-02-18 10:42:21 +00:00
// // Output: 85cdcad0c06eef66f805ecce353bec9accbeecc5
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// For more information, see: http://www.chronox.de/crypto-API/crypto/userspace-if.html.
2024-02-18 10:42:21 +00:00
type SockaddrALG struct {
Type string
Name string
2024-02-18 10:42:21 +00:00
Feature uint32
Mask uint32
raw RawSockaddrALG
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrALG) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
// Leave room for NUL byte terminator.
2024-02-18 10:42:21 +00:00
if len(sa.Type) > len(sa.raw.Type)-1 {
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
if len(sa.Name) > len(sa.raw.Name)-1 {
2024-02-18 10:42:21 +00:00
return nil, 0, EINVAL
2024-02-18 10:42:21 +00:00
}
sa.raw.Family = AF_ALG
2024-02-18 10:42:21 +00:00
sa.raw.Feat = sa.Feature
2024-02-18 10:42:21 +00:00
sa.raw.Mask = sa.Mask
copy(sa.raw.Type[:], sa.Type)
2024-02-18 10:42:21 +00:00
copy(sa.raw.Name[:], sa.Name)
return unsafe.Pointer(&sa.raw), SizeofSockaddrALG, 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 Linux 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
Port uint32
2024-02-18 10:42:21 +00:00
Flags uint8
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.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
2024-02-18 10:42:21 +00:00
sa.raw.Flags = sa.Flags
return unsafe.Pointer(&sa.raw), SizeofSockaddrVM, nil
2024-02-18 10:42:21 +00:00
}
type SockaddrXDP struct {
Flags uint16
Ifindex uint32
QueueID uint32
2024-02-18 10:42:21 +00:00
SharedUmemFD uint32
raw RawSockaddrXDP
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrXDP) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_XDP
2024-02-18 10:42:21 +00:00
sa.raw.Flags = sa.Flags
2024-02-18 10:42:21 +00:00
sa.raw.Ifindex = sa.Ifindex
2024-02-18 10:42:21 +00:00
sa.raw.Queue_id = sa.QueueID
2024-02-18 10:42:21 +00:00
sa.raw.Shared_umem_fd = sa.SharedUmemFD
return unsafe.Pointer(&sa.raw), SizeofSockaddrXDP, nil
2024-02-18 10:42:21 +00:00
}
// This constant mirrors the #define of PX_PROTO_OE in
2024-02-18 10:42:21 +00:00
// linux/if_pppox.h. We're defining this by hand here instead of
2024-02-18 10:42:21 +00:00
// autogenerating through mkerrors.sh because including
2024-02-18 10:42:21 +00:00
// linux/if_pppox.h causes some declaration conflicts with other
2024-02-18 10:42:21 +00:00
// includes (linux/if_pppox.h includes linux/in.h, which conflicts
2024-02-18 10:42:21 +00:00
// with netinet/in.h). Given that we only need a single zero constant
2024-02-18 10:42:21 +00:00
// out of that file, it's cleaner to just define it by hand here.
2024-02-18 10:42:21 +00:00
const px_proto_oe = 0
type SockaddrPPPoE struct {
SID uint16
2024-02-18 10:42:21 +00:00
Remote []byte
Dev string
raw RawSockaddrPPPoX
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrPPPoE) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
if len(sa.Remote) != 6 {
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
if len(sa.Dev) > IFNAMSIZ-1 {
2024-02-18 10:42:21 +00:00
return nil, 0, EINVAL
2024-02-18 10:42:21 +00:00
}
*(*uint16)(unsafe.Pointer(&sa.raw[0])) = AF_PPPOX
2024-02-18 10:42:21 +00:00
// This next field is in host-endian byte order. We can't use the
2024-02-18 10:42:21 +00:00
// same unsafe pointer cast as above, because this value is not
2024-02-18 10:42:21 +00:00
// 32-bit aligned and some architectures don't allow unaligned
2024-02-18 10:42:21 +00:00
// access.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// However, the value of px_proto_oe is 0, so we can use
2024-02-18 10:42:21 +00:00
// encoding/binary helpers to write the bytes without worrying
2024-02-18 10:42:21 +00:00
// about the ordering.
2024-02-18 10:42:21 +00:00
binary.BigEndian.PutUint32(sa.raw[2:6], px_proto_oe)
2024-02-18 10:42:21 +00:00
// This field is deliberately big-endian, unlike the previous
2024-02-18 10:42:21 +00:00
// one. The kernel expects SID to be in network byte order.
2024-02-18 10:42:21 +00:00
binary.BigEndian.PutUint16(sa.raw[6:8], sa.SID)
2024-02-18 10:42:21 +00:00
copy(sa.raw[8:14], sa.Remote)
2024-02-18 10:42:21 +00:00
for i := 14; i < 14+IFNAMSIZ; i++ {
2024-02-18 10:42:21 +00:00
sa.raw[i] = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
copy(sa.raw[14:], sa.Dev)
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrPPPoX, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrTIPC implements the Sockaddr interface for AF_TIPC type sockets.
2024-02-18 10:42:21 +00:00
// For more information on TIPC, see: http://tipc.sourceforge.net/.
2024-02-18 10:42:21 +00:00
type SockaddrTIPC struct {
2024-02-18 10:42:21 +00:00
// Scope is the publication scopes when binding service/service range.
2024-02-18 10:42:21 +00:00
// Should be set to TIPC_CLUSTER_SCOPE or TIPC_NODE_SCOPE.
2024-02-18 10:42:21 +00:00
Scope int
// Addr is the type of address used to manipulate a socket. Addr must be
2024-02-18 10:42:21 +00:00
// one of:
2024-02-18 10:42:21 +00:00
// - *TIPCSocketAddr: "id" variant in the C addr union
2024-02-18 10:42:21 +00:00
// - *TIPCServiceRange: "nameseq" variant in the C addr union
2024-02-18 10:42:21 +00:00
// - *TIPCServiceName: "name" variant in the C addr union
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If nil, EINVAL will be returned when the structure is used.
2024-02-18 10:42:21 +00:00
Addr TIPCAddr
raw RawSockaddrTIPC
}
// TIPCAddr is implemented by types that can be used as an address for
2024-02-18 10:42:21 +00:00
// SockaddrTIPC. It is only implemented by *TIPCSocketAddr, *TIPCServiceRange,
2024-02-18 10:42:21 +00:00
// and *TIPCServiceName.
2024-02-18 10:42:21 +00:00
type TIPCAddr interface {
tipcAddrtype() uint8
2024-02-18 10:42:21 +00:00
tipcAddr() [12]byte
}
func (sa *TIPCSocketAddr) tipcAddr() [12]byte {
2024-02-18 10:42:21 +00:00
var out [12]byte
2024-02-18 10:42:21 +00:00
copy(out[:], (*(*[unsafe.Sizeof(TIPCSocketAddr{})]byte)(unsafe.Pointer(sa)))[:])
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
func (sa *TIPCSocketAddr) tipcAddrtype() uint8 { return TIPC_SOCKET_ADDR }
func (sa *TIPCServiceRange) tipcAddr() [12]byte {
2024-02-18 10:42:21 +00:00
var out [12]byte
2024-02-18 10:42:21 +00:00
copy(out[:], (*(*[unsafe.Sizeof(TIPCServiceRange{})]byte)(unsafe.Pointer(sa)))[:])
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
func (sa *TIPCServiceRange) tipcAddrtype() uint8 { return TIPC_SERVICE_RANGE }
func (sa *TIPCServiceName) tipcAddr() [12]byte {
2024-02-18 10:42:21 +00:00
var out [12]byte
2024-02-18 10:42:21 +00:00
copy(out[:], (*(*[unsafe.Sizeof(TIPCServiceName{})]byte)(unsafe.Pointer(sa)))[:])
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
func (sa *TIPCServiceName) tipcAddrtype() uint8 { return TIPC_SERVICE_ADDR }
func (sa *SockaddrTIPC) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
if sa.Addr == nil {
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_TIPC
2024-02-18 10:42:21 +00:00
sa.raw.Scope = int8(sa.Scope)
2024-02-18 10:42:21 +00:00
sa.raw.Addrtype = sa.Addr.tipcAddrtype()
2024-02-18 10:42:21 +00:00
sa.raw.Addr = sa.Addr.tipcAddr()
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrTIPC, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrL2TPIP implements the Sockaddr interface for IPPROTO_L2TP/AF_INET sockets.
2024-02-18 10:42:21 +00:00
type SockaddrL2TPIP struct {
Addr [4]byte
2024-02-18 10:42:21 +00:00
ConnId uint32
raw RawSockaddrL2TPIP
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrL2TPIP) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_INET
2024-02-18 10:42:21 +00:00
sa.raw.Conn_id = sa.ConnId
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), SizeofSockaddrL2TPIP, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrL2TPIP6 implements the Sockaddr interface for IPPROTO_L2TP/AF_INET6 sockets.
2024-02-18 10:42:21 +00:00
type SockaddrL2TPIP6 struct {
Addr [16]byte
2024-02-18 10:42:21 +00:00
ZoneId uint32
2024-02-18 10:42:21 +00:00
ConnId uint32
raw RawSockaddrL2TPIP6
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrL2TPIP6) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_INET6
2024-02-18 10:42:21 +00:00
sa.raw.Conn_id = sa.ConnId
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), SizeofSockaddrL2TPIP6, nil
2024-02-18 10:42:21 +00:00
}
// SockaddrIUCV implements the Sockaddr interface for AF_IUCV sockets.
2024-02-18 10:42:21 +00:00
type SockaddrIUCV struct {
UserID string
Name string
raw RawSockaddrIUCV
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrIUCV) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_IUCV
2024-02-18 10:42:21 +00:00
// These are EBCDIC encoded by the kernel, but we still need to pad them
2024-02-18 10:42:21 +00:00
// with blanks. Initializing with blanks allows the caller to feed in either
2024-02-18 10:42:21 +00:00
// a padded or an unpadded string.
2024-02-18 10:42:21 +00:00
for i := 0; i < 8; i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Nodeid[i] = ' '
2024-02-18 10:42:21 +00:00
sa.raw.User_id[i] = ' '
2024-02-18 10:42:21 +00:00
sa.raw.Name[i] = ' '
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(sa.UserID) > 8 || len(sa.Name) > 8 {
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
for i, b := range []byte(sa.UserID[:]) {
2024-02-18 10:42:21 +00:00
sa.raw.User_id[i] = int8(b)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i, b := range []byte(sa.Name[:]) {
2024-02-18 10:42:21 +00:00
sa.raw.Name[i] = int8(b)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrIUCV, nil
2024-02-18 10:42:21 +00:00
}
type SockaddrNFC struct {
DeviceIdx uint32
TargetIdx uint32
2024-02-18 10:42:21 +00:00
NFCProtocol uint32
raw RawSockaddrNFC
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrNFC) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Sa_family = AF_NFC
2024-02-18 10:42:21 +00:00
sa.raw.Dev_idx = sa.DeviceIdx
2024-02-18 10:42:21 +00:00
sa.raw.Target_idx = sa.TargetIdx
2024-02-18 10:42:21 +00:00
sa.raw.Nfc_protocol = sa.NFCProtocol
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrNFC, nil
2024-02-18 10:42:21 +00:00
}
type SockaddrNFCLLCP struct {
DeviceIdx uint32
TargetIdx uint32
NFCProtocol uint32
2024-02-18 10:42:21 +00:00
DestinationSAP uint8
SourceSAP uint8
ServiceName string
raw RawSockaddrNFCLLCP
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrNFCLLCP) sockaddr() (unsafe.Pointer, _Socklen, error) {
2024-02-18 10:42:21 +00:00
sa.raw.Sa_family = AF_NFC
2024-02-18 10:42:21 +00:00
sa.raw.Dev_idx = sa.DeviceIdx
2024-02-18 10:42:21 +00:00
sa.raw.Target_idx = sa.TargetIdx
2024-02-18 10:42:21 +00:00
sa.raw.Nfc_protocol = sa.NFCProtocol
2024-02-18 10:42:21 +00:00
sa.raw.Dsap = sa.DestinationSAP
2024-02-18 10:42:21 +00:00
sa.raw.Ssap = sa.SourceSAP
2024-02-18 10:42:21 +00:00
if len(sa.ServiceName) > len(sa.raw.Service_name) {
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
copy(sa.raw.Service_name[:], sa.ServiceName)
2024-02-18 10:42:21 +00:00
sa.raw.SetServiceNameLen(len(sa.ServiceName))
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), SizeofSockaddrNFCLLCP, nil
2024-02-18 10:42:21 +00:00
}
var socketProtocol = func(fd int) (int, error) {
2024-02-18 10:42:21 +00:00
return GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL)
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_NETLINK:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrNetlink)
2024-02-18 10:42:21 +00:00
sa.Family = pp.Family
2024-02-18 10:42:21 +00:00
sa.Pad = pp.Pad
2024-02-18 10:42:21 +00:00
sa.Pid = pp.Pid
2024-02-18 10:42:21 +00:00
sa.Groups = pp.Groups
2024-02-18 10:42:21 +00:00
return sa, nil
case AF_PACKET:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrLinklayer)
2024-02-18 10:42:21 +00:00
sa.Protocol = pp.Protocol
2024-02-18 10:42:21 +00:00
sa.Ifindex = int(pp.Ifindex)
2024-02-18 10:42:21 +00:00
sa.Hatype = pp.Hatype
2024-02-18 10:42:21 +00:00
sa.Pkttype = pp.Pkttype
2024-02-18 10:42:21 +00:00
sa.Halen = pp.Halen
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_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
if pp.Path[0] == 0 {
2024-02-18 10:42:21 +00:00
// "Abstract" Unix domain socket.
2024-02-18 10:42:21 +00:00
// Rewrite leading NUL as @ for textual display.
2024-02-18 10:42:21 +00:00
// (This is the standard convention.)
2024-02-18 10:42:21 +00:00
// Not friendly to overwrite in place,
2024-02-18 10:42:21 +00:00
// but the callers below don't care.
2024-02-18 10:42:21 +00:00
pp.Path[0] = '@'
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 Linux 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
proto, err := socketProtocol(fd)
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
}
switch proto {
2024-02-18 10:42:21 +00:00
case IPPROTO_L2TP:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrL2TPIP)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrL2TPIP)
2024-02-18 10:42:21 +00:00
sa.ConnId = pp.Conn_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
default:
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
2024-02-18 10:42:21 +00:00
}
case AF_INET6:
2024-02-18 10:42:21 +00:00
proto, err := socketProtocol(fd)
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
}
switch proto {
2024-02-18 10:42:21 +00:00
case IPPROTO_L2TP:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrL2TPIP6)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrL2TPIP6)
2024-02-18 10:42:21 +00:00
sa.ConnId = pp.Conn_id
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
default:
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
}
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,
Port: pp.Port,
2024-02-18 10:42:21 +00:00
Flags: pp.Flags,
}
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
case AF_BLUETOOTH:
2024-02-18 10:42:21 +00:00
proto, err := socketProtocol(fd)
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
// only BTPROTO_L2CAP and BTPROTO_RFCOMM can accept connections
2024-02-18 10:42:21 +00:00
switch proto {
2024-02-18 10:42:21 +00:00
case BTPROTO_L2CAP:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrL2)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := &SockaddrL2{
PSM: pp.Psm,
CID: pp.Cid,
Addr: pp.Bdaddr,
2024-02-18 10:42:21 +00:00
AddrType: pp.Bdaddr_type,
}
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
case BTPROTO_RFCOMM:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrRFCOMM)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := &SockaddrRFCOMM{
2024-02-18 10:42:21 +00:00
Channel: pp.Channel,
Addr: pp.Bdaddr,
2024-02-18 10:42:21 +00:00
}
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_XDP:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrXDP)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := &SockaddrXDP{
Flags: pp.Flags,
Ifindex: pp.Ifindex,
QueueID: pp.Queue_id,
2024-02-18 10:42:21 +00:00
SharedUmemFD: pp.Shared_umem_fd,
}
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
case AF_PPPOX:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrPPPoX)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
if binary.BigEndian.Uint32(pp[2:6]) != px_proto_oe {
2024-02-18 10:42:21 +00:00
return nil, EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa := &SockaddrPPPoE{
SID: binary.BigEndian.Uint16(pp[6:8]),
2024-02-18 10:42:21 +00:00
Remote: pp[8:14],
}
2024-02-18 10:42:21 +00:00
for i := 14; i < 14+IFNAMSIZ; i++ {
2024-02-18 10:42:21 +00:00
if pp[i] == 0 {
2024-02-18 10:42:21 +00:00
sa.Dev = string(pp[14:i])
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
}
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
case AF_TIPC:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrTIPC)(unsafe.Pointer(rsa))
sa := &SockaddrTIPC{
2024-02-18 10:42:21 +00:00
Scope: int(pp.Scope),
}
// Determine which union variant is present in pp.Addr by checking
2024-02-18 10:42:21 +00:00
// pp.Addrtype.
2024-02-18 10:42:21 +00:00
switch pp.Addrtype {
2024-02-18 10:42:21 +00:00
case TIPC_SERVICE_RANGE:
2024-02-18 10:42:21 +00:00
sa.Addr = (*TIPCServiceRange)(unsafe.Pointer(&pp.Addr))
2024-02-18 10:42:21 +00:00
case TIPC_SERVICE_ADDR:
2024-02-18 10:42:21 +00:00
sa.Addr = (*TIPCServiceName)(unsafe.Pointer(&pp.Addr))
2024-02-18 10:42:21 +00:00
case TIPC_SOCKET_ADDR:
2024-02-18 10:42:21 +00:00
sa.Addr = (*TIPCSocketAddr)(unsafe.Pointer(&pp.Addr))
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, EINVAL
2024-02-18 10:42:21 +00:00
}
return sa, nil
2024-02-18 10:42:21 +00:00
case AF_IUCV:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrIUCV)(unsafe.Pointer(rsa))
var user [8]byte
2024-02-18 10:42:21 +00:00
var name [8]byte
for i := 0; i < 8; i++ {
2024-02-18 10:42:21 +00:00
user[i] = byte(pp.User_id[i])
2024-02-18 10:42:21 +00:00
name[i] = byte(pp.Name[i])
2024-02-18 10:42:21 +00:00
}
sa := &SockaddrIUCV{
2024-02-18 10:42:21 +00:00
UserID: string(user[:]),
Name: string(name[:]),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sa, nil
case AF_CAN:
2024-02-18 10:42:21 +00:00
proto, err := socketProtocol(fd)
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
}
pp := (*RawSockaddrCAN)(unsafe.Pointer(rsa))
switch proto {
2024-02-18 10:42:21 +00:00
case CAN_J1939:
2024-02-18 10:42:21 +00:00
sa := &SockaddrCANJ1939{
2024-02-18 10:42:21 +00:00
Ifindex: int(pp.Ifindex),
}
2024-02-18 10:42:21 +00:00
name := (*[8]byte)(unsafe.Pointer(&sa.Name))
2024-02-18 10:42:21 +00:00
for i := 0; i < 8; i++ {
2024-02-18 10:42:21 +00:00
name[i] = pp.Addr[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pgn := (*[4]byte)(unsafe.Pointer(&sa.PGN))
2024-02-18 10:42:21 +00:00
for i := 0; i < 4; i++ {
2024-02-18 10:42:21 +00:00
pgn[i] = pp.Addr[i+8]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
addr := (*[1]byte)(unsafe.Pointer(&sa.Addr))
2024-02-18 10:42:21 +00:00
addr[0] = pp.Addr[12]
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
sa := &SockaddrCAN{
2024-02-18 10:42:21 +00:00
Ifindex: int(pp.Ifindex),
}
2024-02-18 10:42:21 +00:00
rx := (*[4]byte)(unsafe.Pointer(&sa.RxID))
2024-02-18 10:42:21 +00:00
for i := 0; i < 4; i++ {
2024-02-18 10:42:21 +00:00
rx[i] = pp.Addr[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
tx := (*[4]byte)(unsafe.Pointer(&sa.TxID))
2024-02-18 10:42:21 +00:00
for i := 0; i < 4; i++ {
2024-02-18 10:42:21 +00:00
tx[i] = pp.Addr[i+4]
2024-02-18 10:42:21 +00:00
}
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_NFC:
2024-02-18 10:42:21 +00:00
proto, err := socketProtocol(fd)
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
switch proto {
2024-02-18 10:42:21 +00:00
case NFC_SOCKPROTO_RAW:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrNFC)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := &SockaddrNFC{
DeviceIdx: pp.Dev_idx,
TargetIdx: pp.Target_idx,
2024-02-18 10:42:21 +00:00
NFCProtocol: pp.Nfc_protocol,
}
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
case NFC_SOCKPROTO_LLCP:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrNFCLLCP)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
if uint64(pp.Service_name_len) > uint64(len(pp.Service_name)) {
2024-02-18 10:42:21 +00:00
return nil, EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa := &SockaddrNFCLLCP{
DeviceIdx: pp.Dev_idx,
TargetIdx: pp.Target_idx,
NFCProtocol: pp.Nfc_protocol,
2024-02-18 10:42:21 +00:00
DestinationSAP: pp.Dsap,
SourceSAP: pp.Ssap,
ServiceName: string(pp.Service_name[:pp.Service_name_len]),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, EINVAL
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, EAFNOSUPPORT
2024-02-18 10:42:21 +00:00
}
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 = accept4(fd, &rsa, &len, 0)
2024-02-18 10:42:21 +00:00
if 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
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
}
func Accept4(fd int, flags 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 = accept4(fd, &rsa, &len, flags)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len > SizeofSockaddrAny {
2024-02-18 10:42:21 +00:00
panic("RawSockaddrAny too small")
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
}
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
}
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
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 GetsockoptUcred(fd, level, opt int) (*Ucred, error) {
2024-02-18 10:42:21 +00:00
var value Ucred
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofUcred)
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 GetsockoptTCPInfo(fd, level, opt int) (*TCPInfo, error) {
2024-02-18 10:42:21 +00:00
var value TCPInfo
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofTCPInfo)
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
}
// 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
if err == ERANGE {
2024-02-18 10:42:21 +00:00
buf = make([]byte, vallen)
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
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ByteSliceToString(buf[:vallen]), nil
2024-02-18 10:42:21 +00:00
}
func GetsockoptTpacketStats(fd, level, opt int) (*TpacketStats, error) {
2024-02-18 10:42:21 +00:00
var value TpacketStats
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofTpacketStats)
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 GetsockoptTpacketStatsV3(fd, level, opt int) (*TpacketStatsV3, error) {
2024-02-18 10:42:21 +00:00
var value TpacketStatsV3
2024-02-18 10:42:21 +00:00
vallen := _Socklen(SizeofTpacketStatsV3)
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 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
}
func SetsockoptPacketMreq(fd, level, opt int, mreq *PacketMreq) 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
}
// SetsockoptSockFprog attaches a classic BPF or an extended BPF program to a
2024-02-18 10:42:21 +00:00
// socket to filter incoming packets. See 'man 7 socket' for usage information.
2024-02-18 10:42:21 +00:00
func SetsockoptSockFprog(fd, level, opt int, fprog *SockFprog) error {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(fprog), unsafe.Sizeof(*fprog))
2024-02-18 10:42:21 +00:00
}
func SetsockoptCanRawFilter(fd, level, opt int, filter []CanFilter) error {
2024-02-18 10:42:21 +00:00
var p unsafe.Pointer
2024-02-18 10:42:21 +00:00
if len(filter) > 0 {
2024-02-18 10:42:21 +00:00
p = unsafe.Pointer(&filter[0])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, p, uintptr(len(filter)*SizeofCanFilter))
2024-02-18 10:42:21 +00:00
}
func SetsockoptTpacketReq(fd, level, opt int, tp *TpacketReq) error {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(tp), unsafe.Sizeof(*tp))
2024-02-18 10:42:21 +00:00
}
func SetsockoptTpacketReq3(fd, level, opt int, tp *TpacketReq3) error {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(tp), unsafe.Sizeof(*tp))
2024-02-18 10:42:21 +00:00
}
func SetsockoptTCPRepairOpt(fd, level, opt int, o []TCPRepairOpt) (err error) {
2024-02-18 10:42:21 +00:00
if len(o) == 0 {
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 setsockopt(fd, level, opt, unsafe.Pointer(&o[0]), uintptr(SizeofTCPRepairOpt*len(o)))
2024-02-18 10:42:21 +00:00
}
func SetsockoptTCPMD5Sig(fd, level, opt int, s *TCPMD5Sig) error {
2024-02-18 10:42:21 +00:00
return setsockopt(fd, level, opt, unsafe.Pointer(s), unsafe.Sizeof(*s))
2024-02-18 10:42:21 +00:00
}
// Keyctl Commands (http://man7.org/linux/man-pages/man2/keyctl.2.html)
// KeyctlInt calls keyctl commands in which each argument is an int.
2024-02-18 10:42:21 +00:00
// These commands are KEYCTL_REVOKE, KEYCTL_CHOWN, KEYCTL_CLEAR, KEYCTL_LINK,
2024-02-18 10:42:21 +00:00
// KEYCTL_UNLINK, KEYCTL_NEGATE, KEYCTL_SET_REQKEY_KEYRING, KEYCTL_SET_TIMEOUT,
2024-02-18 10:42:21 +00:00
// KEYCTL_ASSUME_AUTHORITY, KEYCTL_SESSION_TO_PARENT, KEYCTL_REJECT,
2024-02-18 10:42:21 +00:00
// KEYCTL_INVALIDATE, and KEYCTL_GET_PERSISTENT.
2024-02-18 10:42:21 +00:00
//sys KeyctlInt(cmd int, arg2 int, arg3 int, arg4 int, arg5 int) (ret int, err error) = SYS_KEYCTL
// KeyctlBuffer calls keyctl commands in which the third and fourth
2024-02-18 10:42:21 +00:00
// arguments are a buffer and its length, respectively.
2024-02-18 10:42:21 +00:00
// These commands are KEYCTL_UPDATE, KEYCTL_READ, and KEYCTL_INSTANTIATE.
2024-02-18 10:42:21 +00:00
//sys KeyctlBuffer(cmd int, arg2 int, buf []byte, arg5 int) (ret int, err error) = SYS_KEYCTL
// KeyctlString calls keyctl commands which return a string.
2024-02-18 10:42:21 +00:00
// These commands are KEYCTL_DESCRIBE and KEYCTL_GET_SECURITY.
2024-02-18 10:42:21 +00:00
func KeyctlString(cmd int, id int) (string, error) {
2024-02-18 10:42:21 +00:00
// We must loop as the string data may change in between the syscalls.
2024-02-18 10:42:21 +00:00
// We could allocate a large buffer here to reduce the chance that the
2024-02-18 10:42:21 +00:00
// syscall needs to be called twice; however, this is unnecessary as
2024-02-18 10:42:21 +00:00
// the performance loss is negligible.
2024-02-18 10:42:21 +00:00
var buffer []byte
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
// Try to fill the buffer with data
2024-02-18 10:42:21 +00:00
length, err := KeyctlBuffer(cmd, id, buffer, 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
}
// Check if the data was written
2024-02-18 10:42:21 +00:00
if length <= len(buffer) {
2024-02-18 10:42:21 +00:00
// Exclude the null terminator
2024-02-18 10:42:21 +00:00
return string(buffer[:length-1]), nil
2024-02-18 10:42:21 +00:00
}
// Make a bigger buffer if needed
2024-02-18 10:42:21 +00:00
buffer = make([]byte, length)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Keyctl commands with special signatures.
// KeyctlGetKeyringID implements the KEYCTL_GET_KEYRING_ID command.
2024-02-18 10:42:21 +00:00
// See the full documentation at:
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man3/keyctl_get_keyring_ID.3.html
2024-02-18 10:42:21 +00:00
func KeyctlGetKeyringID(id int, create bool) (ringid int, err error) {
2024-02-18 10:42:21 +00:00
createInt := 0
2024-02-18 10:42:21 +00:00
if create {
2024-02-18 10:42:21 +00:00
createInt = 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return KeyctlInt(KEYCTL_GET_KEYRING_ID, id, createInt, 0, 0)
2024-02-18 10:42:21 +00:00
}
// KeyctlSetperm implements the KEYCTL_SETPERM command. The perm value is the
2024-02-18 10:42:21 +00:00
// key handle permission mask as described in the "keyctl setperm" section of
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man1/keyctl.1.html.
2024-02-18 10:42:21 +00:00
// See the full documentation at:
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man3/keyctl_setperm.3.html
2024-02-18 10:42:21 +00:00
func KeyctlSetperm(id int, perm uint32) error {
2024-02-18 10:42:21 +00:00
_, err := KeyctlInt(KEYCTL_SETPERM, id, int(perm), 0, 0)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
//sys keyctlJoin(cmd int, arg2 string) (ret int, err error) = SYS_KEYCTL
// KeyctlJoinSessionKeyring implements the KEYCTL_JOIN_SESSION_KEYRING command.
2024-02-18 10:42:21 +00:00
// See the full documentation at:
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man3/keyctl_join_session_keyring.3.html
2024-02-18 10:42:21 +00:00
func KeyctlJoinSessionKeyring(name string) (ringid int, err error) {
2024-02-18 10:42:21 +00:00
return keyctlJoin(KEYCTL_JOIN_SESSION_KEYRING, name)
2024-02-18 10:42:21 +00:00
}
//sys keyctlSearch(cmd int, arg2 int, arg3 string, arg4 string, arg5 int) (ret int, err error) = SYS_KEYCTL
// KeyctlSearch implements the KEYCTL_SEARCH command.
2024-02-18 10:42:21 +00:00
// See the full documentation at:
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man3/keyctl_search.3.html
2024-02-18 10:42:21 +00:00
func KeyctlSearch(ringid int, keyType, description string, destRingid int) (id int, err error) {
2024-02-18 10:42:21 +00:00
return keyctlSearch(KEYCTL_SEARCH, ringid, keyType, description, destRingid)
2024-02-18 10:42:21 +00:00
}
//sys keyctlIOV(cmd int, arg2 int, payload []Iovec, arg5 int) (err error) = SYS_KEYCTL
// KeyctlInstantiateIOV implements the KEYCTL_INSTANTIATE_IOV command. This
2024-02-18 10:42:21 +00:00
// command is similar to KEYCTL_INSTANTIATE, except that the payload is a slice
2024-02-18 10:42:21 +00:00
// of Iovec (each of which represents a buffer) instead of a single buffer.
2024-02-18 10:42:21 +00:00
// See the full documentation at:
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man3/keyctl_instantiate_iov.3.html
2024-02-18 10:42:21 +00:00
func KeyctlInstantiateIOV(id int, payload []Iovec, ringid int) error {
2024-02-18 10:42:21 +00:00
return keyctlIOV(KEYCTL_INSTANTIATE_IOV, id, payload, ringid)
2024-02-18 10:42:21 +00:00
}
//sys keyctlDH(cmd int, arg2 *KeyctlDHParams, buf []byte) (ret int, err error) = SYS_KEYCTL
// KeyctlDHCompute implements the KEYCTL_DH_COMPUTE command. This command
2024-02-18 10:42:21 +00:00
// computes a Diffie-Hellman shared secret based on the provide params. The
2024-02-18 10:42:21 +00:00
// secret is written to the provided buffer and the returned size is the number
2024-02-18 10:42:21 +00:00
// of bytes written (returning an error if there is insufficient space in the
2024-02-18 10:42:21 +00:00
// buffer). If a nil buffer is passed in, this function returns the minimum
2024-02-18 10:42:21 +00:00
// buffer length needed to store the appropriate data. Note that this differs
2024-02-18 10:42:21 +00:00
// from KEYCTL_READ's behavior which always returns the requested payload size.
2024-02-18 10:42:21 +00:00
// See the full documentation at:
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man3/keyctl_dh_compute.3.html
2024-02-18 10:42:21 +00:00
func KeyctlDHCompute(params *KeyctlDHParams, buffer []byte) (size int, err error) {
2024-02-18 10:42:21 +00:00
return keyctlDH(KEYCTL_DH_COMPUTE, params, buffer)
2024-02-18 10:42:21 +00:00
}
// KeyctlRestrictKeyring implements the KEYCTL_RESTRICT_KEYRING command. This
2024-02-18 10:42:21 +00:00
// command limits the set of keys that can be linked to the keyring, regardless
2024-02-18 10:42:21 +00:00
// of keyring permissions. The command requires the "setattr" permission.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// When called with an empty keyType the command locks the keyring, preventing
2024-02-18 10:42:21 +00:00
// any further keys from being linked to the keyring.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The "asymmetric" keyType defines restrictions requiring key payloads to be
2024-02-18 10:42:21 +00:00
// DER encoded X.509 certificates signed by keys in another keyring. Restrictions
2024-02-18 10:42:21 +00:00
// for "asymmetric" include "builtin_trusted", "builtin_and_secondary_trusted",
2024-02-18 10:42:21 +00:00
// "key_or_keyring:<key>", and "key_or_keyring:<key>:chain".
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// As of Linux 4.12, only the "asymmetric" keyType defines type-specific
2024-02-18 10:42:21 +00:00
// restrictions.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See the full documentation at:
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man3/keyctl_restrict_keyring.3.html
2024-02-18 10:42:21 +00:00
// http://man7.org/linux/man-pages/man2/keyctl.2.html
2024-02-18 10:42:21 +00:00
func KeyctlRestrictKeyring(ringid int, keyType string, restriction string) error {
2024-02-18 10:42:21 +00:00
if keyType == "" {
2024-02-18 10:42:21 +00:00
return keyctlRestrictKeyring(KEYCTL_RESTRICT_KEYRING, ringid)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return keyctlRestrictKeyringByType(KEYCTL_RESTRICT_KEYRING, ringid, keyType, restriction)
2024-02-18 10:42:21 +00:00
}
//sys keyctlRestrictKeyringByType(cmd int, arg2 int, keyType string, restriction string) (err error) = SYS_KEYCTL
2024-02-18 10:42:21 +00:00
//sys keyctlRestrictKeyring(cmd int, arg2 int) (err error) = SYS_KEYCTL
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
if emptyIovecs(iov) {
2024-02-18 10:42:21 +00:00
var sockType int
2024-02-18 10:42:21 +00:00
sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
2024-02-18 10:42:21 +00:00
if 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
// receive at least one normal byte
2024-02-18 10:42:21 +00:00
if sockType != SOCK_DGRAM {
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
}
2024-02-18 10:42:21 +00:00
msg.Control = &oob[0]
2024-02-18 10:42:21 +00:00
msg.SetControllen(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); 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
oobn = int(msg.Controllen)
2024-02-18 10:42:21 +00:00
recvflags = int(msg.Flags)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
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)(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
empty = emptyIovecs(iov)
2024-02-18 10:42:21 +00:00
if empty {
2024-02-18 10:42:21 +00:00
var sockType int
2024-02-18 10:42:21 +00:00
sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
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
// send at least one normal byte
2024-02-18 10:42:21 +00:00
if sockType != SOCK_DGRAM {
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
}
2024-02-18 10:42:21 +00:00
msg.Control = &oob[0]
2024-02-18 10:42:21 +00:00
msg.SetControllen(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
}
// BindToDevice binds the socket associated with fd to device.
2024-02-18 10:42:21 +00:00
func BindToDevice(fd int, device string) (err error) {
2024-02-18 10:42:21 +00:00
return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
2024-02-18 10:42:21 +00:00
}
//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys ptracePtr(request int, pid int, addr uintptr, data unsafe.Pointer) (err error) = SYS_PTRACE
func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
// The peek requests are machine-size oriented, so we wrap it
2024-02-18 10:42:21 +00:00
// to retrieve arbitrary-length data.
// The ptrace syscall differs from glibc's ptrace.
2024-02-18 10:42:21 +00:00
// Peeks returns the word in *data, not as the return value.
var buf [SizeofPtr]byte
// Leading edge. PEEKTEXT/PEEKDATA don't require aligned
2024-02-18 10:42:21 +00:00
// access (PEEKUSER warns that it might), but if we don't
2024-02-18 10:42:21 +00:00
// align our reads, we might straddle an unmapped page
2024-02-18 10:42:21 +00:00
// boundary and not get the bytes leading up to the page
2024-02-18 10:42:21 +00:00
// boundary.
2024-02-18 10:42:21 +00:00
n := 0
2024-02-18 10:42:21 +00:00
if addr%SizeofPtr != 0 {
2024-02-18 10:42:21 +00:00
err = ptracePtr(req, pid, addr-addr%SizeofPtr, unsafe.Pointer(&buf[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
n += copy(out, buf[addr%SizeofPtr:])
2024-02-18 10:42:21 +00:00
out = out[n:]
2024-02-18 10:42:21 +00:00
}
// Remainder.
2024-02-18 10:42:21 +00:00
for len(out) > 0 {
2024-02-18 10:42:21 +00:00
// We use an internal buffer to guarantee alignment.
2024-02-18 10:42:21 +00:00
// It's not documented if this is necessary, but we're paranoid.
2024-02-18 10:42:21 +00:00
err = ptracePtr(req, pid, addr+uintptr(n), unsafe.Pointer(&buf[0]))
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
}
2024-02-18 10:42:21 +00:00
copied := copy(out, buf[0:])
2024-02-18 10:42:21 +00:00
n += copied
2024-02-18 10:42:21 +00:00
out = out[copied:]
2024-02-18 10:42:21 +00:00
}
return n, nil
2024-02-18 10:42:21 +00:00
}
func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out)
2024-02-18 10:42:21 +00:00
}
func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
return ptracePeek(PTRACE_PEEKDATA, pid, addr, out)
2024-02-18 10:42:21 +00:00
}
func PtracePeekUser(pid int, addr uintptr, out []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
return ptracePeek(PTRACE_PEEKUSR, pid, addr, out)
2024-02-18 10:42:21 +00:00
}
func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
// As for ptracePeek, we need to align our accesses to deal
2024-02-18 10:42:21 +00:00
// with the possibility of straddling an invalid page.
// Leading edge.
2024-02-18 10:42:21 +00:00
n := 0
2024-02-18 10:42:21 +00:00
if addr%SizeofPtr != 0 {
2024-02-18 10:42:21 +00:00
var buf [SizeofPtr]byte
2024-02-18 10:42:21 +00:00
err = ptracePtr(peekReq, pid, addr-addr%SizeofPtr, unsafe.Pointer(&buf[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
n += copy(buf[addr%SizeofPtr:], data)
2024-02-18 10:42:21 +00:00
word := *((*uintptr)(unsafe.Pointer(&buf[0])))
2024-02-18 10:42:21 +00:00
err = ptrace(pokeReq, pid, addr-addr%SizeofPtr, word)
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
data = data[n:]
2024-02-18 10:42:21 +00:00
}
// Interior.
2024-02-18 10:42:21 +00:00
for len(data) > SizeofPtr {
2024-02-18 10:42:21 +00:00
word := *((*uintptr)(unsafe.Pointer(&data[0])))
2024-02-18 10:42:21 +00:00
err = ptrace(pokeReq, pid, addr+uintptr(n), word)
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
}
2024-02-18 10:42:21 +00:00
n += SizeofPtr
2024-02-18 10:42:21 +00:00
data = data[SizeofPtr:]
2024-02-18 10:42:21 +00:00
}
// Trailing edge.
2024-02-18 10:42:21 +00:00
if len(data) > 0 {
2024-02-18 10:42:21 +00:00
var buf [SizeofPtr]byte
2024-02-18 10:42:21 +00:00
err = ptracePtr(peekReq, pid, addr+uintptr(n), unsafe.Pointer(&buf[0]))
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
}
2024-02-18 10:42:21 +00:00
copy(buf[0:], data)
2024-02-18 10:42:21 +00:00
word := *((*uintptr)(unsafe.Pointer(&buf[0])))
2024-02-18 10:42:21 +00:00
err = ptrace(pokeReq, pid, addr+uintptr(n), word)
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
}
2024-02-18 10:42:21 +00:00
n += len(data)
2024-02-18 10:42:21 +00:00
}
return n, nil
2024-02-18 10:42:21 +00:00
}
func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data)
2024-02-18 10:42:21 +00:00
}
func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data)
2024-02-18 10:42:21 +00:00
}
func PtracePokeUser(pid int, addr uintptr, data []byte) (count int, err error) {
2024-02-18 10:42:21 +00:00
return ptracePoke(PTRACE_POKEUSR, PTRACE_PEEKUSR, pid, addr, data)
2024-02-18 10:42:21 +00:00
}
// elfNT_PRSTATUS is a copy of the debug/elf.NT_PRSTATUS constant so
2024-02-18 10:42:21 +00:00
// x/sys/unix doesn't need to depend on debug/elf and thus
2024-02-18 10:42:21 +00:00
// compress/zlib, debug/dwarf, and other packages.
2024-02-18 10:42:21 +00:00
const elfNT_PRSTATUS = 1
func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) {
2024-02-18 10:42:21 +00:00
var iov Iovec
2024-02-18 10:42:21 +00:00
iov.Base = (*byte)(unsafe.Pointer(regsout))
2024-02-18 10:42:21 +00:00
iov.SetLen(int(unsafe.Sizeof(*regsout)))
2024-02-18 10:42:21 +00:00
return ptracePtr(PTRACE_GETREGSET, pid, uintptr(elfNT_PRSTATUS), unsafe.Pointer(&iov))
2024-02-18 10:42:21 +00:00
}
func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) {
2024-02-18 10:42:21 +00:00
var iov Iovec
2024-02-18 10:42:21 +00:00
iov.Base = (*byte)(unsafe.Pointer(regs))
2024-02-18 10:42:21 +00:00
iov.SetLen(int(unsafe.Sizeof(*regs)))
2024-02-18 10:42:21 +00:00
return ptracePtr(PTRACE_SETREGSET, pid, uintptr(elfNT_PRSTATUS), unsafe.Pointer(&iov))
2024-02-18 10:42:21 +00:00
}
func PtraceSetOptions(pid int, options int) (err error) {
2024-02-18 10:42:21 +00:00
return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options))
2024-02-18 10:42:21 +00:00
}
func PtraceGetEventMsg(pid int) (msg uint, err error) {
2024-02-18 10:42:21 +00:00
var data _C_long
2024-02-18 10:42:21 +00:00
err = ptracePtr(PTRACE_GETEVENTMSG, pid, 0, unsafe.Pointer(&data))
2024-02-18 10:42:21 +00:00
msg = uint(data)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func PtraceCont(pid int, signal int) (err error) {
2024-02-18 10:42:21 +00:00
return ptrace(PTRACE_CONT, pid, 0, uintptr(signal))
2024-02-18 10:42:21 +00:00
}
func PtraceSyscall(pid int, signal int) (err error) {
2024-02-18 10:42:21 +00:00
return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal))
2024-02-18 10:42:21 +00:00
}
func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) }
func PtraceInterrupt(pid int) (err error) { return ptrace(PTRACE_INTERRUPT, pid, 0, 0) }
func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) }
func PtraceSeize(pid int) (err error) { return ptrace(PTRACE_SEIZE, pid, 0, 0) }
func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) }
//sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error)
func Reboot(cmd int) (err error) {
2024-02-18 10:42:21 +00:00
return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "")
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
}
//sys mount(source string, target string, fstype string, flags uintptr, data *byte) (err error)
func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) {
2024-02-18 10:42:21 +00:00
// Certain file systems get rather angry and EINVAL if you give
2024-02-18 10:42:21 +00:00
// them an empty string of data, rather than NULL.
2024-02-18 10:42:21 +00:00
if data == "" {
2024-02-18 10:42:21 +00:00
return mount(source, target, fstype, flags, nil)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
datap, err := BytePtrFromString(data)
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 mount(source, target, fstype, flags, datap)
2024-02-18 10:42:21 +00:00
}
//sys mountSetattr(dirfd int, pathname string, flags uint, attr *MountAttr, size uintptr) (err error) = SYS_MOUNT_SETATTR
// MountSetattr is a wrapper for mount_setattr(2).
2024-02-18 10:42:21 +00:00
// https://man7.org/linux/man-pages/man2/mount_setattr.2.html
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Requires kernel >= 5.12.
2024-02-18 10:42:21 +00:00
func MountSetattr(dirfd int, pathname string, flags uint, attr *MountAttr) error {
2024-02-18 10:42:21 +00:00
return mountSetattr(dirfd, pathname, flags, attr, unsafe.Sizeof(*attr))
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
}
// Sendto
2024-02-18 10:42:21 +00:00
// Recvfrom
2024-02-18 10:42:21 +00:00
// Socketpair
/*
2024-02-18 10:42:21 +00:00
* Direct access
2024-02-18 10:42:21 +00:00
*/
2024-02-18 10:42:21 +00:00
//sys Acct(path string) (err error)
2024-02-18 10:42:21 +00:00
//sys AddKey(keyType string, description string, payload []byte, ringid int) (id int, err error)
2024-02-18 10:42:21 +00:00
//sys Adjtimex(buf *Timex) (state int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Capget(hdr *CapUserHeader, data *CapUserData) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Capset(hdr *CapUserHeader, data *CapUserData) (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 Chroot(path string) (err error)
2024-02-18 10:42:21 +00:00
//sys ClockAdjtime(clockid int32, buf *Timex) (state int, err error)
2024-02-18 10:42:21 +00:00
//sys ClockGetres(clockid int32, res *Timespec) (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 ClockNanosleep(clockid int32, flags int, request *Timespec, remain *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 CloseRange(first uint, last uint, flags uint) (err error)
2024-02-18 10:42:21 +00:00
//sys CopyFileRange(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
2024-02-18 10:42:21 +00:00
//sys DeleteModule(name string, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Dup(oldfd int) (fd int, err error)
func Dup2(oldfd, newfd int) error {
2024-02-18 10:42:21 +00:00
return Dup3(oldfd, newfd, 0)
2024-02-18 10:42:21 +00:00
}
//sys Dup3(oldfd int, newfd int, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sysnb EpollCreate1(flag int) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
2024-02-18 10:42:21 +00:00
//sys Eventfd(initval uint, flags int) (fd int, err error) = SYS_EVENTFD2
2024-02-18 10:42:21 +00:00
//sys Exit(code int) = SYS_EXIT_GROUP
2024-02-18 10:42:21 +00:00
//sys Fallocate(fd int, mode uint32, off int64, len int64) (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 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 Fgetxattr(fd int, attr string, dest []byte) (sz int, err error)
2024-02-18 10:42:21 +00:00
//sys FinitModule(fd int, params string, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys Flistxattr(fd int, dest []byte) (sz 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 Fremovexattr(fd int, attr string) (err error)
2024-02-18 10:42:21 +00:00
//sys Fsetxattr(fd int, attr string, dest []byte, flags 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 Fsmount(fd int, flags int, mountAttrs int) (fsfd int, err error)
2024-02-18 10:42:21 +00:00
//sys Fsopen(fsName string, flags int) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sys Fspick(dirfd int, pathName string, flags int) (fd int, err error)
2024-05-14 13:07:09 +00:00
//sys fsconfig(fd int, cmd uint, key *byte, value *byte, aux int) (err error)
func fsconfigCommon(fd int, cmd uint, key string, value *byte, aux int) (err error) {
2024-05-14 13:07:09 +00:00
var keyp *byte
2024-05-14 13:07:09 +00:00
if keyp, err = BytePtrFromString(key); err != nil {
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return fsconfig(fd, cmd, keyp, value, aux)
2024-05-14 13:07:09 +00:00
}
// FsconfigSetFlag is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_SET_FLAG.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// fd is the filesystem context to act upon.
2024-05-14 13:07:09 +00:00
// key the parameter key to set.
2024-05-14 13:07:09 +00:00
func FsconfigSetFlag(fd int, key string) (err error) {
2024-05-14 13:07:09 +00:00
return fsconfigCommon(fd, FSCONFIG_SET_FLAG, key, nil, 0)
2024-05-14 13:07:09 +00:00
}
// FsconfigSetString is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_SET_STRING.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// fd is the filesystem context to act upon.
2024-05-14 13:07:09 +00:00
// key the parameter key to set.
2024-05-14 13:07:09 +00:00
// value is the parameter value to set.
2024-05-14 13:07:09 +00:00
func FsconfigSetString(fd int, key string, value string) (err error) {
2024-05-14 13:07:09 +00:00
var valuep *byte
2024-05-14 13:07:09 +00:00
if valuep, err = BytePtrFromString(value); err != nil {
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return fsconfigCommon(fd, FSCONFIG_SET_STRING, key, valuep, 0)
2024-05-14 13:07:09 +00:00
}
// FsconfigSetBinary is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_SET_BINARY.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// fd is the filesystem context to act upon.
2024-05-14 13:07:09 +00:00
// key the parameter key to set.
2024-05-14 13:07:09 +00:00
// value is the parameter value to set.
2024-05-14 13:07:09 +00:00
func FsconfigSetBinary(fd int, key string, value []byte) (err error) {
2024-05-14 13:07:09 +00:00
if len(value) == 0 {
2024-05-14 13:07:09 +00:00
return EINVAL
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return fsconfigCommon(fd, FSCONFIG_SET_BINARY, key, &value[0], len(value))
2024-05-14 13:07:09 +00:00
}
// FsconfigSetPath is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_SET_PATH.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// fd is the filesystem context to act upon.
2024-05-14 13:07:09 +00:00
// key the parameter key to set.
2024-05-14 13:07:09 +00:00
// path is a non-empty path for specified key.
2024-05-14 13:07:09 +00:00
// atfd is a file descriptor at which to start lookup from or AT_FDCWD.
2024-05-14 13:07:09 +00:00
func FsconfigSetPath(fd int, key string, path string, atfd int) (err error) {
2024-05-14 13:07:09 +00:00
var valuep *byte
2024-05-14 13:07:09 +00:00
if valuep, err = BytePtrFromString(path); err != nil {
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return fsconfigCommon(fd, FSCONFIG_SET_PATH, key, valuep, atfd)
2024-05-14 13:07:09 +00:00
}
// FsconfigSetPathEmpty is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_SET_PATH_EMPTY. The same as
2024-05-14 13:07:09 +00:00
// FconfigSetPath but with AT_PATH_EMPTY implied.
2024-05-14 13:07:09 +00:00
func FsconfigSetPathEmpty(fd int, key string, path string, atfd int) (err error) {
2024-05-14 13:07:09 +00:00
var valuep *byte
2024-05-14 13:07:09 +00:00
if valuep, err = BytePtrFromString(path); err != nil {
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return fsconfigCommon(fd, FSCONFIG_SET_PATH_EMPTY, key, valuep, atfd)
2024-05-14 13:07:09 +00:00
}
// FsconfigSetFd is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_SET_FD.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// fd is the filesystem context to act upon.
2024-05-14 13:07:09 +00:00
// key the parameter key to set.
2024-05-14 13:07:09 +00:00
// value is a file descriptor to be assigned to specified key.
2024-05-14 13:07:09 +00:00
func FsconfigSetFd(fd int, key string, value int) (err error) {
2024-05-14 13:07:09 +00:00
return fsconfigCommon(fd, FSCONFIG_SET_FD, key, nil, value)
2024-05-14 13:07:09 +00:00
}
// FsconfigCreate is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_CMD_CREATE.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// fd is the filesystem context to act upon.
2024-05-14 13:07:09 +00:00
func FsconfigCreate(fd int) (err error) {
2024-05-14 13:07:09 +00:00
return fsconfig(fd, FSCONFIG_CMD_CREATE, nil, nil, 0)
2024-05-14 13:07:09 +00:00
}
// FsconfigReconfigure is equivalent to fsconfig(2) called
2024-05-14 13:07:09 +00:00
// with cmd == FSCONFIG_CMD_RECONFIGURE.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// fd is the filesystem context to act upon.
2024-05-14 13:07:09 +00:00
func FsconfigReconfigure(fd int) (err error) {
2024-05-14 13:07:09 +00:00
return fsconfig(fd, FSCONFIG_CMD_RECONFIGURE, nil, nil, 0)
2024-05-14 13:07:09 +00:00
}
2024-02-18 10:42:21 +00:00
//sys Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64
2024-02-18 10:42:21 +00:00
//sysnb Getpgid(pid int) (pgid int, err error)
func Getpgrp() (pid int) {
2024-02-18 10:42:21 +00:00
pid, _ = Getpgid(0)
2024-02-18 10:42:21 +00:00
return
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
//sys Getrandom(buf []byte, flags int) (n int, 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 Gettid() (tid int)
2024-02-18 10:42:21 +00:00
//sys Getxattr(path string, attr string, dest []byte) (sz int, err error)
2024-02-18 10:42:21 +00:00
//sys InitModule(moduleImage []byte, params string) (err error)
2024-02-18 10:42:21 +00:00
//sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error)
2024-02-18 10:42:21 +00:00
//sysnb InotifyInit1(flags int) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Kill(pid int, sig syscall.Signal) (err error)
2024-02-18 10:42:21 +00:00
//sys Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG
2024-02-18 10:42:21 +00:00
//sys Lgetxattr(path string, attr string, dest []byte) (sz int, err error)
2024-02-18 10:42:21 +00:00
//sys Listxattr(path string, dest []byte) (sz int, err error)
2024-02-18 10:42:21 +00:00
//sys Llistxattr(path string, dest []byte) (sz int, err error)
2024-02-18 10:42:21 +00:00
//sys Lremovexattr(path string, attr string) (err error)
2024-02-18 10:42:21 +00:00
//sys Lsetxattr(path string, attr string, data []byte, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys MemfdCreate(name string, flags int) (fd int, 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 Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
2024-02-18 10:42:21 +00:00
//sys MoveMount(fromDirfd int, fromPathName string, toDirfd int, toPathName string, flags int) (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 OpenTree(dfd int, fileName string, flags uint) (r int, err error)
2024-02-18 10:42:21 +00:00
//sys PerfEventOpen(attr *PerfEventAttr, pid int, cpu int, groupFd int, flags int) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sys PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT
2024-02-18 10:42:21 +00:00
//sys Prctl(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys pselect6(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *sigset_argpack) (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 Removexattr(path string, attr string) (err error)
2024-02-18 10:42:21 +00:00
//sys Renameat2(olddirfd int, oldpath string, newdirfd int, newpath string, flags uint) (err error)
2024-02-18 10:42:21 +00:00
//sys RequestKey(keyType string, description string, callback string, destRingid int) (id int, err error)
2024-02-18 10:42:21 +00:00
//sys Setdomainname(p []byte) (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
//sysnb Setsid() (pid int, err error)
2024-02-18 10:42:21 +00:00
//sysnb Settimeofday(tv *Timeval) (err error)
2024-02-18 10:42:21 +00:00
//sys Setns(fd int, nstype int) (err error)
//go:linkname syscall_prlimit syscall.prlimit
2024-02-18 10:42:21 +00:00
func syscall_prlimit(pid, resource int, newlimit, old *syscall.Rlimit) error
func Prlimit(pid, resource int, newlimit, old *Rlimit) error {
2024-02-18 10:42:21 +00:00
// Just call the syscall version, because as of Go 1.21
2024-02-18 10:42:21 +00:00
// it will affect starting a new process.
2024-02-18 10:42:21 +00:00
return syscall_prlimit(pid, resource, (*syscall.Rlimit)(newlimit), (*syscall.Rlimit)(old))
2024-02-18 10:42:21 +00:00
}
// PrctlRetInt performs a prctl operation specified by option and further
2024-02-18 10:42:21 +00:00
// optional arguments arg2 through arg5 depending on option. It returns a
2024-02-18 10:42:21 +00:00
// non-negative integer that is returned by the prctl syscall.
2024-02-18 10:42:21 +00:00
func PrctlRetInt(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (int, error) {
2024-02-18 10:42:21 +00:00
ret, _, err := Syscall6(SYS_PRCTL, uintptr(option), uintptr(arg2), uintptr(arg3), uintptr(arg4), uintptr(arg5), 0)
2024-02-18 10:42:21 +00:00
if err != 0 {
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
return int(ret), nil
2024-02-18 10:42:21 +00:00
}
func Setuid(uid int) (err error) {
2024-02-18 10:42:21 +00:00
return syscall.Setuid(uid)
2024-02-18 10:42:21 +00:00
}
func Setgid(gid int) (err error) {
2024-02-18 10:42:21 +00:00
return syscall.Setgid(gid)
2024-02-18 10:42:21 +00:00
}
func Setreuid(ruid, euid int) (err error) {
2024-02-18 10:42:21 +00:00
return syscall.Setreuid(ruid, euid)
2024-02-18 10:42:21 +00:00
}
func Setregid(rgid, egid int) (err error) {
2024-02-18 10:42:21 +00:00
return syscall.Setregid(rgid, egid)
2024-02-18 10:42:21 +00:00
}
func Setresuid(ruid, euid, suid int) (err error) {
2024-02-18 10:42:21 +00:00
return syscall.Setresuid(ruid, euid, suid)
2024-02-18 10:42:21 +00:00
}
func Setresgid(rgid, egid, sgid int) (err error) {
2024-02-18 10:42:21 +00:00
return syscall.Setresgid(rgid, egid, sgid)
2024-02-18 10:42:21 +00:00
}
// SetfsgidRetGid sets fsgid for current thread and returns previous fsgid set.
2024-02-18 10:42:21 +00:00
// setfsgid(2) will return a non-nil error only if its caller lacks CAP_SETUID capability.
2024-02-18 10:42:21 +00:00
// If the call fails due to other reasons, current fsgid will be returned.
2024-02-18 10:42:21 +00:00
func SetfsgidRetGid(gid int) (int, error) {
2024-02-18 10:42:21 +00:00
return setfsgid(gid)
2024-02-18 10:42:21 +00:00
}
// SetfsuidRetUid sets fsuid for current thread and returns previous fsuid set.
2024-02-18 10:42:21 +00:00
// setfsgid(2) will return a non-nil error only if its caller lacks CAP_SETUID capability
2024-02-18 10:42:21 +00:00
// If the call fails due to other reasons, current fsuid will be returned.
2024-02-18 10:42:21 +00:00
func SetfsuidRetUid(uid int) (int, error) {
2024-02-18 10:42:21 +00:00
return setfsuid(uid)
2024-02-18 10:42:21 +00:00
}
func Setfsgid(gid int) error {
2024-02-18 10:42:21 +00:00
_, err := setfsgid(gid)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func Setfsuid(uid int) error {
2024-02-18 10:42:21 +00:00
_, err := setfsuid(uid)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func Signalfd(fd int, sigmask *Sigset_t, flags int) (newfd int, err error) {
2024-02-18 10:42:21 +00:00
return signalfd(fd, sigmask, _C__NSIG/8, flags)
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 Setxattr(path string, attr string, data []byte, flags int) (err error)
2024-02-18 10:42:21 +00:00
//sys signalfd(fd int, sigmask *Sigset_t, maskSize uintptr, flags int) (newfd int, err error) = SYS_SIGNALFD4
2024-02-18 10:42:21 +00:00
//sys Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Sync()
2024-02-18 10:42:21 +00:00
//sys Syncfs(fd int) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Sysinfo(info *Sysinfo_t) (err error)
2024-02-18 10:42:21 +00:00
//sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error)
2024-02-18 10:42:21 +00:00
//sysnb TimerfdCreate(clockid int, flags int) (fd int, err error)
2024-02-18 10:42:21 +00:00
//sysnb TimerfdGettime(fd int, currValue *ItimerSpec) (err error)
2024-02-18 10:42:21 +00:00
//sysnb TimerfdSettime(fd int, flags int, newValue *ItimerSpec, oldValue *ItimerSpec) (err error)
2024-02-18 10:42:21 +00:00
//sysnb Tgkill(tgid int, tid int, sig syscall.Signal) (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
//sysnb 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) = SYS_UMOUNT2
2024-02-18 10:42:21 +00:00
//sys Unshare(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 exitThread(code int) (err error) = SYS_EXIT
2024-02-18 10:42:21 +00:00
//sys readv(fd int, iovs []Iovec) (n int, err error) = SYS_READV
2024-02-18 10:42:21 +00:00
//sys writev(fd int, iovs []Iovec) (n int, err error) = SYS_WRITEV
2024-02-18 10:42:21 +00:00
//sys preadv(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr) (n int, err error) = SYS_PREADV
2024-02-18 10:42:21 +00:00
//sys pwritev(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr) (n int, err error) = SYS_PWRITEV
2024-02-18 10:42:21 +00:00
//sys preadv2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PREADV2
2024-02-18 10:42:21 +00:00
//sys pwritev2(fd int, iovs []Iovec, offs_l uintptr, offs_h uintptr, flags int) (n int, err error) = SYS_PWRITEV2
// minIovec is the size of the small initial allocation used by
2024-02-18 10:42:21 +00:00
// Readv, Writev, etc.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This small allocation gets stack allocated, which lets the
2024-02-18 10:42:21 +00:00
// common use case of len(iovs) <= minIovs avoid more expensive
2024-02-18 10:42:21 +00:00
// heap allocations.
2024-02-18 10:42:21 +00:00
const minIovec = 8
// appendBytes converts bs to Iovecs and appends them to vecs.
2024-02-18 10:42:21 +00:00
func appendBytes(vecs []Iovec, bs [][]byte) []Iovec {
2024-02-18 10:42:21 +00:00
for _, b := range bs {
2024-02-18 10:42:21 +00:00
var v Iovec
2024-02-18 10:42:21 +00:00
v.SetLen(len(b))
2024-02-18 10:42:21 +00:00
if len(b) > 0 {
2024-02-18 10:42:21 +00:00
v.Base = &b[0]
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
v.Base = (*byte)(unsafe.Pointer(&_zero))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
vecs = append(vecs, v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return vecs
2024-02-18 10:42:21 +00:00
}
// offs2lohi splits offs into its low and high order bits.
2024-02-18 10:42:21 +00:00
func offs2lohi(offs int64) (lo, hi uintptr) {
2024-02-18 10:42:21 +00:00
const longBits = SizeofLong * 8
2024-02-18 10:42:21 +00:00
return uintptr(offs), uintptr(uint64(offs) >> (longBits - 1) >> 1) // two shifts to avoid false positive in vet
2024-02-18 10:42:21 +00:00
}
func Readv(fd int, iovs [][]byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
iovecs := make([]Iovec, 0, minIovec)
2024-02-18 10:42:21 +00:00
iovecs = appendBytes(iovecs, iovs)
2024-02-18 10:42:21 +00:00
n, err = readv(fd, iovecs)
2024-02-18 10:42:21 +00:00
readvRacedetect(iovecs, n, err)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func Preadv(fd int, iovs [][]byte, offset int64) (n int, err error) {
2024-02-18 10:42:21 +00:00
iovecs := make([]Iovec, 0, minIovec)
2024-02-18 10:42:21 +00:00
iovecs = appendBytes(iovecs, iovs)
2024-02-18 10:42:21 +00:00
lo, hi := offs2lohi(offset)
2024-02-18 10:42:21 +00:00
n, err = preadv(fd, iovecs, lo, hi)
2024-02-18 10:42:21 +00:00
readvRacedetect(iovecs, n, err)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func Preadv2(fd int, iovs [][]byte, offset int64, flags int) (n int, err error) {
2024-02-18 10:42:21 +00:00
iovecs := make([]Iovec, 0, minIovec)
2024-02-18 10:42:21 +00:00
iovecs = appendBytes(iovecs, iovs)
2024-02-18 10:42:21 +00:00
lo, hi := offs2lohi(offset)
2024-02-18 10:42:21 +00:00
n, err = preadv2(fd, iovecs, lo, hi, flags)
2024-02-18 10:42:21 +00:00
readvRacedetect(iovecs, n, err)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func readvRacedetect(iovecs []Iovec, n int, err error) {
2024-02-18 10:42:21 +00:00
if !raceenabled {
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
for i := 0; n > 0 && i < len(iovecs); i++ {
2024-02-18 10:42:21 +00:00
m := int(iovecs[i].Len)
2024-02-18 10:42:21 +00:00
if m > n {
2024-02-18 10:42:21 +00:00
m = n
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n -= m
2024-02-18 10:42:21 +00:00
if m > 0 {
2024-02-18 10:42:21 +00:00
raceWriteRange(unsafe.Pointer(iovecs[i].Base), m)
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 err == nil {
2024-02-18 10:42:21 +00:00
raceAcquire(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func Writev(fd int, iovs [][]byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
iovecs := make([]Iovec, 0, minIovec)
2024-02-18 10:42:21 +00:00
iovecs = appendBytes(iovecs, iovs)
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
raceReleaseMerge(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n, err = writev(fd, iovecs)
2024-02-18 10:42:21 +00:00
writevRacedetect(iovecs, n)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func Pwritev(fd int, iovs [][]byte, offset int64) (n int, err error) {
2024-02-18 10:42:21 +00:00
iovecs := make([]Iovec, 0, minIovec)
2024-02-18 10:42:21 +00:00
iovecs = appendBytes(iovecs, iovs)
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
lo, hi := offs2lohi(offset)
2024-02-18 10:42:21 +00:00
n, err = pwritev(fd, iovecs, lo, hi)
2024-02-18 10:42:21 +00:00
writevRacedetect(iovecs, n)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func Pwritev2(fd int, iovs [][]byte, offset int64, flags int) (n int, err error) {
2024-02-18 10:42:21 +00:00
iovecs := make([]Iovec, 0, minIovec)
2024-02-18 10:42:21 +00:00
iovecs = appendBytes(iovecs, iovs)
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
lo, hi := offs2lohi(offset)
2024-02-18 10:42:21 +00:00
n, err = pwritev2(fd, iovecs, lo, hi, flags)
2024-02-18 10:42:21 +00:00
writevRacedetect(iovecs, n)
2024-02-18 10:42:21 +00:00
return n, err
2024-02-18 10:42:21 +00:00
}
func writevRacedetect(iovecs []Iovec, n int) {
2024-02-18 10:42:21 +00:00
if !raceenabled {
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
for i := 0; n > 0 && i < len(iovecs); i++ {
2024-02-18 10:42:21 +00:00
m := int(iovecs[i].Len)
2024-02-18 10:42:21 +00:00
if m > n {
2024-02-18 10:42:21 +00:00
m = n
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n -= m
2024-02-18 10:42:21 +00:00
if m > 0 {
2024-02-18 10:42:21 +00:00
raceReadRange(unsafe.Pointer(iovecs[i].Base), m)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// mmap varies by architecture; see syscall_linux_*.go.
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 mremap(oldaddr uintptr, oldlength uintptr, newlength uintptr, flags int, newaddr uintptr) (xaddr uintptr, 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 Mprotect(b []byte, prot 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 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)
const (
mremapFixed = MREMAP_FIXED
2024-02-18 10:42:21 +00:00
mremapDontunmap = MREMAP_DONTUNMAP
mremapMaymove = MREMAP_MAYMOVE
2024-02-18 10:42:21 +00:00
)
// Vmsplice splices user pages from a slice of Iovecs into a pipe specified by fd,
2024-02-18 10:42:21 +00:00
// using the specified flags.
2024-02-18 10:42:21 +00:00
func Vmsplice(fd int, iovs []Iovec, flags int) (int, error) {
2024-02-18 10:42:21 +00:00
var p unsafe.Pointer
2024-02-18 10:42:21 +00:00
if len(iovs) > 0 {
2024-02-18 10:42:21 +00:00
p = unsafe.Pointer(&iovs[0])
2024-02-18 10:42:21 +00:00
}
n, _, errno := Syscall6(SYS_VMSPLICE, uintptr(fd), uintptr(p), uintptr(len(iovs)), uintptr(flags), 0, 0)
2024-02-18 10:42:21 +00:00
if errno != 0 {
2024-02-18 10:42:21 +00:00
return 0, syscall.Errno(errno)
2024-02-18 10:42:21 +00:00
}
return int(n), nil
2024-02-18 10:42:21 +00:00
}
func isGroupMember(gid int) bool {
2024-02-18 10:42:21 +00:00
groups, err := Getgroups()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
for _, g := range groups {
2024-02-18 10:42:21 +00:00
if g == gid {
2024-02-18 10:42:21 +00:00
return true
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 false
2024-02-18 10:42:21 +00:00
}
func isCapDacOverrideSet() bool {
2024-02-18 10:42:21 +00:00
hdr := CapUserHeader{Version: LINUX_CAPABILITY_VERSION_3}
2024-02-18 10:42:21 +00:00
data := [2]CapUserData{}
2024-02-18 10:42:21 +00:00
err := Capget(&hdr, &data[0])
return err == nil && data[0].Effective&(1<<CAP_DAC_OVERRIDE) != 0
2024-02-18 10:42:21 +00:00
}
//sys faccessat(dirfd int, path string, mode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys Faccessat2(dirfd int, path string, mode uint32, flags int) (err error)
func Faccessat(dirfd int, path string, mode uint32, flags int) (err error) {
2024-02-18 10:42:21 +00:00
if flags == 0 {
2024-02-18 10:42:21 +00:00
return faccessat(dirfd, path, mode)
2024-02-18 10:42:21 +00:00
}
if err := Faccessat2(dirfd, path, mode, flags); err != ENOSYS && err != EPERM {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// The Linux kernel faccessat system call does not take any flags.
2024-02-18 10:42:21 +00:00
// The glibc faccessat implements the flags itself; see
2024-02-18 10:42:21 +00:00
// https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/faccessat.c;hb=HEAD
2024-02-18 10:42:21 +00:00
// Because people naturally expect syscall.Faccessat to act
2024-02-18 10:42:21 +00:00
// like C faccessat, we do the same.
if flags & ^(AT_SYMLINK_NOFOLLOW|AT_EACCESS) != 0 {
2024-02-18 10:42:21 +00:00
return EINVAL
2024-02-18 10:42:21 +00:00
}
var st Stat_t
2024-02-18 10:42:21 +00:00
if err := Fstatat(dirfd, path, &st, flags&AT_SYMLINK_NOFOLLOW); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
mode &= 7
2024-02-18 10:42:21 +00:00
if mode == 0 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
var uid int
2024-02-18 10:42:21 +00:00
if flags&AT_EACCESS != 0 {
2024-02-18 10:42:21 +00:00
uid = Geteuid()
2024-02-18 10:42:21 +00:00
if uid != 0 && isCapDacOverrideSet() {
2024-02-18 10:42:21 +00:00
// If CAP_DAC_OVERRIDE is set, file access check is
2024-02-18 10:42:21 +00:00
// done by the kernel in the same way as for root
2024-02-18 10:42:21 +00:00
// (see generic_permission() in the Linux sources).
2024-02-18 10:42:21 +00:00
uid = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
uid = Getuid()
2024-02-18 10:42:21 +00:00
}
if uid == 0 {
2024-02-18 10:42:21 +00:00
if mode&1 == 0 {
2024-02-18 10:42:21 +00:00
// Root can read and write any file.
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if st.Mode&0111 != 0 {
2024-02-18 10:42:21 +00:00
// Root can execute any file that anybody can execute.
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return EACCES
2024-02-18 10:42:21 +00:00
}
var fmode uint32
2024-02-18 10:42:21 +00:00
if uint32(uid) == st.Uid {
2024-02-18 10:42:21 +00:00
fmode = (st.Mode >> 6) & 7
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
var gid int
2024-02-18 10:42:21 +00:00
if flags&AT_EACCESS != 0 {
2024-02-18 10:42:21 +00:00
gid = Getegid()
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
gid = Getgid()
2024-02-18 10:42:21 +00:00
}
if uint32(gid) == st.Gid || isGroupMember(int(st.Gid)) {
2024-02-18 10:42:21 +00:00
fmode = (st.Mode >> 3) & 7
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
fmode = st.Mode & 7
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if fmode&mode == mode {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
return EACCES
2024-02-18 10:42:21 +00:00
}
//sys nameToHandleAt(dirFD int, pathname string, fh *fileHandle, mountID *_C_int, flags int) (err error) = SYS_NAME_TO_HANDLE_AT
2024-02-18 10:42:21 +00:00
//sys openByHandleAt(mountFD int, fh *fileHandle, flags int) (fd int, err error) = SYS_OPEN_BY_HANDLE_AT
// fileHandle is the argument to nameToHandleAt and openByHandleAt. We
2024-02-18 10:42:21 +00:00
// originally tried to generate it via unix/linux/types.go with "type
2024-02-18 10:42:21 +00:00
// fileHandle C.struct_file_handle" but that generated empty structs
2024-02-18 10:42:21 +00:00
// for mips64 and mips64le. Instead, hard code it for now (it's the
2024-02-18 10:42:21 +00:00
// same everywhere else) until the mips64 generator issue is fixed.
2024-02-18 10:42:21 +00:00
type fileHandle struct {
Bytes uint32
Type int32
2024-02-18 10:42:21 +00:00
}
// FileHandle represents the C struct file_handle used by
2024-02-18 10:42:21 +00:00
// name_to_handle_at (see NameToHandleAt) and open_by_handle_at (see
2024-02-18 10:42:21 +00:00
// OpenByHandleAt).
2024-02-18 10:42:21 +00:00
type FileHandle struct {
*fileHandle
}
// NewFileHandle constructs a FileHandle.
2024-02-18 10:42:21 +00:00
func NewFileHandle(handleType int32, handle []byte) FileHandle {
2024-02-18 10:42:21 +00:00
const hdrSize = unsafe.Sizeof(fileHandle{})
2024-02-18 10:42:21 +00:00
buf := make([]byte, hdrSize+uintptr(len(handle)))
2024-02-18 10:42:21 +00:00
copy(buf[hdrSize:], handle)
2024-02-18 10:42:21 +00:00
fh := (*fileHandle)(unsafe.Pointer(&buf[0]))
2024-02-18 10:42:21 +00:00
fh.Type = handleType
2024-02-18 10:42:21 +00:00
fh.Bytes = uint32(len(handle))
2024-02-18 10:42:21 +00:00
return FileHandle{fh}
2024-02-18 10:42:21 +00:00
}
func (fh *FileHandle) Size() int { return int(fh.fileHandle.Bytes) }
2024-02-18 10:42:21 +00:00
func (fh *FileHandle) Type() int32 { return fh.fileHandle.Type }
2024-02-18 10:42:21 +00:00
func (fh *FileHandle) Bytes() []byte {
2024-02-18 10:42:21 +00:00
n := fh.Size()
2024-02-18 10:42:21 +00:00
if n == 0 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&fh.fileHandle.Type))+4)), n)
2024-02-18 10:42:21 +00:00
}
// NameToHandleAt wraps the name_to_handle_at system call; it obtains
2024-02-18 10:42:21 +00:00
// a handle for a path name.
2024-02-18 10:42:21 +00:00
func NameToHandleAt(dirfd int, path string, flags int) (handle FileHandle, mountID int, err error) {
2024-02-18 10:42:21 +00:00
var mid _C_int
2024-02-18 10:42:21 +00:00
// Try first with a small buffer, assuming the handle will
2024-02-18 10:42:21 +00:00
// only be 32 bytes.
2024-02-18 10:42:21 +00:00
size := uint32(32 + unsafe.Sizeof(fileHandle{}))
2024-02-18 10:42:21 +00:00
didResize := false
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
buf := make([]byte, size)
2024-02-18 10:42:21 +00:00
fh := (*fileHandle)(unsafe.Pointer(&buf[0]))
2024-02-18 10:42:21 +00:00
fh.Bytes = size - uint32(unsafe.Sizeof(fileHandle{}))
2024-02-18 10:42:21 +00:00
err = nameToHandleAt(dirfd, path, fh, &mid, flags)
2024-02-18 10:42:21 +00:00
if err == EOVERFLOW {
2024-02-18 10:42:21 +00:00
if didResize {
2024-02-18 10:42:21 +00:00
// We shouldn't need to resize more than once
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
didResize = true
2024-02-18 10:42:21 +00:00
size = fh.Bytes + uint32(unsafe.Sizeof(fileHandle{}))
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
if 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 FileHandle{fh}, int(mid), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// OpenByHandleAt wraps the open_by_handle_at system call; it opens a
2024-02-18 10:42:21 +00:00
// file via a handle as previously returned by NameToHandleAt.
2024-02-18 10:42:21 +00:00
func OpenByHandleAt(mountFD int, handle FileHandle, flags int) (fd int, err error) {
2024-02-18 10:42:21 +00:00
return openByHandleAt(mountFD, handle.fileHandle, flags)
2024-02-18 10:42:21 +00:00
}
// Klogset wraps the sys_syslog system call; it sets console_loglevel to
2024-02-18 10:42:21 +00:00
// the value specified by arg and passes a dummy pointer to bufp.
2024-02-18 10:42:21 +00:00
func Klogset(typ int, arg int) (err error) {
2024-02-18 10:42:21 +00:00
var p unsafe.Pointer
2024-02-18 10:42:21 +00:00
_, _, errno := Syscall(SYS_SYSLOG, uintptr(typ), uintptr(p), uintptr(arg))
2024-02-18 10:42:21 +00:00
if errno != 0 {
2024-02-18 10:42:21 +00:00
return errnoErr(errno)
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
}
// RemoteIovec is Iovec with the pointer replaced with an integer.
2024-02-18 10:42:21 +00:00
// It is used for ProcessVMReadv and ProcessVMWritev, where the pointer
2024-02-18 10:42:21 +00:00
// refers to a location in a different process' address space, which
2024-02-18 10:42:21 +00:00
// would confuse the Go garbage collector.
2024-02-18 10:42:21 +00:00
type RemoteIovec struct {
Base uintptr
Len int
2024-02-18 10:42:21 +00:00
}
//sys ProcessVMReadv(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_READV
2024-02-18 10:42:21 +00:00
//sys ProcessVMWritev(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_WRITEV
//sys PidfdOpen(pid int, flags int) (fd int, err error) = SYS_PIDFD_OPEN
2024-02-18 10:42:21 +00:00
//sys PidfdGetfd(pidfd int, targetfd int, flags int) (fd int, err error) = SYS_PIDFD_GETFD
2024-02-18 10:42:21 +00:00
//sys PidfdSendSignal(pidfd int, sig Signal, info *Siginfo, flags int) (err error) = SYS_PIDFD_SEND_SIGNAL
//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)
//sys getitimer(which int, currValue *Itimerval) (err error)
2024-02-18 10:42:21 +00:00
//sys setitimer(which int, newValue *Itimerval, oldValue *Itimerval) (err error)
// MakeItimerval creates an Itimerval from interval and value durations.
2024-02-18 10:42:21 +00:00
func MakeItimerval(interval, value time.Duration) Itimerval {
2024-02-18 10:42:21 +00:00
return Itimerval{
2024-02-18 10:42:21 +00:00
Interval: NsecToTimeval(interval.Nanoseconds()),
Value: NsecToTimeval(value.Nanoseconds()),
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// A value which may be passed to the which parameter for Getitimer and
2024-02-18 10:42:21 +00:00
// Setitimer.
2024-02-18 10:42:21 +00:00
type ItimerWhich int
// Possible which values for Getitimer and Setitimer.
2024-02-18 10:42:21 +00:00
const (
ItimerReal ItimerWhich = ITIMER_REAL
2024-02-18 10:42:21 +00:00
ItimerVirtual ItimerWhich = ITIMER_VIRTUAL
ItimerProf ItimerWhich = ITIMER_PROF
2024-02-18 10:42:21 +00:00
)
// Getitimer wraps getitimer(2) to return the current value of the timer
2024-02-18 10:42:21 +00:00
// specified by which.
2024-02-18 10:42:21 +00:00
func Getitimer(which ItimerWhich) (Itimerval, error) {
2024-02-18 10:42:21 +00:00
var it Itimerval
2024-02-18 10:42:21 +00:00
if err := getitimer(int(which), &it); err != nil {
2024-02-18 10:42:21 +00:00
return Itimerval{}, err
2024-02-18 10:42:21 +00:00
}
return it, nil
2024-02-18 10:42:21 +00:00
}
// Setitimer wraps setitimer(2) to arm or disarm the timer specified by which.
2024-02-18 10:42:21 +00:00
// It returns the previous value of the timer.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If the Itimerval argument is the zero value, the timer will be disarmed.
2024-02-18 10:42:21 +00:00
func Setitimer(which ItimerWhich, it Itimerval) (Itimerval, error) {
2024-02-18 10:42:21 +00:00
var prev Itimerval
2024-02-18 10:42:21 +00:00
if err := setitimer(int(which), &it, &prev); err != nil {
2024-02-18 10:42:21 +00:00
return Itimerval{}, err
2024-02-18 10:42:21 +00:00
}
return prev, nil
2024-02-18 10:42:21 +00:00
}
//sysnb rtSigprocmask(how int, set *Sigset_t, oldset *Sigset_t, sigsetsize uintptr) (err error) = SYS_RT_SIGPROCMASK
func PthreadSigmask(how int, set, oldset *Sigset_t) error {
2024-02-18 10:42:21 +00:00
if oldset != nil {
2024-02-18 10:42:21 +00:00
// Explicitly clear in case Sigset_t is larger than _C__NSIG.
2024-02-18 10:42:21 +00:00
*oldset = Sigset_t{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return rtSigprocmask(how, set, oldset, _C__NSIG/8)
2024-02-18 10:42:21 +00:00
}
//sysnb getresuid(ruid *_C_int, euid *_C_int, suid *_C_int)
2024-02-18 10:42:21 +00:00
//sysnb getresgid(rgid *_C_int, egid *_C_int, sgid *_C_int)
func Getresuid() (ruid, euid, suid int) {
2024-02-18 10:42:21 +00:00
var r, e, s _C_int
2024-02-18 10:42:21 +00:00
getresuid(&r, &e, &s)
2024-02-18 10:42:21 +00:00
return int(r), int(e), int(s)
2024-02-18 10:42:21 +00:00
}
func Getresgid() (rgid, egid, sgid int) {
2024-02-18 10:42:21 +00:00
var r, e, s _C_int
2024-02-18 10:42:21 +00:00
getresgid(&r, &e, &s)
2024-02-18 10:42:21 +00:00
return int(r), int(e), int(s)
2024-02-18 10:42:21 +00:00
}
// Pselect is a wrapper around the Linux pselect6 system call.
2024-02-18 10:42:21 +00:00
// This version does not modify the timeout argument.
2024-02-18 10:42:21 +00:00
func Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error) {
2024-02-18 10:42:21 +00:00
// Per https://man7.org/linux/man-pages/man2/select.2.html#NOTES,
2024-02-18 10:42:21 +00:00
// The Linux pselect6() system call modifies its timeout argument.
2024-02-18 10:42:21 +00:00
// [Not modifying the argument] is the behavior required by POSIX.1-2001.
2024-02-18 10:42:21 +00:00
var mutableTimeout *Timespec
2024-02-18 10:42:21 +00:00
if timeout != nil {
2024-02-18 10:42:21 +00:00
mutableTimeout = new(Timespec)
2024-02-18 10:42:21 +00:00
*mutableTimeout = *timeout
2024-02-18 10:42:21 +00:00
}
// The final argument of the pselect6() system call is not a
2024-02-18 10:42:21 +00:00
// sigset_t * pointer, but is instead a structure
2024-02-18 10:42:21 +00:00
var kernelMask *sigset_argpack
2024-02-18 10:42:21 +00:00
if sigmask != nil {
2024-02-18 10:42:21 +00:00
wordBits := 32 << (^uintptr(0) >> 63) // see math.intSize
// A sigset stores one bit per signal,
2024-02-18 10:42:21 +00:00
// offset by 1 (because signal 0 does not exist).
2024-02-18 10:42:21 +00:00
// So the number of words needed is ⌈__C_NSIG - 1 / wordBits⌉.
2024-02-18 10:42:21 +00:00
sigsetWords := (_C__NSIG - 1 + wordBits - 1) / (wordBits)
sigsetBytes := uintptr(sigsetWords * (wordBits / 8))
2024-02-18 10:42:21 +00:00
kernelMask = &sigset_argpack{
ss: sigmask,
2024-02-18 10:42:21 +00:00
ssLen: sigsetBytes,
}
2024-02-18 10:42:21 +00:00
}
return pselect6(nfd, r, w, e, mutableTimeout, kernelMask)
2024-02-18 10:42:21 +00:00
}
//sys schedSetattr(pid int, attr *SchedAttr, flags uint) (err error)
2024-02-18 10:42:21 +00:00
//sys schedGetattr(pid int, attr *SchedAttr, size uint, flags uint) (err error)
// SchedSetAttr is a wrapper for sched_setattr(2) syscall.
2024-02-18 10:42:21 +00:00
// https://man7.org/linux/man-pages/man2/sched_setattr.2.html
2024-02-18 10:42:21 +00:00
func SchedSetAttr(pid int, attr *SchedAttr, flags uint) error {
2024-02-18 10:42:21 +00:00
if attr == nil {
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
attr.Size = SizeofSchedAttr
2024-02-18 10:42:21 +00:00
return schedSetattr(pid, attr, flags)
2024-02-18 10:42:21 +00:00
}
// SchedGetAttr is a wrapper for sched_getattr(2) syscall.
2024-02-18 10:42:21 +00:00
// https://man7.org/linux/man-pages/man2/sched_getattr.2.html
2024-02-18 10:42:21 +00:00
func SchedGetAttr(pid int, flags uint) (*SchedAttr, error) {
2024-02-18 10:42:21 +00:00
attr := &SchedAttr{}
2024-02-18 10:42:21 +00:00
if err := schedGetattr(pid, attr, SizeofSchedAttr, flags); 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 attr, nil
2024-02-18 10:42:21 +00:00
}
//sys Cachestat(fd uint, crange *CachestatRange, cstat *Cachestat_t, flags uint) (err error)