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 unix 14 15 import ( 16 "syscall" 17 "unsafe" 18 ) 19 20 // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets. 21 type SockaddrDatalink struct { 22 Len uint8 23 Family uint8 24 Index uint16 25 Type uint8 26 Nlen uint8 27 Alen uint8 28 Slen uint8 29 Data [12]int8 30 raw RawSockaddrDatalink 31 } 32 33 func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { 34 return nil, EAFNOSUPPORT 35 } 36 37 func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno) 38 39 func sysctlNodes(mib []_C_int) (nodes []Sysctlnode, err error) { 40 var olen uintptr 41 42 // Get a list of all sysctl nodes below the given MIB by performing 43 // a sysctl for the given MIB with CTL_QUERY appended. 44 mib = append(mib, CTL_QUERY) 45 qnode := Sysctlnode{Flags: SYSCTL_VERS_1} 46 qp := (*byte)(unsafe.Pointer(&qnode)) 47 sz := unsafe.Sizeof(qnode) 48 if err = sysctl(mib, nil, &olen, qp, sz); err != nil { 49 return nil, err 50 } 51 52 // Now that we know the size, get the actual nodes. 53 nodes = make([]Sysctlnode, olen/sz) 54 np := (*byte)(unsafe.Pointer(&nodes[0])) 55 if err = sysctl(mib, np, &olen, qp, sz); err != nil { 56 return nil, err 57 } 58 59 return nodes, nil 60 } 61 62 func nametomib(name string) (mib []_C_int, err error) { 63 // Split name into components. 64 var parts []string 65 last := 0 66 for i := 0; i < len(name); i++ { 67 if name[i] == '.' { 68 parts = append(parts, name[last:i]) 69 last = i + 1 70 } 71 } 72 parts = append(parts, name[last:]) 73 74 // Discover the nodes and construct the MIB OID. 75 for partno, part := range parts { 76 nodes, err := sysctlNodes(mib) 77 if err != nil { 78 return nil, err 79 } 80 for _, node := range nodes { 81 n := make([]byte, 0) 82 for i := range node.Name { 83 if node.Name[i] != 0 { 84 n = append(n, byte(node.Name[i])) 85 } 86 } 87 if string(n) == part { 88 mib = append(mib, _C_int(node.Num)) 89 break 90 } 91 } 92 if len(mib) != partno+1 { 93 return nil, EINVAL 94 } 95 } 96 97 return mib, nil 98 } 99 100 func direntIno(buf []byte) (uint64, bool) { 101 return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno)) 102 } 103 104 func direntReclen(buf []byte) (uint64, bool) { 105 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen)) 106 } 107 108 func direntNamlen(buf []byte) (uint64, bool) { 109 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen)) 110 } 111 112 func SysctlUvmexp(name string) (*Uvmexp, error) { 113 mib, err := sysctlmib(name) 114 if err != nil { 115 return nil, err 116 } 117 118 n := uintptr(SizeofUvmexp) 119 var u Uvmexp 120 if err := sysctl(mib, (*byte)(unsafe.Pointer(&u)), &n, nil, 0); err != nil { 121 return nil, err 122 } 123 return &u, nil 124 } 125 126 func Pipe(p []int) (err error) { 127 return Pipe2(p, 0) 128 } 129 130 //sysnb pipe2(p *[2]_C_int, flags int) (err error) 131 132 func Pipe2(p []int, flags int) error { 133 if len(p) != 2 { 134 return EINVAL 135 } 136 var pp [2]_C_int 137 err := pipe2(&pp, flags) 138 if err == nil { 139 p[0] = int(pp[0]) 140 p[1] = int(pp[1]) 141 } 142 return err 143 } 144 145 //sys Getdents(fd int, buf []byte) (n int, err error) 146 147 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) { 148 n, err = Getdents(fd, buf) 149 if err != nil || basep == nil { 150 return 151 } 152 153 var off int64 154 off, err = Seek(fd, 0, 1 /* SEEK_CUR */) 155 if err != nil { 156 *basep = ^uintptr(0) 157 return 158 } 159 *basep = uintptr(off) 160 if unsafe.Sizeof(*basep) == 8 { 161 return 162 } 163 if off>>32 != 0 { 164 // We can't stuff the offset back into a uintptr, so any 165 // future calls would be suspect. Generate an error. 166 // EIO is allowed by getdirentries. 167 err = EIO 168 } 169 return 170 } 171 172 //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD 173 174 // TODO 175 func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { 176 return -1, ENOSYS 177 } 178 179 //sys ioctl(fd int, req uint, arg uintptr) (err error) 180 //sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL 181 182 //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL 183 184 func IoctlGetPtmget(fd int, req uint) (*Ptmget, error) { 185 var value Ptmget 186 err := ioctlPtr(fd, req, unsafe.Pointer(&value)) 187 return &value, err 188 } 189 190 func Uname(uname *Utsname) error { 191 mib := []_C_int{CTL_KERN, KERN_OSTYPE} 192 n := unsafe.Sizeof(uname.Sysname) 193 if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil { 194 return err 195 } 196 197 mib = []_C_int{CTL_KERN, KERN_HOSTNAME} 198 n = unsafe.Sizeof(uname.Nodename) 199 if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil { 200 return err 201 } 202 203 mib = []_C_int{CTL_KERN, KERN_OSRELEASE} 204 n = unsafe.Sizeof(uname.Release) 205 if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil { 206 return err 207 } 208 209 mib = []_C_int{CTL_KERN, KERN_VERSION} 210 n = unsafe.Sizeof(uname.Version) 211 if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil { 212 return err 213 } 214 215 // The version might have newlines or tabs in it, convert them to 216 // spaces. 217 for i, b := range uname.Version { 218 if b == '\n' || b == '\t' { 219 if i == len(uname.Version)-1 { 220 uname.Version[i] = 0 221 } else { 222 uname.Version[i] = ' ' 223 } 224 } 225 } 226 227 mib = []_C_int{CTL_HW, HW_MACHINE} 228 n = unsafe.Sizeof(uname.Machine) 229 if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil { 230 return err 231 } 232 233 return nil 234 } 235 236 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { 237 if raceenabled { 238 raceReleaseMerge(unsafe.Pointer(&ioSync)) 239 } 240 return sendfile(outfd, infd, offset, count) 241 } 242 243 func Fstatvfs(fd int, buf *Statvfs_t) (err error) { 244 return Fstatvfs1(fd, buf, ST_WAIT) 245 } 246 247 func Statvfs(path string, buf *Statvfs_t) (err error) { 248 return Statvfs1(path, buf, ST_WAIT) 249 } 250 251 /* 252 * Exposed directly 253 */ 254 //sys Access(path string, mode uint32) (err error) 255 //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error) 256 //sys Chdir(path string) (err error) 257 //sys Chflags(path string, flags int) (err error) 258 //sys Chmod(path string, mode uint32) (err error) 259 //sys Chown(path string, uid int, gid int) (err error) 260 //sys Chroot(path string) (err error) 261 //sys ClockGettime(clockid int32, time *Timespec) (err error) 262 //sys Close(fd int) (err error) 263 //sys Dup(fd int) (nfd int, err error) 264 //sys Dup2(from int, to int) (err error) 265 //sys Dup3(from int, to int, flags int) (err error) 266 //sys Exit(code int) 267 //sys ExtattrGetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 268 //sys ExtattrSetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 269 //sys ExtattrDeleteFd(fd int, attrnamespace int, attrname string) (err error) 270 //sys ExtattrListFd(fd int, attrnamespace int, data uintptr, nbytes int) (ret int, err error) 271 //sys ExtattrGetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 272 //sys ExtattrSetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 273 //sys ExtattrDeleteFile(file string, attrnamespace int, attrname string) (err error) 274 //sys ExtattrListFile(file string, attrnamespace int, data uintptr, nbytes int) (ret int, err error) 275 //sys ExtattrGetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 276 //sys ExtattrSetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 277 //sys ExtattrDeleteLink(link string, attrnamespace int, attrname string) (err error) 278 //sys ExtattrListLink(link string, attrnamespace int, data uintptr, nbytes int) (ret int, err error) 279 //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error) 280 //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_POSIX_FADVISE 281 //sys Fchdir(fd int) (err error) 282 //sys Fchflags(fd int, flags int) (err error) 283 //sys Fchmod(fd int, mode uint32) (err error) 284 //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) 285 //sys Fchown(fd int, uid int, gid int) (err error) 286 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) 287 //sys Flock(fd int, how int) (err error) 288 //sys Fpathconf(fd int, name int) (val int, err error) 289 //sys Fstat(fd int, stat *Stat_t) (err error) 290 //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error) 291 //sys Fstatvfs1(fd int, buf *Statvfs_t, flags int) (err error) = SYS_FSTATVFS1 292 //sys Fsync(fd int) (err error) 293 //sys Ftruncate(fd int, length int64) (err error) 294 //sysnb Getegid() (egid int) 295 //sysnb Geteuid() (uid int) 296 //sysnb Getgid() (gid int) 297 //sysnb Getpgid(pid int) (pgid int, err error) 298 //sysnb Getpgrp() (pgrp int) 299 //sysnb Getpid() (pid int) 300 //sysnb Getppid() (ppid int) 301 //sys Getpriority(which int, who int) (prio int, err error) 302 //sysnb Getrlimit(which int, lim *Rlimit) (err error) 303 //sysnb Getrusage(who int, rusage *Rusage) (err error) 304 //sysnb Getsid(pid int) (sid int, err error) 305 //sysnb Gettimeofday(tv *Timeval) (err error) 306 //sysnb Getuid() (uid int) 307 //sys Issetugid() (tainted bool) 308 //sys Kill(pid int, signum syscall.Signal) (err error) 309 //sys Kqueue() (fd int, err error) 310 //sys Lchown(path string, uid int, gid int) (err error) 311 //sys Link(path string, link string) (err error) 312 //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error) 313 //sys Listen(s int, backlog int) (err error) 314 //sys Lstat(path string, stat *Stat_t) (err error) 315 //sys Mkdir(path string, mode uint32) (err error) 316 //sys Mkdirat(dirfd int, path string, mode uint32) (err error) 317 //sys Mkfifo(path string, mode uint32) (err error) 318 //sys Mkfifoat(dirfd int, path string, mode uint32) (err error) 319 //sys Mknod(path string, mode uint32, dev int) (err error) 320 //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error) 321 //sys Nanosleep(time *Timespec, leftover *Timespec) (err error) 322 //sys Open(path string, mode int, perm uint32) (fd int, err error) 323 //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error) 324 //sys Pathconf(path string, name int) (val int, err error) 325 //sys pread(fd int, p []byte, offset int64) (n int, err error) 326 //sys pwrite(fd int, p []byte, offset int64) (n int, err error) 327 //sys read(fd int, p []byte) (n int, err error) 328 //sys Readlink(path string, buf []byte) (n int, err error) 329 //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error) 330 //sys Rename(from string, to string) (err error) 331 //sys Renameat(fromfd int, from string, tofd int, to string) (err error) 332 //sys Revoke(path string) (err error) 333 //sys Rmdir(path string) (err error) 334 //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK 335 //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) 336 //sysnb Setegid(egid int) (err error) 337 //sysnb Seteuid(euid int) (err error) 338 //sysnb Setgid(gid int) (err error) 339 //sysnb Setpgid(pid int, pgid int) (err error) 340 //sys Setpriority(which int, who int, prio int) (err error) 341 //sysnb Setregid(rgid int, egid int) (err error) 342 //sysnb Setreuid(ruid int, euid int) (err error) 343 //sysnb Setsid() (pid int, err error) 344 //sysnb Settimeofday(tp *Timeval) (err error) 345 //sysnb Setuid(uid int) (err error) 346 //sys Stat(path string, stat *Stat_t) (err error) 347 //sys Statvfs1(path string, buf *Statvfs_t, flags int) (err error) = SYS_STATVFS1 348 //sys Symlink(path string, link string) (err error) 349 //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error) 350 //sys Sync() (err error) 351 //sys Truncate(path string, length int64) (err error) 352 //sys Umask(newmask int) (oldmask int) 353 //sys Unlink(path string) (err error) 354 //sys Unlinkat(dirfd int, path string, flags int) (err error) 355 //sys Unmount(path string, flags int) (err error) 356 //sys write(fd int, p []byte) (n int, err error) 357 //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) 358 //sys munmap(addr uintptr, length uintptr) (err error) 359 //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) 360 361 const ( 362 mremapFixed = MAP_FIXED 363 mremapDontunmap = 0 364 mremapMaymove = 0 365 ) 366 367 //sys mremapNetBSD(oldp uintptr, oldsize uintptr, newp uintptr, newsize uintptr, flags int) (xaddr uintptr, err error) = SYS_MREMAP 368 369 func mremap(oldaddr uintptr, oldlength uintptr, newlength uintptr, flags int, newaddr uintptr) (uintptr, error) { 370 return mremapNetBSD(oldaddr, oldlength, newaddr, newlength, flags) 371 } 372