github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/system/syscall_windows.go (about)

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