syscall_openbsd.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
  30. return nil, EAFNOSUPPORT
  31. }
  32. func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
  33. func nametomib(name string) (mib []_C_int, err error) {
  34. i := sort.Search(len(sysctlMib), func(i int) bool {
  35. return sysctlMib[i].ctlname >= name
  36. })
  37. if i < len(sysctlMib) && sysctlMib[i].ctlname == name {
  38. return sysctlMib[i].ctloid, nil
  39. }
  40. return nil, EINVAL
  41. }
  42. func direntIno(buf []byte) (uint64, bool) {
  43. return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
  44. }
  45. func direntReclen(buf []byte) (uint64, bool) {
  46. return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
  47. }
  48. func direntNamlen(buf []byte) (uint64, bool) {
  49. return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
  50. }
  51. func SysctlUvmexp(name string) (*Uvmexp, error) {
  52. mib, err := sysctlmib(name)
  53. if err != nil {
  54. return nil, err
  55. }
  56. n := uintptr(SizeofUvmexp)
  57. var u Uvmexp
  58. if err := sysctl(mib, (*byte)(unsafe.Pointer(&u)), &n, nil, 0); err != nil {
  59. return nil, err
  60. }
  61. if n != SizeofUvmexp {
  62. return nil, EIO
  63. }
  64. return &u, nil
  65. }
  66. func Pipe(p []int) (err error) {
  67. return Pipe2(p, 0)
  68. }
  69. //sysnb pipe2(p *[2]_C_int, flags int) (err error)
  70. func Pipe2(p []int, flags int) error {
  71. if len(p) != 2 {
  72. return EINVAL
  73. }
  74. var pp [2]_C_int
  75. err := pipe2(&pp, flags)
  76. if err == nil {
  77. p[0] = int(pp[0])
  78. p[1] = int(pp[1])
  79. }
  80. return err
  81. }
  82. //sys Getdents(fd int, buf []byte) (n int, err error)
  83. func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
  84. n, err = Getdents(fd, buf)
  85. if err != nil || basep == nil {
  86. return
  87. }
  88. var off int64
  89. off, err = Seek(fd, 0, 1 /* SEEK_CUR */)
  90. if err != nil {
  91. *basep = ^uintptr(0)
  92. return
  93. }
  94. *basep = uintptr(off)
  95. if unsafe.Sizeof(*basep) == 8 {
  96. return
  97. }
  98. if off>>32 != 0 {
  99. // We can't stuff the offset back into a uintptr, so any
  100. // future calls would be suspect. Generate an error.
  101. // EIO was allowed by getdirentries.
  102. err = EIO
  103. }
  104. return
  105. }
  106. //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD
  107. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  108. if raceenabled {
  109. raceReleaseMerge(unsafe.Pointer(&ioSync))
  110. }
  111. return sendfile(outfd, infd, offset, count)
  112. }
  113. // TODO
  114. func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  115. return -1, ENOSYS
  116. }
  117. func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
  118. var bufptr *Statfs_t
  119. var bufsize uintptr
  120. if len(buf) > 0 {
  121. bufptr = &buf[0]
  122. bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
  123. }
  124. return getfsstat(bufptr, bufsize, flags)
  125. }
  126. //sysnb getresuid(ruid *_C_int, euid *_C_int, suid *_C_int)
  127. //sysnb getresgid(rgid *_C_int, egid *_C_int, sgid *_C_int)
  128. func Getresuid() (ruid, euid, suid int) {
  129. var r, e, s _C_int
  130. getresuid(&r, &e, &s)
  131. return int(r), int(e), int(s)
  132. }
  133. func Getresgid() (rgid, egid, sgid int) {
  134. var r, e, s _C_int
  135. getresgid(&r, &e, &s)
  136. return int(r), int(e), int(s)
  137. }
  138. //sys ioctl(fd int, req uint, arg uintptr) (err error)
  139. //sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL
  140. //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
  141. //sys fcntl(fd int, cmd int, arg int) (n int, err error)
  142. //sys fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (n int, err error) = SYS_FCNTL
  143. // FcntlInt performs a fcntl syscall on fd with the provided command and argument.
  144. func FcntlInt(fd uintptr, cmd, arg int) (int, error) {
  145. return fcntl(int(fd), cmd, arg)
  146. }
  147. // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
  148. func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
  149. _, err := fcntlPtr(int(fd), cmd, unsafe.Pointer(lk))
  150. return err
  151. }
  152. //sys ppoll(fds *PollFd, nfds int, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
  153. func Ppoll(fds []PollFd, timeout *Timespec, sigmask *Sigset_t) (n int, err error) {
  154. if len(fds) == 0 {
  155. return ppoll(nil, 0, timeout, sigmask)
  156. }
  157. return ppoll(&fds[0], len(fds), timeout, sigmask)
  158. }
  159. func Uname(uname *Utsname) error {
  160. mib := []_C_int{CTL_KERN, KERN_OSTYPE}
  161. n := unsafe.Sizeof(uname.Sysname)
  162. if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
  163. return err
  164. }
  165. mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
  166. n = unsafe.Sizeof(uname.Nodename)
  167. if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
  168. return err
  169. }
  170. mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
  171. n = unsafe.Sizeof(uname.Release)
  172. if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
  173. return err
  174. }
  175. mib = []_C_int{CTL_KERN, KERN_VERSION}
  176. n = unsafe.Sizeof(uname.Version)
  177. if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
  178. return err
  179. }
  180. // The version might have newlines or tabs in it, convert them to
  181. // spaces.
  182. for i, b := range uname.Version {
  183. if b == '\n' || b == '\t' {
  184. if i == len(uname.Version)-1 {
  185. uname.Version[i] = 0
  186. } else {
  187. uname.Version[i] = ' '
  188. }
  189. }
  190. }
  191. mib = []_C_int{CTL_HW, HW_MACHINE}
  192. n = unsafe.Sizeof(uname.Machine)
  193. if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
  194. return err
  195. }
  196. return nil
  197. }
  198. /*
  199. * Exposed directly
  200. */
  201. //sys Access(path string, mode uint32) (err error)
  202. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  203. //sys Chdir(path string) (err error)
  204. //sys Chflags(path string, flags int) (err error)
  205. //sys Chmod(path string, mode uint32) (err error)
  206. //sys Chown(path string, uid int, gid int) (err error)
  207. //sys Chroot(path string) (err error)
  208. //sys ClockGettime(clockid int32, time *Timespec) (err error)
  209. //sys Close(fd int) (err error)
  210. //sys Dup(fd int) (nfd int, err error)
  211. //sys Dup2(from int, to int) (err error)
  212. //sys Dup3(from int, to int, flags int) (err error)
  213. //sys Exit(code int)
  214. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  215. //sys Fchdir(fd int) (err error)
  216. //sys Fchflags(fd int, flags int) (err error)
  217. //sys Fchmod(fd int, mode uint32) (err error)
  218. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  219. //sys Fchown(fd int, uid int, gid int) (err error)
  220. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  221. //sys Flock(fd int, how int) (err error)
  222. //sys Fpathconf(fd int, name int) (val int, err error)
  223. //sys Fstat(fd int, stat *Stat_t) (err error)
  224. //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
  225. //sys Fstatfs(fd int, stat *Statfs_t) (err error)
  226. //sys Fsync(fd int) (err error)
  227. //sys Ftruncate(fd int, length int64) (err error)
  228. //sysnb Getegid() (egid int)
  229. //sysnb Geteuid() (uid int)
  230. //sysnb Getgid() (gid int)
  231. //sysnb Getpgid(pid int) (pgid int, err error)
  232. //sysnb Getpgrp() (pgrp int)
  233. //sysnb Getpid() (pid int)
  234. //sysnb Getppid() (ppid int)
  235. //sys Getpriority(which int, who int) (prio int, err error)
  236. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  237. //sysnb Getrtable() (rtable int, err error)
  238. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  239. //sysnb Getsid(pid int) (sid int, err error)
  240. //sysnb Gettimeofday(tv *Timeval) (err error)
  241. //sysnb Getuid() (uid int)
  242. //sys Issetugid() (tainted bool)
  243. //sys Kill(pid int, signum syscall.Signal) (err error)
  244. //sys Kqueue() (fd int, err error)
  245. //sys Lchown(path string, uid int, gid int) (err error)
  246. //sys Link(path string, link string) (err error)
  247. //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
  248. //sys Listen(s int, backlog int) (err error)
  249. //sys Lstat(path string, stat *Stat_t) (err error)
  250. //sys Mkdir(path string, mode uint32) (err error)
  251. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  252. //sys Mkfifo(path string, mode uint32) (err error)
  253. //sys Mkfifoat(dirfd int, path string, mode uint32) (err error)
  254. //sys Mknod(path string, mode uint32, dev int) (err error)
  255. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  256. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  257. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  258. //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
  259. //sys Pathconf(path string, name int) (val int, err error)
  260. //sys pread(fd int, p []byte, offset int64) (n int, err error)
  261. //sys pwrite(fd int, p []byte, offset int64) (n int, err error)
  262. //sys read(fd int, p []byte) (n int, err error)
  263. //sys Readlink(path string, buf []byte) (n int, err error)
  264. //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
  265. //sys Rename(from string, to string) (err error)
  266. //sys Renameat(fromfd int, from string, tofd int, to string) (err error)
  267. //sys Revoke(path string) (err error)
  268. //sys Rmdir(path string) (err error)
  269. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
  270. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
  271. //sysnb Setegid(egid int) (err error)
  272. //sysnb Seteuid(euid int) (err error)
  273. //sysnb Setgid(gid int) (err error)
  274. //sys Setlogin(name string) (err error)
  275. //sysnb Setpgid(pid int, pgid int) (err error)
  276. //sys Setpriority(which int, who int, prio int) (err error)
  277. //sysnb Setregid(rgid int, egid int) (err error)
  278. //sysnb Setreuid(ruid int, euid int) (err error)
  279. //sysnb Setresgid(rgid int, egid int, sgid int) (err error)
  280. //sysnb Setresuid(ruid int, euid int, suid int) (err error)
  281. //sysnb Setrtable(rtable int) (err error)
  282. //sysnb Setsid() (pid int, err error)
  283. //sysnb Settimeofday(tp *Timeval) (err error)
  284. //sysnb Setuid(uid int) (err error)
  285. //sys Stat(path string, stat *Stat_t) (err error)
  286. //sys Statfs(path string, stat *Statfs_t) (err error)
  287. //sys Symlink(path string, link string) (err error)
  288. //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
  289. //sys Sync() (err error)
  290. //sys Truncate(path string, length int64) (err error)
  291. //sys Umask(newmask int) (oldmask int)
  292. //sys Unlink(path string) (err error)
  293. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  294. //sys Unmount(path string, flags int) (err error)
  295. //sys write(fd int, p []byte) (n int, err error)
  296. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  297. //sys munmap(addr uintptr, length uintptr) (err error)
  298. //sys getfsstat(stat *Statfs_t, bufsize uintptr, flags int) (n int, err error)
  299. //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
  300. //sys pledge(promises *byte, execpromises *byte) (err error)
  301. //sys unveil(path *byte, flags *byte) (err error)