github.com/portworx/docker@v1.12.1/pkg/system/syscall_windows.go (about)

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