From af7c9a42c1112175650f5261a2363ca37eec3932 Mon Sep 17 00:00:00 2001 From: Shawn Walker-Salas Date: Fri, 4 Sep 2015 16:53:19 -0700 Subject: [PATCH] syscall: implement getwd on Solaris MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit In support of the changes required for #8609, it was suggested that syscall.getwd() be updated to work on Solaris first since the runtime uses it and today it's unimplemented. Fixes #12507 Change-Id: Ifb58ac9db8540936d5685c2c58bdc465dbc836cb Reviewed-on: https://go-review.googlesource.com/14420 Reviewed-by: Aram Hăvărneanu --- src/syscall/mkerrors.sh | 6 +++++ src/syscall/mksyscall_solaris.pl | 35 +++++++++++++++++---------- src/syscall/syscall_solaris.go | 21 ++++++++++++---- src/syscall/types_solaris.go | 7 ++++++ src/syscall/zsyscall_solaris_amd64.go | 16 ++++++++++++ src/syscall/ztypes_solaris_amd64.go | 1 + 6 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/syscall/mkerrors.sh b/src/syscall/mkerrors.sh index 438de6e5d8..b59a46b18f 100755 --- a/src/syscall/mkerrors.sh +++ b/src/syscall/mkerrors.sh @@ -13,6 +13,11 @@ export LC_CTYPE=C CC=${CC:-gcc} +if [[ "$GOOS" -eq "solaris" ]]; then + # Assumes GNU versions of utilities in PATH. + export PATH=/usr/gnu/bin:$PATH +fi + uname=$(uname) includes_Darwin=' @@ -195,6 +200,7 @@ includes_OpenBSD=' ' includes_SunOS=' +#include #include #include #include diff --git a/src/syscall/mksyscall_solaris.pl b/src/syscall/mksyscall_solaris.pl index f5eb4b36e8..cd69ebc8a2 100755 --- a/src/syscall/mksyscall_solaris.pl +++ b/src/syscall/mksyscall_solaris.pl @@ -38,6 +38,11 @@ if($ARGV[0] =~ /^-/) { exit 1; } +if($ENV{'GOARCH'} eq "" || $ENV{'GOOS'} eq "") { + print STDERR "GOARCH or GOOS not defined in environment\n"; + exit 1; +} + sub parseparamlist($) { my ($list) = @_; $list =~ s/^\s*//; @@ -60,9 +65,9 @@ sub parseparam($) { my $package = ""; my $text = ""; -my $vars = ""; my $dynimports = ""; my $linknames = ""; +my @vars = (); while(<>) { chomp; s/\s+/ /g; @@ -100,20 +105,19 @@ while(<>) { } # System call pointer variable name. - my $sysvarname = "libc_$sysname"; + my $sysvarname = "libc_${sysname}"; my $strconvfunc = "BytePtrFromString"; my $strconvtype = "*byte"; - # Library proc address variable. $sysname =~ y/A-Z/a-z/; # All libc functions are lowercase. - if($vars eq "") { - $vars .= "\t$sysvarname"; - } else { - $vars .= ",\n\t$sysvarname"; - } - $dynimports .= "//go:cgo_import_dynamic $sysvarname $sysname \"$modname.so\"\n"; - $linknames .= "//go:linkname $sysvarname $sysvarname\n"; + + # Runtime import of function to allow cross-platform builds. + $dynimports .= "//go:cgo_import_dynamic ${sysvarname} ${sysname} \"$modname.so\"\n"; + # Link symbol to proc address variable. + $linknames .= "//go:linkname ${sysvarname} ${sysvarname}\n"; + # Library proc address variable. + push @vars, $sysvarname; # Go function header. $out = join(', ', @out); @@ -264,6 +268,8 @@ print < #include +#include #include #include #include @@ -69,6 +75,7 @@ const ( sizeofInt = C.sizeof_int sizeofLong = C.sizeof_long sizeofLongLong = C.sizeof_longlong + PathMax = C.PATH_MAX ) // Basic types diff --git a/src/syscall/zsyscall_solaris_amd64.go b/src/syscall/zsyscall_solaris_amd64.go index cabab7ece3..ebdeb92bfb 100644 --- a/src/syscall/zsyscall_solaris_amd64.go +++ b/src/syscall/zsyscall_solaris_amd64.go @@ -7,6 +7,7 @@ package syscall import "unsafe" +//go:cgo_import_dynamic libc_Getcwd getcwd "libc.so" //go:cgo_import_dynamic libc_getgroups getgroups "libc.so" //go:cgo_import_dynamic libc_setgroups setgroups "libc.so" //go:cgo_import_dynamic libc_fcntl fcntl "libc.so" @@ -89,6 +90,7 @@ import "unsafe" //go:cgo_import_dynamic libc_recvfrom recvfrom "libsocket.so" //go:cgo_import_dynamic libc_recvmsg recvmsg "libsocket.so" +//go:linkname libc_Getcwd libc_Getcwd //go:linkname libc_getgroups libc_getgroups //go:linkname libc_setgroups libc_setgroups //go:linkname libc_fcntl libc_fcntl @@ -174,6 +176,7 @@ import "unsafe" type libcFunc uintptr var ( + libc_Getcwd, libc_getgroups, libc_setgroups, libc_fcntl, @@ -257,6 +260,19 @@ var ( libc_recvmsg libcFunc ) +func Getcwd(buf []byte) (n int, err error) { + var _p0 *byte + if len(buf) > 0 { + _p0 = &buf[0] + } + r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_Getcwd)), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0, 0, 0, 0) + n = int(r0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + func getgroups(ngid int, gid *_Gid_t) (n int, err error) { r0, _, e1 := rawSysvicall6(uintptr(unsafe.Pointer(&libc_getgroups)), 2, uintptr(ngid), uintptr(unsafe.Pointer(gid)), 0, 0, 0, 0) n = int(r0) diff --git a/src/syscall/ztypes_solaris_amd64.go b/src/syscall/ztypes_solaris_amd64.go index 2471519ade..4cf07ed496 100644 --- a/src/syscall/ztypes_solaris_amd64.go +++ b/src/syscall/ztypes_solaris_amd64.go @@ -11,6 +11,7 @@ const ( sizeofInt = 0x4 sizeofLong = 0x8 sizeofLongLong = 0x8 + PathMax = 0x400 ) type ( -- 2.48.1