diff_unix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //go:build !windows
  2. // +build !windows
  3. package chrootarchive // import "github.com/docker/docker/pkg/chrootarchive"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "github.com/containerd/containerd/pkg/userns"
  14. "github.com/docker/docker/pkg/archive"
  15. "github.com/docker/docker/pkg/reexec"
  16. "golang.org/x/sys/unix"
  17. )
  18. type applyLayerResponse struct {
  19. LayerSize int64 `json:"layerSize"`
  20. }
  21. // applyLayer is the entry-point for docker-applylayer on re-exec. This is not
  22. // used on Windows as it does not support chroot, hence no point sandboxing
  23. // through chroot and rexec.
  24. func applyLayer() {
  25. var (
  26. err error
  27. options *archive.TarOptions
  28. )
  29. runtime.LockOSThread()
  30. flag.Parse()
  31. inUserns := userns.RunningInUserNS()
  32. if err := chroot(flag.Arg(0)); err != nil {
  33. fatal(err)
  34. }
  35. // We need to be able to set any perms
  36. oldmask := unix.Umask(0)
  37. defer unix.Umask(oldmask)
  38. if err := json.Unmarshal([]byte(os.Getenv("OPT")), &options); err != nil {
  39. fatal(err)
  40. }
  41. if inUserns {
  42. options.InUserNS = true
  43. }
  44. size, err := archive.UnpackLayer("/", os.Stdin, options)
  45. if err != nil {
  46. fatal(err)
  47. }
  48. encoder := json.NewEncoder(os.Stdout)
  49. if err := encoder.Encode(applyLayerResponse{size}); err != nil {
  50. fatal(fmt.Errorf("unable to encode layerSize JSON: %s", err))
  51. }
  52. if _, err := flush(os.Stdin); err != nil {
  53. fatal(err)
  54. }
  55. os.Exit(0)
  56. }
  57. // applyLayerHandler parses a diff in the standard layer format from `layer`, and
  58. // applies it to the directory `dest`. Returns the size in bytes of the
  59. // contents of the layer.
  60. func applyLayerHandler(dest string, layer io.Reader, options *archive.TarOptions, decompress bool) (size int64, err error) {
  61. dest = filepath.Clean(dest)
  62. if decompress {
  63. decompressed, err := archive.DecompressStream(layer)
  64. if err != nil {
  65. return 0, err
  66. }
  67. defer decompressed.Close()
  68. layer = decompressed
  69. }
  70. if options == nil {
  71. options = &archive.TarOptions{}
  72. if userns.RunningInUserNS() {
  73. options.InUserNS = true
  74. }
  75. }
  76. if options.ExcludePatterns == nil {
  77. options.ExcludePatterns = []string{}
  78. }
  79. data, err := json.Marshal(options)
  80. if err != nil {
  81. return 0, fmt.Errorf("ApplyLayer json encode: %v", err)
  82. }
  83. cmd := reexec.Command("docker-applyLayer", dest)
  84. cmd.Stdin = layer
  85. cmd.Env = append(cmd.Env, fmt.Sprintf("OPT=%s", data))
  86. outBuf, errBuf := new(bytes.Buffer), new(bytes.Buffer)
  87. cmd.Stdout, cmd.Stderr = outBuf, errBuf
  88. // reexec.Command() sets cmd.SysProcAttr.Pdeathsig on Linux, which
  89. // causes the started process to be signaled when the creating OS thread
  90. // dies. Ensure that the reexec is not prematurely signaled. See
  91. // https://go.dev/issue/27505 for more information.
  92. runtime.LockOSThread()
  93. defer runtime.UnlockOSThread()
  94. if err = cmd.Run(); err != nil {
  95. return 0, fmt.Errorf("ApplyLayer %s stdout: %s stderr: %s", err, outBuf, errBuf)
  96. }
  97. // Stdout should be a valid JSON struct representing an applyLayerResponse.
  98. response := applyLayerResponse{}
  99. decoder := json.NewDecoder(outBuf)
  100. if err = decoder.Decode(&response); err != nil {
  101. return 0, fmt.Errorf("unable to decode ApplyLayer JSON response: %s", err)
  102. }
  103. return response.LayerSize, nil
  104. }