From e220c6d96dc6969ed301ceff8cf300781304cdeb Mon Sep 17 00:00:00 2001 From: Leszek Kubik <39905449+intxgo@users.noreply.github.com> Date: Mon, 5 Feb 2024 11:42:43 +0100 Subject: [PATCH] fallback to /proc/version on Linux, add changelog --- .changelog/200.txt | 3 +++ providers/linux/arch_linux.go | 24 ++++++++++++++++++++++-- providers/windows/arch_windows.go | 12 ++++++------ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 .changelog/200.txt diff --git a/.changelog/200.txt b/.changelog/200.txt new file mode 100644 index 0000000..51afa52 --- /dev/null +++ b/.changelog/200.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +Adds NativeArchitecture to HostInfo to allow applications to detect whether they are running in emulation. +``` \ No newline at end of file diff --git a/providers/linux/arch_linux.go b/providers/linux/arch_linux.go index 50e943e..06f7e50 100644 --- a/providers/linux/arch_linux.go +++ b/providers/linux/arch_linux.go @@ -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 @@ -47,6 +53,20 @@ 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 } @@ -54,7 +74,7 @@ func NativeArchitecture() (string, error) { } nativeArch := string(data) - nativeArch = strings.TrimSpace(nativeArch) + nativeArch = strings.TrimRight(nativeArch, "\n") return string(data), nil } diff --git a/providers/windows/arch_windows.go b/providers/windows/arch_windows.go index a59fcbc..1c78efc 100644 --- a/providers/windows/arch_windows.go +++ b/providers/windows/arch_windows.go @@ -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) { @@ -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: }