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.

536 lines
14 KiB

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