Source file src/syscall/syscall_freebsd.go

     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 syscall
    14  
    15  import (
    16  	"sync"
    17  	"unsafe"
    18  )
    19  
    20  const (
    21  	_SYS_FSTAT_FREEBSD12         = 551 // { int fstat(int fd, _Out_ struct stat *sb); }
    22  	_SYS_FSTATAT_FREEBSD12       = 552 // { int fstatat(int fd, _In_z_ char *path, _Out_ struct stat *buf, int flag); }
    23  	_SYS_GETDIRENTRIES_FREEBSD12 = 554 // { ssize_t getdirentries(int fd, _Out_writes_bytes_(count) char *buf, size_t count, _Out_ off_t *basep); }
    24  	_SYS_STATFS_FREEBSD12        = 555 // { int statfs(_In_z_ char *path, _Out_ struct statfs *buf); }
    25  	_SYS_FSTATFS_FREEBSD12       = 556 // { int fstatfs(int fd, _Out_ struct statfs *buf); }
    26  	_SYS_GETFSSTAT_FREEBSD12     = 557 // { int getfsstat(_Out_writes_bytes_opt_(bufsize) struct statfs *buf, long bufsize, int mode); }
    27  	_SYS_MKNODAT_FREEBSD12       = 559 // { int mknodat(int fd, _In_z_ char *path, mode_t mode, dev_t dev); }
    28  )
    29  
    30  // See https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/versions.html.
    31  var (
    32  	osreldateOnce sync.Once
    33  	osreldate     uint32
    34  )
    35  
    36  // INO64_FIRST from /usr/src/lib/libc/sys/compat-ino64.h
    37  const _ino64First = 1200031
    38  
    39  func supportsABI(ver uint32) bool {
    40  	osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") })
    41  	return osreldate >= ver
    42  }
    43  
    44  type SockaddrDatalink struct {
    45  	Len    uint8
    46  	Family uint8
    47  	Index  uint16
    48  	Type   uint8
    49  	Nlen   uint8
    50  	Alen   uint8
    51  	Slen   uint8
    52  	Data   [46]int8
    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  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
    90  }
    91  
    92  func direntNamlen(buf []byte) (uint64, bool) {
    93  	return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
    94  }
    95  
    96  func Pipe(p []int) error {
    97  	return Pipe2(p, 0)
    98  }
    99  
   100  //sysnb pipe2(p *[2]_C_int, flags int) (err error)
   101  
   102  func Pipe2(p []int, flags int) error {
   103  	if len(p) != 2 {
   104  		return EINVAL
   105  	}
   106  	var pp [2]_C_int
   107  	err := pipe2(&pp, flags)
   108  	if err == nil {
   109  		p[0] = int(pp[0])
   110  		p[1] = int(pp[1])
   111  	}
   112  	return err
   113  }
   114  
   115  func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
   116  	var value IPMreqn
   117  	vallen := _Socklen(SizeofIPMreqn)
   118  	errno := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
   119  	return &value, errno
   120  }
   121  
   122  func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
   123  	return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq))
   124  }
   125  
   126  func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) {
   127  	var rsa RawSockaddrAny
   128  	var len _Socklen = SizeofSockaddrAny
   129  	nfd, err = accept4(fd, &rsa, &len, flags)
   130  	if err != nil {
   131  		return
   132  	}
   133  	if len > SizeofSockaddrAny {
   134  		panic("RawSockaddrAny too small")
   135  	}
   136  	sa, err = anyToSockaddr(&rsa)
   137  	if err != nil {
   138  		Close(nfd)
   139  		nfd = 0
   140  	}
   141  	return
   142  }
   143  
   144  func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
   145  	var (
   146  		_p0          unsafe.Pointer
   147  		bufsize      uintptr
   148  		oldBuf       []statfs_freebsd11_t
   149  		needsConvert bool
   150  	)
   151  
   152  	if len(buf) > 0 {
   153  		if supportsABI(_ino64First) {
   154  			_p0 = unsafe.Pointer(&buf[0])
   155  			bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
   156  		} else {
   157  			n := len(buf)
   158  			oldBuf = make([]statfs_freebsd11_t, n)
   159  			_p0 = unsafe.Pointer(&oldBuf[0])
   160  			bufsize = unsafe.Sizeof(statfs_freebsd11_t{}) * uintptr(n)
   161  			needsConvert = true
   162  		}
   163  	}
   164  	var sysno uintptr = SYS_GETFSSTAT
   165  	if supportsABI(_ino64First) {
   166  		sysno = _SYS_GETFSSTAT_FREEBSD12
   167  	}
   168  	r0, _, e1 := Syscall(sysno, uintptr(_p0), bufsize, uintptr(flags))
   169  	n = int(r0)
   170  	if e1 != 0 {
   171  		err = e1
   172  	}
   173  	if e1 == 0 && needsConvert {
   174  		for i := range oldBuf {
   175  			buf[i].convertFrom(&oldBuf[i])
   176  		}
   177  	}
   178  	return
   179  }
   180  
   181  func Stat(path string, st *Stat_t) (err error) {
   182  	var oldStat stat_freebsd11_t
   183  	if supportsABI(_ino64First) {
   184  		return fstatat_freebsd12(_AT_FDCWD, path, st, 0)
   185  	}
   186  	err = stat(path, &oldStat)
   187  	if err != nil {
   188  		return err
   189  	}
   190  
   191  	st.convertFrom(&oldStat)
   192  	return nil
   193  }
   194  
   195  func Lstat(path string, st *Stat_t) (err error) {
   196  	var oldStat stat_freebsd11_t
   197  	if supportsABI(_ino64First) {
   198  		return fstatat_freebsd12(_AT_FDCWD, path, st, _AT_SYMLINK_NOFOLLOW)
   199  	}
   200  	err = lstat(path, &oldStat)
   201  	if err != nil {
   202  		return err
   203  	}
   204  
   205  	st.convertFrom(&oldStat)
   206  	return nil
   207  }
   208  
   209  func Fstat(fd int, st *Stat_t) (err error) {
   210  	var oldStat stat_freebsd11_t
   211  	if supportsABI(_ino64First) {
   212  		return fstat_freebsd12(fd, st)
   213  	}
   214  	err = fstat(fd, &oldStat)
   215  	if err != nil {
   216  		return err
   217  	}
   218  
   219  	st.convertFrom(&oldStat)
   220  	return nil
   221  }
   222  
   223  func Fstatat(fd int, path string, st *Stat_t, flags int) (err error) {
   224  	var oldStat stat_freebsd11_t
   225  	if supportsABI(_ino64First) {
   226  		return fstatat_freebsd12(fd, path, st, flags)
   227  	}
   228  	err = fstatat(fd, path, &oldStat, flags)
   229  	if err != nil {
   230  		return err
   231  	}
   232  
   233  	st.convertFrom(&oldStat)
   234  	return nil
   235  }
   236  
   237  func Statfs(path string, st *Statfs_t) (err error) {
   238  	var oldStatfs statfs_freebsd11_t
   239  	if supportsABI(_ino64First) {
   240  		return statfs_freebsd12(path, st)
   241  	}
   242  	err = statfs(path, &oldStatfs)
   243  	if err != nil {
   244  		return err
   245  	}
   246  
   247  	st.convertFrom(&oldStatfs)
   248  	return nil
   249  }
   250  
   251  func Fstatfs(fd int, st *Statfs_t) (err error) {
   252  	var oldStatfs statfs_freebsd11_t
   253  	if supportsABI(_ino64First) {
   254  		return fstatfs_freebsd12(fd, st)
   255  	}
   256  	err = fstatfs(fd, &oldStatfs)
   257  	if err != nil {
   258  		return err
   259  	}
   260  
   261  	st.convertFrom(&oldStatfs)
   262  	return nil
   263  }
   264  
   265  func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
   266  	if supportsABI(_ino64First) {
   267  		if basep == nil || unsafe.Sizeof(*basep) == 8 {
   268  			return getdirentries_freebsd12(fd, buf, (*uint64)(unsafe.Pointer(basep)))
   269  		}
   270  		// The freebsd12 syscall needs a 64-bit base. On 32-bit machines
   271  		// we can't just use the basep passed in. See #32498.
   272  		var base uint64 = uint64(*basep)
   273  		n, err = getdirentries_freebsd12(fd, buf, &base)
   274  		*basep = uintptr(base)
   275  		if base>>32 != 0 {
   276  			// We can't stuff the base back into a uintptr, so any
   277  			// future calls would be suspect. Generate an error.
   278  			// EIO is allowed by getdirentries.
   279  			err = EIO
   280  		}
   281  		return
   282  	}
   283  
   284  	// The old syscall entries are smaller than the new. Use 1/4 of the original
   285  	// buffer size rounded up to DIRBLKSIZ (see /usr/src/lib/libc/sys/getdirentries.c).
   286  	oldBufLen := roundup(len(buf)/4, _dirblksiz)
   287  	oldBuf := make([]byte, oldBufLen)
   288  	n, err = getdirentries(fd, oldBuf, basep)
   289  	if err == nil && n > 0 {
   290  		n = convertFromDirents11(buf, oldBuf[:n])
   291  	}
   292  	return
   293  }
   294  
   295  func Mknod(path string, mode uint32, dev uint64) (err error) {
   296  	var oldDev int
   297  	if supportsABI(_ino64First) {
   298  		return mknodat_freebsd12(_AT_FDCWD, path, mode, dev)
   299  	}
   300  	oldDev = int(dev)
   301  	return mknod(path, mode, oldDev)
   302  }
   303  
   304  // round x to the nearest multiple of y, larger or equal to x.
   305  //
   306  // from /usr/include/sys/param.h Macros for counting and rounding.
   307  // #define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
   308  func roundup(x, y int) int {
   309  	return ((x + y - 1) / y) * y
   310  }
   311  
   312  func (s *Stat_t) convertFrom(old *stat_freebsd11_t) {
   313  	*s = Stat_t{
   314  		Dev:           uint64(old.Dev),
   315  		Ino:           uint64(old.Ino),
   316  		Nlink:         uint64(old.Nlink),
   317  		Mode:          old.Mode,
   318  		Uid:           old.Uid,
   319  		Gid:           old.Gid,
   320  		Rdev:          uint64(old.Rdev),
   321  		Atimespec:     old.Atimespec,
   322  		Mtimespec:     old.Mtimespec,
   323  		Ctimespec:     old.Ctimespec,
   324  		Birthtimespec: old.Birthtimespec,
   325  		Size:          old.Size,
   326  		Blocks:        old.Blocks,
   327  		Blksize:       old.Blksize,
   328  		Flags:         old.Flags,
   329  		Gen:           uint64(old.Gen),
   330  	}
   331  }
   332  
   333  func (s *Statfs_t) convertFrom(old *statfs_freebsd11_t) {
   334  	*s = Statfs_t{
   335  		Version:     _statfsVersion,
   336  		Type:        old.Type,
   337  		Flags:       old.Flags,
   338  		Bsize:       old.Bsize,
   339  		Iosize:      old.Iosize,
   340  		Blocks:      old.Blocks,
   341  		Bfree:       old.Bfree,
   342  		Bavail:      old.Bavail,
   343  		Files:       old.Files,
   344  		Ffree:       old.Ffree,
   345  		Syncwrites:  old.Syncwrites,
   346  		Asyncwrites: old.Asyncwrites,
   347  		Syncreads:   old.Syncreads,
   348  		Asyncreads:  old.Asyncreads,
   349  		// Spare
   350  		Namemax: old.Namemax,
   351  		Owner:   old.Owner,
   352  		Fsid:    old.Fsid,
   353  		// Charspare
   354  		// Fstypename
   355  		// Mntfromname
   356  		// Mntonname
   357  	}
   358  
   359  	sl := old.Fstypename[:]
   360  	n := clen(*(*[]byte)(unsafe.Pointer(&sl)))
   361  	copy(s.Fstypename[:], old.Fstypename[:n])
   362  
   363  	sl = old.Mntfromname[:]
   364  	n = clen(*(*[]byte)(unsafe.Pointer(&sl)))
   365  	copy(s.Mntfromname[:], old.Mntfromname[:n])
   366  
   367  	sl = old.Mntonname[:]
   368  	n = clen(*(*[]byte)(unsafe.Pointer(&sl)))
   369  	copy(s.Mntonname[:], old.Mntonname[:n])
   370  }
   371  
   372  func convertFromDirents11(buf []byte, old []byte) int {
   373  	const (
   374  		fixedSize    = int(unsafe.Offsetof(Dirent{}.Name))
   375  		oldFixedSize = int(unsafe.Offsetof(dirent_freebsd11{}.Name))
   376  	)
   377  
   378  	dstPos := 0
   379  	srcPos := 0
   380  	for dstPos+fixedSize < len(buf) && srcPos+oldFixedSize < len(old) {
   381  		var dstDirent Dirent
   382  		var srcDirent dirent_freebsd11
   383  
   384  		// If multiple direntries are written, sometimes when we reach the final one,
   385  		// we may have cap of old less than size of dirent_freebsd11.
   386  		copy((*[unsafe.Sizeof(srcDirent)]byte)(unsafe.Pointer(&srcDirent))[:], old[srcPos:])
   387  
   388  		reclen := roundup(fixedSize+int(srcDirent.Namlen)+1, 8)
   389  		if dstPos+reclen > len(buf) {
   390  			break
   391  		}
   392  
   393  		dstDirent.Fileno = uint64(srcDirent.Fileno)
   394  		dstDirent.Off = 0
   395  		dstDirent.Reclen = uint16(reclen)
   396  		dstDirent.Type = srcDirent.Type
   397  		dstDirent.Pad0 = 0
   398  		dstDirent.Namlen = uint16(srcDirent.Namlen)
   399  		dstDirent.Pad1 = 0
   400  
   401  		copy(dstDirent.Name[:], srcDirent.Name[:srcDirent.Namlen])
   402  		copy(buf[dstPos:], (*[unsafe.Sizeof(dstDirent)]byte)(unsafe.Pointer(&dstDirent))[:])
   403  		padding := buf[dstPos+fixedSize+int(dstDirent.Namlen) : dstPos+reclen]
   404  		for i := range padding {
   405  			padding[i] = 0
   406  		}
   407  
   408  		dstPos += int(dstDirent.Reclen)
   409  		srcPos += int(srcDirent.Reclen)
   410  	}
   411  
   412  	return dstPos
   413  }
   414  
   415  /*
   416   * Exposed directly
   417   */
   418  //sys	Access(path string, mode uint32) (err error)
   419  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   420  //sys	Chdir(path string) (err error)
   421  //sys	Chflags(path string, flags int) (err error)
   422  //sys	Chmod(path string, mode uint32) (err error)
   423  //sys	Chown(path string, uid int, gid int) (err error)
   424  //sys	Chroot(path string) (err error)
   425  //sys	Close(fd int) (err error)
   426  //sys	Dup(fd int) (nfd int, err error)
   427  //sys	Dup2(from int, to int) (err error)
   428  //sys	Fchdir(fd int) (err error)
   429  //sys	Fchflags(fd int, flags int) (err error)
   430  //sys	Fchmod(fd int, mode uint32) (err error)
   431  //sys	Fchown(fd int, uid int, gid int) (err error)
   432  //sys	Flock(fd int, how int) (err error)
   433  //sys	Fpathconf(fd int, name int) (val int, err error)
   434  //sys	fstat(fd int, stat *stat_freebsd11_t) (err error)
   435  //sys	fstat_freebsd12(fd int, stat *Stat_t) (err error) = _SYS_FSTAT_FREEBSD12
   436  //sys	fstatat(fd int, path string, stat *stat_freebsd11_t, flags int) (err error)
   437  //sys	fstatat_freebsd12(fd int, path string, stat *Stat_t, flags int) (err error) = _SYS_FSTATAT_FREEBSD12
   438  //sys	fstatfs(fd int, stat *statfs_freebsd11_t) (err error)
   439  //sys	fstatfs_freebsd12(fd int, stat *Statfs_t) (err error) = _SYS_FSTATFS_FREEBSD12
   440  //sys	Fsync(fd int) (err error)
   441  //sys	Ftruncate(fd int, length int64) (err error)
   442  //sys	getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error)
   443  //sys	getdirentries_freebsd12(fd int, buf []byte, basep *uint64) (n int, err error) = _SYS_GETDIRENTRIES_FREEBSD12
   444  //sys	Getdtablesize() (size int)
   445  //sysnb	Getegid() (egid int)
   446  //sysnb	Geteuid() (uid int)
   447  //sysnb	Getgid() (gid int)
   448  //sysnb	Getpgid(pid int) (pgid int, err error)
   449  //sysnb	Getpgrp() (pgrp int)
   450  //sysnb	Getpid() (pid int)
   451  //sysnb	Getppid() (ppid int)
   452  //sys	Getpriority(which int, who int) (prio int, err error)
   453  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   454  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   455  //sysnb	Getsid(pid int) (sid int, err error)
   456  //sysnb	Gettimeofday(tv *Timeval) (err error)
   457  //sysnb	Getuid() (uid int)
   458  //sys	Issetugid() (tainted bool)
   459  //sys	Kill(pid int, signum Signal) (err error)
   460  //sys	Kqueue() (fd int, err error)
   461  //sys	Lchown(path string, uid int, gid int) (err error)
   462  //sys	Link(path string, link string) (err error)
   463  //sys	Listen(s int, backlog int) (err error)
   464  //sys	lstat(path string, stat *stat_freebsd11_t) (err error)
   465  //sys	Mkdir(path string, mode uint32) (err error)
   466  //sys	Mkfifo(path string, mode uint32) (err error)
   467  //sys	mknod(path string, mode uint32, dev int) (err error)
   468  //sys	mknodat_freebsd12(fd int, path string, mode uint32, dev uint64) (err error) = _SYS_MKNODAT_FREEBSD12
   469  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   470  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   471  //sys	Pathconf(path string, name int) (val int, err error)
   472  //sys	Pread(fd int, p []byte, offset int64) (n int, err error)
   473  //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
   474  //sys	read(fd int, p []byte) (n int, err error)
   475  //sys	Readlink(path string, buf []byte) (n int, err error)
   476  //sys	Rename(from string, to string) (err error)
   477  //sys	Revoke(path string) (err error)
   478  //sys	Rmdir(path string) (err error)
   479  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
   480  //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
   481  //sysnb	Setegid(egid int) (err error)
   482  //sysnb	Seteuid(euid int) (err error)
   483  //sysnb	Setgid(gid int) (err error)
   484  //sys	Setlogin(name string) (err error)
   485  //sysnb	Setpgid(pid int, pgid int) (err error)
   486  //sys	Setpriority(which int, who int, prio int) (err error)
   487  //sysnb	Setregid(rgid int, egid int) (err error)
   488  //sysnb	Setreuid(ruid int, euid int) (err error)
   489  //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
   490  //sysnb	Setsid() (pid int, err error)
   491  //sysnb	Settimeofday(tp *Timeval) (err error)
   492  //sysnb	Setuid(uid int) (err error)
   493  //sys	stat(path string, stat *stat_freebsd11_t) (err error)
   494  //sys	statfs(path string, stat *statfs_freebsd11_t) (err error)
   495  //sys	statfs_freebsd12(path string, stat *Statfs_t) (err error) = _SYS_STATFS_FREEBSD12
   496  //sys	Symlink(path string, link string) (err error)
   497  //sys	Sync() (err error)
   498  //sys	Truncate(path string, length int64) (err error)
   499  //sys	Umask(newmask int) (oldmask int)
   500  //sys	Undelete(path string) (err error)
   501  //sys	Unlink(path string) (err error)
   502  //sys	Unmount(path string, flags int) (err error)
   503  //sys	write(fd int, p []byte) (n int, err error)
   504  //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   505  //sys   munmap(addr uintptr, length uintptr) (err error)
   506  //sys	readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
   507  //sys	writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
   508  //sys	accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
   509  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flag int) (err error)
   510  //sys	getcwd(buf []byte) (n int, err error) = SYS___GETCWD
   511  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
   512  

View as plain text