-
Notifications
You must be signed in to change notification settings - Fork 136
/
sup.go
71 lines (67 loc) · 1.66 KB
/
sup.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
package sup
import (
"log"
"os"
"os/exec"
"os/signal"
"syscall"
)
// Run forks the ContainerPilot process and then starts signal handlers
// that will reap child processes and pass-thru SIGINT and SIGKILL to
// the ContainerPilot worker process.
func Run() {
self, err := exec.LookPath(os.Args[0])
if err != nil {
log.Fatal("failed to find ContainerPilot binary: ", err)
}
proc, err := os.StartProcess(self, os.Args, &os.ProcAttr{Dir: "", Env: nil,
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, Sys: nil})
if err != nil {
log.Fatal("failed to start ContainerPilot worker process:", err)
}
handleSignals(proc.Pid)
proc.Wait()
}
// handleSignals listens for signals used to gracefully shutdown and
// passes them thru to the ContainerPilot worker process.
func handleSignals(pid int) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, syscall.SIGCHLD, syscall.SIGUSR1)
go func() {
for signal := range sig {
switch signal {
case syscall.SIGINT:
syscall.Kill(pid, syscall.SIGINT)
case syscall.SIGTERM:
syscall.Kill(pid, syscall.SIGTERM)
case syscall.SIGHUP:
syscall.Kill(pid, syscall.SIGHUP)
case syscall.SIGUSR1:
syscall.Kill(pid, syscall.SIGUSR1)
case syscall.SIGCHLD:
go reap()
}
}
}()
}
// reaps child processes that have been reparented to PID1
func reap() {
for {
POLL:
var wstatus syscall.WaitStatus
pid, err := syscall.Wait4(-1, &wstatus, 0, nil)
switch err {
case nil:
if pid > 0 {
goto POLL
}
return
case syscall.ECHILD:
return // no more children, we're done till next signal
case syscall.EINTR:
goto POLL
default:
return
}
}
}