mirror of
https://github.com/tailscale/tailscale.git
synced 2026-04-26 16:01:03 +02:00
The kernel version parser used strings.Cut with "-" to handle versions like "5.4.0-76-generic", but Debian uses "+" in versions like "6.12.41+deb13-amd64". Use strings.IndexAny to find the first "-" or "+" and truncate there. Fixes TestKernelVersion on Debian systems. Fixes #19395 Change-Id: I70e5f95682d54baf908e51f9f4b51c130b00aaaa Co-Authored-By: Brad Fitzpatrick <bradfitz@tailscale.com> Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
// 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[:])
|
|
return parseKernelVersion(release)
|
|
}
|
|
|
|
// parseKernelVersion parses a Linux kernel version string like "6.12.73+deb13-amd64"
|
|
// or "5.15.0-76-generic" and returns the major, minor, and patch components.
|
|
// It returns (0, 0, 0) if the version cannot be parsed.
|
|
func parseKernelVersion(release string) (major, minor, patch int) {
|
|
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 or plus (e.g., "0-76-generic" or "41+deb13-amd64")
|
|
// Extract just the numeric part before any hyphen or plus
|
|
patchStr := parts[2]
|
|
if idx := strings.IndexAny(patchStr, "-+"); idx != -1 {
|
|
patchStr = patchStr[:idx]
|
|
}
|
|
|
|
patch, err = strconv.Atoi(patchStr)
|
|
if err != nil {
|
|
return 0, 0, 0
|
|
}
|
|
|
|
return major, minor, patch
|
|
}
|