hostinfo_freebsd.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build freebsd
  5. // +build freebsd
  6. package hostinfo
  7. import (
  8. "bytes"
  9. "os"
  10. "os/exec"
  11. "golang.org/x/sys/unix"
  12. "tailscale.com/version/distro"
  13. )
  14. func init() {
  15. osVersion = lazyOSVersion.Get
  16. distroName = distroNameFreeBSD
  17. distroVersion = distroVersionFreeBSD
  18. }
  19. var (
  20. lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptrTo(freebsdVersionMeta)}
  21. lazyOSVersion = &lazyAtomicValue[string]{f: ptrTo(osVersionFreeBSD)}
  22. )
  23. func distroNameFreeBSD() string {
  24. return lazyVersionMeta.Get().DistroName
  25. }
  26. func distroVersionFreeBSD() string {
  27. return lazyVersionMeta.Get().DistroVersion
  28. }
  29. type versionMeta struct {
  30. DistroName string
  31. DistroVersion string
  32. DistroCodeName string
  33. }
  34. func osVersionFreeBSD() string {
  35. var un unix.Utsname
  36. unix.Uname(&un)
  37. return unix.ByteSliceToString(un.Release[:])
  38. }
  39. func freebsdVersionMeta() (meta versionMeta) {
  40. d := distro.Get()
  41. meta.DistroName = string(d)
  42. switch d {
  43. case distro.Pfsense:
  44. b, _ := os.ReadFile("/etc/version")
  45. meta.DistroVersion = string(bytes.TrimSpace(b))
  46. case distro.OPNsense:
  47. b, _ := exec.Command("opnsense-version").Output()
  48. meta.DistroVersion = string(bytes.TrimSpace(b))
  49. case distro.TrueNAS:
  50. b, _ := os.ReadFile("/etc/version")
  51. meta.DistroVersion = string(bytes.TrimSpace(b))
  52. }
  53. return
  54. }