summaryrefslogtreecommitdiff
path: root/pkg/parsers/operatingsystem/operatingsystem_windows.go
blob: c62db180f972608eaecb8ac4fd97e159da369a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package operatingsystem // import "github.com/docker/docker/pkg/parsers/operatingsystem"

import (
	"errors"

	"github.com/Microsoft/hcsshim/osversion"
	"golang.org/x/sys/windows"
	"golang.org/x/sys/windows/registry"
)

// VER_NT_WORKSTATION, see https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoexa
const verNTWorkstation = 0x00000001 // VER_NT_WORKSTATION

// GetOperatingSystem gets the name of the current operating system.
func GetOperatingSystem() (string, error) {
	osversion := windows.RtlGetVersion() // Always succeeds.
	rel := windowsOSRelease{
		IsServer: osversion.ProductType != verNTWorkstation,
		Build:    osversion.BuildNumber,
	}

	// Make a best-effort attempt to retrieve the display version and
	// Update Build Revision by querying undocumented registry values.
	key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
	if err == nil {
		defer key.Close()
		if ver, err := getFirstStringValue(key,
			"DisplayVersion", /* Windows 20H2 and above */
			"ReleaseId",      /* Windows 2009 and below */
		); err == nil {
			rel.DisplayVersion = ver
		}
		if ubr, _, err := key.GetIntegerValue("UBR"); err == nil {
			rel.UBR = ubr
		}
	}

	return rel.String(), nil
}

func getFirstStringValue(key registry.Key, names ...string) (string, error) {
	for _, n := range names {
		val, _, err := key.GetStringValue(n)
		if err != nil {
			if !errors.Is(err, registry.ErrNotExist) {
				return "", err
			}
			continue
		}
		return val, nil
	}
	return "", registry.ErrNotExist
}

// GetOperatingSystemVersion gets the version of the current operating system, as a string.
func GetOperatingSystemVersion() (string, error) {
	return osversion.Get().ToString(), nil
}

// IsContainerized returns true if we are running inside a container.
// No-op on Windows, always returns false.
func IsContainerized() (bool, error) {
	return false, nil
}

// IsWindowsClient returns true if the SKU is client. It returns false on
// Windows server.
func IsWindowsClient() bool {
	return windows.RtlGetVersion().ProductType == verNTWorkstation
}