2016-07-29 22:35:10 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-08-08 18:58:41 +00:00
|
|
|
"fmt"
|
2016-09-19 07:21:14 +00:00
|
|
|
"io"
|
2016-08-01 17:39:42 +00:00
|
|
|
"os"
|
2016-07-29 22:35:10 +00:00
|
|
|
"os/exec"
|
2016-08-15 22:57:13 +00:00
|
|
|
"os/signal"
|
2016-08-01 17:39:42 +00:00
|
|
|
"path/filepath"
|
2016-08-08 18:58:41 +00:00
|
|
|
"strings"
|
2016-08-01 23:48:12 +00:00
|
|
|
"syscall"
|
2016-08-15 22:57:13 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
// PRSetChildSubreaper is the value of PR_SET_CHILD_SUBREAPER in prctl(2)
|
|
|
|
const PRSetChildSubreaper = 36
|
2016-08-01 23:48:12 +00:00
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
// ExecCmd executes a command with args and returns its output as a string along
|
|
|
|
// with an error, if any
|
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
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
// ExecCmdWithStdStreams execute a command with the specified standard streams.
|
2016-09-19 07:21:14 +00:00
|
|
|
func ExecCmdWithStdStreams(stdin io.Reader, stdout, stderr io.Writer, name string, args ...string) error {
|
2016-08-01 17:39:42 +00:00
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
cmd.Stdin = stdin
|
|
|
|
cmd.Stdout = stdout
|
|
|
|
cmd.Stderr = stderr
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("`%v %v` failed: %v", name, strings.Join(args, " "), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-09-19 07:21:14 +00:00
|
|
|
return Prctl(PRSetChildSubreaper, uintptr(i), 0, 0, 0)
|
2016-08-01 23:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
|
|
|
|
// CreateFakeRootfs creates a fake rootfs for test.
|
|
|
|
func CreateFakeRootfs(dir string, image string) error {
|
|
|
|
if len(image) <= 9 || image[:9] != "docker://" {
|
|
|
|
return fmt.Errorf("CreateFakeRootfs only support docker images currently")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs := filepath.Join(dir, "rootfs")
|
|
|
|
if err := os.MkdirAll(rootfs, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// docker export $(docker create image[9:]) | tar -C rootfs -xf -
|
|
|
|
return dockerExport(image[9:], rootfs)
|
|
|
|
}
|
|
|
|
|
2016-10-02 09:11:07 +00:00
|
|
|
// CreateInfraRootfs creates a rootfs similar to CreateFakeRootfs, but only
|
|
|
|
// copies a single binary from the host into the rootfs. This is all done
|
|
|
|
// without Docker, and is only used currently for the pause container which is
|
|
|
|
// required for all sandboxes.
|
|
|
|
func CreateInfraRootfs(dir string, src string) error {
|
|
|
|
rootfs := filepath.Join(dir, "rootfs")
|
|
|
|
if err := os.MkdirAll(rootfs, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := filepath.Join(rootfs, filepath.Base(src))
|
|
|
|
logrus.Debugf("copying infra rootfs binary: %v -> %v", src, dest)
|
|
|
|
|
|
|
|
in, err := os.OpenFile(src, os.O_RDONLY, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
out, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(out, in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Sync()
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
func dockerExport(image string, rootfs string) error {
|
|
|
|
out, err := ExecCmd("docker", "create", image)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container := out[:strings.Index(out, "\n")]
|
|
|
|
|
|
|
|
cmd := fmt.Sprintf("docker export %s | tar -C %s -xf -", container, rootfs)
|
|
|
|
if _, err := ExecCmd("/bin/bash", "-c", cmd); err != nil {
|
|
|
|
err1 := dockerRemove(container)
|
|
|
|
if err1 == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%v; %v", err, err1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dockerRemove(container)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dockerRemove(container string) error {
|
2016-09-19 07:21:14 +00:00
|
|
|
_, err := ExecCmd("docker", "rm", container)
|
|
|
|
return err
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
2016-08-15 22:57:13 +00:00
|
|
|
|
|
|
|
// StartReaper starts a goroutine to reap processes
|
|
|
|
func StartReaper() {
|
|
|
|
logrus.Infof("Starting reaper")
|
|
|
|
go func() {
|
|
|
|
sigs := make(chan os.Signal, 10)
|
|
|
|
signal.Notify(sigs, syscall.SIGCHLD)
|
|
|
|
for {
|
|
|
|
// Wait for a child to terminate
|
|
|
|
sig := <-sigs
|
|
|
|
logrus.Infof("Signal received: %v", sig)
|
|
|
|
for {
|
|
|
|
// Reap processes
|
|
|
|
cpid, _ := syscall.Wait4(-1, nil, syscall.WNOHANG, nil)
|
|
|
|
if cpid < 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Reaped process with pid %d", cpid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-09-19 19:24:53 +00:00
|
|
|
|
|
|
|
// StatusToExitCode converts wait status code to an exit code
|
|
|
|
func StatusToExitCode(status int) int {
|
|
|
|
return ((status) & 0xff00) >> 8
|
|
|
|
}
|