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.

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