2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// holdHijackedConnection handles copying input to and output from streams to the
|
|
|
|
// connection
|
2017-05-02 16:14:43 -04:00
|
|
|
// nolint: gocyclo
|
2016-08-29 14:45:29 -04:00
|
|
|
func holdHijackedConnection(ctx context.Context, streams command.Streams, tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
restoreOnce sync.Once
|
|
|
|
)
|
|
|
|
if inputStream != nil && tty {
|
|
|
|
if err := setRawTerminal(streams); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
restoreOnce.Do(func() {
|
|
|
|
restoreTerminal(streams, inputStream)
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
receiveStdout := make(chan error, 1)
|
|
|
|
if outputStream != nil || errorStream != nil {
|
|
|
|
go func() {
|
|
|
|
// When TTY is ON, use regular copy
|
|
|
|
if tty && outputStream != nil {
|
|
|
|
_, err = io.Copy(outputStream, resp.Reader)
|
|
|
|
// 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() {
|
|
|
|
restoreTerminal(streams, inputStream)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err = stdcopy.StdCopy(outputStream, errorStream, resp.Reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debug("[hijack] End of stdout")
|
|
|
|
receiveStdout <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
stdinDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
if inputStream != nil {
|
|
|
|
io.Copy(resp.Conn, inputStream)
|
|
|
|
// 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() {
|
|
|
|
restoreTerminal(streams, inputStream)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
logrus.Debug("[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 {
|
|
|
|
select {
|
|
|
|
case err := <-receiveStdout:
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error receiveStdout: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-29 14:45:29 -04:00
|
|
|
func setRawTerminal(streams command.Streams) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
if err := streams.In().SetRawTerminal(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return streams.Out().SetRawTerminal()
|
|
|
|
}
|
|
|
|
|
2016-08-29 14:45:29 -04:00
|
|
|
func restoreTerminal(streams command.Streams, in io.Closer) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
streams.In().RestoreTerminal()
|
|
|
|
streams.Out().RestoreTerminal()
|
2017-03-21 18:55:18 -04:00
|
|
|
// WARNING: DO NOT REMOVE THE OS CHECKS !!!
|
2016-09-08 13:11:39 -04:00
|
|
|
// For some reason this Close call blocks on darwin..
|
2017-03-21 18:55:18 -04:00
|
|
|
// As the client exits right after, simply discard the close
|
2016-09-08 13:11:39 -04:00
|
|
|
// until we find a better solution.
|
2017-03-21 18:55:18 -04:00
|
|
|
//
|
|
|
|
// This can also cause the client on Windows to get stuck in Win32 CloseHandle()
|
|
|
|
// in some cases. See https://github.com/docker/docker/issues/28267#issuecomment-288237442
|
|
|
|
// Tracked internally at Microsoft by VSO #11352156. In the
|
|
|
|
// Windows case, you hit this if you are using the native/v2 console,
|
|
|
|
// not the "legacy" console, and you start the client in a new window. eg
|
|
|
|
// `start docker run --rm -it microsoft/nanoserver cmd /s /c echo foobar`
|
|
|
|
// will hang. Remove start, and it won't repro.
|
|
|
|
if in != nil && runtime.GOOS != "darwin" && runtime.GOOS != "windows" {
|
2016-09-08 13:11:39 -04:00
|
|
|
return in.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|