syscall_windows.go 2.9 KB

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