syscall_unix.go 11 KB

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