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