io_unix.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 cio
  15. import (
  16. "context"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "sync"
  22. "syscall"
  23. "github.com/containerd/fifo"
  24. "github.com/pkg/errors"
  25. )
  26. // NewFIFOSetInDir returns a new FIFOSet with paths in a temporary directory under root
  27. func NewFIFOSetInDir(root, id string, terminal bool) (*FIFOSet, error) {
  28. if root != "" {
  29. if err := os.MkdirAll(root, 0700); err != nil {
  30. return nil, err
  31. }
  32. }
  33. dir, err := ioutil.TempDir(root, "")
  34. if err != nil {
  35. return nil, err
  36. }
  37. closer := func() error {
  38. return os.RemoveAll(dir)
  39. }
  40. return NewFIFOSet(Config{
  41. Stdin: filepath.Join(dir, id+"-stdin"),
  42. Stdout: filepath.Join(dir, id+"-stdout"),
  43. Stderr: filepath.Join(dir, id+"-stderr"),
  44. Terminal: terminal,
  45. }, closer), nil
  46. }
  47. func copyIO(fifos *FIFOSet, ioset *Streams) (*cio, error) {
  48. var ctx, cancel = context.WithCancel(context.Background())
  49. pipes, err := openFifos(ctx, fifos)
  50. if err != nil {
  51. cancel()
  52. return nil, err
  53. }
  54. if fifos.Stdin != "" {
  55. go func() {
  56. p := bufPool.Get().(*[]byte)
  57. defer bufPool.Put(p)
  58. io.CopyBuffer(pipes.Stdin, ioset.Stdin, *p)
  59. pipes.Stdin.Close()
  60. }()
  61. }
  62. var wg = &sync.WaitGroup{}
  63. if fifos.Stdout != "" {
  64. wg.Add(1)
  65. go func() {
  66. p := bufPool.Get().(*[]byte)
  67. defer bufPool.Put(p)
  68. io.CopyBuffer(ioset.Stdout, pipes.Stdout, *p)
  69. pipes.Stdout.Close()
  70. wg.Done()
  71. }()
  72. }
  73. if !fifos.Terminal && fifos.Stderr != "" {
  74. wg.Add(1)
  75. go func() {
  76. p := bufPool.Get().(*[]byte)
  77. defer bufPool.Put(p)
  78. io.CopyBuffer(ioset.Stderr, pipes.Stderr, *p)
  79. pipes.Stderr.Close()
  80. wg.Done()
  81. }()
  82. }
  83. return &cio{
  84. config: fifos.Config,
  85. wg: wg,
  86. closers: append(pipes.closers(), fifos),
  87. cancel: cancel,
  88. }, nil
  89. }
  90. func openFifos(ctx context.Context, fifos *FIFOSet) (f pipes, retErr error) {
  91. defer func() {
  92. if retErr != nil {
  93. fifos.Close()
  94. }
  95. }()
  96. if fifos.Stdin != "" {
  97. if f.Stdin, retErr = fifo.OpenFifo(ctx, fifos.Stdin, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); retErr != nil {
  98. return f, errors.Wrapf(retErr, "failed to open stdin fifo")
  99. }
  100. defer func() {
  101. if retErr != nil && f.Stdin != nil {
  102. f.Stdin.Close()
  103. }
  104. }()
  105. }
  106. if fifos.Stdout != "" {
  107. if f.Stdout, retErr = fifo.OpenFifo(ctx, fifos.Stdout, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); retErr != nil {
  108. return f, errors.Wrapf(retErr, "failed to open stdout fifo")
  109. }
  110. defer func() {
  111. if retErr != nil && f.Stdout != nil {
  112. f.Stdout.Close()
  113. }
  114. }()
  115. }
  116. if !fifos.Terminal && fifos.Stderr != "" {
  117. if f.Stderr, retErr = fifo.OpenFifo(ctx, fifos.Stderr, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); retErr != nil {
  118. return f, errors.Wrapf(retErr, "failed to open stderr fifo")
  119. }
  120. }
  121. return f, nil
  122. }
  123. // NewDirectIO returns an IO implementation that exposes the IO streams as io.ReadCloser
  124. // and io.WriteCloser.
  125. func NewDirectIO(ctx context.Context, fifos *FIFOSet) (*DirectIO, error) {
  126. ctx, cancel := context.WithCancel(ctx)
  127. pipes, err := openFifos(ctx, fifos)
  128. return &DirectIO{
  129. pipes: pipes,
  130. cio: cio{
  131. config: fifos.Config,
  132. closers: append(pipes.closers(), fifos),
  133. cancel: cancel,
  134. },
  135. }, err
  136. }