syscall_darwin.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. /*
  234. * Exposed directly
  235. */
  236. //sys Access(path string, mode uint32) (err error)
  237. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  238. //sys Chdir(path string) (err error)
  239. //sys Chflags(path string, flags int) (err error)
  240. //sys Chmod(path string, mode uint32) (err error)
  241. //sys Chown(path string, uid int, gid int) (err error)
  242. //sys Chroot(path string) (err error)
  243. //sys Close(fd int) (err error)
  244. //sys Dup(fd int) (nfd int, err error)
  245. //sys Dup2(from int, to int) (err error)
  246. //sys Exchangedata(path1 string, path2 string, options int) (err error)
  247. //sys Exit(code int)
  248. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  249. //sys Fchdir(fd int) (err error)
  250. //sys Fchflags(fd int, flags int) (err error)
  251. //sys Fchmod(fd int, mode uint32) (err error)
  252. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  253. //sys Fchown(fd int, uid int, gid int) (err error)
  254. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  255. //sys Flock(fd int, how int) (err error)
  256. //sys Fpathconf(fd int, name int) (val int, err error)
  257. //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
  258. //sys Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
  259. //sys Fsync(fd int) (err error)
  260. //sys Ftruncate(fd int, length int64) (err error)
  261. //sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
  262. //sys Getdtablesize() (size int)
  263. //sysnb Getegid() (egid int)
  264. //sysnb Geteuid() (uid int)
  265. //sysnb Getgid() (gid int)
  266. //sysnb Getpgid(pid int) (pgid int, err error)
  267. //sysnb Getpgrp() (pgrp int)
  268. //sysnb Getpid() (pid int)
  269. //sysnb Getppid() (ppid int)
  270. //sys Getpriority(which int, who int) (prio int, err error)
  271. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  272. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  273. //sysnb Getsid(pid int) (sid int, err error)
  274. //sysnb Getuid() (uid int)
  275. //sysnb Issetugid() (tainted bool)
  276. //sys Kqueue() (fd int, err error)
  277. //sys Lchown(path string, uid int, gid int) (err error)
  278. //sys Link(path string, link string) (err error)
  279. //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
  280. //sys Listen(s int, backlog int) (err error)
  281. //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
  282. //sys Mkdir(path string, mode uint32) (err error)
  283. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  284. //sys Mkfifo(path string, mode uint32) (err error)
  285. //sys Mknod(path string, mode uint32, dev int) (err error)
  286. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  287. //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
  288. //sys Pathconf(path string, name int) (val int, err error)
  289. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  290. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  291. //sys read(fd int, p []byte) (n int, err error)
  292. //sys Readlink(path string, buf []byte) (n int, err error)
  293. //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
  294. //sys Rename(from string, to string) (err error)
  295. //sys Renameat(fromfd int, from string, tofd int, to string) (err error)
  296. //sys Revoke(path string) (err error)
  297. //sys Rmdir(path string) (err error)
  298. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
  299. //sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
  300. //sys Setegid(egid int) (err error)
  301. //sysnb Seteuid(euid int) (err error)
  302. //sysnb Setgid(gid int) (err error)
  303. //sys Setlogin(name string) (err error)
  304. //sysnb Setpgid(pid int, pgid int) (err error)
  305. //sys Setpriority(which int, who int, prio int) (err error)
  306. //sys Setprivexec(flag int) (err error)
  307. //sysnb Setregid(rgid int, egid int) (err error)
  308. //sysnb Setreuid(ruid int, euid int) (err error)
  309. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  310. //sysnb Setsid() (pid int, err error)
  311. //sysnb Settimeofday(tp *Timeval) (err error)
  312. //sysnb Setuid(uid int) (err error)
  313. //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
  314. //sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
  315. //sys Symlink(path string, link string) (err error)
  316. //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
  317. //sys Sync() (err error)
  318. //sys Truncate(path string, length int64) (err error)
  319. //sys Umask(newmask int) (oldmask int)
  320. //sys Undelete(path string) (err error)
  321. //sys Unlink(path string) (err error)
  322. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  323. //sys Unmount(path string, flags int) (err error)
  324. //sys write(fd int, p []byte) (n int, err error)
  325. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  326. //sys munmap(addr uintptr, length uintptr) (err error)
  327. //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
  328. //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
  329. /*
  330. * Unimplemented
  331. */
  332. // Profil
  333. // Sigaction
  334. // Sigprocmask
  335. // Getlogin
  336. // Sigpending
  337. // Sigaltstack
  338. // Ioctl
  339. // Reboot
  340. // Execve
  341. // Vfork
  342. // Sbrk
  343. // Sstk
  344. // Ovadvise
  345. // Mincore
  346. // Setitimer
  347. // Swapon
  348. // Select
  349. // Sigsuspend
  350. // Readv
  351. // Writev
  352. // Nfssvc
  353. // Getfh
  354. // Quotactl
  355. // Mount
  356. // Csops
  357. // Waitid
  358. // Add_profil
  359. // Kdebug_trace
  360. // Sigreturn
  361. // Atsocket
  362. // Kqueue_from_portset_np
  363. // Kqueue_portset
  364. // Getattrlist
  365. // Setattrlist
  366. // Getdirentriesattr
  367. // Searchfs
  368. // Delete
  369. // Copyfile
  370. // Watchevent
  371. // Waitevent
  372. // Modwatch
  373. // Getxattr
  374. // Fgetxattr
  375. // Setxattr
  376. // Fsetxattr
  377. // Removexattr
  378. // Fremovexattr
  379. // Listxattr
  380. // Flistxattr
  381. // Fsctl
  382. // Initgroups
  383. // Posix_spawn
  384. // Nfsclnt
  385. // Fhopen
  386. // Minherit
  387. // Semsys
  388. // Msgsys
  389. // Shmsys
  390. // Semctl
  391. // Semget
  392. // Semop
  393. // Msgctl
  394. // Msgget
  395. // Msgsnd
  396. // Msgrcv
  397. // Shmat
  398. // Shmctl
  399. // Shmdt
  400. // Shmget
  401. // Shm_open
  402. // Shm_unlink
  403. // Sem_open
  404. // Sem_close
  405. // Sem_unlink
  406. // Sem_wait
  407. // Sem_trywait
  408. // Sem_post
  409. // Sem_getvalue
  410. // Sem_init
  411. // Sem_destroy
  412. // Open_extended
  413. // Umask_extended
  414. // Stat_extended
  415. // Lstat_extended
  416. // Fstat_extended
  417. // Chmod_extended
  418. // Fchmod_extended
  419. // Access_extended
  420. // Settid
  421. // Gettid
  422. // Setsgroups
  423. // Getsgroups
  424. // Setwgroups
  425. // Getwgroups
  426. // Mkfifo_extended
  427. // Mkdir_extended
  428. // Identitysvc
  429. // Shared_region_check_np
  430. // Shared_region_map_np
  431. // __pthread_mutex_destroy
  432. // __pthread_mutex_init
  433. // __pthread_mutex_lock
  434. // __pthread_mutex_trylock
  435. // __pthread_mutex_unlock
  436. // __pthread_cond_init
  437. // __pthread_cond_destroy
  438. // __pthread_cond_broadcast
  439. // __pthread_cond_signal
  440. // Setsid_with_pid
  441. // __pthread_cond_timedwait
  442. // Aio_fsync
  443. // Aio_return
  444. // Aio_suspend
  445. // Aio_cancel
  446. // Aio_error
  447. // Aio_read
  448. // Aio_write
  449. // Lio_listio
  450. // __pthread_cond_wait
  451. // Iopolicysys
  452. // __pthread_kill
  453. // __pthread_sigmask
  454. // __sigwait
  455. // __disable_threadsignal
  456. // __pthread_markcancel
  457. // __pthread_canceled
  458. // __semwait_signal
  459. // Proc_info
  460. // sendfile
  461. // Stat64_extended
  462. // Lstat64_extended
  463. // Fstat64_extended
  464. // __pthread_chdir
  465. // __pthread_fchdir
  466. // Audit
  467. // Auditon
  468. // Getauid
  469. // Setauid
  470. // Getaudit
  471. // Setaudit
  472. // Getaudit_addr
  473. // Setaudit_addr
  474. // Auditctl
  475. // Bsdthread_create
  476. // Bsdthread_terminate
  477. // Stack_snapshot
  478. // Bsdthread_register
  479. // Workq_open
  480. // Workq_ops
  481. // __mac_execve
  482. // __mac_syscall
  483. // __mac_get_file
  484. // __mac_set_file
  485. // __mac_get_link
  486. // __mac_set_link
  487. // __mac_get_proc
  488. // __mac_set_proc
  489. // __mac_get_fd
  490. // __mac_set_fd
  491. // __mac_get_pid
  492. // __mac_get_lcid
  493. // __mac_get_lctx
  494. // __mac_set_lctx
  495. // Setlcid
  496. // Read_nocancel
  497. // Write_nocancel
  498. // Open_nocancel
  499. // Close_nocancel
  500. // Wait4_nocancel
  501. // Recvmsg_nocancel
  502. // Sendmsg_nocancel
  503. // Recvfrom_nocancel
  504. // Accept_nocancel
  505. // Fcntl_nocancel
  506. // Select_nocancel
  507. // Fsync_nocancel
  508. // Connect_nocancel
  509. // Sigsuspend_nocancel
  510. // Readv_nocancel
  511. // Writev_nocancel
  512. // Sendto_nocancel
  513. // Pread_nocancel
  514. // Pwrite_nocancel
  515. // Waitid_nocancel
  516. // Poll_nocancel
  517. // Msgsnd_nocancel
  518. // Msgrcv_nocancel
  519. // Sem_wait_nocancel
  520. // Aio_suspend_nocancel
  521. // __sigwait_nocancel
  522. // __semwait_signal_nocancel
  523. // __mac_mount
  524. // __mac_get_mount
  525. // __mac_getfsstat