-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (80 loc) · 2.27 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"github.com/docker/docker/pkg/reexec"
"golang.org/x/sys/unix"
)
func init() {
fmt.Printf("init start, os.Args = %+v\n", os.Args)
reexec.Register("child", child)
if reexec.Init() {
os.Exit(0)
}
}
func main() {
fmt.Println("LiteBox Starting ...")
fmt.Printf("main start, os.Args = %+v\n", os.Args)
mainConf := HandleFlags()
if mainConf.Exec == "" {
fmt.Println("No executable provided. Put executable path as --exec=<path>")
os.Exit(1)
}
cmd := reexec.Command(append([]string{"child"}, os.Args[1:]...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID,
Pdeathsig: syscall.SIGTERM,
}
check(cmd.Run())
}
func child() {
fmt.Printf("child start, os.Args = %+v\t in PID: %d\n", os.Args, os.Getpid())
conf := HandleFlags()
// Process conf.Exec
c := strings.Split(conf.Exec, " ")
exe := c[0]
args := c[1:]
cmd := exec.Command(exe, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// To drop privileges to nobody, syscall.Setuid is broken, so this workaround
cmd.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{Uid: uint32(65534), Gid: uint32(65534)},
}
check(syscall.Sethostname([]byte("litebox")))
// getResourceLimits()
setResourceLimits(conf.CPU, conf.Memory, conf.Nproc)
getResourceLimits()
check(cmd.Run())
ShowUsage(cmd)
}
// Not using syscall.Setrlimit it's buggy ,using unix.Setrlimit
func setResourceLimits(cpu int, mem int, nproc int) {
fmt.Println("[i] Changing resource limits")
unix.Setrlimit(unix.RLIMIT_CPU, &unix.Rlimit{Cur: uint64(cpu), Max: uint64(cpu)})
unix.Setrlimit(unix.RLIMIT_AS, &unix.Rlimit{Cur: uint64(mem), Max: uint64(mem)})
unix.Setrlimit(unix.RLIMIT_NPROC, &unix.Rlimit{Cur: uint64(nproc), Max: uint64(nproc)})
}
func getResourceLimits() {
var getc unix.Rlimit
var getn unix.Rlimit
var getm unix.Rlimit
unix.Getrlimit(unix.RLIMIT_CPU, &getc)
unix.Getrlimit(unix.RLIMIT_AS, &getm)
unix.Getrlimit(unix.RLIMIT_NPROC, &getn)
fmt.Println("CPU Limit: ", getc)
fmt.Println("Memory Limit: ", getm)
fmt.Println("Process Limit: ", getn)
}
func check(err error) {
if err != nil {
panic(err)
}
}