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