syscall_linux_386.go 10.0 KB

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