syscall_bsd.go 15 KB

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