syscall_bsd.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 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. const ImplementsGetwd = true
  17. func Getwd() (string, error) {
  18. var buf [PathMax]byte
  19. _, err := Getcwd(buf[0:])
  20. if err != nil {
  21. return "", err
  22. }
  23. n := clen(buf[:])
  24. if n < 1 {
  25. return "", EINVAL
  26. }
  27. return string(buf[:n]), nil
  28. }
  29. /*
  30. * Wrapped
  31. */
  32. //sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error)
  33. //sysnb setgroups(ngid int, gid *_Gid_t) (err error)
  34. func Getgroups() (gids []int, err error) {
  35. n, err := getgroups(0, nil)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if n == 0 {
  40. return nil, nil
  41. }
  42. // Sanity check group count. Max is 16 on BSD.
  43. if n < 0 || n > 1000 {
  44. return nil, EINVAL
  45. }
  46. a := make([]_Gid_t, n)
  47. n, err = getgroups(n, &a[0])
  48. if err != nil {
  49. return nil, err
  50. }
  51. gids = make([]int, n)
  52. for i, v := range a[0:n] {
  53. gids[i] = int(v)
  54. }
  55. return
  56. }
  57. func Setgroups(gids []int) (err error) {
  58. if len(gids) == 0 {
  59. return setgroups(0, nil)
  60. }
  61. a := make([]_Gid_t, len(gids))
  62. for i, v := range gids {
  63. a[i] = _Gid_t(v)
  64. }
  65. return setgroups(len(a), &a[0])
  66. }
  67. // Wait status is 7 bits at bottom, either 0 (exited),
  68. // 0x7F (stopped), or a signal number that caused an exit.
  69. // The 0x80 bit is whether there was a core dump.
  70. // An extra number (exit code, signal causing a stop)
  71. // is in the high bits.
  72. type WaitStatus uint32
  73. const (
  74. mask = 0x7F
  75. core = 0x80
  76. shift = 8
  77. exited = 0
  78. killed = 9
  79. stopped = 0x7F
  80. )
  81. func (w WaitStatus) Exited() bool { return w&mask == exited }
  82. func (w WaitStatus) ExitStatus() int {
  83. if w&mask != exited {
  84. return -1
  85. }
  86. return int(w >> shift)
  87. }
  88. func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
  89. func (w WaitStatus) Signal() syscall.Signal {
  90. sig := syscall.Signal(w & mask)
  91. if sig == stopped || sig == 0 {
  92. return -1
  93. }
  94. return sig
  95. }
  96. func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
  97. func (w WaitStatus) Stopped() bool { return w&mask == stopped && syscall.Signal(w>>shift) != SIGSTOP }
  98. func (w WaitStatus) Killed() bool { return w&mask == killed && syscall.Signal(w>>shift) != SIGKILL }
  99. func (w WaitStatus) Continued() bool { return w&mask == stopped && syscall.Signal(w>>shift) == SIGSTOP }
  100. func (w WaitStatus) StopSignal() syscall.Signal {
  101. if !w.Stopped() {
  102. return -1
  103. }
  104. return syscall.Signal(w>>shift) & 0xFF
  105. }
  106. func (w WaitStatus) TrapCause() int { return -1 }
  107. //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
  108. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
  109. var status _C_int
  110. wpid, err = wait4(pid, &status, options, rusage)
  111. if wstatus != nil {
  112. *wstatus = WaitStatus(status)
  113. }
  114. return
  115. }
  116. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
  117. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  118. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  119. //sysnb socket(domain int, typ int, proto int) (fd int, err error)
  120. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
  121. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
  122. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  123. //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  124. //sys Shutdown(s int, how int) (err error)
  125. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  126. if sa.Port < 0 || sa.Port > 0xFFFF {
  127. return nil, 0, EINVAL
  128. }
  129. sa.raw.Len = SizeofSockaddrInet4
  130. sa.raw.Family = AF_INET
  131. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  132. p[0] = byte(sa.Port >> 8)
  133. p[1] = byte(sa.Port)
  134. sa.raw.Addr = sa.Addr
  135. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  136. }
  137. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  138. if sa.Port < 0 || sa.Port > 0xFFFF {
  139. return nil, 0, EINVAL
  140. }
  141. sa.raw.Len = SizeofSockaddrInet6
  142. sa.raw.Family = AF_INET6
  143. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  144. p[0] = byte(sa.Port >> 8)
  145. p[1] = byte(sa.Port)
  146. sa.raw.Scope_id = sa.ZoneId
  147. sa.raw.Addr = sa.Addr
  148. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  149. }
  150. func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
  151. name := sa.Name
  152. n := len(name)
  153. if n >= len(sa.raw.Path) || n == 0 {
  154. return nil, 0, EINVAL
  155. }
  156. sa.raw.Len = byte(3 + n) // 2 for Family, Len; 1 for NUL
  157. sa.raw.Family = AF_UNIX
  158. for i := 0; i < n; i++ {
  159. sa.raw.Path[i] = int8(name[i])
  160. }
  161. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  162. }
  163. func (sa *SockaddrDatalink) sockaddr() (unsafe.Pointer, _Socklen, error) {
  164. if sa.Index == 0 {
  165. return nil, 0, EINVAL
  166. }
  167. sa.raw.Len = sa.Len
  168. sa.raw.Family = AF_LINK
  169. sa.raw.Index = sa.Index
  170. sa.raw.Type = sa.Type
  171. sa.raw.Nlen = sa.Nlen
  172. sa.raw.Alen = sa.Alen
  173. sa.raw.Slen = sa.Slen
  174. sa.raw.Data = sa.Data
  175. return unsafe.Pointer(&sa.raw), SizeofSockaddrDatalink, nil
  176. }
  177. func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
  178. switch rsa.Addr.Family {
  179. case AF_LINK:
  180. pp := (*RawSockaddrDatalink)(unsafe.Pointer(rsa))
  181. sa := new(SockaddrDatalink)
  182. sa.Len = pp.Len
  183. sa.Family = pp.Family
  184. sa.Index = pp.Index
  185. sa.Type = pp.Type
  186. sa.Nlen = pp.Nlen
  187. sa.Alen = pp.Alen
  188. sa.Slen = pp.Slen
  189. sa.Data = pp.Data
  190. return sa, nil
  191. case AF_UNIX:
  192. pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
  193. if pp.Len < 2 || pp.Len > SizeofSockaddrUnix {
  194. return nil, EINVAL
  195. }
  196. sa := new(SockaddrUnix)
  197. // Some BSDs include the trailing NUL in the length, whereas
  198. // others do not. Work around this by subtracting the leading
  199. // family and len. The path is then scanned to see if a NUL
  200. // terminator still exists within the length.
  201. n := int(pp.Len) - 2 // subtract leading Family, Len
  202. for i := 0; i < n; i++ {
  203. if pp.Path[i] == 0 {
  204. // found early NUL; assume Len included the NUL
  205. // or was overestimating.
  206. n = i
  207. break
  208. }
  209. }
  210. sa.Name = string(unsafe.Slice((*byte)(unsafe.Pointer(&pp.Path[0])), n))
  211. return sa, nil
  212. case AF_INET:
  213. pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
  214. sa := new(SockaddrInet4)
  215. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  216. sa.Port = int(p[0])<<8 + int(p[1])
  217. sa.Addr = pp.Addr
  218. return sa, nil
  219. case AF_INET6:
  220. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  221. sa := new(SockaddrInet6)
  222. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  223. sa.Port = int(p[0])<<8 + int(p[1])
  224. sa.ZoneId = pp.Scope_id
  225. sa.Addr = pp.Addr
  226. return sa, nil
  227. }
  228. return anyToSockaddrGOOS(fd, rsa)
  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" || runtime.GOOS == "ios") && 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 ByteSliceToString(buf[:vallen]), 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 recvmsgRaw(fd int, iov []Iovec, oob []byte, flags int, rsa *RawSockaddrAny) (n, oobn int, recvflags int, err error) {
  282. var msg Msghdr
  283. msg.Name = (*byte)(unsafe.Pointer(rsa))
  284. msg.Namelen = uint32(SizeofSockaddrAny)
  285. var dummy byte
  286. if len(oob) > 0 {
  287. // receive at least one normal byte
  288. if emptyIovecs(iov) {
  289. var iova [1]Iovec
  290. iova[0].Base = &dummy
  291. iova[0].SetLen(1)
  292. iov = iova[:]
  293. }
  294. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  295. msg.SetControllen(len(oob))
  296. }
  297. if len(iov) > 0 {
  298. msg.Iov = &iov[0]
  299. msg.SetIovlen(len(iov))
  300. }
  301. if n, err = recvmsg(fd, &msg, flags); err != nil {
  302. return
  303. }
  304. oobn = int(msg.Controllen)
  305. recvflags = int(msg.Flags)
  306. return
  307. }
  308. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
  309. func sendmsgN(fd int, iov []Iovec, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) {
  310. var msg Msghdr
  311. msg.Name = (*byte)(unsafe.Pointer(ptr))
  312. msg.Namelen = uint32(salen)
  313. var dummy byte
  314. var empty bool
  315. if len(oob) > 0 {
  316. // send at least one normal byte
  317. empty = emptyIovecs(iov)
  318. if empty {
  319. var iova [1]Iovec
  320. iova[0].Base = &dummy
  321. iova[0].SetLen(1)
  322. iov = iova[:]
  323. }
  324. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  325. msg.SetControllen(len(oob))
  326. }
  327. if len(iov) > 0 {
  328. msg.Iov = &iov[0]
  329. msg.SetIovlen(len(iov))
  330. }
  331. if n, err = sendmsg(fd, &msg, flags); err != nil {
  332. return 0, err
  333. }
  334. if len(oob) > 0 && empty {
  335. n = 0
  336. }
  337. return n, nil
  338. }
  339. //sys kevent(kq int, change unsafe.Pointer, nchange int, event unsafe.Pointer, nevent int, timeout *Timespec) (n int, err error)
  340. func Kevent(kq int, changes, events []Kevent_t, timeout *Timespec) (n int, err error) {
  341. var change, event unsafe.Pointer
  342. if len(changes) > 0 {
  343. change = unsafe.Pointer(&changes[0])
  344. }
  345. if len(events) > 0 {
  346. event = unsafe.Pointer(&events[0])
  347. }
  348. return kevent(kq, change, len(changes), event, len(events), timeout)
  349. }
  350. // sysctlmib translates name to mib number and appends any additional args.
  351. func sysctlmib(name string, args ...int) ([]_C_int, error) {
  352. // Translate name to mib number.
  353. mib, err := nametomib(name)
  354. if err != nil {
  355. return nil, err
  356. }
  357. for _, a := range args {
  358. mib = append(mib, _C_int(a))
  359. }
  360. return mib, nil
  361. }
  362. func Sysctl(name string) (string, error) {
  363. return SysctlArgs(name)
  364. }
  365. func SysctlArgs(name string, args ...int) (string, error) {
  366. buf, err := SysctlRaw(name, args...)
  367. if err != nil {
  368. return "", err
  369. }
  370. n := len(buf)
  371. // Throw away terminating NUL.
  372. if n > 0 && buf[n-1] == '\x00' {
  373. n--
  374. }
  375. return string(buf[0:n]), nil
  376. }
  377. func SysctlUint32(name string) (uint32, error) {
  378. return SysctlUint32Args(name)
  379. }
  380. func SysctlUint32Args(name string, args ...int) (uint32, error) {
  381. mib, err := sysctlmib(name, args...)
  382. if err != nil {
  383. return 0, err
  384. }
  385. n := uintptr(4)
  386. buf := make([]byte, 4)
  387. if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
  388. return 0, err
  389. }
  390. if n != 4 {
  391. return 0, EIO
  392. }
  393. return *(*uint32)(unsafe.Pointer(&buf[0])), nil
  394. }
  395. func SysctlUint64(name string, args ...int) (uint64, error) {
  396. mib, err := sysctlmib(name, args...)
  397. if err != nil {
  398. return 0, err
  399. }
  400. n := uintptr(8)
  401. buf := make([]byte, 8)
  402. if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
  403. return 0, err
  404. }
  405. if n != 8 {
  406. return 0, EIO
  407. }
  408. return *(*uint64)(unsafe.Pointer(&buf[0])), nil
  409. }
  410. func SysctlRaw(name string, args ...int) ([]byte, error) {
  411. mib, err := sysctlmib(name, args...)
  412. if err != nil {
  413. return nil, err
  414. }
  415. // Find size.
  416. n := uintptr(0)
  417. if err := sysctl(mib, nil, &n, nil, 0); err != nil {
  418. return nil, err
  419. }
  420. if n == 0 {
  421. return nil, nil
  422. }
  423. // Read into buffer of that size.
  424. buf := make([]byte, n)
  425. if err := sysctl(mib, &buf[0], &n, nil, 0); err != nil {
  426. return nil, err
  427. }
  428. // The actual call may return less than the original reported required
  429. // size so ensure we deal with that.
  430. return buf[:n], nil
  431. }
  432. func SysctlClockinfo(name string) (*Clockinfo, error) {
  433. mib, err := sysctlmib(name)
  434. if err != nil {
  435. return nil, err
  436. }
  437. n := uintptr(SizeofClockinfo)
  438. var ci Clockinfo
  439. if err := sysctl(mib, (*byte)(unsafe.Pointer(&ci)), &n, nil, 0); err != nil {
  440. return nil, err
  441. }
  442. if n != SizeofClockinfo {
  443. return nil, EIO
  444. }
  445. return &ci, nil
  446. }
  447. func SysctlTimeval(name string) (*Timeval, error) {
  448. mib, err := sysctlmib(name)
  449. if err != nil {
  450. return nil, err
  451. }
  452. var tv Timeval
  453. n := uintptr(unsafe.Sizeof(tv))
  454. if err := sysctl(mib, (*byte)(unsafe.Pointer(&tv)), &n, nil, 0); err != nil {
  455. return nil, err
  456. }
  457. if n != unsafe.Sizeof(tv) {
  458. return nil, EIO
  459. }
  460. return &tv, nil
  461. }
  462. //sys utimes(path string, timeval *[2]Timeval) (err error)
  463. func Utimes(path string, tv []Timeval) error {
  464. if tv == nil {
  465. return utimes(path, nil)
  466. }
  467. if len(tv) != 2 {
  468. return EINVAL
  469. }
  470. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  471. }
  472. func UtimesNano(path string, ts []Timespec) error {
  473. if ts == nil {
  474. err := utimensat(AT_FDCWD, path, nil, 0)
  475. if err != ENOSYS {
  476. return err
  477. }
  478. return utimes(path, nil)
  479. }
  480. if len(ts) != 2 {
  481. return EINVAL
  482. }
  483. err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
  484. if err != ENOSYS {
  485. return err
  486. }
  487. // Not as efficient as it could be because Timespec and
  488. // Timeval have different types in the different OSes
  489. tv := [2]Timeval{
  490. NsecToTimeval(TimespecToNsec(ts[0])),
  491. NsecToTimeval(TimespecToNsec(ts[1])),
  492. }
  493. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  494. }
  495. func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
  496. if ts == nil {
  497. return utimensat(dirfd, path, nil, flags)
  498. }
  499. if len(ts) != 2 {
  500. return EINVAL
  501. }
  502. return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
  503. }
  504. //sys futimes(fd int, timeval *[2]Timeval) (err error)
  505. func Futimes(fd int, tv []Timeval) error {
  506. if tv == nil {
  507. return futimes(fd, nil)
  508. }
  509. if len(tv) != 2 {
  510. return EINVAL
  511. }
  512. return futimes(fd, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  513. }
  514. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  515. func Poll(fds []PollFd, timeout int) (n int, err error) {
  516. if len(fds) == 0 {
  517. return poll(nil, 0, timeout)
  518. }
  519. return poll(&fds[0], len(fds), timeout)
  520. }
  521. // TODO: wrap
  522. // Acct(name nil-string) (err error)
  523. // Gethostuuid(uuid *byte, timeout *Timespec) (err error)
  524. // Ptrace(req int, pid int, addr uintptr, data int) (ret uintptr, err error)
  525. //sys Madvise(b []byte, behav int) (err error)
  526. //sys Mlock(b []byte) (err error)
  527. //sys Mlockall(flags int) (err error)
  528. //sys Mprotect(b []byte, prot int) (err error)
  529. //sys Msync(b []byte, flags int) (err error)
  530. //sys Munlock(b []byte) (err error)
  531. //sys Munlockall() (err error)