syscall_dragonfly.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // Copyright 2009 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. // DragonFly BSD 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 "unsafe"
  13. // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
  14. type SockaddrDatalink struct {
  15. Len uint8
  16. Family uint8
  17. Index uint16
  18. Type uint8
  19. Nlen uint8
  20. Alen uint8
  21. Slen uint8
  22. Data [12]int8
  23. Rcf uint16
  24. Route [16]uint16
  25. raw RawSockaddrDatalink
  26. }
  27. // Translate "kern.hostname" to []_C_int{0,1,2,3}.
  28. func nametomib(name string) (mib []_C_int, err error) {
  29. const siz = unsafe.Sizeof(mib[0])
  30. // NOTE(rsc): It seems strange to set the buffer to have
  31. // size CTL_MAXNAME+2 but use only CTL_MAXNAME
  32. // as the size. I don't know why the +2 is here, but the
  33. // kernel uses +2 for its own implementation of this function.
  34. // I am scared that if we don't include the +2 here, the kernel
  35. // will silently write 2 words farther than we specify
  36. // and we'll get memory corruption.
  37. var buf [CTL_MAXNAME + 2]_C_int
  38. n := uintptr(CTL_MAXNAME) * siz
  39. p := (*byte)(unsafe.Pointer(&buf[0]))
  40. bytes, err := ByteSliceFromString(name)
  41. if err != nil {
  42. return nil, err
  43. }
  44. // Magic sysctl: "setting" 0.3 to a string name
  45. // lets you read back the array of integers form.
  46. if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
  47. return nil, err
  48. }
  49. return buf[0 : n/siz], nil
  50. }
  51. //sysnb pipe() (r int, w int, err error)
  52. func Pipe(p []int) (err error) {
  53. if len(p) != 2 {
  54. return EINVAL
  55. }
  56. p[0], p[1], err = pipe()
  57. return
  58. }
  59. //sys extpread(fd int, p []byte, flags int, offset int64) (n int, err error)
  60. func Pread(fd int, p []byte, offset int64) (n int, err error) {
  61. return extpread(fd, p, 0, offset)
  62. }
  63. //sys extpwrite(fd int, p []byte, flags int, offset int64) (n int, err error)
  64. func Pwrite(fd int, p []byte, offset int64) (n int, err error) {
  65. return extpwrite(fd, p, 0, offset)
  66. }
  67. func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) {
  68. var rsa RawSockaddrAny
  69. var len _Socklen = SizeofSockaddrAny
  70. nfd, err = accept4(fd, &rsa, &len, flags)
  71. if err != nil {
  72. return
  73. }
  74. if len > SizeofSockaddrAny {
  75. panic("RawSockaddrAny too small")
  76. }
  77. sa, err = anyToSockaddr(fd, &rsa)
  78. if err != nil {
  79. Close(nfd)
  80. nfd = 0
  81. }
  82. return
  83. }
  84. const ImplementsGetwd = true
  85. //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD
  86. func Getwd() (string, error) {
  87. var buf [PathMax]byte
  88. _, err := Getcwd(buf[0:])
  89. if err != nil {
  90. return "", err
  91. }
  92. n := clen(buf[:])
  93. if n < 1 {
  94. return "", EINVAL
  95. }
  96. return string(buf[:n]), nil
  97. }
  98. func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
  99. var _p0 unsafe.Pointer
  100. var bufsize uintptr
  101. if len(buf) > 0 {
  102. _p0 = unsafe.Pointer(&buf[0])
  103. bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
  104. }
  105. r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
  106. n = int(r0)
  107. if e1 != 0 {
  108. err = e1
  109. }
  110. return
  111. }
  112. func setattrlistTimes(path string, times []Timespec, flags int) error {
  113. // used on Darwin for UtimesNano
  114. return ENOSYS
  115. }
  116. //sys ioctl(fd int, req uint, arg uintptr) (err error)
  117. // ioctl itself should not be exposed directly, but additional get/set
  118. // functions for specific types are permissible.
  119. // IoctlSetInt performs an ioctl operation which sets an integer value
  120. // on fd, using the specified request number.
  121. func IoctlSetInt(fd int, req uint, value int) error {
  122. return ioctl(fd, req, uintptr(value))
  123. }
  124. func ioctlSetWinsize(fd int, req uint, value *Winsize) error {
  125. return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  126. }
  127. func ioctlSetTermios(fd int, req uint, value *Termios) error {
  128. return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  129. }
  130. // IoctlGetInt performs an ioctl operation which gets an integer value
  131. // from fd, using the specified request number.
  132. func IoctlGetInt(fd int, req uint) (int, error) {
  133. var value int
  134. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  135. return value, err
  136. }
  137. func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
  138. var value Winsize
  139. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  140. return &value, err
  141. }
  142. func IoctlGetTermios(fd int, req uint) (*Termios, error) {
  143. var value Termios
  144. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  145. return &value, err
  146. }
  147. func sysctlUname(mib []_C_int, old *byte, oldlen *uintptr) error {
  148. err := sysctl(mib, old, oldlen, nil, 0)
  149. if err != nil {
  150. // Utsname members on Dragonfly are only 32 bytes and
  151. // the syscall returns ENOMEM in case the actual value
  152. // is longer.
  153. if err == ENOMEM {
  154. err = nil
  155. }
  156. }
  157. return err
  158. }
  159. func Uname(uname *Utsname) error {
  160. mib := []_C_int{CTL_KERN, KERN_OSTYPE}
  161. n := unsafe.Sizeof(uname.Sysname)
  162. if err := sysctlUname(mib, &uname.Sysname[0], &n); err != nil {
  163. return err
  164. }
  165. uname.Sysname[unsafe.Sizeof(uname.Sysname)-1] = 0
  166. mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
  167. n = unsafe.Sizeof(uname.Nodename)
  168. if err := sysctlUname(mib, &uname.Nodename[0], &n); err != nil {
  169. return err
  170. }
  171. uname.Nodename[unsafe.Sizeof(uname.Nodename)-1] = 0
  172. mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
  173. n = unsafe.Sizeof(uname.Release)
  174. if err := sysctlUname(mib, &uname.Release[0], &n); err != nil {
  175. return err
  176. }
  177. uname.Release[unsafe.Sizeof(uname.Release)-1] = 0
  178. mib = []_C_int{CTL_KERN, KERN_VERSION}
  179. n = unsafe.Sizeof(uname.Version)
  180. if err := sysctlUname(mib, &uname.Version[0], &n); err != nil {
  181. return err
  182. }
  183. // The version might have newlines or tabs in it, convert them to
  184. // spaces.
  185. for i, b := range uname.Version {
  186. if b == '\n' || b == '\t' {
  187. if i == len(uname.Version)-1 {
  188. uname.Version[i] = 0
  189. } else {
  190. uname.Version[i] = ' '
  191. }
  192. }
  193. }
  194. mib = []_C_int{CTL_HW, HW_MACHINE}
  195. n = unsafe.Sizeof(uname.Machine)
  196. if err := sysctlUname(mib, &uname.Machine[0], &n); err != nil {
  197. return err
  198. }
  199. uname.Machine[unsafe.Sizeof(uname.Machine)-1] = 0
  200. return nil
  201. }
  202. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  203. if raceenabled {
  204. raceReleaseMerge(unsafe.Pointer(&ioSync))
  205. }
  206. return sendfile(outfd, infd, offset, count)
  207. }
  208. /*
  209. * Exposed directly
  210. */
  211. //sys Access(path string, mode uint32) (err error)
  212. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  213. //sys Chdir(path string) (err error)
  214. //sys Chflags(path string, flags int) (err error)
  215. //sys Chmod(path string, mode uint32) (err error)
  216. //sys Chown(path string, uid int, gid int) (err error)
  217. //sys Chroot(path string) (err error)
  218. //sys Close(fd int) (err error)
  219. //sys Dup(fd int) (nfd int, err error)
  220. //sys Dup2(from int, to int) (err error)
  221. //sys Exit(code int)
  222. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  223. //sys Fchdir(fd int) (err error)
  224. //sys Fchflags(fd int, flags int) (err error)
  225. //sys Fchmod(fd int, mode uint32) (err error)
  226. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  227. //sys Fchown(fd int, uid int, gid int) (err error)
  228. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  229. //sys Flock(fd int, how int) (err error)
  230. //sys Fpathconf(fd int, name int) (val int, err error)
  231. //sys Fstat(fd int, stat *Stat_t) (err error)
  232. //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
  233. //sys Fstatfs(fd int, stat *Statfs_t) (err error)
  234. //sys Fsync(fd int) (err error)
  235. //sys Ftruncate(fd int, length int64) (err error)
  236. //sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error)
  237. //sys Getdtablesize() (size int)
  238. //sysnb Getegid() (egid int)
  239. //sysnb Geteuid() (uid int)
  240. //sysnb Getgid() (gid int)
  241. //sysnb Getpgid(pid int) (pgid int, err error)
  242. //sysnb Getpgrp() (pgrp int)
  243. //sysnb Getpid() (pid int)
  244. //sysnb Getppid() (ppid int)
  245. //sys Getpriority(which int, who int) (prio int, err error)
  246. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  247. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  248. //sysnb Getsid(pid int) (sid int, err error)
  249. //sysnb Gettimeofday(tv *Timeval) (err error)
  250. //sysnb Getuid() (uid int)
  251. //sys Issetugid() (tainted bool)
  252. //sys Kill(pid int, signum syscall.Signal) (err error)
  253. //sys Kqueue() (fd int, err error)
  254. //sys Lchown(path string, uid int, gid int) (err error)
  255. //sys Link(path string, link string) (err error)
  256. //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
  257. //sys Listen(s int, backlog int) (err error)
  258. //sys Lstat(path string, stat *Stat_t) (err error)
  259. //sys Mkdir(path string, mode uint32) (err error)
  260. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  261. //sys Mkfifo(path string, mode uint32) (err error)
  262. //sys Mknod(path string, mode uint32, dev int) (err error)
  263. //sys Mknodat(fd int, path string, mode uint32, dev int) (err error)
  264. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  265. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  266. //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
  267. //sys Pathconf(path string, name int) (val int, err error)
  268. //sys read(fd int, p []byte) (n int, err error)
  269. //sys Readlink(path string, buf []byte) (n int, err error)
  270. //sys Rename(from string, to string) (err error)
  271. //sys Renameat(fromfd int, from string, tofd int, to string) (err error)
  272. //sys Revoke(path string) (err error)
  273. //sys Rmdir(path string) (err error)
  274. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
  275. //sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
  276. //sysnb Setegid(egid int) (err error)
  277. //sysnb Seteuid(euid int) (err error)
  278. //sysnb Setgid(gid int) (err error)
  279. //sys Setlogin(name string) (err error)
  280. //sysnb Setpgid(pid int, pgid int) (err error)
  281. //sys Setpriority(which int, who int, prio int) (err error)
  282. //sysnb Setregid(rgid int, egid int) (err error)
  283. //sysnb Setreuid(ruid int, euid int) (err error)
  284. //sysnb Setresgid(rgid int, egid int, sgid int) (err error)
  285. //sysnb Setresuid(ruid int, euid int, suid int) (err error)
  286. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  287. //sysnb Setsid() (pid int, err error)
  288. //sysnb Settimeofday(tp *Timeval) (err error)
  289. //sysnb Setuid(uid int) (err error)
  290. //sys Stat(path string, stat *Stat_t) (err error)
  291. //sys Statfs(path string, stat *Statfs_t) (err error)
  292. //sys Symlink(path string, link string) (err error)
  293. //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
  294. //sys Sync() (err error)
  295. //sys Truncate(path string, length int64) (err error)
  296. //sys Umask(newmask int) (oldmask int)
  297. //sys Undelete(path string) (err error)
  298. //sys Unlink(path string) (err error)
  299. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  300. //sys Unmount(path string, flags int) (err error)
  301. //sys write(fd int, p []byte) (n int, err error)
  302. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  303. //sys munmap(addr uintptr, length uintptr) (err error)
  304. //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
  305. //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
  306. //sys accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error)
  307. //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
  308. /*
  309. * Unimplemented
  310. * TODO(jsing): Update this list for DragonFly.
  311. */
  312. // Profil
  313. // Sigaction
  314. // Sigprocmask
  315. // Getlogin
  316. // Sigpending
  317. // Sigaltstack
  318. // Reboot
  319. // Execve
  320. // Vfork
  321. // Sbrk
  322. // Sstk
  323. // Ovadvise
  324. // Mincore
  325. // Setitimer
  326. // Swapon
  327. // Select
  328. // Sigsuspend
  329. // Readv
  330. // Writev
  331. // Nfssvc
  332. // Getfh
  333. // Quotactl
  334. // Mount
  335. // Csops
  336. // Waitid
  337. // Add_profil
  338. // Kdebug_trace
  339. // Sigreturn
  340. // Atsocket
  341. // Kqueue_from_portset_np
  342. // Kqueue_portset
  343. // Getattrlist
  344. // Setattrlist
  345. // Getdirentriesattr
  346. // Searchfs
  347. // Delete
  348. // Copyfile
  349. // Watchevent
  350. // Waitevent
  351. // Modwatch
  352. // Getxattr
  353. // Fgetxattr
  354. // Setxattr
  355. // Fsetxattr
  356. // Removexattr
  357. // Fremovexattr
  358. // Listxattr
  359. // Flistxattr
  360. // Fsctl
  361. // Initgroups
  362. // Posix_spawn
  363. // Nfsclnt
  364. // Fhopen
  365. // Minherit
  366. // Semsys
  367. // Msgsys
  368. // Shmsys
  369. // Semctl
  370. // Semget
  371. // Semop
  372. // Msgctl
  373. // Msgget
  374. // Msgsnd
  375. // Msgrcv
  376. // Shmat
  377. // Shmctl
  378. // Shmdt
  379. // Shmget
  380. // Shm_open
  381. // Shm_unlink
  382. // Sem_open
  383. // Sem_close
  384. // Sem_unlink
  385. // Sem_wait
  386. // Sem_trywait
  387. // Sem_post
  388. // Sem_getvalue
  389. // Sem_init
  390. // Sem_destroy
  391. // Open_extended
  392. // Umask_extended
  393. // Stat_extended
  394. // Lstat_extended
  395. // Fstat_extended
  396. // Chmod_extended
  397. // Fchmod_extended
  398. // Access_extended
  399. // Settid
  400. // Gettid
  401. // Setsgroups
  402. // Getsgroups
  403. // Setwgroups
  404. // Getwgroups
  405. // Mkfifo_extended
  406. // Mkdir_extended
  407. // Identitysvc
  408. // Shared_region_check_np
  409. // Shared_region_map_np
  410. // __pthread_mutex_destroy
  411. // __pthread_mutex_init
  412. // __pthread_mutex_lock
  413. // __pthread_mutex_trylock
  414. // __pthread_mutex_unlock
  415. // __pthread_cond_init
  416. // __pthread_cond_destroy
  417. // __pthread_cond_broadcast
  418. // __pthread_cond_signal
  419. // Setsid_with_pid
  420. // __pthread_cond_timedwait
  421. // Aio_fsync
  422. // Aio_return
  423. // Aio_suspend
  424. // Aio_cancel
  425. // Aio_error
  426. // Aio_read
  427. // Aio_write
  428. // Lio_listio
  429. // __pthread_cond_wait
  430. // Iopolicysys
  431. // __pthread_kill
  432. // __pthread_sigmask
  433. // __sigwait
  434. // __disable_threadsignal
  435. // __pthread_markcancel
  436. // __pthread_canceled
  437. // __semwait_signal
  438. // Proc_info
  439. // Stat64_extended
  440. // Lstat64_extended
  441. // Fstat64_extended
  442. // __pthread_chdir
  443. // __pthread_fchdir
  444. // Audit
  445. // Auditon
  446. // Getauid
  447. // Setauid
  448. // Getaudit
  449. // Setaudit
  450. // Getaudit_addr
  451. // Setaudit_addr
  452. // Auditctl
  453. // Bsdthread_create
  454. // Bsdthread_terminate
  455. // Stack_snapshot
  456. // Bsdthread_register
  457. // Workq_open
  458. // Workq_ops
  459. // __mac_execve
  460. // __mac_syscall
  461. // __mac_get_file
  462. // __mac_set_file
  463. // __mac_get_link
  464. // __mac_set_link
  465. // __mac_get_proc
  466. // __mac_set_proc
  467. // __mac_get_fd
  468. // __mac_set_fd
  469. // __mac_get_pid
  470. // __mac_get_lcid
  471. // __mac_get_lctx
  472. // __mac_set_lctx
  473. // Setlcid
  474. // Read_nocancel
  475. // Write_nocancel
  476. // Open_nocancel
  477. // Close_nocancel
  478. // Wait4_nocancel
  479. // Recvmsg_nocancel
  480. // Sendmsg_nocancel
  481. // Recvfrom_nocancel
  482. // Accept_nocancel
  483. // Fcntl_nocancel
  484. // Select_nocancel
  485. // Fsync_nocancel
  486. // Connect_nocancel
  487. // Sigsuspend_nocancel
  488. // Readv_nocancel
  489. // Writev_nocancel
  490. // Sendto_nocancel
  491. // Pread_nocancel
  492. // Pwrite_nocancel
  493. // Waitid_nocancel
  494. // Msgsnd_nocancel
  495. // Msgrcv_nocancel
  496. // Sem_wait_nocancel
  497. // Aio_suspend_nocancel
  498. // __sigwait_nocancel
  499. // __semwait_signal_nocancel
  500. // __mac_mount
  501. // __mac_get_mount
  502. // __mac_getfsstat