...
Run Format

Source file src/syscall/syscall_freebsd.go

Documentation: syscall

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

View as plain text