components/golang/patches/0502-Ifb58ac9db8540936d5685c2c58bdc465dbc836cb.patch
changeset 5331 9c955076ffe3
equal deleted inserted replaced
5330:c36e3195e3e9 5331:9c955076ffe3
       
     1 commit af7c9a42c1112175650f5261a2363ca37eec3932
       
     2 Author: Shawn Walker-Salas <[email protected]>
       
     3 Date:   Fri Sep 4 16:53:19 2015 -0700
       
     4 
       
     5     syscall: implement getwd on Solaris
       
     6     
       
     7     In support of the changes required for #8609, it was suggested that
       
     8     syscall.getwd() be updated to work on Solaris first since the runtime
       
     9     uses it and today it's unimplemented.
       
    10     
       
    11     Fixes #12507
       
    12     
       
    13     Change-Id: Ifb58ac9db8540936d5685c2c58bdc465dbc836cb
       
    14     Reviewed-on: https://go-review.googlesource.com/14420
       
    15     Reviewed-by: Aram Hăvărneanu <[email protected]>
       
    16 
       
    17 diff --git a/src/syscall/mkerrors.sh b/src/syscall/mkerrors.sh
       
    18 index 438de6e..b59a46b 100755
       
    19 --- a/src/syscall/mkerrors.sh
       
    20 +++ b/src/syscall/mkerrors.sh
       
    21 @@ -13,6 +13,11 @@ export LC_CTYPE=C
       
    22  
       
    23  CC=${CC:-gcc}
       
    24  
       
    25 +if [[ "$GOOS" -eq "solaris" ]]; then
       
    26 +	# Assumes GNU versions of utilities in PATH.
       
    27 +	export PATH=/usr/gnu/bin:$PATH
       
    28 +fi
       
    29 +
       
    30  uname=$(uname)
       
    31  
       
    32  includes_Darwin='
       
    33 @@ -195,6 +200,7 @@ includes_OpenBSD='
       
    34  '
       
    35  
       
    36  includes_SunOS='
       
    37 +#include <limits.h>
       
    38  #include <sys/types.h>
       
    39  #include <sys/socket.h>
       
    40  #include <sys/sockio.h>
       
    41 diff --git a/src/syscall/mksyscall_solaris.pl b/src/syscall/mksyscall_solaris.pl
       
    42 index f5eb4b3..cd69ebc 100755
       
    43 --- a/src/syscall/mksyscall_solaris.pl
       
    44 +++ b/src/syscall/mksyscall_solaris.pl
       
    45 @@ -38,6 +38,11 @@ if($ARGV[0] =~ /^-/) {
       
    46  	exit 1;
       
    47  }
       
    48  
       
    49 +if($ENV{'GOARCH'} eq "" || $ENV{'GOOS'} eq "") {
       
    50 +	print STDERR "GOARCH or GOOS not defined in environment\n";
       
    51 +	exit 1;
       
    52 +}
       
    53 +
       
    54  sub parseparamlist($) {
       
    55  	my ($list) = @_;
       
    56  	$list =~ s/^\s*//;
       
    57 @@ -60,9 +65,9 @@ sub parseparam($) {
       
    58  
       
    59  my $package = "";
       
    60  my $text = "";
       
    61 -my $vars = "";
       
    62  my $dynimports = "";
       
    63  my $linknames = "";
       
    64 +my @vars = ();
       
    65  while(<>) {
       
    66  	chomp;
       
    67  	s/\s+/ /g;
       
    68 @@ -100,20 +105,19 @@ while(<>) {
       
    69  	}
       
    70  
       
    71  	# System call pointer variable name.
       
    72 -	my $sysvarname = "libc_$sysname";
       
    73 +	my $sysvarname = "libc_${sysname}";
       
    74  
       
    75  	my $strconvfunc = "BytePtrFromString";
       
    76  	my $strconvtype = "*byte";
       
    77  
       
    78 -	# Library proc address variable.
       
    79  	$sysname =~ y/A-Z/a-z/; # All libc functions are lowercase.
       
    80 -	if($vars eq "") {
       
    81 -		$vars .= "\t$sysvarname";
       
    82 -	} else {
       
    83 -		$vars .= ",\n\t$sysvarname";
       
    84 -	}
       
    85 -	$dynimports .= "//go:cgo_import_dynamic $sysvarname $sysname \"$modname.so\"\n";
       
    86 -	$linknames .= "//go:linkname $sysvarname $sysvarname\n";
       
    87 +
       
    88 +	# Runtime import of function to allow cross-platform builds.
       
    89 +	$dynimports .= "//go:cgo_import_dynamic ${sysvarname} ${sysname} \"$modname.so\"\n";
       
    90 +	# Link symbol to proc address variable.
       
    91 +	$linknames .= "//go:linkname ${sysvarname} ${sysvarname}\n";
       
    92 +	# Library proc address variable.
       
    93 +	push @vars, $sysvarname;
       
    94  
       
    95  	# Go function header.
       
    96  	$out = join(', ', @out);
       
    97 @@ -264,6 +268,8 @@ print <<EOF;
       
    98  // $cmdline
       
    99  // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
       
   100  
       
   101 +// +build $ENV{'GOARCH'},$ENV{'GOOS'}
       
   102 +
       
   103  package $package
       
   104  
       
   105  import "unsafe"
       
   106 @@ -271,17 +277,20 @@ EOF
       
   107  
       
   108  print "import \"syscall\"\n" if $package ne "syscall";
       
   109  
       
   110 -print <<EOF;
       
   111 +my $vardecls = "\t" . join(",\n\t", @vars);
       
   112 +$vardecls .= " libcFunc";
       
   113 +
       
   114 +chomp($_=<<EOF);
       
   115  
       
   116  $dynimports
       
   117  $linknames
       
   118  type libcFunc uintptr
       
   119  
       
   120  var (
       
   121 -$vars libcFunc
       
   122 +$vardecls
       
   123  )
       
   124  
       
   125  $text
       
   126 -
       
   127  EOF
       
   128 +print $_;
       
   129  exit 0;
       
   130 diff --git a/src/syscall/syscall_solaris.go b/src/syscall/syscall_solaris.go
       
   131 index 0f60e21..2f68760 100644
       
   132 --- a/src/syscall/syscall_solaris.go
       
   133 +++ b/src/syscall/syscall_solaris.go
       
   134 @@ -142,12 +142,23 @@ func Getsockname(fd int) (sa Sockaddr, err error) {
       
   135  	return anyToSockaddr(&rsa)
       
   136  }
       
   137  
       
   138 -// The const provides a compile-time constant so clients
       
   139 -// can adjust to whether there is a working Getwd and avoid
       
   140 -// even linking this function into the binary.  See ../os/getwd.go.
       
   141 -const ImplementsGetwd = false
       
   142 +const ImplementsGetwd = true
       
   143  
       
   144 -func Getwd() (string, error) { return "", ENOTSUP }
       
   145 +//sys	Getcwd(buf []byte) (n int, err error)
       
   146 +
       
   147 +func Getwd() (wd string, err error) {
       
   148 +	var buf [PathMax]byte
       
   149 +	// Getcwd will return an error if it failed for any reason.
       
   150 +	_, err = Getcwd(buf[0:])
       
   151 +	if err != nil {
       
   152 +		return "", err
       
   153 +	}
       
   154 +	n := clen(buf[:])
       
   155 +	if n < 1 {
       
   156 +		return "", EINVAL
       
   157 +	}
       
   158 +	return string(buf[:n]), nil
       
   159 +}
       
   160  
       
   161  /*
       
   162   * Wrapped
       
   163 diff --git a/src/syscall/types_solaris.go b/src/syscall/types_solaris.go
       
   164 index 53fa350..7246434 100644
       
   165 --- a/src/syscall/types_solaris.go
       
   166 +++ b/src/syscall/types_solaris.go
       
   167 @@ -15,8 +15,14 @@ package syscall
       
   168  
       
   169  /*
       
   170  #define KERNEL
       
   171 +// These defines ensure that builds done on newer versions of Solaris are
       
   172 +// backwards-compatible with older versions of Solaris and
       
   173 +// OpenSolaris-based derivatives.
       
   174 +#define __USE_SUNOS_SOCKETS__          // msghdr
       
   175 +#define __USE_LEGACY_PROTOTYPES__      // iovec
       
   176  #include <dirent.h>
       
   177  #include <fcntl.h>
       
   178 +#include <limits.h>
       
   179  #include <signal.h>
       
   180  #include <termios.h>
       
   181  #include <stdio.h>
       
   182 @@ -69,6 +75,7 @@ const (
       
   183  	sizeofInt      = C.sizeof_int
       
   184  	sizeofLong     = C.sizeof_long
       
   185  	sizeofLongLong = C.sizeof_longlong
       
   186 +	PathMax        = C.PATH_MAX
       
   187  )
       
   188  
       
   189  // Basic types
       
   190 diff --git a/src/syscall/zsyscall_solaris_amd64.go b/src/syscall/zsyscall_solaris_amd64.go
       
   191 index cabab7e..ebdeb92 100644
       
   192 --- a/src/syscall/zsyscall_solaris_amd64.go
       
   193 +++ b/src/syscall/zsyscall_solaris_amd64.go
       
   194 @@ -7,6 +7,7 @@ package syscall
       
   195  
       
   196  import "unsafe"
       
   197  
       
   198 +//go:cgo_import_dynamic libc_Getcwd getcwd "libc.so"
       
   199  //go:cgo_import_dynamic libc_getgroups getgroups "libc.so"
       
   200  //go:cgo_import_dynamic libc_setgroups setgroups "libc.so"
       
   201  //go:cgo_import_dynamic libc_fcntl fcntl "libc.so"
       
   202 @@ -89,6 +90,7 @@ import "unsafe"
       
   203  //go:cgo_import_dynamic libc_recvfrom recvfrom "libsocket.so"
       
   204  //go:cgo_import_dynamic libc_recvmsg recvmsg "libsocket.so"
       
   205  
       
   206 +//go:linkname libc_Getcwd libc_Getcwd
       
   207  //go:linkname libc_getgroups libc_getgroups
       
   208  //go:linkname libc_setgroups libc_setgroups
       
   209  //go:linkname libc_fcntl libc_fcntl
       
   210 @@ -174,6 +176,7 @@ import "unsafe"
       
   211  type libcFunc uintptr
       
   212  
       
   213  var (
       
   214 +	libc_Getcwd,
       
   215  	libc_getgroups,
       
   216  	libc_setgroups,
       
   217  	libc_fcntl,
       
   218 @@ -257,6 +260,19 @@ var (
       
   219  	libc_recvmsg libcFunc
       
   220  )
       
   221  
       
   222 +func Getcwd(buf []byte) (n int, err error) {
       
   223 +	var _p0 *byte
       
   224 +	if len(buf) > 0 {
       
   225 +		_p0 = &buf[0]
       
   226 +	}
       
   227 +	r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_Getcwd)), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0, 0, 0, 0)
       
   228 +	n = int(r0)
       
   229 +	if e1 != 0 {
       
   230 +		err = errnoErr(e1)
       
   231 +	}
       
   232 +	return
       
   233 +}
       
   234 +
       
   235  func getgroups(ngid int, gid *_Gid_t) (n int, err error) {
       
   236  	r0, _, e1 := rawSysvicall6(uintptr(unsafe.Pointer(&libc_getgroups)), 2, uintptr(ngid), uintptr(unsafe.Pointer(gid)), 0, 0, 0, 0)
       
   237  	n = int(r0)
       
   238 diff --git a/src/syscall/ztypes_solaris_amd64.go b/src/syscall/ztypes_solaris_amd64.go
       
   239 index 2471519..4cf07ed 100644
       
   240 --- a/src/syscall/ztypes_solaris_amd64.go
       
   241 +++ b/src/syscall/ztypes_solaris_amd64.go
       
   242 @@ -11,6 +11,7 @@ const (
       
   243  	sizeofInt      = 0x4
       
   244  	sizeofLong     = 0x8
       
   245  	sizeofLongLong = 0x8
       
   246 +	PathMax        = 0x400
       
   247  )
       
   248  
       
   249  type (