service_unix.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // +build !windows,!linux
  2. /*
  3. Copyright The containerd Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package shim
  15. import (
  16. "context"
  17. "io"
  18. "net/url"
  19. "os"
  20. "sync"
  21. "syscall"
  22. "github.com/containerd/console"
  23. "github.com/containerd/containerd/namespaces"
  24. "github.com/containerd/containerd/pkg/process"
  25. "github.com/containerd/fifo"
  26. "github.com/pkg/errors"
  27. )
  28. type unixPlatform struct {
  29. }
  30. func (p *unixPlatform) CopyConsole(ctx context.Context, console console.Console, id, stdin, stdout, stderr string, wg *sync.WaitGroup) (cons console.Console, retErr error) {
  31. var cwg sync.WaitGroup
  32. if stdin != "" {
  33. in, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
  34. if err != nil {
  35. return nil, err
  36. }
  37. cwg.Add(1)
  38. go func() {
  39. cwg.Done()
  40. p := bufPool.Get().(*[]byte)
  41. defer bufPool.Put(p)
  42. io.CopyBuffer(console, in, *p)
  43. }()
  44. }
  45. uri, err := url.Parse(stdout)
  46. if err != nil {
  47. return nil, errors.Wrap(err, "unable to parse stdout uri")
  48. }
  49. switch uri.Scheme {
  50. case "binary":
  51. ns, err := namespaces.NamespaceRequired(ctx)
  52. if err != nil {
  53. return nil, err
  54. }
  55. cmd := process.NewBinaryCmd(uri, id, ns)
  56. // In case of unexpected errors during logging binary start, close open pipes
  57. var filesToClose []*os.File
  58. defer func() {
  59. if retErr != nil {
  60. process.CloseFiles(filesToClose...)
  61. }
  62. }()
  63. // Create pipe to be used by logging binary for Stdout
  64. outR, outW, err := os.Pipe()
  65. if err != nil {
  66. return nil, errors.Wrap(err, "failed to create stdout pipes")
  67. }
  68. filesToClose = append(filesToClose, outR)
  69. // Stderr is created for logging binary but unused when terminal is true
  70. serrR, _, err := os.Pipe()
  71. if err != nil {
  72. return nil, errors.Wrap(err, "failed to create stderr pipes")
  73. }
  74. filesToClose = append(filesToClose, serrR)
  75. r, w, err := os.Pipe()
  76. if err != nil {
  77. return nil, err
  78. }
  79. filesToClose = append(filesToClose, r)
  80. cmd.ExtraFiles = append(cmd.ExtraFiles, outR, serrR, w)
  81. wg.Add(1)
  82. cwg.Add(1)
  83. go func() {
  84. cwg.Done()
  85. io.Copy(outW, console)
  86. outW.Close()
  87. wg.Done()
  88. }()
  89. if err := cmd.Start(); err != nil {
  90. return nil, errors.Wrap(err, "failed to start logging binary process")
  91. }
  92. // Close our side of the pipe after start
  93. if err := w.Close(); err != nil {
  94. return nil, errors.Wrap(err, "failed to close write pipe after start")
  95. }
  96. // Wait for the logging binary to be ready
  97. b := make([]byte, 1)
  98. if _, err := r.Read(b); err != nil && err != io.EOF {
  99. return nil, errors.Wrap(err, "failed to read from logging binary")
  100. }
  101. cwg.Wait()
  102. default:
  103. outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0)
  104. if err != nil {
  105. return nil, err
  106. }
  107. outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0)
  108. if err != nil {
  109. return nil, err
  110. }
  111. wg.Add(1)
  112. cwg.Add(1)
  113. go func() {
  114. cwg.Done()
  115. p := bufPool.Get().(*[]byte)
  116. defer bufPool.Put(p)
  117. io.CopyBuffer(outw, console, *p)
  118. outw.Close()
  119. outr.Close()
  120. wg.Done()
  121. }()
  122. cwg.Wait()
  123. }
  124. return console, nil
  125. }
  126. func (p *unixPlatform) ShutdownConsole(ctx context.Context, cons console.Console) error {
  127. return nil
  128. }
  129. func (p *unixPlatform) Close() error {
  130. return nil
  131. }
  132. func (s *Service) initPlatform() error {
  133. s.platform = &unixPlatform{}
  134. return nil
  135. }