You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
9.9 KiB

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