A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

syscall_solaris.go 20KB

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