Skip to content

Commit

Permalink
fallback to /proc/version on Linux, add changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
intxgo committed Feb 5, 2024
1 parent 771ce25 commit e220c6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/200.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Adds NativeArchitecture to HostInfo to allow applications to detect whether they are running in emulation.
```
24 changes: 22 additions & 2 deletions providers/linux/arch_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ import (
"syscall"
)

const procSysKernelArch = "/proc/sys/kernel/arch"
const (
procSysKernelArch = "/proc/sys/kernel/arch"
procVersion = "/proc/version"
archAmd64 = "amd64"
archArm64 = "arm64"
archAarch64 = "aarch64"
)

func Architecture() (string, error) {
var uname syscall.Utsname
Expand All @@ -47,14 +53,28 @@ func NativeArchitecture() (string, error) {
data, err := os.ReadFile(procSysKernelArch)
if err != nil {
if os.IsNotExist(err) {
// fallback to checking version string
version, err := os.ReadFile(procVersion)
if err != nil {
return "", nil
}

versionStr := string(version)
if strings.Contains(versionStr, archAmd64) {
return archAmd64, nil
} else if strings.Contains(versionStr, archArm64) {
// for parity with Architecture() and /proc/sys/kernel/arch
// as aarch64 and arm64 are used interchangeably
return archAarch64, nil
}
return "", nil
}

return "", fmt.Errorf("failed to read kernel arch: %w", err)
}

nativeArch := string(data)
nativeArch = strings.TrimSpace(nativeArch)
nativeArch = strings.TrimRight(nativeArch, "\n")

return string(data), nil
}
12 changes: 6 additions & 6 deletions providers/windows/arch_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
)

const (
IMAGE_FILE_MACHINE_AMD64 = 0x8664
IMAGE_FILE_MACHINE_ARM64 = 0xAA64
archIntel = "x86_64"
archArm64 = "arm64"
imageFileMachineAmd64 = 0x8664
imageFileMachineArm64 = 0xAA64
archIntel = "x86_64"
archArm64 = "arm64"
)

func Architecture() (string, error) {
Expand All @@ -52,10 +52,10 @@ func NativeArchitecture() (string, error) {
nativeArch := ""

switch nativeMachine {
case IMAGE_FILE_MACHINE_AMD64:
case imageFileMachineAmd64:
// for parity with Architecture() as amd64 and x86_64 are used interchangeably
nativeArch = archIntel
case IMAGE_FILE_MACHINE_ARM64:
case imageFileMachineArm64:
nativeArch = archArm64
default:
}
Expand Down

0 comments on commit e220c6d

Please sign in to comment.