1
2
3
4
5
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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
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
108
109
110
111
112
113
114
115
116
117
118
119
120 const (
121
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 }
315
View as plain text