1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package unix
16
17 import "unsafe"
18
19
22
23 func Access(path string, mode uint32) (err error) {
24 return Faccessat(AT_FDCWD, path, mode, 0)
25 }
26
27 func Chmod(path string, mode uint32) (err error) {
28 return Fchmodat(AT_FDCWD, path, mode, 0)
29 }
30
31 func Chown(path string, uid int, gid int) (err error) {
32 return Fchownat(AT_FDCWD, path, uid, gid, 0)
33 }
34
35 func Creat(path string, mode uint32) (fd int, err error) {
36 return Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode)
37 }
38
39
40 func Utimes(path string, tv []Timeval) error {
41 if len(tv) != 2 {
42 return EINVAL
43 }
44 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
45 }
46
47
48 func UtimesNano(path string, ts []Timespec) error {
49 if len(ts) != 2 {
50 return EINVAL
51 }
52 return utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
53 }
54
55 func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
56 if ts == nil {
57 return utimensat(dirfd, path, nil, flags)
58 }
59 if len(ts) != 2 {
60 return EINVAL
61 }
62 return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
63 }
64
65 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
66 if sa.Port < 0 || sa.Port > 0xFFFF {
67 return nil, 0, EINVAL
68 }
69 sa.raw.Family = AF_INET
70 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
71 p[0] = byte(sa.Port >> 8)
72 p[1] = byte(sa.Port)
73 sa.raw.Addr = sa.Addr
74 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
75 }
76
77 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
78 if sa.Port < 0 || sa.Port > 0xFFFF {
79 return nil, 0, EINVAL
80 }
81 sa.raw.Family = AF_INET6
82 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
83 p[0] = byte(sa.Port >> 8)
84 p[1] = byte(sa.Port)
85 sa.raw.Scope_id = sa.ZoneId
86 sa.raw.Addr = sa.Addr
87 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
88 }
89
90 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
91 name := sa.Name
92 n := len(name)
93 if n > len(sa.raw.Path) {
94 return nil, 0, EINVAL
95 }
96 if n == len(sa.raw.Path) && name[0] != '@' {
97 return nil, 0, EINVAL
98 }
99 sa.raw.Family = AF_UNIX
100 for i := 0; i < n; i++ {
101 sa.raw.Path[i] = uint8(name[i])
102 }
103
104 sl := _Socklen(2)
105 if n > 0 {
106 sl += _Socklen(n) + 1
107 }
108 if sa.raw.Path[0] == '@' {
109 sa.raw.Path[0] = 0
110
111 sl--
112 }
113
114 return unsafe.Pointer(&sa.raw), sl, nil
115 }
116
117 func Getsockname(fd int) (sa Sockaddr, err error) {
118 var rsa RawSockaddrAny
119 var len _Socklen = SizeofSockaddrAny
120 if err = getsockname(fd, &rsa, &len); err != nil {
121 return
122 }
123 return anyToSockaddr(fd, &rsa)
124 }
125
126
127
128 const ImplementsGetwd = true
129
130 func Getwd() (ret string, err error) {
131 for len := uint64(4096); ; len *= 2 {
132 b := make([]byte, len)
133 err := getcwd(b)
134 if err == nil {
135 i := 0
136 for b[i] != 0 {
137 i++
138 }
139 return string(b[0:i]), nil
140 }
141 if err != ERANGE {
142 return "", err
143 }
144 }
145 }
146
147 func Getcwd(buf []byte) (n int, err error) {
148 err = getcwd(buf)
149 if err == nil {
150 i := 0
151 for buf[i] != 0 {
152 i++
153 }
154 n = i + 1
155 }
156 return
157 }
158
159 func Getgroups() (gids []int, err error) {
160 n, err := getgroups(0, nil)
161 if err != nil {
162 return nil, err
163 }
164 if n == 0 {
165 return nil, nil
166 }
167
168
169 if n < 0 || n > 1000 {
170 return nil, EINVAL
171 }
172
173 a := make([]_Gid_t, n)
174 n, err = getgroups(n, &a[0])
175 if err != nil {
176 return nil, err
177 }
178 gids = make([]int, n)
179 for i, v := range a[0:n] {
180 gids[i] = int(v)
181 }
182 return
183 }
184
185 func Setgroups(gids []int) (err error) {
186 if len(gids) == 0 {
187 return setgroups(0, nil)
188 }
189
190 a := make([]_Gid_t, len(gids))
191 for i, v := range gids {
192 a[i] = _Gid_t(v)
193 }
194 return setgroups(len(a), &a[0])
195 }
196
197
200
201
202
203 func Accept(fd int) (nfd int, sa Sockaddr, err error) {
204 var rsa RawSockaddrAny
205 var len _Socklen = SizeofSockaddrAny
206 nfd, err = accept(fd, &rsa, &len)
207 if nfd == -1 {
208 return
209 }
210 sa, err = anyToSockaddr(fd, &rsa)
211 if err != nil {
212 Close(nfd)
213 nfd = 0
214 }
215 return
216 }
217
218 func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
219
220 sa := new(SockaddrUnix)
221 return -1, -1, -1, sa, ENOSYS
222 }
223
224 func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
225 _, err = SendmsgN(fd, p, oob, to, flags)
226 return
227 }
228
229 func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
230
231 return -1, ENOSYS
232 }
233
234 func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
235 switch rsa.Addr.Family {
236
237 case AF_UNIX:
238 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
239 sa := new(SockaddrUnix)
240
241
242
243 n := SizeofSockaddrUnix - 3
244 for i := 0; i < n; i++ {
245 if pp.Path[i] == 0 {
246 n = i
247 break
248 }
249 }
250
251 bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
252 sa.Name = string(bytes)
253 return sa, nil
254
255 case AF_INET:
256 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
257 sa := new(SockaddrInet4)
258 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
259 sa.Port = int(p[0])<<8 + int(p[1])
260 sa.Addr = pp.Addr
261 return sa, nil
262
263 case AF_INET6:
264 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
265 sa := new(SockaddrInet6)
266 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
267 sa.Port = int(p[0])<<8 + int(p[1])
268 sa.ZoneId = pp.Scope_id
269 sa.Addr = pp.Addr
270 return sa, nil
271 }
272 return nil, EAFNOSUPPORT
273 }
274
275 func Gettimeofday(tv *Timeval) (err error) {
276 err = gettimeofday(tv, nil)
277 return
278 }
279
280 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
281 if raceenabled {
282 raceReleaseMerge(unsafe.Pointer(&ioSync))
283 }
284 return sendfile(outfd, infd, offset, count)
285 }
286
287
288 func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
289 return -1, ENOSYS
290 }
291
292 func direntIno(buf []byte) (uint64, bool) {
293 return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
294 }
295
296 func direntReclen(buf []byte) (uint64, bool) {
297 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
298 }
299
300 func direntNamlen(buf []byte) (uint64, bool) {
301 reclen, ok := direntReclen(buf)
302 if !ok {
303 return 0, false
304 }
305 return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
306 }
307
308
309 func Getdents(fd int, buf []byte) (n int, err error) {
310 return getdirent(fd, buf)
311 }
312
313
314 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
315 var status _C_int
316 var r Pid_t
317 err = ERESTART
318
319
320 for err == ERESTART {
321 r, err = wait4(Pid_t(pid), &status, options, rusage)
322 }
323 wpid = int(r)
324 if wstatus != nil {
325 *wstatus = WaitStatus(status)
326 }
327 return
328 }
329
330
333
334 type WaitStatus uint32
335
336 func (w WaitStatus) Stopped() bool { return w&0x40 != 0 }
337 func (w WaitStatus) StopSignal() Signal {
338 if !w.Stopped() {
339 return -1
340 }
341 return Signal(w>>8) & 0xFF
342 }
343
344 func (w WaitStatus) Exited() bool { return w&0xFF == 0 }
345 func (w WaitStatus) ExitStatus() int {
346 if !w.Exited() {
347 return -1
348 }
349 return int((w >> 8) & 0xFF)
350 }
351
352 func (w WaitStatus) Signaled() bool { return w&0x40 == 0 && w&0xFF != 0 }
353 func (w WaitStatus) Signal() Signal {
354 if !w.Signaled() {
355 return -1
356 }
357 return Signal(w>>16) & 0xFF
358 }
359
360 func (w WaitStatus) Continued() bool { return w&0x01000000 != 0 }
361
362 func (w WaitStatus) CoreDump() bool { return w&0x80 == 0x80 }
363
364 func (w WaitStatus) TrapCause() int { return -1 }
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381 func Fsync(fd int) error {
382 return fsyncRange(fd, O_SYNC, 0, 0)
383 }
384
385
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492 var mapper = &mmapper{
493 active: make(map[*byte][]byte),
494 mmap: mmap,
495 munmap: munmap,
496 }
497
498 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
499 return mapper.Mmap(fd, offset, length, prot, flags)
500 }
501
502 func Munmap(b []byte) (err error) {
503 return mapper.Munmap(b)
504 }
505
506
507
508
509
510
511
512
513
514
515
516 func Pipe(p []int) (err error) {
517 if len(p) != 2 {
518 return EINVAL
519 }
520 var pp [2]_C_int
521 err = pipe(&pp)
522 p[0] = int(pp[0])
523 p[1] = int(pp[1])
524 return
525 }
526
527
528
529 func Poll(fds []PollFd, timeout int) (n int, err error) {
530 if len(fds) == 0 {
531 return poll(nil, 0, timeout)
532 }
533 return poll(&fds[0], len(fds), timeout)
534 }
535
536
537
538
539
540
541
542
543 func Unmount(target string, flags int) (err error) {
544 if flags != 0 {
545
546 return ENOSYS
547 }
548 return umount(target)
549 }
550
View as plain text