1
2
3
4
5
6
7
8 package unix
9
10 import (
11 "unsafe"
12 )
13
14 func setTimespec(sec, nsec int64) Timespec {
15 return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
16 }
17
18 func setTimeval(sec, usec int64) Timeval {
19 return Timeval{Sec: int32(sec), Usec: int32(usec)}
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
56
57
58
59
60 func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
61 page := uintptr(offset / 4096)
62 if offset != int64(page)*4096 {
63 return 0, EINVAL
64 }
65 return mmap2(addr, length, prot, flags, fd, page)
66 }
67
68 type rlimit32 struct {
69 Cur uint32
70 Max uint32
71 }
72
73
74
75 const rlimInf32 = ^uint32(0)
76 const rlimInf64 = ^uint64(0)
77
78 func Getrlimit(resource int, rlim *Rlimit) (err error) {
79 err = Prlimit(0, resource, nil, rlim)
80 if err != ENOSYS {
81 return err
82 }
83
84 rl := rlimit32{}
85 err = getrlimit(resource, &rl)
86 if err != nil {
87 return
88 }
89
90 if rl.Cur == rlimInf32 {
91 rlim.Cur = rlimInf64
92 } else {
93 rlim.Cur = uint64(rl.Cur)
94 }
95
96 if rl.Max == rlimInf32 {
97 rlim.Max = rlimInf64
98 } else {
99 rlim.Max = uint64(rl.Max)
100 }
101 return
102 }
103
104
105
106 func Setrlimit(resource int, rlim *Rlimit) (err error) {
107 err = Prlimit(0, resource, rlim, nil)
108 if err != ENOSYS {
109 return err
110 }
111
112 rl := rlimit32{}
113 if rlim.Cur == rlimInf64 {
114 rl.Cur = rlimInf32
115 } else if rlim.Cur < uint64(rlimInf32) {
116 rl.Cur = uint32(rlim.Cur)
117 } else {
118 return EINVAL
119 }
120 if rlim.Max == rlimInf64 {
121 rl.Max = rlimInf32
122 } else if rlim.Max < uint64(rlimInf32) {
123 rl.Max = uint32(rlim.Max)
124 } else {
125 return EINVAL
126 }
127
128 return setrlimit(resource, &rl)
129 }
130
131 func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
132 newoffset, errno := seek(fd, offset, whence)
133 if errno != 0 {
134 return 0, errno
135 }
136 return newoffset, nil
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 const (
153
154 _SOCKET = 1
155 _BIND = 2
156 _CONNECT = 3
157 _LISTEN = 4
158 _ACCEPT = 5
159 _GETSOCKNAME = 6
160 _GETPEERNAME = 7
161 _SOCKETPAIR = 8
162 _SEND = 9
163 _RECV = 10
164 _SENDTO = 11
165 _RECVFROM = 12
166 _SHUTDOWN = 13
167 _SETSOCKOPT = 14
168 _GETSOCKOPT = 15
169 _SENDMSG = 16
170 _RECVMSG = 17
171 _ACCEPT4 = 18
172 _RECVMMSG = 19
173 _SENDMMSG = 20
174 )
175
176 func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) {
177 fd, e := socketcall(_ACCEPT, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
178 if e != 0 {
179 err = e
180 }
181 return
182 }
183
184 func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) {
185 fd, e := socketcall(_ACCEPT4, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags), 0, 0)
186 if e != 0 {
187 err = e
188 }
189 return
190 }
191
192 func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
193 _, e := rawsocketcall(_GETSOCKNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
194 if e != 0 {
195 err = e
196 }
197 return
198 }
199
200 func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
201 _, e := rawsocketcall(_GETPEERNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
202 if e != 0 {
203 err = e
204 }
205 return
206 }
207
208 func socketpair(domain int, typ int, flags int, fd *[2]int32) (err error) {
209 _, e := rawsocketcall(_SOCKETPAIR, uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd)), 0, 0)
210 if e != 0 {
211 err = e
212 }
213 return
214 }
215
216 func bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
217 _, e := socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
218 if e != 0 {
219 err = e
220 }
221 return
222 }
223
224 func connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
225 _, e := socketcall(_CONNECT, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
226 if e != 0 {
227 err = e
228 }
229 return
230 }
231
232 func socket(domain int, typ int, proto int) (fd int, err error) {
233 fd, e := rawsocketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0)
234 if e != 0 {
235 err = e
236 }
237 return
238 }
239
240 func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) {
241 _, e := socketcall(_GETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen)), 0)
242 if e != 0 {
243 err = e
244 }
245 return
246 }
247
248 func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) {
249 _, e := socketcall(_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), vallen, 0)
250 if e != 0 {
251 err = e
252 }
253 return
254 }
255
256 func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) {
257 var base uintptr
258 if len(p) > 0 {
259 base = uintptr(unsafe.Pointer(&p[0]))
260 }
261 n, e := socketcall(_RECVFROM, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen)))
262 if e != 0 {
263 err = e
264 }
265 return
266 }
267
268 func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) {
269 var base uintptr
270 if len(p) > 0 {
271 base = uintptr(unsafe.Pointer(&p[0]))
272 }
273 _, e := socketcall(_SENDTO, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen))
274 if e != 0 {
275 err = e
276 }
277 return
278 }
279
280 func recvmsg(s int, msg *Msghdr, flags int) (n int, err error) {
281 n, e := socketcall(_RECVMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
282 if e != 0 {
283 err = e
284 }
285 return
286 }
287
288 func sendmsg(s int, msg *Msghdr, flags int) (n int, err error) {
289 n, e := socketcall(_SENDMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
290 if e != 0 {
291 err = e
292 }
293 return
294 }
295
296 func Listen(s int, n int) (err error) {
297 _, e := socketcall(_LISTEN, uintptr(s), uintptr(n), 0, 0, 0, 0)
298 if e != 0 {
299 err = e
300 }
301 return
302 }
303
304 func Shutdown(s, how int) (err error) {
305 _, e := socketcall(_SHUTDOWN, uintptr(s), uintptr(how), 0, 0, 0, 0)
306 if e != 0 {
307 err = e
308 }
309 return
310 }
311
312 func Fstatfs(fd int, buf *Statfs_t) (err error) {
313 _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
314 if e != 0 {
315 err = e
316 }
317 return
318 }
319
320 func Statfs(path string, buf *Statfs_t) (err error) {
321 pathp, err := BytePtrFromString(path)
322 if err != nil {
323 return err
324 }
325 _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
326 if e != 0 {
327 err = e
328 }
329 return
330 }
331
332 func (r *PtraceRegs) PC() uint64 { return uint64(uint32(r.Eip)) }
333
334 func (r *PtraceRegs) SetPC(pc uint64) { r.Eip = int32(pc) }
335
336 func (iov *Iovec) SetLen(length int) {
337 iov.Len = uint32(length)
338 }
339
340 func (msghdr *Msghdr) SetControllen(length int) {
341 msghdr.Controllen = uint32(length)
342 }
343
344 func (msghdr *Msghdr) SetIovlen(length int) {
345 msghdr.Iovlen = uint32(length)
346 }
347
348 func (cmsg *Cmsghdr) SetLen(length int) {
349 cmsg.Len = uint32(length)
350 }
351
352 func (rsa *RawSockaddrNFCLLCP) SetServiceNameLen(length int) {
353 rsa.Service_name_len = uint32(length)
354 }
355
View as plain text