2018-02-05 16:05:59 -05:00
|
|
|
package stream // import "github.com/docker/docker/container/stream"
|
2015-11-17 19:21:44 -05:00
|
|
|
|
|
|
|
import (
|
2019-06-20 16:21:42 -04:00
|
|
|
"context"
|
2015-11-17 19:21:44 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2016-03-18 14:50:19 -04:00
|
|
|
"sync"
|
2015-11-17 19:21:44 -05:00
|
|
|
|
2017-12-07 14:26:27 -05:00
|
|
|
"github.com/containerd/containerd/cio"
|
2015-11-17 19:21:44 -05:00
|
|
|
"github.com/docker/docker/pkg/broadcaster"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2016-10-17 17:39:52 -04:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-11-17 19:21:44 -05:00
|
|
|
)
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
// Config holds information about I/O streams managed together.
|
2015-11-17 19:21:44 -05:00
|
|
|
//
|
2016-11-14 15:15:09 -05:00
|
|
|
// config.StdinPipe returns a WriteCloser which can be used to feed data
|
2015-11-17 19:21:44 -05:00
|
|
|
// to the standard input of the streamConfig's active process.
|
2016-11-14 15:15:09 -05:00
|
|
|
// config.StdoutPipe and streamConfig.StderrPipe each return a ReadCloser
|
2015-11-17 19:21:44 -05:00
|
|
|
// which can be used to retrieve the standard output (and error) generated
|
|
|
|
// by the container's active process. The output (and error) are actually
|
|
|
|
// copied and delivered to all StdoutPipe and StderrPipe consumers, using
|
|
|
|
// a kind of "broadcaster".
|
2016-11-14 15:15:09 -05:00
|
|
|
type Config struct {
|
2019-06-20 16:21:42 -04:00
|
|
|
wg sync.WaitGroup
|
2015-11-17 19:21:44 -05:00
|
|
|
stdout *broadcaster.Unbuffered
|
|
|
|
stderr *broadcaster.Unbuffered
|
|
|
|
stdin io.ReadCloser
|
|
|
|
stdinPipe io.WriteCloser
|
2019-06-20 16:21:42 -04:00
|
|
|
dio *cio.DirectIO
|
2015-11-17 19:21:44 -05:00
|
|
|
}
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
// NewConfig creates a stream config and initializes
|
2015-11-17 19:21:44 -05:00
|
|
|
// the standard err and standard out to new unbuffered broadcasters.
|
2016-11-14 15:15:09 -05:00
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
2015-11-17 19:21:44 -05:00
|
|
|
stderr: new(broadcaster.Unbuffered),
|
|
|
|
stdout: new(broadcaster.Unbuffered),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stdout returns the standard output in the configuration.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) Stdout() *broadcaster.Unbuffered {
|
|
|
|
return c.stdout
|
2015-11-17 19:21:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stderr returns the standard error in the configuration.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) Stderr() *broadcaster.Unbuffered {
|
|
|
|
return c.stderr
|
2015-11-17 19:21:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stdin returns the standard input in the configuration.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) Stdin() io.ReadCloser {
|
|
|
|
return c.stdin
|
2015-11-17 19:21:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// StdinPipe returns an input writer pipe as an io.WriteCloser.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) StdinPipe() io.WriteCloser {
|
|
|
|
return c.stdinPipe
|
2015-11-17 19:21:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// StdoutPipe creates a new io.ReadCloser with an empty bytes pipe.
|
|
|
|
// It adds this new out pipe to the Stdout broadcaster.
|
2017-01-19 11:02:51 -05:00
|
|
|
// This will block stdout if unconsumed.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) StdoutPipe() io.ReadCloser {
|
2016-03-31 12:50:50 -04:00
|
|
|
bytesPipe := ioutils.NewBytesPipe()
|
2016-11-14 15:15:09 -05:00
|
|
|
c.stdout.Add(bytesPipe)
|
2015-11-17 19:21:44 -05:00
|
|
|
return bytesPipe
|
|
|
|
}
|
|
|
|
|
|
|
|
// StderrPipe creates a new io.ReadCloser with an empty bytes pipe.
|
|
|
|
// It adds this new err pipe to the Stderr broadcaster.
|
2017-01-19 11:02:51 -05:00
|
|
|
// This will block stderr if unconsumed.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) StderrPipe() io.ReadCloser {
|
2016-03-31 12:50:50 -04:00
|
|
|
bytesPipe := ioutils.NewBytesPipe()
|
2016-11-14 15:15:09 -05:00
|
|
|
c.stderr.Add(bytesPipe)
|
2015-11-17 19:21:44 -05:00
|
|
|
return bytesPipe
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInputPipes creates new pipes for both standard inputs, Stdin and StdinPipe.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) NewInputPipes() {
|
|
|
|
c.stdin, c.stdinPipe = io.Pipe()
|
2015-11-17 19:21:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNopInputPipe creates a new input pipe that will silently drop all messages in the input.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) NewNopInputPipe() {
|
2021-08-24 06:10:50 -04:00
|
|
|
c.stdinPipe = ioutils.NopWriteCloser(io.Discard)
|
2015-11-17 19:21:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseStreams ensures that the configured streams are properly closed.
|
2016-11-14 15:15:09 -05:00
|
|
|
func (c *Config) CloseStreams() error {
|
2015-11-17 19:21:44 -05:00
|
|
|
var errors []string
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
if c.stdin != nil {
|
|
|
|
if err := c.stdin.Close(); err != nil {
|
2015-11-17 19:21:44 -05:00
|
|
|
errors = append(errors, fmt.Sprintf("error close stdin: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
if err := c.stdout.Clean(); err != nil {
|
2015-11-17 19:21:44 -05:00
|
|
|
errors = append(errors, fmt.Sprintf("error close stdout: %s", err))
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
if err := c.stderr.Clean(); err != nil {
|
2015-11-17 19:21:44 -05:00
|
|
|
errors = append(errors, fmt.Sprintf("error close stderr: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf(strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-17 17:39:52 -04:00
|
|
|
|
|
|
|
// CopyToPipe connects streamconfig with a libcontainerd.IOPipe
|
2017-12-07 14:26:27 -05:00
|
|
|
func (c *Config) CopyToPipe(iop *cio.DirectIO) {
|
2019-06-20 16:21:42 -04:00
|
|
|
c.dio = iop
|
2017-01-12 01:10:50 -05:00
|
|
|
copyFunc := func(w io.Writer, r io.ReadCloser) {
|
2019-06-20 16:21:42 -04:00
|
|
|
c.wg.Add(1)
|
2016-10-17 17:39:52 -04:00
|
|
|
go func() {
|
|
|
|
if _, err := pools.Copy(w, r); err != nil {
|
2017-09-22 09:52:41 -04:00
|
|
|
logrus.Errorf("stream copy error: %v", err)
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
2017-01-12 01:10:50 -05:00
|
|
|
r.Close()
|
2019-06-20 16:21:42 -04:00
|
|
|
c.wg.Done()
|
2016-10-17 17:39:52 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if iop.Stdout != nil {
|
2016-11-14 15:15:09 -05:00
|
|
|
copyFunc(c.Stdout(), iop.Stdout)
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
if iop.Stderr != nil {
|
2016-11-14 15:15:09 -05:00
|
|
|
copyFunc(c.Stderr(), iop.Stderr)
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
if stdin := c.Stdin(); stdin != nil {
|
2016-10-17 17:39:52 -04:00
|
|
|
if iop.Stdin != nil {
|
|
|
|
go func() {
|
|
|
|
pools.Copy(iop.Stdin, stdin)
|
|
|
|
if err := iop.Stdin.Close(); err != nil {
|
2017-09-22 09:52:41 -04:00
|
|
|
logrus.Warnf("failed to close stdin: %v", err)
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-20 16:21:42 -04:00
|
|
|
|
|
|
|
// Wait for the stream to close
|
|
|
|
// Wait supports timeouts via the context to unblock and forcefully
|
|
|
|
// close the io streams
|
|
|
|
func (c *Config) Wait(ctx context.Context) {
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
c.wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-ctx.Done():
|
|
|
|
if c.dio != nil {
|
|
|
|
c.dio.Cancel()
|
|
|
|
c.dio.Wait()
|
|
|
|
c.dio.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|