Home | History | Annotate | Download | only in 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 // OpenBSD 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   [24]int8
     26 	raw    RawSockaddrDatalink
     27 }
     28 
     29 func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno)
     30 
     31 func nametomib(name string) (mib []_C_int, err error) {
     32 
     33 	// Perform lookup via a binary search
     34 	left := 0
     35 	right := len(sysctlMib) - 1
     36 	for {
     37 		idx := left + (right-left)/2
     38 		switch {
     39 		case name == sysctlMib[idx].ctlname:
     40 			return sysctlMib[idx].ctloid, nil
     41 		case name > sysctlMib[idx].ctlname:
     42 			left = idx + 1
     43 		default:
     44 			right = idx - 1
     45 		}
     46 		if left > right {
     47 			break
     48 		}
     49 	}
     50 	return nil, EINVAL
     51 }
     52 
     53 // ParseDirent parses up to max directory entries in buf,
     54 // appending the names to names. It returns the number
     55 // bytes consumed from buf, the number of entries added
     56 // to names, and the new names slice.
     57 func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
     58 	origlen := len(buf)
     59 	for max != 0 && len(buf) > 0 {
     60 		dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
     61 		if dirent.Reclen == 0 {
     62 			buf = nil
     63 			break
     64 		}
     65 		buf = buf[dirent.Reclen:]
     66 		if dirent.Fileno == 0 { // File absent in directory.
     67 			continue
     68 		}
     69 		bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
     70 		var name = string(bytes[0:dirent.Namlen])
     71 		if name == "." || name == ".." { // Useless names
     72 			continue
     73 		}
     74 		max--
     75 		count++
     76 		names = append(names, name)
     77 	}
     78 	return origlen - len(buf), count, names
     79 }
     80 
     81 //sysnb pipe(p *[2]_C_int) (err error)
     82 func Pipe(p []int) (err error) {
     83 	if len(p) != 2 {
     84 		return EINVAL
     85 	}
     86 	var pp [2]_C_int
     87 	err = pipe(&pp)
     88 	p[0] = int(pp[0])
     89 	p[1] = int(pp[1])
     90 	return
     91 }
     92 
     93 //sys getdents(fd int, buf []byte) (n int, err error)
     94 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
     95 	return getdents(fd, buf)
     96 }
     97 
     98 // TODO
     99 func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
    100 	return -1, ENOSYS
    101 }
    102 
    103 func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
    104 	var _p0 unsafe.Pointer
    105 	var bufsize uintptr
    106 	if len(buf) > 0 {
    107 		_p0 = unsafe.Pointer(&buf[0])
    108 		bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
    109 	}
    110 	r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
    111 	n = int(r0)
    112 	if e1 != 0 {
    113 		err = e1
    114 	}
    115 	return
    116 }
    117 
    118 /*
    119  * Exposed directly
    120  */
    121 //sys	Access(path string, mode uint32) (err error)
    122 //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
    123 //sys	Chdir(path string) (err error)
    124 //sys	Chflags(path string, flags int) (err error)
    125 //sys	Chmod(path string, mode uint32) (err error)
    126 //sys	Chown(path string, uid int, gid int) (err error)
    127 //sys	Chroot(path string) (err error)
    128 //sys	Close(fd int) (err error)
    129 //sys	Dup(fd int) (nfd int, err error)
    130 //sys	Dup2(from int, to int) (err error)
    131 //sys	Exit(code int)
    132 //sys	Fchdir(fd int) (err error)
    133 //sys	Fchflags(fd int, flags int) (err error)
    134 //sys	Fchmod(fd int, mode uint32) (err error)
    135 //sys	Fchown(fd int, uid int, gid int) (err error)
    136 //sys	Flock(fd int, how int) (err error)
    137 //sys	Fpathconf(fd int, name int) (val int, err error)
    138 //sys	Fstat(fd int, stat *Stat_t) (err error)
    139 //sys	Fstatfs(fd int, stat *Statfs_t) (err error)
    140 //sys	Fsync(fd int) (err error)
    141 //sys	Ftruncate(fd int, length int64) (err error)
    142 //sysnb	Getegid() (egid int)
    143 //sysnb	Geteuid() (uid int)
    144 //sysnb	Getgid() (gid int)
    145 //sysnb	Getpgid(pid int) (pgid int, err error)
    146 //sysnb	Getpgrp() (pgrp int)
    147 //sysnb	Getpid() (pid int)
    148 //sysnb	Getppid() (ppid int)
    149 //sys	Getpriority(which int, who int) (prio int, err error)
    150 //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
    151 //sysnb	Getrusage(who int, rusage *Rusage) (err error)
    152 //sysnb	Getsid(pid int) (sid int, err error)
    153 //sysnb	Gettimeofday(tv *Timeval) (err error)
    154 //sysnb	Getuid() (uid int)
    155 //sys	Issetugid() (tainted bool)
    156 //sys	Kill(pid int, signum Signal) (err error)
    157 //sys	Kqueue() (fd int, err error)
    158 //sys	Lchown(path string, uid int, gid int) (err error)
    159 //sys	Link(path string, link string) (err error)
    160 //sys	Listen(s int, backlog int) (err error)
    161 //sys	Lstat(path string, stat *Stat_t) (err error)
    162 //sys	Mkdir(path string, mode uint32) (err error)
    163 //sys	Mkfifo(path string, mode uint32) (err error)
    164 //sys	Mknod(path string, mode uint32, dev int) (err error)
    165 //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
    166 //sys	Open(path string, mode int, perm uint32) (fd int, err error)
    167 //sys	Pathconf(path string, name int) (val int, err error)
    168 //sys	Pread(fd int, p []byte, offset int64) (n int, err error)
    169 //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
    170 //sys	read(fd int, p []byte) (n int, err error)
    171 //sys	Readlink(path string, buf []byte) (n int, err error)
    172 //sys	Rename(from string, to string) (err error)
    173 //sys	Revoke(path string) (err error)
    174 //sys	Rmdir(path string) (err error)
    175 //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
    176 //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
    177 //sysnb	Setegid(egid int) (err error)
    178 //sysnb	Seteuid(euid int) (err error)
    179 //sysnb	Setgid(gid int) (err error)
    180 //sys	Setlogin(name string) (err error)
    181 //sysnb	Setpgid(pid int, pgid int) (err error)
    182 //sys	Setpriority(which int, who int, prio int) (err error)
    183 //sysnb	Setregid(rgid int, egid int) (err error)
    184 //sysnb	Setreuid(ruid int, euid int) (err error)
    185 //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
    186 //sysnb	Setsid() (pid int, err error)
    187 //sysnb	Settimeofday(tp *Timeval) (err error)
    188 //sysnb	Setuid(uid int) (err error)
    189 //sys	Stat(path string, stat *Stat_t) (err error)
    190 //sys	Statfs(path string, stat *Statfs_t) (err error)
    191 //sys	Symlink(path string, link string) (err error)
    192 //sys	Sync() (err error)
    193 //sys	Truncate(path string, length int64) (err error)
    194 //sys	Umask(newmask int) (oldmask int)
    195 //sys	Unlink(path string) (err error)
    196 //sys	Unmount(path string, flags int) (err error)
    197 //sys	write(fd int, p []byte) (n int, err error)
    198 //sys	mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
    199 //sys	munmap(addr uintptr, length uintptr) (err error)
    200 //sys	readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
    201 //sys	writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
    202 
    203 /*
    204  * Unimplemented
    205  */
    206 // __getcwd
    207 // __semctl
    208 // __syscall
    209 // __sysctl
    210 // adjfreq
    211 // break
    212 // clock_getres
    213 // clock_gettime
    214 // clock_settime
    215 // closefrom
    216 // execve
    217 // faccessat
    218 // fchmodat
    219 // fchownat
    220 // fcntl
    221 // fhopen
    222 // fhstat
    223 // fhstatfs
    224 // fork
    225 // fstatat
    226 // futimens
    227 // getfh
    228 // getgid
    229 // getitimer
    230 // getlogin
    231 // getresgid
    232 // getresuid
    233 // getrtable
    234 // getthrid
    235 // ioctl
    236 // ktrace
    237 // lfs_bmapv
    238 // lfs_markv
    239 // lfs_segclean
    240 // lfs_segwait
    241 // linkat
    242 // mincore
    243 // minherit
    244 // mkdirat
    245 // mkfifoat
    246 // mknodat
    247 // mlock
    248 // mlockall
    249 // mount
    250 // mquery
    251 // msgctl
    252 // msgget
    253 // msgrcv
    254 // msgsnd
    255 // munlock
    256 // munlockall
    257 // nfssvc
    258 // nnpfspioctl
    259 // openat
    260 // poll
    261 // preadv
    262 // profil
    263 // pwritev
    264 // quotactl
    265 // readlinkat
    266 // readv
    267 // reboot
    268 // renameat
    269 // rfork
    270 // sched_yield
    271 // semget
    272 // semop
    273 // setgroups
    274 // setitimer
    275 // setresgid
    276 // setresuid
    277 // setrtable
    278 // setsockopt
    279 // shmat
    280 // shmctl
    281 // shmdt
    282 // shmget
    283 // sigaction
    284 // sigaltstack
    285 // sigpending
    286 // sigprocmask
    287 // sigreturn
    288 // sigsuspend
    289 // symlinkat
    290 // sysarch
    291 // syscall
    292 // threxit
    293 // thrsigdivert
    294 // thrsleep
    295 // thrwakeup
    296 // unlinkat
    297 // utimensat
    298 // vfork
    299 // writev
    300