aboutsummaryrefslogtreecommitdiff
path: root/app/main.go
blob: 60585b4dc5b89111be81105655014d1e09dd3833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
)

func main() {
	command := os.Args[3]
	args := os.Args[4:len(os.Args)]
	tmpDir := "/tmp/mydocker"

	cmd := exec.Command(command, args...)

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	_ = os.Mkdir(tmpDir, 0755)

	err := exec.Command("mkdir", "-p", filepath.Join(tmpDir, filepath.Dir(command))).Run()
	if err != nil {
		fmt.Printf("Err: %v", err)
		os.Exit(1)
	}

	err = exec.Command("cp", command, filepath.Join(tmpDir, command)).Run()
	if err != nil {
		fmt.Printf("Err: %v", err)
		os.Exit(1)
	}

	cmd.SysProcAttr = &syscall.SysProcAttr{
		Chroot:     tmpDir,
		Cloneflags: syscall.CLONE_NEWPID,
	}

	err = cmd.Run()
	if err != nil {
		if exitError, ok := err.(*exec.ExitError); ok {
			os.Exit(exitError.ExitCode())
		} else {
			fmt.Printf("Err: %v", err)
			os.Exit(1)
		}
	}
}