From f72712d15d753f70340208ef5bc74f8a9eb4cc38 Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Tue, 2 Mar 2021 14:16:54 +0100 Subject: [PATCH 1/2] Detect whether running under Rosetta translator Bump `golang.org/x/sys` package to latest version --- Gopkg.lock | 6 ++++-- platform/platform_darwin.go | 28 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 66faab0..7c83bd5 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -75,14 +75,16 @@ version = "v1.3.0" [[projects]] - digest = "1:70e5f36e9cecff16b230664bbe7be728b9dcb803b45052c3cd3906ee195f52d2" + branch = "master" + digest = "1:228f27b6ae6aa190a8082e78c76870062506763cae2526543dc8f7ac9bea0947" name = "golang.org/x/sys" packages = [ + "unix", "windows", "windows/registry", ] pruneopts = "UT" - revision = "9a7256cb28ed514b4e1e5f68959914c4c28a92e0" + revision = "77cc2087c03b9062d8eb49dd60e665cbd7c1cde2" [solve-meta] analyzer-name = "dep" diff --git a/platform/platform_darwin.go b/platform/platform_darwin.go index 77c61e1..7f092da 100644 --- a/platform/platform_darwin.go +++ b/platform/platform_darwin.go @@ -1,9 +1,27 @@ package platform -import "strings" +import ( + "strings" + + log "github.com/cihub/seelog" + "golang.org/x/sys/unix" +) var unameOptions = []string{"-s", "-n", "-r", "-m", "-p"} +// processIsTranslated detects if the process using gohai is running under the Rosetta 2 translator +func processIsTranslated() (bool, error) { + // https://developer.apple.com/documentation/apple_silicon/about_the_rosetta_translation_environment#3616845 + ret, err := unix.SysctlUint32("sysctl.proc_translated") + + if err == nil { + return ret == 1, nil + } else if err.(unix.Errno) == unix.ENOENT { + return false, nil + } + return false, err +} + func updateArchInfo(archInfo map[string]interface{}, values []string) { archInfo["kernel_name"] = values[0] archInfo["hostname"] = values[1] @@ -11,4 +29,12 @@ func updateArchInfo(archInfo map[string]interface{}, values []string) { archInfo["machine"] = values[3] archInfo["processor"] = strings.Trim(values[4], "\n") archInfo["os"] = values[0] + + if isTranslated, err := processIsTranslated(); err == nil && isTranslated { + log.Debug("Running under Rosetta translator; overriding architecture values") + archInfo["processor"] = "arm" + archInfo["machine"] = "arm64" + } else if err != nil { + log.Debugf("Error when detecting Rosetta translator: %s", err) + } } From 87a285afbae26a5a4099640e4b832d59e073d102 Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Wed, 3 Mar 2021 11:25:26 +0100 Subject: [PATCH 2/2] Fix merge Bump sys package --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 37138f9..14d821e 100644 --- a/go.mod +++ b/go.mod @@ -10,5 +10,5 @@ require ( github.com/shirou/gopsutil v2.0.0+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect github.com/stretchr/testify v1.3.0 - golang.org/x/sys v0.0.0-20170329061634-9a7256cb28ed + golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 ) diff --git a/go.sum b/go.sum index 991a636..7d4cced 100644 --- a/go.sum +++ b/go.sum @@ -18,3 +18,5 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/sys v0.0.0-20170329061634-9a7256cb28ed h1:1RoWulWaFGB+t8NExLratqmgydjWzGMhG2wyOqZxifQ= golang.org/x/sys v0.0.0-20170329061634-9a7256cb28ed/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 h1:cEhElsAv9LUt9ZUUocxzWe05oFLVd+AA2nstydTeI8g= +golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=