io.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // +build !windows
  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 proc
  15. import (
  16. "context"
  17. "fmt"
  18. "io"
  19. "os"
  20. "sync"
  21. "syscall"
  22. "github.com/containerd/fifo"
  23. runc "github.com/containerd/go-runc"
  24. )
  25. var bufPool = sync.Pool{
  26. New: func() interface{} {
  27. buffer := make([]byte, 32<<10)
  28. return &buffer
  29. },
  30. }
  31. func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, wg, cwg *sync.WaitGroup) error {
  32. var sameFile io.WriteCloser
  33. for _, i := range []struct {
  34. name string
  35. dest func(wc io.WriteCloser, rc io.Closer)
  36. }{
  37. {
  38. name: stdout,
  39. dest: func(wc io.WriteCloser, rc io.Closer) {
  40. wg.Add(1)
  41. cwg.Add(1)
  42. go func() {
  43. cwg.Done()
  44. p := bufPool.Get().(*[]byte)
  45. defer bufPool.Put(p)
  46. io.CopyBuffer(wc, rio.Stdout(), *p)
  47. wg.Done()
  48. wc.Close()
  49. if rc != nil {
  50. rc.Close()
  51. }
  52. }()
  53. },
  54. }, {
  55. name: stderr,
  56. dest: func(wc io.WriteCloser, rc io.Closer) {
  57. wg.Add(1)
  58. cwg.Add(1)
  59. go func() {
  60. cwg.Done()
  61. p := bufPool.Get().(*[]byte)
  62. defer bufPool.Put(p)
  63. io.CopyBuffer(wc, rio.Stderr(), *p)
  64. wg.Done()
  65. wc.Close()
  66. if rc != nil {
  67. rc.Close()
  68. }
  69. }()
  70. },
  71. },
  72. } {
  73. ok, err := isFifo(i.name)
  74. if err != nil {
  75. return err
  76. }
  77. var (
  78. fw io.WriteCloser
  79. fr io.Closer
  80. )
  81. if ok {
  82. if fw, err = fifo.OpenFifo(ctx, i.name, syscall.O_WRONLY, 0); err != nil {
  83. return fmt.Errorf("containerd-shim: opening %s failed: %s", i.name, err)
  84. }
  85. if fr, err = fifo.OpenFifo(ctx, i.name, syscall.O_RDONLY, 0); err != nil {
  86. return fmt.Errorf("containerd-shim: opening %s failed: %s", i.name, err)
  87. }
  88. } else {
  89. if sameFile != nil {
  90. i.dest(sameFile, nil)
  91. continue
  92. }
  93. if fw, err = os.OpenFile(i.name, syscall.O_WRONLY|syscall.O_APPEND, 0); err != nil {
  94. return fmt.Errorf("containerd-shim: opening %s failed: %s", i.name, err)
  95. }
  96. if stdout == stderr {
  97. sameFile = fw
  98. }
  99. }
  100. i.dest(fw, fr)
  101. }
  102. if stdin == "" {
  103. rio.Stdin().Close()
  104. return nil
  105. }
  106. f, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
  107. if err != nil {
  108. return fmt.Errorf("containerd-shim: opening %s failed: %s", stdin, err)
  109. }
  110. cwg.Add(1)
  111. go func() {
  112. cwg.Done()
  113. p := bufPool.Get().(*[]byte)
  114. defer bufPool.Put(p)
  115. io.CopyBuffer(rio.Stdin(), f, *p)
  116. rio.Stdin().Close()
  117. f.Close()
  118. }()
  119. return nil
  120. }
  121. // isFifo checks if a file is a fifo
  122. // if the file does not exist then it returns false
  123. func isFifo(path string) (bool, error) {
  124. stat, err := os.Stat(path)
  125. if err != nil {
  126. if os.IsNotExist(err) {
  127. return false, nil
  128. }
  129. return false, err
  130. }
  131. if stat.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
  132. return true, nil
  133. }
  134. return false, nil
  135. }