Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
[platform] Detect if running under Rosetta translator (#77)
Browse files Browse the repository at this point in the history
* Detect whether running under Rosetta translator
Bump `golang.org/x/sys` package to latest version

* Fix merge
Bump sys package
  • Loading branch information
mx-psi authored Mar 3, 2021
1 parent 37c9029 commit 6b668ac
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
100 changes: 100 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
28 changes: 27 additions & 1 deletion platform/platform_darwin.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
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]
archInfo["kernel_release"] = values[2]
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)
}
}

0 comments on commit 6b668ac

Please sign in to comment.