k3s/vendor/github.com/rootless-containers/rootlesskit/pkg/common/exec.go
Akihiro Suda 11ef43011a bump up RootlessKit
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2021-03-24 00:37:30 -07:00

55 lines
968 B
Go

package common
import (
"io"
"os/exec"
"syscall"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// ErrorWithSys is implemented by *exec.ExitError and *child.reaperErr
type ErrorWithSys interface {
error
Sys() interface{}
}
func GetExecExitStatus(err error) (int, bool) {
err = errors.Cause(err)
if err == nil {
return 0, false
}
exitErr, ok := err.(ErrorWithSys)
if !ok {
return 0, false
}
status, ok := exitErr.Sys().(syscall.WaitStatus)
if !ok {
return 0, false
}
return status.ExitStatus(), true
}
func Execs(o io.Writer, env []string, cmds [][]string) error {
for _, cmd := range cmds {
var args []string
if len(cmd) > 1 {
args = cmd[1:]
}
x := exec.Command(cmd[0], args...)
x.Stdin = nil
x.Stdout = o
x.Stderr = o
x.Env = env
x.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
logrus.Debugf("executing %v", cmd)
if err := x.Run(); err != nil {
return err
}
}
return nil
}