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

621 lines
16KB

  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. "runtime"
  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 [12]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 sysctlNodes(mib []_C_int) (nodes []Sysctlnode, err error) {
  32. var olen uintptr
  33. // Get a list of all sysctl nodes below the given MIB by performing
  34. // a sysctl for the given MIB with CTL_QUERY appended.
  35. mib = append(mib, CTL_QUERY)
  36. qnode := Sysctlnode{Flags: SYSCTL_VERS_1}
  37. qp := (*byte)(unsafe.Pointer(&qnode))
  38. sz := unsafe.Sizeof(qnode)
  39. if err = sysctl(mib, nil, &olen, qp, sz); err != nil {
  40. return nil, err
  41. }
  42. // Now that we know the size, get the actual nodes.
  43. nodes = make([]Sysctlnode, olen/sz)
  44. np := (*byte)(unsafe.Pointer(&nodes[0]))
  45. if err = sysctl(mib, np, &olen, qp, sz); err != nil {
  46. return nil, err
  47. }
  48. return nodes, nil
  49. }
  50. func nametomib(name string) (mib []_C_int, err error) {
  51. // Split name into components.
  52. var parts []string
  53. last := 0
  54. for i := 0; i < len(name); i++ {
  55. if name[i] == '.' {
  56. parts = append(parts, name[last:i])
  57. last = i + 1
  58. }
  59. }
  60. parts = append(parts, name[last:])
  61. // Discover the nodes and construct the MIB OID.
  62. for partno, part := range parts {
  63. nodes, err := sysctlNodes(mib)
  64. if err != nil {
  65. return nil, err
  66. }
  67. for _, node := range nodes {
  68. n := make([]byte, 0)
  69. for i := range node.Name {
  70. if node.Name[i] != 0 {
  71. n = append(n, byte(node.Name[i]))
  72. }
  73. }
  74. if string(n) == part {
  75. mib = append(mib, _C_int(node.Num))
  76. break
  77. }
  78. }
  79. if len(mib) != partno+1 {
  80. return nil, EINVAL
  81. }
  82. }
  83. return mib, nil
  84. }
  85. func direntIno(buf []byte) (uint64, bool) {
  86. return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
  87. }
  88. func direntReclen(buf []byte) (uint64, bool) {
  89. return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
  90. }
  91. func direntNamlen(buf []byte) (uint64, bool) {
  92. return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
  93. }
  94. func SysctlClockinfo(name string) (*Clockinfo, error) {
  95. mib, err := sysctlmib(name)
  96. if err != nil {
  97. return nil, err
  98. }
  99. n := uintptr(SizeofClockinfo)
  100. var ci Clockinfo
  101. if err := sysctl(mib, (*byte)(unsafe.Pointer(&ci)), &n, nil, 0); err != nil {
  102. return nil, err
  103. }
  104. if n != SizeofClockinfo {
  105. return nil, EIO
  106. }
  107. return &ci, nil
  108. }
  109. //sysnb pipe() (fd1 int, fd2 int, err error)
  110. func Pipe(p []int) (err error) {
  111. if len(p) != 2 {
  112. return EINVAL
  113. }
  114. p[0], p[1], err = pipe()
  115. return
  116. }
  117. //sys Getdents(fd int, buf []byte) (n int, err error)
  118. func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
  119. n, err = Getdents(fd, buf)
  120. if err != nil || basep == nil {
  121. return
  122. }
  123. var off int64
  124. off, err = Seek(fd, 0, 1 /* SEEK_CUR */)
  125. if err != nil {
  126. *basep = ^uintptr(0)
  127. return
  128. }
  129. *basep = uintptr(off)
  130. if unsafe.Sizeof(*basep) == 8 {
  131. return
  132. }
  133. if off>>32 != 0 {
  134. // We can't stuff the offset back into a uintptr, so any
  135. // future calls would be suspect. Generate an error.
  136. // EIO is allowed by getdirentries.
  137. err = EIO
  138. }
  139. return
  140. }
  141. const ImplementsGetwd = true
  142. //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD
  143. func Getwd() (string, error) {
  144. var buf [PathMax]byte
  145. _, err := Getcwd(buf[0:])
  146. if err != nil {
  147. return "", err
  148. }
  149. n := clen(buf[:])
  150. if n < 1 {
  151. return "", EINVAL
  152. }
  153. return string(buf[:n]), nil
  154. }
  155. // TODO
  156. func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  157. return -1, ENOSYS
  158. }
  159. func setattrlistTimes(path string, times []Timespec, flags int) error {
  160. // used on Darwin for UtimesNano
  161. return ENOSYS
  162. }
  163. //sys ioctl(fd int, req uint, arg uintptr) (err error)
  164. func IoctlGetPtmget(fd int, req uint) (*Ptmget, error) {
  165. var value Ptmget
  166. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  167. runtime.KeepAlive(value)
  168. return &value, err
  169. }
  170. func Uname(uname *Utsname) error {
  171. mib := []_C_int{CTL_KERN, KERN_OSTYPE}
  172. n := unsafe.Sizeof(uname.Sysname)
  173. if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
  174. return err
  175. }
  176. mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
  177. n = unsafe.Sizeof(uname.Nodename)
  178. if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
  179. return err
  180. }
  181. mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
  182. n = unsafe.Sizeof(uname.Release)
  183. if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
  184. return err
  185. }
  186. mib = []_C_int{CTL_KERN, KERN_VERSION}
  187. n = unsafe.Sizeof(uname.Version)
  188. if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
  189. return err
  190. }
  191. // The version might have newlines or tabs in it, convert them to
  192. // spaces.
  193. for i, b := range uname.Version {
  194. if b == '\n' || b == '\t' {
  195. if i == len(uname.Version)-1 {
  196. uname.Version[i] = 0
  197. } else {
  198. uname.Version[i] = ' '
  199. }
  200. }
  201. }
  202. mib = []_C_int{CTL_HW, HW_MACHINE}
  203. n = unsafe.Sizeof(uname.Machine)
  204. if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
  205. return err
  206. }
  207. return nil
  208. }
  209. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  210. if raceenabled {
  211. raceReleaseMerge(unsafe.Pointer(&ioSync))
  212. }
  213. return sendfile(outfd, infd, offset, count)
  214. }
  215. /*
  216. * Exposed directly
  217. */
  218. //sys Access(path string, mode uint32) (err error)
  219. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  220. //sys Chdir(path string) (err error)
  221. //sys Chflags(path string, flags int) (err error)
  222. //sys Chmod(path string, mode uint32) (err error)
  223. //sys Chown(path string, uid int, gid int) (err error)
  224. //sys Chroot(path string) (err error)
  225. //sys Close(fd int) (err error)
  226. //sys Dup(fd int) (nfd int, err error)
  227. //sys Dup2(from int, to int) (err error)
  228. //sys Exit(code int)
  229. //sys ExtattrGetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
  230. //sys ExtattrSetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
  231. //sys ExtattrDeleteFd(fd int, attrnamespace int, attrname string) (err error)
  232. //sys ExtattrListFd(fd int, attrnamespace int, data uintptr, nbytes int) (ret int, err error)
  233. //sys ExtattrGetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
  234. //sys ExtattrSetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
  235. //sys ExtattrDeleteFile(file string, attrnamespace int, attrname string) (err error)
  236. //sys ExtattrListFile(file string, attrnamespace int, data uintptr, nbytes int) (ret int, err error)
  237. //sys ExtattrGetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
  238. //sys ExtattrSetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
  239. //sys ExtattrDeleteLink(link string, attrnamespace int, attrname string) (err error)
  240. //sys ExtattrListLink(link string, attrnamespace int, data uintptr, nbytes int) (ret int, err error)
  241. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  242. //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_POSIX_FADVISE
  243. //sys Fchdir(fd int) (err error)
  244. //sys Fchflags(fd int, flags int) (err error)
  245. //sys Fchmod(fd int, mode uint32) (err error)
  246. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  247. //sys Fchown(fd int, uid int, gid int) (err error)
  248. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  249. //sys Flock(fd int, how int) (err error)
  250. //sys Fpathconf(fd int, name int) (val int, err error)
  251. //sys Fstat(fd int, stat *Stat_t) (err error)
  252. //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
  253. //sys Fsync(fd int) (err error)
  254. //sys Ftruncate(fd int, length int64) (err error)
  255. //sysnb Getegid() (egid int)
  256. //sysnb Geteuid() (uid int)
  257. //sysnb Getgid() (gid int)
  258. //sysnb Getpgid(pid int) (pgid int, err error)
  259. //sysnb Getpgrp() (pgrp int)
  260. //sysnb Getpid() (pid int)
  261. //sysnb Getppid() (ppid int)
  262. //sys Getpriority(which int, who int) (prio int, err error)
  263. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  264. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  265. //sysnb Getsid(pid int) (sid int, err error)
  266. //sysnb Gettimeofday(tv *Timeval) (err error)
  267. //sysnb Getuid() (uid int)
  268. //sys Issetugid() (tainted bool)
  269. //sys Kill(pid int, signum syscall.Signal) (err error)
  270. //sys Kqueue() (fd int, err error)
  271. //sys Lchown(path string, uid int, gid int) (err error)
  272. //sys Link(path string, link string) (err error)
  273. //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
  274. //sys Listen(s int, backlog int) (err error)
  275. //sys Lstat(path string, stat *Stat_t) (err error)
  276. //sys Mkdir(path string, mode uint32) (err error)
  277. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  278. //sys Mkfifo(path string, mode uint32) (err error)
  279. //sys Mkfifoat(dirfd int, path string, mode uint32) (err error)
  280. //sys Mknod(path string, mode uint32, dev int) (err error)
  281. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  282. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  283. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  284. //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
  285. //sys Pathconf(path string, name int) (val int, err error)
  286. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  287. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  288. //sys read(fd int, p []byte) (n int, err error)
  289. //sys Readlink(path string, buf []byte) (n int, err error)
  290. //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
  291. //sys Rename(from string, to string) (err error)
  292. //sys Renameat(fromfd int, from string, tofd int, to string) (err error)
  293. //sys Revoke(path string) (err error)
  294. //sys Rmdir(path string) (err error)
  295. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
  296. //sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
  297. //sysnb Setegid(egid int) (err error)
  298. //sysnb Seteuid(euid int) (err error)
  299. //sysnb Setgid(gid int) (err error)
  300. //sysnb Setpgid(pid int, pgid int) (err error)
  301. //sys Setpriority(which int, who int, prio int) (err error)
  302. //sysnb Setregid(rgid int, egid int) (err error)
  303. //sysnb Setreuid(ruid int, euid int) (err error)
  304. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  305. //sysnb Setsid() (pid int, err error)
  306. //sysnb Settimeofday(tp *Timeval) (err error)
  307. //sysnb Setuid(uid int) (err error)
  308. //sys Stat(path string, stat *Stat_t) (err error)
  309. //sys Symlink(path string, link string) (err error)
  310. //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
  311. //sys Sync() (err error)
  312. //sys Truncate(path string, length int64) (err error)
  313. //sys Umask(newmask int) (oldmask int)
  314. //sys Unlink(path string) (err error)
  315. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  316. //sys Unmount(path string, flags int) (err error)
  317. //sys write(fd int, p []byte) (n int, err error)
  318. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  319. //sys munmap(addr uintptr, length uintptr) (err error)
  320. //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
  321. //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
  322. //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
  323. /*
  324. * Unimplemented
  325. */
  326. // ____semctl13
  327. // __clone
  328. // __fhopen40
  329. // __fhstat40
  330. // __fhstatvfs140
  331. // __fstat30
  332. // __getcwd
  333. // __getfh30
  334. // __getlogin
  335. // __lstat30
  336. // __mount50
  337. // __msgctl13
  338. // __msync13
  339. // __ntp_gettime30
  340. // __posix_chown
  341. // __posix_fchown
  342. // __posix_lchown
  343. // __posix_rename
  344. // __setlogin
  345. // __shmctl13
  346. // __sigaction_sigtramp
  347. // __sigaltstack14
  348. // __sigpending14
  349. // __sigprocmask14
  350. // __sigsuspend14
  351. // __sigtimedwait
  352. // __stat30
  353. // __syscall
  354. // __vfork14
  355. // _ksem_close
  356. // _ksem_destroy
  357. // _ksem_getvalue
  358. // _ksem_init
  359. // _ksem_open
  360. // _ksem_post
  361. // _ksem_trywait
  362. // _ksem_unlink
  363. // _ksem_wait
  364. // _lwp_continue
  365. // _lwp_create
  366. // _lwp_ctl
  367. // _lwp_detach
  368. // _lwp_exit
  369. // _lwp_getname
  370. // _lwp_getprivate
  371. // _lwp_kill
  372. // _lwp_park
  373. // _lwp_self
  374. // _lwp_setname
  375. // _lwp_setprivate
  376. // _lwp_suspend
  377. // _lwp_unpark
  378. // _lwp_unpark_all
  379. // _lwp_wait
  380. // _lwp_wakeup
  381. // _pset_bind
  382. // _sched_getaffinity
  383. // _sched_getparam
  384. // _sched_setaffinity
  385. // _sched_setparam
  386. // acct
  387. // aio_cancel
  388. // aio_error
  389. // aio_fsync
  390. // aio_read
  391. // aio_return
  392. // aio_suspend
  393. // aio_write
  394. // break
  395. // clock_getres
  396. // clock_gettime
  397. // clock_settime
  398. // compat_09_ogetdomainname
  399. // compat_09_osetdomainname
  400. // compat_09_ouname
  401. // compat_10_omsgsys
  402. // compat_10_osemsys
  403. // compat_10_oshmsys
  404. // compat_12_fstat12
  405. // compat_12_getdirentries
  406. // compat_12_lstat12
  407. // compat_12_msync
  408. // compat_12_oreboot
  409. // compat_12_oswapon
  410. // compat_12_stat12
  411. // compat_13_sigaction13
  412. // compat_13_sigaltstack13
  413. // compat_13_sigpending13
  414. // compat_13_sigprocmask13
  415. // compat_13_sigreturn13
  416. // compat_13_sigsuspend13
  417. // compat_14___semctl
  418. // compat_14_msgctl
  419. // compat_14_shmctl
  420. // compat_16___sigaction14
  421. // compat_16___sigreturn14
  422. // compat_20_fhstatfs
  423. // compat_20_fstatfs
  424. // compat_20_getfsstat
  425. // compat_20_statfs
  426. // compat_30___fhstat30
  427. // compat_30___fstat13
  428. // compat_30___lstat13
  429. // compat_30___stat13
  430. // compat_30_fhopen
  431. // compat_30_fhstat
  432. // compat_30_fhstatvfs1
  433. // compat_30_getdents
  434. // compat_30_getfh
  435. // compat_30_ntp_gettime
  436. // compat_30_socket
  437. // compat_40_mount
  438. // compat_43_fstat43
  439. // compat_43_lstat43
  440. // compat_43_oaccept
  441. // compat_43_ocreat
  442. // compat_43_oftruncate
  443. // compat_43_ogetdirentries
  444. // compat_43_ogetdtablesize
  445. // compat_43_ogethostid
  446. // compat_43_ogethostname
  447. // compat_43_ogetkerninfo
  448. // compat_43_ogetpagesize
  449. // compat_43_ogetpeername
  450. // compat_43_ogetrlimit
  451. // compat_43_ogetsockname
  452. // compat_43_okillpg
  453. // compat_43_olseek
  454. // compat_43_ommap
  455. // compat_43_oquota
  456. // compat_43_orecv
  457. // compat_43_orecvfrom
  458. // compat_43_orecvmsg
  459. // compat_43_osend
  460. // compat_43_osendmsg
  461. // compat_43_osethostid
  462. // compat_43_osethostname
  463. // compat_43_osetrlimit
  464. // compat_43_osigblock
  465. // compat_43_osigsetmask
  466. // compat_43_osigstack
  467. // compat_43_osigvec
  468. // compat_43_otruncate
  469. // compat_43_owait
  470. // compat_43_stat43
  471. // execve
  472. // extattr_delete_fd
  473. // extattr_delete_file
  474. // extattr_delete_link
  475. // extattr_get_fd
  476. // extattr_get_file
  477. // extattr_get_link
  478. // extattr_list_fd
  479. // extattr_list_file
  480. // extattr_list_link
  481. // extattr_set_fd
  482. // extattr_set_file
  483. // extattr_set_link
  484. // extattrctl
  485. // fchroot
  486. // fdatasync
  487. // fgetxattr
  488. // fktrace
  489. // flistxattr
  490. // fork
  491. // fremovexattr
  492. // fsetxattr
  493. // fstatvfs1
  494. // fsync_range
  495. // getcontext
  496. // getitimer
  497. // getvfsstat
  498. // getxattr
  499. // ktrace
  500. // lchflags
  501. // lchmod
  502. // lfs_bmapv
  503. // lfs_markv
  504. // lfs_segclean
  505. // lfs_segwait
  506. // lgetxattr
  507. // lio_listio
  508. // listxattr
  509. // llistxattr
  510. // lremovexattr
  511. // lseek
  512. // lsetxattr
  513. // lutimes
  514. // madvise
  515. // mincore
  516. // minherit
  517. // modctl
  518. // mq_close
  519. // mq_getattr
  520. // mq_notify
  521. // mq_open
  522. // mq_receive
  523. // mq_send
  524. // mq_setattr
  525. // mq_timedreceive
  526. // mq_timedsend
  527. // mq_unlink
  528. // mremap
  529. // msgget
  530. // msgrcv
  531. // msgsnd
  532. // nfssvc
  533. // ntp_adjtime
  534. // pmc_control
  535. // pmc_get_info
  536. // pollts
  537. // preadv
  538. // profil
  539. // pselect
  540. // pset_assign
  541. // pset_create
  542. // pset_destroy
  543. // ptrace
  544. // pwritev
  545. // quotactl
  546. // rasctl
  547. // readv
  548. // reboot
  549. // removexattr
  550. // sa_enable
  551. // sa_preempt
  552. // sa_register
  553. // sa_setconcurrency
  554. // sa_stacks
  555. // sa_yield
  556. // sbrk
  557. // sched_yield
  558. // semconfig
  559. // semget
  560. // semop
  561. // setcontext
  562. // setitimer
  563. // setxattr
  564. // shmat
  565. // shmdt
  566. // shmget
  567. // sstk
  568. // statvfs1
  569. // swapctl
  570. // sysarch
  571. // syscall
  572. // timer_create
  573. // timer_delete
  574. // timer_getoverrun
  575. // timer_gettime
  576. // timer_settime
  577. // undelete
  578. // utrace
  579. // uuidgen
  580. // vadvise
  581. // vfork
  582. // writev
上海开阖软件有限公司 沪ICP备12045867号-1