From f846eda92320c2e39d831c90d68b3ad06d5ebcb0 Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Sun, 11 Mar 2018 23:08:04 +0000 Subject: [PATCH] Eliminate call to uname on FreeBSD Improve performance by eliminating the fork out to uname on FreeBSD which also helps prevent crashes / hangs due to the outstanding fork crash bug: golang/go#15658 Also added a test for PlatformInformation. --- host/host_freebsd.go | 19 +++++-------------- host/host_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/host/host_freebsd.go b/host/host_freebsd.go index 2f51c31..00a8519 100644 --- a/host/host_freebsd.go +++ b/host/host_freebsd.go @@ -8,7 +8,6 @@ import ( "encoding/binary" "io/ioutil" "os" - "os/exec" "runtime" "strings" "sync/atomic" @@ -168,25 +167,17 @@ func PlatformInformation() (string, string, string, error) { } func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { - platform := "" - family := "" - version := "" - uname, err := exec.LookPath("uname") + platform, err := unix.Sysctl("kern.ostype") if err != nil { return "", "", "", err } - out, err := invoke.Command(uname, "-s") - if err == nil { - platform = strings.ToLower(strings.TrimSpace(string(out))) + version, err := unix.Sysctl("kern.osrelease") + if err != nil { + return "", "", "", err } - out, err = invoke.Command(uname, "-r") - if err == nil { - version = strings.ToLower(strings.TrimSpace(string(out))) - } - - return platform, family, version, nil + return strings.ToLower(platform), "", strings.ToLower(version), nil } func Virtualization() (string, string, error) { diff --git a/host/host_test.go b/host/host_test.go index b4f3e40..e6abb78 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -148,3 +148,15 @@ func TestKernelVersion(t *testing.T) { t.Logf("KernelVersion(): %s", version) } + +func TestPlatformInformation(t *testing.T) { + platform, family, version, err := PlatformInformation() + if err != nil { + t.Errorf("PlatformInformation() failed, %v", err) + } + if platform == "" { + t.Errorf("PlatformInformation() retuns empty: %v", platform) + } + + t.Logf("PlatformInformation(): %v, %v, %v", platform, family, version) +}