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