syscall_netbsd.go 12 KB

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