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

View as plain text