/
internal.go
84 lines (65 loc) · 2.04 KB
/
internal.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package core
import (
"encoding/base64"
"log"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
func Child(rootPath string, args []string) {
// log.Println("child", rootPath, args)
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
syscall.Sethostname([]byte("guntainer"))
syscall.Chroot(rootPath)
syscall.Chdir("/")
syscall.Mount("proc", "proc", "proc", 0, "")
defer syscall.Unmount("/proc", 0)
err := cmd.Run()
if err != nil {
log.Fatalln("error running command:", err)
}
}
// each string of cmdWithArgs must be the command and its arguments separated by \u0000
func RunMultipleNoClean(imagePath string, cmdWithArgs []string) (string, error) {
log.Printf("Hmm, running commands: %v", cmdWithArgs)
encodedCmdWithArgs := make([]string, len(cmdWithArgs))
for i, cmd := range cmdWithArgs {
encoded := base64.StdEncoding.EncodeToString([]byte(cmd))
encodedCmdWithArgs[i] = encoded
}
dname, err := RunNoClean(imagePath, encodedCmdWithArgs[0], encodedCmdWithArgs[1:], "childMultiple")
return dname, err
}
func ChildMultiple(rootPath string, args []string) {
syscall.Sethostname([]byte("guntainer"))
syscall.Chroot(rootPath)
syscall.Chdir("/")
syscall.Mount("proc", "proc", "proc", 0, "")
defer syscall.Unmount("/proc", 0)
for _, encoded := range args {
decoded, err := base64.StdEncoding.DecodeString(encoded)
cmdWithArgsString := string(decoded)
divider, err := strconv.Unquote("'\u0000'")
if err != nil {
log.Fatalf("Divider could not be unquoted. If you're seeing this error, uhhh. You shoudn't be.")
}
cmdWithArgs := strings.Split(cmdWithArgsString, divider)
if err != nil {
log.Fatalf("Could not decode command %v: %v", encoded, err)
}
log.Printf("Running command %v with args: %v", cmdWithArgs[0], cmdWithArgs[1:])
cmd := exec.Command(cmdWithArgs[0], cmdWithArgs[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatalf("Error running command %v: %v", cmdWithArgs, err)
}
}
}