attach.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package stream // import "github.com/docker/docker/container/stream"
  2. import (
  3. "context"
  4. "io"
  5. "github.com/containerd/log"
  6. "github.com/docker/docker/pkg/pools"
  7. "github.com/moby/term"
  8. "github.com/pkg/errors"
  9. "golang.org/x/sync/errgroup"
  10. )
  11. var defaultEscapeSequence = []byte{16, 17} // ctrl-p, ctrl-q
  12. // AttachConfig is the config struct used to attach a client to a stream's stdio
  13. type AttachConfig struct {
  14. // Tells the attach copier that the stream's stdin is a TTY and to look for
  15. // escape sequences in stdin to detach from the stream.
  16. // When true the escape sequence is not passed to the underlying stream
  17. TTY bool
  18. // Specifies the detach keys the client will be using
  19. // Only useful when `TTY` is true
  20. DetachKeys []byte
  21. // CloseStdin signals that once done, stdin for the attached stream should be closed
  22. // For example, this would close the attached container's stdin.
  23. CloseStdin bool
  24. // UseStd* indicate whether the client has requested to be connected to the
  25. // given stream or not. These flags are used instead of checking Std* != nil
  26. // at points before the client streams Std* are wired up.
  27. UseStdin, UseStdout, UseStderr bool
  28. // CStd* are the streams directly connected to the container
  29. CStdin io.WriteCloser
  30. CStdout, CStderr io.ReadCloser
  31. // Provide client streams to wire up to
  32. Stdin io.ReadCloser
  33. Stdout, Stderr io.Writer
  34. }
  35. // AttachStreams attaches the container's streams to the AttachConfig
  36. func (c *Config) AttachStreams(cfg *AttachConfig) {
  37. if cfg.UseStdin {
  38. cfg.CStdin = c.StdinPipe()
  39. }
  40. if cfg.UseStdout {
  41. cfg.CStdout = c.StdoutPipe()
  42. }
  43. if cfg.UseStderr {
  44. cfg.CStderr = c.StderrPipe()
  45. }
  46. }
  47. // CopyStreams starts goroutines to copy data in and out to/from the container
  48. func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) <-chan error {
  49. var group errgroup.Group
  50. // Connect stdin of container to the attach stdin stream.
  51. if cfg.Stdin != nil {
  52. group.Go(func() error {
  53. log.G(ctx).Debug("attach: stdin: begin")
  54. defer log.G(ctx).Debug("attach: stdin: end")
  55. defer func() {
  56. if cfg.CloseStdin && !cfg.TTY {
  57. cfg.CStdin.Close()
  58. } else {
  59. // No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
  60. if cfg.CStdout != nil {
  61. cfg.CStdout.Close()
  62. }
  63. if cfg.CStderr != nil {
  64. cfg.CStderr.Close()
  65. }
  66. }
  67. }()
  68. var err error
  69. if cfg.TTY {
  70. _, err = copyEscapable(cfg.CStdin, cfg.Stdin, cfg.DetachKeys)
  71. } else {
  72. _, err = pools.Copy(cfg.CStdin, cfg.Stdin)
  73. }
  74. if err == io.ErrClosedPipe {
  75. err = nil
  76. }
  77. if err != nil {
  78. log.G(ctx).WithError(err).Debug("error on attach stdin")
  79. return errors.Wrap(err, "error on attach stdin")
  80. }
  81. return nil
  82. })
  83. }
  84. attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) error {
  85. log.G(ctx).Debugf("attach: %s: begin", name)
  86. defer log.G(ctx).Debugf("attach: %s: end", name)
  87. defer func() {
  88. // Make sure stdin gets closed
  89. if cfg.Stdin != nil {
  90. cfg.Stdin.Close()
  91. }
  92. streamPipe.Close()
  93. }()
  94. _, err := pools.Copy(stream, streamPipe)
  95. if err == io.ErrClosedPipe {
  96. err = nil
  97. }
  98. if err != nil {
  99. log.G(ctx).WithError(err).Debugf("attach: %s", name)
  100. return errors.Wrapf(err, "error attaching %s stream", name)
  101. }
  102. return nil
  103. }
  104. if cfg.Stdout != nil {
  105. group.Go(func() error {
  106. return attachStream("stdout", cfg.Stdout, cfg.CStdout)
  107. })
  108. }
  109. if cfg.Stderr != nil {
  110. group.Go(func() error {
  111. return attachStream("stderr", cfg.Stderr, cfg.CStderr)
  112. })
  113. }
  114. errs := make(chan error, 1)
  115. go func() {
  116. defer log.G(ctx).Debug("attach done")
  117. groupErr := make(chan error, 1)
  118. go func() {
  119. groupErr <- group.Wait()
  120. }()
  121. select {
  122. case <-ctx.Done():
  123. // close all pipes
  124. if cfg.CStdin != nil {
  125. cfg.CStdin.Close()
  126. }
  127. if cfg.CStdout != nil {
  128. cfg.CStdout.Close()
  129. }
  130. if cfg.CStderr != nil {
  131. cfg.CStderr.Close()
  132. }
  133. // Now with these closed, wait should return.
  134. if err := group.Wait(); err != nil {
  135. errs <- err
  136. return
  137. }
  138. errs <- ctx.Err()
  139. case err := <-groupErr:
  140. errs <- err
  141. }
  142. }()
  143. return errs
  144. }
  145. func copyEscapable(dst io.Writer, src io.ReadCloser, keys []byte) (written int64, err error) {
  146. if len(keys) == 0 {
  147. keys = defaultEscapeSequence
  148. }
  149. pr := term.NewEscapeProxy(src, keys)
  150. defer src.Close()
  151. return pools.Copy(dst, pr)
  152. }