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.

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