|
| 1 | +package mgr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + "strings" |
| 8 | + "syscall" |
| 9 | +) |
| 10 | + |
| 11 | +type errNoSuchProcess struct { |
| 12 | + pid int64 |
| 13 | + signal int |
| 14 | +} |
| 15 | + |
| 16 | +func isErrNoSuchProcess(err error) bool { |
| 17 | + _, ok := err.(errNoSuchProcess) |
| 18 | + return ok |
| 19 | +} |
| 20 | + |
| 21 | +func (e errNoSuchProcess) Error() string { |
| 22 | + return fmt.Sprintf("Cannot kill process (pid=%d) with signal %d: no such process.", e.pid, e.signal) |
| 23 | +} |
| 24 | + |
| 25 | +// Kill one or more running containers |
| 26 | +func (mgr *ContainerManager) Kill(ctx context.Context, name string, signal uint64) (err error) { |
| 27 | + c, err := mgr.container(name) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + if signal == 0 || syscall.Signal(signal) == syscall.SIGKILL { |
| 33 | + return mgr.kill(ctx, c) |
| 34 | + } |
| 35 | + return mgr.killWithSignal(ctx, c, int(signal)) |
| 36 | +} |
| 37 | + |
| 38 | +func (mgr *ContainerManager) kill(ctx context.Context, c *Container) error { |
| 39 | + if !c.IsRunning() { |
| 40 | + return fmt.Errorf("Container %s is not running", c.ID) |
| 41 | + } |
| 42 | + |
| 43 | + if err := mgr.killDeadProcess(ctx, c, int(syscall.SIGKILL)); err != nil { |
| 44 | + if isErrNoSuchProcess(err) { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + if c.IsRunning() { |
| 49 | + return err |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if pid := c.State.Pid; pid != 0 { |
| 54 | + if err := syscall.Kill(int(pid), 9); err != nil { |
| 55 | + if err != syscall.ESRCH { |
| 56 | + return err |
| 57 | + } |
| 58 | + e := errNoSuchProcess{pid, 9} |
| 59 | + logrus.Debug(e) |
| 60 | + return nil |
| 61 | + } |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (mgr *ContainerManager) killDeadProcess(ctx context.Context, c *Container, signal int) error { |
| 67 | + err := mgr.killWithSignal(ctx, c, signal) |
| 68 | + if err == syscall.ESRCH { |
| 69 | + e := errNoSuchProcess{c.State.Pid, signal} |
| 70 | + logrus.Debug(e) |
| 71 | + return e |
| 72 | + } |
| 73 | + return err |
| 74 | +} |
| 75 | + |
| 76 | +func (mgr *ContainerManager) killWithSignal(ctx context.Context, c *Container, signal int) error { |
| 77 | + logrus.Debugf("Sending %d to %s", signal, c.ID) |
| 78 | + c.Lock() |
| 79 | + defer c.Unlock() |
| 80 | + |
| 81 | + if c.State.Paused { |
| 82 | + return fmt.Errorf("Container %s is paused. Unpause the container before stopping", c.ID) |
| 83 | + } |
| 84 | + |
| 85 | + if !c.State.Running { |
| 86 | + return fmt.Errorf("Container %s is not running", c.ID) |
| 87 | + } |
| 88 | + |
| 89 | + if err := mgr.Client.KillContainer(ctx, c.ID, signal); err != nil { |
| 90 | + err = fmt.Errorf("Cannot kill container %s: %s", c.ID, err) |
| 91 | + // if container or process not exists, ignore the error |
| 92 | + if strings.Contains(err.Error(), "container not found") || |
| 93 | + strings.Contains(err.Error(), "no such process") { |
| 94 | + logrus.Warnf("container kill failed because of 'container not found' or 'no such process': %s", err.Error()) |
| 95 | + } else { |
| 96 | + return err |
| 97 | + } |
| 98 | + } |
| 99 | + attributes := map[string]string{ |
| 100 | + "signal": fmt.Sprintf("%d", signal), |
| 101 | + } |
| 102 | + mgr.LogContainerEventWithAttributes(ctx, c, "kill", attributes) |
| 103 | + return nil |
| 104 | +} |
0 commit comments