github.com/shiroyuki/docker@v1.9.0/pkg/parsers/kernel/kernel_windows.go (about)

     1  package kernel
     2  
     3  import (
     4  	"fmt"
     5  	"syscall"
     6  	"unsafe"
     7  )
     8  
     9  // VersionInfo holds information about the kernel.
    10  type VersionInfo struct {
    11  	kvi   string // Version of the kernel (e.g. 6.1.7601.17592 -> 6)
    12  	major int    // Major part of the kernel version (e.g. 6.1.7601.17592 -> 1)
    13  	minor int    // Minor part of the kernel version (e.g. 6.1.7601.17592 -> 7601)
    14  	build int    // Build number of the kernel version (e.g. 6.1.7601.17592 -> 17592)
    15  }
    16  
    17  func (k *VersionInfo) String() string {
    18  	return fmt.Sprintf("%d.%d %d (%s)", k.major, k.minor, k.build, k.kvi)
    19  }
    20  
    21  // GetKernelVersion gets the current kernel version.
    22  func GetKernelVersion() (*VersionInfo, error) {
    23  
    24  	var (
    25  		h         syscall.Handle
    26  		dwVersion uint32
    27  		err       error
    28  	)
    29  
    30  	KVI := &VersionInfo{"Unknown", 0, 0, 0}
    31  
    32  	if err = syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE,
    33  		syscall.StringToUTF16Ptr(`SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\`),
    34  		0,
    35  		syscall.KEY_READ,
    36  		&h); err != nil {
    37  		return KVI, err
    38  	}
    39  	defer syscall.RegCloseKey(h)
    40  
    41  	var buf [1 << 10]uint16
    42  	var typ uint32
    43  	n := uint32(len(buf) * 2) // api expects array of bytes, not uint16
    44  
    45  	if err = syscall.RegQueryValueEx(h,
    46  		syscall.StringToUTF16Ptr("BuildLabEx"),
    47  		nil,
    48  		&typ,
    49  		(*byte)(unsafe.Pointer(&buf[0])),
    50  		&n); err != nil {
    51  		return KVI, err
    52  	}
    53  
    54  	KVI.kvi = syscall.UTF16ToString(buf[:])
    55  
    56  	// Important - docker.exe MUST be manifested for this API to return
    57  	// the correct information.
    58  	if dwVersion, err = syscall.GetVersion(); err != nil {
    59  		return KVI, err
    60  	}
    61  
    62  	KVI.major = int(dwVersion & 0xFF)
    63  	KVI.minor = int((dwVersion & 0XFF00) >> 8)
    64  	KVI.build = int((dwVersion & 0xFFFF0000) >> 16)
    65  
    66  	return KVI, nil
    67  }