syscall_unix.go 9.6 KB

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