From e6e35dba937599d098fc034eff2686e5ddc409e9 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 26 Oct 2020 10:51:06 +0100 Subject: sys/targets: add OS/Arch name consts We use strings to identify OS/Arch. These strings are duplicated throughout the code base massively. golangci-lint points to possiblity of typos and duplication. We already had to define these names in pkg/csource and disable checking for prog package. A future change triggers such warnings in another package. Add OS/Arch name consts to sys/targets so that they can be used to refer to OS/Arch. Use the consts everywhere. --- vm/gce/gce.go | 9 +++++---- vm/qemu/qemu.go | 2 +- vm/vm_test.go | 7 ++++--- vm/vmimpl/util.go | 3 ++- 4 files changed, 12 insertions(+), 9 deletions(-) (limited to 'vm') diff --git a/vm/gce/gce.go b/vm/gce/gce.go index 7bc049cb2..e3f23ccee 100644 --- a/vm/gce/gce.go +++ b/vm/gce/gce.go @@ -28,6 +28,7 @@ import ( "github.com/google/syzkaller/pkg/kd" "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/pkg/osutil" + "github.com/google/syzkaller/sys/targets" "github.com/google/syzkaller/vm/vmimpl" ) @@ -248,7 +249,7 @@ func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command strin } merger := vmimpl.NewOutputMerger(tee) var decoder func(data []byte) (int, int, []byte) - if inst.env.OS == "windows" { + if inst.env.OS == targets.Windows { decoder = kd.Decode } merger.AddDecoder("console", conRpipe, decoder) @@ -264,7 +265,7 @@ func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command strin sshRpipe.Close() return nil, nil, err } - if inst.env.OS == "linux" { + if inst.env.OS == targets.Linux { if inst.sshUser != "root" { command = fmt.Sprintf("sudo bash -c '%v'", command) } @@ -369,10 +370,10 @@ func waitForConsoleConnect(merger *vmimpl.OutputMerger) error { } func (inst *instance) Diagnose() ([]byte, bool) { - if inst.env.OS == "freebsd" { + if inst.env.OS == targets.FreeBSD { return vmimpl.DiagnoseFreeBSD(inst.consolew) } - if inst.env.OS == "openbsd" { + if inst.env.OS == targets.OpenBSD { return vmimpl.DiagnoseOpenBSD(inst.consolew) } return nil, false diff --git a/vm/qemu/qemu.go b/vm/qemu/qemu.go index 222dc4d8a..15b8e0854 100644 --- a/vm/qemu/qemu.go +++ b/vm/qemu/qemu.go @@ -259,7 +259,7 @@ func ctor(env *vmimpl.Env) (vmimpl.Pool, error) { return nil, err } if env.Image == "9p" { - if env.OS != "linux" { + if env.OS != targets.Linux { return nil, fmt.Errorf("9p image is supported for linux only") } if cfg.Kernel == "" { diff --git a/vm/vm_test.go b/vm/vm_test.go index 8c1f82917..31dd65045 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -13,6 +13,7 @@ import ( "github.com/google/syzkaller/pkg/mgrconfig" "github.com/google/syzkaller/pkg/report" + "github.com/google/syzkaller/sys/targets" "github.com/google/syzkaller/vm/vmimpl" ) @@ -340,9 +341,9 @@ func testMonitorExecution(t *testing.T, test *Test) { defer os.RemoveAll(dir) cfg := &mgrconfig.Config{ Workdir: dir, - TargetOS: "linux", - TargetArch: "amd64", - TargetVMArch: "amd64", + TargetOS: targets.Linux, + TargetArch: targets.AMD64, + TargetVMArch: targets.AMD64, Type: "test", } pool, err := Create(cfg, false) diff --git a/vm/vmimpl/util.go b/vm/vmimpl/util.go index 418ae2d66..47340ccde 100644 --- a/vm/vmimpl/util.go +++ b/vm/vmimpl/util.go @@ -9,6 +9,7 @@ import ( "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/pkg/osutil" + "github.com/google/syzkaller/sys/targets" ) // Sleep for d. @@ -24,7 +25,7 @@ func SleepInterruptible(d time.Duration) bool { func WaitForSSH(debug bool, timeout time.Duration, addr, sshKey, sshUser, OS string, port int, stop chan error) error { pwd := "pwd" - if OS == "windows" { + if OS == targets.Windows { pwd = "dir" } startTime := time.Now() -- cgit mrf-deployment