diff --git a/hostinfo/hostinfo_linux.go b/hostinfo/hostinfo_linux.go index e2a878be6..90ceece90 100644 --- a/hostinfo/hostinfo_linux.go +++ b/hostinfo/hostinfo_linux.go @@ -113,6 +113,8 @@ func osVersionLinux() string { return fmt.Sprintf("Synology %s%s", m["productversion"], attr) case distro.OpenWrt: return fmt.Sprintf("OpenWrt %s%s", m["DISTRIB_RELEASE"], attr) + case distro.Gokrazy: + return fmt.Sprintf("Gokrazy%s", attr) } return fmt.Sprintf("Other%s", attr) } diff --git a/version/distro/distro.go b/version/distro/distro.go index 67c455890..4107b8a77 100644 --- a/version/distro/distro.go +++ b/version/distro/distro.go @@ -8,6 +8,7 @@ package distro import ( "os" "runtime" + "sync/atomic" ) type Distro string @@ -22,17 +23,25 @@ const ( Pfsense = Distro("pfsense") OPNsense = Distro("opnsense") TrueNAS = Distro("truenas") + Gokrazy = Distro("gokrazy") ) +var distroAtomic atomic.Value // of Distro + // Get returns the current distro, or the empty string if unknown. func Get() Distro { - if runtime.GOOS == "linux" { - return linuxDistro() + d, ok := distroAtomic.Load().(Distro) + if ok { + return d } - if runtime.GOOS == "freebsd" { - return freebsdDistro() + switch runtime.GOOS { + case "linux": + d = linuxDistro() + case "freebsd": + d = freebsdDistro() } - return "" + distroAtomic.Store(d) // even if empty + return d } func have(file string) bool { @@ -62,6 +71,8 @@ func linuxDistro() Distro { return NixOS case have("/etc/config/uLinux.conf"): return QNAP + case haveDir("/gokrazy"): + return Gokrazy } return "" } diff --git a/version/distro/distro_test.go b/version/distro/distro_test.go new file mode 100644 index 000000000..bc4e8c5b4 --- /dev/null +++ b/version/distro/distro_test.go @@ -0,0 +1,16 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package distro + +import "testing" + +func BenchmarkGet(b *testing.B) { + b.ReportAllocs() + var d Distro + for i := 0; i < b.N; i++ { + d = Get() + } + _ = d +}