linux/shim: Kill container upon SIG{TERM,KILL}

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2017-08-14 14:23:44 -07:00
parent 6ec92ddbc9
commit 9923a49e97
No known key found for this signature in database
GPG Key ID: 40CF16616B361216

View File

@ -9,6 +9,8 @@ import (
"os" "os"
"runtime" "runtime"
"strings" "strings"
"sync"
"syscall"
"time" "time"
"github.com/containerd/containerd" "github.com/containerd/containerd"
@ -20,6 +22,7 @@ import (
"github.com/containerd/containerd/reaper" "github.com/containerd/containerd/reaper"
"github.com/containerd/containerd/typeurl" "github.com/containerd/containerd/typeurl"
"github.com/containerd/containerd/version" "github.com/containerd/containerd/version"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -101,7 +104,7 @@ func main() {
if err := serve(server, socket); err != nil { if err := serve(server, socket); err != nil {
return err return err
} }
return handleSignals(signals, server) return handleSignals(signals, server, sv)
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "containerd-shim: %s\n", err) fmt.Fprintf(os.Stderr, "containerd-shim: %s\n", err)
@ -136,24 +139,38 @@ func serve(server *grpc.Server, path string) error {
return nil return nil
} }
func handleSignals(signals chan os.Signal, server *grpc.Server) error { func handleSignals(signals chan os.Signal, server *grpc.Server, sv *shim.Service) error {
for s := range signals { var (
logrus.WithField("signal", s).Debug("received signal") termOnce sync.Once
done = make(chan struct{})
)
for {
select {
case <-done:
return nil
case s := <-signals:
switch s { switch s {
case unix.SIGCHLD: case unix.SIGCHLD:
if err := reaper.Reap(); err != nil { if err := reaper.Reap(); err != nil {
logrus.WithError(err).Error("reap exit status") logrus.WithError(err).Error("reap exit status")
} }
case unix.SIGTERM, unix.SIGINT: case unix.SIGTERM, unix.SIGINT:
// TODO: should we forward signals to the processes if they are still running? go termOnce.Do(func() {
// i.e. machine reboot
server.Stop() server.Stop()
return nil // Ensure our child is dead if any
sv.Kill(context.Background(), &shimapi.KillRequest{
Signal: uint32(syscall.SIGKILL),
All: true,
})
sv.Delete(context.Background(), &google_protobuf.Empty{})
close(done)
})
case unix.SIGUSR1: case unix.SIGUSR1:
dumpStacks() dumpStacks()
} }
} }
return nil }
} }
func dumpStacks() { func dumpStacks() {