Source file src/syscall/syscall_solaris.go

     1  // Copyright 2009 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  // Solaris system calls.
     6  // This file is compiled as ordinary Go code,
     7  // but it is also input to mksyscall,
     8  // which parses the //sys lines and generates system call stubs.
     9  // Note that sometimes we use a lowercase //sys name and wrap
    10  // it in our own nicer implementation, either here or in
    11  // syscall_solaris.go or syscall_unix.go.
    12  
    13  package syscall
    14  
    15  import "unsafe"
    16  
    17  const _F_DUP2FD_CLOEXEC = F_DUP2FD_CLOEXEC
    18  
    19  // Implemented in asm_solaris_amd64.s.
    20  func rawSysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
    21  func sysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
    22  
    23  type SockaddrDatalink struct {
    24  	Family uint16
    25  	Index  uint16
    26  	Type   uint8
    27  	Nlen   uint8
    28  	Alen   uint8
    29  	Slen   uint8
    30  	Data   [244]int8
    31  	raw    RawSockaddrDatalink
    32  }
    33  
    34  func direntIno(buf []byte) (uint64, bool) {
    35  	return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
    36  }
    37  
    38  func direntReclen(buf []byte) (uint64, bool) {
    39  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
    40  }
    41  
    42  func direntNamlen(buf []byte) (uint64, bool) {
    43  	reclen, ok := direntReclen(buf)
    44  	if !ok {
    45  		return 0, false
    46  	}
    47  	return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
    48  }
    49  
    50  func pipe() (r uintptr, w uintptr, err uintptr)
    51  
    52  func Pipe(p []int) (err error) {
    53  	if len(p) != 2 {
    54  		return EINVAL
    55  	}
    56  	r0, w0, e1 := pipe()
    57  	if e1 != 0 {
    58  		err = Errno(e1)
    59  	}
    60  	if err == nil {
    61  		p[0], p[1] = int(r0), int(w0)
    62  	}
    63  	return
    64  }
    65  
    66  func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
    67  	if sa.Port < 0 || sa.Port > 0xFFFF {
    68  		return nil, 0, EINVAL
    69  	}
    70  	sa.raw.Family = AF_INET
    71  	p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
    72  	p[0] = byte(sa.Port >> 8)
    73  	p[1] = byte(sa.Port)
    74  	sa.raw.Addr = sa.Addr
    75  	return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
    76  }
    77  
    78  func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
    79  	if sa.Port < 0 || sa.Port > 0xFFFF {
    80  		return nil, 0, EINVAL
    81  	}
    82  	sa.raw.Family = AF_INET6
    83  	p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
    84  	p[0] = byte(sa.Port >> 8)
    85  	p[1] = byte(sa.Port)
    86  	sa.raw.Scope_id = sa.ZoneId
    87  	sa.raw.Addr = sa.Addr
    88  	return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
    89  }
    90  
    91  func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
    92  	name := sa.Name
    93  	n := len(name)
    94  	if n >= len(sa.raw.Path) {
    95  		return nil, 0, EINVAL
    96  	}
    97  	sa.raw.Family = AF_UNIX
    98  	for i := 0; i < n; i++ {
    99  		sa.raw.Path[i] = int8(name[i])
   100  	}
   101  	// length is family (uint16), name, NUL.
   102  	sl := _Socklen(2)
   103  	if n > 0 {
   104  		sl += _Socklen(n) + 1
   105  	}
   106  	if sa.raw.Path[0] == '@' {
   107  		sa.raw.Path[0] = 0
   108  		// Don't count trailing NUL for abstract address.
   109  		sl--
   110  	}
   111  
   112  	return unsafe.Pointer(&sa.raw), sl, nil
   113  }
   114  
   115  func Getsockname(fd int) (sa Sockaddr, err error) {
   116  	var rsa RawSockaddrAny
   117  	var len _Socklen = SizeofSockaddrAny
   118  	if err = getsockname(fd, &rsa, &len); err != nil {
   119  		return
   120  	}
   121  	return anyToSockaddr(&rsa)
   122  }
   123  
   124  const ImplementsGetwd = true
   125  
   126  //sys	Getcwd(buf []byte) (n int, err error)
   127  
   128  func Getwd() (wd string, err error) {
   129  	var buf [PathMax]byte
   130  	// Getcwd will return an error if it failed for any reason.
   131  	_, err = Getcwd(buf[0:])
   132  	if err != nil {
   133  		return "", err
   134  	}
   135  	n := clen(buf[:])
   136  	if n < 1 {
   137  		return "", EINVAL
   138  	}
   139  	return string(buf[:n]), nil
   140  }
   141  
   142  /*
   143   * Wrapped
   144   */
   145  
   146  //sysnb	getgroups(ngid int, gid *_Gid_t) (n int, err error)
   147  //sysnb	setgroups(ngid int, gid *_Gid_t) (err error)
   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  	// Sanity check group count. Max is 16 on BSD.
   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 ReadDirent(fd int, buf []byte) (n int, err error) {
   188  	// Final argument is (basep *uintptr) and the syscall doesn't take nil.
   189  	// TODO(rsc): Can we use a single global basep for all calls?
   190  	return Getdents(fd, buf, new(uintptr))
   191  }
   192  
   193  // Wait status is 7 bits at bottom, either 0 (exited),
   194  // 0x7F (stopped), or a signal number that caused an exit.
   195  // The 0x80 bit is whether there was a core dump.
   196  // An extra number (exit code, signal causing a stop)
   197  // is in the high bits.
   198  
   199  type WaitStatus uint32
   200  
   201  const (
   202  	mask  = 0x7F
   203  	core  = 0x80
   204  	shift = 8
   205  
   206  	exited  = 0
   207  	stopped = 0x7F
   208  )
   209  
   210  func (w WaitStatus) Exited() bool { return w&mask == exited }
   211  
   212  func (w WaitStatus) ExitStatus() int {
   213  	if w&mask != exited {
   214  		return -1
   215  	}
   216  	return int(w >> shift)
   217  }
   218  
   219  func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != 0 }
   220  
   221  func (w WaitStatus) Signal() Signal {
   222  	sig := Signal(w & mask)
   223  	if sig == stopped || sig == 0 {
   224  		return -1
   225  	}
   226  	return sig
   227  }
   228  
   229  func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
   230  
   231  func (w WaitStatus) Stopped() bool { return w&mask == stopped && Signal(w>>shift) != SIGSTOP }
   232  
   233  func (w WaitStatus) Continued() bool { return w&mask == stopped && Signal(w>>shift) == SIGSTOP }
   234  
   235  func (w WaitStatus) StopSignal() Signal {
   236  	if !w.Stopped() {
   237  		return -1
   238  	}
   239  	return Signal(w>>shift) & 0xFF
   240  }
   241  
   242  func (w WaitStatus) TrapCause() int { return -1 }
   243  
   244  func wait4(pid uintptr, wstatus *WaitStatus, options uintptr, rusage *Rusage) (wpid uintptr, err uintptr)
   245  
   246  func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
   247  	r0, e1 := wait4(uintptr(pid), wstatus, uintptr(options), rusage)
   248  	if e1 != 0 {
   249  		err = Errno(e1)
   250  	}
   251  	return int(r0), err
   252  }
   253  
   254  func gethostname() (name string, err uintptr)
   255  
   256  func Gethostname() (name string, err error) {
   257  	name, e1 := gethostname()
   258  	if e1 != 0 {
   259  		err = Errno(e1)
   260  	}
   261  	return name, err
   262  }
   263  
   264  func UtimesNano(path string, ts []Timespec) error {
   265  	if len(ts) != 2 {
   266  		return EINVAL
   267  	}
   268  	return utimensat(_AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
   269  }
   270  
   271  //sys	fcntl(fd int, cmd int, arg int) (val int, err error)
   272  
   273  // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
   274  func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
   275  	_, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_fcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(unsafe.Pointer(lk)), 0, 0, 0)
   276  	if e1 != 0 {
   277  		return e1
   278  	}
   279  	return nil
   280  }
   281  
   282  func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
   283  	switch rsa.Addr.Family {
   284  	case AF_UNIX:
   285  		pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
   286  		sa := new(SockaddrUnix)
   287  		// Assume path ends at NUL.
   288  		// This is not technically the Solaris semantics for
   289  		// abstract Unix domain sockets -- they are supposed
   290  		// to be uninterpreted fixed-size binary blobs -- but
   291  		// everyone uses this convention.
   292  		n := 0
   293  		for n < len(pp.Path) && pp.Path[n] != 0 {
   294  			n++
   295  		}
   296  		bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
   297  		sa.Name = string(bytes)
   298  		return sa, nil
   299  
   300  	case AF_INET:
   301  		pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
   302  		sa := new(SockaddrInet4)
   303  		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
   304  		sa.Port = int(p[0])<<8 + int(p[1])
   305  		sa.Addr = pp.Addr
   306  		return sa, nil
   307  
   308  	case AF_INET6:
   309  		pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
   310  		sa := new(SockaddrInet6)
   311  		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
   312  		sa.Port = int(p[0])<<8 + int(p[1])
   313  		sa.ZoneId = pp.Scope_id
   314  		sa.Addr = pp.Addr
   315  		return sa, nil
   316  	}
   317  	return nil, EAFNOSUPPORT
   318  }
   319  
   320  //sys	accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) = libsocket.accept
   321  
   322  func Accept(fd int) (nfd int, sa Sockaddr, err error) {
   323  	var rsa RawSockaddrAny
   324  	var len _Socklen = SizeofSockaddrAny
   325  	nfd, err = accept(fd, &rsa, &len)
   326  	if err != nil {
   327  		return
   328  	}
   329  	sa, err = anyToSockaddr(&rsa)
   330  	if err != nil {
   331  		Close(nfd)
   332  		nfd = 0
   333  	}
   334  	return
   335  }
   336  
   337  func recvmsgRaw(fd int, p, oob []byte, flags int, rsa *RawSockaddrAny) (n, oobn int, recvflags int, err error) {
   338  	var msg Msghdr
   339  	msg.Name = (*byte)(unsafe.Pointer(rsa))
   340  	msg.Namelen = uint32(SizeofSockaddrAny)
   341  	var iov Iovec
   342  	if len(p) > 0 {
   343  		iov.Base = (*int8)(unsafe.Pointer(&p[0]))
   344  		iov.SetLen(len(p))
   345  	}
   346  	var dummy int8
   347  	if len(oob) > 0 {
   348  		// receive at least one normal byte
   349  		if len(p) == 0 {
   350  			iov.Base = &dummy
   351  			iov.SetLen(1)
   352  		}
   353  		msg.Accrights = (*int8)(unsafe.Pointer(&oob[0]))
   354  		msg.Accrightslen = int32(len(oob))
   355  	}
   356  	msg.Iov = &iov
   357  	msg.Iovlen = 1
   358  	if n, err = recvmsg(fd, &msg, flags); err != nil {
   359  		return
   360  	}
   361  	oobn = int(msg.Accrightslen)
   362  	return
   363  }
   364  
   365  //sys	sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_sendmsg
   366  
   367  func sendmsgN(fd int, p, oob []byte, ptr unsafe.Pointer, salen _Socklen, flags int) (n int, err error) {
   368  	var msg Msghdr
   369  	msg.Name = (*byte)(unsafe.Pointer(ptr))
   370  	msg.Namelen = uint32(salen)
   371  	var iov Iovec
   372  	if len(p) > 0 {
   373  		iov.Base = (*int8)(unsafe.Pointer(&p[0]))
   374  		iov.SetLen(len(p))
   375  	}
   376  	var dummy int8
   377  	if len(oob) > 0 {
   378  		// send at least one normal byte
   379  		if len(p) == 0 {
   380  			iov.Base = &dummy
   381  			iov.SetLen(1)
   382  		}
   383  		msg.Accrights = (*int8)(unsafe.Pointer(&oob[0]))
   384  		msg.Accrightslen = int32(len(oob))
   385  	}
   386  	msg.Iov = &iov
   387  	msg.Iovlen = 1
   388  	if n, err = sendmsg(fd, &msg, flags); err != nil {
   389  		return 0, err
   390  	}
   391  	if len(oob) > 0 && len(p) == 0 {
   392  		n = 0
   393  	}
   394  	return n, nil
   395  }
   396  
   397  /*
   398   * Exposed directly
   399   */
   400  //sys	Access(path string, mode uint32) (err error)
   401  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   402  //sys	Chdir(path string) (err error)
   403  //sys	Chmod(path string, mode uint32) (err error)
   404  //sys	Chown(path string, uid int, gid int) (err error)
   405  //sys	Chroot(path string) (err error)
   406  //sys	Close(fd int) (err error)
   407  //sys	Dup(fd int) (nfd int, err error)
   408  //sys	Fchdir(fd int) (err error)
   409  //sys	Fchmod(fd int, mode uint32) (err error)
   410  //sys	Fchown(fd int, uid int, gid int) (err error)
   411  //sys	Fpathconf(fd int, name int) (val int, err error)
   412  //sys	Fstat(fd int, stat *Stat_t) (err error)
   413  //sys	Getdents(fd int, buf []byte, basep *uintptr) (n int, err error)
   414  //sysnb	Getgid() (gid int)
   415  //sysnb	Getpid() (pid int)
   416  //sys	Geteuid() (euid int)
   417  //sys	Getegid() (egid int)
   418  //sys	Getppid() (ppid int)
   419  //sys	Getpriority(which int, who int) (n int, err error)
   420  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   421  //sysnb	Gettimeofday(tv *Timeval) (err error)
   422  //sysnb	Getuid() (uid int)
   423  //sys	Kill(pid int, signum Signal) (err error)
   424  //sys	Lchown(path string, uid int, gid int) (err error)
   425  //sys	Link(path string, link string) (err error)
   426  //sys	Listen(s int, backlog int) (err error) = libsocket.__xnet_listen
   427  //sys	Lstat(path string, stat *Stat_t) (err error)
   428  //sys	Mkdir(path string, mode uint32) (err error)
   429  //sys	Mknod(path string, mode uint32, dev int) (err error)
   430  //sys	Nanosleep(time *Timespec, leftover *Timespec) (err error)
   431  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   432  //sys	Pathconf(path string, name int) (val int, err error)
   433  //sys	Pread(fd int, p []byte, offset int64) (n int, err error)
   434  //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
   435  //sys	read(fd int, p []byte) (n int, err error)
   436  //sys	Readlink(path string, buf []byte) (n int, err error)
   437  //sys	Rename(from string, to string) (err error)
   438  //sys	Rmdir(path string) (err error)
   439  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = lseek
   440  //sys	sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = libsendfile.sendfile
   441  //sysnb	Setegid(egid int) (err error)
   442  //sysnb	Seteuid(euid int) (err error)
   443  //sysnb	Setgid(gid int) (err error)
   444  //sysnb	Setpgid(pid int, pgid int) (err error)
   445  //sys	Setpriority(which int, who int, prio int) (err error)
   446  //sysnb	Setregid(rgid int, egid int) (err error)
   447  //sysnb	Setreuid(ruid int, euid int) (err error)
   448  //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
   449  //sysnb	Setsid() (pid int, err error)
   450  //sysnb	Setuid(uid int) (err error)
   451  //sys	Shutdown(s int, how int) (err error) = libsocket.shutdown
   452  //sys	Stat(path string, stat *Stat_t) (err error)
   453  //sys	Symlink(path string, link string) (err error)
   454  //sys	Sync() (err error)
   455  //sys	Truncate(path string, length int64) (err error)
   456  //sys	Fsync(fd int) (err error)
   457  //sys	Ftruncate(fd int, length int64) (err error)
   458  //sys	Umask(newmask int) (oldmask int)
   459  //sys	Unlink(path string) (err error)
   460  //sys	utimes(path string, times *[2]Timeval) (err error)
   461  //sys	bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_bind
   462  //sys	connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_connect
   463  //sys	mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   464  //sys	munmap(addr uintptr, length uintptr) (err error)
   465  //sys	sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) = libsocket.__xnet_sendto
   466  //sys	socket(domain int, typ int, proto int) (fd int, err error) = libsocket.__xnet_socket
   467  //sysnb	socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) = libsocket.__xnet_socketpair
   468  //sys	write(fd int, p []byte) (n int, err error)
   469  //sys	getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) = libsocket.__xnet_getsockopt
   470  //sysnb	getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getpeername
   471  //sys	getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = libsocket.getsockname
   472  //sys	setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) = libsocket.setsockopt
   473  //sys	recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) = libsocket.recvfrom
   474  //sys	recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.__xnet_recvmsg
   475  //sys	getexecname() (path unsafe.Pointer, err error) = libc.getexecname
   476  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flag int) (err error)
   477  
   478  func Getexecname() (path string, err error) {
   479  	ptr, err := getexecname()
   480  	if err != nil {
   481  		return "", err
   482  	}
   483  	bytes := (*[1 << 29]byte)(ptr)[:]
   484  	for i, b := range bytes {
   485  		if b == 0 {
   486  			return string(bytes[:i]), nil
   487  		}
   488  	}
   489  	panic("unreachable")
   490  }
   491  
   492  func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
   493  	r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_read)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
   494  	n = int(r0)
   495  	if e1 != 0 {
   496  		err = e1
   497  	}
   498  	return
   499  }
   500  
   501  func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
   502  	r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_write)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
   503  	n = int(r0)
   504  	if e1 != 0 {
   505  		err = e1
   506  	}
   507  	return
   508  }
   509  
   510  func Utimes(path string, tv []Timeval) error {
   511  	if len(tv) != 2 {
   512  		return EINVAL
   513  	}
   514  	return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
   515  }
   516  

View as plain text