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

View as plain text