some bug fixes
This commit is contained in:
parent
0e49d60994
commit
0e914c629e
16 changed files with 179 additions and 52 deletions
39
pkg/scan/timeout.go
Normal file
39
pkg/scan/timeout.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package scan
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func timeout(cmd *exec.Cmd, t time.Duration) (output []byte, err error) {
|
||||
done := make(chan error)
|
||||
out := make(chan []byte)
|
||||
go func() {
|
||||
o, e := cmd.Output()
|
||||
done <- e
|
||||
out <- o
|
||||
}()
|
||||
select {
|
||||
case <-time.After(t):
|
||||
e := cmd.Process.Kill()
|
||||
err = <-done
|
||||
<-out
|
||||
if e != nil {
|
||||
err = e
|
||||
} else {
|
||||
err = fmt.Errorf("command '%s' timed out: %s", cmd.Path, err)
|
||||
}
|
||||
case e := <-done:
|
||||
exerr, ok := e.(*exec.ExitError)
|
||||
if ok {
|
||||
err = fmt.Errorf("%s\n%s", e, string(exerr.Stderr))
|
||||
} else {
|
||||
err = e
|
||||
}
|
||||
output = <-out
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue