2014-05-01 20:40:13 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-03-24 21:25:50 -04:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2014-05-01 20:40:13 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-09-17 11:04:56 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2014-05-01 20:40:13 -04:00
|
|
|
)
|
|
|
|
|
2016-06-01 01:19:13 -04:00
|
|
|
// HoldHijackedConnection handles copying input to and output from streams to the
|
|
|
|
// connection
|
2016-05-31 19:49:32 -04:00
|
|
|
func (cli *DockerCli) HoldHijackedConnection(ctx context.Context, tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
|
2016-03-24 21:25:50 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
restoreOnce sync.Once
|
|
|
|
)
|
|
|
|
if inputStream != nil && tty {
|
|
|
|
if err := cli.setRawTerminal(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
restoreOnce.Do(func() {
|
|
|
|
cli.restoreTerminal(inputStream)
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
receiveStdout := make(chan error, 1)
|
|
|
|
if outputStream != nil || errorStream != nil {
|
|
|
|
go func() {
|
|
|
|
// When TTY is ON, use regular copy
|
2016-01-20 16:18:36 -05:00
|
|
|
if tty && outputStream != nil {
|
2015-12-05 22:22:00 -05:00
|
|
|
_, err = io.Copy(outputStream, resp.Reader)
|
2016-03-24 21:25:50 -04:00
|
|
|
// we should restore the terminal as soon as possible once connection end
|
|
|
|
// so any following print messages will be in normal type.
|
|
|
|
if inputStream != nil {
|
|
|
|
restoreOnce.Do(func() {
|
|
|
|
cli.restoreTerminal(inputStream)
|
|
|
|
})
|
|
|
|
}
|
2015-12-05 22:22:00 -05:00
|
|
|
} else {
|
|
|
|
_, err = stdcopy.StdCopy(outputStream, errorStream, resp.Reader)
|
|
|
|
}
|
2016-03-24 21:25:50 -04:00
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
logrus.Debugf("[hijack] End of stdout")
|
|
|
|
receiveStdout <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
stdinDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
if inputStream != nil {
|
|
|
|
io.Copy(resp.Conn, inputStream)
|
2016-03-24 21:25:50 -04:00
|
|
|
// we should restore the terminal as soon as possible once connection end
|
|
|
|
// so any following print messages will be in normal type.
|
|
|
|
if tty {
|
|
|
|
restoreOnce.Do(func() {
|
|
|
|
cli.restoreTerminal(inputStream)
|
|
|
|
})
|
|
|
|
}
|
2015-12-05 22:22:00 -05:00
|
|
|
logrus.Debugf("[hijack] End of stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := resp.CloseWrite(); err != nil {
|
|
|
|
logrus.Debugf("Couldn't send EOF: %s", err)
|
|
|
|
}
|
|
|
|
close(stdinDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-receiveStdout:
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error receiveStdout: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-stdinDone:
|
|
|
|
if outputStream != nil || errorStream != nil {
|
2016-03-24 21:25:50 -04:00
|
|
|
select {
|
|
|
|
case err := <-receiveStdout:
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error receiveStdout: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2015-12-05 22:22:00 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-24 21:25:50 -04:00
|
|
|
case <-ctx.Done():
|
2015-12-05 22:22:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|