syscall_solaris.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. if err == nil {
  59. p[0] = int(pp[0])
  60. p[1] = int(pp[1])
  61. }
  62. return nil
  63. }
  64. //sysnb pipe2(p *[2]_C_int, flags int) (err error)
  65. func Pipe2(p []int, flags int) error {
  66. if len(p) != 2 {
  67. return EINVAL
  68. }
  69. var pp [2]_C_int
  70. err := pipe2(&pp, flags)
  71. if err == nil {
  72. p[0] = int(pp[0])
  73. p[1] = int(pp[1])
  74. }
  75. return err
  76. }
  77. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  78. if sa.Port < 0 || sa.Port > 0xFFFF {
  79. return nil, 0, EINVAL
  80. }
  81. sa.raw.Family = AF_INET
  82. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  83. p[0] = byte(sa.Port >> 8)
  84. p[1] = byte(sa.Port)
  85. sa.raw.Addr = sa.Addr
  86. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
  87. }
  88. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  89. if sa.Port < 0 || sa.Port > 0xFFFF {
  90. return nil, 0, EINVAL
  91. }
  92. sa.raw.Family = AF_INET6
  93. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  94. p[0] = byte(sa.Port >> 8)
  95. p[1] = byte(sa.Port)
  96. sa.raw.Scope_id = sa.ZoneId
  97. sa.raw.Addr = sa.Addr
  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. sa.Addr = pp.Addr
  361. return sa, nil
  362. case AF_INET6:
  363. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  364. sa := new(SockaddrInet6)
  365. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  366. sa.Port = int(p[0])<<8 + int(p[1])
  367. sa.ZoneId = pp.Scope_id
  368. sa.Addr = pp.Addr
  369. return sa, nil
  370. }
  371. return nil, EAFNOSUPPORT
  372. }
  373. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) = libsocket.accept
  374. func Accept(fd int) (nfd int, sa Sockaddr, err error) {
  375. var rsa RawSockaddrAny
  376. var len _Socklen = SizeofSockaddrAny
  377. nfd, err = accept(fd, &rsa, &len)
  378. if nfd == -1 {
  379. return
  380. }
  381. sa, err = anyToSockaddr(fd, &rsa)
  382. if err != nil {
  383. Close(nfd)
  384. nfd = 0
  385. }
  386. return
  387. }
  388. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_recvmsg
  389. func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
  390. var msg Msghdr
  391. var rsa RawSockaddrAny
  392. msg.Name = (*byte)(unsafe.Pointer(&rsa))
  393. msg.Namelen = uint32(SizeofSockaddrAny)
  394. var iov Iovec
  395. if len(p) > 0 {
  396. iov.Base = (*int8)(unsafe.Pointer(&p[0]))
  397. iov.SetLen(len(p))
  398. }
  399. var dummy int8
  400. if len(oob) > 0 {
  401. // receive at least one normal byte
  402. if len(p) == 0 {
  403. iov.Base = &dummy
  404. iov.SetLen(1)
  405. }
  406. msg.Accrightslen = int32(len(oob))
  407. }
  408. msg.Iov = &iov
  409. msg.Iovlen = 1
  410. if n, err = recvmsg(fd, &msg, flags); n == -1 {
  411. return
  412. }
  413. oobn = int(msg.Accrightslen)
  414. // source address is only specified if the socket is unconnected
  415. if rsa.Addr.Family != AF_UNSPEC {
  416. from, err = anyToSockaddr(fd, &rsa)
  417. }
  418. return
  419. }
  420. func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
  421. _, err = SendmsgN(fd, p, oob, to, flags)
  422. return
  423. }
  424. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_sendmsg
  425. func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
  426. var ptr unsafe.Pointer
  427. var salen _Socklen
  428. if to != nil {
  429. ptr, salen, err = to.sockaddr()
  430. if err != nil {
  431. return 0, err
  432. }
  433. }
  434. var msg Msghdr
  435. msg.Name = (*byte)(unsafe.Pointer(ptr))
  436. msg.Namelen = uint32(salen)
  437. var iov Iovec
  438. if len(p) > 0 {
  439. iov.Base = (*int8)(unsafe.Pointer(&p[0]))
  440. iov.SetLen(len(p))
  441. }
  442. var dummy int8
  443. if len(oob) > 0 {
  444. // send at least one normal byte
  445. if len(p) == 0 {
  446. iov.Base = &dummy
  447. iov.SetLen(1)
  448. }
  449. msg.Accrightslen = int32(len(oob))
  450. }
  451. msg.Iov = &iov
  452. msg.Iovlen = 1
  453. if n, err = sendmsg(fd, &msg, flags); err != nil {
  454. return 0, err
  455. }
  456. if len(oob) > 0 && len(p) == 0 {
  457. n = 0
  458. }
  459. return n, nil
  460. }
  461. //sys acct(path *byte) (err error)
  462. func Acct(path string) (err error) {
  463. if len(path) == 0 {
  464. // Assume caller wants to disable accounting.
  465. return acct(nil)
  466. }
  467. pathp, err := BytePtrFromString(path)
  468. if err != nil {
  469. return err
  470. }
  471. return acct(pathp)
  472. }
  473. //sys __makedev(version int, major uint, minor uint) (val uint64)
  474. func Mkdev(major, minor uint32) uint64 {
  475. return __makedev(NEWDEV, uint(major), uint(minor))
  476. }
  477. //sys __major(version int, dev uint64) (val uint)
  478. func Major(dev uint64) uint32 {
  479. return uint32(__major(NEWDEV, dev))
  480. }
  481. //sys __minor(version int, dev uint64) (val uint)
  482. func Minor(dev uint64) uint32 {
  483. return uint32(__minor(NEWDEV, dev))
  484. }
  485. /*
  486. * Expose the ioctl function
  487. */
  488. //sys ioctlRet(fd int, req uint, arg uintptr) (ret int, err error) = libc.ioctl
  489. func ioctl(fd int, req uint, arg uintptr) (err error) {
  490. _, err = ioctlRet(fd, req, arg)
  491. return err
  492. }
  493. func IoctlSetTermio(fd int, req uint, value *Termio) error {
  494. err := ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  495. runtime.KeepAlive(value)
  496. return err
  497. }
  498. func IoctlGetTermio(fd int, req uint) (*Termio, error) {
  499. var value Termio
  500. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  501. return &value, err
  502. }
  503. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  504. func Poll(fds []PollFd, timeout int) (n int, err error) {
  505. if len(fds) == 0 {
  506. return poll(nil, 0, timeout)
  507. }
  508. return poll(&fds[0], len(fds), timeout)
  509. }
  510. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  511. if raceenabled {
  512. raceReleaseMerge(unsafe.Pointer(&ioSync))
  513. }
  514. return sendfile(outfd, infd, offset, count)
  515. }
  516. /*
  517. * Exposed directly
  518. */
  519. //sys Access(path string, mode uint32) (err error)
  520. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  521. //sys Chdir(path string) (err error)
  522. //sys Chmod(path string, mode uint32) (err error)
  523. //sys Chown(path string, uid int, gid int) (err error)
  524. //sys Chroot(path string) (err error)
  525. //sys Close(fd int) (err error)
  526. //sys Creat(path string, mode uint32) (fd int, err error)
  527. //sys Dup(fd int) (nfd int, err error)
  528. //sys Dup2(oldfd int, newfd int) (err error)
  529. //sys Exit(code int)
  530. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  531. //sys Fchdir(fd int) (err error)
  532. //sys Fchmod(fd int, mode uint32) (err error)
  533. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  534. //sys Fchown(fd int, uid int, gid int) (err error)
  535. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  536. //sys Fdatasync(fd int) (err error)
  537. //sys Flock(fd int, how int) (err error)
  538. //sys Fpathconf(fd int, name int) (val int, err error)
  539. //sys Fstat(fd int, stat *Stat_t) (err error)
  540. //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error)
  541. //sys Fstatvfs(fd int, vfsstat *Statvfs_t) (err error)
  542. //sys Getdents(fd int, buf []byte, basep *uintptr) (n int, err error)
  543. //sysnb Getgid() (gid int)
  544. //sysnb Getpid() (pid int)
  545. //sysnb Getpgid(pid int) (pgid int, err error)
  546. //sysnb Getpgrp() (pgid int, err error)
  547. //sys Geteuid() (euid int)
  548. //sys Getegid() (egid int)
  549. //sys Getppid() (ppid int)
  550. //sys Getpriority(which int, who int) (n int, err error)
  551. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  552. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  553. //sysnb Gettimeofday(tv *Timeval) (err error)
  554. //sysnb Getuid() (uid int)
  555. //sys Kill(pid int, signum syscall.Signal) (err error)
  556. //sys Lchown(path string, uid int, gid int) (err error)
  557. //sys Link(path string, link string) (err error)
  558. //sys Listen(s int, backlog int) (err error) = libsocket.__xnet_llisten
  559. //sys Lstat(path string, stat *Stat_t) (err error)
  560. //sys Madvise(b []byte, advice int) (err error)
  561. //sys Mkdir(path string, mode uint32) (err error)
  562. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  563. //sys Mkfifo(path string, mode uint32) (err error)
  564. //sys Mkfifoat(dirfd int, path string, mode uint32) (err error)
  565. //sys Mknod(path string, mode uint32, dev int) (err error)
  566. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  567. //sys Mlock(b []byte) (err error)
  568. //sys Mlockall(flags int) (err error)
  569. //sys Mprotect(b []byte, prot int) (err error)
  570. //sys Msync(b []byte, flags int) (err error)
  571. //sys Munlock(b []byte) (err error)
  572. //sys Munlockall() (err error)
  573. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  574. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  575. //sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
  576. //sys Pathconf(path string, name int) (val int, err error)
  577. //sys Pause() (err error)
  578. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  579. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  580. //sys read(fd int, p []byte) (n int, err error)
  581. //sys Readlink(path string, buf []byte) (n int, err error)
  582. //sys Rename(from string, to string) (err error)
  583. //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
  584. //sys Rmdir(path string) (err error)
  585. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = lseek
  586. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
  587. //sysnb Setegid(egid int) (err error)
  588. //sysnb Seteuid(euid int) (err error)
  589. //sysnb Setgid(gid int) (err error)
  590. //sys Sethostname(p []byte) (err error)
  591. //sysnb Setpgid(pid int, pgid int) (err error)
  592. //sys Setpriority(which int, who int, prio int) (err error)
  593. //sysnb Setregid(rgid int, egid int) (err error)
  594. //sysnb Setreuid(ruid int, euid int) (err error)
  595. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  596. //sysnb Setsid() (pid int, err error)
  597. //sysnb Setuid(uid int) (err error)
  598. //sys Shutdown(s int, how int) (err error) = libsocket.shutdown
  599. //sys Stat(path string, stat *Stat_t) (err error)
  600. //sys Statvfs(path string, vfsstat *Statvfs_t) (err error)
  601. //sys Symlink(path string, link string) (err error)
  602. //sys Sync() (err error)
  603. //sys Sysconf(which int) (n int64, err error)
  604. //sysnb Times(tms *Tms) (ticks uintptr, err error)
  605. //sys Truncate(path string, length int64) (err error)
  606. //sys Fsync(fd int) (err error)
  607. //sys Ftruncate(fd int, length int64) (err error)
  608. //sys Umask(mask int) (oldmask int)
  609. //sysnb Uname(buf *Utsname) (err error)
  610. //sys Unmount(target string, flags int) (err error) = libc.umount
  611. //sys Unlink(path string) (err error)
  612. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  613. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  614. //sys Utime(path string, buf *Utimbuf) (err error)
  615. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_bind
  616. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_connect
  617. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  618. //sys munmap(addr uintptr, length uintptr) (err error)
  619. //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = libsendfile.sendfile
  620. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_sendto
  621. //sys socket(domain int, typ int, proto int) (fd int, err error) = libsocket.__xnet_socket
  622. //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) = libsocket.__xnet_socketpair
  623. //sys write(fd int, p []byte) (n int, err error)
  624. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) = libsocket.__xnet_getsockopt
  625. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getpeername
  626. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) = libsocket.setsockopt
  627. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) = libsocket.recvfrom
  628. func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
  629. r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procread)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
  630. n = int(r0)
  631. if e1 != 0 {
  632. err = e1
  633. }
  634. return
  635. }
  636. func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
  637. r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procwrite)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
  638. n = int(r0)
  639. if e1 != 0 {
  640. err = e1
  641. }
  642. return
  643. }
  644. var mapper = &mmapper{
  645. active: make(map[*byte][]byte),
  646. mmap: mmap,
  647. munmap: munmap,
  648. }
  649. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  650. return mapper.Mmap(fd, offset, length, prot, flags)
  651. }
  652. func Munmap(b []byte) (err error) {
  653. return mapper.Munmap(b)
  654. }
  655. // Event Ports
  656. type fileObjCookie struct {
  657. fobj *fileObj
  658. cookie interface{}
  659. }
  660. // EventPort provides a safe abstraction on top of Solaris/illumos Event Ports.
  661. type EventPort struct {
  662. port int
  663. mu sync.Mutex
  664. fds map[uintptr]interface{}
  665. paths map[string]*fileObjCookie
  666. }
  667. // PortEvent is an abstraction of the port_event C struct.
  668. // Compare Source against PORT_SOURCE_FILE or PORT_SOURCE_FD
  669. // to see if Path or Fd was the event source. The other will be
  670. // uninitialized.
  671. type PortEvent struct {
  672. Cookie interface{}
  673. Events int32
  674. Fd uintptr
  675. Path string
  676. Source uint16
  677. fobj *fileObj
  678. }
  679. // NewEventPort creates a new EventPort including the
  680. // underlying call to port_create(3c).
  681. func NewEventPort() (*EventPort, error) {
  682. port, err := port_create()
  683. if err != nil {
  684. return nil, err
  685. }
  686. e := &EventPort{
  687. port: port,
  688. fds: make(map[uintptr]interface{}),
  689. paths: make(map[string]*fileObjCookie),
  690. }
  691. return e, nil
  692. }
  693. //sys port_create() (n int, err error)
  694. //sys port_associate(port int, source int, object uintptr, events int, user *byte) (n int, err error)
  695. //sys port_dissociate(port int, source int, object uintptr) (n int, err error)
  696. //sys port_get(port int, pe *portEvent, timeout *Timespec) (n int, err error)
  697. //sys port_getn(port int, pe *portEvent, max uint32, nget *uint32, timeout *Timespec) (n int, err error)
  698. // Close closes the event port.
  699. func (e *EventPort) Close() error {
  700. e.mu.Lock()
  701. defer e.mu.Unlock()
  702. e.fds = nil
  703. e.paths = nil
  704. return Close(e.port)
  705. }
  706. // PathIsWatched checks to see if path is associated with this EventPort.
  707. func (e *EventPort) PathIsWatched(path string) bool {
  708. e.mu.Lock()
  709. defer e.mu.Unlock()
  710. _, found := e.paths[path]
  711. return found
  712. }
  713. // FdIsWatched checks to see if fd is associated with this EventPort.
  714. func (e *EventPort) FdIsWatched(fd uintptr) bool {
  715. e.mu.Lock()
  716. defer e.mu.Unlock()
  717. _, found := e.fds[fd]
  718. return found
  719. }
  720. // AssociatePath wraps port_associate(3c) for a filesystem path including
  721. // creating the necessary file_obj from the provided stat information.
  722. func (e *EventPort) AssociatePath(path string, stat os.FileInfo, events int, cookie interface{}) error {
  723. e.mu.Lock()
  724. defer e.mu.Unlock()
  725. if _, found := e.paths[path]; found {
  726. return fmt.Errorf("%v is already associated with this Event Port", path)
  727. }
  728. fobj, err := createFileObj(path, stat)
  729. if err != nil {
  730. return err
  731. }
  732. fCookie := &fileObjCookie{fobj, cookie}
  733. _, err = port_associate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(fobj)), events, (*byte)(unsafe.Pointer(&fCookie.cookie)))
  734. if err != nil {
  735. return err
  736. }
  737. e.paths[path] = fCookie
  738. return nil
  739. }
  740. // DissociatePath wraps port_dissociate(3c) for a filesystem path.
  741. func (e *EventPort) DissociatePath(path string) error {
  742. e.mu.Lock()
  743. defer e.mu.Unlock()
  744. f, ok := e.paths[path]
  745. if !ok {
  746. return fmt.Errorf("%v is not associated with this Event Port", path)
  747. }
  748. _, err := port_dissociate(e.port, PORT_SOURCE_FILE, uintptr(unsafe.Pointer(f.fobj)))
  749. if err != nil {
  750. return err
  751. }
  752. delete(e.paths, path)
  753. return nil
  754. }
  755. // AssociateFd wraps calls to port_associate(3c) on file descriptors.
  756. func (e *EventPort) AssociateFd(fd uintptr, events int, cookie interface{}) error {
  757. e.mu.Lock()
  758. defer e.mu.Unlock()
  759. if _, found := e.fds[fd]; found {
  760. return fmt.Errorf("%v is already associated with this Event Port", fd)
  761. }
  762. pcookie := &cookie
  763. _, err := port_associate(e.port, PORT_SOURCE_FD, fd, events, (*byte)(unsafe.Pointer(pcookie)))
  764. if err != nil {
  765. return err
  766. }
  767. e.fds[fd] = pcookie
  768. return nil
  769. }
  770. // DissociateFd wraps calls to port_dissociate(3c) on file descriptors.
  771. func (e *EventPort) DissociateFd(fd uintptr) error {
  772. e.mu.Lock()
  773. defer e.mu.Unlock()
  774. _, ok := e.fds[fd]
  775. if !ok {
  776. return fmt.Errorf("%v is not associated with this Event Port", fd)
  777. }
  778. _, err := port_dissociate(e.port, PORT_SOURCE_FD, fd)
  779. if err != nil {
  780. return err
  781. }
  782. delete(e.fds, fd)
  783. return nil
  784. }
  785. func createFileObj(name string, stat os.FileInfo) (*fileObj, error) {
  786. fobj := new(fileObj)
  787. bs, err := ByteSliceFromString(name)
  788. if err != nil {
  789. return nil, err
  790. }
  791. fobj.Name = (*int8)(unsafe.Pointer(&bs[0]))
  792. s := stat.Sys().(*syscall.Stat_t)
  793. fobj.Atim.Sec = s.Atim.Sec
  794. fobj.Atim.Nsec = s.Atim.Nsec
  795. fobj.Mtim.Sec = s.Mtim.Sec
  796. fobj.Mtim.Nsec = s.Mtim.Nsec
  797. fobj.Ctim.Sec = s.Ctim.Sec
  798. fobj.Ctim.Nsec = s.Ctim.Nsec
  799. return fobj, nil
  800. }
  801. // GetOne wraps port_get(3c) and returns a single PortEvent.
  802. func (e *EventPort) GetOne(t *Timespec) (*PortEvent, error) {
  803. pe := new(portEvent)
  804. _, err := port_get(e.port, pe, t)
  805. if err != nil {
  806. return nil, err
  807. }
  808. p := new(PortEvent)
  809. p.Events = pe.Events
  810. p.Source = pe.Source
  811. e.mu.Lock()
  812. defer e.mu.Unlock()
  813. switch pe.Source {
  814. case PORT_SOURCE_FD:
  815. p.Fd = uintptr(pe.Object)
  816. cookie := (*interface{})(unsafe.Pointer(pe.User))
  817. p.Cookie = *cookie
  818. delete(e.fds, p.Fd)
  819. case PORT_SOURCE_FILE:
  820. p.fobj = (*fileObj)(unsafe.Pointer(uintptr(pe.Object)))
  821. p.Path = BytePtrToString((*byte)(unsafe.Pointer(p.fobj.Name)))
  822. cookie := (*interface{})(unsafe.Pointer(pe.User))
  823. p.Cookie = *cookie
  824. delete(e.paths, p.Path)
  825. }
  826. return p, nil
  827. }
  828. // Pending wraps port_getn(3c) and returns how many events are pending.
  829. func (e *EventPort) Pending() (int, error) {
  830. var n uint32 = 0
  831. _, err := port_getn(e.port, nil, 0, &n, nil)
  832. return int(n), err
  833. }
  834. // Get wraps port_getn(3c) and fills a slice of PortEvent.
  835. // It will block until either min events have been received
  836. // or the timeout has been exceeded. It will return how many
  837. // events were actually received along with any error information.
  838. func (e *EventPort) Get(s []PortEvent, min int, timeout *Timespec) (int, error) {
  839. if min == 0 {
  840. return 0, fmt.Errorf("need to request at least one event or use Pending() instead")
  841. }
  842. if len(s) < min {
  843. return 0, fmt.Errorf("len(s) (%d) is less than min events requested (%d)", len(s), min)
  844. }
  845. got := uint32(min)
  846. max := uint32(len(s))
  847. var err error
  848. ps := make([]portEvent, max, max)
  849. _, err = port_getn(e.port, &ps[0], max, &got, timeout)
  850. // got will be trustworthy with ETIME, but not any other error.
  851. if err != nil && err != ETIME {
  852. return 0, err
  853. }
  854. e.mu.Lock()
  855. defer e.mu.Unlock()
  856. for i := 0; i < int(got); i++ {
  857. s[i].Events = ps[i].Events
  858. s[i].Source = ps[i].Source
  859. switch ps[i].Source {
  860. case PORT_SOURCE_FD:
  861. s[i].Fd = uintptr(ps[i].Object)
  862. cookie := (*interface{})(unsafe.Pointer(ps[i].User))
  863. s[i].Cookie = *cookie
  864. delete(e.fds, s[i].Fd)
  865. case PORT_SOURCE_FILE:
  866. s[i].fobj = (*fileObj)(unsafe.Pointer(uintptr(ps[i].Object)))
  867. s[i].Path = BytePtrToString((*byte)(unsafe.Pointer(s[i].fobj.Name)))
  868. cookie := (*interface{})(unsafe.Pointer(ps[i].User))
  869. s[i].Cookie = *cookie
  870. delete(e.paths, s[i].Path)
  871. }
  872. }
  873. return int(got), err
  874. }