syscall_openbsd.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. // OpenBSD 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. "sort"
  14. "syscall"
  15. "unsafe"
  16. )
  17. // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
  18. type SockaddrDatalink struct {
  19. Len uint8
  20. Family uint8
  21. Index uint16
  22. Type uint8
  23. Nlen uint8
  24. Alen uint8
  25. Slen uint8
  26. Data [24]int8
  27. raw RawSockaddrDatalink
  28. }
  29. func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
  30. func nametomib(name string) (mib []_C_int, err error) {
  31. i := sort.Search(len(sysctlMib), func(i int) bool {
  32. return sysctlMib[i].ctlname >= name
  33. })
  34. if i < len(sysctlMib) && sysctlMib[i].ctlname == name {
  35. return sysctlMib[i].ctloid, nil
  36. }
  37. return nil, EINVAL
  38. }
  39. func direntIno(buf []byte) (uint64, bool) {
  40. return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
  41. }
  42. func direntReclen(buf []byte) (uint64, bool) {
  43. return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
  44. }
  45. func direntNamlen(buf []byte) (uint64, bool) {
  46. return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
  47. }
  48. func SysctlUvmexp(name string) (*Uvmexp, error) {
  49. mib, err := sysctlmib(name)
  50. if err != nil {
  51. return nil, err
  52. }
  53. n := uintptr(SizeofUvmexp)
  54. var u Uvmexp
  55. if err := sysctl(mib, (*byte)(unsafe.Pointer(&u)), &n, nil, 0); err != nil {
  56. return nil, err
  57. }
  58. if n != SizeofUvmexp {
  59. return nil, EIO
  60. }
  61. return &u, nil
  62. }
  63. //sysnb pipe(p *[2]_C_int) (err error)
  64. func Pipe(p []int) (err error) {
  65. if len(p) != 2 {
  66. return EINVAL
  67. }
  68. var pp [2]_C_int
  69. err = pipe(&pp)
  70. p[0] = int(pp[0])
  71. p[1] = int(pp[1])
  72. return
  73. }
  74. //sys Getdents(fd int, buf []byte) (n int, err error)
  75. func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
  76. n, err = Getdents(fd, buf)
  77. if err != nil || basep == nil {
  78. return
  79. }
  80. var off int64
  81. off, err = Seek(fd, 0, 1 /* SEEK_CUR */)
  82. if err != nil {
  83. *basep = ^uintptr(0)
  84. return
  85. }
  86. *basep = uintptr(off)
  87. if unsafe.Sizeof(*basep) == 8 {
  88. return
  89. }
  90. if off>>32 != 0 {
  91. // We can't stuff the offset back into a uintptr, so any
  92. // future calls would be suspect. Generate an error.
  93. // EIO was allowed by getdirentries.
  94. err = EIO
  95. }
  96. return
  97. }
  98. const ImplementsGetwd = true
  99. //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD
  100. func Getwd() (string, error) {
  101. var buf [PathMax]byte
  102. _, err := Getcwd(buf[0:])
  103. if err != nil {
  104. return "", err
  105. }
  106. n := clen(buf[:])
  107. if n < 1 {
  108. return "", EINVAL
  109. }
  110. return string(buf[:n]), nil
  111. }
  112. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  113. if raceenabled {
  114. raceReleaseMerge(unsafe.Pointer(&ioSync))
  115. }
  116. return sendfile(outfd, infd, offset, count)
  117. }
  118. // TODO
  119. func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  120. return -1, ENOSYS
  121. }
  122. func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
  123. var _p0 unsafe.Pointer
  124. var bufsize uintptr
  125. if len(buf) > 0 {
  126. _p0 = unsafe.Pointer(&buf[0])
  127. bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
  128. }
  129. r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
  130. n = int(r0)
  131. if e1 != 0 {
  132. err = e1
  133. }
  134. return
  135. }
  136. func setattrlistTimes(path string, times []Timespec, flags int) error {
  137. // used on Darwin for UtimesNano
  138. return ENOSYS
  139. }
  140. //sys ioctl(fd int, req uint, arg uintptr) (err error)
  141. //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
  142. //sys ppoll(fds *PollFd, nfds int, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
  143. func Ppoll(fds []PollFd, timeout *Timespec, sigmask *Sigset_t) (n int, err error) {
  144. if len(fds) == 0 {
  145. return ppoll(nil, 0, timeout, sigmask)
  146. }
  147. return ppoll(&fds[0], len(fds), timeout, sigmask)
  148. }
  149. func Uname(uname *Utsname) error {
  150. mib := []_C_int{CTL_KERN, KERN_OSTYPE}
  151. n := unsafe.Sizeof(uname.Sysname)
  152. if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
  153. return err
  154. }
  155. mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
  156. n = unsafe.Sizeof(uname.Nodename)
  157. if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
  158. return err
  159. }
  160. mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
  161. n = unsafe.Sizeof(uname.Release)
  162. if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
  163. return err
  164. }
  165. mib = []_C_int{CTL_KERN, KERN_VERSION}
  166. n = unsafe.Sizeof(uname.Version)
  167. if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
  168. return err
  169. }
  170. // The version might have newlines or tabs in it, convert them to
  171. // spaces.
  172. for i, b := range uname.Version {
  173. if b == '\n' || b == '\t' {
  174. if i == len(uname.Version)-1 {
  175. uname.Version[i] = 0
  176. } else {
  177. uname.Version[i] = ' '
  178. }
  179. }
  180. }
  181. mib = []_C_int{CTL_HW, HW_MACHINE}
  182. n = unsafe.Sizeof(uname.Machine)
  183. if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
  184. return err
  185. }
  186. return nil
  187. }
  188. /*
  189. * Exposed directly
  190. */
  191. //sys Access(path string, mode uint32) (err error)
  192. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  193. //sys Chdir(path string) (err error)
  194. //sys Chflags(path string, flags int) (err error)
  195. //sys Chmod(path string, mode uint32) (err error)
  196. //sys Chown(path string, uid int, gid int) (err error)
  197. //sys Chroot(path string) (err error)
  198. //sys Close(fd int) (err error)
  199. //sys Dup(fd int) (nfd int, err error)
  200. //sys Dup2(from int, to int) (err error)
  201. //sys Dup3(from int, to int, flags int) (err error)
  202. //sys Exit(code int)
  203. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  204. //sys Fchdir(fd int) (err error)
  205. //sys Fchflags(fd int, flags int) (err error)
  206. //sys Fchmod(fd int, mode uint32) (err error)
  207. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  208. //sys Fchown(fd int, uid int, gid int) (err error)
  209. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  210. //sys Flock(fd int, how int) (err error)
  211. //sys Fpathconf(fd int, name int) (val int, err error)
  212. //sys Fstat(fd int, stat *Stat_t) (err error)
  213. //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
  214. //sys Fstatfs(fd int, stat *Statfs_t) (err error)
  215. //sys Fsync(fd int) (err error)
  216. //sys Ftruncate(fd int, length int64) (err error)
  217. //sysnb Getegid() (egid int)
  218. //sysnb Geteuid() (uid int)
  219. //sysnb Getgid() (gid int)
  220. //sysnb Getpgid(pid int) (pgid int, err error)
  221. //sysnb Getpgrp() (pgrp int)
  222. //sysnb Getpid() (pid int)
  223. //sysnb Getppid() (ppid int)
  224. //sys Getpriority(which int, who int) (prio int, err error)
  225. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  226. //sysnb Getrtable() (rtable int, err error)
  227. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  228. //sysnb Getsid(pid int) (sid int, err error)
  229. //sysnb Gettimeofday(tv *Timeval) (err error)
  230. //sysnb Getuid() (uid int)
  231. //sys Issetugid() (tainted bool)
  232. //sys Kill(pid int, signum syscall.Signal) (err error)
  233. //sys Kqueue() (fd int, err error)
  234. //sys Lchown(path string, uid int, gid int) (err error)
  235. //sys Link(path string, link string) (err error)
  236. //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
  237. //sys Listen(s int, backlog int) (err error)
  238. //sys Lstat(path string, stat *Stat_t) (err error)
  239. //sys Mkdir(path string, mode uint32) (err error)
  240. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  241. //sys Mkfifo(path string, mode uint32) (err error)
  242. //sys Mkfifoat(dirfd int, path string, mode uint32) (err error)
  243. //sys Mknod(path string, mode uint32, dev int) (err error)
  244. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  245. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  246. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  247. //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
  248. //sys Pathconf(path string, name int) (val int, err error)
  249. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  250. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  251. //sys read(fd int, p []byte) (n int, err error)
  252. //sys Readlink(path string, buf []byte) (n int, err error)
  253. //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
  254. //sys Rename(from string, to string) (err error)
  255. //sys Renameat(fromfd int, from string, tofd int, to string) (err error)
  256. //sys Revoke(path string) (err error)
  257. //sys Rmdir(path string) (err error)
  258. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
  259. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
  260. //sysnb Setegid(egid int) (err error)
  261. //sysnb Seteuid(euid int) (err error)
  262. //sysnb Setgid(gid int) (err error)
  263. //sys Setlogin(name string) (err error)
  264. //sysnb Setpgid(pid int, pgid int) (err error)
  265. //sys Setpriority(which int, who int, prio int) (err error)
  266. //sysnb Setregid(rgid int, egid int) (err error)
  267. //sysnb Setreuid(ruid int, euid int) (err error)
  268. //sysnb Setresgid(rgid int, egid int, sgid int) (err error)
  269. //sysnb Setresuid(ruid int, euid int, suid int) (err error)
  270. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  271. //sysnb Setrtable(rtable int) (err error)
  272. //sysnb Setsid() (pid int, err error)
  273. //sysnb Settimeofday(tp *Timeval) (err error)
  274. //sysnb Setuid(uid int) (err error)
  275. //sys Stat(path string, stat *Stat_t) (err error)
  276. //sys Statfs(path string, stat *Statfs_t) (err error)
  277. //sys Symlink(path string, link string) (err error)
  278. //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
  279. //sys Sync() (err error)
  280. //sys Truncate(path string, length int64) (err error)
  281. //sys Umask(newmask int) (oldmask int)
  282. //sys Unlink(path string) (err error)
  283. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  284. //sys Unmount(path string, flags int) (err error)
  285. //sys write(fd int, p []byte) (n int, err error)
  286. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  287. //sys munmap(addr uintptr, length uintptr) (err error)
  288. //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
  289. //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
  290. //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
  291. /*
  292. * Unimplemented
  293. */
  294. // __getcwd
  295. // __semctl
  296. // __syscall
  297. // __sysctl
  298. // adjfreq
  299. // break
  300. // clock_getres
  301. // clock_gettime
  302. // clock_settime
  303. // closefrom
  304. // execve
  305. // fhopen
  306. // fhstat
  307. // fhstatfs
  308. // fork
  309. // futimens
  310. // getfh
  311. // getgid
  312. // getitimer
  313. // getlogin
  314. // getresgid
  315. // getresuid
  316. // getthrid
  317. // ktrace
  318. // lfs_bmapv
  319. // lfs_markv
  320. // lfs_segclean
  321. // lfs_segwait
  322. // mincore
  323. // minherit
  324. // mount
  325. // mquery
  326. // msgctl
  327. // msgget
  328. // msgrcv
  329. // msgsnd
  330. // nfssvc
  331. // nnpfspioctl
  332. // preadv
  333. // profil
  334. // pwritev
  335. // quotactl
  336. // readv
  337. // reboot
  338. // renameat
  339. // rfork
  340. // sched_yield
  341. // semget
  342. // semop
  343. // setgroups
  344. // setitimer
  345. // setsockopt
  346. // shmat
  347. // shmctl
  348. // shmdt
  349. // shmget
  350. // sigaction
  351. // sigaltstack
  352. // sigpending
  353. // sigprocmask
  354. // sigreturn
  355. // sigsuspend
  356. // sysarch
  357. // syscall
  358. // threxit
  359. // thrsigdivert
  360. // thrsleep
  361. // thrwakeup
  362. // vfork
  363. // writev