You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

713 lines
20 KiB

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