diff --git a/pkg/host/machine_info.go b/pkg/host/machine_info.go new file mode 100644 index 00000000..7a7cd261 --- /dev/null +++ b/pkg/host/machine_info.go @@ -0,0 +1,34 @@ +// Copyright 2020 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package host + +import ( + "bytes" + "fmt" + "os" + "strings" +) + +func CollectMachineInfo() ([]byte, error) { + buf := new(bytes.Buffer) + for _, pair := range machineInfoFuncs { + fmt.Fprintf(buf, "[%s]\n", pair.name) + err := pair.fn(buf) + if err != nil { + if !os.IsNotExist(err) { + return nil, err + } + fmt.Fprintf(buf, "%v\n", err) + } + fmt.Fprintf(buf, "%v\n\n", strings.Repeat("-", 80)) + } + return buf.Bytes(), nil +} + +var machineInfoFuncs []machineInfoFunc + +type machineInfoFunc struct { + name string + fn func(*bytes.Buffer) error +} diff --git a/syz-fuzzer/machine_info.go b/pkg/host/machine_info_linux.go similarity index 78% rename from syz-fuzzer/machine_info.go rename to pkg/host/machine_info_linux.go index 040655e6..da1ee481 100644 --- a/syz-fuzzer/machine_info.go +++ b/pkg/host/machine_info_linux.go @@ -1,7 +1,7 @@ // Copyright 2020 syzkaller project authors. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. -package main +package host import ( "bufio" @@ -10,41 +10,14 @@ import ( "io/ioutil" "os" "path/filepath" - "runtime" "strings" ) -func CollectMachineInfo() ([]byte, error) { - if runtime.GOOS != "linux" { - return nil, nil - } - - type machineInfoFunc struct { - name string - fn func(*bytes.Buffer) error - } - - allMachineInfo := []machineInfoFunc{ +func init() { + machineInfoFuncs = []machineInfoFunc{ {"CPU Info", readCPUInfo}, {"KVM", readKVMInfo}, } - - buffer := new(bytes.Buffer) - - for _, pair := range allMachineInfo { - fmt.Fprintf(buffer, "[%s]\n", pair.name) - err := pair.fn(buffer) - if err != nil { - if os.IsNotExist(err) { - buffer.WriteString(err.Error() + "\n") - } else { - return nil, err - } - } - fmt.Fprintf(buffer, "-----------------------------------\n\n") - } - - return buffer.Bytes(), nil } func readCPUInfo(buffer *bytes.Buffer) error { @@ -143,7 +116,7 @@ func readKVMInfo(buffer *bytes.Buffer) error { fmt.Fprintf(buffer, "\t%s: ", keyName) buffer.Write(data) } - buffer.WriteString("\n") + buffer.WriteByte('\n') } return nil } diff --git a/syz-fuzzer/machine_info_test.go b/pkg/host/machine_info_test.go similarity index 96% rename from syz-fuzzer/machine_info_test.go rename to pkg/host/machine_info_test.go index 71633ca5..49ef93fe 100644 --- a/syz-fuzzer/machine_info_test.go +++ b/pkg/host/machine_info_test.go @@ -1,16 +1,20 @@ // Copyright 2020 syzkaller project authors. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. -package main +package host import ( "bufio" "bytes" + "runtime" "strings" "testing" ) func TestMachineInfoLinux(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip() + } result, err := CollectMachineInfo() if err != nil { t.Fatal(err) @@ -116,7 +120,7 @@ D: d scanner := bufio.NewScanner(strings.NewReader(input)) buffer := new(bytes.Buffer) scanCPUInfo(buffer, scanner) - result := bufio.NewScanner(strings.NewReader(buffer.String())) + result := bufio.NewScanner(buffer) idx := 0 for result.Scan() { diff --git a/syz-fuzzer/fuzzer.go b/syz-fuzzer/fuzzer.go index 3218d9b8..ae9a5af5 100644 --- a/syz-fuzzer/fuzzer.go +++ b/syz-fuzzer/fuzzer.go @@ -181,7 +181,7 @@ func main() { runtime.MemProfileRate = 0 } - machineInfo, err := CollectMachineInfo() + machineInfo, err := host.CollectMachineInfo() if err != nil { log.Fatalf("failed to collect machine information: %v", err) }