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

View as plain text