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_unix.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright 2009 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. // +build darwin dragonfly freebsd linux netbsd openbsd solaris
  5. package unix
  6. import (
  7. "runtime"
  8. "sync"
  9. "syscall"
  10. "unsafe"
  11. )
  12. var (
  13. Stdin = 0
  14. Stdout = 1
  15. Stderr = 2
  16. )
  17. const (
  18. darwin64Bit = runtime.GOOS == "darwin" && sizeofPtr == 8
  19. dragonfly64Bit = runtime.GOOS == "dragonfly" && sizeofPtr == 8
  20. netbsd32Bit = runtime.GOOS == "netbsd" && sizeofPtr == 4
  21. solaris64Bit = runtime.GOOS == "solaris" && sizeofPtr == 8
  22. )
  23. // Do the interface allocations only once for common
  24. // Errno values.
  25. var (
  26. errEAGAIN error = syscall.EAGAIN
  27. errEINVAL error = syscall.EINVAL
  28. errENOENT error = syscall.ENOENT
  29. )
  30. // errnoErr returns common boxed Errno values, to prevent
  31. // allocations at runtime.
  32. func errnoErr(e syscall.Errno) error {
  33. switch e {
  34. case 0:
  35. return nil
  36. case EAGAIN:
  37. return errEAGAIN
  38. case EINVAL:
  39. return errEINVAL
  40. case ENOENT:
  41. return errENOENT
  42. }
  43. return e
  44. }
  45. // Mmap manager, for use by operating system-specific implementations.
  46. type mmapper struct {
  47. sync.Mutex
  48. active map[*byte][]byte // active mappings; key is last byte in mapping
  49. mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
  50. munmap func(addr uintptr, length uintptr) error
  51. }
  52. func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  53. if length <= 0 {
  54. return nil, EINVAL
  55. }
  56. // Map the requested memory.
  57. addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
  58. if errno != nil {
  59. return nil, errno
  60. }
  61. // Slice memory layout
  62. var sl = struct {
  63. addr uintptr
  64. len int
  65. cap int
  66. }{addr, length, length}
  67. // Use unsafe to turn sl into a []byte.
  68. b := *(*[]byte)(unsafe.Pointer(&sl))
  69. // Register mapping in m and return it.
  70. p := &b[cap(b)-1]
  71. m.Lock()
  72. defer m.Unlock()
  73. m.active[p] = b
  74. return b, nil
  75. }
  76. func (m *mmapper) Munmap(data []byte) (err error) {
  77. if len(data) == 0 || len(data) != cap(data) {
  78. return EINVAL
  79. }
  80. // Find the base of the mapping.
  81. p := &data[cap(data)-1]
  82. m.Lock()
  83. defer m.Unlock()
  84. b := m.active[p]
  85. if b == nil || &b[0] != &data[0] {
  86. return EINVAL
  87. }
  88. // Unmap the memory and update m.
  89. if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
  90. return errno
  91. }
  92. delete(m.active, p)
  93. return nil
  94. }
  95. func Read(fd int, p []byte) (n int, err error) {
  96. n, err = read(fd, p)
  97. if raceenabled {
  98. if n > 0 {
  99. raceWriteRange(unsafe.Pointer(&p[0]), n)
  100. }
  101. if err == nil {
  102. raceAcquire(unsafe.Pointer(&ioSync))
  103. }
  104. }
  105. return
  106. }
  107. func Write(fd int, p []byte) (n int, err error) {
  108. if raceenabled {
  109. raceReleaseMerge(unsafe.Pointer(&ioSync))
  110. }
  111. n, err = write(fd, p)
  112. if raceenabled && n > 0 {
  113. raceReadRange(unsafe.Pointer(&p[0]), n)
  114. }
  115. return
  116. }
  117. // For testing: clients can set this flag to force
  118. // creation of IPv6 sockets to return EAFNOSUPPORT.
  119. var SocketDisableIPv6 bool
  120. type Sockaddr interface {
  121. sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
  122. }
  123. type SockaddrInet4 struct {
  124. Port int
  125. Addr [4]byte
  126. raw RawSockaddrInet4
  127. }
  128. type SockaddrInet6 struct {
  129. Port int
  130. ZoneId uint32
  131. Addr [16]byte
  132. raw RawSockaddrInet6
  133. }
  134. type SockaddrUnix struct {
  135. Name string
  136. raw RawSockaddrUnix
  137. }
  138. func Bind(fd int, sa Sockaddr) (err error) {
  139. ptr, n, err := sa.sockaddr()
  140. if err != nil {
  141. return err
  142. }
  143. return bind(fd, ptr, n)
  144. }
  145. func Connect(fd int, sa Sockaddr) (err error) {
  146. ptr, n, err := sa.sockaddr()
  147. if err != nil {
  148. return err
  149. }
  150. return connect(fd, ptr, n)
  151. }
  152. func Getpeername(fd int) (sa Sockaddr, err error) {
  153. var rsa RawSockaddrAny
  154. var len _Socklen = SizeofSockaddrAny
  155. if err = getpeername(fd, &rsa, &len); err != nil {
  156. return
  157. }
  158. return anyToSockaddr(&rsa)
  159. }
  160. func GetsockoptInt(fd, level, opt int) (value int, err error) {
  161. var n int32
  162. vallen := _Socklen(4)
  163. err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
  164. return int(n), err
  165. }
  166. func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
  167. var rsa RawSockaddrAny
  168. var len _Socklen = SizeofSockaddrAny
  169. if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
  170. return
  171. }
  172. if rsa.Addr.Family != AF_UNSPEC {
  173. from, err = anyToSockaddr(&rsa)
  174. }
  175. return
  176. }
  177. func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
  178. ptr, n, err := to.sockaddr()
  179. if err != nil {
  180. return err
  181. }
  182. return sendto(fd, p, flags, ptr, n)
  183. }
  184. func SetsockoptByte(fd, level, opt int, value byte) (err error) {
  185. return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
  186. }
  187. func SetsockoptInt(fd, level, opt int, value int) (err error) {
  188. var n = int32(value)
  189. return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
  190. }
  191. func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
  192. return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
  193. }
  194. func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
  195. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
  196. }
  197. func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
  198. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
  199. }
  200. func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
  201. return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
  202. }
  203. func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
  204. return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
  205. }
  206. func SetsockoptString(fd, level, opt int, s string) (err error) {
  207. return setsockopt(fd, level, opt, unsafe.Pointer(&[]byte(s)[0]), uintptr(len(s)))
  208. }
  209. func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
  210. return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
  211. }
  212. func Socket(domain, typ, proto int) (fd int, err error) {
  213. if domain == AF_INET6 && SocketDisableIPv6 {
  214. return -1, EAFNOSUPPORT
  215. }
  216. fd, err = socket(domain, typ, proto)
  217. return
  218. }
  219. func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
  220. var fdx [2]int32
  221. err = socketpair(domain, typ, proto, &fdx)
  222. if err == nil {
  223. fd[0] = int(fdx[0])
  224. fd[1] = int(fdx[1])
  225. }
  226. return
  227. }
  228. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  229. if raceenabled {
  230. raceReleaseMerge(unsafe.Pointer(&ioSync))
  231. }
  232. return sendfile(outfd, infd, offset, count)
  233. }
  234. var ioSync int64
  235. func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
  236. func SetNonblock(fd int, nonblocking bool) (err error) {
  237. flag, err := fcntl(fd, F_GETFL, 0)
  238. if err != nil {
  239. return err
  240. }
  241. if nonblocking {
  242. flag |= O_NONBLOCK
  243. } else {
  244. flag &= ^O_NONBLOCK
  245. }
  246. _, err = fcntl(fd, F_SETFL, flag)
  247. return err
  248. }