本站源代码
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.

415 lines
11KB

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