syscall_unix.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 aix darwin dragonfly freebsd linux netbsd openbsd solaris
  5. package unix
  6. import (
  7. "bytes"
  8. "sort"
  9. "sync"
  10. "syscall"
  11. "unsafe"
  12. )
  13. var (
  14. Stdin = 0
  15. Stdout = 1
  16. Stderr = 2
  17. )
  18. // Do the interface allocations only once for common
  19. // Errno values.
  20. var (
  21. errEAGAIN error = syscall.EAGAIN
  22. errEINVAL error = syscall.EINVAL
  23. errENOENT error = syscall.ENOENT
  24. )
  25. // errnoErr returns common boxed Errno values, to prevent
  26. // allocations at runtime.
  27. func errnoErr(e syscall.Errno) error {
  28. switch e {
  29. case 0:
  30. return nil
  31. case EAGAIN:
  32. return errEAGAIN
  33. case EINVAL:
  34. return errEINVAL
  35. case ENOENT:
  36. return errENOENT
  37. }
  38. return e
  39. }
  40. // ErrnoName returns the error name for error number e.
  41. func ErrnoName(e syscall.Errno) string {
  42. i := sort.Search(len(errorList), func(i int) bool {
  43. return errorList[i].num >= e
  44. })
  45. if i < len(errorList) && errorList[i].num == e {
  46. return errorList[i].name
  47. }
  48. return ""
  49. }
  50. // SignalName returns the signal name for signal number s.
  51. func SignalName(s syscall.Signal) string {
  52. i := sort.Search(len(signalList), func(i int) bool {
  53. return signalList[i].num >= s
  54. })
  55. if i < len(signalList) && signalList[i].num == s {
  56. return signalList[i].name
  57. }
  58. return ""
  59. }
  60. // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
  61. func clen(n []byte) int {
  62. i := bytes.IndexByte(n, 0)
  63. if i == -1 {
  64. i = len(n)
  65. }
  66. return i
  67. }
  68. // Mmap manager, for use by operating system-specific implementations.
  69. type mmapper struct {
  70. sync.Mutex
  71. active map[*byte][]byte // active mappings; key is last byte in mapping
  72. mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
  73. munmap func(addr uintptr, length uintptr) error
  74. }
  75. func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  76. if length <= 0 {
  77. return nil, EINVAL
  78. }
  79. // Map the requested memory.
  80. addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
  81. if errno != nil {
  82. return nil, errno
  83. }
  84. // Slice memory layout
  85. var sl = struct {
  86. addr uintptr
  87. len int
  88. cap int
  89. }{addr, length, length}
  90. // Use unsafe to turn sl into a []byte.
  91. b := *(*[]byte)(unsafe.Pointer(&sl))
  92. // Register mapping in m and return it.
  93. p := &b[cap(b)-1]
  94. m.Lock()
  95. defer m.Unlock()
  96. m.active[p] = b
  97. return b, nil
  98. }
  99. func (m *mmapper) Munmap(data []byte) (err error) {
  100. if len(data) == 0 || len(data) != cap(data) {
  101. return EINVAL
  102. }
  103. // Find the base of the mapping.
  104. p := &data[cap(data)-1]
  105. m.Lock()
  106. defer m.Unlock()
  107. b := m.active[p]
  108. if b == nil || &b[0] != &data[0] {
  109. return EINVAL
  110. }
  111. // Unmap the memory and update m.
  112. if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
  113. return errno
  114. }
  115. delete(m.active, p)
  116. return nil
  117. }
  118. func Read(fd int, p []byte) (n int, err error) {
  119. n, err = read(fd, p)
  120. if raceenabled {
  121. if n > 0 {
  122. raceWriteRange(unsafe.Pointer(&p[0]), n)
  123. }
  124. if err == nil {
  125. raceAcquire(unsafe.Pointer(&ioSync))
  126. }
  127. }
  128. return
  129. }
  130. func Write(fd int, p []byte) (n int, err error) {
  131. if raceenabled {
  132. raceReleaseMerge(unsafe.Pointer(&ioSync))
  133. }
  134. n, err = write(fd, p)
  135. if raceenabled && n > 0 {
  136. raceReadRange(unsafe.Pointer(&p[0]), n)
  137. }
  138. return
  139. }
  140. // For testing: clients can set this flag to force
  141. // creation of IPv6 sockets to return EAFNOSUPPORT.
  142. var SocketDisableIPv6 bool
  143. // Sockaddr represents a socket address.
  144. type Sockaddr interface {
  145. sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
  146. }
  147. // SockaddrInet4 implements the Sockaddr interface for AF_INET type sockets.
  148. type SockaddrInet4 struct {
  149. Port int
  150. Addr [4]byte
  151. raw RawSockaddrInet4
  152. }
  153. // SockaddrInet6 implements the Sockaddr interface for AF_INET6 type sockets.
  154. type SockaddrInet6 struct {
  155. Port int
  156. ZoneId uint32
  157. Addr [16]byte
  158. raw RawSockaddrInet6
  159. }
  160. // SockaddrUnix implements the Sockaddr interface for AF_UNIX type sockets.
  161. type SockaddrUnix struct {
  162. Name string
  163. raw RawSockaddrUnix
  164. }
  165. func Bind(fd int, sa Sockaddr) (err error) {
  166. ptr, n, err := sa.sockaddr()
  167. if err != nil {
  168. return err
  169. }
  170. return bind(fd, ptr, n)
  171. }
  172. func Connect(fd int, sa Sockaddr) (err error) {
  173. ptr, n, err := sa.sockaddr()
  174. if err != nil {
  175. return err
  176. }
  177. return connect(fd, ptr, n)
  178. }
  179. func Getpeername(fd int) (sa Sockaddr, err error) {
  180. var rsa RawSockaddrAny
  181. var len _Socklen = SizeofSockaddrAny
  182. if err = getpeername(fd, &rsa, &len); err != nil {
  183. return
  184. }
  185. return anyToSockaddr(fd, &rsa)
  186. }
  187. func GetsockoptByte(fd, level, opt int) (value byte, err error) {
  188. var n byte
  189. vallen := _Socklen(1)
  190. err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
  191. return n, err
  192. }
  193. func GetsockoptInt(fd, level, opt int) (value int, err error) {
  194. var n int32
  195. vallen := _Socklen(4)
  196. err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
  197. return int(n), err
  198. }
  199. func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
  200. vallen := _Socklen(4)
  201. err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
  202. return value, err
  203. }
  204. func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
  205. var value IPMreq
  206. vallen := _Socklen(SizeofIPMreq)
  207. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  208. return &value, err
  209. }
  210. func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
  211. var value IPv6Mreq
  212. vallen := _Socklen(SizeofIPv6Mreq)
  213. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  214. return &value, err
  215. }
  216. func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
  217. var value IPv6MTUInfo
  218. vallen := _Socklen(SizeofIPv6MTUInfo)
  219. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  220. return &value, err
  221. }
  222. func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
  223. var value ICMPv6Filter
  224. vallen := _Socklen(SizeofICMPv6Filter)
  225. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  226. return &value, err
  227. }
  228. func GetsockoptLinger(fd, level, opt int) (*Linger, error) {
  229. var linger Linger
  230. vallen := _Socklen(SizeofLinger)
  231. err := getsockopt(fd, level, opt, unsafe.Pointer(&linger), &vallen)
  232. return &linger, err
  233. }
  234. func GetsockoptTimeval(fd, level, opt int) (*Timeval, error) {
  235. var tv Timeval
  236. vallen := _Socklen(unsafe.Sizeof(tv))
  237. err := getsockopt(fd, level, opt, unsafe.Pointer(&tv), &vallen)
  238. return &tv, err
  239. }
  240. func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
  241. var rsa RawSockaddrAny
  242. var len _Socklen = SizeofSockaddrAny
  243. if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
  244. return
  245. }
  246. if rsa.Addr.Family != AF_UNSPEC {
  247. from, err = anyToSockaddr(fd, &rsa)
  248. }
  249. return
  250. }
  251. func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
  252. ptr, n, err := to.sockaddr()
  253. if err != nil {
  254. return err
  255. }
  256. return sendto(fd, p, flags, ptr, n)
  257. }
  258. func SetsockoptByte(fd, level, opt int, value byte) (err error) {
  259. return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
  260. }
  261. func SetsockoptInt(fd, level, opt int, value int) (err error) {
  262. var n = int32(value)
  263. return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
  264. }
  265. func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
  266. return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
  267. }
  268. func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
  269. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
  270. }
  271. func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
  272. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
  273. }
  274. func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
  275. return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
  276. }
  277. func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
  278. return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
  279. }
  280. func SetsockoptString(fd, level, opt int, s string) (err error) {
  281. return setsockopt(fd, level, opt, unsafe.Pointer(&[]byte(s)[0]), uintptr(len(s)))
  282. }
  283. func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
  284. return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
  285. }
  286. func Socket(domain, typ, proto int) (fd int, err error) {
  287. if domain == AF_INET6 && SocketDisableIPv6 {
  288. return -1, EAFNOSUPPORT
  289. }
  290. fd, err = socket(domain, typ, proto)
  291. return
  292. }
  293. func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
  294. var fdx [2]int32
  295. err = socketpair(domain, typ, proto, &fdx)
  296. if err == nil {
  297. fd[0] = int(fdx[0])
  298. fd[1] = int(fdx[1])
  299. }
  300. return
  301. }
  302. var ioSync int64
  303. func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
  304. func SetNonblock(fd int, nonblocking bool) (err error) {
  305. flag, err := fcntl(fd, F_GETFL, 0)
  306. if err != nil {
  307. return err
  308. }
  309. if nonblocking {
  310. flag |= O_NONBLOCK
  311. } else {
  312. flag &= ^O_NONBLOCK
  313. }
  314. _, err = fcntl(fd, F_SETFL, flag)
  315. return err
  316. }
  317. // Exec calls execve(2), which replaces the calling executable in the process
  318. // tree. argv0 should be the full path to an executable ("/bin/ls") and the
  319. // executable name should also be the first argument in argv (["ls", "-l"]).
  320. // envv are the environment variables that should be passed to the new
  321. // process (["USER=go", "PWD=/tmp"]).
  322. func Exec(argv0 string, argv []string, envv []string) error {
  323. return syscall.Exec(argv0, argv, envv)
  324. }