Source file src/syscall/syscall_netbsd.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  // NetBSD 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 Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno)
    20  func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
    21  func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
    22  
    23  const (
    24  	_SYS_DUP3         = SYS_DUP3
    25  	_F_DUP2FD_CLOEXEC = 0
    26  )
    27  
    28  type SockaddrDatalink struct {
    29  	Len    uint8
    30  	Family uint8
    31  	Index  uint16
    32  	Type   uint8
    33  	Nlen   uint8
    34  	Alen   uint8
    35  	Slen   uint8
    36  	Data   [12]int8
    37  	raw    RawSockaddrDatalink
    38  }
    39  
    40  func sysctlNodes(mib []_C_int) (nodes []Sysctlnode, err error) {
    41  	var olen uintptr
    42  
    43  	// Get a list of all sysctl nodes below the given MIB by performing
    44  	// a sysctl for the given MIB with CTL_QUERY appended.
    45  	mib = append(mib, CTL_QUERY)
    46  	qnode := Sysctlnode{Flags: SYSCTL_VERS_1}
    47  	qp := (*byte)(unsafe.Pointer(&qnode))
    48  	sz := unsafe.Sizeof(qnode)
    49  	if err = sysctl(mib, nil, &olen, qp, sz); err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	// Now that we know the size, get the actual nodes.
    54  	nodes = make([]Sysctlnode, olen/sz)
    55  	np := (*byte)(unsafe.Pointer(&nodes[0]))
    56  	if err = sysctl(mib, np, &olen, qp, sz); err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return nodes, nil
    61  }
    62  
    63  func nametomib(name string) (mib []_C_int, err error) {
    64  	// Split name into components.
    65  	var parts []string
    66  	last := 0
    67  	for i := 0; i < len(name); i++ {
    68  		if name[i] == '.' {
    69  			parts = append(parts, name[last:i])
    70  			last = i + 1
    71  		}
    72  	}
    73  	parts = append(parts, name[last:])
    74  
    75  	// Discover the nodes and construct the MIB OID.
    76  	for partno, part := range parts {
    77  		nodes, err := sysctlNodes(mib)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  		for _, node := range nodes {
    82  			n := make([]byte, 0)
    83  			for i := range node.Name {
    84  				if node.Name[i] != 0 {
    85  					n = append(n, byte(node.Name[i]))
    86  				}
    87  			}
    88  			if string(n) == part {
    89  				mib = append(mib, _C_int(node.Num))
    90  				break
    91  			}
    92  		}
    93  		if len(mib) != partno+1 {
    94  			return nil, EINVAL
    95  		}
    96  	}
    97  
    98  	return mib, nil
    99  }
   100  
   101  func direntIno(buf []byte) (uint64, bool) {
   102  	return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
   103  }
   104  
   105  func direntReclen(buf []byte) (uint64, bool) {
   106  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
   107  }
   108  
   109  func direntNamlen(buf []byte) (uint64, bool) {
   110  	return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
   111  }
   112  
   113  func Pipe(p []int) (err error) {
   114  	return Pipe2(p, 0)
   115  }
   116  
   117  //sysnb pipe2(p *[2]_C_int, flags int) (err error)
   118  
   119  func Pipe2(p []int, flags int) error {
   120  	if len(p) != 2 {
   121  		return EINVAL
   122  	}
   123  	var pp [2]_C_int
   124  	err := pipe2(&pp, flags)
   125  	if err == nil {
   126  		p[0] = int(pp[0])
   127  		p[1] = int(pp[1])
   128  	}
   129  	return err
   130  }
   131  
   132  //sys paccept(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, sigmask *sigset, flags int) (nfd int, err error)
   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 = paccept(fd, &rsa, &len, nil, 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  //sys getdents(fd int, buf []byte) (n int, err error)
   153  
   154  func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
   155  	return getdents(fd, buf)
   156  }
   157  
   158  // TODO, see golang.org/issue/5847
   159  func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   160  	return -1, ENOSYS
   161  }
   162  
   163  /*
   164   * Exposed directly
   165   */
   166  //sys	Access(path string, mode uint32) (err error)
   167  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   168  //sys	Chdir(path string) (err error)
   169  //sys	Chflags(path string, flags int) (err error)
   170  //sys	Chmod(path string, mode uint32) (err error)
   171  //sys	Chown(path string, uid int, gid int) (err error)
   172  //sys	Chroot(path string) (err error)
   173  //sys	Close(fd int) (err error)
   174  //sys	Dup(fd int) (nfd int, err error)
   175  //sys	Dup2(from int, to int) (err error)
   176  //sys	Fchdir(fd int) (err error)
   177  //sys	Fchflags(fd int, flags int) (err error)
   178  //sys	Fchmod(fd int, mode uint32) (err error)
   179  //sys	Fchown(fd int, uid int, gid int) (err error)
   180  //sys	Flock(fd int, how int) (err error)
   181  //sys	Fpathconf(fd int, name int) (val int, err error)
   182  //sys	Fstat(fd int, stat *Stat_t) (err error)
   183  //sys	Fsync(fd int) (err error)
   184  //sys	Ftruncate(fd int, length int64) (err error)
   185  //sysnb	Getegid() (egid int)
   186  //sysnb	Geteuid() (uid int)
   187  //sysnb	Getgid() (gid int)
   188  //sysnb	Getpgid(pid int) (pgid int, err error)
   189  //sysnb	Getpgrp() (pgrp int)
   190  //sysnb	Getpid() (pid int)
   191  //sysnb	Getppid() (ppid int)
   192  //sys	Getpriority(which int, who int) (prio int, err error)
   193  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   194  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   195  //sysnb	Getsid(pid int) (sid int, err error)
   196  //sysnb	Gettimeofday(tv *Timeval) (err error)
   197  //sysnb	Getuid() (uid int)
   198  //sys	Issetugid() (tainted bool)
   199  //sys	Kill(pid int, signum Signal) (err error)
   200  //sys	Kqueue() (fd int, err error)
   201  //sys	Lchown(path string, uid int, gid int) (err error)
   202  //sys	Link(path string, link string) (err error)
   203  //sys	Listen(s int, backlog int) (err error)
   204  //sys	Lstat(path string, stat *Stat_t) (err error)
   205  //sys	Mkdir(path string, mode uint32) (err error)
   206  //sys	Mkfifo(path string, mode uint32) (err error)
   207  //sys	Mknod(path string, mode uint32, dev int) (err error)
   208  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   209  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   210  //sys	Pathconf(path string, name int) (val int, err error)
   211  //sys	pread(fd int, p []byte, offset int64) (n int, err error)
   212  //sys	pwrite(fd int, p []byte, offset int64) (n int, err error)
   213  //sys	read(fd int, p []byte) (n int, err error)
   214  //sys	Readlink(path string, buf []byte) (n int, err error)
   215  //sys	Rename(from string, to string) (err error)
   216  //sys	Revoke(path string) (err error)
   217  //sys	Rmdir(path string) (err error)
   218  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
   219  //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
   220  //sysnb	Setegid(egid int) (err error)
   221  //sysnb	Seteuid(euid int) (err error)
   222  //sysnb	Setgid(gid int) (err error)
   223  //sysnb	Setpgid(pid int, pgid int) (err error)
   224  //sys	Setpriority(which int, who int, prio int) (err error)
   225  //sysnb	Setregid(rgid int, egid int) (err error)
   226  //sysnb	Setreuid(ruid int, euid int) (err error)
   227  //sysnb	setrlimit(which int, lim *Rlimit) (err error)
   228  //sysnb	Setsid() (pid int, err error)
   229  //sysnb	Settimeofday(tp *Timeval) (err error)
   230  //sysnb	Setuid(uid int) (err error)
   231  //sys	Stat(path string, stat *Stat_t) (err error)
   232  //sys	Symlink(path string, link string) (err error)
   233  //sys	Sync() (err error)
   234  //sys	Truncate(path string, length int64) (err error)
   235  //sys	Umask(newmask int) (oldmask int)
   236  //sys	Unlink(path string) (err error)
   237  //sys	Unmount(path string, flags int) (err error)
   238  //sys	write(fd int, p []byte) (n int, err error)
   239  //sys	mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   240  //sys	munmap(addr uintptr, length uintptr) (err error)
   241  //sys	readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
   242  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flag int) (err error)
   243  //sys	getcwd(buf []byte) (n int, err error) = SYS___GETCWD
   244  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
   245  

View as plain text