Simplify Utsname string conversion

Use Utsname from golang.org/x/sys/unix which contains byte array
instead of int8/uint8 array members. This allows to simplify the string
conversions of these members and the marshal.FromUtsname functions are
no longer needed.
This commit is contained in:
Tobias Klauser
2017-11-01 09:54:58 +01:00
parent ac6a629aa2
commit 89f3ce2e64
6 changed files with 18 additions and 59 deletions

View File

@@ -1,33 +1,34 @@
package host
import (
"bytes"
"fmt"
"io/ioutil"
"strconv"
"strings"
"syscall"
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
"github.com/weaveworks/scope/common/marshal"
"github.com/weaveworks/scope/report"
"golang.org/x/sys/unix"
)
const kb = 1024
// Uname is swappable for mocking in tests.
var Uname = syscall.Uname
var Uname = unix.Uname
// GetKernelReleaseAndVersion returns the kernel version as reported by uname.
var GetKernelReleaseAndVersion = func() (string, string, error) {
var utsname syscall.Utsname
var utsname unix.Utsname
if err := Uname(&utsname); err != nil {
return "unknown", "unknown", err
}
release := marshal.FromUtsname(utsname.Release)
version := marshal.FromUtsname(utsname.Version)
return release, version, nil
release := utsname.Release[:bytes.IndexByte(utsname.Release[:], 0)]
version := utsname.Version[:bytes.IndexByte(utsname.Version[:], 0)]
return string(release), string(version), nil
}
// GetLoad returns the current load averages as metrics.

View File

@@ -2,10 +2,11 @@ package host_test
import (
"fmt"
"syscall"
"testing"
"github.com/weaveworks/scope/probe/host"
"golang.org/x/sys/unix"
)
func TestUname(t *testing.T) {
@@ -16,9 +17,9 @@ func TestUname(t *testing.T) {
release = "rls"
version = "ver"
)
host.Uname = func(uts *syscall.Utsname) error {
uts.Release = string2c(release)
uts.Version = string2c(version)
host.Uname = func(uts *unix.Utsname) error {
copy(uts.Release[:], []byte(release))
copy(uts.Version[:], []byte(version))
return nil
}
@@ -31,11 +32,3 @@ func TestUname(t *testing.T) {
t.Errorf("want %q, have %q", want, have)
}
}
func string2c(s string) [65]int8 {
var result [65]int8
for i, c := range s {
result[i] = int8(c)
}
return result
}