syscall_dragonfly.go 14 KB

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