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

View as plain text