github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/signals.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package containerd
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"syscall"
    24  
    25  	"github.com/containerd/containerd/content"
    26  	"github.com/containerd/containerd/images"
    27  	v1 "github.com/opencontainers/image-spec/specs-go/v1"
    28  )
    29  
    30  // StopSignalLabel is a well-known containerd label for storing the stop
    31  // signal specified in the OCI image config
    32  const StopSignalLabel = "io.containerd.image.config.stop-signal"
    33  
    34  // GetStopSignal retrieves the container stop signal, specified by the
    35  // well-known containerd label (StopSignalLabel)
    36  func GetStopSignal(ctx context.Context, container Container, defaultSignal syscall.Signal) (syscall.Signal, error) {
    37  	labels, err := container.Labels(ctx)
    38  	if err != nil {
    39  		return -1, err
    40  	}
    41  
    42  	if stopSignal, ok := labels[StopSignalLabel]; ok {
    43  		return ParseSignal(stopSignal)
    44  	}
    45  
    46  	return defaultSignal, nil
    47  }
    48  
    49  // GetOCIStopSignal retrieves the stop signal specified in the OCI image config
    50  func GetOCIStopSignal(ctx context.Context, image Image, defaultSignal string) (string, error) {
    51  	_, err := ParseSignal(defaultSignal)
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  	ic, err := image.Config(ctx)
    56  	if err != nil {
    57  		return "", err
    58  	}
    59  	var (
    60  		ociimage v1.Image
    61  		config   v1.ImageConfig
    62  	)
    63  	switch ic.MediaType {
    64  	case v1.MediaTypeImageConfig, images.MediaTypeDockerSchema2Config:
    65  		p, err := content.ReadBlob(ctx, image.ContentStore(), ic)
    66  		if err != nil {
    67  			return "", err
    68  		}
    69  
    70  		if err := json.Unmarshal(p, &ociimage); err != nil {
    71  			return "", err
    72  		}
    73  		config = ociimage.Config
    74  	default:
    75  		return "", fmt.Errorf("unknown image config media type %s", ic.MediaType)
    76  	}
    77  
    78  	if config.StopSignal == "" {
    79  		return defaultSignal, nil
    80  	}
    81  
    82  	return config.StopSignal, nil
    83  }