signals.go 2.6 KB

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