2016-07-29 22:35:10 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-08-08 18:58:41 +00:00
|
|
|
"fmt"
|
2016-07-29 22:35:10 +00:00
|
|
|
"os/exec"
|
2016-08-08 18:58:41 +00:00
|
|
|
"strings"
|
2016-08-01 23:48:12 +00:00
|
|
|
"syscall"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
2016-08-01 23:48:12 +00:00
|
|
|
const PR_SET_CHILD_SUBREAPER = 36
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
func ExecCmd(name string, args ...string) (string, error) {
|
|
|
|
cmd := exec.Command(name, args...)
|
2016-08-08 18:58:41 +00:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2016-07-29 22:35:10 +00:00
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2016-08-08 18:58:41 +00:00
|
|
|
return "", fmt.Errorf("`%v %v` failed: %v (%v)", name, strings.Join(args, " "), stderr.String(), err)
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 18:58:41 +00:00
|
|
|
return stdout.String(), nil
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
2016-08-01 23:48:12 +00:00
|
|
|
|
|
|
|
// SetSubreaper sets the value i as the subreaper setting for the calling process
|
|
|
|
func SetSubreaper(i int) error {
|
|
|
|
return Prctl(PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prctl is a way to make the prctl linux syscall
|
|
|
|
func Prctl(option int, arg2, arg3, arg4, arg5 uintptr) (err error) {
|
|
|
|
_, _, e1 := syscall.Syscall6(syscall.SYS_PRCTL, uintptr(option), arg2, arg3, arg4, arg5, 0)
|
|
|
|
if e1 != 0 {
|
|
|
|
err = e1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|