Source file src/syscall/syscall_dragonfly.go

     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 syscall
    14  
    15  import (
    16  	"sync"
    17  	"unsafe"
    18  )
    19  
    20  func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
    21  func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
    22  func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
    23  func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
    24  
    25  const _SYS_DUP3 = 0
    26  
    27  // See version list in https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/sys/sys/param.h
    28  var (
    29  	osreldateOnce sync.Once
    30  	osreldate     uint32
    31  )
    32  
    33  // First __DragonFly_version after September 2019 ABI changes
    34  // http://lists.dragonflybsd.org/pipermail/users/2019-September/358280.html
    35  const _dragonflyABIChangeVersion = 500705
    36  
    37  func supportsABI(ver uint32) bool {
    38  	osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") })
    39  	return osreldate >= ver
    40  }
    41  
    42  type SockaddrDatalink struct {
    43  	Len    uint8
    44  	Family uint8
    45  	Index  uint16
    46  	Type   uint8
    47  	Nlen   uint8
    48  	Alen   uint8
    49  	Slen   uint8
    50  	Data   [12]int8
    51  	Rcf    uint16
    52  	Route  [16]uint16
    53  	raw    RawSockaddrDatalink
    54  }
    55  
    56  // Translate "kern.hostname" to []_C_int{0,1,2,3}.
    57  func nametomib(name string) (mib []_C_int, err error) {
    58  	const siz = unsafe.Sizeof(mib[0])
    59  
    60  	// NOTE(rsc): It seems strange to set the buffer to have
    61  	// size CTL_MAXNAME+2 but use only CTL_MAXNAME
    62  	// as the size. I don't know why the +2 is here, but the
    63  	// kernel uses +2 for its own implementation of this function.
    64  	// I am scared that if we don't include the +2 here, the kernel
    65  	// will silently write 2 words farther than we specify
    66  	// and we'll get memory corruption.
    67  	var buf [CTL_MAXNAME + 2]_C_int
    68  	n := uintptr(CTL_MAXNAME) * siz
    69  
    70  	p := (*byte)(unsafe.Pointer(&buf[0]))
    71  	bytes, err := ByteSliceFromString(name)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	// Magic sysctl: "setting" 0.3 to a string name
    77  	// lets you read back the array of integers form.
    78  	if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
    79  		return nil, err
    80  	}
    81  	return buf[0 : n/siz], nil
    82  }
    83  
    84  func direntIno(buf []byte) (uint64, bool) {
    85  	return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
    86  }
    87  
    88  func direntReclen(buf []byte) (uint64, bool) {
    89  	namlen, ok := direntNamlen(buf)
    90  	if !ok {
    91  		return 0, false
    92  	}
    93  	return (16 + namlen + 1 + 7) &^ 7, true
    94  }
    95  
    96  func direntNamlen(buf []byte) (uint64, bool) {
    97  	return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
    98  }
    99  
   100  //sysnb pipe() (r int, w int, err error)
   101  
   102  func Pipe(p []int) (err error) {
   103  	if len(p) != 2 {
   104  		return EINVAL
   105  	}
   106  	r, w, err := pipe()
   107  	if err == nil {
   108  		p[0], p[1] = r, w
   109  	}
   110  	return err
   111  }
   112  
   113  //sysnb	pipe2(p *[2]_C_int, flags int) (r int, w int, err error)
   114  
   115  func Pipe2(p []int, flags int) (err error) {
   116  	if len(p) != 2 {
   117  		return EINVAL
   118  	}
   119  	var pp [2]_C_int
   120  	// pipe2 on dragonfly takes an fds array as an argument, but still
   121  	// returns the file descriptors.
   122  	r, w, err := pipe2(&pp, flags)
   123  	if err == nil {
   124  		p[0], p[1] = r, w
   125  	}
   126  	return err
   127  }
   128  
   129  //sys	extpread(fd int, p []byte, flags int, offset int64) (n int, err error)
   130  
   131  func pread(fd int, p []byte, offset int64) (n int, err error) {
   132  	return extpread(fd, p, 0, offset)
   133  }
   134  
   135  //sys	extpwrite(fd int, p []byte, flags int, offset int64) (n int, err error)
   136  
   137  func pwrite(fd int, p []byte, offset int64) (n int, err error) {
   138  	return extpwrite(fd, p, 0, offset)
   139  }
   140  
   141  func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) {
   142  	var rsa RawSockaddrAny
   143  	var len _Socklen = SizeofSockaddrAny
   144  	nfd, err = accept4(fd, &rsa, &len, flags)
   145  	if err != nil {
   146  		return
   147  	}
   148  	if len > SizeofSockaddrAny {
   149  		panic("RawSockaddrAny too small")
   150  	}
   151  	sa, err = anyToSockaddr(&rsa)
   152  	if err != nil {
   153  		Close(nfd)
   154  		nfd = 0
   155  	}
   156  	return
   157  }
   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  /*
   175   * Exposed directly
   176   */
   177  //sys	Access(path string, mode uint32) (err error)
   178  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   179  //sys	Chdir(path string) (err error)
   180  //sys	Chflags(path string, flags int) (err error)
   181  //sys	Chmod(path string, mode uint32) (err error)
   182  //sys	Chown(path string, uid int, gid int) (err error)
   183  //sys	Chroot(path string) (err error)
   184  //sys	Close(fd int) (err error)
   185  //sys	Dup(fd int) (nfd int, err error)
   186  //sys	Dup2(from int, to int) (err error)
   187  //sys	Fchdir(fd int) (err error)
   188  //sys	Fchflags(fd int, flags int) (err error)
   189  //sys	Fchmod(fd int, mode uint32) (err error)
   190  //sys	Fchown(fd int, uid int, gid int) (err error)
   191  //sys	Flock(fd int, how int) (err error)
   192  //sys	Fpathconf(fd int, name int) (val int, err error)
   193  //sys	Fstat(fd int, stat *Stat_t) (err error)
   194  //sys	Fstatfs(fd int, stat *Statfs_t) (err error)
   195  //sys	Fsync(fd int) (err error)
   196  //sys	Ftruncate(fd int, length int64) (err error)
   197  //sys	Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error)
   198  //sys	Getdtablesize() (size int)
   199  //sysnb	Getegid() (egid int)
   200  //sysnb	Geteuid() (uid int)
   201  //sysnb	Getgid() (gid int)
   202  //sysnb	Getpgid(pid int) (pgid int, err error)
   203  //sysnb	Getpgrp() (pgrp int)
   204  //sysnb	Getpid() (pid int)
   205  //sysnb	Getppid() (ppid int)
   206  //sys	Getpriority(which int, who int) (prio int, err error)
   207  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   208  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   209  //sysnb	Getsid(pid int) (sid int, err error)
   210  //sysnb	Gettimeofday(tv *Timeval) (err error)
   211  //sysnb	Getuid() (uid int)
   212  //sys	Issetugid() (tainted bool)
   213  //sys	Kill(pid int, signum Signal) (err error)
   214  //sys	Kqueue() (fd int, err error)
   215  //sys	Lchown(path string, uid int, gid int) (err error)
   216  //sys	Link(path string, link string) (err error)
   217  //sys	Listen(s int, backlog int) (err error)
   218  //sys	Lstat(path string, stat *Stat_t) (err error)
   219  //sys	Mkdir(path string, mode uint32) (err error)
   220  //sys	Mkfifo(path string, mode uint32) (err error)
   221  //sys	Mknod(path string, mode uint32, dev int) (err error)
   222  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   223  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   224  //sys	Pathconf(path string, name int) (val int, err error)
   225  //sys	read(fd int, p []byte) (n int, err error)
   226  //sys	Readlink(path string, buf []byte) (n int, err error)
   227  //sys	Rename(from string, to string) (err error)
   228  //sys	Revoke(path string) (err error)
   229  //sys	Rmdir(path string) (err error)
   230  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
   231  //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
   232  //sysnb	Setegid(egid int) (err error)
   233  //sysnb	Seteuid(euid int) (err error)
   234  //sysnb	Setgid(gid int) (err error)
   235  //sys	Setlogin(name string) (err error)
   236  //sysnb	Setpgid(pid int, pgid int) (err error)
   237  //sys	Setpriority(which int, who int, prio int) (err error)
   238  //sysnb	Setregid(rgid int, egid int) (err error)
   239  //sysnb	Setreuid(ruid int, euid int) (err error)
   240  //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
   241  //sysnb	Setsid() (pid int, err error)
   242  //sysnb	Settimeofday(tp *Timeval) (err error)
   243  //sysnb	Setuid(uid int) (err error)
   244  //sys	Stat(path string, stat *Stat_t) (err error)
   245  //sys	Statfs(path string, stat *Statfs_t) (err error)
   246  //sys	Symlink(path string, link string) (err error)
   247  //sys	Sync() (err error)
   248  //sys	Truncate(path string, length int64) (err error)
   249  //sys	Umask(newmask int) (oldmask int)
   250  //sys	Undelete(path string) (err error)
   251  //sys	Unlink(path string) (err error)
   252  //sys	Unmount(path string, flags int) (err error)
   253  //sys	write(fd int, p []byte) (n int, err error)
   254  //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   255  //sys   munmap(addr uintptr, length uintptr) (err error)
   256  //sys	readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
   257  //sys	writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
   258  //sys	accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
   259  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flag int) (err error)
   260  //sys	getcwd(buf []byte) (n int, err error) = SYS___GETCWD
   261  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
   262  

View as plain text