syscall_linux_386.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // TODO(rsc): Rewrite all nn(SP) references into name+(nn-8)(FP)
  5. // so that go vet can check that they are correct.
  6. // +build 386,linux
  7. package unix
  8. import (
  9. "syscall"
  10. "unsafe"
  11. )
  12. func Getpagesize() int { return 4096 }
  13. func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }
  14. func NsecToTimespec(nsec int64) (ts Timespec) {
  15. ts.Sec = int32(nsec / 1e9)
  16. ts.Nsec = int32(nsec % 1e9)
  17. return
  18. }
  19. func NsecToTimeval(nsec int64) (tv Timeval) {
  20. nsec += 999 // round up to microsecond
  21. tv.Sec = int32(nsec / 1e9)
  22. tv.Usec = int32(nsec % 1e9 / 1e3)
  23. return
  24. }
  25. //sysnb pipe(p *[2]_C_int) (err error)
  26. func Pipe(p []int) (err error) {
  27. if len(p) != 2 {
  28. return EINVAL
  29. }
  30. var pp [2]_C_int
  31. err = pipe(&pp)
  32. p[0] = int(pp[0])
  33. p[1] = int(pp[1])
  34. return
  35. }
  36. //sysnb pipe2(p *[2]_C_int, flags int) (err error)
  37. func Pipe2(p []int, flags int) (err error) {
  38. if len(p) != 2 {
  39. return EINVAL
  40. }
  41. var pp [2]_C_int
  42. err = pipe2(&pp, flags)
  43. p[0] = int(pp[0])
  44. p[1] = int(pp[1])
  45. return
  46. }
  47. // 64-bit file system and 32-bit uid calls
  48. // (386 default is 32-bit file system and 16-bit uid).
  49. //sys Dup2(oldfd int, newfd int) (err error)
  50. //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_FADVISE64_64
  51. //sys Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32
  52. //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
  53. //sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
  54. //sysnb Getegid() (egid int) = SYS_GETEGID32
  55. //sysnb Geteuid() (euid int) = SYS_GETEUID32
  56. //sysnb Getgid() (gid int) = SYS_GETGID32
  57. //sysnb Getuid() (uid int) = SYS_GETUID32
  58. //sysnb InotifyInit() (fd int, err error)
  59. //sys Ioperm(from int, num int, on int) (err error)
  60. //sys Iopl(level int) (err error)
  61. //sys Lchown(path string, uid int, gid int) (err error) = SYS_LCHOWN32
  62. //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
  63. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
  64. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
  65. //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64
  66. //sys Setfsgid(gid int) (err error) = SYS_SETFSGID32
  67. //sys Setfsuid(uid int) (err error) = SYS_SETFSUID32
  68. //sysnb Setregid(rgid int, egid int) (err error) = SYS_SETREGID32
  69. //sysnb Setresgid(rgid int, egid int, sgid int) (err error) = SYS_SETRESGID32
  70. //sysnb Setresuid(ruid int, euid int, suid int) (err error) = SYS_SETRESUID32
  71. //sysnb Setreuid(ruid int, euid int) (err error) = SYS_SETREUID32
  72. //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
  73. //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
  74. //sys SyncFileRange(fd int, off int64, n int64, flags int) (err error)
  75. //sys Truncate(path string, length int64) (err error) = SYS_TRUNCATE64
  76. //sysnb getgroups(n int, list *_Gid_t) (nn int, err error) = SYS_GETGROUPS32
  77. //sysnb setgroups(n int, list *_Gid_t) (err error) = SYS_SETGROUPS32
  78. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT
  79. //sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
  80. //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
  81. //sys Pause() (err error)
  82. func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
  83. page := uintptr(offset / 4096)
  84. if offset != int64(page)*4096 {
  85. return 0, EINVAL
  86. }
  87. return mmap2(addr, length, prot, flags, fd, page)
  88. }
  89. type rlimit32 struct {
  90. Cur uint32
  91. Max uint32
  92. }
  93. //sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_GETRLIMIT
  94. const rlimInf32 = ^uint32(0)
  95. const rlimInf64 = ^uint64(0)
  96. func Getrlimit(resource int, rlim *Rlimit) (err error) {
  97. err = prlimit(0, resource, nil, rlim)
  98. if err != ENOSYS {
  99. return err
  100. }
  101. rl := rlimit32{}
  102. err = getrlimit(resource, &rl)
  103. if err != nil {
  104. return
  105. }
  106. if rl.Cur == rlimInf32 {
  107. rlim.Cur = rlimInf64
  108. } else {
  109. rlim.Cur = uint64(rl.Cur)
  110. }
  111. if rl.Max == rlimInf32 {
  112. rlim.Max = rlimInf64
  113. } else {
  114. rlim.Max = uint64(rl.Max)
  115. }
  116. return
  117. }
  118. //sysnb setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
  119. func Setrlimit(resource int, rlim *Rlimit) (err error) {
  120. err = prlimit(0, resource, rlim, nil)
  121. if err != ENOSYS {
  122. return err
  123. }
  124. rl := rlimit32{}
  125. if rlim.Cur == rlimInf64 {
  126. rl.Cur = rlimInf32
  127. } else if rlim.Cur < uint64(rlimInf32) {
  128. rl.Cur = uint32(rlim.Cur)
  129. } else {
  130. return EINVAL
  131. }
  132. if rlim.Max == rlimInf64 {
  133. rl.Max = rlimInf32
  134. } else if rlim.Max < uint64(rlimInf32) {
  135. rl.Max = uint32(rlim.Max)
  136. } else {
  137. return EINVAL
  138. }
  139. return setrlimit(resource, &rl)
  140. }
  141. // Underlying system call writes to newoffset via pointer.
  142. // Implemented in assembly to avoid allocation.
  143. func seek(fd int, offset int64, whence int) (newoffset int64, err syscall.Errno)
  144. func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
  145. newoffset, errno := seek(fd, offset, whence)
  146. if errno != 0 {
  147. return 0, errno
  148. }
  149. return newoffset, nil
  150. }
  151. // Vsyscalls on amd64.
  152. //sysnb Gettimeofday(tv *Timeval) (err error)
  153. //sysnb Time(t *Time_t) (tt Time_t, err error)
  154. //sys Utime(path string, buf *Utimbuf) (err error)
  155. // On x86 Linux, all the socket calls go through an extra indirection,
  156. // I think because the 5-register system call interface can't handle
  157. // the 6-argument calls like sendto and recvfrom. Instead the
  158. // arguments to the underlying system call are the number below
  159. // and a pointer to an array of uintptr. We hide the pointer in the
  160. // socketcall assembly to avoid allocation on every system call.
  161. const (
  162. // see linux/net.h
  163. _SOCKET = 1
  164. _BIND = 2
  165. _CONNECT = 3
  166. _LISTEN = 4
  167. _ACCEPT = 5
  168. _GETSOCKNAME = 6
  169. _GETPEERNAME = 7
  170. _SOCKETPAIR = 8
  171. _SEND = 9
  172. _RECV = 10
  173. _SENDTO = 11
  174. _RECVFROM = 12
  175. _SHUTDOWN = 13
  176. _SETSOCKOPT = 14
  177. _GETSOCKOPT = 15
  178. _SENDMSG = 16
  179. _RECVMSG = 17
  180. _ACCEPT4 = 18
  181. _RECVMMSG = 19
  182. _SENDMMSG = 20
  183. )
  184. func socketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err syscall.Errno)
  185. func rawsocketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err syscall.Errno)
  186. func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) {
  187. fd, e := socketcall(_ACCEPT, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
  188. if e != 0 {
  189. err = e
  190. }
  191. return
  192. }
  193. func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) {
  194. fd, e := socketcall(_ACCEPT4, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags), 0, 0)
  195. if e != 0 {
  196. err = e
  197. }
  198. return
  199. }
  200. func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
  201. _, e := rawsocketcall(_GETSOCKNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
  202. if e != 0 {
  203. err = e
  204. }
  205. return
  206. }
  207. func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
  208. _, e := rawsocketcall(_GETPEERNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
  209. if e != 0 {
  210. err = e
  211. }
  212. return
  213. }
  214. func socketpair(domain int, typ int, flags int, fd *[2]int32) (err error) {
  215. _, e := rawsocketcall(_SOCKETPAIR, uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd)), 0, 0)
  216. if e != 0 {
  217. err = e
  218. }
  219. return
  220. }
  221. func bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
  222. _, e := socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
  223. if e != 0 {
  224. err = e
  225. }
  226. return
  227. }
  228. func connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
  229. _, e := socketcall(_CONNECT, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
  230. if e != 0 {
  231. err = e
  232. }
  233. return
  234. }
  235. func socket(domain int, typ int, proto int) (fd int, err error) {
  236. fd, e := rawsocketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0)
  237. if e != 0 {
  238. err = e
  239. }
  240. return
  241. }
  242. func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) {
  243. _, e := socketcall(_GETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen)), 0)
  244. if e != 0 {
  245. err = e
  246. }
  247. return
  248. }
  249. func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) {
  250. _, e := socketcall(_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), vallen, 0)
  251. if e != 0 {
  252. err = e
  253. }
  254. return
  255. }
  256. func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) {
  257. var base uintptr
  258. if len(p) > 0 {
  259. base = uintptr(unsafe.Pointer(&p[0]))
  260. }
  261. n, e := socketcall(_RECVFROM, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen)))
  262. if e != 0 {
  263. err = e
  264. }
  265. return
  266. }
  267. func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) {
  268. var base uintptr
  269. if len(p) > 0 {
  270. base = uintptr(unsafe.Pointer(&p[0]))
  271. }
  272. _, e := socketcall(_SENDTO, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen))
  273. if e != 0 {
  274. err = e
  275. }
  276. return
  277. }
  278. func recvmsg(s int, msg *Msghdr, flags int) (n int, err error) {
  279. n, e := socketcall(_RECVMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
  280. if e != 0 {
  281. err = e
  282. }
  283. return
  284. }
  285. func sendmsg(s int, msg *Msghdr, flags int) (n int, err error) {
  286. n, e := socketcall(_SENDMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
  287. if e != 0 {
  288. err = e
  289. }
  290. return
  291. }
  292. func Listen(s int, n int) (err error) {
  293. _, e := socketcall(_LISTEN, uintptr(s), uintptr(n), 0, 0, 0, 0)
  294. if e != 0 {
  295. err = e
  296. }
  297. return
  298. }
  299. func Shutdown(s, how int) (err error) {
  300. _, e := socketcall(_SHUTDOWN, uintptr(s), uintptr(how), 0, 0, 0, 0)
  301. if e != 0 {
  302. err = e
  303. }
  304. return
  305. }
  306. func Fstatfs(fd int, buf *Statfs_t) (err error) {
  307. _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  308. if e != 0 {
  309. err = e
  310. }
  311. return
  312. }
  313. func Statfs(path string, buf *Statfs_t) (err error) {
  314. pathp, err := BytePtrFromString(path)
  315. if err != nil {
  316. return err
  317. }
  318. _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  319. if e != 0 {
  320. err = e
  321. }
  322. return
  323. }
  324. func (r *PtraceRegs) PC() uint64 { return uint64(uint32(r.Eip)) }
  325. func (r *PtraceRegs) SetPC(pc uint64) { r.Eip = int32(pc) }
  326. func (iov *Iovec) SetLen(length int) {
  327. iov.Len = uint32(length)
  328. }
  329. func (msghdr *Msghdr) SetControllen(length int) {
  330. msghdr.Controllen = uint32(length)
  331. }
  332. func (cmsg *Cmsghdr) SetLen(length int) {
  333. cmsg.Len = uint32(length)
  334. }
  335. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  336. func Poll(fds []PollFd, timeout int) (n int, err error) {
  337. if len(fds) == 0 {
  338. return poll(nil, 0, timeout)
  339. }
  340. return poll(&fds[0], len(fds), timeout)
  341. }