2014-05-01 20:40:13 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
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"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2014-05-01 20:40:13 -04:00
|
|
|
)
|
|
|
|
|
2015-12-06 00:33:38 -05:00
|
|
|
func (cli *DockerCli) holdHijackedConnection(setRawTerminal bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
|
2015-12-05 22:22:00 -05:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
oldState *term.State
|
|
|
|
)
|
|
|
|
if inputStream != nil && setRawTerminal && cli.isTerminalIn && os.Getenv("NORAW") == "" {
|
|
|
|
oldState, err = term.SetRawTerminal(cli.inFd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer term.RestoreTerminal(cli.inFd, oldState)
|
|
|
|
}
|
|
|
|
|
|
|
|
receiveStdout := make(chan error, 1)
|
|
|
|
if outputStream != nil || errorStream != nil {
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
if inputStream != nil {
|
|
|
|
if setRawTerminal && cli.isTerminalIn {
|
|
|
|
term.RestoreTerminal(cli.inFd, oldState)
|
|
|
|
}
|
|
|
|
inputStream.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// When TTY is ON, use regular copy
|
|
|
|
if setRawTerminal && outputStream != nil {
|
|
|
|
_, err = io.Copy(outputStream, resp.Reader)
|
|
|
|
} else {
|
|
|
|
_, err = stdcopy.StdCopy(outputStream, errorStream, resp.Reader)
|
|
|
|
}
|
|
|
|
logrus.Debugf("[hijack] End of stdout")
|
|
|
|
receiveStdout <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
stdinDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
if inputStream != nil {
|
|
|
|
io.Copy(resp.Conn, inputStream)
|
|
|
|
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 {
|
|
|
|
if err := <-receiveStdout; err != nil {
|
|
|
|
logrus.Debugf("Error receiveStdout: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|