Browse Source

Merge pull request #45298 from thaJeztah/pkg_pkatform_cleanup

pkg/platform: cleanup, and deprecate OSType
Sebastiaan van Stijn 2 years ago
parent
commit
c80f205f86

+ 1 - 1
daemon/info.go

@@ -44,7 +44,7 @@ func (daemon *Daemon) SystemInfo() *types.Info {
 		OperatingSystem:    operatingSystem(),
 		OSVersion:          osVersion(),
 		IndexServerAddress: registry.IndexServer,
-		OSType:             platform.OSType,
+		OSType:             runtime.GOOS,
 		Architecture:       platform.Architecture,
 		RegistryConfig:     daemon.registryService.ServiceConfig(),
 		NCPU:               sysinfo.NumCPU(),

+ 0 - 63
pkg/platform/architecture_windows.go

@@ -1,63 +0,0 @@
-package platform // import "github.com/docker/docker/pkg/platform"
-
-import (
-	"fmt"
-	"syscall"
-	"unsafe"
-
-	"golang.org/x/sys/windows"
-)
-
-var (
-	modkernel32       = windows.NewLazySystemDLL("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
-	ProcessorArchitectureArm64 = 12 // PROCESSOR_ARCHITECTURE_ARM64
-)
-
-// runtimeArchitecture gets the name of the current architecture (x86, x86_64, …)
-func runtimeArchitecture() (string, error) {
-	var sysinfo systeminfo
-	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
-	case ProcessorArchitectureArm64:
-		return "arm64", nil
-	default:
-		return "", fmt.Errorf("unknown processor architecture %+v", sysinfo.wProcessorArchitecture)
-	}
-}
-
-// NumProcs returns the number of processors on the system
-func NumProcs() uint32 {
-	var sysinfo systeminfo
-	syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0)
-	return sysinfo.dwNumberOfProcessors
-}

+ 17 - 8
pkg/platform/platform.go

@@ -1,3 +1,5 @@
+// Package platform provides helper function to get the runtime architecture
+// for different platforms.
 package platform // import "github.com/docker/docker/pkg/platform"
 
 import (
@@ -6,18 +8,25 @@ import (
 	"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
-)
+// Architecture holds the runtime architecture of the process.
+//
+// Unlike [runtime.GOARCH] (which refers to the compiler platform),
+// Architecture refers to the running platform.
+//
+// For example, Architecture reports "x86_64" as architecture, even
+// when running a "linux/386" compiled binary on "linux/amd64" hardware.
+var Architecture string
+
+// OSType holds the runtime operating system type of the process. It is
+// an alias for [runtime.GOOS].
+//
+// Deprecated: use [runtime.GOOS] instead.
+const OSType = runtime.GOOS
 
 func init() {
 	var err error
 	Architecture, err = runtimeArchitecture()
 	if err != nil {
-		logrus.Errorf("Could not read system architecture info: %v", err)
+		logrus.WithError(err).Error("Could not read system architecture info")
 	}
-	OSType = runtime.GOOS
 }

+ 0 - 2
pkg/platform/architecture_unix.go → pkg/platform/platform_unix.go

@@ -1,8 +1,6 @@
 //go:build !windows
 // +build !windows
 
-// Package platform provides helper function to get the runtime architecture
-// for different platforms.
 package platform // import "github.com/docker/docker/pkg/platform"
 
 import (

+ 71 - 0
pkg/platform/platform_windows.go

@@ -0,0 +1,71 @@
+package platform // import "github.com/docker/docker/pkg/platform"
+
+import (
+	"fmt"
+	"syscall"
+	"unsafe"
+
+	"golang.org/x/sys/windows"
+)
+
+var (
+	modkernel32       = windows.NewLazySystemDLL("kernel32.dll")
+	procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
+)
+
+// see https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
+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
+}
+
+// Windows processor architectures.
+//
+// see https://github.com/microsoft/go-winio/blob/v0.6.0/wim/wim.go#L48-L65
+// see https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
+const (
+	processorArchitecture64    = 9  // PROCESSOR_ARCHITECTURE_AMD64
+	processorArchitectureIA64  = 6  // PROCESSOR_ARCHITECTURE_IA64
+	processorArchitecture32    = 0  // PROCESSOR_ARCHITECTURE_INTEL
+	processorArchitectureArm   = 5  // PROCESSOR_ARCHITECTURE_ARM
+	processorArchitectureArm64 = 12 // PROCESSOR_ARCHITECTURE_ARM64
+)
+
+// runtimeArchitecture gets the name of the current architecture (x86, x86_64, …)
+func runtimeArchitecture() (string, error) {
+	// TODO(thaJeztah): rewrite this to use "GetNativeSystemInfo" instead.
+	// See: https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
+	// See: https://github.com/shirou/gopsutil/blob/v3.23.3/host/host_windows.go#L267-L297
+	// > To retrieve accurate information for an application running on WOW64,
+	// > call the GetNativeSystemInfo function.
+	var sysinfo systeminfo
+	_, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&sysinfo)))
+	switch sysinfo.wProcessorArchitecture {
+	case processorArchitecture64, processorArchitectureIA64:
+		return "x86_64", nil
+	case processorArchitecture32:
+		return "i686", nil
+	case processorArchitectureArm:
+		return "arm", nil
+	case processorArchitectureArm64:
+		return "arm64", nil
+	default:
+		return "", fmt.Errorf("unknown processor architecture %+v", sysinfo.wProcessorArchitecture)
+	}
+}
+
+// NumProcs returns the number of processors on the system
+func NumProcs() uint32 {
+	var sysinfo systeminfo
+	_, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&sysinfo)))
+	return sysinfo.dwNumberOfProcessors
+}