syscall_windows.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package system
  2. import (
  3. "syscall"
  4. "unsafe"
  5. "github.com/Sirupsen/logrus"
  6. )
  7. var (
  8. ntuserApiset = syscall.NewLazyDLL("ext-ms-win-ntuser-window-l1-1-0")
  9. procGetVersionExW = modkernel32.NewProc("GetVersionExW")
  10. procGetProductInfo = modkernel32.NewProc("GetProductInfo")
  11. )
  12. // OSVersion is a wrapper for Windows version information
  13. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724439(v=vs.85).aspx
  14. type OSVersion struct {
  15. Version uint32
  16. MajorVersion uint8
  17. MinorVersion uint8
  18. Build uint16
  19. }
  20. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx
  21. type osVersionInfoEx struct {
  22. OSVersionInfoSize uint32
  23. MajorVersion uint32
  24. MinorVersion uint32
  25. BuildNumber uint32
  26. PlatformID uint32
  27. CSDVersion [128]uint16
  28. ServicePackMajor uint16
  29. ServicePackMinor uint16
  30. SuiteMask uint16
  31. ProductType byte
  32. Reserve byte
  33. }
  34. // GetOSVersion gets the operating system version on Windows. Note that
  35. // docker.exe must be manifested to get the correct version information.
  36. func GetOSVersion() OSVersion {
  37. var err error
  38. osv := OSVersion{}
  39. osv.Version, err = syscall.GetVersion()
  40. if err != nil {
  41. // GetVersion never fails.
  42. panic(err)
  43. }
  44. osv.MajorVersion = uint8(osv.Version & 0xFF)
  45. osv.MinorVersion = uint8(osv.Version >> 8 & 0xFF)
  46. osv.Build = uint16(osv.Version >> 16)
  47. return osv
  48. }
  49. // IsWindowsClient returns true if the SKU is client
  50. // @engine maintainers - this function should not be removed or modified as it
  51. // is used to enforce licensing restrictions on Windows.
  52. func IsWindowsClient() bool {
  53. osviex := &osVersionInfoEx{OSVersionInfoSize: 284}
  54. r1, _, err := procGetVersionExW.Call(uintptr(unsafe.Pointer(osviex)))
  55. if r1 == 0 {
  56. logrus.Warnf("GetVersionExW failed - assuming server SKU: %v", err)
  57. return false
  58. }
  59. const verNTWorkstation = 0x00000001
  60. return osviex.ProductType == verNTWorkstation
  61. }
  62. // IsIoTCore returns true if the currently running image is based off of
  63. // Windows 10 IoT Core.
  64. // @engine maintainers - this function should not be removed or modified as it
  65. // is used to enforce licensing restrictions on Windows.
  66. func IsIoTCore() bool {
  67. var returnedProductType uint32
  68. r1, _, err := procGetProductInfo.Call(6, 1, 0, 0, uintptr(unsafe.Pointer(&returnedProductType)))
  69. if r1 == 0 {
  70. logrus.Warnf("GetProductInfo failed - assuming this is not IoT: %v", err)
  71. return false
  72. }
  73. const productIoTUAP = 0x0000007B
  74. const productIoTUAPCommercial = 0x00000083
  75. return returnedProductType == productIoTUAP || returnedProductType == productIoTUAPCommercial
  76. }
  77. // Unmount is a platform-specific helper function to call
  78. // the unmount syscall. Not supported on Windows
  79. func Unmount(dest string) error {
  80. return nil
  81. }
  82. // CommandLineToArgv wraps the Windows syscall to turn a commandline into an argument array.
  83. func CommandLineToArgv(commandLine string) ([]string, error) {
  84. var argc int32
  85. argsPtr, err := syscall.UTF16PtrFromString(commandLine)
  86. if err != nil {
  87. return nil, err
  88. }
  89. argv, err := syscall.CommandLineToArgv(argsPtr, &argc)
  90. if err != nil {
  91. return nil, err
  92. }
  93. defer syscall.LocalFree(syscall.Handle(uintptr(unsafe.Pointer(argv))))
  94. newArgs := make([]string, argc)
  95. for i, v := range (*argv)[:argc] {
  96. newArgs[i] = string(syscall.UTF16ToString((*v)[:]))
  97. }
  98. return newArgs, nil
  99. }
  100. // HasWin32KSupport determines whether containers that depend on win32k can
  101. // run on this machine. Win32k is the driver used to implement windowing.
  102. func HasWin32KSupport() bool {
  103. // For now, check for ntuser API support on the host. In the future, a host
  104. // may support win32k in containers even if the host does not support ntuser
  105. // APIs.
  106. return ntuserApiset.Load() == nil
  107. }