@@ -20,6 +20,7 @@ package runc
2020
2121import (
2222 "context"
23+ "encoding/json"
2324 "io/ioutil"
2425 "os"
2526 "os/exec"
@@ -34,6 +35,7 @@ import (
3435 "github.com/containerd/containerd/api/types/task"
3536 "github.com/containerd/containerd/errdefs"
3637 "github.com/containerd/containerd/events"
38+ "github.com/containerd/containerd/log"
3739 "github.com/containerd/containerd/mount"
3840 "github.com/containerd/containerd/namespaces"
3941 "github.com/containerd/containerd/runtime"
@@ -45,6 +47,7 @@ import (
4547 runcC "github.com/containerd/go-runc"
4648 "github.com/containerd/typeurl"
4749 ptypes "github.com/gogo/protobuf/types"
50+ specs "github.com/opencontainers/runtime-spec/specs-go"
4851 "github.com/pkg/errors"
4952 "github.com/sirupsen/logrus"
5053 "golang.org/x/sys/unix"
@@ -638,13 +641,20 @@ func (s *service) processExits() {
638641}
639642
640643func (s * service ) checkProcesses (e runcC.Exit ) {
644+ shouldKillAll , err := shouldKillAllOnExit (s .bundle )
645+ if err != nil {
646+ log .G (s .context ).WithError (err ).Error ("failed to check shouldKillAll" )
647+ }
648+
641649 for _ , p := range s .allProcesses () {
642650 if p .Pid () == e .Pid {
643- if ip , ok := p .(* proc.Init ); ok {
644- // Ensure all children are killed
645- if err := ip .KillAll (s .context ); err != nil {
646- logrus .WithError (err ).WithField ("id" , ip .ID ()).
647- Error ("failed to kill init's children" )
651+ if shouldKillAll {
652+ if ip , ok := p .(* proc.Init ); ok {
653+ // Ensure all children are killed
654+ if err := ip .KillAll (s .context ); err != nil {
655+ logrus .WithError (err ).WithField ("id" , ip .ID ()).
656+ Error ("failed to kill init's children" )
657+ }
648658 }
649659 }
650660 p .SetExited (e .Status )
@@ -660,6 +670,25 @@ func (s *service) checkProcesses(e runcC.Exit) {
660670 }
661671}
662672
673+ func shouldKillAllOnExit (bundlePath string ) (bool , error ) {
674+ var bundleSpec specs.Spec
675+ bundleConfigContents , err := ioutil .ReadFile (filepath .Join (bundlePath , "config.json" ))
676+ if err != nil {
677+ return false , err
678+ }
679+ json .Unmarshal (bundleConfigContents , & bundleSpec )
680+
681+ if bundleSpec .Linux != nil {
682+ for _ , ns := range bundleSpec .Linux .Namespaces {
683+ if ns .Type == specs .PIDNamespace {
684+ return false , nil
685+ }
686+ }
687+ }
688+
689+ return true , nil
690+ }
691+
663692func (s * service ) allProcesses () (o []rproc.Process ) {
664693 s .mu .Lock ()
665694 defer s .mu .Unlock ()
0 commit comments