A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

syscall_darwin.go 12KB

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