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

[platform] Detect if running under Rosetta translator #77

Merged
merged 3 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Gopkg.lock

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

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)
}
}