Skip to content

Commit

Permalink
Eliminate call to uname on FreeBSD
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevenh committed Mar 11, 2018
1 parent 5776ff9 commit f846eda
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
19 changes: 5 additions & 14 deletions host/host_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/binary"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -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)))
}

out, err = invoke.Command(uname, "-r")
if err == nil {
version = strings.ToLower(strings.TrimSpace(string(out)))
version, err := unix.Sysctl("kern.osrelease")
if err != nil {
return "", "", "", err
}

return platform, family, version, nil
return strings.ToLower(platform), "", strings.ToLower(version), nil
}

func Virtualization() (string, string, error) {
Expand Down
12 changes: 12 additions & 0 deletions host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f846eda

Please sign in to comment.