blob: a76826f443616dfc008d50696dd14a98fbb113dd [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001// Copyright 2018 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.
4
5// +build aix
6
7// Aix system calls.
8// This file is compiled as ordinary Go code,
9// but it is also input to mksyscall,
10// which parses the //sys lines and generates system call stubs.
11// Note that sometimes we use a lowercase //sys name and
12// wrap it in our own nicer implementation.
13
14package unix
15
16import "unsafe"
17
18/*
19 * Wrapped
20 */
21
22//sys utimes(path string, times *[2]Timeval) (err error)
23func Utimes(path string, tv []Timeval) error {
24 if len(tv) != 2 {
25 return EINVAL
26 }
27 return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
28}
29
30//sys utimensat(dirfd int, path string, times *[2]Timespec, flag int) (err error)
31func UtimesNano(path string, ts []Timespec) error {
32 if len(ts) != 2 {
33 return EINVAL
34 }
35 return utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
36}
37
38func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
39 if ts == nil {
40 return utimensat(dirfd, path, nil, flags)
41 }
42 if len(ts) != 2 {
43 return EINVAL
44 }
45 return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
46}
47
48func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
49 if sa.Port < 0 || sa.Port > 0xFFFF {
50 return nil, 0, EINVAL
51 }
52 sa.raw.Family = AF_INET
53 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
54 p[0] = byte(sa.Port >> 8)
55 p[1] = byte(sa.Port)
56 for i := 0; i < len(sa.Addr); i++ {
57 sa.raw.Addr[i] = sa.Addr[i]
58 }
59 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
60}
61
62func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
63 if sa.Port < 0 || sa.Port > 0xFFFF {
64 return nil, 0, EINVAL
65 }
66 sa.raw.Family = AF_INET6
67 p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
68 p[0] = byte(sa.Port >> 8)
69 p[1] = byte(sa.Port)
70 sa.raw.Scope_id = sa.ZoneId
71 for i := 0; i < len(sa.Addr); i++ {
72 sa.raw.Addr[i] = sa.Addr[i]
73 }
74 return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
75}
76
77func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
78 name := sa.Name
79 n := len(name)
80 if n > len(sa.raw.Path) {
81 return nil, 0, EINVAL
82 }
83 if n == len(sa.raw.Path) && name[0] != '@' {
84 return nil, 0, EINVAL
85 }
86 sa.raw.Family = AF_UNIX
87 for i := 0; i < n; i++ {
88 sa.raw.Path[i] = uint8(name[i])
89 }
90 // length is family (uint16), name, NUL.
91 sl := _Socklen(2)
92 if n > 0 {
93 sl += _Socklen(n) + 1
94 }
95 if sa.raw.Path[0] == '@' {
96 sa.raw.Path[0] = 0
97 // Don't count trailing NUL for abstract address.
98 sl--
99 }
100
101 return unsafe.Pointer(&sa.raw), sl, nil
102}
103
104func Getsockname(fd int) (sa Sockaddr, err error) {
105 var rsa RawSockaddrAny
106 var len _Socklen = SizeofSockaddrAny
107 if err = getsockname(fd, &rsa, &len); err != nil {
108 return
109 }
110 return anyToSockaddr(fd, &rsa)
111}
112
113//sys getcwd(buf []byte) (err error)
114
115const ImplementsGetwd = true
116
117func Getwd() (ret string, err error) {
118 for len := uint64(4096); ; len *= 2 {
119 b := make([]byte, len)
120 err := getcwd(b)
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
134func Getcwd(buf []byte) (n int, err error) {
135 err = getcwd(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
146func Getgroups() (gids []int, err error) {
147 n, err := getgroups(0, nil)
148 if err != nil {
149 return nil, err
150 }
151 if n == 0 {
152 return nil, nil
153 }
154
155 // Sanity check group count. Max is 16 on BSD.
156 if n < 0 || n > 1000 {
157 return nil, EINVAL
158 }
159
160 a := make([]_Gid_t, n)
161 n, err = getgroups(n, &a[0])
162 if err != nil {
163 return nil, err
164 }
165 gids = make([]int, n)
166 for i, v := range a[0:n] {
167 gids[i] = int(v)
168 }
169 return
170}
171
172func Setgroups(gids []int) (err error) {
173 if len(gids) == 0 {
174 return setgroups(0, nil)
175 }
176
177 a := make([]_Gid_t, len(gids))
178 for i, v := range gids {
179 a[i] = _Gid_t(v)
180 }
181 return setgroups(len(a), &a[0])
182}
183
184/*
185 * Socket
186 */
187
188//sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
189
190func Accept(fd int) (nfd int, sa Sockaddr, err error) {
191 var rsa RawSockaddrAny
192 var len _Socklen = SizeofSockaddrAny
193 nfd, err = accept(fd, &rsa, &len)
194 if nfd == -1 {
195 return
196 }
197 sa, err = anyToSockaddr(fd, &rsa)
198 if err != nil {
199 Close(nfd)
200 nfd = 0
201 }
202 return
203}
204
205func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
206 // Recvmsg not implemented on AIX
207 sa := new(SockaddrUnix)
208 return -1, -1, -1, sa, ENOSYS
209}
210
211func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
212 _, err = SendmsgN(fd, p, oob, to, flags)
213 return
214}
215
216func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
217 // SendmsgN not implemented on AIX
218 return -1, ENOSYS
219}
220
221func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
222 switch rsa.Addr.Family {
223
224 case AF_UNIX:
225 pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
226 sa := new(SockaddrUnix)
227
228 // Some versions of AIX have a bug in getsockname (see IV78655).
229 // We can't rely on sa.Len being set correctly.
230 n := SizeofSockaddrUnix - 3 // subtract leading Family, Len, terminating NUL.
231 for i := 0; i < n; i++ {
232 if pp.Path[i] == 0 {
233 n = i
234 break
235 }
236 }
237
238 bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
239 sa.Name = string(bytes)
240 return sa, nil
241
242 case AF_INET:
243 pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
244 sa := new(SockaddrInet4)
245 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
246 sa.Port = int(p[0])<<8 + int(p[1])
247 for i := 0; i < len(sa.Addr); i++ {
248 sa.Addr[i] = pp.Addr[i]
249 }
250 return sa, nil
251
252 case AF_INET6:
253 pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
254 sa := new(SockaddrInet6)
255 p := (*[2]byte)(unsafe.Pointer(&pp.Port))
256 sa.Port = int(p[0])<<8 + int(p[1])
257 sa.ZoneId = pp.Scope_id
258 for i := 0; i < len(sa.Addr); i++ {
259 sa.Addr[i] = pp.Addr[i]
260 }
261 return sa, nil
262 }
263 return nil, EAFNOSUPPORT
264}
265
266func Gettimeofday(tv *Timeval) (err error) {
267 err = gettimeofday(tv, nil)
268 return
269}
270
271func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
272 if raceenabled {
273 raceReleaseMerge(unsafe.Pointer(&ioSync))
274 }
275 return sendfile(outfd, infd, offset, count)
276}
277
278// TODO
279func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
280 return -1, ENOSYS
281}
282
Don Newton98fd8812019-09-23 15:15:02 -0400283//sys getdirent(fd int, buf []byte) (n int, err error)
Don Newtone0d34a82019-11-14 10:58:06 -0500284func ReadDirent(fd int, buf []byte) (n int, err error) {
Don Newton98fd8812019-09-23 15:15:02 -0400285 return getdirent(fd, buf)
286}
287
288//sys wait4(pid Pid_t, status *_C_int, options int, rusage *Rusage) (wpid Pid_t, err error)
289func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
290 var status _C_int
291 var r Pid_t
292 err = ERESTART
293 // AIX wait4 may return with ERESTART errno, while the processus is still
294 // active.
295 for err == ERESTART {
296 r, err = wait4(Pid_t(pid), &status, options, rusage)
297 }
298 wpid = int(r)
299 if wstatus != nil {
300 *wstatus = WaitStatus(status)
301 }
302 return
303}
304
305/*
306 * Wait
307 */
308
309type WaitStatus uint32
310
311func (w WaitStatus) Stopped() bool { return w&0x40 != 0 }
312func (w WaitStatus) StopSignal() Signal {
313 if !w.Stopped() {
314 return -1
315 }
316 return Signal(w>>8) & 0xFF
317}
318
319func (w WaitStatus) Exited() bool { return w&0xFF == 0 }
320func (w WaitStatus) ExitStatus() int {
321 if !w.Exited() {
322 return -1
323 }
324 return int((w >> 8) & 0xFF)
325}
326
327func (w WaitStatus) Signaled() bool { return w&0x40 == 0 && w&0xFF != 0 }
328func (w WaitStatus) Signal() Signal {
329 if !w.Signaled() {
330 return -1
331 }
332 return Signal(w>>16) & 0xFF
333}
334
335func (w WaitStatus) Continued() bool { return w&0x01000000 != 0 }
336
Don Newtone0d34a82019-11-14 10:58:06 -0500337func (w WaitStatus) CoreDump() bool { return w&0x200 != 0 }
Don Newton98fd8812019-09-23 15:15:02 -0400338
339func (w WaitStatus) TrapCause() int { return -1 }
340
341//sys ioctl(fd int, req uint, arg uintptr) (err error)
342
Don Newtone0d34a82019-11-14 10:58:06 -0500343// ioctl itself should not be exposed directly, but additional get/set
344// functions for specific types are permissible.
345
346// IoctlSetInt performs an ioctl operation which sets an integer value
347// on fd, using the specified request number.
348func IoctlSetInt(fd int, req uint, value int) error {
349 return ioctl(fd, req, uintptr(value))
350}
351
352func ioctlSetWinsize(fd int, req uint, value *Winsize) error {
353 return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
354}
355
356func ioctlSetTermios(fd int, req uint, value *Termios) error {
357 return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
358}
359
360// IoctlGetInt performs an ioctl operation which gets an integer value
361// from fd, using the specified request number.
362func IoctlGetInt(fd int, req uint) (int, error) {
363 var value int
364 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
365 return value, err
366}
367
368func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
369 var value Winsize
370 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
371 return &value, err
372}
373
374func IoctlGetTermios(fd int, req uint) (*Termios, error) {
375 var value Termios
376 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
377 return &value, err
378}
379
Don Newton98fd8812019-09-23 15:15:02 -0400380// fcntl must never be called with cmd=F_DUP2FD because it doesn't work on AIX
381// There is no way to create a custom fcntl and to keep //sys fcntl easily,
382// Therefore, the programmer must call dup2 instead of fcntl in this case.
383
384// FcntlInt performs a fcntl syscall on fd with the provided command and argument.
385//sys FcntlInt(fd uintptr, cmd int, arg int) (r int,err error) = fcntl
386
387// FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
388//sys FcntlFlock(fd uintptr, cmd int, lk *Flock_t) (err error) = fcntl
389
390//sys fcntl(fd int, cmd int, arg int) (val int, err error)
391
392/*
393 * Direct access
394 */
395
396//sys Acct(path string) (err error)
397//sys Chdir(path string) (err error)
398//sys Chroot(path string) (err error)
399//sys Close(fd int) (err error)
400//sys Dup(oldfd int) (fd int, err error)
401//sys Exit(code int)
402//sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
403//sys Fchdir(fd int) (err error)
404//sys Fchmod(fd int, mode uint32) (err error)
405//sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
406//sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
407//sys Fdatasync(fd int) (err error)
408//sys Fsync(fd int) (err error)
409// readdir_r
410//sysnb Getpgid(pid int) (pgid int, err error)
411
412//sys Getpgrp() (pid int)
413
414//sysnb Getpid() (pid int)
415//sysnb Getppid() (ppid int)
416//sys Getpriority(which int, who int) (prio int, err error)
417//sysnb Getrusage(who int, rusage *Rusage) (err error)
418//sysnb Getsid(pid int) (sid int, err error)
419//sysnb Kill(pid int, sig Signal) (err error)
420//sys Klogctl(typ int, buf []byte) (n int, err error) = syslog
421//sys Mkdir(dirfd int, path string, mode uint32) (err error)
422//sys Mkdirat(dirfd int, path string, mode uint32) (err error)
423//sys Mkfifo(path string, mode uint32) (err error)
424//sys Mknod(path string, mode uint32, dev int) (err error)
425//sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
426//sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
427//sys Open(path string, mode int, perm uint32) (fd int, err error) = open64
428//sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
429//sys read(fd int, p []byte) (n int, err error)
430//sys Readlink(path string, buf []byte) (n int, err error)
431//sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
432//sys Setdomainname(p []byte) (err error)
433//sys Sethostname(p []byte) (err error)
434//sysnb Setpgid(pid int, pgid int) (err error)
435//sysnb Setsid() (pid int, err error)
436//sysnb Settimeofday(tv *Timeval) (err error)
437
438//sys Setuid(uid int) (err error)
439//sys Setgid(uid int) (err error)
440
441//sys Setpriority(which int, who int, prio int) (err error)
442//sys Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error)
443//sys Sync()
444//sysnb Times(tms *Tms) (ticks uintptr, err error)
445//sysnb Umask(mask int) (oldmask int)
446//sysnb Uname(buf *Utsname) (err error)
Don Newtone0d34a82019-11-14 10:58:06 -0500447//TODO umount
448// //sys Unmount(target string, flags int) (err error) = umount
Don Newton98fd8812019-09-23 15:15:02 -0400449//sys Unlink(path string) (err error)
450//sys Unlinkat(dirfd int, path string, flags int) (err error)
451//sys Ustat(dev int, ubuf *Ustat_t) (err error)
452//sys write(fd int, p []byte) (n int, err error)
453//sys readlen(fd int, p *byte, np int) (n int, err error) = read
454//sys writelen(fd int, p *byte, np int) (n int, err error) = write
455
456//sys Dup2(oldfd int, newfd int) (err error)
457//sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = posix_fadvise64
458//sys Fchown(fd int, uid int, gid int) (err error)
Don Newtone0d34a82019-11-14 10:58:06 -0500459//sys Fstat(fd int, stat *Stat_t) (err error)
460//sys Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = fstatat
Don Newton98fd8812019-09-23 15:15:02 -0400461//sys Fstatfs(fd int, buf *Statfs_t) (err error)
462//sys Ftruncate(fd int, length int64) (err error)
463//sysnb Getegid() (egid int)
464//sysnb Geteuid() (euid int)
465//sysnb Getgid() (gid int)
466//sysnb Getuid() (uid int)
467//sys Lchown(path string, uid int, gid int) (err error)
468//sys Listen(s int, n int) (err error)
Don Newtone0d34a82019-11-14 10:58:06 -0500469//sys Lstat(path string, stat *Stat_t) (err error)
Don Newton98fd8812019-09-23 15:15:02 -0400470//sys Pause() (err error)
471//sys Pread(fd int, p []byte, offset int64) (n int, err error) = pread64
472//sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = pwrite64
Don Newtone0d34a82019-11-14 10:58:06 -0500473//TODO Select
474// //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
Don Newton98fd8812019-09-23 15:15:02 -0400475//sys Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
476//sysnb Setregid(rgid int, egid int) (err error)
477//sysnb Setreuid(ruid int, euid int) (err error)
478//sys Shutdown(fd int, how int) (err error)
479//sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
Don Newtone0d34a82019-11-14 10:58:06 -0500480//sys Stat(path string, stat *Stat_t) (err error)
Don Newton98fd8812019-09-23 15:15:02 -0400481//sys Statfs(path string, buf *Statfs_t) (err error)
482//sys Truncate(path string, length int64) (err error)
483
484//sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
485//sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
486//sysnb getgroups(n int, list *_Gid_t) (nn int, err error)
487//sysnb setgroups(n int, list *_Gid_t) (err error)
488//sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
489//sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
490//sysnb socket(domain int, typ int, proto int) (fd int, err error)
491//sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
492//sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
493//sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
494//sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
495//sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
Don Newtone0d34a82019-11-14 10:58:06 -0500496//sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
497//sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
Don Newton98fd8812019-09-23 15:15:02 -0400498
499//sys munmap(addr uintptr, length uintptr) (err error)
500
501var mapper = &mmapper{
502 active: make(map[*byte][]byte),
503 mmap: mmap,
504 munmap: munmap,
505}
506
507func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
508 return mapper.Mmap(fd, offset, length, prot, flags)
509}
510
511func Munmap(b []byte) (err error) {
512 return mapper.Munmap(b)
513}
514
515//sys Madvise(b []byte, advice int) (err error)
516//sys Mprotect(b []byte, prot int) (err error)
517//sys Mlock(b []byte) (err error)
518//sys Mlockall(flags int) (err error)
519//sys Msync(b []byte, flags int) (err error)
520//sys Munlock(b []byte) (err error)
521//sys Munlockall() (err error)
522
523//sysnb pipe(p *[2]_C_int) (err error)
524
525func Pipe(p []int) (err error) {
526 if len(p) != 2 {
527 return EINVAL
528 }
529 var pp [2]_C_int
530 err = pipe(&pp)
531 p[0] = int(pp[0])
532 p[1] = int(pp[1])
533 return
534}
535
536//sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
537
538func Poll(fds []PollFd, timeout int) (n int, err error) {
539 if len(fds) == 0 {
540 return poll(nil, 0, timeout)
541 }
542 return poll(&fds[0], len(fds), timeout)
543}
544
545//sys gettimeofday(tv *Timeval, tzp *Timezone) (err error)
546//sysnb Time(t *Time_t) (tt Time_t, err error)
547//sys Utime(path string, buf *Utimbuf) (err error)