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.

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