syscall_unix.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
  5. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
  6. package unix
  7. import (
  8. "bytes"
  9. "sort"
  10. "sync"
  11. "syscall"
  12. "unsafe"
  13. "golang.org/x/sys/internal/unsafeheader"
  14. )
  15. var (
  16. Stdin = 0
  17. Stdout = 1
  18. Stderr = 2
  19. )
  20. // Do the interface allocations only once for common
  21. // Errno values.
  22. var (
  23. errEAGAIN error = syscall.EAGAIN
  24. errEINVAL error = syscall.EINVAL
  25. errENOENT error = syscall.ENOENT
  26. )
  27. var (
  28. signalNameMapOnce sync.Once
  29. signalNameMap map[string]syscall.Signal
  30. )
  31. // errnoErr returns common boxed Errno values, to prevent
  32. // allocations at runtime.
  33. func errnoErr(e syscall.Errno) error {
  34. switch e {
  35. case 0:
  36. return nil
  37. case EAGAIN:
  38. return errEAGAIN
  39. case EINVAL:
  40. return errEINVAL
  41. case ENOENT:
  42. return errENOENT
  43. }
  44. return e
  45. }
  46. // ErrnoName returns the error name for error number e.
  47. func ErrnoName(e syscall.Errno) string {
  48. i := sort.Search(len(errorList), func(i int) bool {
  49. return errorList[i].num >= e
  50. })
  51. if i < len(errorList) && errorList[i].num == e {
  52. return errorList[i].name
  53. }
  54. return ""
  55. }
  56. // SignalName returns the signal name for signal number s.
  57. func SignalName(s syscall.Signal) string {
  58. i := sort.Search(len(signalList), func(i int) bool {
  59. return signalList[i].num >= s
  60. })
  61. if i < len(signalList) && signalList[i].num == s {
  62. return signalList[i].name
  63. }
  64. return ""
  65. }
  66. // SignalNum returns the syscall.Signal for signal named s,
  67. // or 0 if a signal with such name is not found.
  68. // The signal name should start with "SIG".
  69. func SignalNum(s string) syscall.Signal {
  70. signalNameMapOnce.Do(func() {
  71. signalNameMap = make(map[string]syscall.Signal, len(signalList))
  72. for _, signal := range signalList {
  73. signalNameMap[signal.name] = signal.num
  74. }
  75. })
  76. return signalNameMap[s]
  77. }
  78. // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
  79. func clen(n []byte) int {
  80. i := bytes.IndexByte(n, 0)
  81. if i == -1 {
  82. i = len(n)
  83. }
  84. return i
  85. }
  86. // Mmap manager, for use by operating system-specific implementations.
  87. type mmapper struct {
  88. sync.Mutex
  89. active map[*byte][]byte // active mappings; key is last byte in mapping
  90. mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
  91. munmap func(addr uintptr, length uintptr) error
  92. }
  93. func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  94. if length <= 0 {
  95. return nil, EINVAL
  96. }
  97. // Map the requested memory.
  98. addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
  99. if errno != nil {
  100. return nil, errno
  101. }
  102. // Use unsafe to convert addr into a []byte.
  103. var b []byte
  104. hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b))
  105. hdr.Data = unsafe.Pointer(addr)
  106. hdr.Cap = length
  107. hdr.Len = length
  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 Send(s int, buf []byte, flags int) (err error) {
  274. return sendto(s, buf, flags, nil, 0)
  275. }
  276. func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
  277. ptr, n, err := to.sockaddr()
  278. if err != nil {
  279. return err
  280. }
  281. return sendto(fd, p, flags, ptr, n)
  282. }
  283. func SetsockoptByte(fd, level, opt int, value byte) (err error) {
  284. return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
  285. }
  286. func SetsockoptInt(fd, level, opt int, value int) (err error) {
  287. var n = int32(value)
  288. return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
  289. }
  290. func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
  291. return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
  292. }
  293. func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
  294. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
  295. }
  296. func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
  297. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
  298. }
  299. func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
  300. return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
  301. }
  302. func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
  303. return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
  304. }
  305. func SetsockoptString(fd, level, opt int, s string) (err error) {
  306. var p unsafe.Pointer
  307. if len(s) > 0 {
  308. p = unsafe.Pointer(&[]byte(s)[0])
  309. }
  310. return setsockopt(fd, level, opt, p, uintptr(len(s)))
  311. }
  312. func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
  313. return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
  314. }
  315. func SetsockoptUint64(fd, level, opt int, value uint64) (err error) {
  316. return setsockopt(fd, level, opt, unsafe.Pointer(&value), 8)
  317. }
  318. func Socket(domain, typ, proto int) (fd int, err error) {
  319. if domain == AF_INET6 && SocketDisableIPv6 {
  320. return -1, EAFNOSUPPORT
  321. }
  322. fd, err = socket(domain, typ, proto)
  323. return
  324. }
  325. func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
  326. var fdx [2]int32
  327. err = socketpair(domain, typ, proto, &fdx)
  328. if err == nil {
  329. fd[0] = int(fdx[0])
  330. fd[1] = int(fdx[1])
  331. }
  332. return
  333. }
  334. var ioSync int64
  335. func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
  336. func SetNonblock(fd int, nonblocking bool) (err error) {
  337. flag, err := fcntl(fd, F_GETFL, 0)
  338. if err != nil {
  339. return err
  340. }
  341. if nonblocking {
  342. flag |= O_NONBLOCK
  343. } else {
  344. flag &= ^O_NONBLOCK
  345. }
  346. _, err = fcntl(fd, F_SETFL, flag)
  347. return err
  348. }
  349. // Exec calls execve(2), which replaces the calling executable in the process
  350. // tree. argv0 should be the full path to an executable ("/bin/ls") and the
  351. // executable name should also be the first argument in argv (["ls", "-l"]).
  352. // envv are the environment variables that should be passed to the new
  353. // process (["USER=go", "PWD=/tmp"]).
  354. func Exec(argv0 string, argv []string, envv []string) error {
  355. return syscall.Exec(argv0, argv, envv)
  356. }
  357. // Lutimes sets the access and modification times tv on path. If path refers to
  358. // a symlink, it is not dereferenced and the timestamps are set on the symlink.
  359. // If tv is nil, the access and modification times are set to the current time.
  360. // Otherwise tv must contain exactly 2 elements, with access time as the first
  361. // element and modification time as the second element.
  362. func Lutimes(path string, tv []Timeval) error {
  363. if tv == nil {
  364. return UtimesNanoAt(AT_FDCWD, path, nil, AT_SYMLINK_NOFOLLOW)
  365. }
  366. if len(tv) != 2 {
  367. return EINVAL
  368. }
  369. ts := []Timespec{
  370. NsecToTimespec(TimevalToNsec(tv[0])),
  371. NsecToTimespec(TimevalToNsec(tv[1])),
  372. }
  373. return UtimesNanoAt(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW)
  374. }