From 4ee06083739251b302b09e85e2ffad1ce3b45b4f Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Sat, 14 Nov 2015 23:03:02 +0100 Subject: [PATCH] Add pkg/parsers/architecture and pkg/platform Signed-off-by: Vincent Demeester --- platform/architecture_freebsd.go | 15 +++++++++ platform/architecture_linux.go | 28 +++++++++++++++++ platform/architecture_windows.go | 52 ++++++++++++++++++++++++++++++++ platform/platform.go | 23 ++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 platform/architecture_freebsd.go create mode 100644 platform/architecture_linux.go create mode 100644 platform/architecture_windows.go create mode 100644 platform/platform.go diff --git a/platform/architecture_freebsd.go b/platform/architecture_freebsd.go new file mode 100644 index 0000000..e8bf398 --- /dev/null +++ b/platform/architecture_freebsd.go @@ -0,0 +1,15 @@ +package platform + +import ( + "os/exec" +) + +// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …) +func GetRuntimeArchitecture() (string, error) { + cmd := exec.Command("uname", "-m") + machine, err := cmd.Output() + if err != nil { + return "", err + } + return string(machine), nil +} diff --git a/platform/architecture_linux.go b/platform/architecture_linux.go new file mode 100644 index 0000000..e732534 --- /dev/null +++ b/platform/architecture_linux.go @@ -0,0 +1,28 @@ +// Package platform provides helper function to get the runtime architecture +// for different platforms. +package platform + +import ( + "syscall" +) + +// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …) +func GetRuntimeArchitecture() (string, error) { + utsname := &syscall.Utsname{} + if err := syscall.Uname(utsname); err != nil { + return "", err + } + return charsToString(utsname.Machine), nil +} + +func charsToString(ca [65]int8) string { + s := make([]byte, len(ca)) + var lens int + for ; lens < len(ca); lens++ { + if ca[lens] == 0 { + break + } + s[lens] = uint8(ca[lens]) + } + return string(s[0:lens]) +} diff --git a/platform/architecture_windows.go b/platform/architecture_windows.go new file mode 100644 index 0000000..19717c9 --- /dev/null +++ b/platform/architecture_windows.go @@ -0,0 +1,52 @@ +package platform + +import ( + "fmt" + "syscall" + "unsafe" +) + +var ( + modkernel32 = syscall.NewLazyDLL("kernel32.dll") + procGetSystemInfo = modkernel32.NewProc("GetSystemInfo") +) + +// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx +type systeminfo struct { + wProcessorArchitecture uint16 + wReserved uint16 + dwPageSize uint32 + lpMinimumApplicationAddress uintptr + lpMaximumApplicationAddress uintptr + dwActiveProcessorMask uintptr + dwNumberOfProcessors uint32 + dwProcessorType uint32 + dwAllocationGranularity uint32 + wProcessorLevel uint16 + wProcessorRevision uint16 +} + +// Constants +const ( + ProcessorArchitecture64 = 9 // PROCESSOR_ARCHITECTURE_AMD64 + ProcessorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64 + ProcessorArchitecture32 = 0 // PROCESSOR_ARCHITECTURE_INTEL + ProcessorArchitectureArm = 5 // PROCESSOR_ARCHITECTURE_ARM +) + +var sysinfo systeminfo + +// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …) +func GetRuntimeArchitecture() (string, error) { + syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0) + switch sysinfo.wProcessorArchitecture { + case ProcessorArchitecture64, ProcessorArchitectureIA64: + return "x86_64", nil + case ProcessorArchitecture32: + return "i686", nil + case ProcessorArchitectureArm: + return "arm", nil + default: + return "", fmt.Errorf("Unknown processor architecture") + } +} diff --git a/platform/platform.go b/platform/platform.go new file mode 100644 index 0000000..d84c4f5 --- /dev/null +++ b/platform/platform.go @@ -0,0 +1,23 @@ +package platform + +import ( + "runtime" + + "github.com/Sirupsen/logrus" +) + +var ( + // Architecture holds the runtime architecture of the process. + Architecture string + // OSType holds the runtime operating system type (Linux, …) of the process. + OSType string +) + +func init() { + var err error + Architecture, err = GetRuntimeArchitecture() + if err != nil { + logrus.Errorf("Could no read system architecture info: %v", err) + } + OSType = runtime.GOOS +}