syscall_windows.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. func IsWindowsClient() bool {
  50. osviex := &osVersionInfoEx{OSVersionInfoSize: 284}
  51. r1, _, err := procGetVersionExW.Call(uintptr(unsafe.Pointer(osviex)))
  52. if r1 == 0 {
  53. logrus.Warnf("GetVersionExW failed - assuming server SKU: %v", err)
  54. return false
  55. }
  56. const verNTWorkstation = 0x00000001
  57. return osviex.ProductType == verNTWorkstation
  58. }
  59. // Unmount is a platform-specific helper function to call
  60. // the unmount syscall. Not supported on Windows
  61. func Unmount(dest string) error {
  62. return nil
  63. }
  64. // CommandLineToArgv wraps the Windows syscall to turn a commandline into an argument array.
  65. func CommandLineToArgv(commandLine string) ([]string, error) {
  66. var argc int32
  67. argsPtr, err := syscall.UTF16PtrFromString(commandLine)
  68. if err != nil {
  69. return nil, err
  70. }
  71. argv, err := syscall.CommandLineToArgv(argsPtr, &argc)
  72. if err != nil {
  73. return nil, err
  74. }
  75. defer syscall.LocalFree(syscall.Handle(uintptr(unsafe.Pointer(argv))))
  76. newArgs := make([]string, argc)
  77. for i, v := range (*argv)[:argc] {
  78. newArgs[i] = string(syscall.UTF16ToString((*v)[:]))
  79. }
  80. return newArgs, nil
  81. }
  82. // HasWin32KSupport determines whether containers that depend on win32k can
  83. // run on this machine. Win32k is the driver used to implement windowing.
  84. func HasWin32KSupport() bool {
  85. // For now, check for ntuser API support on the host. In the future, a host
  86. // may support win32k in containers even if the host does not support ntuser
  87. // APIs.
  88. return ntuserApiset.Load() == nil
  89. }