syscall_solaris.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. // Solaris system calls.
  5. // This file is compiled as ordinary Go code,
  6. // but it is also input to mksyscall,
  7. // which parses the //sys lines and generates system call stubs.
  8. // Note that sometimes we use a lowercase //sys name and wrap
  9. // it in our own nicer implementation, either here or in
  10. // syscall_solaris.go or syscall_unix.go.
  11. package unix
  12. import (
  13. "fmt"
  14. "os"
  15. "runtime"
  16. "sync"
  17. "syscall"
  18. "unsafe"
  19. )
  20. // Implemented in runtime/syscall_solaris.go.
  21. type syscallFunc uintptr
  22. func rawSysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
  23. func sysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
  24. // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
  25. type SockaddrDatalink struct {
  26. Family uint16
  27. Index uint16
  28. Type uint8
  29. Nlen uint8
  30. Alen uint8
  31. Slen uint8
  32. Data [244]int8
  33. raw RawSockaddrDatalink
  34. }
  35. func direntIno(buf []byte) (uint64, bool) {
  36. return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
  37. }
  38. func direntReclen(buf []byte) (uint64, bool) {
  39. return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
  40. }
  41. func direntNamlen(buf []byte) (uint64, bool) {
  42. reclen, ok := direntReclen(buf)
  43. if !ok {
  44. return 0, false
  45. }
  46. return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
  47. }
  48. //sysnb pipe(p *[2]_C_int) (n int, err error)
  49. func Pipe(p []int) (err error) {
  50. if len(p) != 2 {
  51. return EINVAL
  52. }
  53. var pp [2]_C_int
  54. n, err := pipe(&pp)
  55. if n != 0 {
  56. return err
  57. }
  58. p[0] = int(pp[0])
  59. p[1] = int(pp[1])
  60. return nil
  61. }
  62. //sysnb pipe2(p *[2]_C_int, flags int) (err error)
  63. func Pipe2(p []int, flags int) error {
  64. if len(p) != 2 {
  65. return EINVAL
  66. }
  67. var pp [2]_C_int
  68. err := pipe2(&pp, flags)
  69. p[0] = int(pp[0])
  70. p[1] = int(pp[1])
  71. return err
  72. }
  73. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  74. if sa.Port < 0 || sa.Port > 0xFFFF {
  75. return nil, 0, EINVAL
  76. }
  77. sa.raw.Family = AF_INET
  78. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  79. p[0] = byte(sa.Port >> 8)
  80. p[1] = byte(sa.Port)
  81. for i := 0; i < len(sa.Addr); i++ {
  82. sa.raw.Addr[i] = sa.Addr[i]
  83. }
  84. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
  85. }
  86. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  87. if sa.Port < 0 || sa.Port > 0xFFFF {
  88. return nil, 0, EINVAL
  89. }
  90. sa.raw.Family = AF_INET6
  91. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  92. p[0] = byte(sa.Port >> 8)
  93. p[1] = byte(sa.Port)
  94. sa.raw.Scope_id = sa.ZoneId
  95. for i := 0; i < len(sa.Addr); i++ {
  96. sa.raw.Addr[i] = sa.Addr[i]
  97. }
  98. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
  99. }
  100. func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
  101. name := sa.Name
  102. n := len(name)
  103. if n >= len(sa.raw.Path) {
  104. return nil, 0, EINVAL
  105. }
  106. sa.raw.Family = AF_UNIX
  107. for i := 0; i < n; i++ {
  108. sa.raw.Path[i] = int8(name[i])
  109. }
  110. // length is family (uint16), name, NUL.
  111. sl := _Socklen(2)
  112. if n > 0 {
  113. sl += _Socklen(n) + 1
  114. }
  115. if sa.raw.Path[0] == '@' {
  116. sa.raw.Path[0] = 0
  117. // Don't count trailing NUL for abstract address.
  118. sl--
  119. }
  120. return unsafe.Pointer(&sa.raw), sl, nil
  121. }
  122. //sys getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getsockname
  123. func Getsockname(fd int) (sa Sockaddr, err error) {
  124. var rsa RawSockaddrAny
  125. var len _Socklen = SizeofSockaddrAny
  126. if err = getsockname(fd, &rsa, &len); err != nil {
  127. return
  128. }
  129. return anyToSockaddr(fd, &rsa)
  130. }
  131. // GetsockoptString returns the string value of the socket option opt for the
  132. // socket associated with fd at the given socket level.
  133. func GetsockoptString(fd, level, opt int) (string, error) {
  134. buf := make([]byte, 256)
  135. vallen := _Socklen(len(buf))
  136. err := getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen)
  137. if err != nil {
  138. return "", err
  139. }
  140. return string(buf[:vallen-1]), nil
  141. }
  142. const ImplementsGetwd = true
  143. //sys Getcwd(buf []byte) (n int, err error)
  144. func Getwd() (wd string, err error) {
  145. var buf [PathMax]byte
  146. // Getcwd will return an error if it failed for any reason.
  147. _, err = Getcwd(buf[0:])
  148. if err != nil {
  149. return "", err
  150. }
  151. n := clen(buf[:])
  152. if n < 1 {
  153. return "", EINVAL
  154. }
  155. return string(buf[:n]), nil
  156. }
  157. /*
  158. * Wrapped
  159. */
  160. //sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error)
  161. //sysnb setgroups(ngid int, gid *_Gid_t) (err error)
  162. func Getgroups() (gids []int, err error) {
  163. n, err := getgroups(0, nil)
  164. // Check for error and sanity check group count. Newer versions of
  165. // Solaris allow up to 1024 (NGROUPS_MAX).
  166. if n < 0 || n > 1024 {
  167. if err != nil {
  168. return nil, err
  169. }
  170. return nil, EINVAL
  171. } else if n == 0 {
  172. return nil, nil
  173. }
  174. a := make([]_Gid_t, n)
  175. n, err = getgroups(n, &a[0])
  176. if n == -1 {
  177. return nil, err
  178. }
  179. gids = make([]int, n)
  180. for i, v := range a[0:n] {
  181. gids[i] = int(v)
  182. }
  183. return
  184. }
  185. func Setgroups(gids []int) (err error) {
  186. if len(gids) == 0 {
  187. return setgroups(0, nil)
  188. }
  189. a := make([]_Gid_t, len(gids))
  190. for i, v := range gids {
  191. a[i] = _Gid_t(v)
  192. }
  193. return setgroups(len(a), &a[0])
  194. }
  195. // ReadDirent reads directory entries from fd and writes them into buf.
  196. func ReadDirent(fd int, buf []byte) (n int, err error) {
  197. // Final argument is (basep *uintptr) and the syscall doesn't take nil.
  198. // TODO(rsc): Can we use a single global basep for all calls?
  199. return Getdents(fd, buf, new(uintptr))
  200. }
  201. // Wait status is 7 bits at bottom, either 0 (exited),
  202. // 0x7F (stopped), or a signal number that caused an exit.
  203. // The 0x80 bit is whether there was a core dump.
  204. // An extra number (exit code, signal causing a stop)
  205. // is in the high bits.
  206. type WaitStatus uint32
  207. const (
  208. mask = 0x7F
  209. core = 0x80
  210. shift = 8
  211. exited = 0
  212. stopped = 0x7F
  213. )
  214. func (w WaitStatus) Exited() bool { return w&mask == exited }
  215. func (w WaitStatus) ExitStatus() int {
  216. if w&mask != exited {
  217. return -1
  218. }
  219. return int(w >> shift)
  220. }
  221. func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
  222. func (w WaitStatus) Signal() syscall.Signal {
  223. sig := syscall.Signal(w & mask)
  224. if sig == stopped || sig == 0 {
  225. return -1
  226. }
  227. return sig
  228. }
  229. func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
  230. func (w WaitStatus) Stopped() bool { return w&mask == stopped && syscall.Signal(w>>shift) != SIGSTOP }
  231. func (w WaitStatus) Continued() bool { return w&mask == stopped && syscall.Signal(w>>shift) == SIGSTOP }
  232. func (w WaitStatus) StopSignal() syscall.Signal {
  233. if !w.Stopped() {
  234. return -1
  235. }
  236. return syscall.Signal(w>>shift) & 0xFF
  237. }
  238. func (w WaitStatus) TrapCause() int { return -1 }
  239. //sys wait4(pid int32, statusp *_C_int, options int, rusage *Rusage) (wpid int32, err error)
  240. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (int, error) {
  241. var status _C_int
  242. rpid, err := wait4(int32(pid), &status, options, rusage)
  243. wpid := int(rpid)
  244. if wpid == -1 {
  245. return wpid, err
  246. }
  247. if wstatus != nil {
  248. *wstatus = WaitStatus(status)
  249. }
  250. return wpid, nil
  251. }
  252. //sys gethostname(buf []byte) (n int, err error)
  253. func Gethostname() (name string, err error) {
  254. var buf [MaxHostNameLen]byte
  255. n, err := gethostname(buf[:])
  256. if n != 0 {
  257. return "", err
  258. }
  259. n = clen(buf[:])
  260. if n < 1 {
  261. return "", EFAULT
  262. }
  263. return string(buf[:n]), nil
  264. }
  265. //sys utimes(path string, times *[2]Timeval) (err error)
  266. func Utimes(path string, tv []Timeval) (err error) {
  267. if tv == nil {
  268. return utimes(path, nil)
  269. }
  270. if len(tv) != 2 {
  271. return EINVAL
  272. }
  273. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  274. }
  275. //sys utimensat(fd int, path string, times *[2]Timespec, flag int) (err error)
  276. func UtimesNano(path string, ts []Timespec) error {
  277. if ts == nil {
  278. return utimensat(AT_FDCWD, path, nil, 0)
  279. }
  280. if len(ts) != 2 {
  281. return EINVAL
  282. }
  283. return utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
  284. }
  285. func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
  286. if ts == nil {
  287. return utimensat(dirfd, path, nil, flags)
  288. }
  289. if len(ts) != 2 {
  290. return EINVAL
  291. }
  292. return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
  293. }
  294. //sys fcntl(fd int, cmd int, arg int) (val int, err error)
  295. // FcntlInt performs a fcntl syscall on fd with the provided command and argument.
  296. func FcntlInt(fd uintptr, cmd, arg int) (int, error) {
  297. valptr, _, errno := sysvicall6(uintptr(unsafe.Pointer(&procfcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0)
  298. var err error
  299. if errno != 0 {
  300. err = errno
  301. }
  302. return int(valptr), err
  303. }
  304. // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
  305. func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
  306. _, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procfcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(unsafe.Pointer(lk)), 0, 0, 0)
  307. if e1 != 0 {
  308. return e1
  309. }
  310. return nil
  311. }
  312. //sys futimesat(fildes int, path *byte, times *[2]Timeval) (err error)
  313. func Futimesat(dirfd int, path string, tv []Timeval) error {
  314. pathp, err := BytePtrFromString(path)
  315. if err != nil {
  316. return err
  317. }
  318. if tv == nil {
  319. return futimesat(dirfd, pathp, nil)
  320. }
  321. if len(tv) != 2 {
  322. return EINVAL
  323. }
  324. return futimesat(dirfd, pathp, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  325. }
  326. // Solaris doesn't have an futimes function because it allows NULL to be
  327. // specified as the path for futimesat. However, Go doesn't like
  328. // NULL-style string interfaces, so this simple wrapper is provided.
  329. func Futimes(fd int, tv []Timeval) error {
  330. if tv == nil {
  331. return futimesat(fd, nil, nil)
  332. }
  333. if len(tv) != 2 {
  334. return EINVAL
  335. }
  336. return futimesat(fd, nil, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  337. }
  338. func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
  339. switch rsa.Addr.Family {
  340. case AF_UNIX:
  341. pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
  342. sa := new(SockaddrUnix)
  343. // Assume path ends at NUL.
  344. // This is not technically the Solaris semantics for
  345. // abstract Unix domain sockets -- they are supposed
  346. // to be uninterpreted fixed-size binary blobs -- but
  347. // everyone uses this convention.
  348. n := 0
  349. for n < len(pp.Path) && pp.Path[n] != 0 {
  350. n++
  351. }
  352. bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
  353. sa.Name = string(bytes)
  354. return sa, nil
  355. case AF_INET:
  356. pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
  357. sa := new(SockaddrInet4)
  358. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  359. sa.Port = int(p[0])<<8 + int(p[1])
  360. for i := 0; i < len(sa.Addr); i++ {
  361. sa.Addr[i] = pp.Addr[i]
  362. }
  363. return sa, nil
  364. case AF_INET6:
  365. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  366. sa := new(SockaddrInet6)
  367. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  368. sa.Port = int(p[0])<<8 + int(p[1])
  369. sa.ZoneId = pp.Scope_id
  370. for i := 0; i < len(sa.Addr); i++ {
  371. sa.Addr[i] = pp.Addr[i]
  372. }
  373. return sa, nil
  374. }
  375. return nil, EAFNOSUPPORT
  376. }
  377. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) = libsocket.accept
  378. func Accept(fd int) (nfd int, sa Sockaddr, err error) {
  379. var rsa RawSockaddrAny
  380. var len _Socklen = SizeofSockaddrAny
  381. nfd, err = accept(fd, &rsa, &len)
  382. if nfd == -1 {
  383. return
  384. }
  385. sa, err = anyToSockaddr(fd, &rsa)
  386. if err != nil {
  387. Close(nfd)
  388. nfd = 0
  389. }
  390. return
  391. }
  392. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_recvmsg
  393. func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
  394. var msg Msghdr
  395. var rsa RawSockaddrAny
  396. msg.Name = (*byte)(unsafe.Pointer(&rsa))
  397. msg.Namelen = uint32(SizeofSockaddrAny)
  398. var iov Iovec
  399. if len(p) > 0 {
  400. iov.Base = (*int8)(unsafe.Pointer(&p[0]))
  401. iov.SetLen(len(p))
  402. }
  403. var dummy int8
  404. if len(oob) > 0 {
  405. // receive at least one normal byte
  406. if len(p) == 0 {
  407. iov.Base = &dummy
  408. iov.SetLen(1)
  409. }
  410. msg.Accrightslen = int32(len(oob))
  411. }
  412. msg.Iov = &iov
  413. msg.Iovlen = 1
  414. if n, err = recvmsg(fd, &msg, flags); n == -1 {
  415. return
  416. }
  417. oobn = int(msg.Accrightslen)
  418. // source address is only specified if the socket is unconnected
  419. if rsa.Addr.Family != AF_UNSPEC {
  420. from, err = anyToSockaddr(fd, &rsa)
  421. }
  422. return
  423. }
  424. func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
  425. _, err = SendmsgN(fd, p, oob, to, flags)
  426. return
  427. }
  428. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_sendmsg
  429. func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
  430. var ptr unsafe.Pointer
  431. var salen _Socklen
  432. if to != nil {
  433. ptr, salen, err = to.sockaddr()
  434. if err != nil {
  435. return 0, err
  436. }
  437. }
  438. var msg Msghdr
  439. msg.Name = (*byte)(unsafe.Pointer(ptr))
  440. msg.Namelen = uint32(salen)
  441. var iov Iovec
  442. if len(p) > 0 {
  443. iov.Base = (*int8)(unsafe.Pointer(&p[0]))
  444. iov.SetLen(len(p))
  445. }
  446. var dummy int8
  447. if len(oob) > 0 {
  448. // send at least one normal byte
  449. if len(p) == 0 {
  450. iov.Base = &dummy
  451. iov.SetLen(1)
  452. }
  453. msg.Accrightslen = int32(len(oob))
  454. }
  455. msg.Iov = &iov
  456. msg.Iovlen = 1
  457. if n, err = sendmsg(fd, &msg, flags); err != nil {
  458. return 0, err
  459. }
  460. if len(oob) > 0 && len(p) == 0 {
  461. n = 0
  462. }
  463. return n, nil
  464. }
  465. //sys acct(path *byte) (err error)
  466. func Acct(path string) (err error) {
  467. if len(path) == 0 {
  468. // Assume caller wants to disable accounting.
  469. return acct(nil)
  470. }
  471. pathp, err := BytePtrFromString(path)
  472. if err != nil {
  473. return err
  474. }
  475. return acct(pathp)
  476. }
  477. //sys __makedev(version int, major uint, minor uint) (val uint64)
  478. func Mkdev(major, minor uint32) uint64 {
  479. return __makedev(NEWDEV, uint(major), uint(minor))
  480. }
  481. //sys __major(version int, dev uint64) (val uint)
  482. func Major(dev uint64) uint32 {
  483. return uint32(__major(NEWDEV, dev))
  484. }
  485. //sys __minor(version int, dev uint64) (val uint)
  486. func Minor(dev uint64) uint32 {
  487. return uint32(__minor(NEWDEV, dev))
  488. }
  489. /*
  490. * Expose the ioctl function
  491. */
  492. //sys ioctlRet(fd int, req uint, arg uintptr) (ret int, err error) = libc.ioctl
  493. func ioctl(fd int, req uint, arg uintptr) (err error) {
  494. _, err = ioctlRet(fd, req, arg)
  495. return err
  496. }
  497. func IoctlSetTermio(fd int, req uint, value *Termio) error {
  498. err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  499. runtime.KeepAlive(value)
  500. return err
  501. }
  502. func IoctlGetTermio(fd int, req uint) (*Termio, error) {
  503. var value Termio
  504. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  505. return &value, err
  506. }
  507. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  508. func Poll(fds []PollFd, timeout int) (n int, err error) {
  509. if len(fds) == 0 {
  510. return poll(nil, 0, timeout)
  511. }
  512. return poll(&fds[0], len(fds), timeout)
  513. }
  514. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  515. if raceenabled {
  516. raceReleaseMerge(unsafe.Pointer(&ioSync))
  517. }
  518. return sendfile(outfd, infd, offset, count)
  519. }
  520. /*
  521. * Exposed directly
  522. */
  523. //sys Access(path string, mode uint32) (err error)
  524. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  525. //sys Chdir(path string) (err error)
  526. //sys Chmod(path string, mode uint32) (err error)
  527. //sys Chown(path string, uid int, gid int) (err error)
  528. //sys Chroot(path string) (err error)
  529. //sys Close(fd int) (err error)
  530. //sys Creat(path string, mode uint32) (fd int, err error)
  531. //sys Dup(fd int) (nfd int, err error)
  532. //sys Dup2(oldfd int, newfd int) (err error)
  533. //sys Exit(code int)
  534. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  535. //sys Fchdir(fd int) (err error)
  536. //sys Fchmod(fd int, mode uint32) (err error)
  537. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  538. //sys Fchown(fd int, uid int, gid int) (err error)
  539. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  540. //sys Fdatasync(fd int) (err error)
  541. //sys Flock(fd int, how int) (err error)
  542. //sys Fpathconf(fd int, name int) (val int, err error)
  543. //sys Fstat(fd int, stat *Stat_t) (err error)
  544. //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
  545. //sys Fstatvfs(fd int, vfsstat *Statvfs_t) (err error)
  546. //sys Getdents(fd int, buf []byte, basep *uintptr) (n int, err error)
  547. //sysnb Getgid() (gid int)
  548. //sysnb Getpid() (pid int)
  549. //sysnb Getpgid(pid int) (pgid int, err error)
  550. //sysnb Getpgrp() (pgid int, err error)
  551. //sys Geteuid() (euid int)
  552. //sys Getegid() (egid int)
  553. //sys Getppid() (ppid int)
  554. //sys Getpriority(which int, who int) (n int, err error)
  555. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  556. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  557. //sysnb Gettimeofday(tv *Timeval) (err error)
  558. //sysnb Getuid() (uid int)
  559. //sys Kill(pid int, signum syscall.Signal) (err error)
  560. //sys Lchown(path string, uid int, gid int) (err error)
  561. //sys Link(path string, link string) (err error)
  562. //sys Listen(s int, backlog int) (err error) = libsocket.__xnet_llisten
  563. //sys Lstat(path string, stat *Stat_t) (err error)
  564. //sys Madvise(b []byte, advice int) (err error)
  565. //sys Mkdir(path string, mode uint32) (err error)
  566. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  567. //sys Mkfifo(path string, mode uint32) (err error)
  568. //sys Mkfifoat(dirfd int, path string, mode uint32) (err error)
  569. //sys Mknod(path string, mode uint32, dev int) (err error)
  570. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  571. //sys Mlock(b []byte) (err error)
  572. //sys Mlockall(flags int) (err error)
  573. //sys Mprotect(b []byte, prot int) (err error)
  574. //sys Msync(b []byte, flags int) (err error)
  575. //sys Munlock(b []byte) (err error)
  576. //sys Munlockall() (err error)
  577. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  578. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  579. //sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
  580. //sys Pathconf(path string, name int) (val int, err error)
  581. //sys Pause() (err error)
  582. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  583. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  584. //sys read(fd int, p []byte) (n int, err error)
  585. //sys Readlink(path string, buf []byte) (n int, err error)
  586. //sys Rename(from string, to string) (err error)
  587. //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
  588. //sys Rmdir(path string) (err error)
  589. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = lseek
  590. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
  591. //sysnb Setegid(egid int) (err error)
  592. //sysnb Seteuid(euid int) (err error)
  593. //sysnb Setgid(gid int) (err error)
  594. //sys Sethostname(p []byte) (err error)
  595. //sysnb Setpgid(pid int, pgid int) (err error)
  596. //sys Setpriority(which int, who int, prio int) (err error)
  597. //sysnb Setregid(rgid int, egid int) (err error)
  598. //sysnb Setreuid(ruid int, euid int) (err error)
  599. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  600. //sysnb Setsid() (pid int, err error)
  601. //sysnb Setuid(uid int) (err error)
  602. //sys Shutdown(s int, how int) (err error) = libsocket.shutdown
  603. //sys Stat(path string, stat *Stat_t) (err error)
  604. //sys Statvfs(path string, vfsstat *Statvfs_t) (err error)
  605. //sys Symlink(path string, link string) (err error)
  606. //sys Sync() (err error)
  607. //sys Sysconf(which int) (n int64, err error)
  608. //sysnb Times(tms *Tms) (ticks uintptr, err error)
  609. //sys Truncate(path string, length int64) (err error)
  610. //sys Fsync(fd int) (err error)
  611. //sys Ftruncate(fd int, length int64) (err error)
  612. //sys Umask(mask int) (oldmask int)
  613. //sysnb Uname(buf *Utsname) (err error)
  614. //sys Unmount(target string, flags int) (err error) = libc.umount
  615. //sys Unlink(path string) (err error)
  616. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  617. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  618. //sys Utime(path string, buf *Utimbuf) (err error)
  619. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_bind
  620. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_connect
  621. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  622. //sys munmap(addr uintptr, length uintptr) (err error)
  623. //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = libsendfile.sendfile
  624. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_sendto
  625. //sys socket(domain int, typ int, proto int) (fd int, err error) = libsocket.__xnet_socket
  626. //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) = libsocket.__xnet_socketpair
  627. //sys write(fd int, p []byte) (n int, err error)
  628. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) = libsocket.__xnet_getsockopt
  629. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getpeername
  630. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) = libsocket.setsockopt
  631. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) = libsocket.recvfrom
  632. func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
  633. r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procread)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
  634. n = int(r0)
  635. if e1 != 0 {
  636. err = e1
  637. }
  638. return
  639. }
  640. func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
  641. r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procwrite)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
  642. n = int(r0)
  643. if e1 != 0 {
  644. err = e1
  645. }
  646. return
  647. }
  648. var mapper = &mmapper{
  649. active: make(map[*byte][]byte),
  650. mmap: mmap,
  651. munmap: munmap,
  652. }
  653. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  654. return mapper.Mmap(fd, offset, length, prot, flags)
  655. }
  656. func Munmap(b []byte) (err error) {
  657. return mapper.Munmap(b)
  658. }
  659. // Event Ports
  660. type fileObjCookie struct {
  661. fobj *fileObj
  662. cookie interface{}
  663. }
  664. // EventPort provides a safe abstraction on top of Solaris/illumos Event Ports.
  665. type EventPort struct {
  666. port int
  667. mu sync.Mutex
  668. fds map[uintptr]interface{}
  669. paths map[string]*fileObjCookie
  670. }
  671. // PortEvent is an abstraction of the port_event C struct.
  672. // Compare Source against PORT_SOURCE_FILE or PORT_SOURCE_FD
  673. // to see if Path or Fd was the event source. The other will be
  674. // uninitialized.
  675. type PortEvent struct {
  676. Cookie interface{}
  677. Events int32
  678. Fd uintptr
  679. Path string
  680. Source uint16
  681. fobj *fileObj
  682. }
  683. // NewEventPort creates a new EventPort including the
  684. // underlying call to port_create(3c).
  685. func NewEventPort() (*EventPort, error) {
  686. port, err := port_create()
  687. if err != nil {
  688. return nil, err
  689. }
  690. e := &EventPort{
  691. port: port,
  692. fds: make(map[uintptr]interface{}),
  693. paths: make(map[string]*fileObjCookie),
  694. }
  695. return e, nil
  696. }
  697. //sys port_create() (n int, err error)
  698. //sys port_associate(port int, source int, object uintptr, events int, user *byte) (n int, err error)
  699. //sys port_dissociate(port int, source int, object uintptr) (n int, err error)
  700. //sys port_get(port int, pe *portEvent, timeout *Timespec) (n int, err error)
  701. //sys port_getn(port int, pe *portEvent, max uint32, nget *uint32, timeout *Timespec) (n int, err error)
  702. // Close closes the event port.
  703. func (e *EventPort) Close() error {
  704. e.mu.Lock()
  705. defer e.mu.Unlock()
  706. e.fds = nil
  707. e.paths = nil
  708. return Close(e.port)
  709. }
  710. // PathIsWatched checks to see if path is associated with this EventPort.
  711. func (e *EventPort) PathIsWatched(path string) bool {
  712. e.mu.Lock()
  713. defer e.mu.Unlock()
  714. _, found := e.paths[path]
  715. return found
  716. }
  717. // FdIsWatched checks to see if fd is associated with this EventPort.
  718. func (e *EventPort) FdIsWatched(fd uintptr) bool {
  719. e.mu.Lock()
  720. defer e.mu.Unlock()
  721. _, found := e.fds[fd]
  722. return found
  723. }
  724. // AssociatePath wraps port_associate(3c) for a filesystem path including
  725. // creating the necessary file_obj from the provided stat information.
  726. func (e *EventPort) AssociatePath(path string, stat os.FileInfo, events int, cookie interface{}) error {
  727. e.mu.Lock()
  728. defer e.mu.Unlock()
  729. if _, found := e.paths[path]; found {
  730. return fmt.Errorf("%v is already associated with this Event Port", path)
  731. }
  732. fobj, err := createFileObj(path, stat)
  733. if err != nil {
  734. return err
  735. }
  736. fCookie := &fileObjCookie{fobj, cookie}
  737. _, err = port_associate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(fobj)), events, (*byte)(unsafe.Pointer(&fCookie.cookie)))
  738. if err != nil {
  739. return err
  740. }
  741. e.paths[path] = fCookie
  742. return nil
  743. }
  744. // DissociatePath wraps port_dissociate(3c) for a filesystem path.
  745. func (e *EventPort) DissociatePath(path string) error {
  746. e.mu.Lock()
  747. defer e.mu.Unlock()
  748. f, ok := e.paths[path]
  749. if !ok {
  750. return fmt.Errorf("%v is not associated with this Event Port", path)
  751. }
  752. _, err := port_dissociate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(f.fobj)))
  753. if err != nil {
  754. return err
  755. }
  756. delete(e.paths, path)
  757. return nil
  758. }
  759. // AssociateFd wraps calls to port_associate(3c) on file descriptors.
  760. func (e *EventPort) AssociateFd(fd uintptr, events int, cookie interface{}) error {
  761. e.mu.Lock()
  762. defer e.mu.Unlock()
  763. if _, found := e.fds[fd]; found {
  764. return fmt.Errorf("%v is already associated with this Event Port", fd)
  765. }
  766. pcookie := &cookie
  767. _, err := port_associate(e.port, PORT_SOURCE_FD, fd, events, (*byte)(unsafe.Pointer(pcookie)))
  768. if err != nil {
  769. return err
  770. }
  771. e.fds[fd] = pcookie
  772. return nil
  773. }
  774. // DissociateFd wraps calls to port_dissociate(3c) on file descriptors.
  775. func (e *EventPort) DissociateFd(fd uintptr) error {
  776. e.mu.Lock()
  777. defer e.mu.Unlock()
  778. _, ok := e.fds[fd]
  779. if !ok {
  780. return fmt.Errorf("%v is not associated with this Event Port", fd)
  781. }
  782. _, err := port_dissociate(e.port, PORT_SOURCE_FD, fd)
  783. if err != nil {
  784. return err
  785. }
  786. delete(e.fds, fd)
  787. return nil
  788. }
  789. func createFileObj(name string, stat os.FileInfo) (*fileObj, error) {
  790. fobj := new(fileObj)
  791. bs, err := ByteSliceFromString(name)
  792. if err != nil {
  793. return nil, err
  794. }
  795. fobj.Name = (*int8)(unsafe.Pointer(&bs[0]))
  796. s := stat.Sys().(*syscall.Stat_t)
  797. fobj.Atim.Sec = s.Atim.Sec
  798. fobj.Atim.Nsec = s.Atim.Nsec
  799. fobj.Mtim.Sec = s.Mtim.Sec
  800. fobj.Mtim.Nsec = s.Mtim.Nsec
  801. fobj.Ctim.Sec = s.Ctim.Sec
  802. fobj.Ctim.Nsec = s.Ctim.Nsec
  803. return fobj, nil
  804. }
  805. // GetOne wraps port_get(3c) and returns a single PortEvent.
  806. func (e *EventPort) GetOne(t *Timespec) (*PortEvent, error) {
  807. pe := new(portEvent)
  808. _, err := port_get(e.port, pe, t)
  809. if err != nil {
  810. return nil, err
  811. }
  812. p := new(PortEvent)
  813. p.Events = pe.Events
  814. p.Source = pe.Source
  815. e.mu.Lock()
  816. defer e.mu.Unlock()
  817. switch pe.Source {
  818. case PORT_SOURCE_FD:
  819. p.Fd = uintptr(pe.Object)
  820. cookie := (*interface{})(unsafe.Pointer(pe.User))
  821. p.Cookie = *cookie
  822. delete(e.fds, p.Fd)
  823. case PORT_SOURCE_FILE:
  824. p.fobj = (*fileObj)(unsafe.Pointer(uintptr(pe.Object)))
  825. p.Path = BytePtrToString((*byte)(unsafe.Pointer(p.fobj.Name)))
  826. cookie := (*interface{})(unsafe.Pointer(pe.User))
  827. p.Cookie = *cookie
  828. delete(e.paths, p.Path)
  829. }
  830. return p, nil
  831. }
  832. // Pending wraps port_getn(3c) and returns how many events are pending.
  833. func (e *EventPort) Pending() (int, error) {
  834. var n uint32 = 0
  835. _, err := port_getn(e.port, nil, 0, &n, nil)
  836. return int(n), err
  837. }
  838. // Get wraps port_getn(3c) and fills a slice of PortEvent.
  839. // It will block until either min events have been received
  840. // or the timeout has been exceeded. It will return how many
  841. // events were actually received along with any error information.
  842. func (e *EventPort) Get(s []PortEvent, min int, timeout *Timespec) (int, error) {
  843. if min == 0 {
  844. return 0, fmt.Errorf("need to request at least one event or use Pending() instead")
  845. }
  846. if len(s) < min {
  847. return 0, fmt.Errorf("len(s) (%d) is less than min events requested (%d)", len(s), min)
  848. }
  849. got := uint32(min)
  850. max := uint32(len(s))
  851. var err error
  852. ps := make([]portEvent, max, max)
  853. _, err = port_getn(e.port, &ps[0], max, &got, timeout)
  854. // got will be trustworthy with ETIME, but not any other error.
  855. if err != nil && err != ETIME {
  856. return 0, err
  857. }
  858. e.mu.Lock()
  859. defer e.mu.Unlock()
  860. for i := 0; i < int(got); i++ {
  861. s[i].Events = ps[i].Events
  862. s[i].Source = ps[i].Source
  863. switch ps[i].Source {
  864. case PORT_SOURCE_FD:
  865. s[i].Fd = uintptr(ps[i].Object)
  866. cookie := (*interface{})(unsafe.Pointer(ps[i].User))
  867. s[i].Cookie = *cookie
  868. delete(e.fds, s[i].Fd)
  869. case PORT_SOURCE_FILE:
  870. s[i].fobj = (*fileObj)(unsafe.Pointer(uintptr(ps[i].Object)))
  871. s[i].Path = BytePtrToString((*byte)(unsafe.Pointer(s[i].fobj.Name)))
  872. cookie := (*interface{})(unsafe.Pointer(ps[i].User))
  873. s[i].Cookie = *cookie
  874. delete(e.paths, s[i].Path)
  875. }
  876. }
  877. return int(got), err
  878. }