syscall_linux_386.go 11 KB

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