Source file
src/syscall/syscall_aix.go
1
2
3
4
5
6
7
8
9
10
11
12 package syscall
13
14 import (
15 "unsafe"
16 )
17
18 func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
19 func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
20 func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
21 func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
22
23
24 func rawSyscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
25 func syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
26
27
28 const (
29 _ = iota
30 TIOCSCTTY
31 SYS_EXECVE
32 SYS_FCNTL
33 )
34
35 const (
36 F_DUPFD_CLOEXEC = 0
37
38 AF_LOCAL = AF_UNIX
39
40 _F_DUP2FD_CLOEXEC = 0
41 )
42
43 func (ts *StTimespec_t) Unix() (sec int64, nsec int64) {
44 return int64(ts.Sec), int64(ts.Nsec)
45 }
46
47 func (ts *StTimespec_t) Nano() int64 {
48 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
49 }
50
51
54
55 func Access(path string, mode uint32) (err error) {
56 return Faccessat(_AT_FDCWD, path, mode, 0)
57 }
58
59
60
61
62
63
64
65
66
67
68
69 func Pipe(p []int) (err error) {
70 if len(p) != 2 {
71 return EINVAL
72 }
73 var pp [2]_C_int
74 err = pipe(&pp)
75 if err == nil {
76 p[0] = int(pp[0])
77 p[1] = int(pp[1])
78 }
79 return
80 }
81
82
83
84 func Readlink(path string, buf []byte) (n int, err error) {
85 s := uint64(len(buf))
86 return readlink(path, buf, s)
87 }
88
89
90
91 func Utimes(path string, tv []Timeval) error {
92 if len(tv) != 2 {
93 return EINVAL
94 }
95 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
96 }
97
98
99
100 func UtimesNano(path string, ts []Timespec) error {
101 if len(ts) != 2 {
102 return EINVAL
103 }
104 return utimensat(_AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
105 }
106
107
108
109 func Unlinkat(dirfd int, path string) (err error) {
110 return unlinkat(dirfd, path, 0)
111 }
112
113
114
115 const ImplementsGetwd = true
116
117 func Getwd() (ret string, err error) {
118 for len := uint64(4096); ; len *= 2 {
119 b := make([]byte, len)
120 err := getcwd(&b[0], len)
121 if err == nil {
122 i := 0
123 for b[i] != 0 {
124 i++
125 }
126 return string(b[0:i]), nil
127 }
128 if err != ERANGE {
129 return "", err
130 }
131 }
132 }
133
134 func Getcwd(buf []byte) (n int, err error) {
135 err = getcwd(&buf[0], uint64(len(buf)))
136 if err == nil {
137 i := 0
138 for buf[i] != 0 {
139 i++
140 }
141 n = i + 1
142 }
143 return
144 }
145
146
147
148
149 func Getgroups() (gids []int, err error) {
150 n, err := getgroups(0, nil)
151 if err != nil {
152 return nil, err
153 }
154 if n == 0 {
155 return nil, nil
156 }
157
158
159 if n < 0 || n > 1000 {
160 return nil, EINVAL
161 }
162
163 a := make([]_Gid_t, n)
164 n, err = getgroups(n, &a[0])
165 if err != nil {
166 return nil, err
167 }
168 gids = make([]int, n)
169 for i, v := range a[0:n] {
170 gids[i] = int(v)
171 }
172 return
173 }
174
175 func Setgroups(gids []int) (err error) {
176 if len(gids) == 0 {
177 return setgroups(0, nil)
178 }
179
180 a := make([]_Gid_t, len(gids))
181 for i, v := range gids {
182 a[i] = _Gid_t(v)
183 }
184 return setgroups(len(a), &a[0])
185 }
186
187 func direntIno(buf []byte) (uint64, bool) {
188 return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
189 }
190
191 func direntReclen(buf []byte) (uint64, bool) {
192 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
193 }
194
195 func direntNamlen(buf []byte) (uint64, bool) {
196 reclen, ok := direntReclen(buf)
197 if !ok {
198 return 0, false
199 }
200 return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
201 }
202
203 func Gettimeofday(tv *Timeval) (err error) {
204 err = gettimeofday(tv, nil)
205 return
206 }
207
208
209 func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
210 return -1, ENOSYS
211 }
212
213
214
215 func ReadDirent(fd int, buf []byte) (n int, err error) {
216 return getdirent(fd, buf)
217 }
218
219
220
221 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
222 var status _C_int
223 var r _Pid_t
224 err = ERESTART
225
226
227 for err == ERESTART {
228 r, err = wait4(_Pid_t(pid), &status, options, rusage)
229 }
230 wpid = int(r)
231 if wstatus != nil {
232 *wstatus = WaitStatus(status)
233 }
234 return
235 }
236
237
238
239 func Fsync(fd int) error {
240 return fsyncRange(fd, O_SYNC, 0, 0)
241 }
242
243
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
266 if sa.Port < 0 || sa.Port > 0xFFFF {
267 return nil, 0, EINVAL
268 }
269 sa.raw.Family = AF_INET
270 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
271 p[0] = byte(sa.Port >> 8)
272 p[1] = byte(sa.Port)
273 sa.raw.Addr = sa.Addr
274 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
275 }
276
277 func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
278 if sa.Port < 0 || sa.Port > 0xFFFF {
279 return nil, 0, EINVAL
280 }
281 sa.raw.Family = AF_INET6
282 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
283 p[0] = byte(sa.Port >> 8)
284 p[1] = byte(sa.Port)
285 sa.raw.Scope_id = sa.ZoneId
286 sa.raw.Addr = sa.Addr
287 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
288 }
289
290 func (sa *RawSockaddrUnix) setLen(n int) {
291 sa.Len = uint8(3 + n)
292 }
293
294 func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
295 name := sa.Name
296 n := len(name)
297 if n > len(sa.raw.Path) {
298 return nil, 0, EINVAL
299 }
300 sa.raw.Family = AF_UNIX
301 sa.raw.setLen(n)
302 for i := 0; i < n; i++ {
303 sa.raw.Path[i] = uint8(name[i])
304 }
305
306 sl := _Socklen(2)
307 if n > 0 {
308 sl += _Socklen(n) + 1
309 }
310
311 return unsafe.Pointer(&sa.raw), sl, nil
312 }
313
314 func Getsockname(fd int) (sa Sockaddr, err error) {
315 var rsa RawSockaddrAny
316 var len _Socklen = SizeofSockaddrAny
317 if err = getsockname(fd, &rsa, &len); err != nil {
318 return
319 }
320 return anyToSockaddr(&rsa)
321 }
322
323
324
325 func Accept(fd int) (nfd int, sa Sockaddr, err error) {
326 var rsa RawSockaddrAny
327 var len _Socklen = SizeofSockaddrAny
328 nfd, err = accept(fd, &rsa, &len)
329 if err != nil {
330 return
331 }
332 sa, err = anyToSockaddr(&rsa)
333 if err != nil {
334 Close(nfd)
335 nfd = 0
336 }
337 return
338 }
339
340 func recvmsgRaw(fd int, p, oob []byte, flags int, rsa *RawSockaddrAny) (n, oobn int, recvflags int, err error) {
341 var msg Msghdr
342 msg.Name = (*byte)(unsafe.Pointer(rsa))
343 msg.Namelen = uint32(SizeofSockaddrAny)
344 var iov Iovec
345 if len(p) > 0 {
346 iov.Base = &p[0]
347 iov.SetLen(len(p))
348 }
349 var dummy byte
350 if len(oob) > 0 {
351 var sockType int
352 sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
353 if err != nil {
354 return
355 }
356
357 if sockType != SOCK_DGRAM && len(p) == 0 {
358 iov.Base = &dummy
359 iov.SetLen(1)
360 }
361 msg.Control = &oob[0]
362 msg.SetControllen(len(oob))
363 }
364 msg.Iov = &iov
365 msg.Iovlen = 1
366 if n, err = recvmsg(fd, &msg, flags); err != nil {
367 return
368 }
369 oobn = int(msg.Controllen)
370 recvflags = int(msg.Flags)
371 return
372 }
373
374 func sendmsgN(fd int, p, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) {
375 var msg Msghdr
376 msg.Name = (*byte)(ptr)
377 msg.Namelen = uint32(salen)
378 var iov Iovec
379 if len(p) > 0 {
380 iov.Base = &p[0]
381 iov.SetLen(len(p))
382 }
383 var dummy byte
384 if len(oob) > 0 {
385 var sockType int
386 sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE)
387 if err != nil {
388 return 0, err
389 }
390
391 if sockType != SOCK_DGRAM && len(p) == 0 {
392 iov.Base = &dummy
393 iov.SetLen(1)
394 }
395 msg.Control = &oob[0]
396 msg.SetControllen(len(oob))
397 }
398 msg.Iov = &iov
399 msg.Iovlen = 1
400 if n, err = sendmsg(fd, &msg, flags); err != nil {
401 return 0, err
402 }
403 if len(oob) > 0 && len(p) == 0 {
404 n = 0
405 }
406 return n, nil
407 }
408
409 func (sa *RawSockaddrUnix) getLen() (int, error) {
410
411
412 n := SizeofSockaddrUnix - 3
413 for i := 0; i < n; i++ {
414 if sa.Path[i] == 0 {
415 n = i
416 break
417 }
418 }
419 return n, nil
420 }
421
422 func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
423 switch rsa.Addr.Family {
424 case AF_UNIX:
425 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
426 sa := new(SockaddrUnix)
427 n, err := pp.getLen()
428 if err != nil {
429 return nil, err
430 }
431 sa.Name = string(unsafe.Slice((*byte)(unsafe.Pointer(&pp.Path[0])), n))
432 return sa, nil
433
434 case AF_INET:
435 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
436 sa := new(SockaddrInet4)
437 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
438 sa.Port = int(p[0])<<8 + int(p[1])
439 sa.Addr = pp.Addr
440 return sa, nil
441
442 case AF_INET6:
443 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
444 sa := new(SockaddrInet6)
445 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
446 sa.Port = int(p[0])<<8 + int(p[1])
447 sa.Addr = pp.Addr
448 return sa, nil
449 }
450 return nil, EAFNOSUPPORT
451 }
452
453 type SockaddrDatalink struct {
454 Len uint8
455 Family uint8
456 Index uint16
457 Type uint8
458 Nlen uint8
459 Alen uint8
460 Slen uint8
461 Data [120]uint8
462 raw RawSockaddrDatalink
463 }
464
465
468
469 type WaitStatus uint32
470
471 func (w WaitStatus) Stopped() bool { return w&0x40 != 0 }
472 func (w WaitStatus) StopSignal() Signal {
473 if !w.Stopped() {
474 return -1
475 }
476 return Signal(w>>8) & 0xFF
477 }
478
479 func (w WaitStatus) Exited() bool { return w&0xFF == 0 }
480 func (w WaitStatus) ExitStatus() int {
481 if !w.Exited() {
482 return -1
483 }
484 return int((w >> 8) & 0xFF)
485 }
486
487 func (w WaitStatus) Signaled() bool { return w&0x40 == 0 && w&0xFF != 0 }
488 func (w WaitStatus) Signal() Signal {
489 if !w.Signaled() {
490 return -1
491 }
492 return Signal(w>>16) & 0xFF
493 }
494
495 func (w WaitStatus) Continued() bool { return w&0x01000000 != 0 }
496
497 func (w WaitStatus) CoreDump() bool { return w&0x80 == 0x80 }
498
499 func (w WaitStatus) TrapCause() int { return -1 }
500
501
504
505
506
507
508
509 func raw_ptrace(request int, pid int, addr *byte, data *byte) Errno {
510 if request == PTRACE_TRACEME {
511
512 err := ptrace64(PT_TRACE_ME, 0, 0, 0, 0)
513 if err != nil {
514 return err.(Errno)
515 }
516 return 0
517 }
518 return ENOSYS
519 }
520
521 func ptracePeek(pid int, addr uintptr, out []byte) (count int, err error) {
522 n := 0
523 for len(out) > 0 {
524 bsize := len(out)
525 if bsize > 1024 {
526 bsize = 1024
527 }
528 err = ptrace64Ptr(PT_READ_BLOCK, int64(pid), int64(addr), bsize, unsafe.Pointer(&out[0]))
529 if err != nil {
530 return 0, err
531 }
532 addr += uintptr(bsize)
533 n += bsize
534 out = out[n:]
535 }
536 return n, nil
537 }
538
539 func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) {
540 return ptracePeek(pid, addr, out)
541 }
542
543 func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
544 return ptracePeek(pid, addr, out)
545 }
546
547 func ptracePoke(pid int, addr uintptr, data []byte) (count int, err error) {
548 n := 0
549 for len(data) > 0 {
550 bsize := len(data)
551 if bsize > 1024 {
552 bsize = 1024
553 }
554 err = ptrace64Ptr(PT_WRITE_BLOCK, int64(pid), int64(addr), bsize, unsafe.Pointer(&data[0]))
555 if err != nil {
556 return 0, err
557 }
558 addr += uintptr(bsize)
559 n += bsize
560 data = data[n:]
561 }
562 return n, nil
563 }
564
565 func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
566 return ptracePoke(pid, addr, data)
567 }
568
569 func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) {
570 return ptracePoke(pid, addr, data)
571 }
572
573 func PtraceCont(pid int, signal int) (err error) {
574 return ptrace64(PT_CONTINUE, int64(pid), 1, signal, 0)
575 }
576
577 func PtraceSingleStep(pid int) (err error) { return ptrace64(PT_STEP, int64(pid), 1, 0, 0) }
578
579 func PtraceAttach(pid int) (err error) { return ptrace64(PT_ATTACH, int64(pid), 0, 0, 0) }
580
581 func PtraceDetach(pid int) (err error) { return ptrace64(PT_DETACH, int64(pid), 0, 0, 0) }
582
583
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650 func setTimespec(sec, nsec int64) Timespec {
651 return Timespec{Sec: sec, Nsec: nsec}
652 }
653
654 func setTimeval(sec, usec int64) Timeval {
655 return Timeval{Sec: sec, Usec: int32(usec)}
656 }
657
658 func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
659 r0, _, e1 := syscall6(uintptr(unsafe.Pointer(&libc_read)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
660 n = int(r0)
661 if e1 != 0 {
662 err = e1
663 }
664 return
665 }
666
667
670
671 var mapper = &mmapper{
672 active: make(map[*byte][]byte),
673 mmap: mmap,
674 munmap: munmap,
675 }
676
677
678
679
680 func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
681 return mapper.Mmap(fd, offset, length, prot, flags)
682 }
683
684 func Munmap(b []byte) (err error) {
685 return mapper.Munmap(b)
686 }
687
View as plain text