2018-02-05 16:05:59 -05:00
|
|
|
package stream // import "github.com/docker/docker/container/stream"
|
2017-01-19 11:02:51 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-01-19 11:02:51 -05:00
|
|
|
"io"
|
|
|
|
|
2017-06-01 10:02:43 -04:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2020-04-16 05:31:08 -04:00
|
|
|
"github.com/moby/term"
|
2018-05-31 10:01:27 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-31 10:01:27 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-01-19 11:02:51 -05:00
|
|
|
)
|
|
|
|
|
2017-01-21 17:19:33 -05:00
|
|
|
var defaultEscapeSequence = []byte{16, 17} // ctrl-p, ctrl-q
|
|
|
|
|
2017-01-19 11:02:51 -05:00
|
|
|
// AttachConfig is the config struct used to attach a client to a stream's stdio
|
|
|
|
type AttachConfig struct {
|
|
|
|
// Tells the attach copier that the stream's stdin is a TTY and to look for
|
|
|
|
// escape sequences in stdin to detach from the stream.
|
|
|
|
// When true the escape sequence is not passed to the underlying stream
|
|
|
|
TTY bool
|
|
|
|
// Specifies the detach keys the client will be using
|
|
|
|
// Only useful when `TTY` is true
|
|
|
|
DetachKeys []byte
|
|
|
|
|
|
|
|
// CloseStdin signals that once done, stdin for the attached stream should be closed
|
|
|
|
// For example, this would close the attached container's stdin.
|
|
|
|
CloseStdin bool
|
|
|
|
|
2017-01-30 07:49:22 -05:00
|
|
|
// UseStd* indicate whether the client has requested to be connected to the
|
|
|
|
// given stream or not. These flags are used instead of checking Std* != nil
|
|
|
|
// at points before the client streams Std* are wired up.
|
|
|
|
UseStdin, UseStdout, UseStderr bool
|
|
|
|
|
|
|
|
// CStd* are the streams directly connected to the container
|
|
|
|
CStdin io.WriteCloser
|
|
|
|
CStdout, CStderr io.ReadCloser
|
|
|
|
|
2017-01-19 11:02:51 -05:00
|
|
|
// Provide client streams to wire up to
|
|
|
|
Stdin io.ReadCloser
|
|
|
|
Stdout, Stderr io.Writer
|
|
|
|
}
|
|
|
|
|
2017-01-30 07:49:22 -05:00
|
|
|
// AttachStreams attaches the container's streams to the AttachConfig
|
|
|
|
func (c *Config) AttachStreams(cfg *AttachConfig) {
|
|
|
|
if cfg.UseStdin {
|
|
|
|
cfg.CStdin = c.StdinPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.UseStdout {
|
|
|
|
cfg.CStdout = c.StdoutPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.UseStderr {
|
|
|
|
cfg.CStderr = c.StderrPipe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyStreams starts goroutines to copy data in and out to/from the container
|
2017-09-21 20:56:45 -04:00
|
|
|
func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) <-chan error {
|
2018-05-31 10:01:27 -04:00
|
|
|
var group errgroup.Group
|
2017-01-19 11:02:51 -05:00
|
|
|
|
2018-05-31 10:01:27 -04:00
|
|
|
// Connect stdin of container to the attach stdin stream.
|
2017-01-19 11:02:51 -05:00
|
|
|
if cfg.Stdin != nil {
|
2018-05-31 10:01:27 -04:00
|
|
|
group.Go(func() error {
|
|
|
|
logrus.Debug("attach: stdin: begin")
|
|
|
|
defer logrus.Debug("attach: stdin: end")
|
2017-01-19 11:02:51 -05:00
|
|
|
|
2018-05-31 10:01:27 -04:00
|
|
|
defer func() {
|
|
|
|
if cfg.CloseStdin && !cfg.TTY {
|
|
|
|
cfg.CStdin.Close()
|
|
|
|
} else {
|
|
|
|
// No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
|
|
|
|
if cfg.CStdout != nil {
|
|
|
|
cfg.CStdout.Close()
|
|
|
|
}
|
|
|
|
if cfg.CStderr != nil {
|
|
|
|
cfg.CStderr.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-01-19 11:02:51 -05:00
|
|
|
|
2018-05-31 10:01:27 -04:00
|
|
|
var err error
|
|
|
|
if cfg.TTY {
|
|
|
|
_, err = copyEscapable(cfg.CStdin, cfg.Stdin, cfg.DetachKeys)
|
|
|
|
} else {
|
|
|
|
_, err = pools.Copy(cfg.CStdin, cfg.Stdin)
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|
2018-05-31 10:01:27 -04:00
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|
2018-05-31 10:01:27 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Debug("error on attach stdin")
|
|
|
|
return errors.Wrap(err, "error on attach stdin")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2017-01-19 11:02:51 -05:00
|
|
|
|
2018-05-31 10:01:27 -04:00
|
|
|
attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) error {
|
2017-01-19 11:02:51 -05:00
|
|
|
logrus.Debugf("attach: %s: begin", name)
|
2018-05-31 10:01:27 -04:00
|
|
|
defer logrus.Debugf("attach: %s: end", name)
|
|
|
|
defer func() {
|
|
|
|
// Make sure stdin gets closed
|
|
|
|
if cfg.Stdin != nil {
|
|
|
|
cfg.Stdin.Close()
|
|
|
|
}
|
|
|
|
streamPipe.Close()
|
|
|
|
}()
|
|
|
|
|
2017-06-01 10:02:43 -04:00
|
|
|
_, err := pools.Copy(stream, streamPipe)
|
2017-01-19 11:02:51 -05:00
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-05-31 10:01:27 -04:00
|
|
|
logrus.WithError(err).Debugf("attach: %s", name)
|
|
|
|
return errors.Wrapf(err, "error attaching %s stream", name)
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|
2018-05-31 10:01:27 -04:00
|
|
|
return nil
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|
|
|
|
|
2018-05-31 10:01:27 -04:00
|
|
|
if cfg.Stdout != nil {
|
|
|
|
group.Go(func() error {
|
|
|
|
return attachStream("stdout", cfg.Stdout, cfg.CStdout)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if cfg.Stderr != nil {
|
|
|
|
group.Go(func() error {
|
|
|
|
return attachStream("stderr", cfg.Stderr, cfg.CStderr)
|
|
|
|
})
|
|
|
|
}
|
2017-01-19 11:02:51 -05:00
|
|
|
|
2017-09-21 20:56:45 -04:00
|
|
|
errs := make(chan error, 1)
|
|
|
|
go func() {
|
2018-05-31 10:01:27 -04:00
|
|
|
defer logrus.Debug("attach done")
|
|
|
|
groupErr := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
groupErr <- group.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// close all pipes
|
|
|
|
if cfg.CStdin != nil {
|
|
|
|
cfg.CStdin.Close()
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|
2018-05-31 10:01:27 -04:00
|
|
|
if cfg.CStdout != nil {
|
|
|
|
cfg.CStdout.Close()
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|
2018-05-31 10:01:27 -04:00
|
|
|
if cfg.CStderr != nil {
|
|
|
|
cfg.CStderr.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now with these closed, wait should return.
|
|
|
|
if err := group.Wait(); err != nil {
|
|
|
|
errs <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errs <- ctx.Err()
|
|
|
|
case err := <-groupErr:
|
|
|
|
errs <- err
|
|
|
|
}
|
2017-09-21 20:56:45 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
return errs
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|
|
|
|
|
2017-01-21 17:19:33 -05:00
|
|
|
func copyEscapable(dst io.Writer, src io.ReadCloser, keys []byte) (written int64, err error) {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
keys = defaultEscapeSequence
|
|
|
|
}
|
2017-03-30 23:01:41 -04:00
|
|
|
pr := term.NewEscapeProxy(src, keys)
|
2017-01-21 17:19:33 -05:00
|
|
|
defer src.Close()
|
|
|
|
|
2017-06-01 10:02:43 -04:00
|
|
|
return pools.Copy(dst, pr)
|
2017-01-19 11:02:51 -05:00
|
|
|
}
|