You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

628 lines
16 KiB

  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 netbsd openbsd
  5. // BSD system call wrappers shared by *BSD based systems
  6. // including OS X (Darwin) and FreeBSD. Like the other
  7. // syscall_*.go files it is compiled as Go code but also
  8. // used as input to mksyscall which parses the //sys
  9. // lines and generates system call stubs.
  10. package unix
  11. import (
  12. "runtime"
  13. "syscall"
  14. "unsafe"
  15. )
  16. /*
  17. * Wrapped
  18. */
  19. //sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error)
  20. //sysnb setgroups(ngid int, gid *_Gid_t) (err error)
  21. func Getgroups() (gids []int, err error) {
  22. n, err := getgroups(0, nil)
  23. if err != nil {
  24. return nil, err
  25. }
  26. if n == 0 {
  27. return nil, nil
  28. }
  29. // Sanity check group count. Max is 16 on BSD.
  30. if n < 0 || n > 1000 {
  31. return nil, EINVAL
  32. }
  33. a := make([]_Gid_t, n)
  34. n, err = getgroups(n, &a[0])
  35. if err != nil {
  36. return nil, err
  37. }
  38. gids = make([]int, n)
  39. for i, v := range a[0:n] {
  40. gids[i] = int(v)
  41. }
  42. return
  43. }
  44. func Setgroups(gids []int) (err error) {
  45. if len(gids) == 0 {
  46. return setgroups(0, nil)
  47. }
  48. a := make([]_Gid_t, len(gids))
  49. for i, v := range gids {
  50. a[i] = _Gid_t(v)
  51. }
  52. return setgroups(len(a), &a[0])
  53. }
  54. func ReadDirent(fd int, buf []byte) (n int, err error) {
  55. // Final argument is (basep *uintptr) and the syscall doesn't take nil.
  56. // 64 bits should be enough. (32 bits isn't even on 386). Since the
  57. // actual system call is getdirentries64, 64 is a good guess.
  58. // TODO(rsc): Can we use a single global basep for all calls?
  59. var base = (*uintptr)(unsafe.Pointer(new(uint64)))
  60. return Getdirentries(fd, buf, base)
  61. }
  62. // Wait status is 7 bits at bottom, either 0 (exited),
  63. // 0x7F (stopped), or a signal number that caused an exit.
  64. // The 0x80 bit is whether there was a core dump.
  65. // An extra number (exit code, signal causing a stop)
  66. // is in the high bits.
  67. type WaitStatus uint32
  68. const (
  69. mask = 0x7F
  70. core = 0x80
  71. shift = 8
  72. exited = 0
  73. stopped = 0x7F
  74. )
  75. func (w WaitStatus) Exited() bool { return w&mask == exited }
  76. func (w WaitStatus) ExitStatus() int {
  77. if w&mask != exited {
  78. return -1
  79. }
  80. return int(w >> shift)
  81. }
  82. func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
  83. func (w WaitStatus) Signal() syscall.Signal {
  84. sig := syscall.Signal(w & mask)
  85. if sig == stopped || sig == 0 {
  86. return -1
  87. }
  88. return sig
  89. }
  90. func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
  91. func (w WaitStatus) Stopped() bool { return w&mask == stopped && syscall.Signal(w>>shift) != SIGSTOP }
  92. func (w WaitStatus) Continued() bool { return w&mask == stopped && syscall.Signal(w>>shift) == SIGSTOP }
  93. func (w WaitStatus) StopSignal() syscall.Signal {
  94. if !w.Stopped() {
  95. return -1
  96. }
  97. return syscall.Signal(w>>shift) & 0xFF
  98. }
  99. func (w WaitStatus) TrapCause() int { return -1 }
  100. //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
  101. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
  102. var status _C_int
  103. wpid, err = wait4(pid, &status, options, rusage)
  104. if wstatus != nil {
  105. *wstatus = WaitStatus(status)
  106. }
  107. return
  108. }
  109. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
  110. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  111. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  112. //sysnb socket(domain int, typ int, proto int) (fd int, err error)
  113. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
  114. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
  115. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  116. //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  117. //sys Shutdown(s int, how int) (err error)
  118. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  119. if sa.Port < 0 || sa.Port > 0xFFFF {
  120. return nil, 0, EINVAL
  121. }
  122. sa.raw.Len = SizeofSockaddrInet4
  123. sa.raw.Family = AF_INET
  124. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  125. p[0] = byte(sa.Port >> 8)
  126. p[1] = byte(sa.Port)
  127. for i := 0; i < len(sa.Addr); i++ {
  128. sa.raw.Addr[i] = sa.Addr[i]
  129. }
  130. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  131. }
  132. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  133. if sa.Port < 0 || sa.Port > 0xFFFF {
  134. return nil, 0, EINVAL
  135. }
  136. sa.raw.Len = SizeofSockaddrInet6
  137. sa.raw.Family = AF_INET6
  138. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  139. p[0] = byte(sa.Port >> 8)
  140. p[1] = byte(sa.Port)
  141. sa.raw.Scope_id = sa.ZoneId
  142. for i := 0; i < len(sa.Addr); i++ {
  143. sa.raw.Addr[i] = sa.Addr[i]
  144. }
  145. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  146. }
  147. func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
  148. name := sa.Name
  149. n := len(name)
  150. if n >= len(sa.raw.Path) || n == 0 {
  151. return nil, 0, EINVAL
  152. }
  153. sa.raw.Len = byte(3 + n) // 2 for Family, Len; 1 for NUL
  154. sa.raw.Family = AF_UNIX
  155. for i := 0; i < n; i++ {
  156. sa.raw.Path[i] = int8(name[i])
  157. }
  158. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  159. }
  160. func (sa *SockaddrDatalink) sockaddr() (unsafe.Pointer, _Socklen, error) {
  161. if sa.Index == 0 {
  162. return nil, 0, EINVAL
  163. }
  164. sa.raw.Len = sa.Len
  165. sa.raw.Family = AF_LINK
  166. sa.raw.Index = sa.Index
  167. sa.raw.Type = sa.Type
  168. sa.raw.Nlen = sa.Nlen
  169. sa.raw.Alen = sa.Alen
  170. sa.raw.Slen = sa.Slen
  171. for i := 0; i < len(sa.raw.Data); i++ {
  172. sa.raw.Data[i] = sa.Data[i]
  173. }
  174. return unsafe.Pointer(&sa.raw), SizeofSockaddrDatalink, nil
  175. }
  176. func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
  177. switch rsa.Addr.Family {
  178. case AF_LINK:
  179. pp := (*RawSockaddrDatalink)(unsafe.Pointer(rsa))
  180. sa := new(SockaddrDatalink)
  181. sa.Len = pp.Len
  182. sa.Family = pp.Family
  183. sa.Index = pp.Index
  184. sa.Type = pp.Type
  185. sa.Nlen = pp.Nlen
  186. sa.Alen = pp.Alen
  187. sa.Slen = pp.Slen
  188. for i := 0; i < len(sa.Data); i++ {
  189. sa.Data[i] = pp.Data[i]
  190. }
  191. return sa, nil
  192. case AF_UNIX:
  193. pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
  194. if pp.Len < 2 || pp.Len > SizeofSockaddrUnix {
  195. return nil, EINVAL
  196. }
  197. sa := new(SockaddrUnix)
  198. // Some BSDs include the trailing NUL in the length, whereas
  199. // others do not. Work around this by subtracting the leading
  200. // family and len. The path is then scanned to see if a NUL
  201. // terminator still exists within the length.
  202. n := int(pp.Len) - 2 // subtract leading Family, Len
  203. for i := 0; i < n; i++ {
  204. if pp.Path[i] == 0 {
  205. // found early NUL; assume Len included the NUL
  206. // or was overestimating.
  207. n = i
  208. break
  209. }
  210. }
  211. bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
  212. sa.Name = string(bytes)
  213. return sa, nil
  214. case AF_INET:
  215. pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
  216. sa := new(SockaddrInet4)
  217. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  218. sa.Port = int(p[0])<<8 + int(p[1])
  219. for i := 0; i < len(sa.Addr); i++ {
  220. sa.Addr[i] = pp.Addr[i]
  221. }
  222. return sa, nil
  223. case AF_INET6:
  224. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  225. sa := new(SockaddrInet6)
  226. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  227. sa.Port = int(p[0])<<8 + int(p[1])
  228. sa.ZoneId = pp.Scope_id
  229. for i := 0; i < len(sa.Addr); i++ {
  230. sa.Addr[i] = pp.Addr[i]
  231. }
  232. return sa, nil
  233. }
  234. return nil, EAFNOSUPPORT
  235. }
  236. func Accept(fd int) (nfd int, sa Sockaddr, err error) {
  237. var rsa RawSockaddrAny
  238. var len _Socklen = SizeofSockaddrAny
  239. nfd, err = accept(fd, &rsa, &len)
  240. if err != nil {
  241. return
  242. }
  243. if runtime.GOOS == "darwin" && len == 0 {
  244. // Accepted socket has no address.
  245. // This is likely due to a bug in xnu kernels,
  246. // where instead of ECONNABORTED error socket
  247. // is accepted, but has no address.
  248. Close(nfd)
  249. return 0, nil, ECONNABORTED
  250. }
  251. sa, err = anyToSockaddr(&rsa)
  252. if err != nil {
  253. Close(nfd)
  254. nfd = 0
  255. }
  256. return
  257. }
  258. func Getsockname(fd int) (sa Sockaddr, err error) {
  259. var rsa RawSockaddrAny
  260. var len _Socklen = SizeofSockaddrAny
  261. if err = getsockname(fd, &rsa, &len); err != nil {
  262. return
  263. }
  264. // TODO(jsing): DragonFly has a "bug" (see issue 3349), which should be
  265. // reported upstream.
  266. if runtime.GOOS == "dragonfly" && rsa.Addr.Family == AF_UNSPEC && rsa.Addr.Len == 0 {
  267. rsa.Addr.Family = AF_UNIX
  268. rsa.Addr.Len = SizeofSockaddrUnix
  269. }
  270. return anyToSockaddr(&rsa)
  271. }
  272. //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
  273. func GetsockoptByte(fd, level, opt int) (value byte, err error) {
  274. var n byte
  275. vallen := _Socklen(1)
  276. err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
  277. return n, err
  278. }
  279. func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
  280. vallen := _Socklen(4)
  281. err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
  282. return value, err
  283. }
  284. func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
  285. var value IPMreq
  286. vallen := _Socklen(SizeofIPMreq)
  287. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  288. return &value, err
  289. }
  290. func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
  291. var value IPv6Mreq
  292. vallen := _Socklen(SizeofIPv6Mreq)
  293. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  294. return &value, err
  295. }
  296. func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
  297. var value IPv6MTUInfo
  298. vallen := _Socklen(SizeofIPv6MTUInfo)
  299. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  300. return &value, err
  301. }
  302. func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
  303. var value ICMPv6Filter
  304. vallen := _Socklen(SizeofICMPv6Filter)
  305. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  306. return &value, err
  307. }
  308. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
  309. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
  310. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
  311. func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
  312. var msg Msghdr
  313. var rsa RawSockaddrAny
  314. msg.Name = (*byte)(unsafe.Pointer(&rsa))
  315. msg.Namelen = uint32(SizeofSockaddrAny)
  316. var iov Iovec
  317. if len(p) > 0 {
  318. iov.Base = (*byte)(unsafe.Pointer(&p[0]))
  319. iov.SetLen(len(p))
  320. }
  321. var dummy byte
  322. if len(oob) > 0 {
  323. // receive at least one normal byte
  324. if len(p) == 0 {
  325. iov.Base = &dummy
  326. iov.SetLen(1)
  327. }
  328. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  329. msg.SetControllen(len(oob))
  330. }
  331. msg.Iov = &iov
  332. msg.Iovlen = 1
  333. if n, err = recvmsg(fd, &msg, flags); err != nil {
  334. return
  335. }
  336. oobn = int(msg.Controllen)
  337. recvflags = int(msg.Flags)
  338. // source address is only specified if the socket is unconnected
  339. if rsa.Addr.Family != AF_UNSPEC {
  340. from, err = anyToSockaddr(&rsa)
  341. }
  342. return
  343. }
  344. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
  345. func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
  346. _, err = SendmsgN(fd, p, oob, to, flags)
  347. return
  348. }
  349. func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
  350. var ptr unsafe.Pointer
  351. var salen _Socklen
  352. if to != nil {
  353. ptr, salen, err = to.sockaddr()
  354. if err != nil {
  355. return 0, err
  356. }
  357. }
  358. var msg Msghdr
  359. msg.Name = (*byte)(unsafe.Pointer(ptr))
  360. msg.Namelen = uint32(salen)
  361. var iov Iovec
  362. if len(p) > 0 {
  363. iov.Base = (*byte)(unsafe.Pointer(&p[0]))
  364. iov.SetLen(len(p))
  365. }
  366. var dummy byte
  367. if len(oob) > 0 {
  368. // send at least one normal byte
  369. if len(p) == 0 {
  370. iov.Base = &dummy
  371. iov.SetLen(1)
  372. }
  373. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  374. msg.SetControllen(len(oob))
  375. }
  376. msg.Iov = &iov
  377. msg.Iovlen = 1
  378. if n, err = sendmsg(fd, &msg, flags); err != nil {
  379. return 0, err
  380. }
  381. if len(oob) > 0 && len(p) == 0 {
  382. n = 0
  383. }
  384. return n, nil
  385. }
  386. //sys kevent(kq int, change unsafe.Pointer, nchange int, event unsafe.Pointer, nevent int, timeout *Timespec) (n int, err error)
  387. func Kevent(kq int, changes, events []Kevent_t, timeout *Timespec) (n int, err error) {
  388. var change, event unsafe.Pointer
  389. if len(changes) > 0 {
  390. change = unsafe.Pointer(&changes[0])
  391. }
  392. if len(events) > 0 {
  393. event = unsafe.Pointer(&events[0])
  394. }
  395. return kevent(kq, change, len(changes), event, len(events), timeout)
  396. }
  397. //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL
  398. // sysctlmib translates name to mib number and appends any additional args.
  399. func sysctlmib(name string, args ...int) ([]_C_int, error) {
  400. // Translate name to mib number.
  401. mib, err := nametomib(name)
  402. if err != nil {
  403. return nil, err
  404. }
  405. for _, a := range args {
  406. mib = append(mib, _C_int(a))
  407. }
  408. return mib, nil
  409. }
  410. func Sysctl(name string) (string, error) {
  411. return SysctlArgs(name)
  412. }
  413. func SysctlArgs(name string, args ...int) (string, error) {
  414. mib, err := sysctlmib(name, args...)
  415. if err != nil {
  416. return "", err
  417. }
  418. // Find size.
  419. n := uintptr(0)
  420. if err := sysctl(mib, nil, &n, nil, 0); err != nil {
  421. return "", err
  422. }
  423. if n == 0 {
  424. return "", nil
  425. }
  426. // Read into buffer of that size.
  427. buf := make([]byte, n)
  428. if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
  429. return "", err
  430. }
  431. // Throw away terminating NUL.
  432. if n > 0 && buf[n-1] == '\x00' {
  433. n--
  434. }
  435. return string(buf[0:n]), nil
  436. }
  437. func SysctlUint32(name string) (uint32, error) {
  438. return SysctlUint32Args(name)
  439. }
  440. func SysctlUint32Args(name string, args ...int) (uint32, error) {
  441. mib, err := sysctlmib(name, args...)
  442. if err != nil {
  443. return 0, err
  444. }
  445. n := uintptr(4)
  446. buf := make([]byte, 4)
  447. if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
  448. return 0, err
  449. }
  450. if n != 4 {
  451. return 0, EIO
  452. }
  453. return *(*uint32)(unsafe.Pointer(&buf[0])), nil
  454. }
  455. func SysctlUint64(name string, args ...int) (uint64, error) {
  456. mib, err := sysctlmib(name, args...)
  457. if err != nil {
  458. return 0, err
  459. }
  460. n := uintptr(8)
  461. buf := make([]byte, 8)
  462. if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
  463. return 0, err
  464. }
  465. if n != 8 {
  466. return 0, EIO
  467. }
  468. return *(*uint64)(unsafe.Pointer(&buf[0])), nil
  469. }
  470. func SysctlRaw(name string, args ...int) ([]byte, error) {
  471. mib, err := sysctlmib(name, args...)
  472. if err != nil {
  473. return nil, err
  474. }
  475. // Find size.
  476. n := uintptr(0)
  477. if err := sysctl(mib, nil, &n, nil, 0); err != nil {
  478. return nil, err
  479. }
  480. if n == 0 {
  481. return nil, nil
  482. }
  483. // Read into buffer of that size.
  484. buf := make([]byte, n)
  485. if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
  486. return nil, err
  487. }
  488. // The actual call may return less than the original reported required
  489. // size so ensure we deal with that.
  490. return buf[:n], nil
  491. }
  492. //sys utimes(path string, timeval *[2]Timeval) (err error)
  493. func Utimes(path string, tv []Timeval) error {
  494. if tv == nil {
  495. return utimes(path, nil)
  496. }
  497. if len(tv) != 2 {
  498. return EINVAL
  499. }
  500. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  501. }
  502. func UtimesNano(path string, ts []Timespec) error {
  503. if ts == nil {
  504. return utimes(path, nil)
  505. }
  506. // TODO: The BSDs can do utimensat with SYS_UTIMENSAT but it
  507. // isn't supported by darwin so this uses utimes instead
  508. if len(ts) != 2 {
  509. return EINVAL
  510. }
  511. // Not as efficient as it could be because Timespec and
  512. // Timeval have different types in the different OSes
  513. tv := [2]Timeval{
  514. NsecToTimeval(TimespecToNsec(ts[0])),
  515. NsecToTimeval(TimespecToNsec(ts[1])),
  516. }
  517. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  518. }
  519. //sys futimes(fd int, timeval *[2]Timeval) (err error)
  520. func Futimes(fd int, tv []Timeval) error {
  521. if tv == nil {
  522. return futimes(fd, nil)
  523. }
  524. if len(tv) != 2 {
  525. return EINVAL
  526. }
  527. return futimes(fd, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  528. }
  529. //sys fcntl(fd int, cmd int, arg int) (val int, err error)
  530. // TODO: wrap
  531. // Acct(name nil-string) (err error)
  532. // Gethostuuid(uuid *byte, timeout *Timespec) (err error)
  533. // Madvise(addr *byte, len int, behav int) (err error)
  534. // Mprotect(addr *byte, len int, prot int) (err error)
  535. // Msync(addr *byte, len int, flags int) (err error)
  536. // Ptrace(req int, pid int, addr uintptr, data int) (ret uintptr, err error)
  537. var mapper = &mmapper{
  538. active: make(map[*byte][]byte),
  539. mmap: mmap,
  540. munmap: munmap,
  541. }
  542. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  543. return mapper.Mmap(fd, offset, length, prot, flags)
  544. }
  545. func Munmap(b []byte) (err error) {
  546. return mapper.Munmap(b)
  547. }