syscall_dragonfly.go 14 KB

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