github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/unix/syscall_aix.go (about)

     1  // 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  
    14  package unix
    15  
    16  import "unsafe"
    17  
    18  /*
    19   * Wrapped
    20   */
    21  
    22  //sys	utimes(path string, times *[2]Timeval) (err error)
    23  func 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)
    31  func 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  
    38  func 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  
    48  func (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  
    62  func (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  
    77  func (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  
   104  func 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  
   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)
   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)
   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  func 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  
   172  func 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  
   190  func 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  
   205  func 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  
   211  func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
   212  	_, err = SendmsgN(fd, p, oob, to, flags)
   213  	return
   214  }
   215  
   216  func 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  
   221  func 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  
   266  func Gettimeofday(tv *Timeval) (err error) {
   267  	err = gettimeofday(tv, nil)
   268  	return
   269  }
   270  
   271  func 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
   279  func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
   280  	return -1, ENOSYS
   281  }
   282  
   283  func direntIno(buf []byte) (uint64, bool) {
   284  	return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
   285  }
   286  
   287  func direntReclen(buf []byte) (uint64, bool) {
   288  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
   289  }
   290  
   291  func direntNamlen(buf []byte) (uint64, bool) {
   292  	reclen, ok := direntReclen(buf)
   293  	if !ok {
   294  		return 0, false
   295  	}
   296  	return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
   297  }
   298  
   299  //sys	getdirent(fd int, buf []byte) (n int, err error)
   300  func Getdents(fd int, buf []byte) (n int, err error) {
   301  	return getdirent(fd, buf)
   302  }
   303  
   304  //sys	wait4(pid Pid_t, status *_C_int, options int, rusage *Rusage) (wpid Pid_t, err error)
   305  func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
   306  	var status _C_int
   307  	var r Pid_t
   308  	err = ERESTART
   309  	// AIX wait4 may return with ERESTART errno, while the processus is still
   310  	// active.
   311  	for err == ERESTART {
   312  		r, err = wait4(Pid_t(pid), &status, options, rusage)
   313  	}
   314  	wpid = int(r)
   315  	if wstatus != nil {
   316  		*wstatus = WaitStatus(status)
   317  	}
   318  	return
   319  }
   320  
   321  /*
   322   * Wait
   323   */
   324  
   325  type WaitStatus uint32
   326  
   327  func (w WaitStatus) Stopped() bool { return w&0x40 != 0 }
   328  func (w WaitStatus) StopSignal() Signal {
   329  	if !w.Stopped() {
   330  		return -1
   331  	}
   332  	return Signal(w>>8) & 0xFF
   333  }
   334  
   335  func (w WaitStatus) Exited() bool { return w&0xFF == 0 }
   336  func (w WaitStatus) ExitStatus() int {
   337  	if !w.Exited() {
   338  		return -1
   339  	}
   340  	return int((w >> 8) & 0xFF)
   341  }
   342  
   343  func (w WaitStatus) Signaled() bool { return w&0x40 == 0 && w&0xFF != 0 }
   344  func (w WaitStatus) Signal() Signal {
   345  	if !w.Signaled() {
   346  		return -1
   347  	}
   348  	return Signal(w>>16) & 0xFF
   349  }
   350  
   351  func (w WaitStatus) Continued() bool { return w&0x01000000 != 0 }
   352  
   353  func (w WaitStatus) CoreDump() bool { return w&0x200 != 0 }
   354  
   355  func (w WaitStatus) TrapCause() int { return -1 }
   356  
   357  //sys	ioctl(fd int, req uint, arg uintptr) (err error)
   358  
   359  // fcntl must never be called with cmd=F_DUP2FD because it doesn't work on AIX
   360  // There is no way to create a custom fcntl and to keep //sys fcntl easily,
   361  // Therefore, the programmer must call dup2 instead of fcntl in this case.
   362  
   363  // FcntlInt performs a fcntl syscall on fd with the provided command and argument.
   364  //sys	FcntlInt(fd uintptr, cmd int, arg int) (r int,err error) = fcntl
   365  
   366  // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
   367  //sys	FcntlFlock(fd uintptr, cmd int, lk *Flock_t) (err error) = fcntl
   368  
   369  //sys	fcntl(fd int, cmd int, arg int) (val int, err error)
   370  
   371  /*
   372   * Direct access
   373   */
   374  
   375  //sys	Acct(path string) (err error)
   376  //sys	Chdir(path string) (err error)
   377  //sys	Chroot(path string) (err error)
   378  //sys	Close(fd int) (err error)
   379  //sys	Dup(oldfd int) (fd int, err error)
   380  //sys	Exit(code int)
   381  //sys	Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
   382  //sys	Fchdir(fd int) (err error)
   383  //sys	Fchmod(fd int, mode uint32) (err error)
   384  //sys	Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
   385  //sys	Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
   386  //sys	Fdatasync(fd int) (err error)
   387  //sys	Fsync(fd int) (err error)
   388  // readdir_r
   389  //sysnb	Getpgid(pid int) (pgid int, err error)
   390  
   391  //sys	Getpgrp() (pid int)
   392  
   393  //sysnb	Getpid() (pid int)
   394  //sysnb	Getppid() (ppid int)
   395  //sys	Getpriority(which int, who int) (prio int, err error)
   396  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   397  //sysnb	Getsid(pid int) (sid int, err error)
   398  //sysnb	Kill(pid int, sig Signal) (err error)
   399  //sys	Klogctl(typ int, buf []byte) (n int, err error) = syslog
   400  //sys	Mkdir(dirfd int, path string, mode uint32) (err error)
   401  //sys	Mkdirat(dirfd int, path string, mode uint32) (err error)
   402  //sys	Mkfifo(path string, mode uint32) (err error)
   403  //sys	Mknod(path string, mode uint32, dev int) (err error)
   404  //sys	Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
   405  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   406  //sys   Open(path string, mode int, perm uint32) (fd int, err error) = open64
   407  //sys   Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
   408  //sys	read(fd int, p []byte) (n int, err error)
   409  //sys	Readlink(path string, buf []byte) (n int, err error)
   410  //sys	Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
   411  //sys	Setdomainname(p []byte) (err error)
   412  //sys	Sethostname(p []byte) (err error)
   413  //sysnb	Setpgid(pid int, pgid int) (err error)
   414  //sysnb	Setsid() (pid int, err error)
   415  //sysnb	Settimeofday(tv *Timeval) (err error)
   416  
   417  //sys	Setuid(uid int) (err error)
   418  //sys	Setgid(uid int) (err error)
   419  
   420  //sys	Setpriority(which int, who int, prio int) (err error)
   421  //sys	Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error)
   422  //sys	Sync()
   423  //sysnb	Times(tms *Tms) (ticks uintptr, err error)
   424  //sysnb	Umask(mask int) (oldmask int)
   425  //sysnb	Uname(buf *Utsname) (err error)
   426  //sys   Unlink(path string) (err error)
   427  //sys   Unlinkat(dirfd int, path string, flags int) (err error)
   428  //sys	Ustat(dev int, ubuf *Ustat_t) (err error)
   429  //sys	write(fd int, p []byte) (n int, err error)
   430  //sys	readlen(fd int, p *byte, np int) (n int, err error) = read
   431  //sys	writelen(fd int, p *byte, np int) (n int, err error) = write
   432  
   433  //sys	Dup2(oldfd int, newfd int) (err error)
   434  //sys	Fadvise(fd int, offset int64, length int64, advice int) (err error) = posix_fadvise64
   435  //sys	Fchown(fd int, uid int, gid int) (err error)
   436  //sys	fstat(fd int, stat *Stat_t) (err error)
   437  //sys	fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = fstatat
   438  //sys	Fstatfs(fd int, buf *Statfs_t) (err error)
   439  //sys	Ftruncate(fd int, length int64) (err error)
   440  //sysnb	Getegid() (egid int)
   441  //sysnb	Geteuid() (euid int)
   442  //sysnb	Getgid() (gid int)
   443  //sysnb	Getuid() (uid int)
   444  //sys	Lchown(path string, uid int, gid int) (err error)
   445  //sys	Listen(s int, n int) (err error)
   446  //sys	lstat(path string, stat *Stat_t) (err error)
   447  //sys	Pause() (err error)
   448  //sys	Pread(fd int, p []byte, offset int64) (n int, err error) = pread64
   449  //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error) = pwrite64
   450  //sys	Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
   451  //sys	Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
   452  //sysnb	Setregid(rgid int, egid int) (err error)
   453  //sysnb	Setreuid(ruid int, euid int) (err error)
   454  //sys	Shutdown(fd int, how int) (err error)
   455  //sys	Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
   456  //sys	stat(path string, statptr *Stat_t) (err error)
   457  //sys	Statfs(path string, buf *Statfs_t) (err error)
   458  //sys	Truncate(path string, length int64) (err error)
   459  
   460  //sys	bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
   461  //sys	connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
   462  //sysnb	getgroups(n int, list *_Gid_t) (nn int, err error)
   463  //sysnb	setgroups(n int, list *_Gid_t) (err error)
   464  //sys	getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
   465  //sys	setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
   466  //sysnb	socket(domain int, typ int, proto int) (fd int, err error)
   467  //sysnb	socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
   468  //sysnb	getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
   469  //sysnb	getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
   470  //sys	recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
   471  //sys	sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
   472  
   473  // In order to use msghdr structure with Control, Controllen, nrecvmsg and nsendmsg must be used.
   474  //sys	recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = nrecvmsg
   475  //sys	sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = nsendmsg
   476  
   477  //sys	munmap(addr uintptr, length uintptr) (err error)
   478  
   479  var mapper = &mmapper{
   480  	active: make(map[*byte][]byte),
   481  	mmap:   mmap,
   482  	munmap: munmap,
   483  }
   484  
   485  func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
   486  	return mapper.Mmap(fd, offset, length, prot, flags)
   487  }
   488  
   489  func Munmap(b []byte) (err error) {
   490  	return mapper.Munmap(b)
   491  }
   492  
   493  //sys	Madvise(b []byte, advice int) (err error)
   494  //sys	Mprotect(b []byte, prot int) (err error)
   495  //sys	Mlock(b []byte) (err error)
   496  //sys	Mlockall(flags int) (err error)
   497  //sys	Msync(b []byte, flags int) (err error)
   498  //sys	Munlock(b []byte) (err error)
   499  //sys	Munlockall() (err error)
   500  
   501  //sysnb pipe(p *[2]_C_int) (err error)
   502  
   503  func Pipe(p []int) (err error) {
   504  	if len(p) != 2 {
   505  		return EINVAL
   506  	}
   507  	var pp [2]_C_int
   508  	err = pipe(&pp)
   509  	p[0] = int(pp[0])
   510  	p[1] = int(pp[1])
   511  	return
   512  }
   513  
   514  //sys	poll(fds *PollFd, nfds int, timeout int) (n int, err error)
   515  
   516  func Poll(fds []PollFd, timeout int) (n int, err error) {
   517  	if len(fds) == 0 {
   518  		return poll(nil, 0, timeout)
   519  	}
   520  	return poll(&fds[0], len(fds), timeout)
   521  }
   522  
   523  //sys	gettimeofday(tv *Timeval, tzp *Timezone) (err error)
   524  //sysnb	Time(t *Time_t) (tt Time_t, err error)
   525  //sys	Utime(path string, buf *Utimbuf) (err error)
   526  
   527  //sys	Getsystemcfg(label int) (n uint64)
   528  
   529  //sys	umount(target string) (err error)
   530  func Unmount(target string, flags int) (err error) {
   531  	if flags != 0 {
   532  		// AIX doesn't have any flags for umount.
   533  		return ENOSYS
   534  	}
   535  	return umount(target)
   536  }