golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/unix/syscall_dragonfly.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // DragonFly BSD 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 version list in https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/sys/sys/param.h
    21  var (
    22  	osreldateOnce sync.Once
    23  	osreldate     uint32
    24  )
    25  
    26  // First __DragonFly_version after September 2019 ABI changes
    27  // http://lists.dragonflybsd.org/pipermail/users/2019-September/358280.html
    28  const _dragonflyABIChangeVersion = 500705
    29  
    30  func supportsABI(ver uint32) bool {
    31  	osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") })
    32  	return osreldate >= ver
    33  }
    34  
    35  // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
    36  type SockaddrDatalink struct {
    37  	Len    uint8
    38  	Family uint8
    39  	Index  uint16
    40  	Type   uint8
    41  	Nlen   uint8
    42  	Alen   uint8
    43  	Slen   uint8
    44  	Data   [12]int8
    45  	Rcf    uint16
    46  	Route  [16]uint16
    47  	raw    RawSockaddrDatalink
    48  }
    49  
    50  func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
    51  	return nil, EAFNOSUPPORT
    52  }
    53  
    54  // Translate "kern.hostname" to []_C_int{0,1,2,3}.
    55  func nametomib(name string) (mib []_C_int, err error) {
    56  	const siz = unsafe.Sizeof(mib[0])
    57  
    58  	// NOTE(rsc): It seems strange to set the buffer to have
    59  	// size CTL_MAXNAME+2 but use only CTL_MAXNAME
    60  	// as the size. I don't know why the +2 is here, but the
    61  	// kernel uses +2 for its own implementation of this function.
    62  	// I am scared that if we don't include the +2 here, the kernel
    63  	// will silently write 2 words farther than we specify
    64  	// and we'll get memory corruption.
    65  	var buf [CTL_MAXNAME + 2]_C_int
    66  	n := uintptr(CTL_MAXNAME) * siz
    67  
    68  	p := (*byte)(unsafe.Pointer(&buf[0]))
    69  	bytes, err := ByteSliceFromString(name)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	// Magic sysctl: "setting" 0.3 to a string name
    75  	// lets you read back the array of integers form.
    76  	if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
    77  		return nil, err
    78  	}
    79  	return buf[0 : n/siz], nil
    80  }
    81  
    82  func direntIno(buf []byte) (uint64, bool) {
    83  	return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
    84  }
    85  
    86  func direntReclen(buf []byte) (uint64, bool) {
    87  	namlen, ok := direntNamlen(buf)
    88  	if !ok {
    89  		return 0, false
    90  	}
    91  	return (16 + namlen + 1 + 7) &^ 7, true
    92  }
    93  
    94  func direntNamlen(buf []byte) (uint64, bool) {
    95  	return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
    96  }
    97  
    98  //sysnb	pipe() (r int, w int, err error)
    99  
   100  func Pipe(p []int) (err error) {
   101  	if len(p) != 2 {
   102  		return EINVAL
   103  	}
   104  	r, w, err := pipe()
   105  	if err == nil {
   106  		p[0], p[1] = r, w
   107  	}
   108  	return
   109  }
   110  
   111  //sysnb	pipe2(p *[2]_C_int, flags int) (r int, w int, err error)
   112  
   113  func Pipe2(p []int, flags int) (err error) {
   114  	if len(p) != 2 {
   115  		return EINVAL
   116  	}
   117  	var pp [2]_C_int
   118  	// pipe2 on dragonfly takes an fds array as an argument, but still
   119  	// returns the file descriptors.
   120  	r, w, err := pipe2(&pp, flags)
   121  	if err == nil {
   122  		p[0], p[1] = r, w
   123  	}
   124  	return err
   125  }
   126  
   127  //sys	extpread(fd int, p []byte, flags int, offset int64) (n int, err error)
   128  
   129  func pread(fd int, p []byte, offset int64) (n int, err error) {
   130  	return extpread(fd, p, 0, offset)
   131  }
   132  
   133  //sys	extpwrite(fd int, p []byte, flags int, offset int64) (n int, err error)
   134  
   135  func pwrite(fd int, p []byte, offset int64) (n int, err error) {
   136  	return extpwrite(fd, p, 0, offset)
   137  }
   138  
   139  func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) {
   140  	var rsa RawSockaddrAny
   141  	var len _Socklen = SizeofSockaddrAny
   142  	nfd, err = accept4(fd, &rsa, &len, flags)
   143  	if err != nil {
   144  		return
   145  	}
   146  	if len > SizeofSockaddrAny {
   147  		panic("RawSockaddrAny too small")
   148  	}
   149  	sa, err = anyToSockaddr(fd, &rsa)
   150  	if err != nil {
   151  		Close(nfd)
   152  		nfd = 0
   153  	}
   154  	return
   155  }
   156  
   157  //sys	Getcwd(buf []byte) (n int, err error) = SYS___GETCWD
   158  
   159  func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
   160  	var _p0 unsafe.Pointer
   161  	var bufsize uintptr
   162  	if len(buf) > 0 {
   163  		_p0 = unsafe.Pointer(&buf[0])
   164  		bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
   165  	}
   166  	r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
   167  	n = int(r0)
   168  	if e1 != 0 {
   169  		err = e1
   170  	}
   171  	return
   172  }
   173  
   174  //sys	ioctl(fd int, req uint, arg uintptr) (err error)
   175  //sys	ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL
   176  
   177  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
   178  
   179  func sysctlUname(mib []_C_int, old *byte, oldlen *uintptr) error {
   180  	err := sysctl(mib, old, oldlen, nil, 0)
   181  	if err != nil {
   182  		// Utsname members on Dragonfly are only 32 bytes and
   183  		// the syscall returns ENOMEM in case the actual value
   184  		// is longer.
   185  		if err == ENOMEM {
   186  			err = nil
   187  		}
   188  	}
   189  	return err
   190  }
   191  
   192  func Uname(uname *Utsname) error {
   193  	mib := []_C_int{CTL_KERN, KERN_OSTYPE}
   194  	n := unsafe.Sizeof(uname.Sysname)
   195  	if err := sysctlUname(mib, &uname.Sysname[0], &n); err != nil {
   196  		return err
   197  	}
   198  	uname.Sysname[unsafe.Sizeof(uname.Sysname)-1] = 0
   199  
   200  	mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
   201  	n = unsafe.Sizeof(uname.Nodename)
   202  	if err := sysctlUname(mib, &uname.Nodename[0], &n); err != nil {
   203  		return err
   204  	}
   205  	uname.Nodename[unsafe.Sizeof(uname.Nodename)-1] = 0
   206  
   207  	mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
   208  	n = unsafe.Sizeof(uname.Release)
   209  	if err := sysctlUname(mib, &uname.Release[0], &n); err != nil {
   210  		return err
   211  	}
   212  	uname.Release[unsafe.Sizeof(uname.Release)-1] = 0
   213  
   214  	mib = []_C_int{CTL_KERN, KERN_VERSION}
   215  	n = unsafe.Sizeof(uname.Version)
   216  	if err := sysctlUname(mib, &uname.Version[0], &n); err != nil {
   217  		return err
   218  	}
   219  
   220  	// The version might have newlines or tabs in it, convert them to
   221  	// spaces.
   222  	for i, b := range uname.Version {
   223  		if b == '\n' || b == '\t' {
   224  			if i == len(uname.Version)-1 {
   225  				uname.Version[i] = 0
   226  			} else {
   227  				uname.Version[i] = ' '
   228  			}
   229  		}
   230  	}
   231  
   232  	mib = []_C_int{CTL_HW, HW_MACHINE}
   233  	n = unsafe.Sizeof(uname.Machine)
   234  	if err := sysctlUname(mib, &uname.Machine[0], &n); err != nil {
   235  		return err
   236  	}
   237  	uname.Machine[unsafe.Sizeof(uname.Machine)-1] = 0
   238  
   239  	return nil
   240  }
   241  
   242  func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   243  	if raceenabled {
   244  		raceReleaseMerge(unsafe.Pointer(&ioSync))
   245  	}
   246  	return sendfile(outfd, infd, offset, count)
   247  }
   248  
   249  /*
   250   * Exposed directly
   251   */
   252  //sys	Access(path string, mode uint32) (err error)
   253  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   254  //sys	Chdir(path string) (err error)
   255  //sys	Chflags(path string, flags int) (err error)
   256  //sys	Chmod(path string, mode uint32) (err error)
   257  //sys	Chown(path string, uid int, gid int) (err error)
   258  //sys	Chroot(path string) (err error)
   259  //sys	ClockGettime(clockid int32, time *Timespec) (err error)
   260  //sys	Close(fd int) (err error)
   261  //sys	Dup(fd int) (nfd int, err error)
   262  //sys	Dup2(from int, to int) (err error)
   263  //sys	Exit(code int)
   264  //sys	Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
   265  //sys	Fchdir(fd int) (err error)
   266  //sys	Fchflags(fd int, flags int) (err error)
   267  //sys	Fchmod(fd int, mode uint32) (err error)
   268  //sys	Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
   269  //sys	Fchown(fd int, uid int, gid int) (err error)
   270  //sys	Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
   271  //sys	Flock(fd int, how int) (err error)
   272  //sys	Fpathconf(fd int, name int) (val int, err error)
   273  //sys	Fstat(fd int, stat *Stat_t) (err error)
   274  //sys	Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
   275  //sys	Fstatfs(fd int, stat *Statfs_t) (err error)
   276  //sys	Fsync(fd int) (err error)
   277  //sys	Ftruncate(fd int, length int64) (err error)
   278  //sys	Getdents(fd int, buf []byte) (n int, err error)
   279  //sys	Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error)
   280  //sys	Getdtablesize() (size int)
   281  //sysnb	Getegid() (egid int)
   282  //sysnb	Geteuid() (uid int)
   283  //sysnb	Getgid() (gid int)
   284  //sysnb	Getpgid(pid int) (pgid int, err error)
   285  //sysnb	Getpgrp() (pgrp int)
   286  //sysnb	Getpid() (pid int)
   287  //sysnb	Getppid() (ppid int)
   288  //sys	Getpriority(which int, who int) (prio int, err error)
   289  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   290  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   291  //sysnb	Getsid(pid int) (sid int, err error)
   292  //sysnb	Gettimeofday(tv *Timeval) (err error)
   293  //sysnb	Getuid() (uid int)
   294  //sys	Issetugid() (tainted bool)
   295  //sys	Kill(pid int, signum syscall.Signal) (err error)
   296  //sys	Kqueue() (fd int, err error)
   297  //sys	Lchown(path string, uid int, gid int) (err error)
   298  //sys	Link(path string, link string) (err error)
   299  //sys	Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
   300  //sys	Listen(s int, backlog int) (err error)
   301  //sys	Lstat(path string, stat *Stat_t) (err error)
   302  //sys	Mkdir(path string, mode uint32) (err error)
   303  //sys	Mkdirat(dirfd int, path string, mode uint32) (err error)
   304  //sys	Mkfifo(path string, mode uint32) (err error)
   305  //sys	Mknod(path string, mode uint32, dev int) (err error)
   306  //sys	Mknodat(fd int, path string, mode uint32, dev int) (err error)
   307  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   308  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   309  //sys	Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
   310  //sys	Pathconf(path string, name int) (val int, err error)
   311  //sys	read(fd int, p []byte) (n int, err error)
   312  //sys	Readlink(path string, buf []byte) (n int, err error)
   313  //sys	Rename(from string, to string) (err error)
   314  //sys	Renameat(fromfd int, from string, tofd int, to string) (err error)
   315  //sys	Revoke(path string) (err error)
   316  //sys	Rmdir(path string) (err error)
   317  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
   318  //sys	Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
   319  //sysnb	Setegid(egid int) (err error)
   320  //sysnb	Seteuid(euid int) (err error)
   321  //sysnb	Setgid(gid int) (err error)
   322  //sys	Setlogin(name string) (err error)
   323  //sysnb	Setpgid(pid int, pgid int) (err error)
   324  //sys	Setpriority(which int, who int, prio int) (err error)
   325  //sysnb	Setregid(rgid int, egid int) (err error)
   326  //sysnb	Setreuid(ruid int, euid int) (err error)
   327  //sysnb	Setresgid(rgid int, egid int, sgid int) (err error)
   328  //sysnb	Setresuid(ruid int, euid int, suid int) (err error)
   329  //sysnb	Setsid() (pid int, err error)
   330  //sysnb	Settimeofday(tp *Timeval) (err error)
   331  //sysnb	Setuid(uid int) (err error)
   332  //sys	Stat(path string, stat *Stat_t) (err error)
   333  //sys	Statfs(path string, stat *Statfs_t) (err error)
   334  //sys	Symlink(path string, link string) (err error)
   335  //sys	Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
   336  //sys	Sync() (err error)
   337  //sys	Truncate(path string, length int64) (err error)
   338  //sys	Umask(newmask int) (oldmask int)
   339  //sys	Undelete(path string) (err error)
   340  //sys	Unlink(path string) (err error)
   341  //sys	Unlinkat(dirfd int, path string, flags int) (err error)
   342  //sys	Unmount(path string, flags int) (err error)
   343  //sys	write(fd int, p []byte) (n int, err error)
   344  //sys	mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   345  //sys	munmap(addr uintptr, length uintptr) (err error)
   346  //sys	accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
   347  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)