syscall_darwin.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. // ParseDirent parses up to max directory entries in buf,
  69. // appending the names to names. It returns the number
  70. // bytes consumed from buf, the number of entries added
  71. // to names, and the new names slice.
  72. func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
  73. origlen := len(buf)
  74. for max != 0 && len(buf) > 0 {
  75. dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
  76. if dirent.Reclen == 0 {
  77. buf = nil
  78. break
  79. }
  80. buf = buf[dirent.Reclen:]
  81. if dirent.Ino == 0 { // File absent in directory.
  82. continue
  83. }
  84. bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
  85. var name = string(bytes[0:dirent.Namlen])
  86. if name == "." || name == ".." { // Useless names
  87. continue
  88. }
  89. max--
  90. count++
  91. names = append(names, name)
  92. }
  93. return origlen - len(buf), count, names
  94. }
  95. //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
  96. func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
  97. func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
  98. const (
  99. attrBitMapCount = 5
  100. attrCmnFullpath = 0x08000000
  101. )
  102. type attrList struct {
  103. bitmapCount uint16
  104. _ uint16
  105. CommonAttr uint32
  106. VolAttr uint32
  107. DirAttr uint32
  108. FileAttr uint32
  109. Forkattr uint32
  110. }
  111. func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) {
  112. if len(attrBuf) < 4 {
  113. return nil, errorspkg.New("attrBuf too small")
  114. }
  115. attrList.bitmapCount = attrBitMapCount
  116. var _p0 *byte
  117. _p0, err = BytePtrFromString(path)
  118. if err != nil {
  119. return nil, err
  120. }
  121. _, _, e1 := Syscall6(
  122. SYS_GETATTRLIST,
  123. uintptr(unsafe.Pointer(_p0)),
  124. uintptr(unsafe.Pointer(&attrList)),
  125. uintptr(unsafe.Pointer(&attrBuf[0])),
  126. uintptr(len(attrBuf)),
  127. uintptr(options),
  128. 0,
  129. )
  130. if e1 != 0 {
  131. return nil, e1
  132. }
  133. size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
  134. // dat is the section of attrBuf that contains valid data,
  135. // without the 4 byte length header. All attribute offsets
  136. // are relative to dat.
  137. dat := attrBuf
  138. if int(size) < len(attrBuf) {
  139. dat = dat[:size]
  140. }
  141. dat = dat[4:] // remove length prefix
  142. for i := uint32(0); int(i) < len(dat); {
  143. header := dat[i:]
  144. if len(header) < 8 {
  145. return attrs, errorspkg.New("truncated attribute header")
  146. }
  147. datOff := *(*int32)(unsafe.Pointer(&header[0]))
  148. attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
  149. if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
  150. return attrs, errorspkg.New("truncated results; attrBuf too small")
  151. }
  152. end := uint32(datOff) + attrLen
  153. attrs = append(attrs, dat[datOff:end])
  154. i = end
  155. if r := i % 4; r != 0 {
  156. i += (4 - r)
  157. }
  158. }
  159. return
  160. }
  161. //sysnb pipe() (r int, w int, err error)
  162. func Pipe(p []int) (err error) {
  163. if len(p) != 2 {
  164. return EINVAL
  165. }
  166. p[0], p[1], err = pipe()
  167. return
  168. }
  169. func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
  170. var _p0 unsafe.Pointer
  171. var bufsize uintptr
  172. if len(buf) > 0 {
  173. _p0 = unsafe.Pointer(&buf[0])
  174. bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
  175. }
  176. r0, _, e1 := Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags))
  177. n = int(r0)
  178. if e1 != 0 {
  179. err = e1
  180. }
  181. return
  182. }
  183. /*
  184. * Wrapped
  185. */
  186. //sys kill(pid int, signum int, posix int) (err error)
  187. func Kill(pid int, signum syscall.Signal) (err error) { return kill(pid, int(signum), 1) }
  188. /*
  189. * Exposed directly
  190. */
  191. //sys Access(path string, mode uint32) (err error)
  192. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  193. //sys Chdir(path string) (err error)
  194. //sys Chflags(path string, flags int) (err error)
  195. //sys Chmod(path string, mode uint32) (err error)
  196. //sys Chown(path string, uid int, gid int) (err error)
  197. //sys Chroot(path string) (err error)
  198. //sys Close(fd int) (err error)
  199. //sys Dup(fd int) (nfd int, err error)
  200. //sys Dup2(from int, to int) (err error)
  201. //sys Exchangedata(path1 string, path2 string, options int) (err error)
  202. //sys Exit(code int)
  203. //sys Fchdir(fd int) (err error)
  204. //sys Fchflags(fd int, flags int) (err error)
  205. //sys Fchmod(fd int, mode uint32) (err error)
  206. //sys Fchown(fd int, uid int, gid int) (err error)
  207. //sys Flock(fd int, how int) (err error)
  208. //sys Fpathconf(fd int, name int) (val int, err error)
  209. //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
  210. //sys Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
  211. //sys Fsync(fd int) (err error)
  212. //sys Ftruncate(fd int, length int64) (err error)
  213. //sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
  214. //sys Getdtablesize() (size int)
  215. //sysnb Getegid() (egid int)
  216. //sysnb Geteuid() (uid int)
  217. //sysnb Getgid() (gid int)
  218. //sysnb Getpgid(pid int) (pgid int, err error)
  219. //sysnb Getpgrp() (pgrp int)
  220. //sysnb Getpid() (pid int)
  221. //sysnb Getppid() (ppid int)
  222. //sys Getpriority(which int, who int) (prio int, err error)
  223. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  224. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  225. //sysnb Getsid(pid int) (sid int, err error)
  226. //sysnb Getuid() (uid int)
  227. //sysnb Issetugid() (tainted bool)
  228. //sys Kqueue() (fd int, err error)
  229. //sys Lchown(path string, uid int, gid int) (err error)
  230. //sys Link(path string, link string) (err error)
  231. //sys Listen(s int, backlog int) (err error)
  232. //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
  233. //sys Mkdir(path string, mode uint32) (err error)
  234. //sys Mkfifo(path string, mode uint32) (err error)
  235. //sys Mknod(path string, mode uint32, dev int) (err error)
  236. //sys Mlock(b []byte) (err error)
  237. //sys Mlockall(flags int) (err error)
  238. //sys Mprotect(b []byte, prot int) (err error)
  239. //sys Munlock(b []byte) (err error)
  240. //sys Munlockall() (err error)
  241. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  242. //sys Pathconf(path string, name int) (val int, err error)
  243. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  244. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  245. //sys read(fd int, p []byte) (n int, err error)
  246. //sys Readlink(path string, buf []byte) (n int, err error)
  247. //sys Rename(from string, to string) (err error)
  248. //sys Revoke(path string) (err error)
  249. //sys Rmdir(path string) (err error)
  250. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
  251. //sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
  252. //sys Setegid(egid int) (err error)
  253. //sysnb Seteuid(euid int) (err error)
  254. //sysnb Setgid(gid int) (err error)
  255. //sys Setlogin(name string) (err error)
  256. //sysnb Setpgid(pid int, pgid int) (err error)
  257. //sys Setpriority(which int, who int, prio int) (err error)
  258. //sys Setprivexec(flag int) (err error)
  259. //sysnb Setregid(rgid int, egid int) (err error)
  260. //sysnb Setreuid(ruid int, euid int) (err error)
  261. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  262. //sysnb Setsid() (pid int, err error)
  263. //sysnb Settimeofday(tp *Timeval) (err error)
  264. //sysnb Setuid(uid int) (err error)
  265. //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
  266. //sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
  267. //sys Symlink(path string, link string) (err error)
  268. //sys Sync() (err error)
  269. //sys Truncate(path string, length int64) (err error)
  270. //sys Umask(newmask int) (oldmask int)
  271. //sys Undelete(path string) (err error)
  272. //sys Unlink(path string) (err error)
  273. //sys Unmount(path string, flags int) (err error)
  274. //sys write(fd int, p []byte) (n int, err error)
  275. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  276. //sys munmap(addr uintptr, length uintptr) (err error)
  277. //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
  278. //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
  279. /*
  280. * Unimplemented
  281. */
  282. // Profil
  283. // Sigaction
  284. // Sigprocmask
  285. // Getlogin
  286. // Sigpending
  287. // Sigaltstack
  288. // Ioctl
  289. // Reboot
  290. // Execve
  291. // Vfork
  292. // Sbrk
  293. // Sstk
  294. // Ovadvise
  295. // Mincore
  296. // Setitimer
  297. // Swapon
  298. // Select
  299. // Sigsuspend
  300. // Readv
  301. // Writev
  302. // Nfssvc
  303. // Getfh
  304. // Quotactl
  305. // Mount
  306. // Csops
  307. // Waitid
  308. // Add_profil
  309. // Kdebug_trace
  310. // Sigreturn
  311. // Mmap
  312. // Mlock
  313. // Munlock
  314. // Atsocket
  315. // Kqueue_from_portset_np
  316. // Kqueue_portset
  317. // Getattrlist
  318. // Setattrlist
  319. // Getdirentriesattr
  320. // Searchfs
  321. // Delete
  322. // Copyfile
  323. // Poll
  324. // Watchevent
  325. // Waitevent
  326. // Modwatch
  327. // Getxattr
  328. // Fgetxattr
  329. // Setxattr
  330. // Fsetxattr
  331. // Removexattr
  332. // Fremovexattr
  333. // Listxattr
  334. // Flistxattr
  335. // Fsctl
  336. // Initgroups
  337. // Posix_spawn
  338. // Nfsclnt
  339. // Fhopen
  340. // Minherit
  341. // Semsys
  342. // Msgsys
  343. // Shmsys
  344. // Semctl
  345. // Semget
  346. // Semop
  347. // Msgctl
  348. // Msgget
  349. // Msgsnd
  350. // Msgrcv
  351. // Shmat
  352. // Shmctl
  353. // Shmdt
  354. // Shmget
  355. // Shm_open
  356. // Shm_unlink
  357. // Sem_open
  358. // Sem_close
  359. // Sem_unlink
  360. // Sem_wait
  361. // Sem_trywait
  362. // Sem_post
  363. // Sem_getvalue
  364. // Sem_init
  365. // Sem_destroy
  366. // Open_extended
  367. // Umask_extended
  368. // Stat_extended
  369. // Lstat_extended
  370. // Fstat_extended
  371. // Chmod_extended
  372. // Fchmod_extended
  373. // Access_extended
  374. // Settid
  375. // Gettid
  376. // Setsgroups
  377. // Getsgroups
  378. // Setwgroups
  379. // Getwgroups
  380. // Mkfifo_extended
  381. // Mkdir_extended
  382. // Identitysvc
  383. // Shared_region_check_np
  384. // Shared_region_map_np
  385. // __pthread_mutex_destroy
  386. // __pthread_mutex_init
  387. // __pthread_mutex_lock
  388. // __pthread_mutex_trylock
  389. // __pthread_mutex_unlock
  390. // __pthread_cond_init
  391. // __pthread_cond_destroy
  392. // __pthread_cond_broadcast
  393. // __pthread_cond_signal
  394. // Setsid_with_pid
  395. // __pthread_cond_timedwait
  396. // Aio_fsync
  397. // Aio_return
  398. // Aio_suspend
  399. // Aio_cancel
  400. // Aio_error
  401. // Aio_read
  402. // Aio_write
  403. // Lio_listio
  404. // __pthread_cond_wait
  405. // Iopolicysys
  406. // Mlockall
  407. // Munlockall
  408. // __pthread_kill
  409. // __pthread_sigmask
  410. // __sigwait
  411. // __disable_threadsignal
  412. // __pthread_markcancel
  413. // __pthread_canceled
  414. // __semwait_signal
  415. // Proc_info
  416. // sendfile
  417. // Stat64_extended
  418. // Lstat64_extended
  419. // Fstat64_extended
  420. // __pthread_chdir
  421. // __pthread_fchdir
  422. // Audit
  423. // Auditon
  424. // Getauid
  425. // Setauid
  426. // Getaudit
  427. // Setaudit
  428. // Getaudit_addr
  429. // Setaudit_addr
  430. // Auditctl
  431. // Bsdthread_create
  432. // Bsdthread_terminate
  433. // Stack_snapshot
  434. // Bsdthread_register
  435. // Workq_open
  436. // Workq_ops
  437. // __mac_execve
  438. // __mac_syscall
  439. // __mac_get_file
  440. // __mac_set_file
  441. // __mac_get_link
  442. // __mac_set_link
  443. // __mac_get_proc
  444. // __mac_set_proc
  445. // __mac_get_fd
  446. // __mac_set_fd
  447. // __mac_get_pid
  448. // __mac_get_lcid
  449. // __mac_get_lctx
  450. // __mac_set_lctx
  451. // Setlcid
  452. // Read_nocancel
  453. // Write_nocancel
  454. // Open_nocancel
  455. // Close_nocancel
  456. // Wait4_nocancel
  457. // Recvmsg_nocancel
  458. // Sendmsg_nocancel
  459. // Recvfrom_nocancel
  460. // Accept_nocancel
  461. // Msync_nocancel
  462. // Fcntl_nocancel
  463. // Select_nocancel
  464. // Fsync_nocancel
  465. // Connect_nocancel
  466. // Sigsuspend_nocancel
  467. // Readv_nocancel
  468. // Writev_nocancel
  469. // Sendto_nocancel
  470. // Pread_nocancel
  471. // Pwrite_nocancel
  472. // Waitid_nocancel
  473. // Poll_nocancel
  474. // Msgsnd_nocancel
  475. // Msgrcv_nocancel
  476. // Sem_wait_nocancel
  477. // Aio_suspend_nocancel
  478. // __sigwait_nocancel
  479. // __semwait_signal_nocancel
  480. // __mac_mount
  481. // __mac_get_mount
  482. // __mac_getfsstat