syscall_solaris.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. "sync/atomic"
  14. "syscall"
  15. "unsafe"
  16. )
  17. // Implemented in runtime/syscall_solaris.go.
  18. type syscallFunc uintptr
  19. func rawSysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
  20. func sysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
  21. type SockaddrDatalink struct {
  22. Family uint16
  23. Index uint16
  24. Type uint8
  25. Nlen uint8
  26. Alen uint8
  27. Slen uint8
  28. Data [244]int8
  29. raw RawSockaddrDatalink
  30. }
  31. func clen(n []byte) int {
  32. for i := 0; i < len(n); i++ {
  33. if n[i] == 0 {
  34. return i
  35. }
  36. }
  37. return len(n)
  38. }
  39. // ParseDirent parses up to max directory entries in buf,
  40. // appending the names to names. It returns the number
  41. // bytes consumed from buf, the number of entries added
  42. // to names, and the new names slice.
  43. func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
  44. origlen := len(buf)
  45. for max != 0 && len(buf) > 0 {
  46. dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
  47. if dirent.Reclen == 0 {
  48. buf = nil
  49. break
  50. }
  51. buf = buf[dirent.Reclen:]
  52. if dirent.Ino == 0 { // File absent in directory.
  53. continue
  54. }
  55. bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
  56. var name = string(bytes[0:clen(bytes[:])])
  57. if name == "." || name == ".." { // Useless names
  58. continue
  59. }
  60. max--
  61. count++
  62. names = append(names, name)
  63. }
  64. return origlen - len(buf), count, names
  65. }
  66. //sysnb pipe(p *[2]_C_int) (n int, err error)
  67. func Pipe(p []int) (err error) {
  68. if len(p) != 2 {
  69. return EINVAL
  70. }
  71. var pp [2]_C_int
  72. n, err := pipe(&pp)
  73. if n != 0 {
  74. return err
  75. }
  76. p[0] = int(pp[0])
  77. p[1] = int(pp[1])
  78. return nil
  79. }
  80. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  81. if sa.Port < 0 || sa.Port > 0xFFFF {
  82. return nil, 0, EINVAL
  83. }
  84. sa.raw.Family = AF_INET
  85. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  86. p[0] = byte(sa.Port >> 8)
  87. p[1] = byte(sa.Port)
  88. for i := 0; i < len(sa.Addr); i++ {
  89. sa.raw.Addr[i] = sa.Addr[i]
  90. }
  91. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
  92. }
  93. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  94. if sa.Port < 0 || sa.Port > 0xFFFF {
  95. return nil, 0, EINVAL
  96. }
  97. sa.raw.Family = AF_INET6
  98. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  99. p[0] = byte(sa.Port >> 8)
  100. p[1] = byte(sa.Port)
  101. sa.raw.Scope_id = sa.ZoneId
  102. for i := 0; i < len(sa.Addr); i++ {
  103. sa.raw.Addr[i] = sa.Addr[i]
  104. }
  105. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
  106. }
  107. func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
  108. name := sa.Name
  109. n := len(name)
  110. if n >= len(sa.raw.Path) {
  111. return nil, 0, EINVAL
  112. }
  113. sa.raw.Family = AF_UNIX
  114. for i := 0; i < n; i++ {
  115. sa.raw.Path[i] = int8(name[i])
  116. }
  117. // length is family (uint16), name, NUL.
  118. sl := _Socklen(2)
  119. if n > 0 {
  120. sl += _Socklen(n) + 1
  121. }
  122. if sa.raw.Path[0] == '@' {
  123. sa.raw.Path[0] = 0
  124. // Don't count trailing NUL for abstract address.
  125. sl--
  126. }
  127. return unsafe.Pointer(&sa.raw), sl, nil
  128. }
  129. //sys getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getsockname
  130. func Getsockname(fd int) (sa Sockaddr, err error) {
  131. var rsa RawSockaddrAny
  132. var len _Socklen = SizeofSockaddrAny
  133. if err = getsockname(fd, &rsa, &len); err != nil {
  134. return
  135. }
  136. return anyToSockaddr(&rsa)
  137. }
  138. const ImplementsGetwd = true
  139. //sys Getcwd(buf []byte) (n int, err error)
  140. func Getwd() (wd string, err error) {
  141. var buf [PathMax]byte
  142. // Getcwd will return an error if it failed for any reason.
  143. _, err = Getcwd(buf[0:])
  144. if err != nil {
  145. return "", err
  146. }
  147. n := clen(buf[:])
  148. if n < 1 {
  149. return "", EINVAL
  150. }
  151. return string(buf[:n]), nil
  152. }
  153. /*
  154. * Wrapped
  155. */
  156. //sysnb getgroups(ngid int, gid *_Gid_t) (n int, err error)
  157. //sysnb setgroups(ngid int, gid *_Gid_t) (err error)
  158. func Getgroups() (gids []int, err error) {
  159. n, err := getgroups(0, nil)
  160. // Check for error and sanity check group count. Newer versions of
  161. // Solaris allow up to 1024 (NGROUPS_MAX).
  162. if n < 0 || n > 1024 {
  163. if err != nil {
  164. return nil, err
  165. }
  166. return nil, EINVAL
  167. } else if n == 0 {
  168. return nil, nil
  169. }
  170. a := make([]_Gid_t, n)
  171. n, err = getgroups(n, &a[0])
  172. if n == -1 {
  173. return nil, err
  174. }
  175. gids = make([]int, n)
  176. for i, v := range a[0:n] {
  177. gids[i] = int(v)
  178. }
  179. return
  180. }
  181. func Setgroups(gids []int) (err error) {
  182. if len(gids) == 0 {
  183. return setgroups(0, nil)
  184. }
  185. a := make([]_Gid_t, len(gids))
  186. for i, v := range gids {
  187. a[i] = _Gid_t(v)
  188. }
  189. return setgroups(len(a), &a[0])
  190. }
  191. func ReadDirent(fd int, buf []byte) (n int, err error) {
  192. // Final argument is (basep *uintptr) and the syscall doesn't take nil.
  193. // TODO(rsc): Can we use a single global basep for all calls?
  194. return Getdents(fd, buf, new(uintptr))
  195. }
  196. // Wait status is 7 bits at bottom, either 0 (exited),
  197. // 0x7F (stopped), or a signal number that caused an exit.
  198. // The 0x80 bit is whether there was a core dump.
  199. // An extra number (exit code, signal causing a stop)
  200. // is in the high bits.
  201. type WaitStatus uint32
  202. const (
  203. mask = 0x7F
  204. core = 0x80
  205. shift = 8
  206. exited = 0
  207. stopped = 0x7F
  208. )
  209. func (w WaitStatus) Exited() bool { return w&mask == exited }
  210. func (w WaitStatus) ExitStatus() int {
  211. if w&mask != exited {
  212. return -1
  213. }
  214. return int(w >> shift)
  215. }
  216. func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
  217. func (w WaitStatus) Signal() syscall.Signal {
  218. sig := syscall.Signal(w & mask)
  219. if sig == stopped || sig == 0 {
  220. return -1
  221. }
  222. return sig
  223. }
  224. func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
  225. func (w WaitStatus) Stopped() bool { return w&mask == stopped && syscall.Signal(w>>shift) != SIGSTOP }
  226. func (w WaitStatus) Continued() bool { return w&mask == stopped && syscall.Signal(w>>shift) == SIGSTOP }
  227. func (w WaitStatus) StopSignal() syscall.Signal {
  228. if !w.Stopped() {
  229. return -1
  230. }
  231. return syscall.Signal(w>>shift) & 0xFF
  232. }
  233. func (w WaitStatus) TrapCause() int { return -1 }
  234. //sys wait4(pid int32, statusp *_C_int, options int, rusage *Rusage) (wpid int32, err error)
  235. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (int, error) {
  236. var status _C_int
  237. rpid, err := wait4(int32(pid), &status, options, rusage)
  238. wpid := int(rpid)
  239. if wpid == -1 {
  240. return wpid, err
  241. }
  242. if wstatus != nil {
  243. *wstatus = WaitStatus(status)
  244. }
  245. return wpid, nil
  246. }
  247. //sys gethostname(buf []byte) (n int, err error)
  248. func Gethostname() (name string, err error) {
  249. var buf [MaxHostNameLen]byte
  250. n, err := gethostname(buf[:])
  251. if n != 0 {
  252. return "", err
  253. }
  254. n = clen(buf[:])
  255. if n < 1 {
  256. return "", EFAULT
  257. }
  258. return string(buf[:n]), nil
  259. }
  260. //sys utimes(path string, times *[2]Timeval) (err error)
  261. func Utimes(path string, tv []Timeval) (err error) {
  262. if tv == nil {
  263. return utimes(path, nil)
  264. }
  265. if len(tv) != 2 {
  266. return EINVAL
  267. }
  268. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  269. }
  270. //sys utimensat(fd int, path string, times *[2]Timespec, flag int) (err error)
  271. func UtimesNano(path string, ts []Timespec) error {
  272. if ts == nil {
  273. return utimensat(AT_FDCWD, path, nil, 0)
  274. }
  275. if len(ts) != 2 {
  276. return EINVAL
  277. }
  278. return utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
  279. }
  280. func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
  281. if ts == nil {
  282. return utimensat(dirfd, path, nil, flags)
  283. }
  284. if len(ts) != 2 {
  285. return EINVAL
  286. }
  287. return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
  288. }
  289. //sys fcntl(fd int, cmd int, arg int) (val int, err error)
  290. // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
  291. func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
  292. _, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procfcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(unsafe.Pointer(lk)), 0, 0, 0)
  293. if e1 != 0 {
  294. return e1
  295. }
  296. return nil
  297. }
  298. //sys futimesat(fildes int, path *byte, times *[2]Timeval) (err error)
  299. func Futimesat(dirfd int, path string, tv []Timeval) error {
  300. pathp, err := BytePtrFromString(path)
  301. if err != nil {
  302. return err
  303. }
  304. if tv == nil {
  305. return futimesat(dirfd, pathp, nil)
  306. }
  307. if len(tv) != 2 {
  308. return EINVAL
  309. }
  310. return futimesat(dirfd, pathp, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  311. }
  312. // Solaris doesn't have an futimes function because it allows NULL to be
  313. // specified as the path for futimesat. However, Go doesn't like
  314. // NULL-style string interfaces, so this simple wrapper is provided.
  315. func Futimes(fd int, tv []Timeval) error {
  316. if tv == nil {
  317. return futimesat(fd, nil, nil)
  318. }
  319. if len(tv) != 2 {
  320. return EINVAL
  321. }
  322. return futimesat(fd, nil, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  323. }
  324. func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
  325. switch rsa.Addr.Family {
  326. case AF_UNIX:
  327. pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
  328. sa := new(SockaddrUnix)
  329. // Assume path ends at NUL.
  330. // This is not technically the Solaris semantics for
  331. // abstract Unix domain sockets -- they are supposed
  332. // to be uninterpreted fixed-size binary blobs -- but
  333. // everyone uses this convention.
  334. n := 0
  335. for n < len(pp.Path) && pp.Path[n] != 0 {
  336. n++
  337. }
  338. bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
  339. sa.Name = string(bytes)
  340. return sa, nil
  341. case AF_INET:
  342. pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
  343. sa := new(SockaddrInet4)
  344. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  345. sa.Port = int(p[0])<<8 + int(p[1])
  346. for i := 0; i < len(sa.Addr); i++ {
  347. sa.Addr[i] = pp.Addr[i]
  348. }
  349. return sa, nil
  350. case AF_INET6:
  351. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  352. sa := new(SockaddrInet6)
  353. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  354. sa.Port = int(p[0])<<8 + int(p[1])
  355. sa.ZoneId = pp.Scope_id
  356. for i := 0; i < len(sa.Addr); i++ {
  357. sa.Addr[i] = pp.Addr[i]
  358. }
  359. return sa, nil
  360. }
  361. return nil, EAFNOSUPPORT
  362. }
  363. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) = libsocket.accept
  364. func Accept(fd int) (nfd int, sa Sockaddr, err error) {
  365. var rsa RawSockaddrAny
  366. var len _Socklen = SizeofSockaddrAny
  367. nfd, err = accept(fd, &rsa, &len)
  368. if nfd == -1 {
  369. return
  370. }
  371. sa, err = anyToSockaddr(&rsa)
  372. if err != nil {
  373. Close(nfd)
  374. nfd = 0
  375. }
  376. return
  377. }
  378. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.recvmsg
  379. func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
  380. var msg Msghdr
  381. var rsa RawSockaddrAny
  382. msg.Name = (*byte)(unsafe.Pointer(&rsa))
  383. msg.Namelen = uint32(SizeofSockaddrAny)
  384. var iov Iovec
  385. if len(p) > 0 {
  386. iov.Base = (*int8)(unsafe.Pointer(&p[0]))
  387. iov.SetLen(len(p))
  388. }
  389. var dummy int8
  390. if len(oob) > 0 {
  391. // receive at least one normal byte
  392. if len(p) == 0 {
  393. iov.Base = &dummy
  394. iov.SetLen(1)
  395. }
  396. msg.Accrights = (*int8)(unsafe.Pointer(&oob[0]))
  397. }
  398. msg.Iov = &iov
  399. msg.Iovlen = 1
  400. if n, err = recvmsg(fd, &msg, flags); n == -1 {
  401. return
  402. }
  403. oobn = int(msg.Accrightslen)
  404. // source address is only specified if the socket is unconnected
  405. if rsa.Addr.Family != AF_UNSPEC {
  406. from, err = anyToSockaddr(&rsa)
  407. }
  408. return
  409. }
  410. func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
  411. _, err = SendmsgN(fd, p, oob, to, flags)
  412. return
  413. }
  414. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.sendmsg
  415. func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
  416. var ptr unsafe.Pointer
  417. var salen _Socklen
  418. if to != nil {
  419. ptr, salen, err = to.sockaddr()
  420. if err != nil {
  421. return 0, err
  422. }
  423. }
  424. var msg Msghdr
  425. msg.Name = (*byte)(unsafe.Pointer(ptr))
  426. msg.Namelen = uint32(salen)
  427. var iov Iovec
  428. if len(p) > 0 {
  429. iov.Base = (*int8)(unsafe.Pointer(&p[0]))
  430. iov.SetLen(len(p))
  431. }
  432. var dummy int8
  433. if len(oob) > 0 {
  434. // send at least one normal byte
  435. if len(p) == 0 {
  436. iov.Base = &dummy
  437. iov.SetLen(1)
  438. }
  439. msg.Accrights = (*int8)(unsafe.Pointer(&oob[0]))
  440. }
  441. msg.Iov = &iov
  442. msg.Iovlen = 1
  443. if n, err = sendmsg(fd, &msg, flags); err != nil {
  444. return 0, err
  445. }
  446. if len(oob) > 0 && len(p) == 0 {
  447. n = 0
  448. }
  449. return n, nil
  450. }
  451. //sys acct(path *byte) (err error)
  452. func Acct(path string) (err error) {
  453. if len(path) == 0 {
  454. // Assume caller wants to disable accounting.
  455. return acct(nil)
  456. }
  457. pathp, err := BytePtrFromString(path)
  458. if err != nil {
  459. return err
  460. }
  461. return acct(pathp)
  462. }
  463. /*
  464. * Expose the ioctl function
  465. */
  466. //sys ioctl(fd int, req int, arg uintptr) (err error)
  467. func IoctlSetInt(fd int, req int, value int) (err error) {
  468. return ioctl(fd, req, uintptr(value))
  469. }
  470. func IoctlSetWinsize(fd int, req int, value *Winsize) (err error) {
  471. return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  472. }
  473. func IoctlSetTermios(fd int, req int, value *Termios) (err error) {
  474. return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  475. }
  476. func IoctlSetTermio(fd int, req int, value *Termio) (err error) {
  477. return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  478. }
  479. func IoctlGetInt(fd int, req int) (int, error) {
  480. var value int
  481. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  482. return value, err
  483. }
  484. func IoctlGetWinsize(fd int, req int) (*Winsize, error) {
  485. var value Winsize
  486. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  487. return &value, err
  488. }
  489. func IoctlGetTermios(fd int, req int) (*Termios, error) {
  490. var value Termios
  491. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  492. return &value, err
  493. }
  494. func IoctlGetTermio(fd int, req int) (*Termio, error) {
  495. var value Termio
  496. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  497. return &value, err
  498. }
  499. /*
  500. * Exposed directly
  501. */
  502. //sys Access(path string, mode uint32) (err error)
  503. //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
  504. //sys Chdir(path string) (err error)
  505. //sys Chmod(path string, mode uint32) (err error)
  506. //sys Chown(path string, uid int, gid int) (err error)
  507. //sys Chroot(path string) (err error)
  508. //sys Close(fd int) (err error)
  509. //sys Creat(path string, mode uint32) (fd int, err error)
  510. //sys Dup(fd int) (nfd int, err error)
  511. //sys Dup2(oldfd int, newfd int) (err error)
  512. //sys Exit(code int)
  513. //sys Fchdir(fd int) (err error)
  514. //sys Fchmod(fd int, mode uint32) (err error)
  515. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  516. //sys Fchown(fd int, uid int, gid int) (err error)
  517. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  518. //sys Fdatasync(fd int) (err error)
  519. //sys Fpathconf(fd int, name int) (val int, err error)
  520. //sys Fstat(fd int, stat *Stat_t) (err error)
  521. //sys Getdents(fd int, buf []byte, basep *uintptr) (n int, err error)
  522. //sysnb Getgid() (gid int)
  523. //sysnb Getpid() (pid int)
  524. //sysnb Getpgid(pid int) (pgid int, err error)
  525. //sysnb Getpgrp() (pgid int, err error)
  526. //sys Geteuid() (euid int)
  527. //sys Getegid() (egid int)
  528. //sys Getppid() (ppid int)
  529. //sys Getpriority(which int, who int) (n int, err error)
  530. //sysnb Getrlimit(which int, lim *Rlimit) (err error)
  531. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  532. //sysnb Gettimeofday(tv *Timeval) (err error)
  533. //sysnb Getuid() (uid int)
  534. //sys Kill(pid int, signum syscall.Signal) (err error)
  535. //sys Lchown(path string, uid int, gid int) (err error)
  536. //sys Link(path string, link string) (err error)
  537. //sys Listen(s int, backlog int) (err error) = libsocket.listen
  538. //sys Lstat(path string, stat *Stat_t) (err error)
  539. //sys Madvise(b []byte, advice int) (err error)
  540. //sys Mkdir(path string, mode uint32) (err error)
  541. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  542. //sys Mkfifo(path string, mode uint32) (err error)
  543. //sys Mkfifoat(dirfd int, path string, mode uint32) (err error)
  544. //sys Mknod(path string, mode uint32, dev int) (err error)
  545. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  546. //sys Mlock(b []byte) (err error)
  547. //sys Mlockall(flags int) (err error)
  548. //sys Mprotect(b []byte, prot int) (err error)
  549. //sys Munlock(b []byte) (err error)
  550. //sys Munlockall() (err error)
  551. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  552. //sys Open(path string, mode int, perm uint32) (fd int, err error)
  553. //sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
  554. //sys Pathconf(path string, name int) (val int, err error)
  555. //sys Pause() (err error)
  556. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  557. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  558. //sys read(fd int, p []byte) (n int, err error)
  559. //sys Readlink(path string, buf []byte) (n int, err error)
  560. //sys Rename(from string, to string) (err error)
  561. //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
  562. //sys Rmdir(path string) (err error)
  563. //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = lseek
  564. //sysnb Setegid(egid int) (err error)
  565. //sysnb Seteuid(euid int) (err error)
  566. //sysnb Setgid(gid int) (err error)
  567. //sys Sethostname(p []byte) (err error)
  568. //sysnb Setpgid(pid int, pgid int) (err error)
  569. //sys Setpriority(which int, who int, prio int) (err error)
  570. //sysnb Setregid(rgid int, egid int) (err error)
  571. //sysnb Setreuid(ruid int, euid int) (err error)
  572. //sysnb Setrlimit(which int, lim *Rlimit) (err error)
  573. //sysnb Setsid() (pid int, err error)
  574. //sysnb Setuid(uid int) (err error)
  575. //sys Shutdown(s int, how int) (err error) = libsocket.shutdown
  576. //sys Stat(path string, stat *Stat_t) (err error)
  577. //sys Symlink(path string, link string) (err error)
  578. //sys Sync() (err error)
  579. //sysnb Times(tms *Tms) (ticks uintptr, err error)
  580. //sys Truncate(path string, length int64) (err error)
  581. //sys Fsync(fd int) (err error)
  582. //sys Ftruncate(fd int, length int64) (err error)
  583. //sys Umask(mask int) (oldmask int)
  584. //sysnb Uname(buf *Utsname) (err error)
  585. //sys Unmount(target string, flags int) (err error) = libc.umount
  586. //sys Unlink(path string) (err error)
  587. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  588. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  589. //sys Utime(path string, buf *Utimbuf) (err error)
  590. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.bind
  591. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.connect
  592. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
  593. //sys munmap(addr uintptr, length uintptr) (err error)
  594. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.sendto
  595. //sys socket(domain int, typ int, proto int) (fd int, err error) = libsocket.socket
  596. //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) = libsocket.socketpair
  597. //sys write(fd int, p []byte) (n int, err error)
  598. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) = libsocket.getsockopt
  599. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getpeername
  600. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) = libsocket.setsockopt
  601. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) = libsocket.recvfrom
  602. func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
  603. r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procread)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
  604. n = int(r0)
  605. if e1 != 0 {
  606. err = e1
  607. }
  608. return
  609. }
  610. func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
  611. r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&procwrite)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
  612. n = int(r0)
  613. if e1 != 0 {
  614. err = e1
  615. }
  616. return
  617. }
  618. var mapper = &mmapper{
  619. active: make(map[*byte][]byte),
  620. mmap: mmap,
  621. munmap: munmap,
  622. }
  623. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  624. return mapper.Mmap(fd, offset, length, prot, flags)
  625. }
  626. func Munmap(b []byte) (err error) {
  627. return mapper.Munmap(b)
  628. }
  629. //sys sysconf(name int) (n int64, err error)
  630. // pageSize caches the value of Getpagesize, since it can't change
  631. // once the system is booted.
  632. var pageSize int64 // accessed atomically
  633. func Getpagesize() int {
  634. n := atomic.LoadInt64(&pageSize)
  635. if n == 0 {
  636. n, _ = sysconf(_SC_PAGESIZE)
  637. atomic.StoreInt64(&pageSize, n)
  638. }
  639. return int(n)
  640. }