From 0e1503aea296e419ec219e36c56edb68f1abaf0f Mon Sep 17 00:00:00 2001
From: jingrui <jingrui@huawei.com>
Date: Tue, 18 Jun 2019 00:12:41 +0800
Subject: [PATCH] containerd: support kill D state container

Change-Id: I80a1c0c4f88530fe9732e6e9a2d1fb222ece118c
Signed-off-by: jingrui <jingrui@huawei.com>
---
 runtime/v1/shim/service.go | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/runtime/v1/shim/service.go b/runtime/v1/shim/service.go
index 32431a4..a3b4a8f 100644
--- a/runtime/v1/shim/service.go
+++ b/runtime/v1/shim/service.go
@@ -26,6 +26,7 @@ import (
 	"os"
 	"path/filepath"
 	"sync"
+	"syscall"
 	"time"
 
 	"github.com/containerd/console"
@@ -47,6 +48,7 @@ import (
 	ptypes "github.com/gogo/protobuf/types"
 	specs "github.com/opencontainers/runtime-spec/specs-go"
 	"github.com/sirupsen/logrus"
+	"golang.org/x/sys/unix"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
 )
@@ -375,11 +377,33 @@ func (s *Service) Resume(ctx context.Context, r *ptypes.Empty) (*ptypes.Empty, e
 
 // Kill a process with the provided signal
 func (s *Service) Kill(ctx context.Context, r *shimapi.KillRequest) (*ptypes.Empty, error) {
+	delayKill := func(p process.Process) {
+		if s.id != p.ID() || r.Signal != uint32(syscall.SIGKILL) {
+			return
+		}
+		for i := 1; i < 5; i++ {
+			time.Sleep(10 * time.Second)
+			err := p.Kill(ctx, r.Signal, r.All)
+			logrus.Infof("delay kill %s retry %d error=%v", s.id, i, err)
+			if err != nil {
+				err := unix.Kill(p.Pid(), syscall.SIGKILL)
+				logrus.Infof("delay kill-direct %s retry %d error=%v", s.id, i, err)
+			}
+		}
+
+		logrus.Infof("force exit shim %s ...", s.id)
+		p.SetExited(137)
+		err := p.Delete(ctx)
+		logrus.Infof("force exit shim %s error=%v", s.id, err)
+		os.Exit(0)
+	}
+
 	if r.ID == "" {
 		p, err := s.getInitProcess()
 		if err != nil {
 			return nil, err
 		}
+		go delayKill(p)
 		if err := p.Kill(ctx, r.Signal, r.All); err != nil {
 			return nil, errdefs.ToGRPC(err)
 		}
-- 
2.33.0

