github.com/containerd/Containerd@v1.4.13/runtime/v2/runc/util.go (about)

     1  // +build linux
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package runc
    20  
    21  import (
    22  	"context"
    23  	"encoding/json"
    24  	"io/ioutil"
    25  	"path/filepath"
    26  
    27  	"github.com/containerd/containerd/api/events"
    28  	"github.com/containerd/containerd/log"
    29  	"github.com/containerd/containerd/runtime"
    30  	specs "github.com/opencontainers/runtime-spec/specs-go"
    31  	"github.com/sirupsen/logrus"
    32  )
    33  
    34  // GetTopic converts an event from an interface type to the specific
    35  // event topic id
    36  func GetTopic(e interface{}) string {
    37  	switch e.(type) {
    38  	case *events.TaskCreate:
    39  		return runtime.TaskCreateEventTopic
    40  	case *events.TaskStart:
    41  		return runtime.TaskStartEventTopic
    42  	case *events.TaskOOM:
    43  		return runtime.TaskOOMEventTopic
    44  	case *events.TaskExit:
    45  		return runtime.TaskExitEventTopic
    46  	case *events.TaskDelete:
    47  		return runtime.TaskDeleteEventTopic
    48  	case *events.TaskExecAdded:
    49  		return runtime.TaskExecAddedEventTopic
    50  	case *events.TaskExecStarted:
    51  		return runtime.TaskExecStartedEventTopic
    52  	case *events.TaskPaused:
    53  		return runtime.TaskPausedEventTopic
    54  	case *events.TaskResumed:
    55  		return runtime.TaskResumedEventTopic
    56  	case *events.TaskCheckpointed:
    57  		return runtime.TaskCheckpointedEventTopic
    58  	default:
    59  		logrus.Warnf("no topic for type %#v", e)
    60  	}
    61  	return runtime.TaskUnknownTopic
    62  }
    63  
    64  // ShouldKillAllOnExit reads the bundle's OCI spec and returns true if
    65  // there is an error reading the spec or if the container has a private PID namespace
    66  func ShouldKillAllOnExit(ctx context.Context, bundlePath string) bool {
    67  	var bundleSpec specs.Spec
    68  	bundleConfigContents, err := ioutil.ReadFile(filepath.Join(bundlePath, "config.json"))
    69  	if err != nil {
    70  		log.G(ctx).WithError(err).Error("shouldKillAllOnExit: failed to read config.json")
    71  		return true
    72  	}
    73  	if err := json.Unmarshal(bundleConfigContents, &bundleSpec); err != nil {
    74  		log.G(ctx).WithError(err).Error("shouldKillAllOnExit: failed to unmarshal bundle json")
    75  		return true
    76  	}
    77  	if bundleSpec.Linux != nil {
    78  		for _, ns := range bundleSpec.Linux.Namespaces {
    79  			if ns.Type == specs.PIDNamespace && ns.Path == "" {
    80  				return false
    81  			}
    82  		}
    83  	}
    84  	return true
    85  }