forked from cloudfoundry/guardian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stopper_linux.go
68 lines (53 loc) · 1.41 KB
/
stopper_linux.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
package stopper
import (
"fmt"
"syscall"
"code.cloudfoundry.org/lager"
"github.com/opencontainers/runc/libcontainer/cgroups"
)
func (stopper *CgroupStopper) StopAll(log lager.Logger, cgroupName string, exceptions []int, kill bool) error {
log = log.Session("stop-all", lager.Data{
"name": cgroupName,
})
log.Debug("start")
defer log.Debug("finished")
devicesSubsystemPath, err := stopper.cgroupPathResolver.Resolve(cgroupName, "devices")
if err != nil {
return err
}
if !kill {
stopper.retrier.Run(func() error {
return stopper.killAllRemaining(syscall.SIGTERM, devicesSubsystemPath, exceptions)
})
}
stopper.retrier.Run(func() error {
return stopper.killAllRemaining(syscall.SIGKILL, devicesSubsystemPath, exceptions)
})
return nil // we killed, so everything must die
}
func (stopper *CgroupStopper) killAllRemaining(signal syscall.Signal, cgroupPath string, exceptions []int) error {
pidsInCgroup, err := cgroups.GetAllPids(cgroupPath)
if err != nil {
return err
}
var pidsToKill []int
for _, pid := range pidsInCgroup {
if contains(exceptions, pid) {
continue
}
pidsToKill = append(pidsToKill, pid)
}
if len(pidsToKill) == 0 {
return nil
}
stopper.killer.Kill(signal, pidsToKill...)
return fmt.Errorf("still running after signal %s, %v", signal, pidsToKill)
}
func contains(a []int, b int) bool {
for _, i := range a {
if i == b {
return true
}
}
return false
}