|
| 1 | +package sup |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "os/signal" |
| 8 | + "syscall" |
| 9 | +) |
| 10 | + |
| 11 | +// Run forks the ContainerPilot process and then starts signal handlers |
| 12 | +// that will reap child processes and pass-thru SIGINT and SIGKILL to |
| 13 | +// the ContainerPilot worker process. |
| 14 | +func Run() { |
| 15 | + self, err := exec.LookPath(os.Args[0]) |
| 16 | + if err != nil { |
| 17 | + log.Fatal("failed to find ContainerPilot binary: ", err) |
| 18 | + } |
| 19 | + proc, err := os.StartProcess(self, os.Args, &os.ProcAttr{Dir: "", Env: nil, |
| 20 | + Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, Sys: nil}) |
| 21 | + if err != nil { |
| 22 | + log.Fatal("failed to start ContainerPilot worker process:", err) |
| 23 | + } |
| 24 | + handleSignals(proc.Pid) |
| 25 | + proc.Wait() |
| 26 | +} |
| 27 | + |
| 28 | +// handleSignals listens for signals used to gracefully shutdown and |
| 29 | +// passes them thru to the ContainerPilot worker process. |
| 30 | +func handleSignals(pid int) { |
| 31 | + sig := make(chan os.Signal, 1) |
| 32 | + signal.Notify(sig, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, syscall.SIGCHLD, syscall.SIGUSR1) |
| 33 | + go func() { |
| 34 | + for signal := range sig { |
| 35 | + switch signal { |
| 36 | + case syscall.SIGINT: |
| 37 | + syscall.Kill(pid, syscall.SIGINT) |
| 38 | + case syscall.SIGTERM: |
| 39 | + syscall.Kill(pid, syscall.SIGTERM) |
| 40 | + case syscall.SIGHUP: |
| 41 | + syscall.Kill(pid, syscall.SIGHUP) |
| 42 | + case syscall.SIGUSR1: |
| 43 | + syscall.Kill(pid, syscall.SIGUSR1) |
| 44 | + case syscall.SIGCHLD: |
| 45 | + go reap() |
| 46 | + } |
| 47 | + } |
| 48 | + }() |
| 49 | +} |
| 50 | + |
| 51 | +// reaps child processes that have been reparented to PID1 |
| 52 | +func reap() { |
| 53 | + for { |
| 54 | + POLL: |
| 55 | + var wstatus syscall.WaitStatus |
| 56 | + pid, err := syscall.Wait4(-1, &wstatus, 0, nil) |
| 57 | + switch err { |
| 58 | + case nil: |
| 59 | + if pid > 0 { |
| 60 | + goto POLL |
| 61 | + } |
| 62 | + return |
| 63 | + case syscall.ECHILD: |
| 64 | + return // no more children, we're done till next signal |
| 65 | + case syscall.EINTR: |
| 66 | + goto POLL |
| 67 | + default: |
| 68 | + return |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments