-
Notifications
You must be signed in to change notification settings - Fork 231
/
reexec.go
78 lines (69 loc) · 1.94 KB
/
reexec.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
package main
import (
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/sirupsen/logrus"
)
func reexec() {
// TODO(jessfraz): This is a hack to re-exec our selves and wait for the
// process since it was not exiting correctly with the constructor.
if len(os.Getenv("IMG_RUNNING_TESTS")) <= 0 && len(os.Getenv("IMG_DO_UNSHARE")) <= 0 && system.GetParentNSeuid() != 0 {
var (
pgid int
err error
)
// On ^C, or SIGTERM handle exit.
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range c {
logrus.Infof("Received %s, exiting.", sig.String())
if err := syscall.Kill(-pgid, syscall.SIGKILL); err != nil {
logrus.Fatalf("syscall.Kill %d error: %v", pgid, err)
continue
}
os.Exit(0)
}
}()
// If newuidmap is not present re-exec will fail
if _, err := exec.LookPath("newuidmap"); err != nil {
logrus.Fatalf("newuidmap not found (install uidmap package?): %v", err)
}
// Initialize and re-exec with our unshare.
cmd := exec.Command("/proc/self/exe", os.Args[1:]...)
cmd.Env = append(os.Environ(), "IMG_DO_UNSHARE=1")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if err := cmd.Start(); err != nil {
logrus.Fatalf("cmd.Start error: %v", err)
}
pgid, err = syscall.Getpgid(cmd.Process.Pid)
if err != nil {
logrus.Fatalf("getpgid error: %v", err)
}
var (
ws syscall.WaitStatus
exitCode int
)
for {
// Store the exitCode before calling wait so we get the real one.
exitCode = ws.ExitStatus()
_, err := syscall.Wait4(-pgid, &ws, syscall.WNOHANG, nil)
if err != nil {
if err.Error() == "no child processes" {
// We exited. We need to pass the correct error code from
// the child.
os.Exit(exitCode)
}
logrus.Fatalf("wait4 error: %v", err)
}
}
}
}