mirror of
https://github.com/reactos/syzkaller.git
synced 2024-11-23 03:19:51 +00:00
pkg/host: move machine info functionality from syz-fuzzer
It's better to keep functionality in packages rather than in main. It makes it reusable and better organized. Move machine info functionality to pkg/host and do some cosmetic refactoring.
This commit is contained in:
parent
7aa6bd6859
commit
01622de2d0
34
pkg/host/machine_info.go
Normal file
34
pkg/host/machine_info.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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() {
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user