summaryrefslogtreecommitdiffhomepage
path: root/tstest/kernel_linux.go
blob: ab7c0d529fc13c520c54df0fcb99c06517ef60e8 (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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

//go:build linux

package tstest

import (
	"strconv"
	"strings"

	"golang.org/x/sys/unix"
)

// KernelVersion returns the major, minor, and patch version of the Linux kernel.
// It returns (0, 0, 0) if the version cannot be determined.
func KernelVersion() (major, minor, patch int) {
	var uname unix.Utsname
	if err := unix.Uname(&uname); err != nil {
		return 0, 0, 0
	}
	release := unix.ByteSliceToString(uname.Release[:])

	// Parse version string (e.g., "5.15.0-...")
	parts := strings.Split(release, ".")
	if len(parts) < 3 {
		return 0, 0, 0
	}

	major, err := strconv.Atoi(parts[0])
	if err != nil {
		return 0, 0, 0
	}

	minor, err = strconv.Atoi(parts[1])
	if err != nil {
		return 0, 0, 0
	}

	// Patch version may have additional info after a hyphen (e.g., "0-76-generic")
	// Extract just the numeric part before any hyphen
	patchStr, _, _ := strings.Cut(parts[2], "-")

	patch, err = strconv.Atoi(patchStr)
	if err != nil {
		return 0, 0, 0
	}

	return major, minor, patch
}