github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/golang.org/x/sys/unix/syscall_freebsd.go (about)

     1  // Copyright 2009,2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // FreeBSD system calls.
     6  // This file is compiled as ordinary Go code,
     7  // but it is also input to mksyscall,
     8  // which parses the //sys lines and generates system call stubs.
     9  // Note that sometimes we use a lowercase //sys name and wrap
    10  // it in our own nicer implementation, either here or in
    11  // syscall_bsd.go or syscall_unix.go.
    12  
    13  package unix
    14  
    15  import (
    16  	"sync"
    17  	"unsafe"
    18  )
    19  
    20  // See https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/versions.html.
    21  var (
    22  	osreldateOnce sync.Once
    23  	osreldate     uint32
    24  )
    25  
    26  func supportsABI(ver uint32) bool {
    27  	osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") })
    28  	return osreldate >= ver
    29  }
    30  
    31  // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
    32  type SockaddrDatalink struct {
    33  	Len    uint8
    34  	Family uint8
    35  	Index  uint16
    36  	Type   uint8
    37  	Nlen   uint8
    38  	Alen   uint8
    39  	Slen   uint8
    40  	Data   [46]int8
    41  	raw    RawSockaddrDatalink
    42  }
    43  
    44  func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
    45  	return nil, EAFNOSUPPORT
    46  }
    47  
    48  // Translate "kern.hostname" to []_C_int{0,1,2,3}.
    49  func nametomib(name string) (mib []_C_int, err error) {
    50  	const siz = unsafe.Sizeof(mib[0])
    51  
    52  	// NOTE(rsc): It seems strange to set the buffer to have
    53  	// size CTL_MAXNAME+2 but use only CTL_MAXNAME
    54  	// as the size. I don't know why the +2 is here, but the
    55  	// kernel uses +2 for its own implementation of this function.
    56  	// I am scared that if we don't include the +2 here, the kernel
    57  	// will silently write 2 words farther than we specify
    58  	// and we'll get memory corruption.
    59  	var buf [CTL_MAXNAME + 2]_C_int
    60  	n := uintptr(CTL_MAXNAME) * siz
    61  
    62  	p := (*byte)(unsafe.Pointer(&buf[0]))
    63  	bytes, err := ByteSliceFromString(name)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	// Magic sysctl: "setting" 0.3 to a string name
    69  	// lets you read back the array of integers form.
    70  	if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
    71  		return nil, err
    72  	}
    73  	return buf[0 : n/siz], nil
    74  }
    75  
    76  func direntIno(buf []byte) (uint64, bool) {
    77  	return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
    78  }
    79  
    80  func direntReclen(buf []byte) (uint64, bool) {
    81  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
    82  }
    83  
    84  func direntNamlen(buf []byte) (uint64, bool) {
    85  	return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
    86  }
    87  
    88  func Pipe(p []int) (err error) {
    89  	return Pipe2(p, 0)
    90  }
    91  
    92  //sysnb	pipe2(p *[2]_C_int, flags int) (err error)
    93  
    94  func Pipe2(p []int, flags int) error {
    95  	if len(p) != 2 {
    96  		return EINVAL
    97  	}
    98  	var pp [2]_C_int
    99  	err := pipe2(&pp, flags)
   100  	if err == nil {
   101  		p[0] = int(pp[0])
   102  		p[1] = int(pp[1])
   103  	}
   104  	return err
   105  }
   106  
   107  func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
   108  	var value IPMreqn
   109  	vallen := _Socklen(SizeofIPMreqn)
   110  	errno := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   111  	return &value, errno
   112  }
   113  
   114  func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
   115  	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq))
   116  }
   117  
   118  // GetsockoptXucred is a getsockopt wrapper that returns an Xucred struct.
   119  // The usual level and opt are SOL_LOCAL and LOCAL_PEERCRED, respectively.
   120  func GetsockoptXucred(fd, level, opt int) (*Xucred, error) {
   121  	x := new(Xucred)
   122  	vallen := _Socklen(SizeofXucred)
   123  	err := getsockopt(fd, level, opt, unsafe.Pointer(x), &vallen)
   124  	return x, err
   125  }
   126  
   127  func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) {
   128  	var rsa RawSockaddrAny
   129  	var len _Socklen = SizeofSockaddrAny
   130  	nfd, err = accept4(fd, &rsa, &len, flags)
   131  	if err != nil {
   132  		return
   133  	}
   134  	if len > SizeofSockaddrAny {
   135  		panic("RawSockaddrAny too small")
   136  	}
   137  	sa, err = anyToSockaddr(fd, &rsa)
   138  	if err != nil {
   139  		Close(nfd)
   140  		nfd = 0
   141  	}
   142  	return
   143  }
   144  
   145  //sys	Getcwd(buf []byte) (n int, err error) = SYS___GETCWD
   146  
   147  func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
   148  	var (
   149  		_p0     unsafe.Pointer
   150  		bufsize uintptr
   151  	)
   152  	if len(buf) > 0 {
   153  		_p0 = unsafe.Pointer(&buf[0])
   154  		bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
   155  	}
   156  	r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
   157  	n = int(r0)
   158  	if e1 != 0 {
   159  		err = e1
   160  	}
   161  	return
   162  }
   163  
   164  //sys	ioctl(fd int, req uint, arg uintptr) (err error) = SYS_IOCTL
   165  //sys	ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL
   166  
   167  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
   168  
   169  func Uname(uname *Utsname) error {
   170  	mib := []_C_int{CTL_KERN, KERN_OSTYPE}
   171  	n := unsafe.Sizeof(uname.Sysname)
   172  	if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
   173  		return err
   174  	}
   175  
   176  	mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
   177  	n = unsafe.Sizeof(uname.Nodename)
   178  	if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
   179  		return err
   180  	}
   181  
   182  	mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
   183  	n = unsafe.Sizeof(uname.Release)
   184  	if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
   185  		return err
   186  	}
   187  
   188  	mib = []_C_int{CTL_KERN, KERN_VERSION}
   189  	n = unsafe.Sizeof(uname.Version)
   190  	if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
   191  		return err
   192  	}
   193  
   194  	// The version might have newlines or tabs in it, convert them to
   195  	// spaces.
   196  	for i, b := range uname.Version {
   197  		if b == '\n' || b == '\t' {
   198  			if i == len(uname.Version)-1 {
   199  				uname.Version[i] = 0
   200  			} else {
   201  				uname.Version[i] = ' '
   202  			}
   203  		}
   204  	}
   205  
   206  	mib = []_C_int{CTL_HW, HW_MACHINE}
   207  	n = unsafe.Sizeof(uname.Machine)
   208  	if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
   209  		return err
   210  	}
   211  
   212  	return nil
   213  }
   214  
   215  func Stat(path string, st *Stat_t) (err error) {
   216  	return Fstatat(AT_FDCWD, path, st, 0)
   217  }
   218  
   219  func Lstat(path string, st *Stat_t) (err error) {
   220  	return Fstatat(AT_FDCWD, path, st, AT_SYMLINK_NOFOLLOW)
   221  }
   222  
   223  func Getdents(fd int, buf []byte) (n int, err error) {
   224  	return Getdirentries(fd, buf, nil)
   225  }
   226  
   227  func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
   228  	if basep == nil || unsafe.Sizeof(*basep) == 8 {
   229  		return getdirentries(fd, buf, (*uint64)(unsafe.Pointer(basep)))
   230  	}
   231  	// The syscall needs a 64-bit base. On 32-bit machines
   232  	// we can't just use the basep passed in. See #32498.
   233  	var base uint64 = uint64(*basep)
   234  	n, err = getdirentries(fd, buf, &base)
   235  	*basep = uintptr(base)
   236  	if base>>32 != 0 {
   237  		// We can't stuff the base back into a uintptr, so any
   238  		// future calls would be suspect. Generate an error.
   239  		// EIO is allowed by getdirentries.
   240  		err = EIO
   241  	}
   242  	return
   243  }
   244  
   245  func Mknod(path string, mode uint32, dev uint64) (err error) {
   246  	return Mknodat(AT_FDCWD, path, mode, dev)
   247  }
   248  
   249  func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   250  	if raceenabled {
   251  		raceReleaseMerge(unsafe.Pointer(&ioSync))
   252  	}
   253  	return sendfile(outfd, infd, offset, count)
   254  }
   255  
   256  //sys	ptrace(request int, pid int, addr uintptr, data int) (err error)
   257  //sys	ptracePtr(request int, pid int, addr unsafe.Pointer, data int) (err error) = SYS_PTRACE
   258  
   259  func PtraceAttach(pid int) (err error) {
   260  	return ptrace(PT_ATTACH, pid, 0, 0)
   261  }
   262  
   263  func PtraceCont(pid int, signal int) (err error) {
   264  	return ptrace(PT_CONTINUE, pid, 1, signal)
   265  }
   266  
   267  func PtraceDetach(pid int) (err error) {
   268  	return ptrace(PT_DETACH, pid, 1, 0)
   269  }
   270  
   271  func PtraceGetFpRegs(pid int, fpregsout *FpReg) (err error) {
   272  	return ptracePtr(PT_GETFPREGS, pid, unsafe.Pointer(fpregsout), 0)
   273  }
   274  
   275  func PtraceGetRegs(pid int, regsout *Reg) (err error) {
   276  	return ptracePtr(PT_GETREGS, pid, unsafe.Pointer(regsout), 0)
   277  }
   278  
   279  func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) {
   280  	ioDesc := PtraceIoDesc{
   281  		Op:   int32(req),
   282  		Offs: offs,
   283  	}
   284  	if countin > 0 {
   285  		_ = out[:countin] // check bounds
   286  		ioDesc.Addr = &out[0]
   287  	} else if out != nil {
   288  		ioDesc.Addr = (*byte)(unsafe.Pointer(&_zero))
   289  	}
   290  	ioDesc.SetLen(countin)
   291  
   292  	err = ptracePtr(PT_IO, pid, unsafe.Pointer(&ioDesc), 0)
   293  	return int(ioDesc.Len), err
   294  }
   295  
   296  func PtraceLwpEvents(pid int, enable int) (err error) {
   297  	return ptrace(PT_LWP_EVENTS, pid, 0, enable)
   298  }
   299  
   300  func PtraceLwpInfo(pid int, info *PtraceLwpInfoStruct) (err error) {
   301  	return ptracePtr(PT_LWPINFO, pid, unsafe.Pointer(info), int(unsafe.Sizeof(*info)))
   302  }
   303  
   304  func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
   305  	return PtraceIO(PIOD_READ_D, pid, addr, out, SizeofLong)
   306  }
   307  
   308  func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) {
   309  	return PtraceIO(PIOD_READ_I, pid, addr, out, SizeofLong)
   310  }
   311  
   312  func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) {
   313  	return PtraceIO(PIOD_WRITE_D, pid, addr, data, SizeofLong)
   314  }
   315  
   316  func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
   317  	return PtraceIO(PIOD_WRITE_I, pid, addr, data, SizeofLong)
   318  }
   319  
   320  func PtraceSetRegs(pid int, regs *Reg) (err error) {
   321  	return ptracePtr(PT_SETREGS, pid, unsafe.Pointer(regs), 0)
   322  }
   323  
   324  func PtraceSingleStep(pid int) (err error) {
   325  	return ptrace(PT_STEP, pid, 1, 0)
   326  }
   327  
   328  func Dup3(oldfd, newfd, flags int) error {
   329  	if oldfd == newfd || flags&^O_CLOEXEC != 0 {
   330  		return EINVAL
   331  	}
   332  	how := F_DUP2FD
   333  	if flags&O_CLOEXEC != 0 {
   334  		how = F_DUP2FD_CLOEXEC
   335  	}
   336  	_, err := fcntl(oldfd, how, newfd)
   337  	return err
   338  }
   339  
   340  /*
   341   * Exposed directly
   342   */
   343  //sys	Access(path string, mode uint32) (err error)
   344  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   345  //sys	CapEnter() (err error)
   346  //sys	capRightsGet(version int, fd int, rightsp *CapRights) (err error) = SYS___CAP_RIGHTS_GET
   347  //sys	capRightsLimit(fd int, rightsp *CapRights) (err error)
   348  //sys	Chdir(path string) (err error)
   349  //sys	Chflags(path string, flags int) (err error)
   350  //sys	Chmod(path string, mode uint32) (err error)
   351  //sys	Chown(path string, uid int, gid int) (err error)
   352  //sys	Chroot(path string) (err error)
   353  //sys	ClockGettime(clockid int32, time *Timespec) (err error)
   354  //sys	Close(fd int) (err error)
   355  //sys	Dup(fd int) (nfd int, err error)
   356  //sys	Dup2(from int, to int) (err error)
   357  //sys	Exit(code int)
   358  //sys	ExtattrGetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
   359  //sys	ExtattrSetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
   360  //sys	ExtattrDeleteFd(fd int, attrnamespace int, attrname string) (err error)
   361  //sys	ExtattrListFd(fd int, attrnamespace int, data uintptr, nbytes int) (ret int, err error)
   362  //sys	ExtattrGetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
   363  //sys	ExtattrSetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
   364  //sys	ExtattrDeleteFile(file string, attrnamespace int, attrname string) (err error)
   365  //sys	ExtattrListFile(file string, attrnamespace int, data uintptr, nbytes int) (ret int, err error)
   366  //sys	ExtattrGetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
   367  //sys	ExtattrSetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
   368  //sys	ExtattrDeleteLink(link string, attrnamespace int, attrname string) (err error)
   369  //sys	ExtattrListLink(link string, attrnamespace int, data uintptr, nbytes int) (ret int, err error)
   370  //sys	Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_POSIX_FADVISE
   371  //sys	Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
   372  //sys	Fchdir(fd int) (err error)
   373  //sys	Fchflags(fd int, flags int) (err error)
   374  //sys	Fchmod(fd int, mode uint32) (err error)
   375  //sys	Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
   376  //sys	Fchown(fd int, uid int, gid int) (err error)
   377  //sys	Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
   378  //sys	Flock(fd int, how int) (err error)
   379  //sys	Fpathconf(fd int, name int) (val int, err error)
   380  //sys	Fstat(fd int, stat *Stat_t) (err error)
   381  //sys	Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
   382  //sys	Fstatfs(fd int, stat *Statfs_t) (err error)
   383  //sys	Fsync(fd int) (err error)
   384  //sys	Ftruncate(fd int, length int64) (err error)
   385  //sys	getdirentries(fd int, buf []byte, basep *uint64) (n int, err error)
   386  //sys	Getdtablesize() (size int)
   387  //sysnb	Getegid() (egid int)
   388  //sysnb	Geteuid() (uid int)
   389  //sysnb	Getgid() (gid int)
   390  //sysnb	Getpgid(pid int) (pgid int, err error)
   391  //sysnb	Getpgrp() (pgrp int)
   392  //sysnb	Getpid() (pid int)
   393  //sysnb	Getppid() (ppid int)
   394  //sys	Getpriority(which int, who int) (prio int, err error)
   395  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   396  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   397  //sysnb	Getsid(pid int) (sid int, err error)
   398  //sysnb	Gettimeofday(tv *Timeval) (err error)
   399  //sysnb	Getuid() (uid int)
   400  //sys	Issetugid() (tainted bool)
   401  //sys	Kill(pid int, signum syscall.Signal) (err error)
   402  //sys	Kqueue() (fd int, err error)
   403  //sys	Lchown(path string, uid int, gid int) (err error)
   404  //sys	Link(path string, link string) (err error)
   405  //sys	Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
   406  //sys	Listen(s int, backlog int) (err error)
   407  //sys	Mkdir(path string, mode uint32) (err error)
   408  //sys	Mkdirat(dirfd int, path string, mode uint32) (err error)
   409  //sys	Mkfifo(path string, mode uint32) (err error)
   410  //sys	Mknodat(fd int, path string, mode uint32, dev uint64) (err error)
   411  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   412  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   413  //sys	Openat(fdat int, path string, mode int, perm uint32) (fd int, err error)
   414  //sys	Pathconf(path string, name int) (val int, err error)
   415  //sys	pread(fd int, p []byte, offset int64) (n int, err error)
   416  //sys	pwrite(fd int, p []byte, offset int64) (n int, err error)
   417  //sys	read(fd int, p []byte) (n int, err error)
   418  //sys	Readlink(path string, buf []byte) (n int, err error)
   419  //sys	Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
   420  //sys	Rename(from string, to string) (err error)
   421  //sys	Renameat(fromfd int, from string, tofd int, to string) (err error)
   422  //sys	Revoke(path string) (err error)
   423  //sys	Rmdir(path string) (err error)
   424  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
   425  //sys	Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
   426  //sysnb	Setegid(egid int) (err error)
   427  //sysnb	Seteuid(euid int) (err error)
   428  //sysnb	Setgid(gid int) (err error)
   429  //sys	Setlogin(name string) (err error)
   430  //sysnb	Setpgid(pid int, pgid int) (err error)
   431  //sys	Setpriority(which int, who int, prio int) (err error)
   432  //sysnb	Setregid(rgid int, egid int) (err error)
   433  //sysnb	Setreuid(ruid int, euid int) (err error)
   434  //sysnb	Setresgid(rgid int, egid int, sgid int) (err error)
   435  //sysnb	Setresuid(ruid int, euid int, suid int) (err error)
   436  //sysnb	Setsid() (pid int, err error)
   437  //sysnb	Settimeofday(tp *Timeval) (err error)
   438  //sysnb	Setuid(uid int) (err error)
   439  //sys	Statfs(path string, stat *Statfs_t) (err error)
   440  //sys	Symlink(path string, link string) (err error)
   441  //sys	Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
   442  //sys	Sync() (err error)
   443  //sys	Truncate(path string, length int64) (err error)
   444  //sys	Umask(newmask int) (oldmask int)
   445  //sys	Undelete(path string) (err error)
   446  //sys	Unlink(path string) (err error)
   447  //sys	Unlinkat(dirfd int, path string, flags int) (err error)
   448  //sys	Unmount(path string, flags int) (err error)
   449  //sys	write(fd int, p []byte) (n int, err error)
   450  //sys	mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   451  //sys	munmap(addr uintptr, length uintptr) (err error)
   452  //sys	accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
   453  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)