aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/osutil
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-06-01 15:54:10 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-06-03 10:41:09 +0200
commit1a7e5fb38dee3b87db687c243038556c536386dd (patch)
tree4296ed991cbc4db64658c2ebe997bfae3a5f71cb /pkg/osutil
parent48d4af340f9b30c659aae910419aea6d666a7ae0 (diff)
pkg/kernel: add new package
Move functionality to build kernel/image from syz-gce to a separate package.
Diffstat (limited to 'pkg/osutil')
-rw-r--r--pkg/osutil/osutil.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go
new file mode 100644
index 000000000..0bd4cdfac
--- /dev/null
+++ b/pkg/osutil/osutil.go
@@ -0,0 +1,36 @@
+// Copyright 2017 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 osutil
+
+import (
+ "bytes"
+ "fmt"
+ "os/exec"
+ "time"
+)
+
+// RunCmd runs "bin args..." in dir with timeout and returns its output.
+func RunCmd(timeout time.Duration, dir, bin string, args ...string) ([]byte, error) {
+ output := new(bytes.Buffer)
+ cmd := exec.Command(bin, args...)
+ cmd.Dir = dir
+ cmd.Stdout = output
+ cmd.Stderr = output
+ if err := cmd.Start(); err != nil {
+ return nil, fmt.Errorf("failed to start %v %+v: %v", bin, args, err)
+ }
+ done := make(chan bool)
+ go func() {
+ select {
+ case <-time.After(time.Hour):
+ cmd.Process.Kill()
+ case <-done:
+ }
+ }()
+ defer close(done)
+ if err := cmd.Wait(); err != nil {
+ return nil, fmt.Errorf("failed to run %v %+v: %v\n%v", bin, args, err, output.String())
+ }
+ return output.Bytes(), nil
+}